blob: 754ad55a66040838023d0bb58286fd440de0bcb3 [file] [log] [blame]
Chris Lattner2815bc92013-01-19 18:47:35 +00001//===-- CXLoadedDiagnostic.cpp - Handling of persisent diags ----*- 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// Implements handling of persisent diagnostics.
11//
12//===----------------------------------------------------------------------===//
Ted Kremenek15322172011-11-10 08:43:12 +000013
14#include "CXLoadedDiagnostic.h"
15#include "CXString.h"
16#include "clang/Basic/Diagnostic.h"
17#include "clang/Basic/FileManager.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070018#include "clang/Basic/LLVM.h"
Stephen Hines176edba2014-12-01 14:53:08 -080019#include "clang/Frontend/SerializedDiagnosticReader.h"
Stephen Hines0e2c34f2015-03-23 12:09:02 -070020#include "clang/Frontend/SerializedDiagnostics.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070021#include "llvm/ADT/Optional.h"
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070022#include "llvm/ADT/STLExtras.h"
Ted Kremenek15322172011-11-10 08:43:12 +000023#include "llvm/ADT/StringRef.h"
24#include "llvm/ADT/Twine.h"
Ted Kremenek15322172011-11-10 08:43:12 +000025#include "llvm/Bitcode/BitstreamReader.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070026#include "llvm/Support/ErrorHandling.h"
Ted Kremenek15322172011-11-10 08:43:12 +000027#include "llvm/Support/MemoryBuffer.h"
Ted Kremenek15322172011-11-10 08:43:12 +000028using namespace clang;
Ted Kremenek15322172011-11-10 08:43:12 +000029
30//===----------------------------------------------------------------------===//
31// Extend CXDiagnosticSetImpl which contains strings for diagnostics.
32//===----------------------------------------------------------------------===//
33
Dmitri Gribenko9ba76272013-02-18 19:50:38 +000034typedef llvm::DenseMap<unsigned, const char *> Strings;
Ted Kremenek15322172011-11-10 08:43:12 +000035
36namespace {
37class CXLoadedDiagnosticSetImpl : public CXDiagnosticSetImpl {
38public:
39 CXLoadedDiagnosticSetImpl() : CXDiagnosticSetImpl(true), FakeFiles(FO) {}
Pirama Arumuga Nainar33337ca2015-05-06 11:48:57 -070040 ~CXLoadedDiagnosticSetImpl() override {}
Ted Kremenek15322172011-11-10 08:43:12 +000041
Ted Kremenek15322172011-11-10 08:43:12 +000042 llvm::BumpPtrAllocator Alloc;
43 Strings Categories;
44 Strings WarningFlags;
45 Strings FileNames;
46
47 FileSystemOptions FO;
48 FileManager FakeFiles;
49 llvm::DenseMap<unsigned, const FileEntry *> Files;
Dmitri Gribenko9ba76272013-02-18 19:50:38 +000050
51 /// \brief Copy the string into our own allocator.
52 const char *copyString(StringRef Blob) {
Chris Lattnerb3ce3572013-01-20 02:38:54 +000053 char *mem = Alloc.Allocate<char>(Blob.size() + 1);
54 memcpy(mem, Blob.data(), Blob.size());
Chris Lattnerb3ce3572013-01-20 02:38:54 +000055 mem[Blob.size()] = '\0';
Dmitri Gribenko9ba76272013-02-18 19:50:38 +000056 return mem;
Chris Lattnerb3ce3572013-01-20 02:38:54 +000057 }
Ted Kremenek15322172011-11-10 08:43:12 +000058};
59}
60
Ted Kremenek15322172011-11-10 08:43:12 +000061//===----------------------------------------------------------------------===//
62// Cleanup.
63//===----------------------------------------------------------------------===//
64
65CXLoadedDiagnostic::~CXLoadedDiagnostic() {}
66
67//===----------------------------------------------------------------------===//
68// Public CXLoadedDiagnostic methods.
69//===----------------------------------------------------------------------===//
70
71CXDiagnosticSeverity CXLoadedDiagnostic::getSeverity() const {
Stephen Hines651f13c2014-04-23 16:59:28 -070072 // FIXME: Fail more softly if the diagnostic level is unknown?
73 auto severityAsLevel = static_cast<serialized_diags::Level>(severity);
74 assert(severity == static_cast<unsigned>(severityAsLevel) &&
75 "unknown serialized diagnostic level");
76
77 switch (severityAsLevel) {
78#define CASE(X) case serialized_diags::X: return CXDiagnostic_##X;
79 CASE(Ignored)
80 CASE(Note)
81 CASE(Warning)
82 CASE(Error)
83 CASE(Fatal)
Stephen Hines651f13c2014-04-23 16:59:28 -070084#undef CASE
Stephen Hines6bcf27b2014-05-29 04:14:42 -070085 // The 'Remark' level isn't represented in the stable API.
86 case serialized_diags::Remark: return CXDiagnostic_Warning;
Ted Kremenek15322172011-11-10 08:43:12 +000087 }
88
89 llvm_unreachable("Invalid diagnostic level");
Ted Kremenek15322172011-11-10 08:43:12 +000090}
91
92static CXSourceLocation makeLocation(const CXLoadedDiagnostic::Location *DLoc) {
93 // The lowest bit of ptr_data[0] is always set to 1 to indicate this
94 // is a persistent diagnostic.
95 uintptr_t V = (uintptr_t) DLoc;
96 V |= 0x1;
Stephen Hinesc568f1e2014-07-21 00:47:37 -070097 CXSourceLocation Loc = { { (void*) V, nullptr }, 0 };
Ted Kremenek15322172011-11-10 08:43:12 +000098 return Loc;
99}
100
101CXSourceLocation CXLoadedDiagnostic::getLocation() const {
102 // The lowest bit of ptr_data[0] is always set to 1 to indicate this
103 // is a persistent diagnostic.
104 return makeLocation(&DiagLoc);
105}
106
107CXString CXLoadedDiagnostic::getSpelling() const {
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000108 return cxstring::createRef(Spelling);
Ted Kremenek15322172011-11-10 08:43:12 +0000109}
110
111CXString CXLoadedDiagnostic::getDiagnosticOption(CXString *Disable) const {
112 if (DiagOption.empty())
Dmitri Gribenkodc66adb2013-02-01 14:21:22 +0000113 return cxstring::createEmpty();
Ted Kremenek15322172011-11-10 08:43:12 +0000114
115 // FIXME: possibly refactor with logic in CXStoredDiagnostic.
116 if (Disable)
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000117 *Disable = cxstring::createDup((Twine("-Wno-") + DiagOption).str());
118 return cxstring::createDup((Twine("-W") + DiagOption).str());
Ted Kremenek15322172011-11-10 08:43:12 +0000119}
120
121unsigned CXLoadedDiagnostic::getCategory() const {
122 return category;
123}
124
Ted Kremenek78d5d3b2012-04-12 00:03:31 +0000125CXString CXLoadedDiagnostic::getCategoryText() const {
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000126 return cxstring::createDup(CategoryText);
Ted Kremenek78d5d3b2012-04-12 00:03:31 +0000127}
128
Ted Kremenek15322172011-11-10 08:43:12 +0000129unsigned CXLoadedDiagnostic::getNumRanges() const {
130 return Ranges.size();
131}
132
133CXSourceRange CXLoadedDiagnostic::getRange(unsigned Range) const {
134 assert(Range < Ranges.size());
135 return Ranges[Range];
136}
137
138unsigned CXLoadedDiagnostic::getNumFixIts() const {
139 return FixIts.size();
140}
141
142CXString CXLoadedDiagnostic::getFixIt(unsigned FixIt,
143 CXSourceRange *ReplacementRange) const {
144 assert(FixIt < FixIts.size());
145 if (ReplacementRange)
146 *ReplacementRange = FixIts[FixIt].first;
Dmitri Gribenko9ba76272013-02-18 19:50:38 +0000147 return cxstring::createRef(FixIts[FixIt].second);
Ted Kremenek15322172011-11-10 08:43:12 +0000148}
149
150void CXLoadedDiagnostic::decodeLocation(CXSourceLocation location,
151 CXFile *file,
152 unsigned int *line,
153 unsigned int *column,
154 unsigned int *offset) {
155
156
157 // CXSourceLocation consists of the following fields:
158 //
159 // void *ptr_data[2];
160 // unsigned int_data;
161 //
162 // The lowest bit of ptr_data[0] is always set to 1 to indicate this
163 // is a persistent diagnostic.
164 //
165 // For now, do the unoptimized approach and store the data in a side
166 // data structure. We can optimize this case later.
167
168 uintptr_t V = (uintptr_t) location.ptr_data[0];
169 assert((V & 0x1) == 1);
170 V &= ~(uintptr_t)1;
171
172 const Location &Loc = *((Location*)V);
173
174 if (file)
175 *file = Loc.file;
176 if (line)
177 *line = Loc.line;
178 if (column)
179 *column = Loc.column;
180 if (offset)
181 *offset = Loc.offset;
182}
183
184//===----------------------------------------------------------------------===//
185// Deserialize diagnostics.
186//===----------------------------------------------------------------------===//
187
Ted Kremenek15322172011-11-10 08:43:12 +0000188namespace {
Stephen Hines176edba2014-12-01 14:53:08 -0800189class DiagLoader : serialized_diags::SerializedDiagnosticReader {
Ted Kremenek15322172011-11-10 08:43:12 +0000190 enum CXLoadDiag_Error *error;
191 CXString *errorString;
Stephen Hines176edba2014-12-01 14:53:08 -0800192 std::unique_ptr<CXLoadedDiagnosticSetImpl> TopDiags;
193 SmallVector<std::unique_ptr<CXLoadedDiagnostic>, 8> CurrentDiags;
194
195 std::error_code reportBad(enum CXLoadDiag_Error code, llvm::StringRef err) {
Ted Kremenek15322172011-11-10 08:43:12 +0000196 if (error)
197 *error = code;
198 if (errorString)
Dmitri Gribenko5595ded2013-02-02 02:19:29 +0000199 *errorString = cxstring::createDup(err);
Stephen Hines176edba2014-12-01 14:53:08 -0800200 return serialized_diags::SDError::HandlerFailed;
Ted Kremenek15322172011-11-10 08:43:12 +0000201 }
202
Stephen Hines176edba2014-12-01 14:53:08 -0800203 std::error_code reportInvalidFile(llvm::StringRef err) {
Ted Kremenek15322172011-11-10 08:43:12 +0000204 return reportBad(CXLoadDiag_InvalidFile, err);
205 }
206
Stephen Hines176edba2014-12-01 14:53:08 -0800207 std::error_code readRange(const serialized_diags::Location &SDStart,
208 const serialized_diags::Location &SDEnd,
209 CXSourceRange &SR);
Ted Kremenek15322172011-11-10 08:43:12 +0000210
Stephen Hines176edba2014-12-01 14:53:08 -0800211 std::error_code readLocation(const serialized_diags::Location &SDLoc,
212 CXLoadedDiagnostic::Location &LoadedLoc);
Ted Kremenek15322172011-11-10 08:43:12 +0000213
Stephen Hines176edba2014-12-01 14:53:08 -0800214protected:
215 std::error_code visitStartOfDiagnostic() override;
216 std::error_code visitEndOfDiagnostic() override;
Ted Kremenek15322172011-11-10 08:43:12 +0000217
Stephen Hines176edba2014-12-01 14:53:08 -0800218 std::error_code visitCategoryRecord(unsigned ID, StringRef Name) override;
219
220 std::error_code visitDiagFlagRecord(unsigned ID, StringRef Name) override;
221
222 std::error_code visitDiagnosticRecord(
223 unsigned Severity, const serialized_diags::Location &Location,
224 unsigned Category, unsigned Flag, StringRef Message) override;
225
226 std::error_code visitFilenameRecord(unsigned ID, unsigned Size,
227 unsigned Timestamp,
228 StringRef Name) override;
229
230 std::error_code visitFixitRecord(const serialized_diags::Location &Start,
231 const serialized_diags::Location &End,
232 StringRef CodeToInsert) override;
233
234 std::error_code
235 visitSourceRangeRecord(const serialized_diags::Location &Start,
236 const serialized_diags::Location &End) override;
237
Ted Kremenek15322172011-11-10 08:43:12 +0000238public:
239 DiagLoader(enum CXLoadDiag_Error *e, CXString *es)
Stephen Hines176edba2014-12-01 14:53:08 -0800240 : SerializedDiagnosticReader(), error(e), errorString(es) {
241 if (error)
242 *error = CXLoadDiag_None;
243 if (errorString)
244 *errorString = cxstring::createEmpty();
245 }
Ted Kremeneke97ac9e2011-11-11 15:19:48 +0000246
Ted Kremenek15322172011-11-10 08:43:12 +0000247 CXDiagnosticSet load(const char *file);
248};
249}
250
251CXDiagnosticSet DiagLoader::load(const char *file) {
Stephen Hines176edba2014-12-01 14:53:08 -0800252 TopDiags = llvm::make_unique<CXLoadedDiagnosticSetImpl>();
Ted Kremenek15322172011-11-10 08:43:12 +0000253
Stephen Hines176edba2014-12-01 14:53:08 -0800254 std::error_code EC = readDiagnostics(file);
255 if (EC) {
256 switch (EC.value()) {
257 case static_cast<int>(serialized_diags::SDError::HandlerFailed):
258 // We've already reported the problem.
259 break;
260 case static_cast<int>(serialized_diags::SDError::CouldNotLoad):
261 reportBad(CXLoadDiag_CannotLoad, EC.message());
262 break;
263 default:
264 reportInvalidFile(EC.message());
265 break;
Ted Kremenek15322172011-11-10 08:43:12 +0000266 }
Stephen Hines176edba2014-12-01 14:53:08 -0800267 return 0;
Ted Kremenek15322172011-11-10 08:43:12 +0000268 }
Stephen Hines176edba2014-12-01 14:53:08 -0800269
270 return (CXDiagnosticSet)TopDiags.release();
Ted Kremenek15322172011-11-10 08:43:12 +0000271}
272
Stephen Hines176edba2014-12-01 14:53:08 -0800273std::error_code
274DiagLoader::readLocation(const serialized_diags::Location &SDLoc,
275 CXLoadedDiagnostic::Location &LoadedLoc) {
276 unsigned FileID = SDLoc.FileID;
277 if (FileID == 0)
278 LoadedLoc.file = nullptr;
279 else {
280 LoadedLoc.file = const_cast<FileEntry *>(TopDiags->Files[FileID]);
281 if (!LoadedLoc.file)
282 return reportInvalidFile("Corrupted file entry in source location");
Ted Kremenek15322172011-11-10 08:43:12 +0000283 }
Stephen Hines176edba2014-12-01 14:53:08 -0800284 LoadedLoc.line = SDLoc.Line;
285 LoadedLoc.column = SDLoc.Col;
286 LoadedLoc.offset = SDLoc.Offset;
287 return std::error_code();
Ted Kremenek15322172011-11-10 08:43:12 +0000288}
289
Stephen Hines176edba2014-12-01 14:53:08 -0800290std::error_code
291DiagLoader::readRange(const serialized_diags::Location &SDStart,
292 const serialized_diags::Location &SDEnd,
293 CXSourceRange &SR) {
Ted Kremenek15322172011-11-10 08:43:12 +0000294 CXLoadedDiagnostic::Location *Start, *End;
Stephen Hines176edba2014-12-01 14:53:08 -0800295 Start = TopDiags->Alloc.Allocate<CXLoadedDiagnostic::Location>();
296 End = TopDiags->Alloc.Allocate<CXLoadedDiagnostic::Location>();
297
298 std::error_code EC;
299 if ((EC = readLocation(SDStart, *Start)))
300 return EC;
301 if ((EC = readLocation(SDEnd, *End)))
302 return EC;
Ted Kremenek15322172011-11-10 08:43:12 +0000303
304 CXSourceLocation startLoc = makeLocation(Start);
305 CXSourceLocation endLoc = makeLocation(End);
306 SR = clang_getRange(startLoc, endLoc);
Stephen Hines176edba2014-12-01 14:53:08 -0800307 return std::error_code();
Ted Kremenek15322172011-11-10 08:43:12 +0000308}
309
Stephen Hines176edba2014-12-01 14:53:08 -0800310std::error_code DiagLoader::visitStartOfDiagnostic() {
311 CurrentDiags.push_back(llvm::make_unique<CXLoadedDiagnostic>());
312 return std::error_code();
313}
Ted Kremenek15322172011-11-10 08:43:12 +0000314
Stephen Hines176edba2014-12-01 14:53:08 -0800315std::error_code DiagLoader::visitEndOfDiagnostic() {
316 auto D = CurrentDiags.pop_back_val();
317 if (CurrentDiags.empty())
318 TopDiags->appendDiagnostic(std::move(D));
319 else
320 CurrentDiags.back()->getChildDiagnostics().appendDiagnostic(std::move(D));
321 return std::error_code();
322}
Stephen Hines651f13c2014-04-23 16:59:28 -0700323
Stephen Hines176edba2014-12-01 14:53:08 -0800324std::error_code DiagLoader::visitCategoryRecord(unsigned ID, StringRef Name) {
325 // FIXME: Why do we care about long strings?
326 if (Name.size() > 65536)
327 return reportInvalidFile("Out-of-bounds string in category");
328 TopDiags->Categories[ID] = TopDiags->copyString(Name);
329 return std::error_code();
330}
Ted Kremenek15322172011-11-10 08:43:12 +0000331
Stephen Hines176edba2014-12-01 14:53:08 -0800332std::error_code DiagLoader::visitDiagFlagRecord(unsigned ID, StringRef Name) {
333 // FIXME: Why do we care about long strings?
334 if (Name.size() > 65536)
335 return reportInvalidFile("Out-of-bounds string in warning flag");
336 TopDiags->WarningFlags[ID] = TopDiags->copyString(Name);
337 return std::error_code();
338}
Ted Kremenek15322172011-11-10 08:43:12 +0000339
Stephen Hines176edba2014-12-01 14:53:08 -0800340std::error_code DiagLoader::visitFilenameRecord(unsigned ID, unsigned Size,
341 unsigned Timestamp,
342 StringRef Name) {
343 // FIXME: Why do we care about long strings?
344 if (Name.size() > 65536)
345 return reportInvalidFile("Out-of-bounds string in filename");
346 TopDiags->FileNames[ID] = TopDiags->copyString(Name);
347 TopDiags->Files[ID] =
348 TopDiags->FakeFiles.getVirtualFile(Name, Size, Timestamp);
349 return std::error_code();
350}
Ted Kremenek15322172011-11-10 08:43:12 +0000351
Stephen Hines176edba2014-12-01 14:53:08 -0800352std::error_code
353DiagLoader::visitSourceRangeRecord(const serialized_diags::Location &Start,
354 const serialized_diags::Location &End) {
355 CXSourceRange SR;
356 if (std::error_code EC = readRange(Start, End, SR))
357 return EC;
358 CurrentDiags.back()->Ranges.push_back(SR);
359 return std::error_code();
360}
361
362std::error_code
363DiagLoader::visitFixitRecord(const serialized_diags::Location &Start,
364 const serialized_diags::Location &End,
365 StringRef CodeToInsert) {
366 CXSourceRange SR;
367 if (std::error_code EC = readRange(Start, End, SR))
368 return EC;
369 // FIXME: Why do we care about long strings?
370 if (CodeToInsert.size() > 65536)
371 return reportInvalidFile("Out-of-bounds string in FIXIT");
372 CurrentDiags.back()->FixIts.push_back(
373 std::make_pair(SR, TopDiags->copyString(CodeToInsert)));
374 return std::error_code();
375}
376
377std::error_code DiagLoader::visitDiagnosticRecord(
378 unsigned Severity, const serialized_diags::Location &Location,
379 unsigned Category, unsigned Flag, StringRef Message) {
380 CXLoadedDiagnostic &D = *CurrentDiags.back();
381 D.severity = Severity;
382 if (std::error_code EC = readLocation(Location, D.DiagLoc))
383 return EC;
384 D.category = Category;
385 D.DiagOption = Flag ? TopDiags->WarningFlags[Flag] : "";
386 D.CategoryText = Category ? TopDiags->Categories[Category] : "";
387 D.Spelling = TopDiags->copyString(Message);
388 return std::error_code();
Ted Kremenek15322172011-11-10 08:43:12 +0000389}
390
391extern "C" {
392CXDiagnosticSet clang_loadDiagnostics(const char *file,
393 enum CXLoadDiag_Error *error,
394 CXString *errorString) {
395 DiagLoader L(error, errorString);
396 return L.load(file);
397}
398} // end extern 'C'.