Coverage Report

Created: 2019-07-24 05:18

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