CXLoadedDiagnostic.cpp 13 KB

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