Index: lib/StaticAnalyzer/Core/SarifDiagnostics.cpp =================================================================== --- lib/StaticAnalyzer/Core/SarifDiagnostics.cpp +++ 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" @@ -118,12 +119,31 @@ } 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 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,11 +210,12 @@ } 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) { const PathDiagnosticLocation &P = Piece->getLocation(); + const auto *CP = dyn_cast(Piece.get()); Locations.push_back(createThreadFlowLocation( createLocation(createPhysicalLocation(P.asRange(), *P.asLocation().getFileEntry(), @@ -206,7 +227,7 @@ } static json::Object createCodeFlow(const PathPieces &Pieces, - json::Object &Files) { + json::Array &Files) { return json::Object{ {"threadFlows", json::Array{createThreadFlow(Pieces, Files)}}}; } @@ -218,11 +239,14 @@ {"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 +254,7 @@ json::Array{createLocation(createPhysicalLocation( Diag.getLocation().asRange(), *Diag.getLocation().asLocation().getFileEntry(), SMgr, Files))}}, + {"ruleIndex", Iter->getValue()}, {"ruleId", Diag.getCheckName()}}; } @@ -248,38 +273,43 @@ 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 +329,8 @@ } 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))); } Index: test/Analysis/diagnostics/Inputs/expected-sarif/sarif-diagnostics-taint-test.c.sarif =================================================================== --- test/Analysis/diagnostics/Inputs/expected-sarif/sarif-diagnostics-taint-test.c.sarif +++ 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" } Index: test/Analysis/diagnostics/Inputs/expected-sarif/sarif-multi-diagnostic-test.c.sarif =================================================================== --- test/Analysis/diagnostics/Inputs/expected-sarif/sarif-multi-diagnostic-test.c.sarif +++ 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" }