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/System/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 intepretation depends on exactly
57 /// what sort of argument kind it is.
58 intptr_t DiagArgumentsVal[MaxArguments];
60 /// DiagRanges - The list of ranges added to this diagnostic. It currently
61 /// only support 10 ranges, could easily be extended if needed.
62 CharSourceRange DiagRanges[10];
64 enum { MaxFixItHints = 3 };
66 /// FixItHints - If valid, provides a hint with some code
67 /// to insert, remove, or modify at a particular position.
68 FixItHint FixItHints[MaxFixItHints];
71 /// \brief An allocator for Storage objects, which uses a small cache to
72 /// objects, used to reduce malloc()/free() traffic for partial diagnostics.
73 class StorageAllocator {
74 static const unsigned NumCached = 4;
75 Storage Cached[NumCached];
76 Storage *FreeList[NumCached];
77 unsigned NumFreeListEntries;
83 /// \brief Allocate new storage.
85 if (NumFreeListEntries == 0)
88 Storage *Result = FreeList[--NumFreeListEntries];
89 Result->NumDiagArgs = 0;
90 Result->NumDiagRanges = 0;
91 Result->NumFixItHints = 0;
95 /// \brief Free the given storage object.
96 void Deallocate(Storage *S) {
97 if (S >= Cached && S <= Cached + NumCached) {
98 FreeList[NumFreeListEntries++] = S;
107 // NOTE: Sema assumes that PartialDiagnostic is location-invariant
108 // in the sense that its bits can be safely memcpy'ed and destructed
109 // in the new location.
111 /// DiagID - The diagnostic ID.
112 mutable unsigned DiagID;
114 /// DiagStorage - Storage for args and ranges.
115 mutable Storage *DiagStorage;
117 /// \brief Allocator used to allocate storage for this diagnostic.
118 StorageAllocator *Allocator;
120 /// \brief Retrieve storage for this particular diagnostic.
121 Storage *getStorage() const {
126 DiagStorage = Allocator->Allocate();
128 assert(Allocator != reinterpret_cast<StorageAllocator *>(~uintptr_t(0)));
129 DiagStorage = new Storage;
139 Allocator->Deallocate(DiagStorage);
140 else if (Allocator != reinterpret_cast<StorageAllocator *>(~uintptr_t(0)))
145 void AddSourceRange(const CharSourceRange &R) const {
147 DiagStorage = getStorage();
149 assert(DiagStorage->NumDiagRanges <
150 llvm::array_lengthof(DiagStorage->DiagRanges) &&
151 "Too many arguments to diagnostic!");
152 DiagStorage->DiagRanges[DiagStorage->NumDiagRanges++] = R;
155 void AddFixItHint(const FixItHint &Hint) const {
160 DiagStorage = getStorage();
162 assert(DiagStorage->NumFixItHints < Storage::MaxFixItHints &&
163 "Too many code modification hints!");
164 DiagStorage->FixItHints[DiagStorage->NumFixItHints++]
169 PartialDiagnostic(unsigned DiagID, StorageAllocator &Allocator)
170 : DiagID(DiagID), DiagStorage(0), Allocator(&Allocator) { }
172 PartialDiagnostic(const PartialDiagnostic &Other)
173 : DiagID(Other.DiagID), DiagStorage(0), Allocator(Other.Allocator)
175 if (Other.DiagStorage) {
176 DiagStorage = getStorage();
177 *DiagStorage = *Other.DiagStorage;
181 PartialDiagnostic(const PartialDiagnostic &Other, Storage *DiagStorage)
182 : DiagID(Other.DiagID), DiagStorage(DiagStorage),
183 Allocator(reinterpret_cast<StorageAllocator *>(~uintptr_t(0)))
185 if (Other.DiagStorage)
186 *this->DiagStorage = *Other.DiagStorage;
189 PartialDiagnostic &operator=(const PartialDiagnostic &Other) {
190 DiagID = Other.DiagID;
191 if (Other.DiagStorage) {
193 DiagStorage = getStorage();
195 *DiagStorage = *Other.DiagStorage;
203 ~PartialDiagnostic() {
207 unsigned getDiagID() const { return DiagID; }
209 void AddTaggedVal(intptr_t V, Diagnostic::ArgumentKind Kind) const {
211 DiagStorage = getStorage();
213 assert(DiagStorage->NumDiagArgs < Storage::MaxArguments &&
214 "Too many arguments to diagnostic!");
215 DiagStorage->DiagArgumentsKind[DiagStorage->NumDiagArgs] = Kind;
216 DiagStorage->DiagArgumentsVal[DiagStorage->NumDiagArgs++] = V;
219 void Emit(const DiagnosticBuilder &DB) const {
223 // Add all arguments.
224 for (unsigned i = 0, e = DiagStorage->NumDiagArgs; i != e; ++i) {
225 DB.AddTaggedVal(DiagStorage->DiagArgumentsVal[i],
226 (Diagnostic::ArgumentKind)DiagStorage->DiagArgumentsKind[i]);
230 for (unsigned i = 0, e = DiagStorage->NumDiagRanges; i != e; ++i)
231 DB.AddSourceRange(DiagStorage->DiagRanges[i]);
233 // Add all code modification hints
234 for (unsigned i = 0, e = DiagStorage->NumFixItHints; i != e; ++i)
235 DB.AddFixItHint(DiagStorage->FixItHints[i]);
238 /// \brief Clear out this partial diagnostic, giving it a new diagnostic ID
239 /// and removing all of its arguments, ranges, and fix-it hints.
240 void Reset(unsigned DiagID = 0) {
241 this->DiagID = DiagID;
245 bool hasStorage() const { return DiagStorage != 0; }
247 friend const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
249 PD.AddTaggedVal(I, Diagnostic::ak_uint);
253 friend const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
255 PD.AddTaggedVal(I, Diagnostic::ak_sint);
259 friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
261 PD.AddTaggedVal(reinterpret_cast<intptr_t>(S), Diagnostic::ak_c_string);
265 friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
266 const SourceRange &R) {
267 PD.AddSourceRange(CharSourceRange::getTokenRange(R));
271 friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
272 const CharSourceRange &R) {
273 PD.AddSourceRange(R);
277 friend const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
278 const FixItHint &Hint) {
279 PD.AddFixItHint(Hint);
285 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
286 const PartialDiagnostic &PD) {
292 } // end namespace clang