Chris Lattner | 2815bc9 | 2013-01-19 18:47:35 +0000 | [diff] [blame] | 1 | //===-- 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 Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 13 | |
| 14 | #include "CXLoadedDiagnostic.h" |
| 15 | #include "CXString.h" |
| 16 | #include "clang/Basic/Diagnostic.h" |
| 17 | #include "clang/Basic/FileManager.h" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 18 | #include "clang/Basic/LLVM.h" |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 19 | #include "clang/Frontend/SerializedDiagnosticReader.h" |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 20 | #include "clang/Frontend/SerializedDiagnostics.h" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 21 | #include "llvm/ADT/Optional.h" |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 22 | #include "llvm/ADT/STLExtras.h" |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringRef.h" |
| 24 | #include "llvm/ADT/Twine.h" |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 25 | #include "llvm/Bitcode/BitstreamReader.h" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 27 | #include "llvm/Support/MemoryBuffer.h" |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 28 | using namespace clang; |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 29 | |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | // Extend CXDiagnosticSetImpl which contains strings for diagnostics. |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | |
Dmitri Gribenko | 9ba7627 | 2013-02-18 19:50:38 +0000 | [diff] [blame] | 34 | typedef llvm::DenseMap<unsigned, const char *> Strings; |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 35 | |
| 36 | namespace { |
| 37 | class CXLoadedDiagnosticSetImpl : public CXDiagnosticSetImpl { |
| 38 | public: |
| 39 | CXLoadedDiagnosticSetImpl() : CXDiagnosticSetImpl(true), FakeFiles(FO) {} |
Pirama Arumuga Nainar | 33337ca | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 40 | ~CXLoadedDiagnosticSetImpl() override {} |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 41 | |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 42 | 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 Gribenko | 9ba7627 | 2013-02-18 19:50:38 +0000 | [diff] [blame] | 50 | |
| 51 | /// \brief Copy the string into our own allocator. |
| 52 | const char *copyString(StringRef Blob) { |
Chris Lattner | b3ce357 | 2013-01-20 02:38:54 +0000 | [diff] [blame] | 53 | char *mem = Alloc.Allocate<char>(Blob.size() + 1); |
| 54 | memcpy(mem, Blob.data(), Blob.size()); |
Chris Lattner | b3ce357 | 2013-01-20 02:38:54 +0000 | [diff] [blame] | 55 | mem[Blob.size()] = '\0'; |
Dmitri Gribenko | 9ba7627 | 2013-02-18 19:50:38 +0000 | [diff] [blame] | 56 | return mem; |
Chris Lattner | b3ce357 | 2013-01-20 02:38:54 +0000 | [diff] [blame] | 57 | } |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 58 | }; |
| 59 | } |
| 60 | |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 61 | //===----------------------------------------------------------------------===// |
| 62 | // Cleanup. |
| 63 | //===----------------------------------------------------------------------===// |
| 64 | |
| 65 | CXLoadedDiagnostic::~CXLoadedDiagnostic() {} |
| 66 | |
| 67 | //===----------------------------------------------------------------------===// |
| 68 | // Public CXLoadedDiagnostic methods. |
| 69 | //===----------------------------------------------------------------------===// |
| 70 | |
| 71 | CXDiagnosticSeverity CXLoadedDiagnostic::getSeverity() const { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 72 | // 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 Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 84 | #undef CASE |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 85 | // The 'Remark' level isn't represented in the stable API. |
| 86 | case serialized_diags::Remark: return CXDiagnostic_Warning; |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | llvm_unreachable("Invalid diagnostic level"); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static 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 Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 97 | CXSourceLocation Loc = { { (void*) V, nullptr }, 0 }; |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 98 | return Loc; |
| 99 | } |
| 100 | |
| 101 | CXSourceLocation 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 | |
| 107 | CXString CXLoadedDiagnostic::getSpelling() const { |
Dmitri Gribenko | 5595ded | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 108 | return cxstring::createRef(Spelling); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | CXString CXLoadedDiagnostic::getDiagnosticOption(CXString *Disable) const { |
| 112 | if (DiagOption.empty()) |
Dmitri Gribenko | dc66adb | 2013-02-01 14:21:22 +0000 | [diff] [blame] | 113 | return cxstring::createEmpty(); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 114 | |
| 115 | // FIXME: possibly refactor with logic in CXStoredDiagnostic. |
| 116 | if (Disable) |
Dmitri Gribenko | 5595ded | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 117 | *Disable = cxstring::createDup((Twine("-Wno-") + DiagOption).str()); |
| 118 | return cxstring::createDup((Twine("-W") + DiagOption).str()); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | unsigned CXLoadedDiagnostic::getCategory() const { |
| 122 | return category; |
| 123 | } |
| 124 | |
Ted Kremenek | 78d5d3b | 2012-04-12 00:03:31 +0000 | [diff] [blame] | 125 | CXString CXLoadedDiagnostic::getCategoryText() const { |
Dmitri Gribenko | 5595ded | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 126 | return cxstring::createDup(CategoryText); |
Ted Kremenek | 78d5d3b | 2012-04-12 00:03:31 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 129 | unsigned CXLoadedDiagnostic::getNumRanges() const { |
| 130 | return Ranges.size(); |
| 131 | } |
| 132 | |
| 133 | CXSourceRange CXLoadedDiagnostic::getRange(unsigned Range) const { |
| 134 | assert(Range < Ranges.size()); |
| 135 | return Ranges[Range]; |
| 136 | } |
| 137 | |
| 138 | unsigned CXLoadedDiagnostic::getNumFixIts() const { |
| 139 | return FixIts.size(); |
| 140 | } |
| 141 | |
| 142 | CXString CXLoadedDiagnostic::getFixIt(unsigned FixIt, |
| 143 | CXSourceRange *ReplacementRange) const { |
| 144 | assert(FixIt < FixIts.size()); |
| 145 | if (ReplacementRange) |
| 146 | *ReplacementRange = FixIts[FixIt].first; |
Dmitri Gribenko | 9ba7627 | 2013-02-18 19:50:38 +0000 | [diff] [blame] | 147 | return cxstring::createRef(FixIts[FixIt].second); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void 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 Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 188 | namespace { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 189 | class DiagLoader : serialized_diags::SerializedDiagnosticReader { |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 190 | enum CXLoadDiag_Error *error; |
| 191 | CXString *errorString; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 192 | 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 Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 196 | if (error) |
| 197 | *error = code; |
| 198 | if (errorString) |
Dmitri Gribenko | 5595ded | 2013-02-02 02:19:29 +0000 | [diff] [blame] | 199 | *errorString = cxstring::createDup(err); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 200 | return serialized_diags::SDError::HandlerFailed; |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 203 | std::error_code reportInvalidFile(llvm::StringRef err) { |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 204 | return reportBad(CXLoadDiag_InvalidFile, err); |
| 205 | } |
| 206 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 207 | std::error_code readRange(const serialized_diags::Location &SDStart, |
| 208 | const serialized_diags::Location &SDEnd, |
| 209 | CXSourceRange &SR); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 210 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 211 | std::error_code readLocation(const serialized_diags::Location &SDLoc, |
| 212 | CXLoadedDiagnostic::Location &LoadedLoc); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 213 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 214 | protected: |
| 215 | std::error_code visitStartOfDiagnostic() override; |
| 216 | std::error_code visitEndOfDiagnostic() override; |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 217 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 218 | 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 Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 238 | public: |
| 239 | DiagLoader(enum CXLoadDiag_Error *e, CXString *es) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 240 | : SerializedDiagnosticReader(), error(e), errorString(es) { |
| 241 | if (error) |
| 242 | *error = CXLoadDiag_None; |
| 243 | if (errorString) |
| 244 | *errorString = cxstring::createEmpty(); |
| 245 | } |
Ted Kremenek | e97ac9e | 2011-11-11 15:19:48 +0000 | [diff] [blame] | 246 | |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 247 | CXDiagnosticSet load(const char *file); |
| 248 | }; |
| 249 | } |
| 250 | |
| 251 | CXDiagnosticSet DiagLoader::load(const char *file) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 252 | TopDiags = llvm::make_unique<CXLoadedDiagnosticSetImpl>(); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 253 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 254 | 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 Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 266 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 267 | return 0; |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 268 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 269 | |
| 270 | return (CXDiagnosticSet)TopDiags.release(); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 273 | std::error_code |
| 274 | DiagLoader::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 Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 283 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 284 | LoadedLoc.line = SDLoc.Line; |
| 285 | LoadedLoc.column = SDLoc.Col; |
| 286 | LoadedLoc.offset = SDLoc.Offset; |
| 287 | return std::error_code(); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 290 | std::error_code |
| 291 | DiagLoader::readRange(const serialized_diags::Location &SDStart, |
| 292 | const serialized_diags::Location &SDEnd, |
| 293 | CXSourceRange &SR) { |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 294 | CXLoadedDiagnostic::Location *Start, *End; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 295 | 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 Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 303 | |
| 304 | CXSourceLocation startLoc = makeLocation(Start); |
| 305 | CXSourceLocation endLoc = makeLocation(End); |
| 306 | SR = clang_getRange(startLoc, endLoc); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 307 | return std::error_code(); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 310 | std::error_code DiagLoader::visitStartOfDiagnostic() { |
| 311 | CurrentDiags.push_back(llvm::make_unique<CXLoadedDiagnostic>()); |
| 312 | return std::error_code(); |
| 313 | } |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 314 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 315 | std::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 Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 323 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 324 | std::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 Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 331 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 332 | std::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 Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 339 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 340 | std::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 Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 351 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 352 | std::error_code |
| 353 | DiagLoader::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 | |
| 362 | std::error_code |
| 363 | DiagLoader::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 | |
| 377 | std::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 Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | extern "C" { |
| 392 | CXDiagnosticSet 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'. |