Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9612426

Browse files
committed
C++: Initial file-related metric queries.
This adds a library `FailedExtractions.qll` that classifies extractor errors and provides a unified interface for both recoverable and irrecoverable extractor errors. This interface is then used by the new diagnostic queries to list successfully extracted files, as well as files that encountered an extraction error.
1 parent 2e8e04f commit 9612426

3 files changed

Lines changed: 145 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @name Failed extractions
3+
* @description Gives the command-line of compilations for which extraction did not run to completion.
4+
* @kind diagnostic
5+
* @id cpp/diagnostics/failed-extractions
6+
*/
7+
8+
import cpp
9+
import FailedExtractions
10+
11+
from ExtractionError error
12+
where
13+
error instanceof ExtractionUnknownError or
14+
exists(error.getFile().getRelativePath())
15+
select error, "Extracting file $@ failed with $@ (at $@)", error.getFile(), error.getErrorMessage(),
16+
error.getLocation(), error.getSeverity()
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import cpp
2+
3+
/**
4+
* The class of errors upon we mark a file as non-successfully extracted.
5+
*/
6+
class ReportableError extends Diagnostic {
7+
ReportableError() {
8+
(
9+
this instanceof CompilerDiscretionaryError or
10+
this instanceof CompilerError or
11+
this instanceof CompilerCatastrophe
12+
) and
13+
// If the extractor encounters an error in a compilation, it always emits a
14+
// catch-all diagnostic "There was an error during this compilation", to ensure
15+
// that the error makes it to the database.
16+
// This error doesn't have a file path attached to it, and is thus
17+
// useless for us to report. Furthermore, in the common case, we will have a
18+
// proper diagnostic for this error we can show.
19+
// Instead, we synthesize `TUnknownError` if this is the only error that we can show to the user.
20+
not this.getFile().getAbsolutePath() = ""
21+
}
22+
}
23+
24+
newtype TExtractionError =
25+
TReportableError(ReportableError err) or
26+
TCompilationFailed(Compilation c, File f) {
27+
f = c.getAFileCompiled() and not c.normalTermination()
28+
} or
29+
// Report generic extractor errors only if we haven't seen any other error-level diagnostic
30+
TUnknownError(CompilerError err) { not exists(ReportableError e) }
31+
32+
class ExtractionError extends TExtractionError {
33+
string toString() { none() }
34+
35+
string getErrorMessage() { none() }
36+
37+
File getFile() { none() }
38+
39+
Location getLocation() { none() }
40+
41+
int getSeverity() {
42+
// Unfortunately, we can't distinguish between errors and fatal errors in SARIF,
43+
// so all errors have severity 2.
44+
result = 2
45+
}
46+
}
47+
48+
/**
49+
* An irrecoverable extraction failure, where extraction was unable to finish.
50+
* This can be caused by a multitude of reasons, for example:
51+
* - hitting a frontend assertion
52+
* - crashing due to dereferencing an invalid pointer
53+
* - stack overflow
54+
* - out of memory
55+
*/
56+
class ExtractionIrrecoverableError extends ExtractionError, TCompilationFailed {
57+
Compilation c;
58+
File f;
59+
60+
ExtractionIrrecoverableError() { this = TCompilationFailed(c, f) }
61+
62+
override string toString() {
63+
result = "Irrecoverable extraction error: " + c.toString() + " in " + f.toString()
64+
}
65+
66+
override string getErrorMessage() {
67+
result =
68+
"Irrecoverable compilation failure, check logs/build-tracer.log in the database directory for more information."
69+
}
70+
71+
override File getFile() { result = f }
72+
73+
override Location getLocation() { result = f.getLocation() }
74+
}
75+
76+
/**
77+
* A recoverable extraction error.
78+
* These are compiler errors from the frontend.
79+
* Upon encountering one of these, we still continue extraction, but the
80+
* database will be incomplete for that file.
81+
*/
82+
class ExtractionRecoverableError extends ExtractionError, TReportableError {
83+
ReportableError err;
84+
85+
ExtractionRecoverableError() { this = TReportableError(err) }
86+
87+
override string toString() { result = "Recoverable extraction error: " + err }
88+
89+
override string getErrorMessage() { result = err.getFullMessage() }
90+
91+
override File getFile() { result = err.getFile() }
92+
93+
override Location getLocation() { result = err.getLocation() }
94+
}
95+
96+
/**
97+
* An unknown error happened during extraction.
98+
* These are only displayed if we know that we encountered an error during extraction,
99+
* but, for some reason, failed to emit a proper diagnostic with location information
100+
* and error message.
101+
*/
102+
class ExtractionUnknownError extends ExtractionError, TUnknownError {
103+
CompilerError err;
104+
105+
ExtractionUnknownError() { this = TUnknownError(err) }
106+
107+
override string toString() { result = "Unknown extraction error: " + err }
108+
109+
override string getErrorMessage() { result = err.getFullMessage() }
110+
111+
override File getFile() { result = err.getFile() }
112+
113+
override Location getLocation() { result = err.getLocation() }
114+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @name Successfully extracted files.
3+
* @description Lists all files in the database that were extracted without encountering an error.
4+
* @kind diagnostic
5+
* @id cpp/diagnostics/successfully-extracted-files
6+
*/
7+
8+
import cpp
9+
import FailedExtractions
10+
11+
from File f
12+
where
13+
not exists(ExtractionError e | e.getFile() = f) and
14+
exists(f.getRelativePath())
15+
select f

0 commit comments

Comments
 (0)