From a1f4fa7061e977357a9ada2dbc71c36ace2fe7df Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Wed, 14 Feb 2024 09:39:23 -0800 Subject: [PATCH] Drop diagnostics that are reported inside ErrorProneTokens javac's tokenize now emits some diagnostics (including the `text-blocks` lint warnings), and this can result in ErrorProneTokens causing bogus diagnostics to be logged. PiperOrigin-RevId: 607014734 --- .../errorprone/util/ErrorProneTokens.java | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/check_api/src/main/java/com/google/errorprone/util/ErrorProneTokens.java b/check_api/src/main/java/com/google/errorprone/util/ErrorProneTokens.java index f9ba35232fc..22601df048a 100644 --- a/check_api/src/main/java/com/google/errorprone/util/ErrorProneTokens.java +++ b/check_api/src/main/java/com/google/errorprone/util/ErrorProneTokens.java @@ -27,6 +27,7 @@ import com.sun.tools.javac.parser.Tokens.TokenKind; import com.sun.tools.javac.parser.UnicodeReader; import com.sun.tools.javac.util.Context; +import com.sun.tools.javac.util.Log; import com.sun.tools.javac.util.Position.LineMap; /** A utility for tokenizing and preserving comments. */ @@ -34,6 +35,7 @@ public class ErrorProneTokens { private final int offset; private final CommentSavingTokenizer commentSavingTokenizer; private final ScannerFactory scannerFactory; + private final Log log; public ErrorProneTokens(String source, Context context) { this(source, 0, context); @@ -42,6 +44,7 @@ public ErrorProneTokens(String source, Context context) { public ErrorProneTokens(String source, int offset, Context context) { this.offset = offset; scannerFactory = ScannerFactory.instance(context); + log = Log.instance(context); char[] buffer = source == null ? new char[] {} : source.toCharArray(); commentSavingTokenizer = new CommentSavingTokenizer(scannerFactory, buffer, buffer.length); } @@ -51,13 +54,18 @@ public LineMap getLineMap() { } public ImmutableList getTokens() { - Scanner scanner = new AccessibleScanner(scannerFactory, commentSavingTokenizer); - ImmutableList.Builder tokens = ImmutableList.builder(); - do { - scanner.nextToken(); - tokens.add(new ErrorProneToken(scanner.token(), offset)); - } while (scanner.token().kind != TokenKind.EOF); - return tokens.build(); + Log.DiagnosticHandler diagHandler = new Log.DiscardDiagnosticHandler(log); + try { + Scanner scanner = new AccessibleScanner(scannerFactory, commentSavingTokenizer); + ImmutableList.Builder tokens = ImmutableList.builder(); + do { + scanner.nextToken(); + tokens.add(new ErrorProneToken(scanner.token(), offset)); + } while (scanner.token().kind != TokenKind.EOF); + return tokens.build(); + } finally { + log.popDiagnosticHandler(diagHandler); + } } /** Returns the tokens for the given source text, including comments. */