diff --git a/clang/include/clang/Basic/Diagnostic.h b/clang/include/clang/Basic/Diagnostic.h --- a/clang/include/clang/Basic/Diagnostic.h +++ b/clang/include/clang/Basic/Diagnostic.h @@ -1692,6 +1692,7 @@ DiagnosticsEngine::Level Level; FullSourceLoc Loc; std::string Message; + std::string StableName; std::vector Ranges; std::vector FixIts; @@ -1699,9 +1700,9 @@ StoredDiagnostic() = default; StoredDiagnostic(DiagnosticsEngine::Level Level, const Diagnostic &Info); StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID, - StringRef Message); + StringRef Message, StringRef StableName); StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID, - StringRef Message, FullSourceLoc Loc, + StringRef Message, StringRef StableName, FullSourceLoc Loc, ArrayRef Ranges, ArrayRef Fixits); @@ -1712,6 +1713,7 @@ DiagnosticsEngine::Level getLevel() const { return Level; } const FullSourceLoc &getLocation() const { return Loc; } StringRef getMessage() const { return Message; } + StringRef getStableName() const { return StableName; } void setLocation(FullSourceLoc Loc) { this->Loc = Loc; } diff --git a/clang/include/clang/Basic/DiagnosticIDs.h b/clang/include/clang/Basic/DiagnosticIDs.h --- a/clang/include/clang/Basic/DiagnosticIDs.h +++ b/clang/include/clang/Basic/DiagnosticIDs.h @@ -197,6 +197,9 @@ /// Given a diagnostic ID, return a description of the issue. StringRef getDescription(unsigned DiagID) const; + /// Given a diagnostic ID, return its stable name + StringRef getStableName(unsigned DiagID) const; + /// Return true if the unmapped diagnostic levelof the specified /// diagnostic ID is a Warning or Extension. /// diff --git a/clang/include/clang/Frontend/ASTUnit.h b/clang/include/clang/Frontend/ASTUnit.h --- a/clang/include/clang/Frontend/ASTUnit.h +++ b/clang/include/clang/Frontend/ASTUnit.h @@ -99,6 +99,7 @@ DiagnosticsEngine::Level Level; std::string Message; std::string Filename; + std::string StableName; unsigned LocOffset; std::vector> Ranges; std::vector FixIts; diff --git a/clang/lib/Basic/Diagnostic.cpp b/clang/lib/Basic/Diagnostic.cpp --- a/clang/lib/Basic/Diagnostic.cpp +++ b/clang/lib/Basic/Diagnostic.cpp @@ -1154,8 +1154,8 @@ } StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID, - StringRef Message) - : ID(ID), Level(Level), Message(Message) {} + StringRef Message, StringRef StableName) + : ID(ID), Level(Level), Message(Message), StableName(StableName) {} StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, const Diagnostic &Info) @@ -1172,13 +1172,13 @@ } StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID, - StringRef Message, FullSourceLoc Loc, + StringRef Message, StringRef StableName, + FullSourceLoc Loc, ArrayRef Ranges, ArrayRef FixIts) - : ID(ID), Level(Level), Loc(Loc), Message(Message), - Ranges(Ranges.begin(), Ranges.end()), FixIts(FixIts.begin(), FixIts.end()) -{ -} + : ID(ID), Level(Level), Loc(Loc), Message(Message), StableName(StableName), + Ranges(Ranges.begin(), Ranges.end()), + FixIts(FixIts.begin(), FixIts.end()) {} llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS, const StoredDiagnostic &SD) { diff --git a/clang/lib/Basic/DiagnosticIDs.cpp b/clang/lib/Basic/DiagnosticIDs.cpp --- a/clang/lib/Basic/DiagnosticIDs.cpp +++ b/clang/lib/Basic/DiagnosticIDs.cpp @@ -99,6 +99,28 @@ #undef DIAG }; +// Stable names +const char *const StaticDiagInfoStableNames[] = { +#define DIAG(ENUM, CLASS, DEFAULT_SEVERITY, DESC, GROUP, SFINAE, NOWERROR, \ + SHOWINSYSHEADER, SHOWINSYSMACRO, DEFERRABLE, CATEGORY) \ + #ENUM, +// clang-format off +#include "clang/Basic/DiagnosticCommonKinds.inc" +#include "clang/Basic/DiagnosticDriverKinds.inc" +#include "clang/Basic/DiagnosticFrontendKinds.inc" +#include "clang/Basic/DiagnosticSerializationKinds.inc" +#include "clang/Basic/DiagnosticLexKinds.inc" +#include "clang/Basic/DiagnosticParseKinds.inc" +#include "clang/Basic/DiagnosticASTKinds.inc" +#include "clang/Basic/DiagnosticCommentKinds.inc" +#include "clang/Basic/DiagnosticCrossTUKinds.inc" +#include "clang/Basic/DiagnosticSemaKinds.inc" +#include "clang/Basic/DiagnosticAnalysisKinds.inc" +#include "clang/Basic/DiagnosticRefactoringKinds.inc" +// clang-format on +#undef DIAG +}; + // Diagnostic classes. enum { CLASS_NOTE = 0x01, @@ -134,6 +156,11 @@ return StringRef(&Table[StringOffset], DescriptionLen); } + StringRef getStableName() const { + size_t MyIndex = this - &StaticDiagInfo[0]; + return StringRef(StaticDiagInfoStableNames[MyIndex]); + } + diag::Flavor getFlavor() const { return Class == CLASS_REMARK ? diag::Flavor::Remark : diag::Flavor::WarningOrError; @@ -373,6 +400,12 @@ return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second; } + StringRef getStableName(unsigned DiagID) const { + assert(DiagID - DIAG_UPPER_LIMIT < DiagInfo.size() && + "Invalid diagnostic ID"); + return DiagInfo[DiagID - DIAG_UPPER_LIMIT].second; + } + /// getLevel - Return the level of the specified custom diagnostic. DiagnosticIDs::Level getLevel(unsigned DiagID) const { assert(DiagID - DIAG_UPPER_LIMIT < DiagInfo.size() && @@ -469,6 +502,13 @@ return CustomDiagInfo->getDescription(DiagID); } +StringRef DiagnosticIDs::getStableName(unsigned DiagID) const { + if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) + return Info->getStableName(); + assert(CustomDiagInfo && "Invalid CustomDiagInfo"); + return CustomDiagInfo->getStableName(DiagID); +} + static DiagnosticIDs::Level toLevel(diag::Severity SV) { switch (SV) { case diag::Severity::Ignored: diff --git a/clang/lib/Frontend/ASTUnit.cpp b/clang/lib/Frontend/ASTUnit.cpp --- a/clang/lib/Frontend/ASTUnit.cpp +++ b/clang/lib/Frontend/ASTUnit.cpp @@ -2417,8 +2417,8 @@ FH.RemoveRange = CharSourceRange::getCharRange(BL, EL); } - Result.push_back(StoredDiagnostic(SD.Level, SD.ID, - SD.Message, Loc, Ranges, FixIts)); + Result.push_back(StoredDiagnostic(SD.Level, SD.ID, SD.Message, + SD.StableName, Loc, Ranges, FixIts)); } Result.swap(Out); } 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 @@ -46,7 +46,10 @@ if (!Diag) return; - SarifRule Rule = SarifRule::create().setRuleId(std::to_string(Diag->getID())); + std::string StableName = + Diag->getDiags()->getDiagnosticIDs()->getStableName(Diag->getID()).str(); + std::replace(StableName.begin(), StableName.end(), '_', '.'); + SarifRule Rule = SarifRule::create().setRuleId(StableName); Rule = addDiagnosticLevelToRule(Rule, Level); 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 @@ -70,7 +70,7 @@ // SARIF: "message":{ // SARIF: "text":"'main' must return 'int'" // SARIF: }, -// SARIF: "ruleId":"3485", +// SARIF: "ruleId":"err.main.returns.nonint", // SARIF: "ruleIndex":0 // SARIF: }, // SARIF: { @@ -93,7 +93,7 @@ // SARIF: "message":{ // SARIF: "text":"use of undeclared identifier 'hello'" // SARIF: }, -// SARIF: "ruleId":"4632", +// SARIF: "ruleId":"err.undeclared.var.use", // SARIF: "ruleIndex":1 // SARIF: }, // SARIF: { @@ -116,7 +116,7 @@ // SARIF: "message":{ // SARIF: "text":"invalid digit 'a' in decimal constant" // SARIF: }, -// SARIF: "ruleId":"898", +// SARIF: "ruleId":"err.invalid.digit", // SARIF: "ruleIndex":2 // SARIF: }, // SARIF: { @@ -139,7 +139,7 @@ // SARIF: "message":{ // SARIF: "text":"misleading indentation; statement is not part of the previous 'if'" // SARIF: }, -// SARIF: "ruleId":"1826", +// SARIF: "ruleId":"warn.misleading.indentation", // SARIF: "ruleIndex":3 // SARIF: }, // SARIF: { @@ -162,7 +162,7 @@ // SARIF: "message":{ // SARIF: "text":"previous statement is here" // SARIF: }, -// SARIF: "ruleId":"1746", +// SARIF: "ruleId":"note.previous.statement", // SARIF: "ruleIndex":4 // SARIF: }, // SARIF: { @@ -185,7 +185,7 @@ // SARIF: "message":{ // SARIF: "text":"unused variable 'Yes'" // SARIF: }, -// SARIF: "ruleId":"6593", +// SARIF: "ruleId":"warn.unused.variable", // SARIF: "ruleIndex":5 // SARIF: }, // SARIF: { @@ -208,7 +208,7 @@ // SARIF: "message":{ // SARIF: "text":"use of undeclared identifier 'hi'" // SARIF: }, -// SARIF: "ruleId":"4632", +// SARIF: "ruleId":"err.undeclared.var.use", // SARIF: "ruleIndex":6 // SARIF: }, // SARIF: { @@ -231,7 +231,7 @@ // SARIF: "message":{ // SARIF: "text":"extraneous closing brace ('}')" // SARIF: }, -// SARIF: "ruleId":"1400", +// SARIF: "ruleId":"err.extraneous.closing.brace", // SARIF: "ruleIndex":7 // SARIF: }, // SARIF: { @@ -282,7 +282,7 @@ // SARIF: "message":{ // SARIF: "text":"invalid operands to binary expression ('t1' and 't1')" // SARIF: }, -// SARIF: "ruleId":"4567", +// SARIF: "ruleId":"err.typecheck.invalid.operands", // SARIF: "ruleIndex":8 // SARIF: } // SARIF: ], @@ -302,7 +302,7 @@ // SARIF: "fullDescription":{ // SARIF: "text":"" // SARIF: }, -// SARIF: "id":"3485", +// SARIF: "id":"err.main.returns.nonint", // SARIF: "name":"" // SARIF: }, // SARIF: { @@ -314,7 +314,7 @@ // SARIF: "fullDescription":{ // SARIF: "text":"" // SARIF: }, -// SARIF: "id":"4632", +// SARIF: "id":"err.undeclared.var.use", // SARIF: "name":"" // SARIF: }, // SARIF: { @@ -326,7 +326,7 @@ // SARIF: "fullDescription":{ // SARIF: "text":"" // SARIF: }, -// SARIF: "id":"898", +// SARIF: "id":"err.invalid.digit", // SARIF: "name":"" // SARIF: }, // SARIF: { @@ -338,7 +338,7 @@ // SARIF: "fullDescription":{ // SARIF: "text":"" // SARIF: }, -// SARIF: "id":"1826", +// SARIF: "id":"warn.misleading.indentation", // SARIF: "name":"" // SARIF: }, // SARIF: { @@ -350,7 +350,7 @@ // SARIF: "fullDescription":{ // SARIF: "text":"" // SARIF: }, -// SARIF: "id":"1746", +// SARIF: "id":"note.previous.statement", // SARIF: "name":"" // SARIF: }, // SARIF: { @@ -362,7 +362,7 @@ // SARIF: "fullDescription":{ // SARIF: "text":"" // SARIF: }, -// SARIF: "id":"6593", +// SARIF: "id":"warn.unused.variable", // SARIF: "name":"" // SARIF: }, // SARIF: { @@ -374,7 +374,7 @@ // SARIF: "fullDescription":{ // SARIF: "text":"" // SARIF: }, -// SARIF: "id":"4632", +// SARIF: "id":"err.undeclared.var.use", // SARIF: "name":"" // SARIF: }, // SARIF: { @@ -386,7 +386,7 @@ // SARIF: "fullDescription":{ // SARIF: "text":"" // SARIF: }, -// SARIF: "id":"1400", +// SARIF: "id":"err.extraneous.closing.brace", // SARIF: "name":"" // SARIF: }, // SARIF: { @@ -398,7 +398,7 @@ // SARIF: "fullDescription":{ // SARIF: "text":"" // SARIF: }, -// SARIF: "id":"4567", +// SARIF: "id":"err.typecheck.invalid.operands", // SARIF: "name":"" // SARIF: } // SARIF: ],