Search in sources :

Example 1 with SeverityLevel

use of com.google.errorprone.BugPattern.SeverityLevel in project error-prone by google.

the class ScannerSupplier method applyOverrides.

/**
   * Applies options to this {@link ScannerSupplier}.
   *
   * <p>Command-line options to override check severities may do any of the following:
   * <ul>
   * <li>Enable a check that is currently off</li>
   * <li>Disable a check that is currently on</li>
   * <li>Change the severity of a check that is on, promoting a warning to an error or demoting
   * an error to a warning</li>
   * </ul>
   *
   * @param errorProneOptions an {@link ErrorProneOptions} object that encapsulates the overrides
   * for this compilation
   * @throws InvalidCommandLineOptionException if the override map attempts to disable a check
   * that may not be disabled
   */
@CheckReturnValue
public ScannerSupplier applyOverrides(ErrorProneOptions errorProneOptions) throws InvalidCommandLineOptionException {
    Map<String, Severity> severityOverrides = errorProneOptions.getSeverityMap();
    if (severityOverrides.isEmpty() && !errorProneOptions.isEnableAllChecks() && !errorProneOptions.isDropErrorsToWarnings() && !errorProneOptions.isDisableAllChecks()) {
        return this;
    }
    // Initialize result allChecks map and enabledChecks set with current state of this Supplier.
    ImmutableBiMap<String, BugCheckerInfo> checks = getAllChecks();
    Map<String, SeverityLevel> severities = new LinkedHashMap<>(severities());
    Set<String> disabled = new HashSet<>(disabled());
    if (errorProneOptions.isEnableAllChecks()) {
        disabled.forEach(c -> severities.put(c, checks.get(c).defaultSeverity()));
        disabled.clear();
    }
    if (errorProneOptions.isDropErrorsToWarnings()) {
        getAllChecks().values().stream().filter(c -> c.defaultSeverity() == SeverityLevel.ERROR && c.suppressibility().disableable()).forEach(c -> severities.put(c.canonicalName(), SeverityLevel.WARNING));
    }
    if (errorProneOptions.isDisableAllChecks()) {
        getAllChecks().values().stream().filter(c -> c.suppressibility().disableable()).forEach(c -> disabled.add(c.canonicalName()));
    }
    // Process overrides
    severityOverrides.forEach((checkName, newSeverity) -> {
        BugCheckerInfo check = getAllChecks().get(checkName);
        if (check == null) {
            if (errorProneOptions.ignoreUnknownChecks()) {
                return;
            }
            throw new InvalidCommandLineOptionException(checkName + " is not a valid checker name");
        }
        switch(newSeverity) {
            case OFF:
                if (!check.suppressibility().disableable()) {
                    throw new InvalidCommandLineOptionException(check.canonicalName() + " may not be disabled");
                }
                severities.remove(check.canonicalName());
                disabled.add(check.canonicalName());
                break;
            case DEFAULT:
                severities.put(check.canonicalName(), check.defaultSeverity());
                disabled.remove(check.canonicalName());
                break;
            case WARN:
                // Demoting an enabled check from an error to a warning is a form of disabling
                if (!disabled().contains(check.canonicalName()) && !check.suppressibility().disableable() && check.defaultSeverity() == SeverityLevel.ERROR) {
                    throw new InvalidCommandLineOptionException(check.canonicalName() + " is not disableable and may not be demoted to a warning");
                }
                severities.put(check.canonicalName(), SeverityLevel.WARNING);
                disabled.remove(check.canonicalName());
                break;
            case ERROR:
                severities.put(check.canonicalName(), SeverityLevel.ERROR);
                disabled.remove(check.canonicalName());
                break;
            default:
                throw new IllegalStateException("Unexpected severity level: " + newSeverity);
        }
    });
    return new ScannerSupplierImpl(checks, ImmutableMap.copyOf(severities), ImmutableSet.copyOf(disabled));
}
Also used : Arrays(java.util.Arrays) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Supplier(com.google.common.base.Supplier) BugChecker(com.google.errorprone.bugpatterns.BugChecker) Set(java.util.Set) Severity(com.google.errorprone.ErrorProneOptions.Severity) Sets(com.google.common.collect.Sets) ImmutableBiMap(com.google.common.collect.ImmutableBiMap) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) CheckReturnValue(javax.annotation.CheckReturnValue) ImmutableList(com.google.common.collect.ImmutableList) Predicate(com.google.common.base.Predicate) BugCheckerInfo(com.google.errorprone.BugCheckerInfo) ErrorProneOptions(com.google.errorprone.ErrorProneOptions) Map(java.util.Map) BugPattern(com.google.errorprone.BugPattern) VisibleForTesting(com.google.common.annotations.VisibleForTesting) InvalidCommandLineOptionException(com.google.errorprone.InvalidCommandLineOptionException) SeverityLevel(com.google.errorprone.BugPattern.SeverityLevel) BugCheckerInfo(com.google.errorprone.BugCheckerInfo) InvalidCommandLineOptionException(com.google.errorprone.InvalidCommandLineOptionException) Severity(com.google.errorprone.ErrorProneOptions.Severity) LinkedHashMap(java.util.LinkedHashMap) SeverityLevel(com.google.errorprone.BugPattern.SeverityLevel) HashSet(java.util.HashSet) CheckReturnValue(javax.annotation.CheckReturnValue)

