Skip to content

Commit

Permalink
Migrate ErrorProneAnalyzer to run with a CodeTransformer instead of a…
Browse files Browse the repository at this point in the history
… Scanner. This is dependent on separate work to obviate the need to run top-level analysis separately.

RELNOTES: none
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=101480168
  • Loading branch information
lowasser authored and cushon committed Sep 14, 2015
1 parent 04732c2 commit 63d00ee
Showing 1 changed file with 16 additions and 26 deletions.
42 changes: 16 additions & 26 deletions core/src/main/java/com/google/errorprone/ErrorProneAnalyzer.java
Expand Up @@ -22,6 +22,7 @@


import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
import com.google.errorprone.scanner.ErrorProneScannerTransformer;
import com.google.errorprone.scanner.Scanner; import com.google.errorprone.scanner.Scanner;


import com.sun.source.tree.CompilationUnitTree; import com.sun.source.tree.CompilationUnitTree;
Expand All @@ -48,7 +49,7 @@ public class ErrorProneAnalyzer implements TaskListener {


public static ErrorProneAnalyzer create(Scanner scanner) { public static ErrorProneAnalyzer create(Scanner scanner) {
checkNotNull(scanner); checkNotNull(scanner);
return new ErrorProneAnalyzer(scanner); return new ErrorProneAnalyzer(ErrorProneScannerTransformer.create(scanner));
} }


/** /**
Expand All @@ -70,7 +71,7 @@ public ErrorProneAnalyzer register(Context context, ErrorProneOptions errorProne
return this; return this;
} }


private final Scanner errorProneScanner; private final CodeTransformer transformer;
// The set of trees that have already been scanned. // The set of trees that have already been scanned.
private final Set<Tree> seen = new HashSet<>(); private final Set<Tree> seen = new HashSet<>();


Expand All @@ -80,8 +81,8 @@ public ErrorProneAnalyzer register(Context context, ErrorProneOptions errorProne
private JavaCompiler compiler; private JavaCompiler compiler;
private boolean initialized = false; private boolean initialized = false;


private ErrorProneAnalyzer(Scanner scanner) { private ErrorProneAnalyzer(CodeTransformer transformer) {
this.errorProneScanner = scanner; this.transformer = checkNotNull(transformer);
} }


@Override @Override
Expand Down Expand Up @@ -117,12 +118,7 @@ public void finished(TaskEvent taskEvent) {
* Returns true if all declarations inside the given compilation unit have been visited. * Returns true if all declarations inside the given compilation unit have been visited.
*/ */
private boolean finishedCompilation(CompilationUnitTree tree) { private boolean finishedCompilation(CompilationUnitTree tree) {
for (Tree type : tree.getTypeDecls()) { return seen.containsAll(tree.getTypeDecls());
if (!seen.contains(type)) {
return false;
}
}
return true;
} }


/** /**
Expand All @@ -135,18 +131,24 @@ public void reportReadyForAnalysis(TaskEvent taskEvent, TreePath path, boolean h
try { try {
// Assert that the event is unique and scan the current tree. // Assert that the event is unique and scan the current tree.
verify(seen.add(path.getLeaf()), "Duplicate FLOW event for: %s", taskEvent.getTypeElement()); verify(seen.add(path.getLeaf()), "Duplicate FLOW event for: %s", taskEvent.getTypeElement());

Context subContext = new SubContext(context);
VisitorState state = createVisitorState(path.getCompilationUnit()); subContext.put(ErrorProneOptions.class, errorProneOptions);
CompilationUnitTree compilation = path.getCompilationUnit();
DescriptionListener logReporter = new JavacErrorDescriptionListener(
log,
((JCCompilationUnit) compilation).endPositions,
compilation.getSourceFile());
if (path.getLeaf().getKind() == Tree.Kind.COMPILATION_UNIT) { if (path.getLeaf().getKind() == Tree.Kind.COMPILATION_UNIT) {
// We only get TaskEvents for compilation units if they contain no package declarations // We only get TaskEvents for compilation units if they contain no package declarations
// (e.g. package-info.java files). In this case it's safe to analyze the // (e.g. package-info.java files). In this case it's safe to analyze the
// CompilationUnitTree immediately. // CompilationUnitTree immediately.
errorProneScanner.scan(path, state); transformer.apply(path, subContext, logReporter);
} else if (finishedCompilation(path.getCompilationUnit())) { } else if (finishedCompilation(path.getCompilationUnit())) {
// Otherwise this TaskEvent is for a ClassTree, and we can scan the whole // Otherwise this TaskEvent is for a ClassTree, and we can scan the whole
// CompilationUnitTree once we've seen all the enclosed classes. // CompilationUnitTree once we've seen all the enclosed classes.
errorProneScanner.scan(new TreePath(path.getCompilationUnit()), state); transformer.apply(new TreePath(compilation), subContext, logReporter);
} }

} catch (CompletionFailure e) { } catch (CompletionFailure e) {
// A CompletionFailure can be triggered when error-prone tries to complete a symbol // A CompletionFailure can be triggered when error-prone tries to complete a symbol
// that isn't on the compilation classpath. This can occur when a check performs an // that isn't on the compilation classpath. This can occur when a check performs an
Expand All @@ -164,16 +166,4 @@ public void reportReadyForAnalysis(TaskEvent taskEvent, TreePath path, boolean h
} }
} }
} }

/**
* Create a VisitorState object from a compilation unit.
*/
private VisitorState createVisitorState(CompilationUnitTree compilation) {
DescriptionListener logReporter = new JavacErrorDescriptionListener(
log,
((JCCompilationUnit) compilation).endPositions,
compilation.getSourceFile());
return new VisitorState(
context, logReporter, errorProneScanner.severityMap(), errorProneOptions);
}
} }

0 comments on commit 63d00ee

Please sign in to comment.