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//===----------------------------------------------------------------------===// 13 14#include "CXLoadedDiagnostic.h" 15#include "CXString.h" 16#include "clang/Basic/Diagnostic.h" 17#include "clang/Basic/FileManager.h" 18#include "clang/Basic/LLVM.h" 19#include "clang/Frontend/SerializedDiagnosticReader.h" 20#include "clang/Frontend/SerializedDiagnostics.h" 21#include "llvm/ADT/Optional.h" 22#include "llvm/ADT/STLExtras.h" 23#include "llvm/ADT/StringRef.h" 24#include "llvm/ADT/Twine.h" 25#include "llvm/Bitcode/BitstreamReader.h" 26#include "llvm/Support/ErrorHandling.h" 27#include "llvm/Support/MemoryBuffer.h" 28using namespace clang; 29 30//===----------------------------------------------------------------------===// 31// Extend CXDiagnosticSetImpl which contains strings for diagnostics. 32//===----------------------------------------------------------------------===// 33 34typedef llvm::DenseMap<unsigned, const char *> Strings; 35 36namespace { 37class CXLoadedDiagnosticSetImpl : public CXDiagnosticSetImpl { 38public: 39 CXLoadedDiagnosticSetImpl() : CXDiagnosticSetImpl(true), FakeFiles(FO) {} 40 ~CXLoadedDiagnosticSetImpl() override {} 41 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; 50 51 /// \brief Copy the string into our own allocator. 52 const char *copyString(StringRef Blob) { 53 char *mem = Alloc.Allocate<char>(Blob.size() + 1); 54 memcpy(mem, Blob.data(), Blob.size()); 55 mem[Blob.size()] = '\0'; 56 return mem; 57 } 58}; 59} 60 61//===----------------------------------------------------------------------===// 62// Cleanup. 63//===----------------------------------------------------------------------===// 64 65CXLoadedDiagnostic::~CXLoadedDiagnostic() {} 66 67//===----------------------------------------------------------------------===// 68// Public CXLoadedDiagnostic methods. 69//===----------------------------------------------------------------------===// 70 71CXDiagnosticSeverity CXLoadedDiagnostic::getSeverity() const { 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) 84#undef CASE 85 // The 'Remark' level isn't represented in the stable API. 86 case serialized_diags::Remark: return CXDiagnostic_Warning; 87 } 88 89 llvm_unreachable("Invalid diagnostic level"); 90} 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; 97 CXSourceLocation Loc = { { (void*) V, nullptr }, 0 }; 98 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 { 108 return cxstring::createRef(Spelling); 109} 110 111CXString CXLoadedDiagnostic::getDiagnosticOption(CXString *Disable) const { 112 if (DiagOption.empty()) 113 return cxstring::createEmpty(); 114 115 // FIXME: possibly refactor with logic in CXStoredDiagnostic. 116 if (Disable) 117 *Disable = cxstring::createDup((Twine("-Wno-") + DiagOption).str()); 118 return cxstring::createDup((Twine("-W") + DiagOption).str()); 119} 120 121unsigned CXLoadedDiagnostic::getCategory() const { 122 return category; 123} 124 125CXString CXLoadedDiagnostic::getCategoryText() const { 126 return cxstring::createDup(CategoryText); 127} 128 129unsigned 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; 147 return cxstring::createRef(FixIts[FixIt].second); 148} 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 188namespace { 189class DiagLoader : serialized_diags::SerializedDiagnosticReader { 190 enum CXLoadDiag_Error *error; 191 CXString *errorString; 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) { 196 if (error) 197 *error = code; 198 if (errorString) 199 *errorString = cxstring::createDup(err); 200 return serialized_diags::SDError::HandlerFailed; 201 } 202 203 std::error_code reportInvalidFile(llvm::StringRef err) { 204 return reportBad(CXLoadDiag_InvalidFile, err); 205 } 206 207 std::error_code readRange(const serialized_diags::Location &SDStart, 208 const serialized_diags::Location &SDEnd, 209 CXSourceRange &SR); 210 211 std::error_code readLocation(const serialized_diags::Location &SDLoc, 212 CXLoadedDiagnostic::Location &LoadedLoc); 213 214protected: 215 std::error_code visitStartOfDiagnostic() override; 216 std::error_code visitEndOfDiagnostic() override; 217 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 238public: 239 DiagLoader(enum CXLoadDiag_Error *e, CXString *es) 240 : SerializedDiagnosticReader(), error(e), errorString(es) { 241 if (error) 242 *error = CXLoadDiag_None; 243 if (errorString) 244 *errorString = cxstring::createEmpty(); 245 } 246 247 CXDiagnosticSet load(const char *file); 248}; 249} 250 251CXDiagnosticSet DiagLoader::load(const char *file) { 252 TopDiags = llvm::make_unique<CXLoadedDiagnosticSetImpl>(); 253 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; 266 } 267 return 0; 268 } 269 270 return (CXDiagnosticSet)TopDiags.release(); 271} 272 273std::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"); 283 } 284 LoadedLoc.line = SDLoc.Line; 285 LoadedLoc.column = SDLoc.Col; 286 LoadedLoc.offset = SDLoc.Offset; 287 return std::error_code(); 288} 289 290std::error_code 291DiagLoader::readRange(const serialized_diags::Location &SDStart, 292 const serialized_diags::Location &SDEnd, 293 CXSourceRange &SR) { 294 CXLoadedDiagnostic::Location *Start, *End; 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; 303 304 CXSourceLocation startLoc = makeLocation(Start); 305 CXSourceLocation endLoc = makeLocation(End); 306 SR = clang_getRange(startLoc, endLoc); 307 return std::error_code(); 308} 309 310std::error_code DiagLoader::visitStartOfDiagnostic() { 311 CurrentDiags.push_back(llvm::make_unique<CXLoadedDiagnostic>()); 312 return std::error_code(); 313} 314 315std::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} 323 324std::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} 331 332std::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} 339 340std::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} 351 352std::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(); 389} 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'. 399