DiagnosticRenderer.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- DiagnosticRenderer.h - Diagnostic Pretty-Printing --------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This is a utility class that provides support for pretty-printing of
  15. // diagnostics. It is used to implement the different code paths which require
  16. // such functionality in a consistent way.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_CLANG_FRONTEND_DIAGNOSTICRENDERER_H
  20. #define LLVM_CLANG_FRONTEND_DIAGNOSTICRENDERER_H
  21. #include "clang/Basic/Diagnostic.h"
  22. #include "clang/Basic/DiagnosticOptions.h"
  23. #include "clang/Basic/LLVM.h"
  24. #include "clang/Basic/SourceLocation.h"
  25. #include "llvm/ADT/ArrayRef.h"
  26. #include "llvm/ADT/IntrusiveRefCntPtr.h"
  27. #include "llvm/ADT/PointerUnion.h"
  28. #include "llvm/ADT/StringRef.h"
  29. namespace clang {
  30. class LangOptions;
  31. class SourceManager;
  32. using DiagOrStoredDiag =
  33. llvm::PointerUnion<const Diagnostic *, const StoredDiagnostic *>;
  34. /// Class to encapsulate the logic for formatting a diagnostic message.
  35. ///
  36. /// Actual "printing" logic is implemented by subclasses.
  37. ///
  38. /// This class provides an interface for building and emitting
  39. /// diagnostic, including all of the macro backtraces, caret diagnostics, FixIt
  40. /// Hints, and code snippets. In the presence of macros this involves
  41. /// a recursive process, synthesizing notes for each macro expansion.
  42. ///
  43. /// A brief worklist:
  44. /// FIXME: Sink the recursive printing of template instantiations into this
  45. /// class.
  46. class DiagnosticRenderer {
  47. protected:
  48. const LangOptions &LangOpts;
  49. IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
  50. /// The location of the previous diagnostic if known.
  51. ///
  52. /// This will be invalid in cases where there is no (known) previous
  53. /// diagnostic location, or that location itself is invalid or comes from
  54. /// a different source manager than SM.
  55. SourceLocation LastLoc;
  56. /// The location of the last include whose stack was printed if known.
  57. ///
  58. /// Same restriction as LastLoc essentially, but tracking include stack
  59. /// root locations rather than diagnostic locations.
  60. SourceLocation LastIncludeLoc;
  61. /// The level of the last diagnostic emitted.
  62. ///
  63. /// The level of the last diagnostic emitted. Used to detect level changes
  64. /// which change the amount of information displayed.
  65. DiagnosticsEngine::Level LastLevel = DiagnosticsEngine::Ignored;
  66. DiagnosticRenderer(const LangOptions &LangOpts,
  67. DiagnosticOptions *DiagOpts);
  68. virtual ~DiagnosticRenderer();
  69. virtual void emitDiagnosticMessage(FullSourceLoc Loc, PresumedLoc PLoc,
  70. DiagnosticsEngine::Level Level,
  71. StringRef Message,
  72. ArrayRef<CharSourceRange> Ranges,
  73. DiagOrStoredDiag Info) = 0;
  74. virtual void emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc,
  75. DiagnosticsEngine::Level Level,
  76. ArrayRef<CharSourceRange> Ranges) = 0;
  77. virtual void emitCodeContext(FullSourceLoc Loc,
  78. DiagnosticsEngine::Level Level,
  79. SmallVectorImpl<CharSourceRange> &Ranges,
  80. ArrayRef<FixItHint> Hints) = 0;
  81. virtual void emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) = 0;
  82. virtual void emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc,
  83. StringRef ModuleName) = 0;
  84. virtual void emitBuildingModuleLocation(FullSourceLoc Loc, PresumedLoc PLoc,
  85. StringRef ModuleName) = 0;
  86. virtual void beginDiagnostic(DiagOrStoredDiag D,
  87. DiagnosticsEngine::Level Level) {}
  88. virtual void endDiagnostic(DiagOrStoredDiag D,
  89. DiagnosticsEngine::Level Level) {}
  90. private:
  91. void emitBasicNote(StringRef Message);
  92. void emitIncludeStack(FullSourceLoc Loc, PresumedLoc PLoc,
  93. DiagnosticsEngine::Level Level);
  94. void emitIncludeStackRecursively(FullSourceLoc Loc);
  95. void emitImportStack(FullSourceLoc Loc);
  96. void emitImportStackRecursively(FullSourceLoc Loc, StringRef ModuleName);
  97. void emitModuleBuildStack(const SourceManager &SM);
  98. void emitCaret(FullSourceLoc Loc, DiagnosticsEngine::Level Level,
  99. ArrayRef<CharSourceRange> Ranges, ArrayRef<FixItHint> Hints);
  100. void emitSingleMacroExpansion(FullSourceLoc Loc,
  101. DiagnosticsEngine::Level Level,
  102. ArrayRef<CharSourceRange> Ranges);
  103. void emitMacroExpansions(FullSourceLoc Loc, DiagnosticsEngine::Level Level,
  104. ArrayRef<CharSourceRange> Ranges,
  105. ArrayRef<FixItHint> Hints);
  106. public:
  107. /// Emit a diagnostic.
  108. ///
  109. /// This is the primary entry point for emitting diagnostic messages.
  110. /// It handles formatting and rendering the message as well as any ancillary
  111. /// information needed based on macros whose expansions impact the
  112. /// diagnostic.
  113. ///
  114. /// \param Loc The location for this caret.
  115. /// \param Level The level of the diagnostic to be emitted.
  116. /// \param Message The diagnostic message to emit.
  117. /// \param Ranges The underlined ranges for this code snippet.
  118. /// \param FixItHints The FixIt hints active for this diagnostic.
  119. void emitDiagnostic(FullSourceLoc Loc, DiagnosticsEngine::Level Level,
  120. StringRef Message, ArrayRef<CharSourceRange> Ranges,
  121. ArrayRef<FixItHint> FixItHints,
  122. DiagOrStoredDiag D = (Diagnostic *)nullptr);
  123. void emitStoredDiagnostic(StoredDiagnostic &Diag);
  124. };
  125. /// Subclass of DiagnosticRender that turns all subdiagostics into explicit
  126. /// notes. It is up to subclasses to further define the behavior.
  127. class DiagnosticNoteRenderer : public DiagnosticRenderer {
  128. public:
  129. DiagnosticNoteRenderer(const LangOptions &LangOpts,
  130. DiagnosticOptions *DiagOpts)
  131. : DiagnosticRenderer(LangOpts, DiagOpts) {}
  132. ~DiagnosticNoteRenderer() override;
  133. void emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) override;
  134. void emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc,
  135. StringRef ModuleName) override;
  136. void emitBuildingModuleLocation(FullSourceLoc Loc, PresumedLoc PLoc,
  137. StringRef ModuleName) override;
  138. virtual void emitNote(FullSourceLoc Loc, StringRef Message) = 0;
  139. };
  140. } // namespace clang
  141. #endif // LLVM_CLANG_FRONTEND_DIAGNOSTICRENDERER_H
  142. #ifdef __GNUC__
  143. #pragma GCC diagnostic pop
  144. #endif