Skip to content

Commit

Permalink
Add an option to exclude paths from being analyzed
Browse files Browse the repository at this point in the history
Fixes #599

RELNOTES: Add an option to exclude paths from being analyzed

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=175049602
  • Loading branch information
msridhar authored and cushon committed Nov 9, 2017
1 parent 7c9a32a commit 92254ee
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.common.base.Throwables;
import com.google.errorprone.scanner.ErrorProneScannerTransformer;
import com.google.errorprone.scanner.ScannerSupplier;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.Tree;
import com.sun.source.util.TaskEvent;
Expand All @@ -40,6 +41,8 @@
import com.sun.tools.javac.util.PropagatedException;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;
import javax.tools.JavaFileObject;

/** A {@link TaskListener} that runs Error Prone over attributed compilation units. */
@Trusted
Expand Down Expand Up @@ -134,6 +137,9 @@ public void finished(TaskEvent taskEvent) {
DescriptionListener descriptionListener =
descriptionListenerFactory.getDescriptionListener(log, compilation);
try {
if (shouldExcludeSourceFile(compilation.getSourceFile())) {
return;
}
if (path.getLeaf().getKind() == Tree.Kind.COMPILATION_UNIT) {
// 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
Expand All @@ -160,6 +166,13 @@ public void finished(TaskEvent taskEvent) {
}
}

/** Returns true if the given source file should be excluded from analysis. */
private boolean shouldExcludeSourceFile(JavaFileObject sourceFile) {
Pattern excludedPattern = errorProneOptions.getExcludedPattern();
return excludedPattern != null
&& excludedPattern.matcher(ASTHelpers.getFileNameFromUri(sourceFile.toUri())).matches();
}

/** Returns true if all declarations inside the given compilation unit have been visited. */
private boolean finishedCompilation(CompilationUnitTree tree) {
OUTER:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

/**
* Processes command-line options specific to error-prone.
Expand All @@ -56,6 +57,7 @@ public class ErrorProneOptions {
private static final String DISABLE_WARNINGS_IN_GENERATED_CODE_FLAG =
"-XepDisableWarningsInGeneratedCode";
private static final String COMPILING_TEST_ONLY_CODE = "-XepCompilingTestOnlyCode";
private static final String EXCLUDED_PATHS_PREFIX = "-XepExcludedPaths:";

/** see {@link javax.tools.OptionChecker#isSupportedOption(String)} */
public static int isSupportedOption(String option) {
Expand All @@ -64,6 +66,7 @@ public static int isSupportedOption(String option) {
|| option.startsWith(ErrorProneFlags.PREFIX)
|| option.startsWith(PATCH_OUTPUT_LOCATION)
|| option.startsWith(PATCH_CHECKS_PREFIX)
|| option.startsWith(EXCLUDED_PATHS_PREFIX)
|| option.equals(IGNORE_UNKNOWN_CHECKS_FLAG)
|| option.equals(DISABLE_WARNINGS_IN_GENERATED_CODE_FLAG)
|| option.equals(ERRORS_AS_WARNINGS_FLAG)
Expand Down Expand Up @@ -156,6 +159,7 @@ final PatchingOptions build() {
private final boolean isTestOnlyTarget;
private final ErrorProneFlags flags;
private final PatchingOptions patchingOptions;
private final Pattern excludedPattern;

private ErrorProneOptions(
ImmutableMap<String, Severity> severityMap,
Expand All @@ -167,7 +171,8 @@ private ErrorProneOptions(
boolean disableAllChecks,
boolean isTestOnlyTarget,
ErrorProneFlags flags,
PatchingOptions patchingOptions) {
PatchingOptions patchingOptions,
Pattern excludedPattern) {
this.severityMap = severityMap;
this.remainingArgs = remainingArgs;
this.ignoreUnknownChecks = ignoreUnknownChecks;
Expand All @@ -178,6 +183,7 @@ private ErrorProneOptions(
this.isTestOnlyTarget = isTestOnlyTarget;
this.flags = flags;
this.patchingOptions = patchingOptions;
this.excludedPattern = excludedPattern;
}

public String[] getRemainingArgs() {
Expand Down Expand Up @@ -212,6 +218,10 @@ public PatchingOptions patchingOptions() {
return patchingOptions;
}

public Pattern getExcludedPattern() {
return excludedPattern;
}

private static class Builder {
private boolean ignoreUnknownChecks = false;
private boolean disableWarningsInGeneratedCode = false;
Expand All @@ -222,6 +232,7 @@ private static class Builder {
private Map<String, Severity> severityMap = new HashMap<>();
private final ErrorProneFlags.Builder flagsBuilder = ErrorProneFlags.builder();
private final PatchingOptions.Builder patchingOptionsBuilder = PatchingOptions.builder();
private Pattern excludedPattern;

private void parseSeverity(String arg) {
// Strip prefix
Expand Down Expand Up @@ -301,7 +312,12 @@ public ErrorProneOptions build(ImmutableList<String> remainingArgs) {
disableAllChecks,
isTestOnlyTarget,
flagsBuilder.build(),
patchingOptionsBuilder.build());
patchingOptionsBuilder.build(),
excludedPattern);
}

public void setExcludedPattern(Pattern excludedPattern) {
this.excludedPattern = excludedPattern;
}
}

Expand Down Expand Up @@ -391,6 +407,9 @@ public static ErrorProneOptions processArgs(Iterable<String> args) {
String remaining = arg.substring(PATCH_IMPORT_ORDER_PREFIX.length());
ImportOrganizer importOrganizer = ImportOrderParser.getImportOrganizer(remaining);
builder.patchingOptionsBuilder().importOrganizer(importOrganizer);
} else if (arg.startsWith(EXCLUDED_PATHS_PREFIX)) {
String pathRegex = arg.substring(EXCLUDED_PATHS_PREFIX.length());
builder.setExcludedPattern(Pattern.compile(pathRegex));
} else {
remainingArgs.add(arg);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand Down Expand Up @@ -163,6 +164,21 @@ public void recognizesCompilingTestOnlyCode() {
assertThat(options.isTestOnlyTarget()).isTrue();
}

@Test
public void recognizesExcludedPaths() {
ErrorProneOptions options =
ErrorProneOptions.processArgs(
new String[] {"-XepExcludedPaths:(.*/)?(build/generated|other_output)/.*\\.java"});
Pattern excludedPattern = options.getExcludedPattern();
assertThat(excludedPattern).isNotNull();
assertThat(excludedPattern.matcher("fizz/build/generated/Gen.java").matches()).isTrue();
assertThat(excludedPattern.matcher("fizz/bazz/generated/Gen.java").matches()).isFalse();
assertThat(excludedPattern.matcher("fizz/abuild/generated/Gen.java").matches()).isFalse();
assertThat(excludedPattern.matcher("other_output/Gen.java").matches()).isTrue();
assertThat(excludedPattern.matcher("foo/other_output/subdir/Gen.java").matches()).isTrue();
assertThat(excludedPattern.matcher("foo/other_output/subdir/Gen.cpp").matches()).isFalse();
}

@Test
public void recognizesPatch() {
ErrorProneOptions options =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,31 @@ public void testFixGeneratedConstructor() throws Exception {
.contains("AssertionError: Cannot edit synthetic AST nodes");
}

@Test
public void testWithExcludedPaths() throws Exception {
CompilationResult result =
doCompile(
Arrays.asList("bugpatterns/testdata/SelfAssignmentPositiveCases1.java"),
Collections.<String>emptyList(),
Collections.<Class<? extends BugChecker>>emptyList());
assertThat(result.succeeded).isFalse();

result =
doCompile(
Arrays.asList("bugpatterns/testdata/SelfAssignmentPositiveCases1.java"),
Arrays.asList("-XepExcludedPaths:.*/bugpatterns/.*"),
Collections.<Class<? extends BugChecker>>emptyList());
assertThat(result.succeeded).isTrue();

// ensure regexp must match the full path
result =
doCompile(
Arrays.asList("bugpatterns/testdata/SelfAssignmentPositiveCases1.java"),
Arrays.asList("-XepExcludedPaths:bugpatterns"),
Collections.<Class<? extends BugChecker>>emptyList());
assertThat(result.succeeded).isFalse();
}

private static class CompilationResult {
public final boolean succeeded;
public final DiagnosticTestHelper diagnosticHelper;
Expand Down

0 comments on commit 92254ee

Please sign in to comment.