clang  5.0.0
DiagnosticRenderer.h
Go to the documentation of this file.
1 //===--- DiagnosticRenderer.h - 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 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_DIAGNOSTICRENDERER_H
17 #define LLVM_CLANG_FRONTEND_DIAGNOSTICRENDERER_H
18 
19 #include "clang/Basic/Diagnostic.h"
20 #include "clang/Basic/LLVM.h"
22 #include "llvm/ADT/PointerUnion.h"
23 
24 namespace clang {
25 
26 class DiagnosticOptions;
27 class LangOptions;
29 
30 typedef llvm::PointerUnion<const Diagnostic *,
32 
33 /// \brief Class to encapsulate the logic for formatting a diagnostic message.
34 ///
35 /// Actual "printing" logic is implemented by subclasses.
36 ///
37 /// This class provides an interface for building and emitting
38 /// diagnostic, including all of the macro backtraces, caret diagnostics, FixIt
39 /// Hints, and code snippets. In the presence of macros this involves
40 /// a recursive process, synthesizing notes for each macro expansion.
41 ///
42 /// A brief worklist:
43 /// FIXME: Sink the recursive printing of template instantiations into this
44 /// class.
46 protected:
49 
50  /// \brief 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.
56 
57  /// \brief The location of the last include whose stack was printed if known.
58  ///
59  /// Same restriction as LastLoc essentially, but tracking include stack
60  /// root locations rather than diagnostic locations.
62 
63  /// \brief The level of the last diagnostic emitted.
64  ///
65  /// The level of the last diagnostic emitted. Used to detect level changes
66  /// which change the amount of information displayed.
68 
71 
72  virtual ~DiagnosticRenderer();
73 
74  virtual void emitDiagnosticMessage(FullSourceLoc Loc, PresumedLoc PLoc,
76  StringRef Message,
78  DiagOrStoredDiag Info) = 0;
79 
80  virtual void emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc,
82  ArrayRef<CharSourceRange> Ranges) = 0;
83 
84  virtual void emitCodeContext(FullSourceLoc Loc,
87  ArrayRef<FixItHint> Hints) = 0;
88 
89  virtual void emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) = 0;
90  virtual void emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc,
91  StringRef ModuleName) = 0;
93  StringRef ModuleName) = 0;
94 
99 
100 
101 private:
102  void emitBasicNote(StringRef Message);
103  void emitIncludeStack(FullSourceLoc Loc, PresumedLoc PLoc,
105  void emitIncludeStackRecursively(FullSourceLoc Loc);
106  void emitImportStack(FullSourceLoc Loc);
107  void emitImportStackRecursively(FullSourceLoc Loc, StringRef ModuleName);
108  void emitModuleBuildStack(const SourceManager &SM);
109  void emitCaret(FullSourceLoc Loc, DiagnosticsEngine::Level Level,
111  void emitSingleMacroExpansion(FullSourceLoc Loc,
114  void emitMacroExpansions(FullSourceLoc Loc, DiagnosticsEngine::Level Level,
116  ArrayRef<FixItHint> Hints);
117 
118 public:
119  /// \brief Emit a diagnostic.
120  ///
121  /// This is the primary entry point for emitting diagnostic messages.
122  /// It handles formatting and rendering the message as well as any ancillary
123  /// information needed based on macros whose expansions impact the
124  /// diagnostic.
125  ///
126  /// \param Loc The location for this caret.
127  /// \param Level The level of the diagnostic to be emitted.
128  /// \param Message The diagnostic message to emit.
129  /// \param Ranges The underlined ranges for this code snippet.
130  /// \param FixItHints The FixIt hints active for this diagnostic.
132  StringRef Message, ArrayRef<CharSourceRange> Ranges,
133  ArrayRef<FixItHint> FixItHints,
134  DiagOrStoredDiag D = (Diagnostic *)nullptr);
135 
137 };
138 
139 /// Subclass of DiagnosticRender that turns all subdiagostics into explicit
140 /// notes. It is up to subclasses to further define the behavior.
142 public:
145  : DiagnosticRenderer(LangOpts, DiagOpts) {}
146 
147  ~DiagnosticNoteRenderer() override;
148 
149  void emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) override;
150 
152  StringRef ModuleName) override;
153 
155  StringRef ModuleName) override;
156 
157  virtual void emitNote(FullSourceLoc Loc, StringRef Message) = 0;
158 };
159 } // end clang namespace
160 #endif
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
void emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc, StringRef ModuleName) override
virtual void emitCodeContext(FullSourceLoc Loc, DiagnosticsEngine::Level Level, SmallVectorImpl< CharSourceRange > &Ranges, ArrayRef< FixItHint > Hints)=0
Represents a diagnostic in a form that can be retained until its corresponding source manager is dest...
Definition: Diagnostic.h:1346
void emitStoredDiagnostic(StoredDiagnostic &Diag)
virtual void emitNote(FullSourceLoc Loc, StringRef Message)=0
const LangOptions & LangOpts
virtual void emitBuildingModuleLocation(FullSourceLoc Loc, PresumedLoc PLoc, StringRef ModuleName)=0
void emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) override
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:48
Class to encapsulate the logic for formatting a diagnostic message.
virtual void emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc, StringRef ModuleName)=0
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
void emitDiagnostic(FullSourceLoc Loc, DiagnosticsEngine::Level Level, StringRef Message, ArrayRef< CharSourceRange > Ranges, ArrayRef< FixItHint > FixItHints, DiagOrStoredDiag D=(Diagnostic *) nullptr)
Emit a diagnostic.
DiagnosticsEngine::Level LastLevel
The level of the last diagnostic emitted.
Defines the Diagnostic-related interfaces.
IntrusiveRefCntPtr< DiagnosticOptions > DiagOpts
DiagnosticNoteRenderer(const LangOptions &LangOpts, DiagnosticOptions *DiagOpts)
Represents an unpacked "presumed" location which can be presented to the user.
const SourceManager & SM
Definition: Format.cpp:1293
virtual void emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc, DiagnosticsEngine::Level Level, ArrayRef< CharSourceRange > Ranges)=0
virtual void emitDiagnosticMessage(FullSourceLoc Loc, PresumedLoc PLoc, DiagnosticsEngine::Level Level, StringRef Message, ArrayRef< CharSourceRange > Ranges, DiagOrStoredDiag Info)=0
llvm::PointerUnion< const Diagnostic *, const StoredDiagnostic * > DiagOrStoredDiag
Encodes a location in the source.
Subclass of DiagnosticRender that turns all subdiagostics into explicit notes.
Options for controlling the compiler diagnostics engine.
virtual void beginDiagnostic(DiagOrStoredDiag D, DiagnosticsEngine::Level Level)
DiagnosticRenderer(const LangOptions &LangOpts, DiagnosticOptions *DiagOpts)
SourceLocation LastLoc
The location of the previous diagnostic if known.
SourceLocation LastIncludeLoc
The location of the last include whose stack was printed if known.
virtual void endDiagnostic(DiagOrStoredDiag D, DiagnosticsEngine::Level Level)
virtual void emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc)=0
Defines the clang::SourceLocation class and associated facilities.
Level
The level of the diagnostic, after it has been through mapping.
Definition: Diagnostic.h:150
A little helper class (which is basically a smart pointer that forwards info from DiagnosticsEngine) ...
Definition: Diagnostic.h:1225
A SourceLocation and its associated SourceManager.
void emitBuildingModuleLocation(FullSourceLoc Loc, PresumedLoc PLoc, StringRef ModuleName) override
This class handles loading and caching of source files into memory.