Coverage Report

Created: 2021-09-21 08:58

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/tools/libclang/CXLoadedDiagnostic.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- CXLoadedDiagnostic.cpp - Handling of persisent diags ----*- 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
// Implements handling of persisent diagnostics.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "CXLoadedDiagnostic.h"
14
#include "CXString.h"
15
#include "clang/Basic/Diagnostic.h"
16
#include "clang/Basic/FileManager.h"
17
#include "clang/Basic/LLVM.h"
18
#include "clang/Frontend/SerializedDiagnosticReader.h"
19
#include "clang/Frontend/SerializedDiagnostics.h"
20
#include "llvm/ADT/STLExtras.h"
21
#include "llvm/ADT/StringRef.h"
22
#include "llvm/ADT/Twine.h"
23
#include "llvm/Bitstream/BitstreamReader.h"
24
#include "llvm/Support/ErrorHandling.h"
25
26
using namespace clang;
27
28
//===----------------------------------------------------------------------===//
29
// Extend CXDiagnosticSetImpl which contains strings for diagnostics.
30
//===----------------------------------------------------------------------===//
31
32
typedef llvm::DenseMap<unsigned, const char *> Strings;
33
34
namespace {
35
class CXLoadedDiagnosticSetImpl : public CXDiagnosticSetImpl {
36
public:
37
17
  CXLoadedDiagnosticSetImpl() : CXDiagnosticSetImpl(true), FakeFiles(FO) {}
38
17
  ~CXLoadedDiagnosticSetImpl() override {}
39
40
  llvm::BumpPtrAllocator Alloc;
41
  Strings Categories;
42
  Strings WarningFlags;
43
  Strings FileNames;
44
  
45
  FileSystemOptions FO;
46
  FileManager FakeFiles;
47
  llvm::DenseMap<unsigned, const FileEntry *> Files;
48
49
  /// Copy the string into our own allocator.
50
164
  const char *copyString(StringRef Blob) {
51
164
    char *mem = Alloc.Allocate<char>(Blob.size() + 1);
52
164
    memcpy(mem, Blob.data(), Blob.size());
53
164
    mem[Blob.size()] = '\0';
54
164
    return mem;
55
164
  }
56
};
57
} // end anonymous namespace
58
59
//===----------------------------------------------------------------------===//
60
// Cleanup.
61
//===----------------------------------------------------------------------===//
62
63
78
CXLoadedDiagnostic::~CXLoadedDiagnostic() {}
64
65
//===----------------------------------------------------------------------===//
66
// Public CXLoadedDiagnostic methods.
67
//===----------------------------------------------------------------------===//
68
69
78
CXDiagnosticSeverity CXLoadedDiagnostic::getSeverity() const {
70
  // FIXME: Fail more softly if the diagnostic level is unknown?
71
78
  auto severityAsLevel = static_cast<serialized_diags::Level>(severity);
72
78
  assert(severity == static_cast<unsigned>(severityAsLevel) &&
73
78
         "unknown serialized diagnostic level");
74
75
0
  switch (severityAsLevel) {
76
78
#define CASE(X) case serialized_diags::X: return CXDiagnostic_##X;
77
0
  CASE(Ignored)
78
38
  CASE
(0
Note)
79
19
  CASE
(0
Warning)
80
17
  CASE
(0
Error)
81
4
  CASE
(0
Fatal)
82
0
#undef CASE
83
  // The 'Remark' level isn't represented in the stable API.
84
0
  case serialized_diags::Remark: return CXDiagnostic_Warning;
85
78
  }
86
  
87
0
  llvm_unreachable("Invalid diagnostic level");
88
0
}
89
90
174
static CXSourceLocation makeLocation(const CXLoadedDiagnostic::Location *DLoc) {
91
  // The lowest bit of ptr_data[0] is always set to 1 to indicate this
92
  // is a persistent diagnostic.
93
174
  uintptr_t V = (uintptr_t) DLoc;
94
174
  V |= 0x1;
95
174
  CXSourceLocation Loc = { {  (void*) V, nullptr }, 0 };
96
174
  return Loc;
97
174
}  
98
99
78
CXSourceLocation CXLoadedDiagnostic::getLocation() const {
100
  // The lowest bit of ptr_data[0] is always set to 1 to indicate this
101
  // is a persistent diagnostic.
102
78
  return makeLocation(&DiagLoc);
103
78
}
104
105
78
CXString CXLoadedDiagnostic::getSpelling() const {
106
78
  return cxstring::createRef(Spelling);
107
78
}
108
109
78
CXString CXLoadedDiagnostic::getDiagnosticOption(CXString *Disable) const {
110
78
  if (DiagOption.empty())
111
59
    return cxstring::createEmpty();
112
113
  // FIXME: possibly refactor with logic in CXStoredDiagnostic.
114
19
  if (Disable)
115
0
    *Disable = cxstring::createDup((Twine("-Wno-") + DiagOption).str());
116
19
  return cxstring::createDup((Twine("-W") + DiagOption).str());
117
78
}
118
119
0
unsigned CXLoadedDiagnostic::getCategory() const {
120
0
  return category;
121
0
}
122
123
78
CXString CXLoadedDiagnostic::getCategoryText() const {
124
78
  return cxstring::createDup(CategoryText);
125
78
}
126
127
114
unsigned CXLoadedDiagnostic::getNumRanges() const {
128
114
  return Ranges.size();
129
114
}
130
131
36
CXSourceRange CXLoadedDiagnostic::getRange(unsigned Range) const {
132
36
  assert(Range < Ranges.size());
133
0
  return Ranges[Range];
134
36
}
135
136
90
unsigned CXLoadedDiagnostic::getNumFixIts() const {
137
90
  return FixIts.size();
138
90
}
139
140
CXString CXLoadedDiagnostic::getFixIt(unsigned FixIt,
141
12
                                      CXSourceRange *ReplacementRange) const {
142
12
  assert(FixIt < FixIts.size());
143
12
  if (ReplacementRange)
144
12
    *ReplacementRange = FixIts[FixIt].first;
145
12
  return cxstring::createRef(FixIts[FixIt].second);
146
12
}
147
148
void CXLoadedDiagnostic::decodeLocation(CXSourceLocation location,
149
                                        CXFile *file,
150
                                        unsigned int *line,
151
                                        unsigned int *column,
152
174
                                        unsigned int *offset) {
153
  
154
  
155
  // CXSourceLocation consists of the following fields:
156
  //
157
  //   void *ptr_data[2];
158
  //   unsigned int_data;
159
  //
160
  // The lowest bit of ptr_data[0] is always set to 1 to indicate this
161
  // is a persistent diagnostic.
162
  //
163
  // For now, do the unoptimized approach and store the data in a side
164
  // data structure.  We can optimize this case later.
165
  
166
174
  uintptr_t V = (uintptr_t) location.ptr_data[0];
167
174
  assert((V & 0x1) == 1);
168
0
  V &= ~(uintptr_t)1;
169
  
170
174
  const Location &Loc = *((Location*)V);
171
  
172
174
  if (file)
173
174
    *file = Loc.file;  
174
174
  if (line)
175
174
    *line = Loc.line;
176
174
  if (column)
177
174
    *column = Loc.column;
178
174
  if (offset)
179
174
    *offset = Loc.offset;
180
174
}
181
182
//===----------------------------------------------------------------------===//
183
// Deserialize diagnostics.
184
//===----------------------------------------------------------------------===//
185
186
namespace {
187
class DiagLoader : serialized_diags::SerializedDiagnosticReader {
188
  enum CXLoadDiag_Error *error;
189
  CXString *errorString;
190
  std::unique_ptr<CXLoadedDiagnosticSetImpl> TopDiags;
191
  SmallVector<std::unique_ptr<CXLoadedDiagnostic>, 8> CurrentDiags;
192
193
1
  std::error_code reportBad(enum CXLoadDiag_Error code, llvm::StringRef err) {
194
1
    if (error)
195
1
      *error = code;
196
1
    if (errorString)
197
1
      *errorString = cxstring::createDup(err);
198
1
    return serialized_diags::SDError::HandlerFailed;
199
1
  }
200
  
201
1
  std::error_code reportInvalidFile(llvm::StringRef err) {
202
1
    return reportBad(CXLoadDiag_InvalidFile, err);
203
1
  }
204
205
  std::error_code readRange(const serialized_diags::Location &SDStart,
206
                            const serialized_diags::Location &SDEnd,
207
                            CXSourceRange &SR);
208
209
  std::error_code readLocation(const serialized_diags::Location &SDLoc,
210
                               CXLoadedDiagnostic::Location &LoadedLoc);
211
212
protected:
213
  std::error_code visitStartOfDiagnostic() override;
214
  std::error_code visitEndOfDiagnostic() override;
215
216
  std::error_code visitCategoryRecord(unsigned ID, StringRef Name) override;
217
218
  std::error_code visitDiagFlagRecord(unsigned ID, StringRef Name) override;
219
220
  std::error_code visitDiagnosticRecord(
221
      unsigned Severity, const serialized_diags::Location &Location,
222
      unsigned Category, unsigned Flag, StringRef Message) override;
223
224
  std::error_code visitFilenameRecord(unsigned ID, unsigned Size,
225
                                      unsigned Timestamp,
226
                                      StringRef Name) override;
227
228
  std::error_code visitFixitRecord(const serialized_diags::Location &Start,
229
                                   const serialized_diags::Location &End,
230
                                   StringRef CodeToInsert) override;
231
232
  std::error_code
233
  visitSourceRangeRecord(const serialized_diags::Location &Start,
234
                         const serialized_diags::Location &End) override;
235
236
public:
237
  DiagLoader(enum CXLoadDiag_Error *e, CXString *es)
238
17
      : SerializedDiagnosticReader(), error(e), errorString(es) {
239
17
    if (error)
240
17
      *error = CXLoadDiag_None;
241
17
    if (errorString)
242
17
      *errorString = cxstring::createEmpty();
243
17
  }
244
245
  CXDiagnosticSet load(const char *file);
246
};
247
} // end anonymous namespace
248
249
17
CXDiagnosticSet DiagLoader::load(const char *file) {
250
17
  TopDiags = std::make_unique<CXLoadedDiagnosticSetImpl>();
251
252
17
  std::error_code EC = readDiagnostics(file);
253
17
  if (EC) {
254
1
    switch (EC.value()) {
255
0
    case static_cast<int>(serialized_diags::SDError::HandlerFailed):
256
      // We've already reported the problem.
257
0
      break;
258
0
    case static_cast<int>(serialized_diags::SDError::CouldNotLoad):
259
0
      reportBad(CXLoadDiag_CannotLoad, EC.message());
260
0
      break;
261
1
    default:
262
1
      reportInvalidFile(EC.message());
263
1
      break;
264
1
    }
265
1
    return nullptr;
266
1
  }
267
268
16
  return (CXDiagnosticSet)TopDiags.release();
269
17
}
270
271
std::error_code
272
DiagLoader::readLocation(const serialized_diags::Location &SDLoc,
273
174
                         CXLoadedDiagnostic::Location &LoadedLoc) {
274
174
  unsigned FileID = SDLoc.FileID;
275
174
  if (FileID == 0)
276
4
    LoadedLoc.file = nullptr;
277
170
  else {
278
170
    LoadedLoc.file = const_cast<FileEntry *>(TopDiags->Files[FileID]);
279
170
    if (!LoadedLoc.file)
280
0
      return reportInvalidFile("Corrupted file entry in source location");
281
170
  }
282
174
  LoadedLoc.line = SDLoc.Line;
283
174
  LoadedLoc.column = SDLoc.Col;
284
174
  LoadedLoc.offset = SDLoc.Offset;
285
174
  return std::error_code();
286
174
}
287
288
std::error_code
289
DiagLoader::readRange(const serialized_diags::Location &SDStart,
290
                      const serialized_diags::Location &SDEnd,
291
48
                      CXSourceRange &SR) {
292
48
  CXLoadedDiagnostic::Location *Start, *End;
293
48
  Start = TopDiags->Alloc.Allocate<CXLoadedDiagnostic::Location>();
294
48
  End = TopDiags->Alloc.Allocate<CXLoadedDiagnostic::Location>();
295
296
48
  std::error_code EC;
297
48
  if ((EC = readLocation(SDStart, *Start)))
298
0
    return EC;
299
48
  if ((EC = readLocation(SDEnd, *End)))
300
0
    return EC;
301
  
302
48
  CXSourceLocation startLoc = makeLocation(Start);
303
48
  CXSourceLocation endLoc = makeLocation(End);
304
48
  SR = clang_getRange(startLoc, endLoc);
305
48
  return std::error_code();
306
48
}
307
308
78
std::error_code DiagLoader::visitStartOfDiagnostic() {
309
78
  CurrentDiags.push_back(std::make_unique<CXLoadedDiagnostic>());
310
78
  return std::error_code();
311
78
}
312
313
78
std::error_code DiagLoader::visitEndOfDiagnostic() {
314
78
  auto D = CurrentDiags.pop_back_val();
315
78
  if (CurrentDiags.empty())
316
40
    TopDiags->appendDiagnostic(std::move(D));
317
38
  else
318
38
    CurrentDiags.back()->getChildDiagnostics().appendDiagnostic(std::move(D));
319
78
  return std::error_code();
320
78
}
321
322
30
std::error_code DiagLoader::visitCategoryRecord(unsigned ID, StringRef Name) {
323
  // FIXME: Why do we care about long strings?
324
30
  if (Name.size() > 65536)
325
0
    return reportInvalidFile("Out-of-bounds string in category");
326
30
  TopDiags->Categories[ID] = TopDiags->copyString(Name);
327
30
  return std::error_code();
328
30
}
329
330
16
std::error_code DiagLoader::visitDiagFlagRecord(unsigned ID, StringRef Name) {
331
  // FIXME: Why do we care about long strings?
332
16
  if (Name.size() > 65536)
333
0
    return reportInvalidFile("Out-of-bounds string in warning flag");
334
16
  TopDiags->WarningFlags[ID] = TopDiags->copyString(Name);
335
16
  return std::error_code();
336
16
}
337
338
std::error_code DiagLoader::visitFilenameRecord(unsigned ID, unsigned Size,
339
                                                unsigned Timestamp,
340
28
                                                StringRef Name) {
341
  // FIXME: Why do we care about long strings?
342
28
  if (Name.size() > 65536)
343
0
    return reportInvalidFile("Out-of-bounds string in filename");
344
28
  TopDiags->FileNames[ID] = TopDiags->copyString(Name);
345
28
  TopDiags->Files[ID] =
346
28
      TopDiags->FakeFiles.getVirtualFile(Name, Size, Timestamp);
347
28
  return std::error_code();
348
28
}
349
350
std::error_code
351
DiagLoader::visitSourceRangeRecord(const serialized_diags::Location &Start,
352
36
                                   const serialized_diags::Location &End) {
353
36
  CXSourceRange SR;
354
36
  if (std::error_code EC = readRange(Start, End, SR))
355
0
    return EC;
356
36
  CurrentDiags.back()->Ranges.push_back(SR);
357
36
  return std::error_code();
358
36
}
359
360
std::error_code
361
DiagLoader::visitFixitRecord(const serialized_diags::Location &Start,
362
                             const serialized_diags::Location &End,
363
12
                             StringRef CodeToInsert) {
364
12
  CXSourceRange SR;
365
12
  if (std::error_code EC = readRange(Start, End, SR))
366
0
    return EC;
367
  // FIXME: Why do we care about long strings?
368
12
  if (CodeToInsert.size() > 65536)
369
0
    return reportInvalidFile("Out-of-bounds string in FIXIT");
370
12
  CurrentDiags.back()->FixIts.push_back(
371
12
      std::make_pair(SR, TopDiags->copyString(CodeToInsert)));
372
12
  return std::error_code();
373
12
}
374
375
std::error_code DiagLoader::visitDiagnosticRecord(
376
    unsigned Severity, const serialized_diags::Location &Location,
377
78
    unsigned Category, unsigned Flag, StringRef Message) {
378
78
  CXLoadedDiagnostic &D = *CurrentDiags.back();
379
78
  D.severity = Severity;
380
78
  if (std::error_code EC = readLocation(Location, D.DiagLoc))
381
0
    return EC;
382
78
  D.category = Category;
383
78
  D.DiagOption = Flag ? 
TopDiags->WarningFlags[Flag]19
:
""59
;
384
78
  D.CategoryText = Category ? 
TopDiags->Categories[Category]52
:
""26
;
385
78
  D.Spelling = TopDiags->copyString(Message);
386
78
  return std::error_code();
387
78
}
388
389
CXDiagnosticSet clang_loadDiagnostics(const char *file,
390
                                      enum CXLoadDiag_Error *error,
391
17
                                      CXString *errorString) {
392
17
  DiagLoader L(error, errorString);
393
17
  return L.load(file);
394
17
}