1 //===--- PartialDiagnostic.h - Diagnostic "closures" ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements a partial diagnostic that can be emitted anwyhere
11 // in a DiagnosticBuilder stream.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_PARTIALDIAGNOSTIC_H
16 #define LLVM_CLANG_PARTIALDIAGNOSTIC_H
18 #include "clang/Basic/Diagnostic.h"
19 #include "clang/Basic/SourceLocation.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/Support/DataTypes.h"
26 class PartialDiagnostic {
29 Storage() : NumDiagArgs(0), NumDiagRanges(0), NumFixItHints(0) { }
32 /// MaxArguments - The maximum number of arguments we can hold. We
33 /// currently only support up to 10 arguments (%0-%9).
34 /// A single diagnostic with more than that almost certainly has to
35 /// be simplified anyway.
39 /// NumDiagArgs - This contains the number of entries in Arguments.
40 unsigned char NumDiagArgs;
42 /// NumDiagRanges - This is the number of ranges in the DiagRanges array.
43 unsigned char NumDiagRanges;
45 /// \brief The number of code modifications hints in the
47 unsigned char NumFixItHints;
49 /// DiagArgumentsKind - This is an array of ArgumentKind::ArgumentKind enum
50 /// values, with one for each argument. This specifies whether the argument
51 /// is in DiagArgumentsStr or in DiagArguments.
52 unsigned char DiagArgumentsKind[MaxArguments];
54 /// DiagArgumentsVal - The values for the various substitution positions.
55 /// This is used when the argument is not an std::string. The specific value
56 /// is mangled into an intptr_t and the interpretation depends on exactly
57 /// what sort of argument kind it is.
58 intptr_t DiagArgumentsVal[MaxArguments];
60 /// \brief The values for the various substitution positions that have
62 std::string DiagArgumentsStr[MaxArguments];
64 /// DiagRanges - The list of ranges added to this diagnostic. It currently
65 /// only support 10 ranges, could easily be extended if needed.
66 CharSourceRange DiagRanges[10];
68 enum { MaxFixItHints = 3 };
70 /// FixItHints - If valid, provides a hint with some code
71 /// to insert, remove, or modify at a particular position.
72 FixItHint FixItHints[MaxFixItHints];
75 /// \brief An allocator for Storage objects, which uses a small cache to
76 /// objects, used to reduce malloc()/free() traffic for partial diagnostics.
77 class StorageAllocator {
78 static const unsigned NumCached = 16;
79 Storage Cached[NumCached];
80 Storage *FreeList[NumCached];
81 unsigned NumFreeListEntries;
87 /// \brief Allocate new storage.
89 if (NumFreeListEntries == 0)
92 Storage *Result = FreeList[--NumFreeListEntries];
93 Result->NumDiagArgs = 0;
94 Result->NumDiagRanges = 0;
95 Result->NumFixItHints = 0;
99 /// \brief Free the given storage object.
100 void Deallocate(Storage *S) {
101 if (S >= Cached && S <= Cached + NumCached) {
102 FreeList[NumFreeListEntries++] = S;
111 // NOTE: Sema assumes that PartialDiagnostic is location-invariant
112 // in the sense that its bits can be safely memcpy'ed and destructed
113 // in the new location.
115 /// DiagID - The diagnostic ID.
116 mutable unsigned DiagID;
118 /// DiagStorage - Storage for args and ranges.
119 mutable Storage *DiagStorage;
121 /// \brief Allocator used to allocate storage for this diagnostic.
122 StorageAllocator *Allocator;
124 /// \brief Retrieve storage for this particular diagnostic.
125 Storage *getStorage() const {
130 DiagStorage = Allocator->Allocate();
132 assert(Allocator != reinterpret_cast<StorageAllocator *>(~uintptr_t(0)));
133 DiagStorage = new Storage;
143 Allocator->Deallocate(DiagStorage);
144 else if (Allocator != reinterpret_cast<StorageAllocator *>(~uintptr_t(0)))
149 void AddSourceRange(const CharSourceRange &R) const {
151 DiagStorage = getStorage();
153 assert(DiagStorage->NumDiagRanges <
154 llvm::array_lengthof(DiagStorage->DiagRanges) &&
155 "Too many arguments to diagnostic!");
156 DiagStorage->DiagRanges[DiagStorage->NumDiagRanges++] = R;
159 void AddFixItHint(const FixItHint &Hint) const {
164 DiagStorage = getStorage();
166 assert(DiagStorage->NumFixItHints < Storage::MaxFixItHints &&
167 "Too many code modification hints!");
168 DiagStorage->FixItHints[DiagStorage->NumFixItHints++]
173 PartialDiagnostic(unsigned DiagID, StorageAllocator &Allocator)
174 : DiagID(DiagID), DiagStorage(0), Allocator(&Allocator) { }
176 PartialDiagnostic(const PartialDiagnostic &Other)
177 : DiagID(Other.DiagID), DiagStorage(0), Allocator(Other.Allocator)
179 if (Other.DiagStorage) {
180 DiagStorage = getStorage();
181 *DiagStorage = *Other.DiagStorage;
185 PartialDiagnostic(const PartialDiagnostic &Other, Storage *DiagStorage)
186 : DiagID(Other.DiagID), DiagStorage(DiagStorage),
187 Allocator(reinterpret_cast<StorageAllocator *>(~uintptr_t(0)))
189 if (Other.DiagStorage)
190 *this->DiagStorage = *Other.DiagStorage;
193 PartialDiagnostic(const DiagnosticInfo &Other, StorageAllocator &Allocator)
194 : DiagID(Other.getID()), DiagStorage(0), Allocator(&Allocator)
197 for (unsigned I = 0, N = Other.getNumArgs(); I != N; ++I) {
198 if (Other.getArgKind(I) == Diagnostic::ak_std_string)
199 AddString(Other.getArgStdStr(I));
201 AddTaggedVal(Other.getRawArg(I), Other.getArgKind(I));
204 // Copy source ranges.
205 for (unsigned I = 0, N = Other.getNumRanges(); I != N; ++I)
206 AddSourceRange(Other.getRange(I));
209 for (unsigned I = 0, N = Other.getNumFixItHints(); I != N; ++I)
210 AddFixItHint(Other.getFixItHint(I));
213 PartialDiagnostic &operator=(const PartialDiagnostic &Other) {
214 DiagID = Other.DiagID;
215 if (Other.DiagStorage) {
217 DiagStorage = getStorage();
219 *DiagStorage = *Other.DiagStorage;
227 ~PartialDiagnostic() {
231 unsigned getDiagID() const { return DiagID; }
233 void AddTaggedVal(intptr_t V, Diagnostic::ArgumentKind Kind) const {
235 DiagStorage = getStorage();
237 assert(DiagStorage->NumDiagArgs < Storage::MaxArguments &&
238 "Too many arguments to diagnostic!");
239 DiagStorage->DiagArgumentsKind[DiagStorage->NumDiagArgs] = Kind;
240 DiagStorage->DiagArgumentsVal[DiagStorage->NumDiagArgs++] = V;
243 void AddString(llvm::StringRef V) const {
245 DiagStorage = getStorage();
247 assert(DiagStorage->NumDiagArgs < Storage::MaxArguments &&
248 "Too many arguments to diagnostic!");
249 DiagStorage->DiagArgumentsKind[DiagStorage->NumDiagArgs]
250 = Diagnostic::ak_std_string;
251 DiagStorage->DiagArgumentsStr[DiagStorage->NumDiagArgs++] = V;
254 void Emit(const DiagnosticBuilder &DB) const {
258 // Add all arguments.
259 for (unsigned i = 0, e = DiagStorage->NumDiagArgs; i != e; ++i) {
260 if ((Diagnostic::ArgumentKind)DiagStorage->DiagArgumentsKind[i]
261 == Diagnostic::ak_std_string)
262 DB.AddString(DiagStorage->DiagArgumentsStr[i]);
264 DB.AddTaggedVal(DiagStorage->DiagArgumentsVal[i],
265 (Diagnostic::ArgumentKind)DiagStorage->DiagArgumentsKind[i]);
269 for (unsigned i = 0, e = DiagStorage->NumDiagRanges; i != e; ++i)
270 DB.AddSourceRange(DiagStorage->DiagRanges[i]);
273 for (unsigned i = 0, e = DiagStorage->NumFixItHints; i != e; ++i)
274 DB.AddFixItHint(DiagStorage->FixItHints[i]);
277 /// \brief Clear out this partial diagnostic, giving it a new diagnostic ID
278 /// and removing all of its arguments, ranges, and fix-it hints.
279 void Reset(unsigned DiagID = 0) {
280 this->DiagID = DiagID;
284 bool hasStorage() const { return DiagStorage != 0; }
286 friend const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
288 PD.AddTaggedVal(I, Diagnostic::ak_uint);
292 friend const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
294 PD.AddTaggedVal(I, Diagnostic::ak_sint);
298 friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
300 PD.AddTaggedVal(reinterpret_cast<intptr_t>(S), Diagnostic::ak_c_string);
304 friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
311 friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
312 const SourceRange &R) {
313 PD.AddSourceRange(CharSourceRange::getTokenRange(R));
317 friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
318 const CharSourceRange &R) {
319 PD.AddSourceRange(R);
323 friend const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
324 const FixItHint &Hint) {
325 PD.AddFixItHint(Hint);
331 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
332 const PartialDiagnostic &PD) {
337 /// \brief A partial diagnostic along with the source location where this
338 /// diagnostic occurs.
339 typedef std::pair<SourceLocation, PartialDiagnostic> PartialDiagnosticAt;
341 } // end namespace clang