Skip to content

Commit caf7ebc

Browse files
committed
Java/Kotlin: Add a diagnostic consistency query
1 parent 185d43a commit caf7ebc

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

java/ql/consistency-queries/diags.ql

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import semmle.code.java.Diagnostics
2+
3+
/*
4+
This query fails if any unexpected diagnostics are recorded in the
5+
database. By putting
6+
// Diagnostic Matches: PAT
7+
in any source files, you can declare that diagnostics matching PAT
8+
(in the string.matches(string) sense) are expected.
9+
*/
10+
11+
class DiagnosticException extends Top {
12+
string pattern;
13+
14+
DiagnosticException() {
15+
this.(KtComment).getText() = "// Diagnostic Matches: " + pattern
16+
or
17+
this.(Javadoc).toString() = "// Diagnostic Matches: " + pattern
18+
}
19+
20+
Diagnostic getException() { diagnosticMessage(result).matches(pattern) }
21+
}
22+
23+
string diagnosticMessage(Diagnostic d) {
24+
if d.getFullMessage() != "" then result = d.getFullMessage() else result = d.getMessage()
25+
}
26+
27+
// Check that there aren't any old DiagnosticExceptions left after
28+
// something is fixed.
29+
query predicate unusedDiagnosticException(DiagnosticException de) { not exists(de.getException()) }
30+
31+
query predicate unexpectedDiagnostic(Diagnostic d, string s) {
32+
s = diagnosticMessage(d) and
33+
not d = any(DiagnosticException de).getException()
34+
}

0 commit comments

Comments
 (0)