Skip to content

Commit cf47ee1

Browse files
committed
Support src filter in -WConf (Closes #17635)
1 parent c0eae68 commit cf47ee1

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,9 @@ private sealed trait WarningSettings:
247247
| - Message name: name=PureExpressionInStatementPosition
248248
| The message name is printed with the warning in verbose warning mode.
249249
|
250+
| - Source location: src=regex
251+
| The regex is evaluated against the full source path.
252+
|
250253
|In verbose warning mode the compiler prints matching filters for warnings.
251254
|Verbose mode can be enabled globally using `-Wconf:any:verbose`, or locally
252255
|using the @nowarn annotation (example: `@nowarn("v") def test = try 1`).
@@ -266,6 +269,7 @@ private sealed trait WarningSettings:
266269
|Examples:
267270
| - change every warning into an error: -Wconf:any:error
268271
| - silence deprecations: -Wconf:cat=deprecation:s
272+
| - silence warnings in src_managed directory: -Wconf:src=src_managed/.*
269273
|
270274
|Note: on the command-line you might need to quote configurations containing `*` or `&`
271275
|to prevent the shell from expanding patterns.""".stripMargin,

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ package dotc
33
package reporting
44

55
import scala.language.unsafeNulls
6-
7-
import dotty.tools.dotc.core.Contexts._
8-
import dotty.tools.dotc.util.SourcePosition
6+
import dotty.tools.dotc.core.Contexts.*
7+
import dotty.tools.dotc.util.{NoSourcePosition, SourcePosition}
98

109
import java.util.regex.PatternSyntaxException
1110
import scala.annotation.internal.sharable
@@ -21,11 +20,15 @@ enum MessageFilter:
2120
val noHighlight = message.msg.message.replaceAll("\\e\\[[\\d;]*[^\\d;]","")
2221
pattern.findFirstIn(noHighlight).nonEmpty
2322
case MessageID(errorId) => message.msg.errorId == errorId
23+
case SourcePattern(pattern) =>
24+
val path = message.position.orElse(NoSourcePosition).source().path()
25+
pattern.findFirstIn(path).nonEmpty
2426
case None => false
2527

2628
case Any, Deprecated, Feature, Unchecked, None
2729
case MessagePattern(pattern: Regex)
2830
case MessageID(errorId: ErrorMessageID)
31+
case SourcePattern(pattern: Regex)
2932

3033
enum Action:
3134
case Error, Warning, Verbose, Info, Silent
@@ -84,6 +87,9 @@ object WConf:
8487
case "feature" => Right(Feature)
8588
case "unchecked" => Right(Unchecked)
8689
case _ => Left(s"unknown category: $conf")
90+
91+
case "src" => regex(conf).map(SourcePattern.apply)
92+
8793
case _ => Left(s"unknown filter: $filter")
8894
case _ => Left(s"unknown filter: $s")
8995

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

+31
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import org.junit.Test
88
import org.junit.Assert._
99
import core.Decorators.toMessage
1010

11+
import java.net.URI
12+
1113
class ScalaSettingsTests:
1214

1315
@Test def `A setting with aliases is accepted`: Unit =
@@ -96,5 +98,34 @@ class ScalaSettingsTests:
9698
assertEquals(Action.Silent, sut.action(depr))
9799

98100

101+
private def wconfSrcFilterTest(argsStr: String, source: util.SourceFile, expectedAction: reporting.Action): Unit =
102+
import reporting.Diagnostic
103+
val settings = new ScalaSettings
104+
val args = ArgsSummary(settings.defaultState, List(argsStr), errors = Nil, warnings = Nil)
105+
val proc = settings.processArguments(args, processAll = true, skipped = Nil)
106+
val wconfStr = settings.Wconf.valueIn(proc.sstate)
107+
val warning = new Diagnostic.Warning(
108+
"A warning".toMessage,
109+
util.SourcePosition(
110+
source = source,
111+
span = util.Spans.Span(1L)
112+
)
113+
)
114+
val wconf = reporting.WConf.fromSettings(wconfStr)
115+
assertEquals(Right(expectedAction), wconf.map(_.action(warning)))
116+
117+
@Test def `WConf src filter silences warnings from a matching path`: Unit =
118+
wconfSrcFilterTest(
119+
argsStr = "-Wconf:src=path/.*:s",
120+
source = util.SourceFile.virtual(new URI("file:///some/path/file.scala"), ""),
121+
expectedAction = reporting.Action.Silent
122+
)
123+
124+
@Test def `WConf src filter doesn't silence warnings from a non-matching path`: Unit =
125+
wconfSrcFilterTest(
126+
argsStr = "-Wconf:src=another/.*:s",
127+
source = util.SourceFile.virtual(new URI("file:///some/path/file.scala"), ""),
128+
expectedAction = reporting.Action.Warning
129+
)
99130

100131
end ScalaSettingsTests

0 commit comments

Comments
 (0)