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 // The MaxArguments and MaxFixItHints member enum values from
30 // DiagnosticsEngine are private but DiagnosticsEngine declares
31 // PartialDiagnostic a friend. These enum values are redeclared
32 // here so that the nested Storage class below can access them.
33 MaxArguments = DiagnosticsEngine::MaxArguments
37 Storage() : NumDiagArgs(0), NumDiagRanges(0) { }
40 /// MaxArguments - The maximum number of arguments we can hold. We
41 /// currently only support up to 10 arguments (%0-%9).
42 /// A single diagnostic with more than that almost certainly has to
43 /// be simplified anyway.
44 MaxArguments = PartialDiagnostic::MaxArguments
47 /// NumDiagArgs - This contains the number of entries in Arguments.
48 unsigned char NumDiagArgs;
50 /// NumDiagRanges - This is the number of ranges in the DiagRanges array.
51 unsigned char NumDiagRanges;
53 /// DiagArgumentsKind - This is an array of ArgumentKind::ArgumentKind enum
54 /// values, with one for each argument. This specifies whether the argument
55 /// is in DiagArgumentsStr or in DiagArguments.
56 unsigned char DiagArgumentsKind[MaxArguments];
58 /// DiagArgumentsVal - The values for the various substitution positions.
59 /// This is used when the argument is not an std::string. The specific value
60 /// is mangled into an intptr_t and the interpretation depends on exactly
61 /// what sort of argument kind it is.
62 intptr_t DiagArgumentsVal[MaxArguments];
64 /// \brief The values for the various substitution positions that have
66 std::string DiagArgumentsStr[MaxArguments];
68 /// DiagRanges - The list of ranges added to this diagnostic. It currently
69 /// only support 10 ranges, could easily be extended if needed.
70 CharSourceRange DiagRanges[10];
72 /// FixItHints - If valid, provides a hint with some code
73 /// to insert, remove, or modify at a particular position.
74 SmallVector<FixItHint, 6> FixItHints;
77 /// \brief An allocator for Storage objects, which uses a small cache to
78 /// objects, used to reduce malloc()/free() traffic for partial diagnostics.
79 class StorageAllocator {
80 static const unsigned NumCached = 16;
81 Storage Cached[NumCached];
82 Storage *FreeList[NumCached];
83 unsigned NumFreeListEntries;
89 /// \brief Allocate new storage.
91 if (NumFreeListEntries == 0)
94 Storage *Result = FreeList[--NumFreeListEntries];
95 Result->NumDiagArgs = 0;
96 Result->NumDiagRanges = 0;
97 Result->FixItHints.clear();
101 /// \brief Free the given storage object.
102 void Deallocate(Storage *S) {
103 if (S >= Cached && S <= Cached + NumCached) {
104 FreeList[NumFreeListEntries++] = S;
113 // NOTE: Sema assumes that PartialDiagnostic is location-invariant
114 // in the sense that its bits can be safely memcpy'ed and destructed
115 // in the new location.
117 /// DiagID - The diagnostic ID.
118 mutable unsigned DiagID;
120 /// DiagStorage - Storage for args and ranges.
121 mutable Storage *DiagStorage;
123 /// \brief Allocator used to allocate storage for this diagnostic.
124 StorageAllocator *Allocator;
126 /// \brief Retrieve storage for this particular diagnostic.
127 Storage *getStorage() const {
132 DiagStorage = Allocator->Allocate();
134 assert(Allocator != reinterpret_cast<StorageAllocator *>(~uintptr_t(0)));
135 DiagStorage = new Storage;
144 // The hot path for PartialDiagnostic is when we just used it to wrap an ID
145 // (typically so we have the flexibility of passing a more complex
146 // diagnostic into the callee, but that does not commonly occur).
148 // Split this out into a slow function for silly compilers (*cough*) which
149 // can't do decent partial inlining.
153 void freeStorageSlow() {
155 Allocator->Deallocate(DiagStorage);
156 else if (Allocator != reinterpret_cast<StorageAllocator *>(~uintptr_t(0)))
161 void AddSourceRange(const CharSourceRange &R) const {
163 DiagStorage = getStorage();
165 assert(DiagStorage->NumDiagRanges <
166 llvm::array_lengthof(DiagStorage->DiagRanges) &&
167 "Too many arguments to diagnostic!");
168 DiagStorage->DiagRanges[DiagStorage->NumDiagRanges++] = R;
171 void AddFixItHint(const FixItHint &Hint) const {
176 DiagStorage = getStorage();
178 DiagStorage->FixItHints.push_back(Hint);
182 PartialDiagnostic(unsigned DiagID, StorageAllocator &Allocator)
183 : DiagID(DiagID), DiagStorage(0), Allocator(&Allocator) { }
185 PartialDiagnostic(const PartialDiagnostic &Other)
186 : DiagID(Other.DiagID), DiagStorage(0), Allocator(Other.Allocator)
188 if (Other.DiagStorage) {
189 DiagStorage = getStorage();
190 *DiagStorage = *Other.DiagStorage;
194 PartialDiagnostic(const PartialDiagnostic &Other, Storage *DiagStorage)
195 : DiagID(Other.DiagID), DiagStorage(DiagStorage),
196 Allocator(reinterpret_cast<StorageAllocator *>(~uintptr_t(0)))
198 if (Other.DiagStorage)
199 *this->DiagStorage = *Other.DiagStorage;
202 PartialDiagnostic(const Diagnostic &Other, StorageAllocator &Allocator)
203 : DiagID(Other.getID()), DiagStorage(0), Allocator(&Allocator)
206 for (unsigned I = 0, N = Other.getNumArgs(); I != N; ++I) {
207 if (Other.getArgKind(I) == DiagnosticsEngine::ak_std_string)
208 AddString(Other.getArgStdStr(I));
210 AddTaggedVal(Other.getRawArg(I), Other.getArgKind(I));
213 // Copy source ranges.
214 for (unsigned I = 0, N = Other.getNumRanges(); I != N; ++I)
215 AddSourceRange(Other.getRange(I));
218 for (unsigned I = 0, N = Other.getNumFixItHints(); I != N; ++I)
219 AddFixItHint(Other.getFixItHint(I));
222 PartialDiagnostic &operator=(const PartialDiagnostic &Other) {
223 DiagID = Other.DiagID;
224 if (Other.DiagStorage) {
226 DiagStorage = getStorage();
228 *DiagStorage = *Other.DiagStorage;
236 ~PartialDiagnostic() {
240 unsigned getDiagID() const { return DiagID; }
242 void AddTaggedVal(intptr_t V, DiagnosticsEngine::ArgumentKind Kind) const {
244 DiagStorage = getStorage();
246 assert(DiagStorage->NumDiagArgs < Storage::MaxArguments &&
247 "Too many arguments to diagnostic!");
248 DiagStorage->DiagArgumentsKind[DiagStorage->NumDiagArgs] = Kind;
249 DiagStorage->DiagArgumentsVal[DiagStorage->NumDiagArgs++] = V;
252 void AddString(StringRef V) const {
254 DiagStorage = getStorage();
256 assert(DiagStorage->NumDiagArgs < Storage::MaxArguments &&
257 "Too many arguments to diagnostic!");
258 DiagStorage->DiagArgumentsKind[DiagStorage->NumDiagArgs]
259 = DiagnosticsEngine::ak_std_string;
260 DiagStorage->DiagArgumentsStr[DiagStorage->NumDiagArgs++] = V;
263 void Emit(const DiagnosticBuilder &DB) const {
267 // Add all arguments.
268 for (unsigned i = 0, e = DiagStorage->NumDiagArgs; i != e; ++i) {
269 if ((DiagnosticsEngine::ArgumentKind)DiagStorage->DiagArgumentsKind[i]
270 == DiagnosticsEngine::ak_std_string)
271 DB.AddString(DiagStorage->DiagArgumentsStr[i]);
273 DB.AddTaggedVal(DiagStorage->DiagArgumentsVal[i],
274 (DiagnosticsEngine::ArgumentKind)DiagStorage->DiagArgumentsKind[i]);
278 for (unsigned i = 0, e = DiagStorage->NumDiagRanges; i != e; ++i)
279 DB.AddSourceRange(DiagStorage->DiagRanges[i]);
282 for (unsigned i = 0, e = DiagStorage->FixItHints.size(); i != e; ++i)
283 DB.AddFixItHint(DiagStorage->FixItHints[i]);
286 /// \brief Clear out this partial diagnostic, giving it a new diagnostic ID
287 /// and removing all of its arguments, ranges, and fix-it hints.
288 void Reset(unsigned DiagID = 0) {
289 this->DiagID = DiagID;
293 bool hasStorage() const { return DiagStorage != 0; }
295 friend const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
297 PD.AddTaggedVal(I, DiagnosticsEngine::ak_uint);
301 friend const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
303 PD.AddTaggedVal(I, DiagnosticsEngine::ak_sint);
307 friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
309 PD.AddTaggedVal(reinterpret_cast<intptr_t>(S),
310 DiagnosticsEngine::ak_c_string);
314 friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
321 friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
322 const SourceRange &R) {
323 PD.AddSourceRange(CharSourceRange::getTokenRange(R));
327 friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
328 const CharSourceRange &R) {
329 PD.AddSourceRange(R);
333 friend const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
334 const FixItHint &Hint) {
335 PD.AddFixItHint(Hint);
341 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
342 const PartialDiagnostic &PD) {
347 /// \brief A partial diagnostic along with the source location where this
348 /// diagnostic occurs.
349 typedef std::pair<SourceLocation, PartialDiagnostic> PartialDiagnosticAt;
351 } // end namespace clang