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