Skip to content

Commit

Permalink
Support src filter in -WConf (Closes scala#18782)
Browse files Browse the repository at this point in the history
  • Loading branch information
povder committed Oct 28, 2023
1 parent c0eae68 commit ccf5ac0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
4 changes: 4 additions & 0 deletions compiler/src/dotty/tools/dotc/config/ScalaSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ private sealed trait WarningSettings:
| - Message name: name=PureExpressionInStatementPosition
| The message name is printed with the warning in verbose warning mode.
|
| - Source location: src=regex
| The regex is evaluated against the full source path.
|
|In verbose warning mode the compiler prints matching filters for warnings.
|Verbose mode can be enabled globally using `-Wconf:any:verbose`, or locally
|using the @nowarn annotation (example: `@nowarn("v") def test = try 1`).
Expand All @@ -266,6 +269,7 @@ private sealed trait WarningSettings:
|Examples:
| - change every warning into an error: -Wconf:any:error
| - silence deprecations: -Wconf:cat=deprecation:s
| - silence warnings in src_managed directory: -Wconf:src=src_managed/.*
|
|Note: on the command-line you might need to quote configurations containing `*` or `&`
|to prevent the shell from expanding patterns.""".stripMargin,
Expand Down
12 changes: 9 additions & 3 deletions compiler/src/dotty/tools/dotc/reporting/WConf.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package dotc
package reporting

import scala.language.unsafeNulls

import dotty.tools.dotc.core.Contexts._
import dotty.tools.dotc.util.SourcePosition
import dotty.tools.dotc.core.Contexts.*
import dotty.tools.dotc.util.{NoSourcePosition, SourcePosition}

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

case Any, Deprecated, Feature, Unchecked, None
case MessagePattern(pattern: Regex)
case MessageID(errorId: ErrorMessageID)
case SourcePattern(pattern: Regex)

enum Action:
case Error, Warning, Verbose, Info, Silent
Expand Down Expand Up @@ -84,6 +87,9 @@ object WConf:
case "feature" => Right(Feature)
case "unchecked" => Right(Unchecked)
case _ => Left(s"unknown category: $conf")

case "src" => regex(conf).map(SourcePattern.apply)

case _ => Left(s"unknown filter: $filter")
case _ => Left(s"unknown filter: $s")

Expand Down
31 changes: 31 additions & 0 deletions compiler/test/dotty/tools/dotc/config/ScalaSettingsTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import org.junit.Test
import org.junit.Assert._
import core.Decorators.toMessage

import java.net.URI

class ScalaSettingsTests:

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


private def wconfSrcFilterTest(argsStr: String, source: util.SourceFile, expectedAction: reporting.Action): Unit =
import reporting.Diagnostic
val settings = new ScalaSettings
val args = ArgsSummary(settings.defaultState, List(argsStr), errors = Nil, warnings = Nil)
val proc = settings.processArguments(args, processAll = true, skipped = Nil)
val wconfStr = settings.Wconf.valueIn(proc.sstate)
val warning = new Diagnostic.Warning(
"A warning".toMessage,
util.SourcePosition(
source = source,
span = util.Spans.Span(1L)
)
)
val wconf = reporting.WConf.fromSettings(wconfStr)
assertEquals(Right(expectedAction), wconf.map(_.action(warning)))

@Test def `WConf src filter silences warnings from a matching path`: Unit =
wconfSrcFilterTest(
argsStr = "-Wconf:src=path/.*:s",
source = util.SourceFile.virtual(new URI("file:///some/path/file.scala"), ""),
expectedAction = reporting.Action.Silent
)

@Test def `WConf src filter doesn't silence warnings from a non-matching path`: Unit =
wconfSrcFilterTest(
argsStr = "-Wconf:src=another/.*:s",
source = util.SourceFile.virtual(new URI("file:///some/path/file.scala"), ""),
expectedAction = reporting.Action.Warning
)

end ScalaSettingsTests

0 comments on commit ccf5ac0

Please sign in to comment.