Skip to content

Commit

Permalink
allow filtering messages by error id
Browse files Browse the repository at this point in the history
  • Loading branch information
lrytz committed Aug 16, 2021
1 parent a5345dd commit 1164e85
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
5 changes: 4 additions & 1 deletion compiler/src/dotty/tools/dotc/config/ScalaSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,13 @@ private sealed trait WarningSettings:
| - Message content: msg=regex
| The regex need only match some part of the message, not all of it.
|
| - Message id: id=E129
| The message id is printed with the warning.
|
|<action>
| - error / e
| - warning / w
| - info / i (infos are not counted as warnings and don't affect `-Werror`)
| - info / i (infos are not counted as warnings and not affected by `-Werror`)
| - silent / s
|
|The default configuration is empty.
Expand Down
37 changes: 25 additions & 12 deletions compiler/src/dotty/tools/dotc/reporting/WConf.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import dotty.tools.dotc.core.Contexts._
import dotty.tools.dotc.util.SourcePosition

import java.util.regex.PatternSyntaxException
import scala.annotation.internal.sharable
import scala.collection.mutable.ListBuffer
import scala.util.matching.Regex

Expand All @@ -17,10 +18,12 @@ enum MessageFilter:
case MessagePattern(pattern) =>
val noHighlight = message.msg.rawMessage.replaceAll("\\e\\[[\\d;]*[^\\d;]","")
pattern.findFirstIn(noHighlight).nonEmpty
case MessageID(errorId) => message.msg.errorId == errorId
case None => false
}
case Any, Deprecated, Feature, None
case MessagePattern(pattern: Regex)
case MessageID(errorId: ErrorMessageID)

enum Action:
case Error, Warning, Info, Silent
Expand Down Expand Up @@ -48,18 +51,28 @@ object WConf:
try Right(s.r)
catch { case e: PatternSyntaxException => Left(s"invalid pattern `$s`: ${e.getMessage}") }

def parseFilter(s: String): Either[String, MessageFilter] =
val splitter = raw"([^=]+)=(.+)".r
s match
case "any" => Right(Any)
case splitter(filter, conf) => filter match
case "msg" => regex(conf).map(MessagePattern.apply)
case "cat" => conf match
case "deprecation" => Right(Deprecated)
case "feature" => Right(Feature)
case _ => Left(s"unknown category: $conf")
case _ => Left(s"unknown filter: $filter")
case _ => Left(s"unknown filter: $s")
@sharable val Splitter = raw"([^=]+)=(.+)".r
@sharable val ErrorId = raw"E?(\d+)".r

def parseFilter(s: String): Either[String, MessageFilter] = s match
case "any" => Right(Any)
case Splitter(filter, conf) => filter match
case "msg" => regex(conf).map(MessagePattern.apply)
case "id" => conf match
case ErrorId(num) =>
val n = num.toInt + 2
if n < ErrorMessageID.values.length then
Right(MessageID(ErrorMessageID.fromOrdinal(n)))
else
Left(s"unknonw error message id: E$n")
case _ =>
Left(s"invalid error message id: $conf")
case "cat" => conf match
case "deprecation" => Right(Deprecated)
case "feature" => Right(Feature)
case _ => Left(s"unknown category: $conf")
case _ => Left(s"unknown filter: $filter")
case _ => Left(s"unknown filter: $s")

def parsed(using Context): WConf =
val setting = ctx.settings.Wconf.value
Expand Down
7 changes: 7 additions & 0 deletions tests/neg-custom-args/nowarn/nowarn-parser-typer.check
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
| A try without catch or finally is equivalent to putting
| its body in a block; no exceptions are handled.

longer explanation available when compiling with `-explain`
-- [E000] Syntax Error: tests/neg-custom-args/nowarn/nowarn-parser-typer.scala:22:26 -----------------------------------
22 |@nowarn("id=1") def t5d = try 1 // error, wrong id
| ^^^^^
| A try without catch or finally is equivalent to putting
| its body in a block; no exceptions are handled.

longer explanation available when compiling with `-explain`
-- [E129] Potential Issue Error: tests/neg-custom-args/nowarn/nowarn-parser-typer.scala:16:11 --------------------------
16 |def t3 = { 1; 2 } // error, the invalid nowarn doesn't silence this warning
Expand Down
6 changes: 6 additions & 0 deletions tests/neg-custom-args/nowarn/nowarn-parser-typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ def t2 = f // not reported because refchecks doesn't run

@nowarn("wat?") // error, invalid filter
def t3 = { 1; 2 } // error, the invalid nowarn doesn't silence this warning

@nowarn("id=E129") def t4 = { 1; 2 }
@nowarn("id=E000") def t5a = try 1
@nowarn("id=E0") def t5b = try 1
@nowarn("id=0") def t5c = try 1
@nowarn("id=1") def t5d = try 1 // error, wrong id

0 comments on commit 1164e85

Please sign in to comment.