clang 20.0.0git
PartialDiagnostic.h
Go to the documentation of this file.
1//===- PartialDiagnostic.h - Diagnostic "closures" --------------*- 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/// \file
10/// Implements a partial diagnostic that can be emitted anwyhere
11/// in a DiagnosticBuilder stream.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_BASIC_PARTIALDIAGNOSTIC_H
16#define LLVM_CLANG_BASIC_PARTIALDIAGNOSTIC_H
17
19#include "clang/Basic/LLVM.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/StringRef.h"
23#include <cassert>
24#include <cstdint>
25#include <string>
26#include <type_traits>
27#include <utility>
28
29namespace clang {
30
32private:
33 // NOTE: Sema assumes that PartialDiagnostic is location-invariant
34 // in the sense that its bits can be safely memcpy'ed and destructed
35 // in the new location.
36
37 /// The diagnostic ID.
38 mutable unsigned DiagID = 0;
39public:
40 struct NullDiagnostic {};
41
42 /// Create a null partial diagnostic, which cannot carry a payload,
43 /// and only exists to be swapped with a real partial diagnostic.
45
46 PartialDiagnostic(unsigned DiagID, DiagStorageAllocator &Allocator_)
47 : StreamingDiagnostic(Allocator_), DiagID(DiagID) {}
48
50 : StreamingDiagnostic(), DiagID(Other.DiagID) {
51 Allocator = Other.Allocator;
52 if (Other.DiagStorage) {
54 *DiagStorage = *Other.DiagStorage;
55 }
56 }
57
58 template <typename T> const PartialDiagnostic &operator<<(const T &V) const {
59 const StreamingDiagnostic &DB = *this;
60 DB << V;
61 return *this;
62 }
63
64 // It is necessary to limit this to rvalue reference to avoid calling this
65 // function with a bitfield lvalue argument since non-const reference to
66 // bitfield is not allowed.
67 template <typename T,
68 typename = std::enable_if_t<!std::is_lvalue_reference<T>::value>>
69 const PartialDiagnostic &operator<<(T &&V) const {
70 const StreamingDiagnostic &DB = *this;
71 DB << std::move(V);
72 return *this;
73 }
74
76 Allocator = Other.Allocator;
77 DiagStorage = Other.DiagStorage;
78 Other.DiagStorage = nullptr;
79 }
80
82 DiagnosticStorage *DiagStorage_)
83 : DiagID(Other.DiagID) {
84 Allocator = reinterpret_cast<DiagStorageAllocator *>(~uintptr_t(0));
85 DiagStorage = DiagStorage_;
86 if (Other.DiagStorage)
87 *this->DiagStorage = *Other.DiagStorage;
88 }
89
91 : DiagID(Other.getID()) {
92 Allocator = &Allocator_;
93 // Copy arguments.
94 for (unsigned I = 0, N = Other.getNumArgs(); I != N; ++I) {
95 if (Other.getArgKind(I) == DiagnosticsEngine::ak_std_string)
96 AddString(Other.getArgStdStr(I));
97 else
98 AddTaggedVal(Other.getRawArg(I), Other.getArgKind(I));
99 }
100
101 // Copy source ranges.
102 for (unsigned I = 0, N = Other.getNumRanges(); I != N; ++I)
103 AddSourceRange(Other.getRange(I));
104
105 // Copy fix-its.
106 for (unsigned I = 0, N = Other.getNumFixItHints(); I != N; ++I)
107 AddFixItHint(Other.getFixItHint(I));
108 }
109
111 DiagID = Other.DiagID;
112 if (Other.DiagStorage) {
113 if (!DiagStorage)
115
116 *DiagStorage = *Other.DiagStorage;
117 } else {
118 freeStorage();
119 }
120
121 return *this;
122 }
123
125 freeStorage();
126
127 DiagID = Other.DiagID;
128 DiagStorage = Other.DiagStorage;
129 Allocator = Other.Allocator;
130
131 Other.DiagStorage = nullptr;
132 return *this;
133 }
134
136 std::swap(DiagID, PD.DiagID);
137 std::swap(DiagStorage, PD.DiagStorage);
138 std::swap(Allocator, PD.Allocator);
139 }
140
141 unsigned getDiagID() const { return DiagID; }
142 void setDiagID(unsigned ID) { DiagID = ID; }
143
144 void Emit(const DiagnosticBuilder &DB) const {
145 if (!DiagStorage)
146 return;
147
148 // Add all arguments.
149 for (unsigned i = 0, e = DiagStorage->NumDiagArgs; i != e; ++i) {
153 else
156 }
157
158 // Add all ranges.
161
162 // Add all fix-its.
163 for (const FixItHint &Fix : DiagStorage->FixItHints)
164 DB.AddFixItHint(Fix);
165 }
166
168 SmallVectorImpl<char> &Buf) const {
170 Emit(DB);
171 Diagnostic(&Diags, DB).FormatDiagnostic(Buf);
172 DB.Clear();
173 }
174
175 /// Clear out this partial diagnostic, giving it a new diagnostic ID
176 /// and removing all of its arguments, ranges, and fix-it hints.
177 void Reset(unsigned DiagID = 0) {
178 this->DiagID = DiagID;
179 freeStorage();
180 }
181
182 bool hasStorage() const { return DiagStorage != nullptr; }
183
184 /// Retrieve the string argument at the given index.
185 StringRef getStringArg(unsigned I) {
186 assert(DiagStorage && "No diagnostic storage?");
187 assert(I < DiagStorage->NumDiagArgs && "Not enough diagnostic args");
189 == DiagnosticsEngine::ak_std_string && "Not a string arg");
190 return DiagStorage->DiagArgumentsStr[I];
191 }
192};
193
195 const PartialDiagnostic &PD) {
196 PD.Emit(DB);
197 return DB;
198}
199
200/// A partial diagnostic along with the source location where this
201/// diagnostic occurs.
202using PartialDiagnosticAt = std::pair<SourceLocation, PartialDiagnostic>;
203
204} // namespace clang
205
206#endif // LLVM_CLANG_BASIC_PARTIALDIAGNOSTIC_H
#define V(N, I)
Definition: ASTContext.h:3443
static char ID
Definition: Arena.cpp:183
Defines the Diagnostic-related interfaces.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
SourceRange Range
Definition: SemaObjC.cpp:758
Defines the clang::SourceLocation class and associated facilities.
Represents a character-granular source range.
An allocator for DiagnosticStorage objects, which uses a small cache to objects, used to reduce mallo...
Definition: Diagnostic.h:192
A little helper class used to produce diagnostics.
Definition: Diagnostic.h:1220
void Clear() const
Clear out the current diagnostic.
Definition: Diagnostic.h:1255
A little helper class (which is basically a smart pointer that forwards info from DiagnosticsEngine a...
Definition: Diagnostic.h:1512
void FormatDiagnostic(SmallVectorImpl< char > &OutStr) const
Format this diagnostic into a string, substituting the formal arguments into the %0 slots.
Definition: Diagnostic.cpp:971
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:231
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1493
@ ak_std_string
std::string
Definition: Diagnostic.h:245
Annotates a diagnostic with some code that should be inserted, removed, or replaced to fix the proble...
Definition: Diagnostic.h:75
void EmitToString(DiagnosticsEngine &Diags, SmallVectorImpl< char > &Buf) const
PartialDiagnostic(const PartialDiagnostic &Other, DiagnosticStorage *DiagStorage_)
PartialDiagnostic(const PartialDiagnostic &Other)
const PartialDiagnostic & operator<<(T &&V) const
PartialDiagnostic(const Diagnostic &Other, DiagStorageAllocator &Allocator_)
StringRef getStringArg(unsigned I)
Retrieve the string argument at the given index.
void Emit(const DiagnosticBuilder &DB) const
void setDiagID(unsigned ID)
PartialDiagnostic(unsigned DiagID, DiagStorageAllocator &Allocator_)
PartialDiagnostic & operator=(const PartialDiagnostic &Other)
const PartialDiagnostic & operator<<(const T &V) const
void Reset(unsigned DiagID=0)
Clear out this partial diagnostic, giving it a new diagnostic ID and removing all of its arguments,...
PartialDiagnostic & operator=(PartialDiagnostic &&Other)
void swap(PartialDiagnostic &PD)
PartialDiagnostic(NullDiagnostic)
Create a null partial diagnostic, which cannot carry a payload, and only exists to be swapped with a ...
PartialDiagnostic(PartialDiagnostic &&Other)
The streaming interface shared between DiagnosticBuilder and PartialDiagnostic.
Definition: Diagnostic.h:1102
DiagStorageAllocator * Allocator
Allocator used to allocate storage for this diagnostic.
Definition: Diagnostic.h:1110
DiagnosticStorage * DiagStorage
Definition: Diagnostic.h:1107
void AddString(StringRef V) const
Definition: Diagnostic.h:1153
void AddTaggedVal(uint64_t V, DiagnosticsEngine::ArgumentKind Kind) const
Definition: Diagnostic.h:1143
void AddSourceRange(const CharSourceRange &R) const
Definition: Diagnostic.h:1164
DiagnosticStorage * getStorage() const
Retrieve storage for this particular diagnostic.
Definition: Diagnostic.h:1114
void AddFixItHint(const FixItHint &Hint) const
Definition: Diagnostic.h:1171
The JSON file list parser is used to communicate input to InstallAPI.
const StreamingDiagnostic & operator<<(const StreamingDiagnostic &DB, const ASTContext::SectionInfo &Section)
Insertion operator for diagnostics.
const FunctionProtoType * T
std::pair< SourceLocation, PartialDiagnostic > PartialDiagnosticAt
A partial diagnostic along with the source location where this diagnostic occurs.
@ Other
Other implicit parameter.
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...
unsigned char DiagArgumentsKind[MaxArguments]
Specifies for each argument whether it is in DiagArgumentsStr or in DiagArguments.
Definition: Diagnostic.h:167
SmallVector< CharSourceRange, 8 > DiagRanges
The list of ranges added to this diagnostic.
Definition: Diagnostic.h:181
unsigned char NumDiagArgs
The number of entries in Arguments.
Definition: Diagnostic.h:163
SmallVector< FixItHint, 6 > FixItHints
If valid, provides a hint with some code to insert, remove, or modify at a particular position.
Definition: Diagnostic.h:185
std::string DiagArgumentsStr[MaxArguments]
The values for the various substitution positions that have string arguments.
Definition: Diagnostic.h:178
uint64_t DiagArgumentsVal[MaxArguments]
The values for the various substitution positions.
Definition: Diagnostic.h:174