Skip to content

Commit a517a05

Browse files
authored
Merge pull request github#6830 from github/henrymercer/report-extraction-errors-as-warnings
C++: Improve SARIF severity level reporting of extractor diagnostics
2 parents 0e5f89a + 5b26d41 commit a517a05

File tree

6 files changed

+80
-58
lines changed

6 files changed

+80
-58
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
codescanning
2+
* Problems with extraction that in most cases won't break the analysis in a significant way are now reported as warnings rather than errors.
3+
* The failed extractor invocations query now has severity `error`.

cpp/ql/src/Diagnostics/ExtractionErrors.ql

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
2-
* Provides a common hierarchy of all types of errors that can occur during extraction.
2+
* Provides a common hierarchy of all types of problems that can occur during extraction.
33
*/
44

55
import cpp
66

77
/*
88
* A note about how the C/C++ extractor emits diagnostics:
9-
* When the extractor frontend encounters an error, it emits a diagnostic message,
9+
* When the extractor frontend encounters a problem, it emits a diagnostic message,
1010
* that includes a message, location and severity.
1111
* However, that process is best-effort and may fail (e.g. due to lack of memory).
1212
* Thus, if the extractor emitted at least one diagnostic of severity discretionary
@@ -15,17 +15,17 @@ import cpp
1515
* In the common case, this means that a compilation during which one or more errors happened also gets
1616
* the catch-all diagnostic.
1717
* This diagnostic has the empty string as file path.
18-
* We filter out these useless diagnostics if there is at least one error-level diagnostic
18+
* We filter out these useless diagnostics if there is at least one warning-level diagnostic
1919
* for the affected compilation in the database.
2020
* Otherwise, we show it to indicate that something went wrong and that we
2121
* don't know what exactly happened.
2222
*/
2323

