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