Skip to content

Commit

Permalink
Drop diagnostics that are reported inside ErrorProneTokens
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cushon authored and Error Prone Team committed Feb 14, 2024
1 parent 32312a2 commit a1f4fa7
Showing 1 changed file with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@
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. */
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);
Expand All @@ -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);
}
Expand All @@ -51,13 +54,18 @@ public LineMap getLineMap() {
}

public ImmutableList<ErrorProneToken> getTokens() {
Scanner scanner = new AccessibleScanner(scannerFactory, commentSavingTokenizer);
ImmutableList.Builder<ErrorProneToken> 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<ErrorProneToken> 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. */
Expand Down

0 comments on commit a1f4fa7

Please sign in to comment.