Coverage Report

Created: 2020-09-15 12:33

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/StaticAnalyzer/Core/SarifDiagnostics.cpp
Line
Count
Source (jump to first uncovered line)
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:
35
  SarifDiagnostics(AnalyzerOptions &, const std::string &Output,
36
                   const LangOptions &LO)
37
2
      : OutputFile(Output), LO(LO) {}
38
2
  ~SarifDiagnostics() override = default;
39
40
  void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
41
                            FilesMade *FM) override;
42
43
0
  StringRef getName() const override { return "SarifDiagnostics"; }
44
162
  PathGenerationScheme getGenerationScheme() const override { return Minimal; }
45
0
  bool supportsLogicalOpControlFlow() const override { return true; }
46
6
  bool supportsCrossFileDiagnostics() const override { return true; }
47
};
48
} // end anonymous namespace
49
50
void ento::createSarifDiagnosticConsumer(
51
    AnalyzerOptions &AnalyzerOpts, PathDiagnosticConsumers &C,
52
    const std::string &Output, const Preprocessor &PP,
53
2
    const cross_tu::CrossTranslationUnitContext &CTU) {
54
2
55
  // TODO: Emit an error here.
56
2
  if (Output.empty())
57
0
    return;
58
2
59
2
  C.push_back(new SarifDiagnostics(AnalyzerOpts, Output, PP.getLangOpts()));
60
2
  createTextMinimalPathDiagnosticConsumer(AnalyzerOpts, C, Output, PP, CTU);
61
2
}
62
63
24
static StringRef getFileName(const FileEntry &FE) {
64
24
  StringRef Filename = FE.tryGetRealPathName();
65
24
  if (Filename.empty())
66
0
    Filename = FE.getName();
67
24
  return Filename;
68
24
}
69
70
2.59k
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
2.59k
  if (llvm::isAlnum(C) ||
77
120
      StringRef::npos != StringRef("-._~:@!$&'()*+,;=").find(C))
78
2.59k
    return std::string(&C, 1);
79
0
  return "%" + llvm::toHex(StringRef(&C, 1));
80
0
}
81
82
24
static std::string fileNameToURI(StringRef Filename) {
83
24
  llvm::SmallString<32> Ret = StringRef("file://");
84
24
85
  // Get the root name to see if it has a URI authority.
86
24
  StringRef Root = sys::path::root_name(Filename);
87
24
  if (Root.startswith("//")) {
88
    // There is an authority, so add it to the URI.
89
0
    Ret += Root.drop_front(2).str();
90
24
  } else if (!Root.empty()) {
91
    // There is no authority, so end the component and add the root to the URI.
92
0
    Ret += Twine("/" + Root).str();
93
0
  }
94
24
95
24
  auto Iter = sys::path::begin(Filename), End = sys::path::end(Filename);
96
24
  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
264
  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
264
    if (Component == "\\")
104
0
      return;
105
264
106
    // Add the separator between the previous path part and the one being
107
    // currently processed.
108
264
    Ret += "/";
109
264
110
    // URI encode the part.
111
2.59k
    for (char C : Component) {
112
2.59k
      Ret += percentEncodeURICharacter(C);
113
2.59k
    }
114
264
  });
115
24
116
24
  return std::string(Ret);