Example 2 with SeverityLevel

use of com.google.errorprone.BugPattern.SeverityLevel in project error-prone by google.

the class VisitorState method reportMatch.

public void reportMatch(Description description) {
    // TODO(cushon): creating Descriptions with the default severity and updating them here isn't
    // ideal (we could forget to do the update), so consider removing severity from Description.
    // Instead, there could be another method on the listener that took a description and a
    // (separate) SeverityLevel. Adding the method to the interface would require updating the
    // existing implementations, though. Wait for default methods?
    SeverityLevel override = severityMap.get(description.checkName);
    if (override != null) {
        description = description.applySeverityOverride(override);
    }
    descriptionListener.onDescribed(description);
}
Also used : SeverityLevel(com.google.errorprone.BugPattern.SeverityLevel)

Example 3 with SeverityLevel

use of com.google.errorprone.BugPattern.SeverityLevel in project error-prone by google.

the class RestrictedApiChecker method checkRestriction.

private Description checkRestriction(@Nullable RestrictedApi restriction, Tree where, VisitorState state) {
    if (restriction == null) {
        return Description.NO_MATCH;
    }
    if (!restriction.allowedOnPath().isEmpty()) {
        JCCompilationUnit compilationUnit = (JCCompilationUnit) state.getPath().getCompilationUnit();
        if (Pattern.matches(restriction.allowedOnPath(), compilationUnit.getSourceFile().toUri().getRawPath())) {
            return Description.NO_MATCH;
        }
    }
    boolean warn = Matchers.enclosingNode(shouldAllowWithWarning(restriction, state)).matches(where, state);
    boolean allow = Matchers.enclosingNode(shouldAllow(restriction, state)).matches(where, state);
    if (warn && allow) {
        // TODO(bangert): Clarify this message if possible.
        return buildDescription(where).setMessage("The Restricted API ([" + restriction.checkerName() + "]" + restriction.explanation() + ") call here is both whitelisted-as-warning and " + "silently whitelisted. " + "Please remove one of the conflicting suppression annotations.").build();
    }
    if (allow) {
        return Description.NO_MATCH;
    }
    SeverityLevel level = warn ? SeverityLevel.WARNING : SeverityLevel.ERROR;
    return Description.builder(where, restriction.checkerName(), restriction.link(), level, restriction.explanation()).build();
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) SeverityLevel(com.google.errorprone.BugPattern.SeverityLevel)

Example 4 with SeverityLevel

use of com.google.errorprone.BugPattern.SeverityLevel in project error-prone by google.

the class ScannerSupplierTest method applyOverridesSetsSeverity.

@Test
public void applyOverridesSetsSeverity() throws Exception {
    ScannerSupplier ss = ScannerSupplier.fromBugCheckerClasses(BadShiftAmount.class, ChainingConstructorIgnoresParameter.class, StringEquality.class);
    ErrorProneOptions epOptions = ErrorProneOptions.processArgs(ImmutableList.of("-Xep:ChainingConstructorIgnoresParameter:WARN", "-Xep:StringEquality:ERROR"));
    ScannerSupplier overriddenScannerSupplier = ss.applyOverrides(epOptions);
    Map<String, SeverityLevel> expected = ImmutableMap.of("BadShiftAmount", SeverityLevel.ERROR, "ChainingConstructorIgnoresParameter", SeverityLevel.WARNING, "StringEquality", SeverityLevel.ERROR);
    assertScanner(overriddenScannerSupplier).hasSeverities(expected);
}
Also used : SeverityLevel(com.google.errorprone.BugPattern.SeverityLevel) ErrorProneOptions(com.google.errorprone.ErrorProneOptions) ErrorProneJavaCompilerTest(com.google.errorprone.ErrorProneJavaCompilerTest) Test(org.junit.Test)

Aggregations

SeverityLevel (com.google.errorprone.BugPattern.SeverityLevel)4 ErrorProneOptions (com.google.errorprone.ErrorProneOptions)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Predicate (com.google.common.base.Predicate)1 Supplier (com.google.common.base.Supplier)1 ImmutableBiMap (com.google.common.collect.ImmutableBiMap)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Sets (com.google.common.collect.Sets)1 BugCheckerInfo (com.google.errorprone.BugCheckerInfo)1 BugPattern (com.google.errorprone.BugPattern)1 ErrorProneJavaCompilerTest (com.google.errorprone.ErrorProneJavaCompilerTest)1 Severity (com.google.errorprone.ErrorProneOptions.Severity)1 InvalidCommandLineOptionException (com.google.errorprone.InvalidCommandLineOptionException)1 BugChecker (com.google.errorprone.bugpatterns.BugChecker)1 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)1 Arrays (java.util.Arrays)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1