clang 24.0.0git
DiagnosticRenderer.h
Go to the documentation of this file.
1//===- DiagnosticRenderer.h - 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 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_DIAGNOSTICRENDERER_H
16#define LLVM_CLANG_FRONTEND_DIAGNOSTICRENDERER_H
17
20#include "clang/Basic/LLVM.h"
22#include "llvm/ADT/ArrayRef.h"
23#include "llvm/ADT/IntrusiveRefCntPtr.h"
24#include "llvm/ADT/PointerUnion.h"
25#include "llvm/ADT/StringRef.h"
26#include <optional>
27
28namespace clang {
29
30class LangOptions;
31class SourceManager;
32
34 llvm::PointerUnion<const Diagnostic *, const StoredDiagnostic *>;
35
36/// Maps both endpoints of \p Range to their macro expansion, so that the range
37/// can be shown to a user.
38///
39/// \returns nullopt if \p Range is invalid, or if an endpoint lies outside
40/// \p FID, or if the \p Range ends before it starts.
41std::optional<CharSourceRange> getExpansionRangeInFile(CharSourceRange Range,
42 FileID FID,
43 const SourceManager &SM);
44
45/// Class to encapsulate the logic for formatting a diagnostic message.
46///
47/// Actual "printing" logic is implemented by subclasses.
48///
49/// This class provides an interface for building and emitting
50/// diagnostic, including all of the macro backtraces, caret diagnostics, FixIt
51/// Hints, and code snippets. In the presence of macros this involves
52/// a recursive process, synthesizing notes for each macro expansion.
53///
54/// A brief worklist:
55/// FIXME: Sink the recursive printing of template instantiations into this
56/// class.
58protected:
61
62 /// The location of the previous diagnostic if known.
63 ///
64 /// This will be invalid in cases where there is no (known) previous
65 /// diagnostic location, or that location itself is invalid or comes from
66 /// a different source manager than SM.
68
69 /// The location of the last include whose stack was printed if known.
70 ///
71 /// Same restriction as LastLoc essentially, but tracking include stack
72 /// root locations rather than diagnostic locations.
74
75 /// The level of the last diagnostic emitted.
76 ///
77 /// The level of the last diagnostic emitted. Used to detect level changes
78 /// which change the amount of information displayed.
80
82
84
87 StringRef Message,
89 DiagOrStoredDiag Info) = 0;
90
93 ArrayRef<CharSourceRange> Ranges) = 0;
94
98 ArrayRef<FixItHint> Hints) = 0;
99
100 virtual void emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) = 0;
102 StringRef ModuleName) = 0;
104 StringRef ModuleName) = 0;
105
110
111private:
112 void emitBasicNote(StringRef Message);
113 void emitIncludeStack(FullSourceLoc Loc, PresumedLoc PLoc,
115 void emitIncludeStackRecursively(FullSourceLoc Loc);
116 void emitImportStack(FullSourceLoc Loc);
117 void emitImportStackRecursively(FullSourceLoc Loc, StringRef ModuleName);
118 void emitModuleBuildStack(const SourceManager &SM);
119 void emitCaret(FullSourceLoc Loc, DiagnosticsEngine::Level Level,
121 void emitSingleMacroExpansion(FullSourceLoc Loc,
124 void emitMacroExpansions(FullSourceLoc Loc, DiagnosticsEngine::Level Level,
126 ArrayRef<FixItHint> Hints);
127
128public:
129 /// Emit a diagnostic.
130 ///
131 /// This is the primary entry point for emitting diagnostic messages.
132 /// It handles formatting and rendering the message as well as any ancillary
133 /// information needed based on macros whose expansions impact the
134 /// diagnostic.
135 ///
136 /// \param Loc The location for this caret.
137 /// \param Level The level of the diagnostic to be emitted.
138 /// \param Message The diagnostic message to emit.
139 /// \param Ranges The underlined ranges for this code snippet.
140 /// \param FixItHints The FixIt hints active for this diagnostic.
142 StringRef Message, ArrayRef<CharSourceRange> Ranges,
143 ArrayRef<FixItHint> FixItHints,
144 DiagOrStoredDiag D = (Diagnostic *)nullptr);
145
147};
148
149/// Subclass of DiagnosticRender that turns all subdiagostics into explicit
150/// notes. It is up to subclasses to further define the behavior.
152public:
156
158
159 void emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) override;
160
162 StringRef ModuleName) override;
163
165 StringRef ModuleName) override;
166
167 virtual void emitNote(FullSourceLoc Loc, StringRef Message) = 0;
168};
169
170} // namespace clang
171
172#endif // LLVM_CLANG_FRONTEND_DIAGNOSTICRENDERER_H
Defines the Diagnostic-related interfaces.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
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.
Defines the clang::SourceLocation class and associated facilities.
Represents a byte-granular source range.
virtual void emitNote(FullSourceLoc Loc, StringRef Message)=0
void emitBuildingModuleLocation(FullSourceLoc Loc, PresumedLoc PLoc, StringRef ModuleName) override
DiagnosticNoteRenderer(const LangOptions &LangOpts, DiagnosticOptions &DiagOpts)
void emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) override
void emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc, StringRef ModuleName) override
Options for controlling the compiler diagnostics engine.
virtual void endDiagnostic(DiagOrStoredDiag D, DiagnosticsEngine::Level Level)
virtual void emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc)=0
const LangOptions & LangOpts
void emitStoredDiagnostic(StoredDiagnostic &Diag)
virtual void emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc, DiagnosticsEngine::Level Level, ArrayRef< CharSourceRange > Ranges)=0
SourceLocation LastLoc
The location of the previous diagnostic if known.
DiagnosticOptions & DiagOpts
DiagnosticsEngine::Level LastLevel
The level of the last diagnostic emitted.
virtual void emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc, StringRef ModuleName)=0
virtual void emitDiagnosticMessage(FullSourceLoc Loc, PresumedLoc PLoc, DiagnosticsEngine::Level Level, StringRef Message, ArrayRef< CharSourceRange > Ranges, DiagOrStoredDiag Info)=0
SourceLocation LastIncludeLoc
The location of the last include whose stack was printed if known.
virtual void emitBuildingModuleLocation(FullSourceLoc Loc, PresumedLoc PLoc, StringRef ModuleName)=0
void emitDiagnostic(FullSourceLoc Loc, DiagnosticsEngine::Level Level, StringRef Message, ArrayRef< CharSourceRange > Ranges, ArrayRef< FixItHint > FixItHints, DiagOrStoredDiag D=(Diagnostic *) nullptr)
Emit a diagnostic.
virtual void emitCodeContext(FullSourceLoc Loc, DiagnosticsEngine::Level Level, SmallVectorImpl< CharSourceRange > &Ranges, ArrayRef< FixItHint > Hints)=0
DiagnosticRenderer(const LangOptions &LangOpts, DiagnosticOptions &DiagOpts)
virtual void beginDiagnostic(DiagOrStoredDiag D, DiagnosticsEngine::Level Level)
A little helper class (which is basically a smart pointer that forwards info from DiagnosticsEngine a...
Level
The level of the diagnostic, after it has been through mapping.
Definition Diagnostic.h:239
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
A SourceLocation and its associated SourceManager.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Represents an unpacked "presumed" location which can be presented to the user.
Encodes a location in the source.
This class handles loading and caching of source files into memory.
Represents a diagnostic in a form that can be retained until its corresponding source manager is dest...
Top level wrappers for InstallAPI frontend operations.
llvm::PointerUnion< const Diagnostic *, const StoredDiagnostic * > DiagOrStoredDiag
std::optional< CharSourceRange > getExpansionRangeInFile(CharSourceRange Range, FileID FID, const SourceManager &SM)
Maps both endpoints of Range to their macro expansion, so that the range can be shown to a user.