117
24
}
118
119
2
static json::Object createArtifactLocation(const FileEntry &FE) {
120
2
  return json::Object{{"uri", fileNameToURI(getFileName(FE))}};
121
2
}
122
123
2
static json::Object createArtifact(const FileEntry &FE) {
124
2
  return json::Object{{"location", createArtifactLocation(FE)},
125
2
                      {"roles", json::Array{"resultFile"}},
126
2
                      {"length", FE.getSize()},
127
2
                      {"mimeType", "text/plain"}};
128
2
}
129
130
static json::Object createArtifactLocation(const FileEntry &FE,
131
22
                                           json::Array &Artifacts) {
132
22
  std::string FileURI = fileNameToURI(getFileName(FE));
133
22
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
20
  auto I = llvm::find_if(Artifacts, [&](const json::Value &File) {
137
20
    if (const json::Object *Obj = File.getAsObject()) {
138
20
      if (const json::Object *FileLoc = Obj->getObject("location")) {
139
20
        Optional<StringRef> URI = FileLoc->getString("uri");
140
20
        return URI && URI->equals(FileURI);
141
20
      }
142
0
    }
143
0
    return false;
144
0
  });
145
22
146
  // Calculate the index within the artifact array so it can be stored in
147
  // the JSON object.
148
22
  auto Index = static_cast<unsigned>(std::distance(Artifacts.begin(), I));
149
22
  if (I == Artifacts.end())
150
2
    Artifacts.push_back(createArtifact(FE));
151
22
152
22
  return json::Object{{"uri", FileURI}, {"index", Index}};
153
22
}
154
155
static unsigned int adjustColumnPos(const SourceManager &SM, SourceLocation Loc,
156
44
                                    unsigned int TokenLen = 0) {
157
44
  assert(!Loc.isInvalid() && "invalid Loc when adjusting column position");
158
44
159
44
  std::pair<FileID, unsigned> LocInfo = SM.getDecomposedExpansionLoc(Loc);
160
44
  assert(LocInfo.second > SM.getExpansionColumnNumber(Loc) &&
161
44
         "position in file is before column number?");
162
44
163
44
  bool InvalidBuffer = false;
164
44
  const MemoryBuffer *Buf = SM.getBuffer(LocInfo.first, &InvalidBuffer);
165
44
  assert(!InvalidBuffer && "got an invalid buffer for the location's file");
166
44
  assert(Buf->getBufferSize() >= (LocInfo.second + TokenLen) &&
167
44
         "token extends past end of buffer?");
168
44
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
44
  unsigned int Off = LocInfo.second - (SM.getExpansionColumnNumber(Loc) - 1);
172
44
  unsigned int Ret = 1;
173
471
  while (Off < (LocInfo.second + TokenLen)) {
174
427
    Off += getNumBytesForUTF8(Buf->getBuffer()[Off]);
175
427
    Ret++;
176
427
  }
177
44
178
44
  return Ret;
179
44
}
180
181
static json::Object createTextRegion(const LangOptions &LO, SourceRange R,
182
22
                                     const SourceManager &SM) {
183
22
  json::Object Region{
184
22
      {"startLine", SM.getExpansionLineNumber(R.getBegin())},
185
22
      {"startColumn", adjustColumnPos(SM, R.getBegin())},
186
22
  };
187
22
  if (R.getBegin() == R.getEnd()) {
188
8
    Region["endColumn"] = adjustColumnPos(SM, R.getBegin());
189
14
  } else {
190
14
    Region["endLine"] = SM.getExpansionLineNumber(R.getEnd());
191
14
    Region["endColumn"] = adjustColumnPos(
192
14
        SM, R.getEnd(),
193
14
        Lexer::MeasureTokenLength(R.getEnd(), SM, LO));
194
14
  }
195
22
  return Region;
196
22
}
197
198
static json::Object createPhysicalLocation(const LangOptions &LO,
199
                                           SourceRange R, const FileEntry &FE,
200
                                           const SourceManager &SMgr,
201
22
                                           json::Array &Artifacts) {
202
22
  return json::Object{
203
22
      {{"artifactLocation", createArtifactLocation(FE, Artifacts)},
204
22
       {"region", createTextRegion(LO, R, SMgr)}}};
205
22
}
206
207
enum class Importance { Important, Essential, Unimportant };
208
209
16
static StringRef importanceToStr(Importance I) {
210
16
  switch (I) {
211
2
  case Importance::Important:
212
2
    return "important";
213
12
  case Importance::Essential:
214
12
    return "essential";
215
2
  case Importance::Unimportant:
216
2
    return "unimportant";
217
0
  }
218
0
  llvm_unreachable("Fully covered switch is not so fully covered");
219
0
}
220
221
static json::Object createThreadFlowLocation(json::Object &&Location,
222
16
                                             Importance I) {
223
16
  return json::Object{{"location", std::move(Location)},
224
16
                      {"importance", importanceToStr(I)}};
225
16
}
226
227
27
static json::Object createMessage(StringRef Text) {
228
27
  return json::Object{{"text", Text.str()}};
229
27
}
230
231
static json::Object createLocation(json::Object &&PhysicalLocation,
232
22
                                   StringRef Message = "") {
233
22
  json::Object Ret{{"physicalLocation", std::move(PhysicalLocation)}};
234
22
  if (!Message.empty())
235
16
    Ret.insert({"message", createMessage(Message)});
236
22
  return Ret;
237
22
}
238
239
16
static Importance calculateImportance(const PathDiagnosticPiece &Piece) {
240
16
  switch (Piece.getKind()) {
241
0
  case PathDiagnosticPiece::Call:
242
0
  case PathDiagnosticPiece::Macro:
243
0
  case PathDiagnosticPiece::Note:
244
0
  case PathDiagnosticPiece::PopUp:
245
    // FIXME: What should be reported here?
246
0
    break;
247
14
  case PathDiagnosticPiece::Event:
248
2
    return Piece.getTagStr() == "ConditionBRVisitor" ? Importance::Important
249
12
                                                     : Importance::Essential;
250
2
  case PathDiagnosticPiece::ControlFlow:
251
2
    return Importance::Unimportant;
252
0
  }
253
0
  return Importance::Unimportant;
254
0
}
255
256
static json::Object createThreadFlow(const LangOptions &LO,
257
                                     const PathPieces &Pieces,
258
6
                                     json::Array &Artifacts) {
259
6
  const SourceManager &SMgr = Pieces.front()->getLocation().getManager();
260
6
  json::Array Locations;
261
16
  for (const auto &Piece : Pieces) {
262
16
    const PathDiagnosticLocation &P = Piece->getLocation();
263
16
    Locations.push_back(createThreadFlowLocation(
264
16
        createLocation(createPhysicalLocation(
265
16
                           LO, P.asRange(),
266
16
                           *P.asLocation().getExpansionLoc().getFileEntry(),
267
16
                           SMgr, Artifacts),
268
16
                       Piece->getString()),
269
16
        calculateImportance(*Piece)));
270
16
  }
271
6
  return json::Object{{"locations", std::move(Locations)}};
272
6
}
273
274
static json::Object createCodeFlow(const LangOptions &LO,
275
                                   const PathPieces &Pieces,
276
6
                                   json::Array &Artifacts) {
277
6
  return json::Object{
278
6
      {"threadFlows", json::Array{createThreadFlow(LO, Pieces, Artifacts)}}};
279
6
}
280
281
static json::Object createResult(const LangOptions &LO,
282
                                 const PathDiagnostic &Diag,
283
                                 json::Array &Artifacts,
284
6
                                 const StringMap<unsigned> &RuleMapping) {
285
6
  const PathPieces &Path = Diag.path.flatten(false);
286
6
  const SourceManager &SMgr = Path.front()->getLocation().getManager();
287
6
288
6
  auto Iter = RuleMapping.find(Diag.getCheckerName());
289
6
  assert(Iter != RuleMapping.end() && "Rule ID is not in the array index map?");
290
6
291
6
  return json::Object{
292
6
      {"message", createMessage(Diag.getVerboseDescription())},
293
6
      {"codeFlows", json::Array{createCodeFlow(LO, Path, Artifacts)}},
294
6
      {"locations",
295
6
       json::Array{createLocation(createPhysicalLocation(
296
6
           LO, Diag.getLocation().asRange(),
297
6
           *Diag.getLocation().asLocation().getExpansionLoc().getFileEntry(),
298
6
           SMgr, Artifacts))}},
299
6
      {"ruleIndex", Iter->getValue()},
300
6
      {"ruleId", Diag.getCheckerName()}};
301
6
}
302
303
5
static StringRef getRuleDescription(StringRef CheckName) {
304
5
  return llvm::StringSwitch<StringRef>(CheckName)
305
5
#define GET_CHECKERS
306
5
#define CHECKER(FULLNAME, CLASS, HELPTEXT, DOC_URI, IS_HIDDEN)                 \
307
920
  .Case(FULLNAME, HELPTEXT)
308
5
#include "clang/StaticAnalyzer/Checkers/Checkers.inc"
309
5
#undef CHECKER
310
5
#undef GET_CHECKERS
311
5
      ;
312
5
}
313
314
5
static StringRef getRuleHelpURIStr(StringRef CheckName) {
315
5
  return llvm::StringSwitch<StringRef>(CheckName)
316
5
#define GET_CHECKERS
317
5
#define CHECKER(FULLNAME, CLASS, HELPTEXT, DOC_URI, IS_HIDDEN)                 \
318
920
  .Case(FULLNAME, DOC_URI)
319
5
#include "clang/StaticAnalyzer/Checkers/Checkers.inc"
320
5
#undef CHECKER
321
5
#undef GET_CHECKERS
322
5
      ;
323
5
}
324
325
5
static json::Object createRule(const PathDiagnostic &Diag) {
326
5
  StringRef CheckName = Diag.getCheckerName();
327
5
  json::Object Ret{
328
5
      {"fullDescription", createMessage(getRuleDescription(CheckName))},
329
5
      {"name", CheckName},
330
5
      {"id", CheckName}};
331
5
332
5
  std::string RuleURI = std::string(getRuleHelpURIStr(CheckName));
333
5
  if (!RuleURI.empty())
334
3
    Ret["helpUri"] = RuleURI;
335
5
336
5
  return Ret;
337
5
}
338
339
static json::Array createRules(std::vector<const PathDiagnostic *> &Diags,
340
2
                               StringMap<unsigned> &RuleMapping) {
341
2
  json::Array Rules;
342
2
  llvm::StringSet<> Seen;
343
2
344
6
  llvm::for_each(Diags, [&](const PathDiagnostic *D) {
345
6
    StringRef RuleID = D->getCheckerName();
346
6
    std::pair<llvm::StringSet<>::iterator, bool> P = Seen.insert(RuleID);
347
6
    if (P.second) {
348
5
      RuleMapping[RuleID] = Rules.size(); // Maps RuleID to an Array Index.
349
5
      Rules.push_back(createRule(*D));
350
5
    }
351
6
  });
352
2
353
2
  return Rules;
354
2
}
355
356
static json::Object createTool(std::vector<const PathDiagnostic *> &Diags,
357
2
                               StringMap<unsigned> &RuleMapping) {
358
2
  return json::Object{
359
2
      {"driver", json::Object{{"name", "clang"},
360
2
                              {"fullName", "clang static analyzer"},
361
2
                              {"language", "en-US"},
362
2
                              {"version", getClangFullVersion()},
363
2
                              {"rules", createRules(Diags, RuleMapping)}}}};
364
2
}
365
366
static json::Object createRun(const LangOptions &LO,
367
2
                              std::vector<const PathDiagnostic *> &Diags) {
368
2
  json::Array Results, Artifacts;
369
2
  StringMap<unsigned> RuleMapping;
370
2
  json::Object Tool = createTool(Diags, RuleMapping);
371
2
  
372
6
  llvm::for_each(Diags, [&](const PathDiagnostic *D) {
373
6
    Results.push_back(createResult(LO, *D, Artifacts, RuleMapping));
374
6
  });
375
2
376
2
  return json::Object{{"tool", std::move(Tool)},
377
2
                      {"results", std::move(Results)},
378
2
                      {"artifacts", std::move(Artifacts)},
379
2
                      {"columnKind", "unicodeCodePoints"}};
380
2
}
381
382
void SarifDiagnostics::FlushDiagnosticsImpl(
383
2
    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
2
  std::error_code EC;
390
2
  llvm::raw_fd_ostream OS(OutputFile, EC, llvm::sys::fs::OF_Text);
391
2
  if (EC) {
392
0
    llvm::errs() << "warning: could not create file: " << EC.message() << '\n';
393
0
    return;
394
0
  }
395
2
  json::Object Sarif{
396
2
      {"$schema",
397
2
       "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json"},
398
2
      {"version", "2.1.0"},
399
2
      {"runs", json::Array{createRun(LO, Diags)}}};
400
2
  OS << llvm::formatv("{0:2}\n", json::Value(std::move(Sarif)));
401
2
}