clang 8.0.0

TextDiagnostic.h
Go to the documentation of this file.
1 //===--- TextDiagnostic.h - Text Diagnostic Pretty-Printing -----*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This is a utility class that provides support for textual pretty-printing of
11 // diagnostics. It is used to implement the different code paths which require
12 // such functionality in a consistent way.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_CLANG_FRONTEND_TEXTDIAGNOSTIC_H
17 #define LLVM_CLANG_FRONTEND_TEXTDIAGNOSTIC_H
18
19 #include "clang/Frontend/DiagnosticRenderer.h"
20
21 namespace clang {
22
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
38 public:
39 TextDiagnostic(raw_ostream &OS,
40 const LangOptions &LangOpts,
41 DiagnosticOptions *DiagOpts);
42
43 ~TextDiagnostic() override;
44
45 /// Print the diagonstic level to a raw_ostream.
46 ///
47 /// This is a static helper that handles colorizing the level and formatting
48 /// it into an arbitrary output stream. This is used internally by the
49 /// TextDiagnostic emission code, but it can also be used directly by
50 /// consumers that don't have a source manager or other state that the full
51 /// TextDiagnostic logic requires.
52 static void printDiagnosticLevel(raw_ostream &OS,
53 DiagnosticsEngine::Level Level,
54 bool ShowColors,
55 bool CLFallbackMode = false);
56
57 /// Pretty-print a diagnostic message to a raw_ostream.
58 ///
59 /// This is a static helper to handle the line wrapping, colorizing, and
60 /// rendering of a diagnostic message to a particular ostream. It is
61 /// publicly visible so that clients which do not have sufficient state to
62 /// build a complete TextDiagnostic object can still get consistent
63 /// formatting of their diagnostic messages.
64 ///
65 /// \param OS Where the message is printed
66 /// \param IsSupplemental true if this is a continuation note diagnostic
67 /// \param Message The text actually printed
68 /// \param CurrentColumn The starting column of the first line, accounting
69 /// for any prefix.
70 /// \param Columns The number of columns to use in line-wrapping, 0 disables
71 /// all line-wrapping.
72 /// \param ShowColors Enable colorizing of the message.
73 static void printDiagnosticMessage(raw_ostream &OS, bool IsSupplemental,
74 StringRef Message, unsigned CurrentColumn,
75 unsigned Columns, bool ShowColors);
76
77 protected:
78 void emitDiagnosticMessage(FullSourceLoc Loc, PresumedLoc PLoc,
79 DiagnosticsEngine::Level Level, StringRef Message,
80 ArrayRef<CharSourceRange> Ranges,
81 DiagOrStoredDiag D) override;
82
83 void emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc,
84 DiagnosticsEngine::Level Level,
85 ArrayRef<CharSourceRange> Ranges) override;
86
87 void emitCodeContext(FullSourceLoc Loc, DiagnosticsEngine::Level Level,
88 SmallVectorImpl<CharSourceRange> &Ranges,
89 ArrayRef<FixItHint> Hints) override {
90 emitSnippetAndCaret(Loc, Level, Ranges, Hints);
91 }
92
93 void emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) override;
94
95 void emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc,
96 StringRef ModuleName) override;
97
98 void emitBuildingModuleLocation(FullSourceLoc Loc, PresumedLoc PLoc,
99 StringRef ModuleName) override;
100
101 private:
102 void emitFilename(StringRef Filename, const SourceManager &SM);
103
104 void emitSnippetAndCaret(FullSourceLoc Loc, DiagnosticsEngine::Level Level,
105 SmallVectorImpl<CharSourceRange> &Ranges,
106 ArrayRef<FixItHint> Hints);
107
108 void emitSnippet(StringRef SourceLine);
109
110 void emitParseableFixits(ArrayRef<FixItHint> Hints, const SourceManager &SM);
111 };
112
113 } // end namespace clang
114
115 #endif
clang::DiagOrStoredDiag
llvm::PointerUnion< const Diagnostic *, const StoredDiagnostic * > DiagOrStoredDiag
Definition: DiagnosticRenderer.h:34
clang::DiagnosticRenderer::LangOpts
const LangOptions & LangOpts
Definition: DiagnosticRenderer.h:50
clang::TextDiagnostic
Class to encapsulate the logic for formatting and printing a textual diagnostic message.
Definition: TextDiagnostic.h:35
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:50
llvm::SmallVectorImpl
Definition: LLVM.h:37
clang::DiagnosticRenderer
Class to encapsulate the logic for formatting a diagnostic message.
Definition: DiagnosticRenderer.h:48
DiagnosticRenderer.h
clang::TextDiagnostic::emitDiagnosticMessage
void emitDiagnosticMessage(FullSourceLoc Loc, PresumedLoc PLoc, DiagnosticsEngine::Level Level, StringRef Message, ArrayRef< CharSourceRange > Ranges, DiagOrStoredDiag D) override
Definition: TextDiagnostic.cpp:675
clang::TextDiagnostic::TextDiagnostic
TextDiagnostic(raw_ostream &OS, const LangOptions &LangOpts, DiagnosticOptions *DiagOpts)
Definition: TextDiagnostic.cpp:668
llvm::ArrayRef
Definition: LLVM.h:32
clang::TextDiagnostic::emitBuildingModuleLocation
void emitBuildingModuleLocation(FullSourceLoc Loc, PresumedLoc PLoc, StringRef ModuleName) override
Definition: TextDiagnostic.cpp:906
clang::TextDiagnostic::~TextDiagnostic
~TextDiagnostic() override
Definition: TextDiagnostic.cpp:673
clang::TextDiagnostic::emitIncludeLocation
void emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) override
Definition: TextDiagnostic.cpp:889
Filename
StringRef Filename
Definition: Format.cpp:1629
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:786
clang::DiagnosticRenderer::DiagOpts
IntrusiveRefCntPtr< DiagnosticOptions > DiagOpts
Definition: DiagnosticRenderer.h:51
clang::PresumedLoc
Represents an unpacked "presumed" location which can be presented to the user.
Definition: SourceLocation.h:285
SM
const SourceManager & SM
Definition: Format.cpp:1490
clang::DiagnosticOptions
Options for controlling the compiler diagnostics engine.
Definition: DiagnosticOptions.h:64
clang::TextDiagnostic::printDiagnosticLevel
static void printDiagnosticLevel(raw_ostream &OS, DiagnosticsEngine::Level Level, bool ShowColors, bool CLFallbackMode=false)
Print the diagonstic level to a raw_ostream.
Definition: TextDiagnostic.cpp:697
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:738
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:22
clang::TextDiagnostic::emitCodeContext
void emitCodeContext(FullSourceLoc Loc, DiagnosticsEngine::Level Level, SmallVectorImpl< CharSourceRange > &Ranges, ArrayRef< FixItHint > Hints) override
Definition: TextDiagnostic.h:87
clang::DiagnosticsEngine::Level
Level
The level of the diagnostic, after it has been through mapping.
Definition: Diagnostic.h:152
clang::FullSourceLoc
A SourceLocation and its associated SourceManager.
Definition: SourceLocation.h:340
clang::TextDiagnostic::emitImportLocation
void emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc, StringRef ModuleName) override
Definition: TextDiagnostic.cpp:897
clang::prec::Level
Level
Definition: OperatorPrecedence.h:27
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:582