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