CIndexDiagnostic.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*===-- CIndexDiagnostic.h - Diagnostics C Interface ------------*- C++ -*-===*\
  2. |* *|
  3. |* Part of the LLVM Project, under the Apache License v2.0 with LLVM *|
  4. |* Exceptions. *|
  5. |* See https://llvm.org/LICENSE.txt for license information. *|
  6. |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *|
  7. |* *|
  8. |*===----------------------------------------------------------------------===*|
  9. |* *|
  10. |* Implements the diagnostic functions of the Clang C interface. *|
  11. |* *|
  12. \*===----------------------------------------------------------------------===*/
  13. #ifndef LLVM_CLANG_TOOLS_LIBCLANG_CINDEXDIAGNOSTIC_H
  14. #define LLVM_CLANG_TOOLS_LIBCLANG_CINDEXDIAGNOSTIC_H
  15. #include "clang-c/Index.h"
  16. #include <memory>
  17. #include <vector>
  18. #include <assert.h>
  19. namespace clang {
  20. class LangOptions;
  21. class StoredDiagnostic;
  22. class CXDiagnosticImpl;
  23. class CXDiagnosticSetImpl {
  24. std::vector<std::unique_ptr<CXDiagnosticImpl>> Diagnostics;
  25. const bool IsExternallyManaged;
  26. public:
  27. CXDiagnosticSetImpl(bool isManaged = false)
  28. : IsExternallyManaged(isManaged) {}
  29. virtual ~CXDiagnosticSetImpl();
  30. size_t getNumDiagnostics() const {
  31. return Diagnostics.size();
  32. }
  33. CXDiagnosticImpl *getDiagnostic(unsigned i) const {
  34. assert(i < getNumDiagnostics());
  35. return Diagnostics[i].get();
  36. }
  37. void appendDiagnostic(std::unique_ptr<CXDiagnosticImpl> D);
  38. bool empty() const {
  39. return Diagnostics.empty();
  40. }
  41. bool isExternallyManaged() const { return IsExternallyManaged; }
  42. };
  43. class CXDiagnosticImpl {
  44. public:
  45. enum Kind { StoredDiagnosticKind, LoadedDiagnosticKind,
  46. CustomNoteDiagnosticKind };
  47. virtual ~CXDiagnosticImpl();
  48. /// Return the severity of the diagnostic.
  49. virtual CXDiagnosticSeverity getSeverity() const = 0;
  50. /// Return the location of the diagnostic.
  51. virtual CXSourceLocation getLocation() const = 0;
  52. /// Return the spelling of the diagnostic.
  53. virtual CXString getSpelling() const = 0;
  54. /// Return the text for the diagnostic option.
  55. virtual CXString getDiagnosticOption(CXString *Disable) const = 0;
  56. /// Return the category of the diagnostic.
  57. virtual unsigned getCategory() const = 0;
  58. /// Return the category string of the diagnostic.
  59. virtual CXString getCategoryText() const = 0;
  60. /// Return the number of source ranges for the diagnostic.
  61. virtual unsigned getNumRanges() const = 0;
  62. /// Return the source ranges for the diagnostic.
  63. virtual CXSourceRange getRange(unsigned Range) const = 0;
  64. /// Return the number of FixIts.
  65. virtual unsigned getNumFixIts() const = 0;
  66. /// Return the FixIt information (source range and inserted text).
  67. virtual CXString getFixIt(unsigned FixIt,
  68. CXSourceRange *ReplacementRange) const = 0;
  69. Kind getKind() const { return K; }
  70. CXDiagnosticSetImpl &getChildDiagnostics() {
  71. return ChildDiags;
  72. }
  73. protected:
  74. CXDiagnosticImpl(Kind k) : K(k) {}
  75. CXDiagnosticSetImpl ChildDiags;
  76. void append(std::unique_ptr<CXDiagnosticImpl> D) {
  77. ChildDiags.appendDiagnostic(std::move(D));
  78. }
  79. private:
  80. Kind K;
  81. };
  82. /// The storage behind a CXDiagnostic
  83. struct CXStoredDiagnostic : public CXDiagnosticImpl {
  84. const StoredDiagnostic &Diag;
  85. const LangOptions &LangOpts;
  86. CXStoredDiagnostic(const StoredDiagnostic &Diag,
  87. const LangOptions &LangOpts)
  88. : CXDiagnosticImpl(StoredDiagnosticKind),
  89. Diag(Diag), LangOpts(LangOpts) { }
  90. ~CXStoredDiagnostic() override {}
  91. /// Return the severity of the diagnostic.
  92. CXDiagnosticSeverity getSeverity() const override;
  93. /// Return the location of the diagnostic.
  94. CXSourceLocation getLocation() const override;
  95. /// Return the spelling of the diagnostic.
  96. CXString getSpelling() const override;
  97. /// Return the text for the diagnostic option.
  98. CXString getDiagnosticOption(CXString *Disable) const override;
  99. /// Return the category of the diagnostic.
  100. unsigned getCategory() const override;
  101. /// Return the category string of the diagnostic.
  102. CXString getCategoryText() const override;
  103. /// Return the number of source ranges for the diagnostic.
  104. unsigned getNumRanges() const override;
  105. /// Return the source ranges for the diagnostic.
  106. CXSourceRange getRange(unsigned Range) const override;
  107. /// Return the number of FixIts.
  108. unsigned getNumFixIts() const override;
  109. /// Return the FixIt information (source range and inserted text).
  110. CXString getFixIt(unsigned FixIt,
  111. CXSourceRange *ReplacementRange) const override;
  112. static bool classof(const CXDiagnosticImpl *D) {
  113. return D->getKind() == StoredDiagnosticKind;
  114. }
  115. };
  116. namespace cxdiag {
  117. CXDiagnosticSetImpl *lazyCreateDiags(CXTranslationUnit TU,
  118. bool checkIfChanged = false);
  119. } // end namespace cxdiag
  120. } // end namespace clang
  121. #endif