Skip to content

Commit 00137f2

Browse files
authored
Merge pull request github#5721 from github/yo-h/java-diagnostic-queries
Java: add extractor `diagnostic` queries
2 parents a5cfdd2 + 87cd724 commit 00137f2

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* Provides classes and predicates for reporting extractor diagnostics to end users.
3+
*/
4+
5+
import java
6+
7+
/** Gets the SARIF severity level that indicates an error. */
8+
private int getErrorSeverity() { result = 2 }
9+
10+
/** Gets the SARIF severity level that indicates a warning. */
11+
private int getWarnSeverity() { result = 1 }
12+
13+
private predicate knownWarnings(@diagnostic d, string msg, int sev) {
14+
exists(string filename |
15+
diagnostics(d, 2, _, "Skipping Lombok-ed source file: " + filename, _, _) and
16+
msg = "Use of Lombok detected. Skipping file: " + filename and
17+
sev = getWarnSeverity()
18+
)
19+
}
20+
21+
private predicate knownErrors(@diagnostic d, string msg, int sev) {
22+
exists(string numErr, Location l |
23+
diagnostics(d, 6, _, numErr, _, l) and
24+
msg = "Frontend errors in file: " + l.getFile().getAbsolutePath() + " (" + numErr + ")" and
25+
sev = getErrorSeverity()
26+
)
27+
or
28+
exists(string filename, Location l |
29+
diagnostics(d, 7, _, "Exception compiling file " + filename, _, l) and
30+
msg = "Extraction incomplete in file: " + filename and
31+
sev = getErrorSeverity()
32+
)
33+
or
34+
exists(string errMsg, Location l |
35+
diagnostics(d, 8, _, errMsg, _, l) and
36+
msg = "Severe error: " + errMsg and
37+
sev = getErrorSeverity()
38+
)
39+
}
40+
41+
private predicate unknownErrors(@diagnostic d, string msg, int sev) {
42+
not knownErrors(d, _, _) and
43+
exists(Location l, File f, int diagSev |
44+
diagnostics(d, diagSev, _, _, _, l) and l.getFile() = f and diagSev > 3
45+
|
46+
exists(f.getRelativePath()) and
47+
msg = "Unknown errors in file: " + f.getAbsolutePath() + " (" + diagSev + ")" and
48+
sev = getErrorSeverity()
49+
)
50+
}
51+
52+
/**
53+
* Holds if an extraction error or warning occurred that should be reported to end users,
54+
* with the error message `msg` and SARIF severity `sev`.
55+
*/
56+
predicate reportableDiagnostics(@diagnostic d, string msg, int sev) {
57+
knownWarnings(d, msg, sev) or knownErrors(d, msg, sev) or unknownErrors(d, msg, sev)
58+
}
59+
60+
/**
61+
* Holds if compilation unit `f` is a source file that has
62+
* no relevant extraction diagnostics associated with it.
63+
*/
64+
predicate successfullyExtracted(CompilationUnit f) {
65+
not exists(@diagnostic d, Location l |
66+
reportableDiagnostics(d, _, _) and diagnostics(d, _, _, _, _, l) and l.getFile() = f
67+
) and
68+
exists(f.getRelativePath()) and
69+
f.fromSource()
70+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @name Extraction errors
3+
* @description A list of extraction errors for files in the source code directory.
4+
* @kind diagnostic
5+
* @id java/diagnostics/extraction-errors
6+
*/
7+
8+
import java
9+
import DiagnosticsReporting
10+
11+
from string msg, int sev
12+
where reportableDiagnostics(_, msg, sev)
13+
select msg, sev
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @name Successfully extracted files
3+
* @description A list of all files in the source code directory that
4+
* were extracted without encountering an error in the file.
5+
* @kind diagnostic
6+
* @id java/diagnostics/successfully-extracted-files
7+
*/
8+
9+
import java
10+
import DiagnosticsReporting
11+
12+
from CompilationUnit f
13+
where successfullyExtracted(f)
14+
select f, ""

0 commit comments

Comments
 (0)