Skip to content

Commit

Permalink
Simplify the syntax for -AresourceLeakIgnoredExceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Calvin-L committed Oct 23, 2023
1 parent 9af1d93 commit 0e0fa16
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,11 @@ public ResourceLeakChecker() {}

/**
* A pattern that matches an exception specifier for the {@link #IGNORED_EXCEPTIONS} option: an
* optional "? extends" followed by a qualified name. The whole thing can be padded with
* whitespace.
* optional "=" followed by a qualified name. The whole thing can be padded with whitespace.
*/
private static final Pattern EXCEPTION_SPECIFIER =
Pattern.compile(
"^\\s*"
+ "("
+ Pattern.quote("?")
+ "\\s+extends\\s+"
+ ")?"
+ "(\\w+(?:\\.\\w+)*)"
+ "\\s*$");
"^\\s*" + "(" + Pattern.quote("=") + "\\s*" + ")?" + "(\\w+(?:\\.\\w+)*)" + "\\s*$");

/**
* The number of expressions with must-call obligations that were checked. Incremented only if the
Expand Down Expand Up @@ -233,7 +226,7 @@ protected SetOfTypes parseIgnoredExceptions(String ignoredExceptionsOptionValue)
String exceptionSpecifier, String ignoredExceptionsOptionValue) {
Matcher m = EXCEPTION_SPECIFIER.matcher(exceptionSpecifier);
if (m.matches()) {
@Nullable String questionMarkExtendsClause = m.group(1);
@Nullable String equalsSign = m.group(1);
String qualifiedName = m.group(2);

if (qualifiedName.equalsIgnoreCase("default")) {
Expand All @@ -246,8 +239,8 @@ protected SetOfTypes parseIgnoredExceptions(String ignoredExceptionsOptionValue)
// ignored anyway (in case it's just an inaccessible type).
//
// Note that if the user asked to ignore subtypes of this exception, this code won't do it
// because we can't know what those subtypes are. We have to ignore the
// `questionMarkExtendsClause` if it was provided.
// because we can't know what those subtypes are. We have to treat this as if it were
// "=qualifiedName" even if no equals sign was provided.
message(
Diagnostic.Kind.WARNING,
"The exception '%s' appears in the -A%s=%s option, but it does not seem to exist",
Expand All @@ -256,9 +249,7 @@ protected SetOfTypes parseIgnoredExceptions(String ignoredExceptionsOptionValue)
ignoredExceptionsOptionValue);
return SetOfTypes.anyOfTheseNames(ImmutableSet.of(qualifiedName));
} else {
return questionMarkExtendsClause != null
? SetOfTypes.allSubtypes(type)
: SetOfTypes.singleton(type);
return equalsSign == null ? SetOfTypes.allSubtypes(type) : SetOfTypes.singleton(type);
}
} else if (!exceptionSpecifier.trim().isEmpty()) {
message(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public ResourceLeakCustomIgnoredExceptionsTest(List<File> testFiles) {
testFiles,
ResourceLeakChecker.class,
"resourceleak-customignoredexceptions",
"-AresourceLeakIgnoredExceptions=? extends java.lang.Error, java.lang.NullPointerException",
"-AresourceLeakIgnoredExceptions=java.lang.Error, =java.lang.NullPointerException",
"-AwarnUnneededSuppressions",
"-encoding",
"UTF-8");
Expand Down
15 changes: 15 additions & 0 deletions checker/tests/resourceleak-customignoredexceptions/BasicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,19 @@ public void ignoreNPE() throws IOException {
}
r.close();
}

static class CustomNPESubtype extends NullPointerException {
static final CustomNPESubtype INSTANCE = new CustomNPESubtype();
}

public void doNotIgnoreNPESubtype() throws IOException {
// Only NullPointerException should be ignored, not its subtypes, since the options
// specified "=java.lang.NullPointerException".
// ::error: (required.method.not.called)
Closeable r = alloc();
if (true) {
throw CustomNPESubtype.INSTANCE;
}
r.close();
}
}
7 changes: 4 additions & 3 deletions docs/manual/resource-leak-checker.tex
Original file line number Diff line number Diff line change
Expand Up @@ -514,11 +514,12 @@

The set of ignored exception types can be controlled with the option
\<-AresourceLeakIgnoredExceptions=...>. The option takes a comma-separated list of
fully-qualified exception types. It can also include \<? extends TYPE> to ignore all
subtypes of an exception. For example, for a very pedantic set of ignored exceptions use:
fully-qualified exception types. A type can be prefixed with \<=> to ignore exactly
that type and not its subclasses. For example, for a very pedantic set of ignored
exceptions use:

\begin{verbatim}
-AresourceLeakIgnoredExceptions=? extends java.lang.Error, java.lang.NullPointerException
-AresourceLeakIgnoredExceptions=java.lang.Error, =java.lang.NullPointerException
\end{verbatim}

which ignores \<java.lang.Error> (and all its subclasses) as well as
Expand Down

0 comments on commit 0e0fa16

Please sign in to comment.