clang  14.0.0git
TextDiagnostic.h
Go to the documentation of this file.
1 //===--- TextDiagnostic.h - Text Diagnostic Pretty-Printing -----*- 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 // This is a utility class that provides support for textual pretty-printing of
10 // diagnostics. It is used to implement the different code paths which require
11 // such functionality in a consistent way.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CLANG_FRONTEND_TEXTDIAGNOSTIC_H
16 #define LLVM_CLANG_FRONTEND_TEXTDIAGNOSTIC_H
17 
19 
20 namespace clang {
21 
22 /// Class to encapsulate the logic for formatting and printing a textual
23 /// diagnostic message.
24 ///
25 /// This class provides an interface for building and emitting a textual
26 /// diagnostic, including all of the macro backtraces, caret diagnostics, FixIt
27 /// Hints, and code snippets. In the presence of macros this involves
28 /// a recursive process, synthesizing notes for each macro expansion.
29 ///
30 /// The purpose of this class is to isolate the implementation of printing
31 /// beautiful text diagnostics from any particular interfaces. The Clang
32 /// DiagnosticClient is implemented through this class as is diagnostic
33 /// printing coming out of libclang.
35  raw_ostream &OS;
36 
37 public:
38  TextDiagnostic(raw_ostream &OS,
39  const LangOptions &LangOpts,
41 
42  ~TextDiagnostic() override;
43 
44  /// Print the diagonstic level to a raw_ostream.
45  ///
46  /// This is a static helper that handles colorizing the level and formatting
47  /// it into an arbitrary output stream. This is used internally by the
48  /// TextDiagnostic emission code, but it can also be used directly by
49  /// consumers that don't have a source manager or other state that the full
50  /// TextDiagnostic logic requires.
51  static void printDiagnosticLevel(raw_ostream &OS,
53  bool ShowColors);
54 
55  /// Pretty-print a diagnostic message to a raw_ostream.
56  ///
57  /// This is a static helper to handle the line wrapping, colorizing, and
58  /// rendering of a diagnostic message to a particular ostream. It is
59  /// publicly visible so that clients which do not have sufficient state to
60  /// build a complete TextDiagnostic object can still get consistent
61  /// formatting of their diagnostic messages.
62  ///
63  /// \param OS Where the message is printed
64  /// \param IsSupplemental true if this is a continuation note diagnostic
65  /// \param Message The text actually printed
66  /// \param CurrentColumn The starting column of the first line, accounting
67  /// for any prefix.
68  /// \param Columns The number of columns to use in line-wrapping, 0 disables
69  /// all line-wrapping.
70  /// \param ShowColors Enable colorizing of the message.
71  static void printDiagnosticMessage(raw_ostream &OS, bool IsSupplemental,
72  StringRef Message, unsigned CurrentColumn,
73  unsigned Columns, bool ShowColors);
74 
75 protected:
77  DiagnosticsEngine::Level Level, StringRef Message,
79  DiagOrStoredDiag D) override;
80 
83  ArrayRef<CharSourceRange> Ranges) override;
84 
87  ArrayRef<FixItHint> Hints) override {
88  emitSnippetAndCaret(Loc, Level, Ranges, Hints);
89  }
90 
91  void emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) override;
92 
94  StringRef ModuleName) override;
95 
97  StringRef ModuleName) override;
98 
99 private:
100  void emitFilename(StringRef Filename, const SourceManager &SM);
101 
102  void emitSnippetAndCaret(FullSourceLoc Loc, DiagnosticsEngine::Level Level,
104  ArrayRef<FixItHint> Hints);
105 
106  void emitSnippet(StringRef SourceLine);
107 
108  void emitParseableFixits(ArrayRef<FixItHint> Hints, const SourceManager &SM);
109 };
110 
111 } // end namespace clang
112 
113 #endif
clang::DiagnosticsEngine::Level
Level
The level of the diagnostic, after it has been through mapping.
Definition: Diagnostic.h:194
clang::FullSourceLoc
A SourceLocation and its associated SourceManager.
Definition: SourceLocation.h:370
Filename
StringRef Filename
Definition: Format.cpp:2333
clang::DiagOrStoredDiag
llvm::PointerUnion< const Diagnostic *, const StoredDiagnostic * > DiagOrStoredDiag
Definition: DiagnosticRenderer.h:33
clang::DiagnosticRenderer
Class to encapsulate the logic for formatting a diagnostic message.
Definition: DiagnosticRenderer.h:47
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:626
clang::TextDiagnostic::emitBuildingModuleLocation
void emitBuildingModuleLocation(FullSourceLoc Loc, PresumedLoc PLoc, StringRef ModuleName) override
Definition: TextDiagnostic.cpp:915
clang::DiagnosticRenderer::LangOpts
const LangOptions & LangOpts
Definition: DiagnosticRenderer.h:49
clang::TextDiagnostic::TextDiagnostic
TextDiagnostic(raw_ostream &OS, const LangOptions &LangOpts, DiagnosticOptions *DiagOpts)
Definition: TextDiagnostic.cpp:666
clang::TextDiagnostic::~TextDiagnostic
~TextDiagnostic() override
Definition: TextDiagnostic.cpp:671
clang::TextDiagnostic::printDiagnosticLevel
static void printDiagnosticLevel(raw_ostream &OS, DiagnosticsEngine::Level Level, bool ShowColors)
Print the diagonstic level to a raw_ostream.
Definition: TextDiagnostic.cpp:695
clang::TextDiagnostic::emitDiagnosticMessage
void emitDiagnosticMessage(FullSourceLoc Loc, PresumedLoc PLoc, DiagnosticsEngine::Level Level, StringRef Message, ArrayRef< CharSourceRange > Ranges, DiagOrStoredDiag D) override
Definition: TextDiagnostic.cpp:673
clang::TextDiagnostic::emitIncludeLocation
void emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) override
Definition: TextDiagnostic.cpp:898
clang::DiagnosticRenderer::DiagOpts
IntrusiveRefCntPtr< DiagnosticOptions > DiagOpts
Definition: DiagnosticRenderer.h:50
clang::TextDiagnostic::emitDiagnosticLoc
void emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc, DiagnosticsEngine::Level Level, ArrayRef< CharSourceRange > Ranges) override
Print out the file/line/column information and include trace.
Definition: TextDiagnostic.cpp:794
clang::TextDiagnostic::printDiagnosticMessage
static void printDiagnosticMessage(raw_ostream &OS, bool IsSupplemental, StringRef Message, unsigned CurrentColumn, unsigned Columns, bool ShowColors)
Pretty-print a diagnostic message to a raw_ostream.
Definition: TextDiagnostic.cpp:726
llvm::ArrayRef
Definition: LLVM.h:34
clang::LangOptions
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:58
clang
Definition: CalledOnceCheck.h:17
clang::prec::Level
Level
Definition: OperatorPrecedence.h:26
clang::TextDiagnostic::emitImportLocation
void emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc, StringRef ModuleName) override
Definition: TextDiagnostic.cpp:906
clang::TextDiagnostic::emitCodeContext
void emitCodeContext(FullSourceLoc Loc, DiagnosticsEngine::Level Level, SmallVectorImpl< CharSourceRange > &Ranges, ArrayRef< FixItHint > Hints) override
Definition: TextDiagnostic.h:85
clang::TextDiagnostic
Class to encapsulate the logic for formatting and printing a textual diagnostic message.
Definition: TextDiagnostic.h:34
DiagnosticRenderer.h
clang::PresumedLoc
Represents an unpacked "presumed" location which can be presented to the user.
Definition: SourceLocation.h:304
llvm::SmallVectorImpl
Definition: LLVM.h:39
SM
#define SM(sm)
Definition: Cuda.cpp:78
clang::DiagnosticOptions
Options for controlling the compiler diagnostics engine.
Definition: DiagnosticOptions.h:70