|
| 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 | +} |
0 commit comments