From b53ab0e23fc3229a6e9815c0602165114591a911 Mon Sep 17 00:00:00 2001 From: Aaron Ballman Date: Fri, 14 Dec 2018 20:34:23 +0000 Subject: [PATCH] Update our SARIF support from 10-10 to 11-28. Functional changes include: * The run.files property is now an array instead of a mapping. * fileLocation objects now have a fileIndex property specifying the array index into run.files. * The resource.rules property is now an array instead of a mapping. * The result object was given a ruleIndex property that is an index into the resource.rules array. * rule objects now have their "id" field filled out in addition to the name field. * Updated the schema and spec version numbers to 11-28. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@349188 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/StaticAnalyzer/Core/SarifDiagnostics.cpp | 79 +++++++++++++------ .../sarif-diagnostics-taint-test.c.sarif | 23 +++--- .../sarif-multi-diagnostic-test.c.sarif | 57 ++++++++----- 3 files changed, 105 insertions(+), 54 deletions(-) diff --git a/lib/StaticAnalyzer/Core/SarifDiagnostics.cpp b/lib/StaticAnalyzer/Core/SarifDiagnostics.cpp index ee86195758..94894a393a 100644 --- a/lib/StaticAnalyzer/Core/SarifDiagnostics.cpp +++ b/lib/StaticAnalyzer/Core/SarifDiagnostics.cpp @@ -17,6 +17,7 @@ #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h" #include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h" #include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/StringMap.h" #include "llvm/Support/JSON.h" #include "llvm/Support/Path.h" @@ -77,7 +78,7 @@ static std::string fileNameToURI(StringRef Filename) { if (Root.startswith("//")) { // There is an authority, so add it to the URI. Ret += Root.drop_front(2).str(); - } else { + } else if (!Root.empty()) { // There is no authority, so end the component and add the root to the URI. Ret += Twine("/" + Root).str(); } @@ -118,12 +119,31 @@ static json::Object createFile(const FileEntry &FE) { } static json::Object createFileLocation(const FileEntry &FE, - json::Object &Files) { + json::Array &Files) { std::string FileURI = fileNameToURI(getFileName(FE)); - if (!Files.get(FileURI)) - Files[FileURI] = createFile(FE); - return json::Object{{"uri", FileURI}}; + // See if the Files array contains this URI already. If it does not, create + // a new file object to add to the array. Calculate the index within the file + // location array so it can be stored in the JSON object. + unsigned Index = 0; + for (const json::Value &File : Files) { + if (const json::Object *Obj = File.getAsObject()) { + if (const json::Object *FileLoc = Obj->getObject("fileLocation")) { + Optional URI = FileLoc->getString("uri"); + if (URI && URI->equals(FileURI)) + break; + } + } + ++Index; + } + + // If we reached the end of the array, then add the file to the list of files + // we're tracking; Index then points to the last element of the array. Note + // that an empty file lists always causes a file to be added. + if (Files.empty() || Index == Files.size()) + Files.push_back(createFile(FE)); + + return json::Object{{"uri", FileURI}, {"fileIndex", Index}}; } static json::Object createTextRegion(SourceRange R, const SourceManager &SM) { @@ -136,7 +156,7 @@ static json::Object createTextRegion(SourceRange R, const SourceManager &SM) { static json::Object createPhysicalLocation(SourceRange R, const FileEntry &FE, const SourceManager &SMgr, - json::Object &Files) { + json::Array &Files) { return json::Object{{{"fileLocation", createFileLocation(FE, Files)}, {"region", createTextRegion(R, SMgr)}}}; } @@ -190,7 +210,7 @@ static Importance calculateImportance(const PathDiagnosticPiece &Piece) { } static json::Object createThreadFlow(const PathPieces &Pieces, - json::Object &Files) { + json::Array &Files) { const SourceManager &SMgr = Pieces.front()->getLocation().getManager(); json::Array Locations; for (const auto &Piece : Pieces) { @@ -206,7 +226,7 @@ static json::Object createThreadFlow(const PathPieces &Pieces, } static json::Object createCodeFlow(const PathPieces &Pieces, - json::Object &Files) { + json::Array &Files) { return json::Object{ {"threadFlows", json::Array{createThreadFlow(Pieces, Files)}}}; } @@ -218,11 +238,14 @@ static json::Object createTool() { {"version", getClangFullVersion()}}; } -static json::Object createResult(const PathDiagnostic &Diag, - json::Object &Files) { +static json::Object createResult(const PathDiagnostic &Diag, json::Array &Files, + const StringMap &RuleMapping) { const PathPieces &Path = Diag.path.flatten(false); const SourceManager &SMgr = Path.front()->getLocation().getManager(); + auto Iter = RuleMapping.find(Diag.getCheckName()); + assert(Iter != RuleMapping.end() && "Rule ID is not in the array index map?"); + return json::Object{ {"message", createMessage(Diag.getVerboseDescription())}, {"codeFlows", json::Array{createCodeFlow(Path, Files)}}, @@ -230,6 +253,7 @@ static json::Object createResult(const PathDiagnostic &Diag, json::Array{createLocation(createPhysicalLocation( Diag.getLocation().asRange(), *Diag.getLocation().asLocation().getFileEntry(), SMgr, Files))}}, + {"ruleIndex", Iter->getValue()}, {"ruleId", Diag.getCheckName()}}; } @@ -248,38 +272,43 @@ static json::Object createRule(const PathDiagnostic &Diag) { StringRef CheckName = Diag.getCheckName(); return json::Object{ {"fullDescription", createMessage(getRuleDescription(CheckName))}, - {"name", createMessage(CheckName)}}; + {"name", createMessage(CheckName)}, + {"id", CheckName}}; } -static json::Object createRules(std::vector &Diags) { - json::Object Rules; +static json::Array createRules(std::vector &Diags, + StringMap &RuleMapping) { + json::Array Rules; llvm::StringSet<> Seen; llvm::for_each(Diags, [&](const PathDiagnostic *D) { StringRef RuleID = D->getCheckName(); std::pair::iterator, bool> P = Seen.insert(RuleID); - if (P.second) - Rules[RuleID] = createRule(*D); + if (P.second) { + RuleMapping[RuleID] = Rules.size(); // Maps RuleID to an Array Index. + Rules.push_back(createRule(*D)); + } }); return Rules; } -static json::Object -createResources(std::vector &Diags) { - return json::Object{{"rules", createRules(Diags)}}; +static json::Object createResources(std::vector &Diags, + StringMap &RuleMapping) { + return json::Object{{"rules", createRules(Diags, RuleMapping)}}; } static json::Object createRun(std::vector &Diags) { - json::Array Results; - json::Object Files; - + json::Array Results, Files; + StringMap RuleMapping; + json::Object Resources = createResources(Diags, RuleMapping); + llvm::for_each(Diags, [&](const PathDiagnostic *D) { - Results.push_back(createResult(*D, Files)); + Results.push_back(createResult(*D, Files, RuleMapping)); }); return json::Object{{"tool", createTool()}, - {"resources", createResources(Diags)}, + {"resources", std::move(Resources)}, {"results", std::move(Results)}, {"files", std::move(Files)}}; } @@ -299,8 +328,8 @@ void SarifDiagnostics::FlushDiagnosticsImpl( } json::Object Sarif{ {"$schema", - "http://json.schemastore.org/sarif-2.0.0-csd.2.beta.2018-10-10"}, - {"version", "2.0.0-csd.2.beta.2018-10-10"}, + "http://json.schemastore.org/sarif-2.0.0-csd.2.beta.2018-11-28"}, + {"version", "2.0.0-csd.2.beta.2018-11-28"}, {"runs", json::Array{createRun(Diags)}}}; OS << llvm::formatv("{0:2}", json::Value(std::move(Sarif))); } diff --git a/test/Analysis/diagnostics/Inputs/expected-sarif/sarif-diagnostics-taint-test.c.sarif b/test/Analysis/diagnostics/Inputs/expected-sarif/sarif-diagnostics-taint-test.c.sarif index d6c7747974..cdf4a2daa2 100644 --- a/test/Analysis/diagnostics/Inputs/expected-sarif/sarif-diagnostics-taint-test.c.sarif +++ b/test/Analysis/diagnostics/Inputs/expected-sarif/sarif-diagnostics-taint-test.c.sarif @@ -1,9 +1,9 @@ { - "$schema": "http://json.schemastore.org/sarif-2.0.0-csd.2.beta.2018-10-10", + "$schema": "http://json.schemastore.org/sarif-2.0.0-csd.2.beta.2018-11-28", "runs": [ { - "files": { - "file:sarif-diagnostics-taint-test.c": { + "files": [ + { "fileLocation": { "uri": "file:sarif-diagnostics-taint-test.c" }, @@ -13,18 +13,19 @@ "resultFile" ] } - }, + ], "resources": { - "rules": { - "debug.TaintTest": { + "rules": [ + { "fullDescription": { "text": "Mark tainted symbols as such." }, + "id": "debug.TaintTest", "name": { "text": "debug.TaintTest" } } - } + ] }, "results": [ { @@ -41,6 +42,7 @@ }, "physicalLocation": { "fileLocation": { + "fileIndex": 0, "uri": "file:sarif-diagnostics-taint-test.c" }, "region": { @@ -60,6 +62,7 @@ }, "physicalLocation": { "fileLocation": { + "fileIndex": 0, "uri": "file:sarif-diagnostics-taint-test.c" }, "region": { @@ -80,6 +83,7 @@ { "physicalLocation": { "fileLocation": { + "fileIndex": 0, "uri": "file:sarif-diagnostics-taint-test.c" }, "region": { @@ -94,7 +98,8 @@ "message": { "text": "tainted" }, - "ruleId": "debug.TaintTest" + "ruleId": "debug.TaintTest", + "ruleIndex": 0 } ], "tool": { @@ -105,5 +110,5 @@ } } ], - "version": "2.0.0-csd.2.beta.2018-10-10" + "version": "2.0.0-csd.2.beta.2018-11-28" } diff --git a/test/Analysis/diagnostics/Inputs/expected-sarif/sarif-multi-diagnostic-test.c.sarif b/test/Analysis/diagnostics/Inputs/expected-sarif/sarif-multi-diagnostic-test.c.sarif index ae3c5b1fbc..4b581b2e0f 100644 --- a/test/Analysis/diagnostics/Inputs/expected-sarif/sarif-multi-diagnostic-test.c.sarif +++ b/test/Analysis/diagnostics/Inputs/expected-sarif/sarif-multi-diagnostic-test.c.sarif @@ -1,9 +1,9 @@ { - "$schema": "http://json.schemastore.org/sarif-2.0.0-csd.2.beta.2018-10-10", + "$schema": "http://json.schemastore.org/sarif-2.0.0-csd.2.beta.2018-11-28", "runs": [ { - "files": { - "file:sarif-multi-diagnostic-test.c": { + "files": [ + { "fileLocation": { "uri": "file:sarif-multi-diagnostic-test.c" }, @@ -13,34 +13,37 @@ "resultFile" ] } - }, + ], "resources": { - "rules": { - "core.CallAndMessage": { + "rules": [ + { "fullDescription": { - "text": "Check for logical errors for function calls and Objective-C message expressions (e.g., uninitialized arguments, null function pointers)" + "text": "Mark tainted symbols as such." }, + "id": "debug.TaintTest", "name": { - "text": "core.CallAndMessage" + "text": "debug.TaintTest" } - }, - "core.DivideZero": { + }, + { "fullDescription": { - "text": "Check for division by zero" + "text": "Check for logical errors for function calls and Objective-C message expressions (e.g., uninitialized arguments, null function pointers)" }, + "id": "core.CallAndMessage", "name": { - "text": "core.DivideZero" + "text": "core.CallAndMessage" } }, - "debug.TaintTest": { + { "fullDescription": { - "text": "Mark tainted symbols as such." + "text": "Check for division by zero" }, + "id": "core.DivideZero", "name": { - "text": "debug.TaintTest" + "text": "core.DivideZero" } } - } + ] }, "results": [ { @@ -57,6 +60,7 @@ }, "physicalLocation": { "fileLocation": { + "fileIndex": 0, "uri": "file:sarif-multi-diagnostic-test.c" }, "region": { @@ -76,6 +80,7 @@ }, "physicalLocation": { "fileLocation": { + "fileIndex": 0, "uri": "file:sarif-multi-diagnostic-test.c" }, "region": { @@ -96,6 +101,7 @@ { "physicalLocation": { "fileLocation": { + "fileIndex": 0, "uri": "file:sarif-multi-diagnostic-test.c" }, "region": { @@ -110,7 +116,8 @@ "message": { "text": "tainted" }, - "ruleId": "debug.TaintTest" + "ruleId": "debug.TaintTest", + "ruleIndex": 0 }, { "codeFlows": [ @@ -126,6 +133,7 @@ }, "physicalLocation": { "fileLocation": { + "fileIndex": 0, "uri": "file:sarif-multi-diagnostic-test.c" }, "region": { @@ -145,6 +153,7 @@ }, "physicalLocation": { "fileLocation": { + "fileIndex": 0, "uri": "file:sarif-multi-diagnostic-test.c" }, "region": { @@ -164,6 +173,7 @@ }, "physicalLocation": { "fileLocation": { + "fileIndex": 0, "uri": "file:sarif-multi-diagnostic-test.c" }, "region": { @@ -184,6 +194,7 @@ { "physicalLocation": { "fileLocation": { + "fileIndex": 0, "uri": "file:sarif-multi-diagnostic-test.c" }, "region": { @@ -198,7 +209,8 @@ "message": { "text": "Called function pointer is an uninitialized pointer value" }, - "ruleId": "core.CallAndMessage" + "ruleId": "core.CallAndMessage", + "ruleIndex": 1 }, { "codeFlows": [ @@ -214,6 +226,7 @@ }, "physicalLocation": { "fileLocation": { + "fileIndex": 0, "uri": "file:sarif-multi-diagnostic-test.c" }, "region": { @@ -233,6 +246,7 @@ }, "physicalLocation": { "fileLocation": { + "fileIndex": 0, "uri": "file:sarif-multi-diagnostic-test.c" }, "region": { @@ -252,6 +266,7 @@ }, "physicalLocation": { "fileLocation": { + "fileIndex": 0, "uri": "file:sarif-multi-diagnostic-test.c" }, "region": { @@ -272,6 +287,7 @@ { "physicalLocation": { "fileLocation": { + "fileIndex": 0, "uri": "file:sarif-multi-diagnostic-test.c" }, "region": { @@ -286,7 +302,8 @@ "message": { "text": "Division by zero" }, - "ruleId": "core.DivideZero" + "ruleId": "core.DivideZero", + "ruleIndex": 2 } ], "tool": { @@ -297,5 +314,5 @@ } } ], - "version": "2.0.0-csd.2.beta.2018-10-10" + "version": "2.0.0-csd.2.beta.2018-11-28" } -- 2.40.0