Skip to content

Commit a1f4fa7

Browse files
cushonError Prone Team
authored andcommitted
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
1 parent 32312a2 commit a1f4fa7

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

check_api/src/main/java/com/google/errorprone/util/ErrorProneTokens.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@
2727
import com.sun.tools.javac.parser.Tokens.TokenKind;
2828
import com.sun.tools.javac.parser.UnicodeReader;
2929
import com.sun.tools.javac.util.Context;
30+
import com.sun.tools.javac.util.Log;
3031
import com.sun.tools.javac.util.Position.LineMap;
3132

3233
/** A utility for tokenizing and preserving comments. */
3334
public class ErrorProneTokens {
3435
private final int offset;
3536
private final CommentSavingTokenizer commentSavingTokenizer;
3637
private final ScannerFactory scannerFactory;
38+
private final Log log;
3739

3840
public ErrorProneTokens(String source, Context context) {
3941
this(source, 0, context);
@@ -42,6 +44,7 @@ public ErrorProneTokens(String source, Context context) {
4244
public ErrorProneTokens(String source, int offset, Context context) {
4345
this.offset = offset;
4446
scannerFactory = ScannerFactory.instance(context);
47+
log = Log.instance(context);
4548
char[] buffer = source == null ? new char[] {} : source.toCharArray();
4649
commentSavingTokenizer = new CommentSavingTokenizer(scannerFactory, buffer, buffer.length);
4750
}
@@ -51,13 +54,18 @@ public LineMap getLineMap() {
5154
}
5255

5356
public ImmutableList<ErrorProneToken> getTokens() {
54-
Scanner scanner = new AccessibleScanner(scannerFactory, commentSavingTokenizer);
55-
ImmutableList.Builder<ErrorProneToken> tokens = ImmutableList.builder();
56-
do {
57-
scanner.nextToken();
58-
tokens.add(new ErrorProneToken(scanner.token(), offset));
59-
} while (scanner.token().kind != TokenKind.EOF);
60-
return tokens.build();
57+
Log.DiagnosticHandler diagHandler = new Log.DiscardDiagnosticHandler(log);
58+
try {
59+
Scanner scanner = new AccessibleScanner(scannerFactory, commentSavingTokenizer);
60+
ImmutableList.Builder<ErrorProneToken> tokens = ImmutableList.builder();
61+
do {
62+
scanner.nextToken();
63+
tokens.add(new ErrorProneToken(scanner.token(), offset));
64+
} while (scanner.token().kind != TokenKind.EOF);
65+
return tokens.build();
66+
} finally {
67+
log.popDiagnosticHandler(diagHandler);
68+
}
6169
}
6270

6371
/** Returns the tokens for the given source text, including comments. */

0 commit comments

Comments
 (0)