CIndexDiagnostic.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. //===- CIndexDiagnostic.cpp - Diagnostics C Interface ---------------------===//
  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 the diagnostic functions of the Clang C interface.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "CIndexDiagnostic.h"
  13. #include "CIndexer.h"
  14. #include "CXTranslationUnit.h"
  15. #include "CXSourceLocation.h"
  16. #include "CXString.h"
  17. #include "clang/Basic/DiagnosticOptions.h"
  18. #include "clang/Frontend/ASTUnit.h"
  19. #include "clang/Frontend/DiagnosticRenderer.h"
  20. #include "llvm/ADT/SmallString.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. using namespace clang;
  23. using namespace clang::cxloc;
  24. using namespace clang::cxdiag;
  25. using namespace llvm;
  26. CXDiagnosticSetImpl::~CXDiagnosticSetImpl() {}
  27. void
  28. CXDiagnosticSetImpl::appendDiagnostic(std::unique_ptr<CXDiagnosticImpl> D) {
  29. Diagnostics.push_back(std::move(D));
  30. }
  31. CXDiagnosticImpl::~CXDiagnosticImpl() {}
  32. namespace {
  33. class CXDiagnosticCustomNoteImpl : public CXDiagnosticImpl {
  34. std::string Message;
  35. CXSourceLocation Loc;
  36. public:
  37. CXDiagnosticCustomNoteImpl(StringRef Msg, CXSourceLocation L)
  38. : CXDiagnosticImpl(CustomNoteDiagnosticKind), Message(std::string(Msg)),
  39. Loc(L) {}
  40. ~CXDiagnosticCustomNoteImpl() override {}
  41. CXDiagnosticSeverity getSeverity() const override {
  42. return CXDiagnostic_Note;
  43. }
  44. CXSourceLocation getLocation() const override { return Loc; }
  45. CXString getSpelling() const override {
  46. return cxstring::createRef(Message.c_str());
  47. }
  48. CXString getDiagnosticOption(CXString *Disable) const override {
  49. if (Disable)
  50. *Disable = cxstring::createEmpty();
  51. return cxstring::createEmpty();
  52. }
  53. unsigned getCategory() const override { return 0; }
  54. CXString getCategoryText() const override { return cxstring::createEmpty(); }
  55. unsigned getNumRanges() const override { return 0; }
  56. CXSourceRange getRange(unsigned Range) const override {
  57. return clang_getNullRange();
  58. }
  59. unsigned getNumFixIts() const override { return 0; }
  60. CXString getFixIt(unsigned FixIt,
  61. CXSourceRange *ReplacementRange) const override {
  62. if (ReplacementRange)
  63. *ReplacementRange = clang_getNullRange();
  64. return cxstring::createEmpty();
  65. }
  66. };
  67. class CXDiagnosticRenderer : public DiagnosticNoteRenderer {
  68. public:
  69. CXDiagnosticRenderer(const LangOptions &LangOpts,
  70. DiagnosticOptions *DiagOpts,
  71. CXDiagnosticSetImpl *mainSet)
  72. : DiagnosticNoteRenderer(LangOpts, DiagOpts),
  73. CurrentSet(mainSet), MainSet(mainSet) {}
  74. ~CXDiagnosticRenderer() override {}
  75. void beginDiagnostic(DiagOrStoredDiag D,
  76. DiagnosticsEngine::Level Level) override {
  77. const StoredDiagnostic *SD = D.dyn_cast<const StoredDiagnostic*>();
  78. if (!SD)
  79. return;
  80. if (Level != DiagnosticsEngine::Note)
  81. CurrentSet = MainSet;
  82. auto Owner = std::make_unique<CXStoredDiagnostic>(*SD, LangOpts);
  83. CXStoredDiagnostic &CD = *Owner;
  84. CurrentSet->appendDiagnostic(std::move(Owner));
  85. if (Level != DiagnosticsEngine::Note)
  86. CurrentSet = &CD.getChildDiagnostics();
  87. }
  88. void emitDiagnosticMessage(FullSourceLoc Loc, PresumedLoc PLoc,
  89. DiagnosticsEngine::Level Level, StringRef Message,
  90. ArrayRef<CharSourceRange> Ranges,
  91. DiagOrStoredDiag D) override {
  92. if (!D.isNull())
  93. return;
  94. CXSourceLocation L;
  95. if (Loc.hasManager())
  96. L = translateSourceLocation(Loc.getManager(), LangOpts, Loc);
  97. else
  98. L = clang_getNullLocation();
  99. CurrentSet->appendDiagnostic(
  100. std::make_unique<CXDiagnosticCustomNoteImpl>(Message, L));
  101. }
  102. void emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc,
  103. DiagnosticsEngine::Level Level,
  104. ArrayRef<CharSourceRange> Ranges) override {}
  105. void emitCodeContext(FullSourceLoc Loc, DiagnosticsEngine::Level Level,
  106. SmallVectorImpl<CharSourceRange> &Ranges,
  107. ArrayRef<FixItHint> Hints) override {}
  108. void emitNote(FullSourceLoc Loc, StringRef Message) override {
  109. CXSourceLocation L;
  110. if (Loc.hasManager())
  111. L = translateSourceLocation(Loc.getManager(), LangOpts, Loc);
  112. else
  113. L = clang_getNullLocation();
  114. CurrentSet->appendDiagnostic(
  115. std::make_unique<CXDiagnosticCustomNoteImpl>(Message, L));
  116. }
  117. CXDiagnosticSetImpl *CurrentSet;
  118. CXDiagnosticSetImpl *MainSet;
  119. };
  120. }
  121. CXDiagnosticSetImpl *cxdiag::lazyCreateDiags(CXTranslationUnit TU,
  122. bool checkIfChanged) {
  123. ASTUnit *AU = cxtu::getASTUnit(TU);
  124. if (TU->Diagnostics && checkIfChanged) {
  125. // In normal use, ASTUnit's diagnostics should not change unless we reparse.
  126. // Currently they can only change by using the internal testing flag
  127. // '-error-on-deserialized-decl' which will error during deserialization of
  128. // a declaration. What will happen is:
  129. //
  130. // -c-index-test gets a CXTranslationUnit
  131. // -checks the diagnostics, the diagnostics set is lazily created,
  132. // no errors are reported
  133. // -later does an operation, like annotation of tokens, that triggers
  134. // -error-on-deserialized-decl, that will emit a diagnostic error,
  135. // that ASTUnit will catch and add to its stored diagnostics vector.
  136. // -c-index-test wants to check whether an error occurred after performing
  137. // the operation but can only query the lazily created set.
  138. //
  139. // We check here if a new diagnostic was appended since the last time the
  140. // diagnostic set was created, in which case we reset it.
  141. CXDiagnosticSetImpl *
  142. Set = static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics);
  143. if (AU->stored_diag_size() != Set->getNumDiagnostics()) {
  144. // Diagnostics in the ASTUnit were updated, reset the associated
  145. // diagnostics.
  146. delete Set;
  147. TU->Diagnostics = nullptr;
  148. }
  149. }
  150. if (!TU->Diagnostics) {
  151. CXDiagnosticSetImpl *Set = new CXDiagnosticSetImpl();
  152. TU->Diagnostics = Set;
  153. IntrusiveRefCntPtr<DiagnosticOptions> DOpts = new DiagnosticOptions;
  154. CXDiagnosticRenderer Renderer(AU->getASTContext().getLangOpts(),
  155. &*DOpts, Set);
  156. for (ASTUnit::stored_diag_iterator it = AU->stored_diag_begin(),
  157. ei = AU->stored_diag_end(); it != ei; ++it) {
  158. Renderer.emitStoredDiagnostic(*it);
  159. }
  160. }
  161. return static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics);
  162. }
  163. //-----------------------------------------------------------------------------
  164. // C Interface Routines
  165. //-----------------------------------------------------------------------------
  166. unsigned clang_getNumDiagnostics(CXTranslationUnit Unit) {
  167. if (cxtu::isNotUsableTU(Unit)) {
  168. LOG_BAD_TU(Unit);
  169. return 0;
  170. }
  171. if (!cxtu::getASTUnit(Unit))
  172. return 0;
  173. return lazyCreateDiags(Unit, /*checkIfChanged=*/true)->getNumDiagnostics();
  174. }
  175. CXDiagnostic clang_getDiagnostic(CXTranslationUnit Unit, unsigned Index) {
  176. if (cxtu::isNotUsableTU(Unit)) {
  177. LOG_BAD_TU(Unit);
  178. return nullptr;
  179. }
  180. CXDiagnosticSet D = clang_getDiagnosticSetFromTU(Unit);
  181. if (!D)
  182. return nullptr;
  183. CXDiagnosticSetImpl *Diags = static_cast<CXDiagnosticSetImpl*>(D);
  184. if (Index >= Diags->getNumDiagnostics())
  185. return nullptr;
  186. return Diags->getDiagnostic(Index);
  187. }
  188. CXDiagnosticSet clang_getDiagnosticSetFromTU(CXTranslationUnit Unit) {
  189. if (cxtu::isNotUsableTU(Unit)) {
  190. LOG_BAD_TU(Unit);
  191. return nullptr;
  192. }
  193. if (!cxtu::getASTUnit(Unit))
  194. return nullptr;
  195. return static_cast<CXDiagnostic>(lazyCreateDiags(Unit));
  196. }
  197. void clang_disposeDiagnostic(CXDiagnostic Diagnostic) {
  198. // No-op. Kept as a legacy API. CXDiagnostics are now managed
  199. // by the enclosing CXDiagnosticSet.
  200. }
  201. CXString clang_formatDiagnostic(CXDiagnostic Diagnostic, unsigned Options) {
  202. if (!Diagnostic)
  203. return cxstring::createEmpty();
  204. CXDiagnosticSeverity Severity = clang_getDiagnosticSeverity(Diagnostic);
  205. SmallString<256> Str;
  206. llvm::raw_svector_ostream Out(Str);
  207. if (Options & CXDiagnostic_DisplaySourceLocation) {
  208. // Print source location (file:line), along with optional column
  209. // and source ranges.
  210. CXFile File;
  211. unsigned Line, Column;
  212. clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
  213. &File, &Line, &Column, nullptr);
  214. if (File) {
  215. CXString FName = clang_getFileName(File);
  216. Out << clang_getCString(FName) << ":" << Line << ":";
  217. clang_disposeString(FName);
  218. if (Options & CXDiagnostic_DisplayColumn)
  219. Out << Column << ":";
  220. if (Options & CXDiagnostic_DisplaySourceRanges) {
  221. unsigned N = clang_getDiagnosticNumRanges(Diagnostic);
  222. bool PrintedRange = false;
  223. for (unsigned I = 0; I != N; ++I) {
  224. CXFile StartFile, EndFile;
  225. CXSourceRange Range = clang_getDiagnosticRange(Diagnostic, I);
  226. unsigned StartLine, StartColumn, EndLine, EndColumn;
  227. clang_getSpellingLocation(clang_getRangeStart(Range),
  228. &StartFile, &StartLine, &StartColumn,
  229. nullptr);
  230. clang_getSpellingLocation(clang_getRangeEnd(Range),
  231. &EndFile, &EndLine, &EndColumn, nullptr);
  232. if (StartFile != EndFile || StartFile != File)
  233. continue;
  234. Out << "{" << StartLine << ":" << StartColumn << "-"
  235. << EndLine << ":" << EndColumn << "}";
  236. PrintedRange = true;
  237. }
  238. if (PrintedRange)
  239. Out << ":";
  240. }
  241. Out << " ";
  242. }
  243. }
  244. /* Print warning/error/etc. */
  245. switch (Severity) {
  246. case CXDiagnostic_Ignored: llvm_unreachable("impossible");
  247. case CXDiagnostic_Note: Out << "note: "; break;
  248. case CXDiagnostic_Warning: Out << "warning: "; break;
  249. case CXDiagnostic_Error: Out << "error: "; break;
  250. case CXDiagnostic_Fatal: Out << "fatal error: "; break;
  251. }
  252. CXString Text = clang_getDiagnosticSpelling(Diagnostic);
  253. if (clang_getCString(Text))
  254. Out << clang_getCString(Text);
  255. else
  256. Out << "<no diagnostic text>";
  257. clang_disposeString(Text);
  258. if (Options & (CXDiagnostic_DisplayOption | CXDiagnostic_DisplayCategoryId |
  259. CXDiagnostic_DisplayCategoryName)) {
  260. bool NeedBracket = true;
  261. bool NeedComma = false;
  262. if (Options & CXDiagnostic_DisplayOption) {
  263. CXString OptionName = clang_getDiagnosticOption(Diagnostic, nullptr);
  264. if (const char *OptionText = clang_getCString(OptionName)) {
  265. if (OptionText[0]) {
  266. Out << " [" << OptionText;
  267. NeedBracket = false;
  268. NeedComma = true;
  269. }
  270. }
  271. clang_disposeString(OptionName);
  272. }
  273. if (Options & (CXDiagnostic_DisplayCategoryId |
  274. CXDiagnostic_DisplayCategoryName)) {
  275. if (unsigned CategoryID = clang_getDiagnosticCategory(Diagnostic)) {
  276. if (Options & CXDiagnostic_DisplayCategoryId) {
  277. if (NeedBracket)
  278. Out << " [";
  279. if (NeedComma)
  280. Out << ", ";
  281. Out << CategoryID;
  282. NeedBracket = false;
  283. NeedComma = true;
  284. }
  285. if (Options & CXDiagnostic_DisplayCategoryName) {
  286. CXString CategoryName = clang_getDiagnosticCategoryText(Diagnostic);
  287. if (NeedBracket)
  288. Out << " [";
  289. if (NeedComma)
  290. Out << ", ";
  291. Out << clang_getCString(CategoryName);
  292. NeedBracket = false;
  293. NeedComma = true;
  294. clang_disposeString(CategoryName);
  295. }
  296. }
  297. }
  298. (void) NeedComma; // Silence dead store warning.
  299. if (!NeedBracket)
  300. Out << "]";
  301. }
  302. return cxstring::createDup(Out.str());
  303. }
  304. unsigned clang_defaultDiagnosticDisplayOptions() {
  305. return CXDiagnostic_DisplaySourceLocation | CXDiagnostic_DisplayColumn |
  306. CXDiagnostic_DisplayOption;
  307. }
  308. enum CXDiagnosticSeverity clang_getDiagnosticSeverity(CXDiagnostic Diag) {
  309. if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl*>(Diag))
  310. return D->getSeverity();
  311. return CXDiagnostic_Ignored;
  312. }
  313. CXSourceLocation clang_getDiagnosticLocation(CXDiagnostic Diag) {
  314. if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl*>(Diag))
  315. return D->getLocation();
  316. return clang_getNullLocation();
  317. }
  318. CXString clang_getDiagnosticSpelling(CXDiagnostic Diag) {
  319. if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
  320. return D->getSpelling();
  321. return cxstring::createEmpty();
  322. }
  323. CXString clang_getDiagnosticOption(CXDiagnostic Diag, CXString *Disable) {
  324. if (Disable)
  325. *Disable = cxstring::createEmpty();
  326. if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
  327. return D->getDiagnosticOption(Disable);
  328. return cxstring::createEmpty();
  329. }
  330. unsigned clang_getDiagnosticCategory(CXDiagnostic Diag) {
  331. if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
  332. return D->getCategory();
  333. return 0;
  334. }
  335. CXString clang_getDiagnosticCategoryName(unsigned Category) {
  336. // Kept for backward compatibility.
  337. return cxstring::createRef(DiagnosticIDs::getCategoryNameFromID(Category));
  338. }
  339. CXString clang_getDiagnosticCategoryText(CXDiagnostic Diag) {
  340. if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
  341. return D->getCategoryText();
  342. return cxstring::createEmpty();
  343. }
  344. unsigned clang_getDiagnosticNumRanges(CXDiagnostic Diag) {
  345. if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
  346. return D->getNumRanges();
  347. return 0;
  348. }
  349. CXSourceRange clang_getDiagnosticRange(CXDiagnostic Diag, unsigned Range) {
  350. CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag);
  351. if (!D || Range >= D->getNumRanges())
  352. return clang_getNullRange();
  353. return D->getRange(Range);
  354. }
  355. unsigned clang_getDiagnosticNumFixIts(CXDiagnostic Diag) {
  356. if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag))
  357. return D->getNumFixIts();
  358. return 0;
  359. }
  360. CXString clang_getDiagnosticFixIt(CXDiagnostic Diag, unsigned FixIt,
  361. CXSourceRange *ReplacementRange) {
  362. CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag);
  363. if (!D || FixIt >= D->getNumFixIts()) {
  364. if (ReplacementRange)
  365. *ReplacementRange = clang_getNullRange();
  366. return cxstring::createEmpty();
  367. }
  368. return D->getFixIt(FixIt, ReplacementRange);
  369. }
  370. void clang_disposeDiagnosticSet(CXDiagnosticSet Diags) {
  371. if (CXDiagnosticSetImpl *D = static_cast<CXDiagnosticSetImpl *>(Diags)) {
  372. if (D->isExternallyManaged())
  373. delete D;
  374. }
  375. }
  376. CXDiagnostic clang_getDiagnosticInSet(CXDiagnosticSet Diags,
  377. unsigned Index) {
  378. if (CXDiagnosticSetImpl *D = static_cast<CXDiagnosticSetImpl*>(Diags))
  379. if (Index < D->getNumDiagnostics())
  380. return D->getDiagnostic(Index);
  381. return nullptr;
  382. }
  383. CXDiagnosticSet clang_getChildDiagnostics(CXDiagnostic Diag) {
  384. if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag)) {
  385. CXDiagnosticSetImpl &ChildDiags = D->getChildDiagnostics();
  386. return ChildDiags.empty() ? nullptr : (CXDiagnosticSet) &ChildDiags;
  387. }
  388. return nullptr;
  389. }
  390. unsigned clang_getNumDiagnosticsInSet(CXDiagnosticSet Diags) {
  391. if (CXDiagnosticSetImpl *D = static_cast<CXDiagnosticSetImpl*>(Diags))
  392. return D->getNumDiagnostics();
  393. return 0;
  394. }