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