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/Basic/Sarif.cpp b/clang/lib/Basic/Sarif.cpp --- a/clang/lib/Basic/Sarif.cpp +++ b/clang/lib/Basic/Sarif.cpp @@ -311,6 +311,10 @@ Artifact["offset"] = *A.Offset; Artifacts->push_back(json::Value(std::move(Artifact))); } + llvm::sort(*Artifacts, [](const json::Value &x, const json::Value &y) { + return x.getAsObject()->getNumber("index") < + y.getAsObject()->getNumber("index"); + }); // Clear, reset temporaries before next run. reset(); 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,24 @@ } void SARIFDiagnostic::emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) { - assert(false && "Not implemented in SARIF mode"); + SarifRule Rule = SarifRule::create().setRuleId( + "-1"); // FIXME(llvm-project#61597) replace with something interesting + 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 " + << emitFilename(PLoc.getFilename(), Loc.getManager()) << ':' + << 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://{{[^"]+/clang/test/Frontend/sarif-diagnostics.hpp}}" +// SARIF: }, +// SARIF: "mimeType":"text/plain", +// SARIF: "roles":[ +// SARIF: "resultFile" +// SARIF: ] // SARIF: } // SARIF: ], // SARIF: "columnKind":"unicodeCodePoints", @@ -70,7 +83,7 @@ // SARIF: "message":{ // SARIF: "text":"'main' must return 'int'" // SARIF: }, -// SARIF: "ruleId":"3485", +// SARIF: "ruleId":"3486", // SARIF: "ruleIndex":0 // SARIF: }, // SARIF: { @@ -93,7 +106,7 @@ // SARIF: "message":{ // SARIF: "text":"use of undeclared identifier 'hello'" // SARIF: }, -// SARIF: "ruleId":"4632", +// SARIF: "ruleId":"4633", // SARIF: "ruleIndex":1 // SARIF: }, // SARIF: { @@ -172,7 +185,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":10, @@ -185,7 +198,7 @@ // SARIF: "message":{ // SARIF: "text":"unused variable 'Yes'" // SARIF: }, -// SARIF: "ruleId":"6593", +// SARIF: "ruleId":"6595", // SARIF: "ruleIndex":5 // SARIF: }, // SARIF: { @@ -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, @@ -208,7 +221,7 @@ // SARIF: "message":{ // SARIF: "text":"use of undeclared identifier 'hi'" // SARIF: }, -// SARIF: "ruleId":"4632", +// SARIF: "ruleId":"4633", // SARIF: "ruleIndex":6 // SARIF: }, // SARIF: { @@ -282,8 +295,54 @@ // SARIF: "message":{ // SARIF: "text":"invalid operands to binary expression ('t1' and 't1')" // SARIF: }, -// SARIF: "ruleId":"4567", +// SARIF: "ruleId":"4568", // 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 {{[^"]+/clang/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":"4658", +// SARIF: "ruleIndex":10 // SARIF: } // SARIF: ], // SARIF: "tool":{ @@ -302,7 +361,7 @@ // SARIF: "fullDescription":{ // SARIF: "text":"" // SARIF: }, -// SARIF: "id":"3485", +// SARIF: "id":"3486", // SARIF: "name":"" // SARIF: }, // SARIF: { @@ -314,7 +373,7 @@ // SARIF: "fullDescription":{ // SARIF: "text":"" // SARIF: }, -// SARIF: "id":"4632", +// SARIF: "id":"4633", // SARIF: "name":"" // SARIF: }, // SARIF: { @@ -362,7 +421,7 @@ // SARIF: "fullDescription":{ // SARIF: "text":"" // SARIF: }, -// SARIF: "id":"6593", +// SARIF: "id":"6595", // SARIF: "name":"" // SARIF: }, // SARIF: { @@ -374,7 +433,7 @@ // SARIF: "fullDescription":{ // SARIF: "text":"" // SARIF: }, -// SARIF: "id":"4632", +// SARIF: "id":"4633", // SARIF: "name":"" // SARIF: }, // SARIF: { @@ -398,7 +457,31 @@ // SARIF: "fullDescription":{ // SARIF: "text":"" // SARIF: }, -// SARIF: "id":"4567", +// SARIF: "id":"4568", +// 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":"4658", // SARIF: "name":"" // SARIF: } // SARIF: ], @@ -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.