TextDiagnostic.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- TextDiagnostic.h - Text 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 textual 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_TEXTDIAGNOSTIC_H
  20. #define LLVM_CLANG_FRONTEND_TEXTDIAGNOSTIC_H
  21. #include "clang/Frontend/DiagnosticRenderer.h"
  22. namespace clang {
  23. /// Class to encapsulate the logic for formatting and printing a textual
  24. /// diagnostic message.
  25. ///
  26. /// This class provides an interface for building and emitting a textual
  27. /// diagnostic, including all of the macro backtraces, caret diagnostics, FixIt
  28. /// Hints, and code snippets. In the presence of macros this involves
  29. /// a recursive process, synthesizing notes for each macro expansion.
  30. ///
  31. /// The purpose of this class is to isolate the implementation of printing
  32. /// beautiful text diagnostics from any particular interfaces. The Clang
  33. /// DiagnosticClient is implemented through this class as is diagnostic
  34. /// printing coming out of libclang.
  35. class TextDiagnostic : public DiagnosticRenderer {
  36. raw_ostream &OS;
  37. public:
  38. TextDiagnostic(raw_ostream &OS,
  39. const LangOptions &LangOpts,
  40. DiagnosticOptions *DiagOpts);
  41. ~TextDiagnostic() override;
  42. /// Print the diagonstic level to a raw_ostream.
  43. ///
  44. /// This is a static helper that handles colorizing the level and formatting
  45. /// it into an arbitrary output stream. This is used internally by the
  46. /// TextDiagnostic emission code, but it can also be used directly by
  47. /// consumers that don't have a source manager or other state that the full
  48. /// TextDiagnostic logic requires.
  49. static void printDiagnosticLevel(raw_ostream &OS,
  50. DiagnosticsEngine::Level Level,
  51. bool ShowColors);
  52. /// Pretty-print a diagnostic message to a raw_ostream.
  53. ///
  54. /// This is a static helper to handle the line wrapping, colorizing, and
  55. /// rendering of a diagnostic message to a particular ostream. It is
  56. /// publicly visible so that clients which do not have sufficient state to
  57. /// build a complete TextDiagnostic object can still get consistent
  58. /// formatting of their diagnostic messages.
  59. ///
  60. /// \param OS Where the message is printed
  61. /// \param IsSupplemental true if this is a continuation note diagnostic
  62. /// \param Message The text actually printed
  63. /// \param CurrentColumn The starting column of the first line, accounting
  64. /// for any prefix.
  65. /// \param Columns The number of columns to use in line-wrapping, 0 disables
  66. /// all line-wrapping.
  67. /// \param ShowColors Enable colorizing of the message.
  68. static void printDiagnosticMessage(raw_ostream &OS, bool IsSupplemental,
  69. StringRef Message, unsigned CurrentColumn,
  70. unsigned Columns, bool ShowColors);
  71. protected:
  72. void emitDiagnosticMessage(FullSourceLoc Loc, PresumedLoc PLoc,
  73. DiagnosticsEngine::Level Level, StringRef Message,
  74. ArrayRef<CharSourceRange> Ranges,
  75. DiagOrStoredDiag D) override;
  76. void emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc,
  77. DiagnosticsEngine::Level Level,
  78. ArrayRef<CharSourceRange> Ranges) override;
  79. void emitCodeContext(FullSourceLoc Loc, DiagnosticsEngine::Level Level,
  80. SmallVectorImpl<CharSourceRange> &Ranges,
  81. ArrayRef<FixItHint> Hints) override {
  82. emitSnippetAndCaret(Loc, Level, Ranges, Hints);
  83. }
  84. void emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) override;
  85. void emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc,
  86. StringRef ModuleName) override;
  87. void emitBuildingModuleLocation(FullSourceLoc Loc, PresumedLoc PLoc,
  88. StringRef ModuleName) override;
  89. private:
  90. void emitFilename(StringRef Filename, const SourceManager &SM);
  91. void emitSnippetAndCaret(FullSourceLoc Loc, DiagnosticsEngine::Level Level,
  92. SmallVectorImpl<CharSourceRange> &Ranges,
  93. ArrayRef<FixItHint> Hints);
  94. void emitSnippet(StringRef SourceLine);
  95. void emitParseableFixits(ArrayRef<FixItHint> Hints, const SourceManager &SM);
  96. };
  97. } // end namespace clang
  98. #endif
  99. #ifdef __GNUC__
  100. #pragma GCC diagnostic pop
  101. #endif