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.
36 MaxArguments = DiagnosticsEngine::MaxArguments
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 = DiagnosticsEngine::MaxFixItHints };
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 if (DiagStorage->NumFixItHints >= Storage::MaxFixItHints)
169 return; // Don't crash in release builds
170 DiagStorage->FixItHints[DiagStorage->NumFixItHints++]
175 PartialDiagnostic(unsigned DiagID, StorageAllocator &Allocator)
176 : DiagID(DiagID), DiagStorage(0), Allocator(&Allocator) { }
178 PartialDiagnostic(const PartialDiagnostic &Other)
179 : DiagID(Other.DiagID), DiagStorage(0), Allocator(Other.Allocator)
181 if (Other.DiagStorage) {
182 DiagStorage = getStorage();
183 *DiagStorage = *Other.DiagStorage;
187 PartialDiagnostic(const PartialDiagnostic &Other, Storage *DiagStorage)
188 : DiagID(Other.DiagID), DiagStorage(DiagStorage),
189 Allocator(reinterpret_cast<StorageAllocator *>(~uintptr_t(0)))
191 if (Other.DiagStorage)
192 *this->DiagStorage = *Other.DiagStorage;
195 PartialDiagnostic(const Diagnostic &Other, StorageAllocator &Allocator)
196 : DiagID(Other.getID()), DiagStorage(0), Allocator(&Allocator)
199 for (unsigned I = 0, N = Other.getNumArgs(); I != N; ++I) {
200 if (Other.getArgKind(I) == DiagnosticsEngine::ak_std_string)
201 AddString(Other.getArgStdStr(I));
203 AddTaggedVal(Other.getRawArg(I), Other.getArgKind(I));
206 // Copy source ranges.
207 for (unsigned I = 0, N = Other.getNumRanges(); I != N; ++I)
208 AddSourceRange(Other.getRange(I));
211 for (unsigned I = 0, N = Other.getNumFixItHints(); I != N; ++I)
212 AddFixItHint(Other.getFixItHint(I));
215 PartialDiagnostic &operator=(const PartialDiagnostic &Other) {
216 DiagID = Other.DiagID;
217 if (Other.DiagStorage) {
219 DiagStorage = getStorage();
221 *DiagStorage = *Other.DiagStorage;
229 ~PartialDiagnostic() {
233 unsigned getDiagID() const { return DiagID; }
235 void AddTaggedVal(intptr_t V, DiagnosticsEngine::ArgumentKind Kind) const {
237 DiagStorage = getStorage();
239 assert(DiagStorage->NumDiagArgs < Storage::MaxArguments &&
240 "Too many arguments to diagnostic!");
241 DiagStorage->DiagArgumentsKind[DiagStorage->NumDiagArgs] = Kind;
242 DiagStorage->DiagArgumentsVal[DiagStorage->NumDiagArgs++] = V;
245 void AddString(StringRef V) const {
247 DiagStorage = getStorage();
249 assert(DiagStorage->NumDiagArgs < Storage::MaxArguments &&
250 "Too many arguments to diagnostic!");
251 DiagStorage->DiagArgumentsKind[DiagStorage->NumDiagArgs]
252 = DiagnosticsEngine::ak_std_string;
253 DiagStorage->DiagArgumentsStr[DiagStorage->NumDiagArgs++] = V;
256 void Emit(const DiagnosticBuilder &DB) const {
260 // Add all arguments.
261 for (unsigned i = 0, e = DiagStorage->NumDiagArgs; i != e; ++i) {
262 if ((DiagnosticsEngine::ArgumentKind)DiagStorage->DiagArgumentsKind[i]
263 == DiagnosticsEngine::ak_std_string)
264 DB.AddString(DiagStorage->DiagArgumentsStr[i]);
266 DB.AddTaggedVal(DiagStorage->DiagArgumentsVal[i],
267 (DiagnosticsEngine::ArgumentKind)DiagStorage->DiagArgumentsKind[i]);
271 for (unsigned i = 0, e = DiagStorage->NumDiagRanges; i != e; ++i)
272 DB.AddSourceRange(DiagStorage->DiagRanges[i]);
275 for (unsigned i = 0, e = DiagStorage->NumFixItHints; i != e; ++i)
276 DB.AddFixItHint(DiagStorage->FixItHints[i]);
279 /// \brief Clear out this partial diagnostic, giving it a new diagnostic ID
280 /// and removing all of its arguments, ranges, and fix-it hints.
281 void Reset(unsigned DiagID = 0) {
282 this->DiagID = DiagID;
286 bool hasStorage() const { return DiagStorage != 0; }
288 friend const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
290 PD.AddTaggedVal(I, DiagnosticsEngine::ak_uint);
294 friend const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
296 PD.AddTaggedVal(I, DiagnosticsEngine::ak_sint);
300 friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
302 PD.AddTaggedVal(reinterpret_cast<intptr_t>(S),
303 DiagnosticsEngine::ak_c_string);
307 friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
314 friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
315 const SourceRange &R) {
316 PD.AddSourceRange(CharSourceRange::getTokenRange(R));
320 friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
321 const CharSourceRange &R) {
322 PD.AddSourceRange(R);
326 friend const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
327 const FixItHint &Hint) {
328 PD.AddFixItHint(Hint);
334 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
335 const PartialDiagnostic &PD) {
340 /// \brief A partial diagnostic along with the source location where this
341 /// diagnostic occurs.
342 typedef std::pair<SourceLocation, PartialDiagnostic> PartialDiagnosticAt;
344 } // end namespace clang