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   if (Iter != End) {
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 
107   return Ret.str().str();
108 }
109 
110 static json::Object createFileLocation(const FileEntry &FE) {
111   return json::Object{{"uri", fileNameToURI(getFileName(FE))}};
112 }
113 
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 
121 static json::Object createFileLocation(const FileEntry &FE,
122                                        json::Object &Files) {
123   std::string FileURI = fileNameToURI(getFileName(FE));
124   if (!Files.get(FileURI))
125     Files[FileURI] = createFile(FE);
126 
127   return json::Object{{"uri", FileURI}};
128 }
129 
130 static json::Object createTextRegion(SourceRange R, const SourceManager &SM) {
131   return json::Object{
132       {"startLine", SM.getExpansionLineNumber(R.getBegin())},
133       {"endLine", SM.getExpansionLineNumber(R.getEnd())},
134       {"startColumn", SM.getExpansionColumnNumber(R.getBegin())},
135       {"endColumn", SM.getExpansionColumnNumber(R.getEnd())}};
136 }
137 
138 static json::Object createPhysicalLocation(SourceRange R, const FileEntry &FE,
139                                            const SourceManager &SMgr,
140                                            json::Object &Files) {
141   return json::Object{{{"fileLocation", createFileLocation(FE, Files)},
142                        {"region", createTextRegion(R, SMgr)}}};
143 }
144 
145 enum class Importance { Important, Essential, Unimportant };
146 
147 static StringRef importanceToStr(Importance I) {
148   switch (I) {
149   case Importance::Important:
150     return "important";
151   case Importance::Essential:
152     return "essential";
153   case Importance::Unimportant:
154     return "unimportant";
155   }
156   llvm_unreachable("Fully covered switch is not so fully covered");
157 }
158 
159 static json::Object createThreadFlowLocation(json::Object &&Location,
160                                              Importance I) {
161   return json::Object{{"location", std::move(Location)},
162                       {"importance", importanceToStr(I)}};
163 }
164 
165 static json::Object createMessage(StringRef Text) {
166   return json::Object{{"text", Text.str()}};
167 }
168 
169 static json::Object createLocation(json::Object &&PhysicalLocation,
170                                    StringRef Message = "") {
171   json::Object Ret{{"physicalLocation", std::move(PhysicalLocation)}};
172   if (!Message.empty())
173     Ret.insert({"message", createMessage(Message)});
174   return Ret;
175 }
176 
177 static Importance calculateImportance(const PathDiagnosticPiece &Piece) {
178   switch (Piece.getKind()) {
179   case PathDiagnosticPiece::Kind::Call:
180   case PathDiagnosticPiece::Kind::Macro:
181   case PathDiagnosticPiece::Kind::Note:
182     // FIXME: What should be reported here?
183     break;
184   case PathDiagnosticPiece::Kind::Event:
185     return Piece.getTagStr() == "ConditionBRVisitor" ? Importance::Important
186                                                      : Importance::Essential;
187   case PathDiagnosticPiece::Kind::ControlFlow:
188     return Importance::Unimportant;
189   }
190   return Importance::Unimportant;
191 }
192 
193 static json::Object createThreadFlow(const PathPieces &Pieces,
194                                      json::Object &Files) {
195   const SourceManager &SMgr = Pieces.front()->getLocation().getManager();
196   json::Array Locations;
197   for (const auto &Piece : Pieces) {
198     const PathDiagnosticLocation &P = Piece->getLocation();
199     Locations.push_back(createThreadFlowLocation(
200         createLocation(createPhysicalLocation(P.asRange(),
201                                               *P.asLocation().getFileEntry(),
202                                               SMgr, Files),
203                        Piece->getString()),
204         calculateImportance(*Piece)));
205   }
206   return json::Object{{"locations", std::move(Locations)}};
207 }
208 
209 static json::Object createCodeFlow(const PathPieces &Pieces,
210                                    json::Object &Files) {
211   return json::Object{
212       {"threadFlows", json::Array{createThreadFlow(Pieces, Files)}}};
213 }
214 
215 static json::Object createTool() {
216   return json::Object{{"name", "clang"},
217                       {"fullName", "clang static analyzer"},
218                       {"language", "en-US"},
219                       {"version", getClangFullVersion()}};
220 }
221 
222 static json::Object createResult(const PathDiagnostic &Diag,
223                                  json::Object &Files) {
224   const PathPieces &Path = Diag.path.flatten(false);
225   const SourceManager &SMgr = Path.front()->getLocation().getManager();
226 
227   return json::Object{
228       {"message", createMessage(Diag.getVerboseDescription())},
229       {"codeFlows", json::Array{createCodeFlow(Path, Files)}},
230       {"locations",
231        json::Array{createLocation(createPhysicalLocation(
232            Diag.getLocation().asRange(),
233            *Diag.getLocation().asLocation().getFileEntry(), SMgr, Files))}},
234       {"ruleId", Diag.getCheckName()}};
235 }
236 
237 static StringRef getRuleDescription(StringRef CheckName) {
238   return llvm::StringSwitch<StringRef>(CheckName)
239 #define GET_CHECKERS
240 #define CHECKER(FULLNAME, CLASS, CXXFILE, HELPTEXT, GROUPINDEX, HIDDEN)        \
241   .Case(FULLNAME, HELPTEXT)
242 #include "clang/StaticAnalyzer/Checkers/Checkers.inc"
243 #undef CHECKER
244 #undef GET_CHECKERS
245       ;
246 }
247 
248 static json::Object createRule(const PathDiagnostic &Diag) {
249   StringRef CheckName = Diag.getCheckName();
250   return json::Object{
251       {"fullDescription", createMessage(getRuleDescription(CheckName))},
252       {"name", createMessage(CheckName)}};
253 }
254 
255 static json::Object createRules(std::vector<const PathDiagnostic *> &Diags) {
256   json::Object Rules;
257   llvm::StringSet<> Seen;
258 
259   llvm::for_each(Diags, [&](const PathDiagnostic *D) {
260     StringRef RuleID = D->getCheckName();
261     std::pair<llvm::StringSet<>::iterator, bool> P = Seen.insert(RuleID);
262     if (P.second)
263       Rules[RuleID] = createRule(*D);
264   });
265 
266   return Rules;
267 }
268 
269 static json::Object
270 createResources(std::vector<const PathDiagnostic *> &Diags) {
271   return json::Object{{"rules", createRules(Diags)}};
272 }
273 
274 static json::Object createRun(std::vector<const PathDiagnostic *> &Diags) {
275   json::Array Results;
276   json::Object Files;
277 
278   llvm::for_each(Diags, [&](const PathDiagnostic *D) {
279     Results.push_back(createResult(*D, Files));
280   });
281 
282   return json::Object{{"tool", createTool()},
283                       {"resources", createResources(Diags)},
284                       {"results", std::move(Results)},
285                       {"files", std::move(Files)}};
286 }
287 
288 void SarifDiagnostics::FlushDiagnosticsImpl(
289     std::vector<const PathDiagnostic *> &Diags, FilesMade *) {
290   // We currently overwrite the file if it already exists. However, it may be
291   // useful to add a feature someday that allows the user to append a run to an
292   // existing SARIF file. One danger from that approach is that the size of the
293   // file can become large very quickly, so decoding into JSON to append a run
294   // may be an expensive operation.
295   std::error_code EC;
296   llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::F_Text);
297   if (EC) {
298     llvm::errs() << "warning: could not create file: " << EC.message() << '\n';
299     return;
300   }
301   json::Object Sarif{
302       {"$schema",
303        "http://json.schemastore.org/sarif-2.0.0-csd.2.beta.2018-10-10"},
304       {"version", "2.0.0-csd.2.beta.2018-10-10"},
305       {"runs", json::Array{createRun(Diags)}}};
306   OS << llvm::formatv("{0:2}", json::Value(std::move(Sarif)));
307 }
308