2424
/**
25-
* An error that, if present, leads to a file being marked as non-successfully extracted.
25+
* A problem with a file that, if present, leads to a file being marked as non-successfully extracted.
2626
*/
27-
class ReportableError extends Diagnostic {
28-
ReportableError() {
27+
class ReportableWarning extends Diagnostic {
28+
ReportableWarning() {
2929
(
3030
this instanceof CompilerDiscretionaryError or
3131
this instanceof CompilerError or
@@ -36,39 +36,35 @@ class ReportableError extends Diagnostic {
3636
}
3737
}
3838

39-
private newtype TExtractionError =
40-
TReportableError(ReportableError err) or
39+
private newtype TExtractionProblem =
40+
TReportableWarning(ReportableWarning err) or
4141
TCompilationFailed(Compilation c, File f) {
4242
f = c.getAFileCompiled() and not c.normalTermination()
4343
} or
4444
// Show the catch-all diagnostic (see note above) only if we haven't seen any other error-level diagnostic
4545
// for that compilation
46-
TUnknownError(CompilerError err) {
47-
not exists(ReportableError e | e.getCompilation() = err.getCompilation())
46+
TUnknownProblem(CompilerError err) {
47+
not exists(ReportableWarning e | e.getCompilation() = err.getCompilation())
4848
}
4949

5050
/**
51-
* Superclass for the extraction error hierarchy.
51+
* Superclass for the extraction problem hierarchy.
5252
*/
53-
class ExtractionError extends TExtractionError {
54-
/** Gets the string representation of the error. */
53+
class ExtractionProblem extends TExtractionProblem {
54+
/** Gets the string representation of the problem. */
5555
string toString() { none() }
5656

57-
/** Gets the error message for this error. */
58-
string getErrorMessage() { none() }
57+
/** Gets the problem message for this problem. */
58+
string getProblemMessage() { none() }
5959

60-
/** Gets the file this error occured in. */
60+
/** Gets the file this problem occured in. */
6161
File getFile() { none() }
6262

63-
/** Gets the location this error occured in. */
63+
/** Gets the location this problem occured in. */
6464
Location getLocation() { none() }
6565

66-
/** Gets the SARIF severity of this error. */
67-
int getSeverity() {
68-
// Unfortunately, we can't distinguish between errors and fatal errors in SARIF,
69-
// so all errors have severity 2.
70-
result = 2
71-
}
66+
/** Gets the SARIF severity of this problem. */
67+
int getSeverity() { none() }
7268
}
7369

7470
/**
@@ -79,7 +75,7 @@ class ExtractionError extends TExtractionError {
7975
* - stack overflow
8076
* - out of memory
8177
*/
82-
class ExtractionUnrecoverableError extends ExtractionError, TCompilationFailed {
78+
class ExtractionUnrecoverableError extends ExtractionProblem, TCompilationFailed {
8379
Compilation c;
8480
File f;
8581

@@ -89,49 +85,67 @@ class ExtractionUnrecoverableError extends ExtractionError, TCompilationFailed {
8985
result = "Unrecoverable extraction error while compiling " + f.toString()
9086
}
9187

92-
override string getErrorMessage() { result = "unrecoverable compilation failure." }
88+
override string getProblemMessage() { result = "unrecoverable compilation failure." }
9389

9490
override File getFile() { result = f }
9591

9692
override Location getLocation() { result = f.getLocation() }
93+
94+
override int getSeverity() {
95+
// These extractor errors break the analysis, so we mark them in SARIF as
96+
// [errors](https://docs.oasis-open.org/sarif/sarif/v2.1.0/csprd01/sarif-v2.1.0-csprd01.html#_Toc10541338).
97+
result = 2
98+
}
9799
}
98100

99101
/**
100-
* A recoverable extraction error.
102+
* A recoverable extraction warning.
101103
* These are compiler errors from the frontend.
102104
* Upon encountering one of these, we still continue extraction, but the
103105
* database will be incomplete for that file.
104106
*/
105-
class ExtractionRecoverableError extends ExtractionError, TReportableError {
106-
ReportableError err;
107+
class ExtractionRecoverableWarning extends ExtractionProblem, TReportableWarning {
108+
ReportableWarning err;
107109

108-
ExtractionRecoverableError() { this = TReportableError(err) }
110+
ExtractionRecoverableWarning() { this = TReportableWarning(err) }
109111

110112
override string toString() { result = "Recoverable extraction error: " + err }
111113

112-
override string getErrorMessage() { result = err.getFullMessage() }
114+
override string getProblemMessage() { result = err.getFullMessage() }
113115

114116
override File getFile() { result = err.getFile() }
115117

116118
override Location getLocation() { result = err.getLocation() }
119+
120+
override int getSeverity() {
121+
// Recoverable extraction problems don't tend to break the analysis, so we mark them in SARIF as
122+
// [warnings](https://docs.oasis-open.org/sarif/sarif/v2.1.0/csprd01/sarif-v2.1.0-csprd01.html#_Toc10541338).
123+
result = 1
124+
}
117125
}
118126

119127
/**
120-
* An unknown error happened during extraction.
121-
* These are only displayed if we know that we encountered an error during extraction,
128+
* An unknown problem happened during extraction.
129+
* These are only displayed if we know that we encountered an problem during extraction,
122130
* but, for some reason, failed to emit a proper diagnostic with location information
123-
* and error message.
131+
* and problem message.
124132
*/
125-
class ExtractionUnknownError extends ExtractionError, TUnknownError {
133+
class ExtractionUnknownProblem extends ExtractionProblem, TUnknownProblem {
126134
CompilerError err;
127135

128-
ExtractionUnknownError() { this = TUnknownError(err) }
136+
ExtractionUnknownProblem() { this = TUnknownProblem(err) }
129137

130-
override string toString() { result = "Unknown extraction error: " + err }
138+
override string toString() { result = "Unknown extraction problem: " + err }
131139

132-
override string getErrorMessage() { result = err.getFullMessage() }
140+
override string getProblemMessage() { result = err.getFullMessage() }
133141

134142
override File getFile() { result = err.getFile() }
135143

136144
override Location getLocation() { result = err.getLocation() }
145+
146+
override int getSeverity() {
147+
// Unknown extraction problems don't tend to break the analysis, so we mark them in SARIF as
148+
// [warnings](https://docs.oasis-open.org/sarif/sarif/v2.1.0/csprd01/sarif-v2.1.0-csprd01.html#_Toc10541338).
149+
result = 1
150+
}
137151
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @name Extraction warnings
3+
* @description List all extraction warnings for files in the source code directory.
4+
* @kind diagnostic
5+
* @id cpp/diagnostics/extraction-warnings
6+
*/
7+
8+
import cpp
9+
import ExtractionProblems
10+
11+
from ExtractionProblem warning
12+
where
13+
warning instanceof ExtractionRecoverableWarning and exists(warning.getFile().getRelativePath())
14+
or
15+
warning instanceof ExtractionUnknownProblem
16+
select warning,
17+
"Extraction failed in " + warning.getFile() + " with warning " + warning.getProblemMessage(),
18+
warning.getSeverity()

cpp/ql/src/Diagnostics/FailedExtractorInvocations.ql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ string describe(Compilation c) {
1313
else result = "extractor invocation " + concat(int i | | c.getArgument(i), " " order by i)
1414
}
1515

16+
/** Gets the SARIF severity level that indicates an error. */
17+
private int getErrorSeverity() { result = 2 }
18+
1619
from Compilation c
1720
where not c.normalTermination()
18-
select "Extraction aborted for " + describe(c)
21+
select "Extraction aborted for " + describe(c), getErrorSeverity()
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
/**
22
* @name Successfully extracted files
3-
* @description Lists all files in the source code directory that were extracted without encountering an error in the file.
3+
* @description Lists all files in the source code directory that were extracted without encountering a problem in the file.
44
* @kind diagnostic
55
* @id cpp/diagnostics/successfully-extracted-files
66
*/
77

88
import cpp
9-
import ExtractionErrors
9+
import ExtractionProblems
1010

1111
from File f
1212
where
13-
not exists(ExtractionError e | e.getFile() = f) and
13+
not exists(ExtractionProblem e | e.getFile() = f) and
1414
exists(f.getRelativePath())
15-
select f, ""
15+
select f, "File successfully extracted"

0 commit comments

Comments
 (0)