1 //===--- SarifDiagnostics.cpp - Sarif Diagnostics for Paths -----*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the SarifDiagnostics object.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang/Basic/Version.h"
15 #include "clang/Lex/Preprocessor.h"
16 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
17 #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
18 #include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/Support/JSON.h"
22 #include "llvm/Support/Path.h"
23
24 using namespace llvm;
25 using namespace clang;
26 using namespace ento;
27
28 namespace {
29 class SarifDiagnostics : public PathDiagnosticConsumer {
30 std::string OutputFile;
31
32 public:
SarifDiagnostics(AnalyzerOptions &,const std::string & Output)33 SarifDiagnostics(AnalyzerOptions &, const std::string &Output)
34 : OutputFile(Output) {}
35 ~SarifDiagnostics() override = default;
36
37 void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
38 FilesMade *FM) override;
39
getName() const40 StringRef getName() const override { return "SarifDiagnostics"; }
getGenerationScheme() const41 PathGenerationScheme getGenerationScheme() const override { return Minimal; }
supportsLogicalOpControlFlow() const42 bool supportsLogicalOpControlFlow() const override { return true; }
supportsCrossFileDiagnostics() const43 bool supportsCrossFileDiagnostics() const override { return true; }
44 };
45 } // end anonymous namespace
46
createSarifDiagnosticConsumer(AnalyzerOptions & AnalyzerOpts,PathDiagnosticConsumers & C,const std::string & Output,const Preprocessor &)47 void ento::createSarifDiagnosticConsumer(AnalyzerOptions &AnalyzerOpts,
48 PathDiagnosticConsumers &C,
49 const std::string &Output,
50 const Preprocessor &) {
51 C.push_back(new SarifDiagnostics(AnalyzerOpts, Output));
52 }
53
getFileName(const FileEntry & FE)54 static StringRef getFileName(const FileEntry &FE) {
55 StringRef Filename = FE.tryGetRealPathName();
56 if (Filename.empty())
57 Filename = FE.getName();
58 return Filename;
59 }
60
percentEncodeURICharacter(char C)61 static std::string percentEncodeURICharacter(char C) {
62 // RFC 3986 claims alpha, numeric, and this handful of
63 // characters are not reserved for the path component and
64 // should be written out directly. Otherwise, percent
65 // encode the character and write that out instead of the
66 // reserved character.
67 if (llvm::isAlnum(C) ||
68 StringRef::npos != StringRef("-._~:@!$&'()*+,;=").find(C))
69 return std::string(&C, 1);
70 return "%" + llvm::toHex(StringRef(&C, 1));
71 }
72
fileNameToURI(StringRef Filename)73 static std::string fileNameToURI(StringRef Filename) {
74 llvm::SmallString<32> Ret = StringRef("file://");
75
76 // Get the root name to see if it has a URI authority.
77 StringRef Root = sys::path::root_name(Filename);
78 if (Root.startswith("//")) {
79 // There is an authority, so add it to the URI.
80 Ret += Root.drop_front(2).str();
81 } else if (!Root.empty()) {
82 // There is no authority, so end the component and add the root to the URI.
83 Ret += Twine("/" + Root).str();
84 }
85
86 auto Iter = sys::path::begin(Filename), End = sys::path::end(Filename);
87 assert(Iter != End && "Expected there to be a non-root path component.");
88 // Add the rest of the path components, encoding any reserved characters;
89 // we skip past the first path component, as it was handled it above.
90 std::for_each(++Iter, End, [&Ret](StringRef Component) {
91 // For reasons unknown to me, we may get a backslash with Windows native
92 // paths for the initial backslash following the drive component, which
93 // we need to ignore as a URI path part.
94 if (Component == "\\")
95 return;
96
97 // Add the separator between the previous path part and the one being
98 // currently processed.
99 Ret += "/";
100
101 // URI encode the part.
102 for (char C : Component) {
103 Ret += percentEncodeURICharacter(C);
104 }
105 });
106
107 return Ret.str().str();
108 }
109
createFileLocation(const FileEntry & FE)110 static json::Object createFileLocation(const FileEntry &FE) {
111 return json::Object{{"uri", fileNameToURI(getFileName(FE))}};
112 }
113
createFile(const FileEntry & FE)114 static json::Object createFile(const FileEntry &FE) {
115 return json::Object{{"fileLocation", createFileLocation(FE)},
116 {"roles", json::Array{"resultFile"}},
117 {"length", FE.getSize()},
118 {"mimeType", "text/plain"}};
119 }
120
createFileLocation(const FileEntry & FE,json::Array & Files)121 static json::Object createFileLocation(const FileEntry &FE,
122 json::Array &Files) {
123 std::string FileURI = fileNameToURI(getFileName(FE));
124
125 // See if the Files array contains this URI already. If it does not, create
126 // a new file object to add to the array.
127 auto I = llvm::find_if(Files, [&](const json::Value &File) {
128 if (const json::Object *Obj = File.getAsObject()) {
129 if (const json::Object *FileLoc = Obj->getObject("fileLocation")) {
130 Optional<StringRef> URI = FileLoc->getString("uri");
131 return URI && URI->equals(FileURI);
132 }
133 }
134 return false;
135 });
136
137 // Calculate the index within the file location array so it can be stored in
138 // the JSON object.
139 auto Index = static_cast<unsigned>(std::distance(Files.begin(), I));
140 if (I == Files.end())
141 Files.push_back(createFile(FE));
142
143 return json::Object{{"uri", FileURI}, {"fileIndex", Index}};
144 }
145
createTextRegion(SourceRange R,const SourceManager & SM)146 static json::Object createTextRegion(SourceRange R, const SourceManager &SM) {
147 return json::Object{
148 {"startLine", SM.getExpansionLineNumber(R.getBegin())},
149 {"endLine", SM.getExpansionLineNumber(R.getEnd())},
150 {"startColumn", SM.getExpansionColumnNumber(R.getBegin())},
151 {"endColumn", SM.getExpansionColumnNumber(R.getEnd())}};
152 }
153
createPhysicalLocation(SourceRange R,const FileEntry & FE,const SourceManager & SMgr,json::Array & Files)154 static json::Object createPhysicalLocation(SourceRange R, const FileEntry &FE,
155 const SourceManager &SMgr,
156 json::Array &Files) {
157 return json::Object{{{"fileLocation", createFileLocation(FE, Files)},
158 {"region", createTextRegion(R, SMgr)}}};
159 }
160
161 enum class Importance { Important, Essential, Unimportant };
162
importanceToStr(Importance I)163 static StringRef importanceToStr(Importance I) {
164 switch (I) {
165 case Importance::Important:
166 return "important";
167 case Importance::Essential:
168 return "essential";
169 case Importance::Unimportant:
170 return "unimportant";
171 }
172 llvm_unreachable("Fully covered switch is not so fully covered");
173 }
174
createThreadFlowLocation(json::Object && Location,Importance I)175 static json::Object createThreadFlowLocation(json::Object &&Location,
176 Importance I) {
177 return json::Object{{"location", std::move(Location)},
178 {"importance", importanceToStr(I)}};
179 }
180
createMessage(StringRef Text)181 static json::Object createMessage(StringRef Text) {
182 return json::Object{{"text", Text.str()}};
183 }
184
createLocation(json::Object && PhysicalLocation,StringRef Message="")185 static json::Object createLocation(json::Object &&PhysicalLocation,
186 StringRef Message = "") {
187 json::Object Ret{{"physicalLocation", std::move(PhysicalLocation)}};
188 if (!Message.empty())
189 Ret.insert({"message", createMessage(Message)});
190 return Ret;
191 }
192
calculateImportance(const PathDiagnosticPiece & Piece)193 static Importance calculateImportance(const PathDiagnosticPiece &Piece) {
194 switch (Piece.getKind()) {
195 case PathDiagnosticPiece::Kind::Call:
196 case PathDiagnosticPiece::Kind::Macro:
197 case PathDiagnosticPiece::Kind::Note:
198 // FIXME: What should be reported here?
199 break;
200 case PathDiagnosticPiece::Kind::Event:
201 return Piece.getTagStr() == "ConditionBRVisitor" ? Importance::Important
202 : Importance::Essential;
203 case PathDiagnosticPiece::Kind::ControlFlow:
204 return Importance::Unimportant;
205 }
206 return Importance::Unimportant;
207 }
208
createThreadFlow(const PathPieces & Pieces,json::Array & Files)209 static json::Object createThreadFlow(const PathPieces &Pieces,
210 json::Array &Files) {
211 const SourceManager &SMgr = Pieces.front()->getLocation().getManager();
212 json::Array Locations;
213 for (const auto &Piece : Pieces) {
214 const PathDiagnosticLocation &P = Piece->getLocation();
215 Locations.push_back(createThreadFlowLocation(
216 createLocation(createPhysicalLocation(P.asRange(),
217 *P.asLocation().getFileEntry(),
218 SMgr, Files),
219 Piece->getString()),
220 calculateImportance(*Piece)));
221 }
222 return json::Object{{"locations", std::move(Locations)}};
223 }
224
createCodeFlow(const PathPieces & Pieces,json::Array & Files)225 static json::Object createCodeFlow(const PathPieces &Pieces,
226 json::Array &Files) {
227 return json::Object{
228 {"threadFlows", json::Array{createThreadFlow(Pieces, Files)}}};
229 }
230
createTool()231 static json::Object createTool() {
232 return json::Object{{"name", "clang"},
233 {"fullName", "clang static analyzer"},
234 {"language", "en-US"},
235 {"version", getClangFullVersion()}};
236 }
237
createResult(const PathDiagnostic & Diag,json::Array & Files,const StringMap<unsigned> & RuleMapping)238 static json::Object createResult(const PathDiagnostic &Diag, json::Array &Files,
239 const StringMap<unsigned> &RuleMapping) {
240 const PathPieces &Path = Diag.path.flatten(false);
241 const SourceManager &SMgr = Path.front()->getLocation().getManager();
242
243 auto Iter = RuleMapping.find(Diag.getCheckName());
244 assert(Iter != RuleMapping.end() && "Rule ID is not in the array index map?");
245
246 return json::Object{
247 {"message", createMessage(Diag.getVerboseDescription())},
248 {"codeFlows", json::Array{createCodeFlow(Path, Files)}},
249 {"locations",
250 json::Array{createLocation(createPhysicalLocation(
251 Diag.getLocation().asRange(),
252 *Diag.getLocation().asLocation().getFileEntry(), SMgr, Files))}},
253 {"ruleIndex", Iter->getValue()},
254 {"ruleId", Diag.getCheckName()}};
255 }
256
getRuleDescription(StringRef CheckName)257 static StringRef getRuleDescription(StringRef CheckName) {
258 return llvm::StringSwitch<StringRef>(CheckName)
259 #define GET_CHECKERS
260 #define CHECKER(FULLNAME, CLASS, HELPTEXT, DOC_URI) \
261 .Case(FULLNAME, HELPTEXT)
262 #include "clang/StaticAnalyzer/Checkers/Checkers.inc"
263 #undef CHECKER
264 #undef GET_CHECKERS
265 ;
266 }
267
getRuleHelpURIStr(StringRef CheckName)268 static StringRef getRuleHelpURIStr(StringRef CheckName) {
269 return llvm::StringSwitch<StringRef>(CheckName)
270 #define GET_CHECKERS
271 #define CHECKER(FULLNAME, CLASS, HELPTEXT, DOC_URI) \
272 .Case(FULLNAME, DOC_URI)
273 #include "clang/StaticAnalyzer/Checkers/Checkers.inc"
274 #undef CHECKER
275 #undef GET_CHECKERS
276 ;
277 }
278
createRule(const PathDiagnostic & Diag)279 static json::Object createRule(const PathDiagnostic &Diag) {
280 StringRef CheckName = Diag.getCheckName();
281 json::Object Ret{
282 {"fullDescription", createMessage(getRuleDescription(CheckName))},
283 {"name", createMessage(CheckName)},
284 {"id", CheckName}};
285
286 std::string RuleURI = getRuleHelpURIStr(CheckName);
287 if (!RuleURI.empty())
288 Ret["helpUri"] = RuleURI;
289
290 return Ret;
291 }
292
createRules(std::vector<const PathDiagnostic * > & Diags,StringMap<unsigned> & RuleMapping)293 static json::Array createRules(std::vector<const PathDiagnostic *> &Diags,
294 StringMap<unsigned> &RuleMapping) {
295 json::Array Rules;
296 llvm::StringSet<> Seen;
297
298 llvm::for_each(Diags, [&](const PathDiagnostic *D) {
299 StringRef RuleID = D->getCheckName();
300 std::pair<llvm::StringSet<>::iterator, bool> P = Seen.insert(RuleID);
301 if (P.second) {
302 RuleMapping[RuleID] = Rules.size(); // Maps RuleID to an Array Index.
303 Rules.push_back(createRule(*D));
304 }
305 });
306
307 return Rules;
308 }
309
createResources(std::vector<const PathDiagnostic * > & Diags,StringMap<unsigned> & RuleMapping)310 static json::Object createResources(std::vector<const PathDiagnostic *> &Diags,
311 StringMap<unsigned> &RuleMapping) {
312 return json::Object{{"rules", createRules(Diags, RuleMapping)}};
313 }
314
createRun(std::vector<const PathDiagnostic * > & Diags)315 static json::Object createRun(std::vector<const PathDiagnostic *> &Diags) {
316 json::Array Results, Files;
317 StringMap<unsigned> RuleMapping;
318 json::Object Resources = createResources(Diags, RuleMapping);
319
320 llvm::for_each(Diags, [&](const PathDiagnostic *D) {
321 Results.push_back(createResult(*D, Files, RuleMapping));
322 });
323
324 return json::Object{{"tool", createTool()},
325 {"resources", std::move(Resources)},
326 {"results", std::move(Results)},
327 {"files", std::move(Files)}};
328 }
329
FlushDiagnosticsImpl(std::vector<const PathDiagnostic * > & Diags,FilesMade *)330 void SarifDiagnostics::FlushDiagnosticsImpl(
331 std::vector<const PathDiagnostic *> &Diags, FilesMade *) {
332 // We currently overwrite the file if it already exists. However, it may be
333 // useful to add a feature someday that allows the user to append a run to an
334 // existing SARIF file. One danger from that approach is that the size of the
335 // file can become large very quickly, so decoding into JSON to append a run
336 // may be an expensive operation.
337 std::error_code EC;
338 llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::F_Text);
339 if (EC) {
340 llvm::errs() << "warning: could not create file: " << EC.message() << '\n';
341 return;
342 }
343 json::Object Sarif{
344 {"$schema",
345 "http://json.schemastore.org/sarif-2.0.0-csd.2.beta.2018-11-28"},
346 {"version", "2.0.0-csd.2.beta.2018-11-28"},
347 {"runs", json::Array{createRun(Diags)}}};
348 OS << llvm::formatv("{0:2}", json::Value(std::move(Sarif)));
349 }
350