r349188 - Update our SARIF support from 10-10 to 11-28.
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Fri Dec 14 12:34:23 PST 2018
Author: aaronballman
Date: Fri Dec 14 12:34:23 2018
New Revision: 349188
URL: http://llvm.org/viewvc/llvm-project?rev=349188&view=rev
Log:
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.
Modified:
cfe/trunk/lib/StaticAnalyzer/Core/SarifDiagnostics.cpp
cfe/trunk/test/Analysis/diagnostics/Inputs/expected-sarif/sarif-diagnostics-taint-test.c.sarif
cfe/trunk/test/Analysis/diagnostics/Inputs/expected-sarif/sarif-multi-diagnostic-test.c.sarif
Modified: cfe/trunk/lib/StaticAnalyzer/Core/SarifDiagnostics.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/SarifDiagnostics.cpp?rev=349188&r1=349187&r2=349188&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/SarifDiagnostics.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/SarifDiagnostics.cpp Fri Dec 14 12:34:23 2018
@@ -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(StringR
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 Fil
}
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<StringRef> 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(Sou
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(co
}
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(con
}
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<unsigned> &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 P
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 Pat
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<const PathDiagnostic *> &Diags) {
- json::Object Rules;
+static json::Array createRules(std::vector<const PathDiagnostic *> &Diags,
+ StringMap<unsigned> &RuleMapping) {
+ json::Array Rules;
llvm::StringSet<> Seen;
llvm::for_each(Diags, [&](const PathDiagnostic *D) {
StringRef RuleID = D->getCheckName();
std::pair<llvm::StringSet<>::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<const PathDiagnostic *> &Diags) {
- return json::Object{{"rules", createRules(Diags)}};
+static json::Object createResources(std::vector<const PathDiagnostic *> &Diags,
+ StringMap<unsigned> &RuleMapping) {
+ return json::Object{{"rules", createRules(Diags, RuleMapping)}};
}
static json::Object createRun(std::vector<const PathDiagnostic *> &Diags) {
- json::Array Results;
- json::Object Files;
-
+ json::Array Results, Files;
+ StringMap<unsigned> 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::FlushDiagnosticsI
}
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)));
}
Modified: cfe/trunk/test/Analysis/diagnostics/Inputs/expected-sarif/sarif-diagnostics-taint-test.c.sarif
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/diagnostics/Inputs/expected-sarif/sarif-diagnostics-taint-test.c.sarif?rev=349188&r1=349187&r2=349188&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/diagnostics/Inputs/expected-sarif/sarif-diagnostics-taint-test.c.sarif (original)
+++ cfe/trunk/test/Analysis/diagnostics/Inputs/expected-sarif/sarif-diagnostics-taint-test.c.sarif Fri Dec 14 12:34:23 2018
@@ -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"
}
Modified: cfe/trunk/test/Analysis/diagnostics/Inputs/expected-sarif/sarif-multi-diagnostic-test.c.sarif
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/diagnostics/Inputs/expected-sarif/sarif-multi-diagnostic-test.c.sarif?rev=349188&r1=349187&r2=349188&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/diagnostics/Inputs/expected-sarif/sarif-multi-diagnostic-test.c.sarif (original)
+++ cfe/trunk/test/Analysis/diagnostics/Inputs/expected-sarif/sarif-multi-diagnostic-test.c.sarif Fri Dec 14 12:34:23 2018
@@ -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"
}
More information about the cfe-commits
mailing list