]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/Basic/PartialDiagnostic.h
Merge libpcap-1.1.1.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / Basic / PartialDiagnostic.h
1 //===--- PartialDiagnostic.h - Diagnostic "closures" ------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file implements a partial diagnostic that can be emitted anwyhere
11 //  in a DiagnosticBuilder stream.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CLANG_PARTIALDIAGNOSTIC_H
16 #define LLVM_CLANG_PARTIALDIAGNOSTIC_H
17
18 #include "clang/Basic/Diagnostic.h"
19 #include "clang/Basic/SourceLocation.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/System/DataTypes.h"
22 #include <cassert>
23
24 namespace clang {
25
26 class PartialDiagnostic {
27 public:
28   struct Storage {
29     Storage() : NumDiagArgs(0), NumDiagRanges(0), NumFixItHints(0) { }
30
31     enum {
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 = 10
37     };
38   
39     /// NumDiagArgs - This contains the number of entries in Arguments.
40     unsigned char NumDiagArgs;
41   
42     /// NumDiagRanges - This is the number of ranges in the DiagRanges array.
43     unsigned char NumDiagRanges;
44
45     /// \brief The number of code modifications hints in the
46     /// FixItHints array.
47     unsigned char NumFixItHints;
48     
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];
53   
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];
59   
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];
63     
64     enum { MaxFixItHints = 3 };
65     
66     /// FixItHints - If valid, provides a hint with some code
67     /// to insert, remove, or modify at a particular position.
68     FixItHint FixItHints[MaxFixItHints];    
69   };
70
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;
78     
79   public:
80     StorageAllocator();
81     ~StorageAllocator();
82     
83     /// \brief Allocate new storage.
84     Storage *Allocate() {
85       if (NumFreeListEntries == 0)
86         return new Storage;
87       
88       Storage *Result = FreeList[--NumFreeListEntries];
89       Result->NumDiagArgs = 0;
90       Result->NumDiagRanges = 0;
91       Result->NumFixItHints = 0;
92       return Result;
93     }
94     
95     /// \brief Free the given storage object.
96     void Deallocate(Storage *S) {
97       if (S >= Cached && S <= Cached + NumCached) {
98         FreeList[NumFreeListEntries++] = S;
99         return;
100       }
101       
102       delete S;
103     }
104   };
105   
106 private:
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.
110
111   /// DiagID - The diagnostic ID.
112   mutable unsigned DiagID;
113   
114   /// DiagStorage - Storage for args and ranges.
115   mutable Storage *DiagStorage;
116
117   /// \brief Allocator used to allocate storage for this diagnostic.
118   StorageAllocator *Allocator;
119   
120   /// \brief Retrieve storage for this particular diagnostic.
121   Storage *getStorage() const {
122     if (DiagStorage)
123       return DiagStorage;
124     
125     if (Allocator)
126       DiagStorage = Allocator->Allocate();
127     else {
128       assert(Allocator != reinterpret_cast<StorageAllocator *>(~uintptr_t(0)));
129       DiagStorage = new Storage;
130     }
131     return DiagStorage;
132   }
133   
134   void freeStorage() { 
135     if (!DiagStorage)
136       return;
137     
138     if (Allocator)
139       Allocator->Deallocate(DiagStorage);
140     else if (Allocator != reinterpret_cast<StorageAllocator *>(~uintptr_t(0)))
141       delete DiagStorage;
142     DiagStorage = 0;
143   }
144   
145   void AddSourceRange(const CharSourceRange &R) const {
146     if (!DiagStorage)
147       DiagStorage = getStorage();
148
149     assert(DiagStorage->NumDiagRanges < 
150            llvm::array_lengthof(DiagStorage->DiagRanges) &&
151            "Too many arguments to diagnostic!");
152     DiagStorage->DiagRanges[DiagStorage->NumDiagRanges++] = R;
153   }  
154
155   void AddFixItHint(const FixItHint &Hint) const {
156     if (Hint.isNull())
157       return;
158     
159     if (!DiagStorage)
160       DiagStorage = getStorage();
161
162     assert(DiagStorage->NumFixItHints < Storage::MaxFixItHints &&
163            "Too many code modification hints!");
164     DiagStorage->FixItHints[DiagStorage->NumFixItHints++]
165       = Hint;
166   }
167   
168 public:
169   PartialDiagnostic(unsigned DiagID, StorageAllocator &Allocator)
170     : DiagID(DiagID), DiagStorage(0), Allocator(&Allocator) { }
171   
172   PartialDiagnostic(const PartialDiagnostic &Other) 
173     : DiagID(Other.DiagID), DiagStorage(0), Allocator(Other.Allocator)
174   {
175     if (Other.DiagStorage) {
176       DiagStorage = getStorage();
177       *DiagStorage = *Other.DiagStorage;
178     }
179   }
180
181   PartialDiagnostic(const PartialDiagnostic &Other, Storage *DiagStorage) 
182     : DiagID(Other.DiagID), DiagStorage(DiagStorage), 
183       Allocator(reinterpret_cast<StorageAllocator *>(~uintptr_t(0)))
184   {
185     if (Other.DiagStorage)
186       *this->DiagStorage = *Other.DiagStorage;
187   }
188   
189   PartialDiagnostic &operator=(const PartialDiagnostic &Other) {
190     DiagID = Other.DiagID;
191     if (Other.DiagStorage) {
192       if (!DiagStorage)
193         DiagStorage = getStorage();
194       
195       *DiagStorage = *Other.DiagStorage;
196     } else {
197       freeStorage();
198     }
199
200     return *this;
201   }
202
203   ~PartialDiagnostic() {
204     freeStorage();
205   }
206
207   unsigned getDiagID() const { return DiagID; }
208
209   void AddTaggedVal(intptr_t V, Diagnostic::ArgumentKind Kind) const {
210     if (!DiagStorage)
211       DiagStorage = getStorage();
212
213     assert(DiagStorage->NumDiagArgs < Storage::MaxArguments &&
214            "Too many arguments to diagnostic!");
215     DiagStorage->DiagArgumentsKind[DiagStorage->NumDiagArgs] = Kind;
216     DiagStorage->DiagArgumentsVal[DiagStorage->NumDiagArgs++] = V;
217   }
218
219   void Emit(const DiagnosticBuilder &DB) const {
220     if (!DiagStorage)
221       return;
222     
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]);
227     }
228     
229     // Add all ranges.
230     for (unsigned i = 0, e = DiagStorage->NumDiagRanges; i != e; ++i)
231       DB.AddSourceRange(DiagStorage->DiagRanges[i]);
232     
233     // Add all code modification hints
234     for (unsigned i = 0, e = DiagStorage->NumFixItHints; i != e; ++i)
235       DB.AddFixItHint(DiagStorage->FixItHints[i]);
236   }
237   
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;
242     freeStorage();
243   }
244   
245   bool hasStorage() const { return DiagStorage != 0; }
246   
247   friend const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
248                                              unsigned I) {
249     PD.AddTaggedVal(I, Diagnostic::ak_uint);
250     return PD;
251   }
252
253   friend const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
254                                              int I) {
255     PD.AddTaggedVal(I, Diagnostic::ak_sint);
256     return PD;
257   }
258
259   friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
260                                                     const char *S) {
261     PD.AddTaggedVal(reinterpret_cast<intptr_t>(S), Diagnostic::ak_c_string);
262     return PD;
263   }
264
265   friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
266                                                     const SourceRange &R) {
267     PD.AddSourceRange(CharSourceRange::getTokenRange(R));
268     return PD;
269   }
270
271   friend inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
272                                                     const CharSourceRange &R) {
273     PD.AddSourceRange(R);
274     return PD;
275   }
276   
277   friend const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
278                                              const FixItHint &Hint) {
279     PD.AddFixItHint(Hint);
280     return PD;
281   }
282   
283 };
284
285 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
286                                            const PartialDiagnostic &PD) {
287   PD.Emit(DB);
288   return DB;
289 }
290   
291
292 }  // end namespace clang
293 #endif