diff --git a/clang/include/clang/Frontend/SARIFDiagnostic.h b/clang/include/clang/Frontend/SARIFDiagnostic.h --- a/clang/include/clang/Frontend/SARIFDiagnostic.h +++ b/clang/include/clang/Frontend/SARIFDiagnostic.h @@ -60,8 +60,7 @@ SarifResult addLocationToResult(SarifResult Result, FullSourceLoc Loc, PresumedLoc PLoc, - ArrayRef Ranges, - const Diagnostic &Diag); + ArrayRef Ranges); SarifRule addDiagnosticLevelToRule(SarifRule Rule, DiagnosticsEngine::Level Level); diff --git a/clang/lib/Frontend/SARIFDiagnostic.cpp b/clang/lib/Frontend/SARIFDiagnostic.cpp --- a/clang/lib/Frontend/SARIFDiagnostic.cpp +++ b/clang/lib/Frontend/SARIFDiagnostic.cpp @@ -26,6 +26,7 @@ #include "llvm/Support/Path.h" #include "llvm/Support/raw_ostream.h" #include +#include #include namespace clang { @@ -56,14 +57,15 @@ SarifResult::create(RuleIdx).setDiagnosticMessage(Message); if (Loc.isValid()) - Result = addLocationToResult(Result, Loc, PLoc, Ranges, *Diag); + Result = addLocationToResult(Result, Loc, PLoc, Ranges); Writer->appendResult(Result); } -SarifResult SARIFDiagnostic::addLocationToResult( - SarifResult Result, FullSourceLoc Loc, PresumedLoc PLoc, - ArrayRef Ranges, const Diagnostic &Diag) { +SarifResult +SARIFDiagnostic::addLocationToResult(SarifResult Result, FullSourceLoc Loc, + PresumedLoc PLoc, + ArrayRef Ranges) { SmallVector Locations = {}; if (PLoc.isInvalid()) { @@ -209,7 +211,22 @@ } void SARIFDiagnostic::emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) { - assert(false && "Not implemented in SARIF mode"); + SarifRule Rule = SarifRule::create().setRuleId(std::to_string(-1)); + Rule = addDiagnosticLevelToRule(Rule, DiagnosticsEngine::Level::Note); + + unsigned RuleIdx = Writer->createRule(Rule); + + SmallString<200> MessageStorage; + llvm::raw_svector_ostream Message(MessageStorage); + Message << "in file included from " << PLoc.getFilename() << ':' + << PLoc.getLine() << ":\n"; + SarifResult Result = + SarifResult::create(RuleIdx).setDiagnosticMessage(Message.str()); + + if (Loc.isValid()) + Result = addLocationToResult(Result, Loc, PLoc, {}); + + Writer->appendResult(Result); } void SARIFDiagnostic::emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc, diff --git a/clang/test/Frontend/sarif-diagnostics.hpp b/clang/test/Frontend/sarif-diagnostics.hpp new file mode 100644 --- /dev/null +++ b/clang/test/Frontend/sarif-diagnostics.hpp @@ -0,0 +1 @@ +Test test; diff --git a/clang/test/Frontend/sarif-diagnostics.cpp b/clang/test/Frontend/sarif-diagnostics.cpp --- a/clang/test/Frontend/sarif-diagnostics.cpp +++ b/clang/test/Frontend/sarif-diagnostics.cpp @@ -27,6 +27,8 @@ x + y; } +#include "sarif-diagnostics.hpp" + // RUN: %clang -fsyntax-only -Wall -Wextra -fdiagnostics-format=sarif %s > %t.txt 2>&1 || true // RUN: FileCheck -dump-input=always %s --input-file=%t.txt --check-prefixes=STDERR,SARIF @@ -46,6 +48,17 @@ // SARIF: "roles":[ // SARIF: "resultFile" // SARIF: ] +// SARIF: }, +// SARIF: { +// SARIF: "length":{{[0-9]+}}, +// SARIF: "location":{ +// SARIF: "index":1, +// SARIF: "uri":"file://{{[^"]+test/Frontend/sarif-diagnostics.hpp}}" +// SARIF: }, +// SARIF: "mimeType":"text/plain", +// SARIF: "roles":[ +// SARIF: "resultFile" +// SARIF: ] // SARIF: } // SARIF: ], // SARIF: "columnKind":"unicodeCodePoints", @@ -126,7 +139,7 @@ // SARIF: "physicalLocation":{ // SARIF: "artifactLocation":{ // SARIF: "index":0, -// SARIF: "uri":{{"file://[^"]+clang/test/Frontend/sarif-diagnostics.cpp"}} +// SARIF: "uri":"file://{{[^"]+/test/Frontend/sarif-diagnostics.cpp}}" // SARIF: }, // SARIF: "region":{ // SARIF: "endColumn":5, @@ -195,7 +208,7 @@ // SARIF: "physicalLocation":{ // SARIF: "artifactLocation":{ // SARIF: "index":0, -// SARIF: "uri":{{"file://[^"]+/clang/test/Frontend/sarif-diagnostics.cpp"}} +// SARIF: "uri":{{"file://[^"]+clang/test/Frontend/sarif-diagnostics.cpp"}} // SARIF: }, // SARIF: "region":{ // SARIF: "endColumn":12, @@ -284,6 +297,52 @@ // SARIF: }, // SARIF: "ruleId":"4567", // SARIF: "ruleIndex":8 +// SARIF: }, +// SARIF: { +// SARIF: "level":"note", +// SARIF: "locations":[ +// SARIF: { +// SARIF: "physicalLocation":{ +// SARIF: "artifactLocation":{ +// SARIF: "index":0, +// SARIF: "uri":{{"file://[^"]+/clang/test/Frontend/sarif-diagnostics.cpp"}} +// SARIF: }, +// SARIF: "region":{ +// SARIF: "endColumn":10, +// SARIF: "startColumn":10, +// SARIF: "startLine":30 +// SARIF: } +// SARIF: } +// SARIF: } +// SARIF: ], +// SARIF: "message":{ +// SARIF: "text":"in file included from {{[^"]+test/Frontend/sarif-diagnostics.cpp:30:}}\n" +// SARIF: }, +// SARIF: "ruleId":"-1", +// SARIF: "ruleIndex":9 +// SARIF: }, +// SARIF: { +// SARIF: "level":"error", +// SARIF: "locations":[ +// SARIF: { +// SARIF: "physicalLocation":{ +// SARIF: "artifactLocation":{ +// SARIF: "index":1, +// SARIF: "uri":"file:///{{[^"]+/test/Frontend/sarif-diagnostics.hpp}}" +// SARIF: }, +// SARIF: "region":{ +// SARIF: "endColumn":1, +// SARIF: "startColumn":1, +// SARIF: "startLine":1 +// SARIF: } +// SARIF: } +// SARIF: } +// SARIF: ], +// SARIF: "message":{ +// SARIF: "text":"unknown type name 'Test'" +// SARIF: }, +// SARIF: "ruleId":"4657", +// SARIF: "ruleIndex":10 // SARIF: } // SARIF: ], // SARIF: "tool":{ @@ -400,6 +459,30 @@ // SARIF: }, // SARIF: "id":"4567", // SARIF: "name":"" +// SARIF: }, +// SARIF: { +// SARIF: "defaultConfiguration":{ +// SARIF: "enabled":true, +// SARIF: "level":"note", +// SARIF: "rank":-1 +// SARIF: }, +// SARIF: "fullDescription":{ +// SARIF: "text":"" +// SARIF: }, +// SARIF: "id":"-1", +// SARIF: "name":"" +// SARIF: }, +// SARIF: { +// SARIF: "defaultConfiguration":{ +// SARIF: "enabled":true, +// SARIF: "level":"error", +// SARIF: "rank":50 +// SARIF: }, +// SARIF: "fullDescription":{ +// SARIF: "text":"" +// SARIF: }, +// SARIF: "id":"4657", +// SARIF: "name":"" // SARIF: } // SARIF: ], // SARIF: "version":"17.0.0" @@ -409,4 +492,4 @@ // SARIF: ], // SARIF: "version":"2.1.0" // SARIF: } -// STDERR: 2 warnings and 6 errors generated. +// STDERR: 2 warnings and 7 errors generated.