Skip to content

Commit d65e8f8

Browse files
committed
Support src filter in -WConf (Closes #18782)
1 parent c7a0459 commit d65e8f8

File tree

3 files changed

+118
-1
lines changed

3 files changed

+118
-1
lines changed

Diff for: compiler/src/dotty/tools/dotc/config/ScalaSettings.scala

+4
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ private sealed trait WarningSettings:
252252
| - Message name: name=PureExpressionInStatementPosition
253253
| The message name is printed with the warning in verbose warning mode.
254254
|
255+
| - Source location: src=regex
256+
| The regex is evaluated against the full source path.
257+
|
255258
|In verbose warning mode the compiler prints matching filters for warnings.
256259
|Verbose mode can be enabled globally using `-Wconf:any:verbose`, or locally
257260
|using the @nowarn annotation (example: `@nowarn("v") def test = try 1`).
@@ -271,6 +274,7 @@ private sealed trait WarningSettings:
271274
|Examples:
272275
| - change every warning into an error: -Wconf:any:error
273276
| - silence deprecations: -Wconf:cat=deprecation:s
277+
| - silence warnings in src_managed directory: -Wconf:src=src_managed/.*:s
274278
|
275279
|Note: on the command-line you might need to quote configurations containing `*` or `&`
276280
|to prevent the shell from expanding patterns.""".stripMargin,

Diff for: compiler/src/dotty/tools/dotc/reporting/WConf.scala

+14-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ package reporting
55
import scala.language.unsafeNulls
66

77
import dotty.tools.dotc.core.Contexts.*
8-
import dotty.tools.dotc.util.SourcePosition
8+
import dotty.tools.dotc.util.{NoSourcePosition, SourcePosition}
9+
import dotty.tools.dotc.interfaces.SourceFile
10+
import dotty.tools.dotc.reporting.MessageFilter.SourcePattern
911

1012
import java.util.regex.PatternSyntaxException
1113
import scala.annotation.internal.sharable
@@ -21,11 +23,19 @@ enum MessageFilter:
2123
val noHighlight = message.msg.message.replaceAll("\\e\\[[\\d;]*[^\\d;]","")
2224
pattern.findFirstIn(noHighlight).nonEmpty
2325
case MessageID(errorId) => message.msg.errorId == errorId
26+
case SourcePattern(pattern) =>
27+
val source = message.position.orElse(NoSourcePosition).source()
28+
val path = source.jfile()
29+
.map(_.toPath.toAbsolutePath.toUri.normalize().getRawPath)
30+
.orElse(source.path())
31+
pattern.findFirstIn(path).nonEmpty
32+
2433
case None => false
2534

2635
case Any, Deprecated, Feature, Unchecked, None
2736
case MessagePattern(pattern: Regex)
2837
case MessageID(errorId: ErrorMessageID)
38+
case SourcePattern(pattern: Regex)
2939

3040
enum Action:
3141
case Error, Warning, Verbose, Info, Silent
@@ -84,6 +94,9 @@ object WConf:
8494
case "feature" => Right(Feature)
8595
case "unchecked" => Right(Unchecked)
8696
case _ => Left(s"unknown category: $conf")
97+
98+
case "src" => regex(conf).map(SourcePattern.apply)
99+
87100
case _ => Left(s"unknown filter: $filter")
88101
case _ => Left(s"unknown filter: $s")
89102

Diff for: compiler/test/dotty/tools/dotc/config/ScalaSettingsTests.scala

+100
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import Settings._
77
import org.junit.Test
88
import org.junit.Assert._
99
import core.Decorators.toMessage
10+
import dotty.tools.io.{Path, PlainFile}
11+
12+
import java.net.URI
13+
import java.nio.file.Files
14+
import scala.util.Using
1015

1116
class ScalaSettingsTests:
1217

@@ -96,5 +101,100 @@ class ScalaSettingsTests:
96101
assertEquals(Action.Silent, sut.action(depr))
97102

98103

104+
private def wconfSrcFilterTest(argsStr: String,
105+
warning: reporting.Diagnostic.Warning): Either[List[String], reporting.Action] =
106+
import reporting.Diagnostic
107+
val settings = new ScalaSettings
108+
val args = ArgsSummary(settings.defaultState, List(argsStr), errors = Nil, warnings = Nil)
109+
val proc = settings.processArguments(args, processAll = true, skipped = Nil)
110+
val wconfStr = settings.Wconf.valueIn(proc.sstate)
111+
val wconf = reporting.WConf.fromSettings(wconfStr)
112+
wconf.map(_.action(warning))
113+
114+
@Test def `WConf src filter silences warnings from a matching path for virtual file`: Unit =
115+
val result = wconfSrcFilterTest(
116+
argsStr = "-Wconf:src=path/.*:s",
117+
warning = reporting.Diagnostic.Warning(
118+
"A warning".toMessage,
119+
util.SourcePosition(
120+
source = util.SourceFile.virtual(new URI("file:///some/path/file.scala"), ""),
121+
span = util.Spans.Span(1L)
122+
)
123+
)
124+
)
125+
assertEquals(result, Right(reporting.Action.Silent))
126+
127+
@Test def `WConf src filter doesn't silence warnings from a non-matching path`: Unit =
128+
val result = wconfSrcFilterTest(
129+
argsStr = "-Wconf:src=another/.*:s",
130+
warning = reporting.Diagnostic.Warning(
131+
"A warning".toMessage,
132+
util.SourcePosition(
133+
source = util.SourceFile.virtual(new URI("file:///some/path/file.scala"), ""),
134+
span = util.Spans.Span(1L)
135+
)
136+
)
137+
)
138+
assertEquals(result, Right(reporting.Action.Warning))
139+
140+
@Test def `WConf src filter silences warnings from a matching path for real file`: Unit =
141+
val result = Using.resource(Files.createTempFile("myfile", ".scala").nn) { file =>
142+
wconfSrcFilterTest(
143+
argsStr = "-Wconf:src=myfile.*?\\.scala:s",
144+
warning = reporting.Diagnostic.Warning(
145+
"A warning".toMessage,
146+
util.SourcePosition(
147+
source = util.SourceFile(new PlainFile(Path(file)), "UTF-8"),
148+
span = util.Spans.Span(1L)
149+
)
150+
)
151+
)
152+
}(Files.deleteIfExists(_))
153+
assertEquals(result, Right(reporting.Action.Silent))
154+
155+
@Test def `WConf src filter doesn't silence warnings from a non-matching path for real file`: Unit =
156+
val result = Using.resource(Files.createTempFile("myfile", ".scala").nn) { file =>
157+
wconfSrcFilterTest(
158+
argsStr = "-Wconf:src=another.*?\\.scala:s",
159+
warning = reporting.Diagnostic.Warning(
160+
"A warning".toMessage,
161+
util.SourcePosition(
162+
source = util.SourceFile(new PlainFile(Path(file)), "UTF-8"),
163+
span = util.Spans.Span(1L)
164+
)
165+
)
166+
)
167+
}(Files.deleteIfExists(_))
168+
assertEquals(result, Right(reporting.Action.Warning))
169+
170+
@Test def `WConf src filter reports an error on an invalid regex`: Unit =
171+
val result = wconfSrcFilterTest(
172+
argsStr = """-Wconf:src=\:s""",
173+
warning = reporting.Diagnostic.Warning(
174+
"A warning".toMessage,
175+
util.SourcePosition(
176+
source = util.SourceFile.virtual(new URI("file:///some/path/file.scala"), ""),
177+
span = util.Spans.Span(1L)
178+
)
179+
),
180+
)
181+
assertTrue(
182+
result.left.exists(errors =>
183+
errors.sizeIs == 1 && errors.headOption.exists(_.startsWith("invalid pattern"))
184+
)
185+
)
186+
187+
@Test def `WConf src filter can be mixed with other filters with rightmost taking precedence`: Unit =
188+
val result = wconfSrcFilterTest(
189+
argsStr = "-Wconf:src=.*:s,cat=deprecation:e",
190+
warning = reporting.Diagnostic.DeprecationWarning(
191+
"A warning".toMessage,
192+
util.SourcePosition(
193+
source = util.SourceFile.virtual(new URI("file:///some/path/file.scala"), ""),
194+
span = util.Spans.Span(1L)
195+
)
196+
)
197+
)
198+
assertEquals(result, Right(reporting.Action.Error))
99199

100200
end ScalaSettingsTests

0 commit comments

Comments
 (0)