blob: 0402aaa1af3c51bd0bfcc06fdb7c365e1569dcd6 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Diagnostic.cpp - C Language Family Diagnostic Handling -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Diagnostic-related interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Ted Kremenekec55c942010-04-12 19:54:17 +000014#include "clang/Basic/Diagnostic.h"
Chris Lattner43b628c2008-11-19 07:32:16 +000015#include "clang/Basic/IdentifierTable.h"
Ted Kremenekec55c942010-04-12 19:54:17 +000016#include "clang/Basic/PartialDiagnostic.h"
Chris Lattnerf4c83962008-11-19 06:51:40 +000017#include "llvm/ADT/SmallVector.h"
Daniel Dunbar23e47c62009-10-17 18:12:14 +000018#include "llvm/Support/raw_ostream.h"
Ted Kremenek03201fb2011-03-21 18:40:07 +000019#include "llvm/Support/CrashRecoveryContext.h"
20
Reid Spencer5f016e22007-07-11 17:01:13 +000021using namespace clang;
22
David Blaikied6471f72011-09-25 23:23:43 +000023static void DummyArgToStringFn(DiagnosticsEngine::ArgumentKind AK, intptr_t QT,
Chris Lattner3fdf4b02008-11-23 09:21:17 +000024 const char *Modifier, unsigned ML,
25 const char *Argument, unsigned ArgLen,
David Blaikied6471f72011-09-25 23:23:43 +000026 const DiagnosticsEngine::ArgumentValue *PrevArgs,
Chris Lattnerb54d8af2009-10-20 05:25:22 +000027 unsigned NumPrevArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +000028 SmallVectorImpl<char> &Output,
Chandler Carruth0673cb32011-07-11 17:49:21 +000029 void *Cookie,
Chris Lattner5f9e2722011-07-23 10:55:15 +000030 SmallVectorImpl<intptr_t> &QualTypeVals) {
Chris Lattner3fdf4b02008-11-23 09:21:17 +000031 const char *Str = "<can't format argument>";
Chris Lattner22caddc2008-11-23 09:13:29 +000032 Output.append(Str, Str+strlen(Str));
33}
34
35
David Blaikied6471f72011-09-25 23:23:43 +000036DiagnosticsEngine::DiagnosticsEngine(
37 const llvm::IntrusiveRefCntPtr<DiagnosticIDs> &diags,
David Blaikie78ad0b92011-09-25 23:39:51 +000038 DiagnosticConsumer *client, bool ShouldOwnClient)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000039 : Diags(diags), Client(client), OwnsDiagClient(ShouldOwnClient),
40 SourceMgr(0) {
Chris Lattner3fdf4b02008-11-23 09:21:17 +000041 ArgToStringFn = DummyArgToStringFn;
Chris Lattner92dd3862009-02-19 23:53:20 +000042 ArgToStringCookie = 0;
Mike Stump1eb44332009-09-09 15:08:12 +000043
Douglas Gregorcc5888d2010-07-31 00:40:00 +000044 AllExtensionsSilenced = 0;
45 IgnoreAllWarnings = false;
46 WarningsAsErrors = false;
Ted Kremenek1e473cc2011-08-18 01:12:56 +000047 EnableAllWarnings = false;
Douglas Gregorcc5888d2010-07-31 00:40:00 +000048 ErrorsAsFatal = false;
49 SuppressSystemWarnings = false;
50 SuppressAllDiagnostics = false;
51 ShowOverloads = Ovl_All;
52 ExtBehavior = Ext_Ignore;
53
54 ErrorLimit = 0;
55 TemplateBacktraceLimit = 0;
Douglas Gregorcc5888d2010-07-31 00:40:00 +000056
Douglas Gregorabc563f2010-07-19 21:46:24 +000057 Reset();
Reid Spencer5f016e22007-07-11 17:01:13 +000058}
59
David Blaikied6471f72011-09-25 23:23:43 +000060DiagnosticsEngine::~DiagnosticsEngine() {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000061 if (OwnsDiagClient)
62 delete Client;
Chris Lattner182745a2007-12-02 01:09:57 +000063}
64
David Blaikie78ad0b92011-09-25 23:39:51 +000065void DiagnosticsEngine::setClient(DiagnosticConsumer *client,
David Blaikied6471f72011-09-25 23:23:43 +000066 bool ShouldOwnClient) {
Douglas Gregor4f5e21e2011-01-31 22:04:05 +000067 if (OwnsDiagClient && Client)
68 delete Client;
69
70 Client = client;
71 OwnsDiagClient = ShouldOwnClient;
72}
Chris Lattner04ae2df2009-07-12 21:18:45 +000073
David Blaikied6471f72011-09-25 23:23:43 +000074void DiagnosticsEngine::pushMappings(SourceLocation Loc) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +000075 DiagStateOnPushStack.push_back(GetCurDiagState());
Chris Lattner04ae2df2009-07-12 21:18:45 +000076}
77
David Blaikied6471f72011-09-25 23:23:43 +000078bool DiagnosticsEngine::popMappings(SourceLocation Loc) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +000079 if (DiagStateOnPushStack.empty())
Chris Lattner04ae2df2009-07-12 21:18:45 +000080 return false;
81
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +000082 if (DiagStateOnPushStack.back() != GetCurDiagState()) {
83 // State changed at some point between push/pop.
84 PushDiagStatePoint(DiagStateOnPushStack.back(), Loc);
85 }
86 DiagStateOnPushStack.pop_back();
Chris Lattner04ae2df2009-07-12 21:18:45 +000087 return true;
88}
89
David Blaikied6471f72011-09-25 23:23:43 +000090void DiagnosticsEngine::Reset() {
Douglas Gregorabc563f2010-07-19 21:46:24 +000091 ErrorOccurred = false;
92 FatalErrorOccurred = false;
Douglas Gregor85bea972011-07-06 17:40:26 +000093 UnrecoverableErrorOccurred = false;
Douglas Gregorabc563f2010-07-19 21:46:24 +000094
95 NumWarnings = 0;
96 NumErrors = 0;
97 NumErrorsSuppressed = 0;
Argyrios Kyrtzidisc0a575f2011-07-29 01:25:44 +000098 TrapNumErrorsOccurred = 0;
99 TrapNumUnrecoverableErrorsOccurred = 0;
Douglas Gregor85bea972011-07-06 17:40:26 +0000100
Douglas Gregorabc563f2010-07-19 21:46:24 +0000101 CurDiagID = ~0U;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000102 // Set LastDiagLevel to an "unset" state. If we set it to 'Ignored', notes
David Blaikied6471f72011-09-25 23:23:43 +0000103 // using a DiagnosticsEngine associated to a translation unit that follow
104 // diagnostics from a DiagnosticsEngine associated to anoter t.u. will not be
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000105 // displayed.
106 LastDiagLevel = (DiagnosticIDs::Level)-1;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000107 DelayedDiagID = 0;
Argyrios Kyrtzidisdc0a2da2011-03-26 18:58:17 +0000108
109 // Clear state related to #pragma diagnostic.
110 DiagStates.clear();
111 DiagStatePoints.clear();
112 DiagStateOnPushStack.clear();
113
114 // Create a DiagState and DiagStatePoint representing diagnostic changes
115 // through command-line.
116 DiagStates.push_back(DiagState());
117 PushDiagStatePoint(&DiagStates.back(), SourceLocation());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000118}
Reid Spencer5f016e22007-07-11 17:01:13 +0000119
David Blaikied6471f72011-09-25 23:23:43 +0000120void DiagnosticsEngine::SetDelayedDiagnostic(unsigned DiagID, StringRef Arg1,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000121 StringRef Arg2) {
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000122 if (DelayedDiagID)
123 return;
124
125 DelayedDiagID = DiagID;
Douglas Gregor9e2dac92010-03-22 15:47:45 +0000126 DelayedDiagArg1 = Arg1.str();
127 DelayedDiagArg2 = Arg2.str();
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000128}
129
David Blaikied6471f72011-09-25 23:23:43 +0000130void DiagnosticsEngine::ReportDelayed() {
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000131 Report(DelayedDiagID) << DelayedDiagArg1 << DelayedDiagArg2;
132 DelayedDiagID = 0;
133 DelayedDiagArg1.clear();
134 DelayedDiagArg2.clear();
135}
136
David Blaikied6471f72011-09-25 23:23:43 +0000137DiagnosticsEngine::DiagStatePointsTy::iterator
138DiagnosticsEngine::GetDiagStatePointForLoc(SourceLocation L) const {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000139 assert(!DiagStatePoints.empty());
140 assert(DiagStatePoints.front().Loc.isInvalid() &&
141 "Should have created a DiagStatePoint for command-line");
142
143 FullSourceLoc Loc(L, *SourceMgr);
144 if (Loc.isInvalid())
145 return DiagStatePoints.end() - 1;
146
147 DiagStatePointsTy::iterator Pos = DiagStatePoints.end();
148 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
149 if (LastStateChangePos.isValid() &&
150 Loc.isBeforeInTranslationUnitThan(LastStateChangePos))
151 Pos = std::upper_bound(DiagStatePoints.begin(), DiagStatePoints.end(),
152 DiagStatePoint(0, Loc));
153 --Pos;
154 return Pos;
155}
156
157/// \brief This allows the client to specify that certain
158/// warnings are ignored. Notes can never be mapped, errors can only be
159/// mapped to fatal, and WARNINGs and EXTENSIONs can be mapped arbitrarily.
160///
161/// \param The source location that this change of diagnostic state should
162/// take affect. It can be null if we are setting the latest state.
David Blaikied6471f72011-09-25 23:23:43 +0000163void DiagnosticsEngine::setDiagnosticMapping(diag::kind Diag, diag::Mapping Map,
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000164 SourceLocation L) {
165 assert(Diag < diag::DIAG_UPPER_LIMIT &&
166 "Can only map builtin diagnostics");
167 assert((Diags->isBuiltinWarningOrExtension(Diag) ||
168 (Map == diag::MAP_FATAL || Map == diag::MAP_ERROR)) &&
169 "Cannot map errors into warnings!");
170 assert(!DiagStatePoints.empty());
171
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +0000172 bool isPragma = L.isValid();
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000173 FullSourceLoc Loc(L, *SourceMgr);
174 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000175 DiagnosticMappingInfo MappingInfo = DiagnosticMappingInfo::MakeInfo(
176 Map, /*IsUser=*/true, isPragma);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000177
178 // Common case; setting all the diagnostics of a group in one place.
179 if (Loc.isInvalid() || Loc == LastStateChangePos) {
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000180 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000181 return;
182 }
183
184 // Another common case; modifying diagnostic state in a source location
185 // after the previous one.
186 if ((Loc.isValid() && LastStateChangePos.isInvalid()) ||
187 LastStateChangePos.isBeforeInTranslationUnitThan(Loc)) {
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000188 // A diagnostic pragma occurred, create a new DiagState initialized with
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000189 // the current one and a new DiagStatePoint to record at which location
190 // the new state became active.
191 DiagStates.push_back(*GetCurDiagState());
192 PushDiagStatePoint(&DiagStates.back(), Loc);
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000193 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000194 return;
195 }
196
197 // We allow setting the diagnostic state in random source order for
198 // completeness but it should not be actually happening in normal practice.
199
200 DiagStatePointsTy::iterator Pos = GetDiagStatePointForLoc(Loc);
201 assert(Pos != DiagStatePoints.end());
202
203 // Update all diagnostic states that are active after the given location.
204 for (DiagStatePointsTy::iterator
205 I = Pos+1, E = DiagStatePoints.end(); I != E; ++I) {
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000206 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000207 }
208
209 // If the location corresponds to an existing point, just update its state.
210 if (Pos->Loc == Loc) {
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000211 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000212 return;
213 }
214
215 // Create a new state/point and fit it into the vector of DiagStatePoints
216 // so that the vector is always ordered according to location.
217 Pos->Loc.isBeforeInTranslationUnitThan(Loc);
218 DiagStates.push_back(*Pos->State);
219 DiagState *NewState = &DiagStates.back();
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000220 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000221 DiagStatePoints.insert(Pos+1, DiagStatePoint(NewState,
222 FullSourceLoc(Loc, *SourceMgr)));
223}
224
Daniel Dunbar3f839462011-09-29 01:47:16 +0000225bool DiagnosticsEngine::setDiagnosticGroupMapping(
226 StringRef Group, diag::Mapping Map, SourceLocation Loc)
227{
228 // Get the diagnostics in this group.
229 llvm::SmallVector<diag::kind, 8> GroupDiags;
230 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
231 return true;
232
233 // Set the mapping.
234 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i)
235 setDiagnosticMapping(GroupDiags[i], Map, Loc);
236
237 return false;
238}
239
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000240bool DiagnosticsEngine::setDiagnosticGroupWarningAsError(StringRef Group,
241 bool Enabled) {
242 diag::Mapping Map = Enabled ? diag::MAP_ERROR : diag::MAP_WARNING_NO_WERROR;
243 return setDiagnosticGroupMapping(Group, Map);
244}
245
246bool DiagnosticsEngine::setDiagnosticGroupErrorAsFatal(StringRef Group,
247 bool Enabled) {
248 diag::Mapping Map = Enabled ? diag::MAP_FATAL : diag::MAP_ERROR_NO_WFATAL;
249 return setDiagnosticGroupMapping(Group, Map);
250}
251
David Blaikied6471f72011-09-25 23:23:43 +0000252void DiagnosticsEngine::Report(const StoredDiagnostic &storedDiag) {
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000253 assert(CurDiagID == ~0U && "Multiple diagnostics in flight at once!");
254
255 CurDiagLoc = storedDiag.getLocation();
256 CurDiagID = storedDiag.getID();
257 NumDiagArgs = 0;
258
259 NumDiagRanges = storedDiag.range_size();
260 assert(NumDiagRanges < sizeof(DiagRanges)/sizeof(DiagRanges[0]) &&
261 "Too many arguments to diagnostic!");
262 unsigned i = 0;
263 for (StoredDiagnostic::range_iterator
264 RI = storedDiag.range_begin(),
265 RE = storedDiag.range_end(); RI != RE; ++RI)
266 DiagRanges[i++] = *RI;
267
268 NumFixItHints = storedDiag.fixit_size();
David Blaikied6471f72011-09-25 23:23:43 +0000269 assert(NumFixItHints < DiagnosticsEngine::MaxFixItHints &&
270 "Too many fix-it hints!");
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000271 i = 0;
272 for (StoredDiagnostic::fixit_iterator
273 FI = storedDiag.fixit_begin(),
274 FE = storedDiag.fixit_end(); FI != FE; ++FI)
275 FixItHints[i++] = *FI;
276
David Blaikie78ad0b92011-09-25 23:39:51 +0000277 assert(Client && "DiagnosticConsumer not set!");
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000278 Level DiagLevel = storedDiag.getLevel();
David Blaikie40847cf2011-09-26 01:18:08 +0000279 Diagnostic Info(this, storedDiag.getMessage());
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000280 Client->HandleDiagnostic(DiagLevel, Info);
281 if (Client->IncludeInDiagnosticCounts()) {
David Blaikied6471f72011-09-25 23:23:43 +0000282 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000283 ++NumWarnings;
284 }
285
286 CurDiagID = ~0U;
287}
288
Douglas Gregorb5350412010-10-13 17:22:14 +0000289void DiagnosticBuilder::FlushCounts() {
290 DiagObj->NumDiagArgs = NumArgs;
291 DiagObj->NumDiagRanges = NumRanges;
292 DiagObj->NumFixItHints = NumFixItHints;
293}
294
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000295bool DiagnosticBuilder::Emit() {
296 // If DiagObj is null, then its soul was stolen by the copy ctor
297 // or the user called Emit().
298 if (DiagObj == 0) return false;
299
300 // When emitting diagnostics, we set the final argument count into
David Blaikied6471f72011-09-25 23:23:43 +0000301 // the DiagnosticsEngine object.
Douglas Gregorb5350412010-10-13 17:22:14 +0000302 FlushCounts();
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000303
304 // Process the diagnostic, sending the accumulated information to the
David Blaikie78ad0b92011-09-25 23:39:51 +0000305 // DiagnosticConsumer.
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000306 bool Emitted = DiagObj->ProcessDiag();
307
308 // Clear out the current diagnostic object.
Douglas Gregor9e2dac92010-03-22 15:47:45 +0000309 unsigned DiagID = DiagObj->CurDiagID;
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000310 DiagObj->Clear();
311
312 // If there was a delayed diagnostic, emit it now.
Douglas Gregor9e2dac92010-03-22 15:47:45 +0000313 if (DiagObj->DelayedDiagID && DiagObj->DelayedDiagID != DiagID)
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000314 DiagObj->ReportDelayed();
315
316 // This diagnostic is dead.
317 DiagObj = 0;
318
319 return Emitted;
320}
321
Nico Weber7bfaaae2008-08-10 19:59:06 +0000322
David Blaikie78ad0b92011-09-25 23:39:51 +0000323DiagnosticConsumer::~DiagnosticConsumer() {}
Nico Weber7bfaaae2008-08-10 19:59:06 +0000324
David Blaikie78ad0b92011-09-25 23:39:51 +0000325void DiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
David Blaikie40847cf2011-09-26 01:18:08 +0000326 const Diagnostic &Info) {
Argyrios Kyrtzidisf2224d82010-11-18 20:06:46 +0000327 if (!IncludeInDiagnosticCounts())
328 return;
329
David Blaikied6471f72011-09-25 23:23:43 +0000330 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidisf2224d82010-11-18 20:06:46 +0000331 ++NumWarnings;
David Blaikied6471f72011-09-25 23:23:43 +0000332 else if (DiagLevel >= DiagnosticsEngine::Error)
Argyrios Kyrtzidisf2224d82010-11-18 20:06:46 +0000333 ++NumErrors;
334}
Chris Lattnerf4c83962008-11-19 06:51:40 +0000335
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000336/// ModifierIs - Return true if the specified modifier matches specified string.
337template <std::size_t StrLen>
338static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
339 const char (&Str)[StrLen]) {
340 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
341}
342
John McCall909c1822010-01-14 20:11:39 +0000343/// ScanForward - Scans forward, looking for the given character, skipping
344/// nested clauses and escaped characters.
345static const char *ScanFormat(const char *I, const char *E, char Target) {
346 unsigned Depth = 0;
347
348 for ( ; I != E; ++I) {
349 if (Depth == 0 && *I == Target) return I;
350 if (Depth != 0 && *I == '}') Depth--;
351
352 if (*I == '%') {
353 I++;
354 if (I == E) break;
355
356 // Escaped characters get implicitly skipped here.
357
358 // Format specifier.
359 if (!isdigit(*I) && !ispunct(*I)) {
360 for (I++; I != E && !isdigit(*I) && *I != '{'; I++) ;
361 if (I == E) break;
362 if (*I == '{')
363 Depth++;
364 }
365 }
366 }
367 return E;
368}
369
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000370/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
371/// like this: %select{foo|bar|baz}2. This means that the integer argument
372/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
373/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
374/// This is very useful for certain classes of variant diagnostics.
David Blaikie40847cf2011-09-26 01:18:08 +0000375static void HandleSelectModifier(const Diagnostic &DInfo, unsigned ValNo,
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000376 const char *Argument, unsigned ArgumentLen,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000377 SmallVectorImpl<char> &OutStr) {
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000378 const char *ArgumentEnd = Argument+ArgumentLen;
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000380 // Skip over 'ValNo' |'s.
381 while (ValNo) {
John McCall909c1822010-01-14 20:11:39 +0000382 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000383 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
384 " larger than the number of options in the diagnostic string!");
385 Argument = NextVal+1; // Skip this string.
386 --ValNo;
387 }
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000389 // Get the end of the value. This is either the } or the |.
John McCall909c1822010-01-14 20:11:39 +0000390 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
John McCall9f286142010-01-13 23:58:20 +0000391
392 // Recursively format the result of the select clause into the output string.
393 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000394}
395
396/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
397/// letter 's' to the string if the value is not 1. This is used in cases like
398/// this: "you idiot, you have %4 parameter%s4!".
399static void HandleIntegerSModifier(unsigned ValNo,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000400 SmallVectorImpl<char> &OutStr) {
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000401 if (ValNo != 1)
402 OutStr.push_back('s');
403}
404
John McCall3be16b72010-01-14 00:50:32 +0000405/// HandleOrdinalModifier - Handle the integer 'ord' modifier. This
406/// prints the ordinal form of the given integer, with 1 corresponding
407/// to the first ordinal. Currently this is hard-coded to use the
408/// English form.
409static void HandleOrdinalModifier(unsigned ValNo,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000410 SmallVectorImpl<char> &OutStr) {
John McCall3be16b72010-01-14 00:50:32 +0000411 assert(ValNo != 0 && "ValNo must be strictly positive!");
412
413 llvm::raw_svector_ostream Out(OutStr);
414
415 // We could use text forms for the first N ordinals, but the numeric
416 // forms are actually nicer in diagnostics because they stand out.
417 Out << ValNo;
418
419 // It is critically important that we do this perfectly for
420 // user-written sequences with over 100 elements.
421 switch (ValNo % 100) {
422 case 11:
423 case 12:
424 case 13:
425 Out << "th"; return;
426 default:
427 switch (ValNo % 10) {
428 case 1: Out << "st"; return;
429 case 2: Out << "nd"; return;
430 case 3: Out << "rd"; return;
431 default: Out << "th"; return;
432 }
433 }
434}
435
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000436
Sebastian Redle4c452c2008-11-22 13:44:36 +0000437/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000438static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000439 // Programming 101: Parse a decimal number :-)
440 unsigned Val = 0;
441 while (Start != End && *Start >= '0' && *Start <= '9') {
442 Val *= 10;
443 Val += *Start - '0';
444 ++Start;
445 }
446 return Val;
447}
448
449/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000450static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000451 if (*Start != '[') {
452 unsigned Ref = PluralNumber(Start, End);
453 return Ref == Val;
454 }
455
456 ++Start;
457 unsigned Low = PluralNumber(Start, End);
458 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
459 ++Start;
460 unsigned High = PluralNumber(Start, End);
461 assert(*Start == ']' && "Bad plural expression syntax: expected )");
462 ++Start;
463 return Low <= Val && Val <= High;
464}
465
466/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000467static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000468 // Empty condition?
469 if (*Start == ':')
470 return true;
471
472 while (1) {
473 char C = *Start;
474 if (C == '%') {
475 // Modulo expression
476 ++Start;
477 unsigned Arg = PluralNumber(Start, End);
478 assert(*Start == '=' && "Bad plural expression syntax: expected =");
479 ++Start;
480 unsigned ValMod = ValNo % Arg;
481 if (TestPluralRange(ValMod, Start, End))
482 return true;
483 } else {
Sebastian Redle2065322008-11-27 07:28:14 +0000484 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redle4c452c2008-11-22 13:44:36 +0000485 "Bad plural expression syntax: unexpected character");
486 // Range expression
487 if (TestPluralRange(ValNo, Start, End))
488 return true;
489 }
490
491 // Scan for next or-expr part.
492 Start = std::find(Start, End, ',');
Mike Stump1eb44332009-09-09 15:08:12 +0000493 if (Start == End)
Sebastian Redle4c452c2008-11-22 13:44:36 +0000494 break;
495 ++Start;
496 }
497 return false;
498}
499
500/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
501/// for complex plural forms, or in languages where all plurals are complex.
502/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
503/// conditions that are tested in order, the form corresponding to the first
504/// that applies being emitted. The empty condition is always true, making the
505/// last form a default case.
506/// Conditions are simple boolean expressions, where n is the number argument.
507/// Here are the rules.
508/// condition := expression | empty
509/// empty := -> always true
510/// expression := numeric [',' expression] -> logical or
511/// numeric := range -> true if n in range
512/// | '%' number '=' range -> true if n % number in range
513/// range := number
514/// | '[' number ',' number ']' -> ranges are inclusive both ends
515///
516/// Here are some examples from the GNU gettext manual written in this form:
517/// English:
518/// {1:form0|:form1}
519/// Latvian:
520/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
521/// Gaeilge:
522/// {1:form0|2:form1|:form2}
523/// Romanian:
524/// {1:form0|0,%100=[1,19]:form1|:form2}
525/// Lithuanian:
526/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
527/// Russian (requires repeated form):
528/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
529/// Slovak
530/// {1:form0|[2,4]:form1|:form2}
531/// Polish (requires repeated form):
532/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
David Blaikie40847cf2011-09-26 01:18:08 +0000533static void HandlePluralModifier(const Diagnostic &DInfo, unsigned ValNo,
Sebastian Redle4c452c2008-11-22 13:44:36 +0000534 const char *Argument, unsigned ArgumentLen,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000535 SmallVectorImpl<char> &OutStr) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000536 const char *ArgumentEnd = Argument + ArgumentLen;
537 while (1) {
538 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
539 const char *ExprEnd = Argument;
540 while (*ExprEnd != ':') {
541 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
542 ++ExprEnd;
543 }
544 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
545 Argument = ExprEnd + 1;
John McCall909c1822010-01-14 20:11:39 +0000546 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
John McCalle53a44b2010-10-14 01:55:31 +0000547
548 // Recursively format the result of the plural clause into the
549 // output string.
550 DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000551 return;
552 }
John McCall909c1822010-01-14 20:11:39 +0000553 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
Sebastian Redle4c452c2008-11-22 13:44:36 +0000554 }
555}
556
557
Chris Lattnerf4c83962008-11-19 06:51:40 +0000558/// FormatDiagnostic - Format this diagnostic into a string, substituting the
559/// formal arguments into the %0 slots. The result is appended onto the Str
560/// array.
David Blaikie40847cf2011-09-26 01:18:08 +0000561void Diagnostic::
Chris Lattner5f9e2722011-07-23 10:55:15 +0000562FormatDiagnostic(SmallVectorImpl<char> &OutStr) const {
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000563 if (!StoredDiagMessage.empty()) {
564 OutStr.append(StoredDiagMessage.begin(), StoredDiagMessage.end());
565 return;
566 }
567
Chris Lattner5f9e2722011-07-23 10:55:15 +0000568 StringRef Diag =
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000569 getDiags()->getDiagnosticIDs()->getDescription(getID());
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000571 FormatDiagnostic(Diag.begin(), Diag.end(), OutStr);
John McCall9f286142010-01-13 23:58:20 +0000572}
573
David Blaikie40847cf2011-09-26 01:18:08 +0000574void Diagnostic::
John McCall9f286142010-01-13 23:58:20 +0000575FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000576 SmallVectorImpl<char> &OutStr) const {
John McCall9f286142010-01-13 23:58:20 +0000577
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000578 /// FormattedArgs - Keep track of all of the arguments formatted by
579 /// ConvertArgToString and pass them into subsequent calls to
580 /// ConvertArgToString, allowing the implementation to avoid redundancies in
581 /// obvious cases.
David Blaikied6471f72011-09-25 23:23:43 +0000582 SmallVector<DiagnosticsEngine::ArgumentValue, 8> FormattedArgs;
Chandler Carruth0673cb32011-07-11 17:49:21 +0000583
584 /// QualTypeVals - Pass a vector of arrays so that QualType names can be
585 /// compared to see if more information is needed to be printed.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000586 SmallVector<intptr_t, 2> QualTypeVals;
Chandler Carruth0673cb32011-07-11 17:49:21 +0000587 for (unsigned i = 0, e = getNumArgs(); i < e; ++i)
David Blaikied6471f72011-09-25 23:23:43 +0000588 if (getArgKind(i) == DiagnosticsEngine::ak_qualtype)
Chandler Carruth0673cb32011-07-11 17:49:21 +0000589 QualTypeVals.push_back(getRawArg(i));
590
Chris Lattnerf4c83962008-11-19 06:51:40 +0000591 while (DiagStr != DiagEnd) {
592 if (DiagStr[0] != '%') {
593 // Append non-%0 substrings to Str if we have one.
594 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
595 OutStr.append(DiagStr, StrEnd);
596 DiagStr = StrEnd;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000597 continue;
John McCall909c1822010-01-14 20:11:39 +0000598 } else if (ispunct(DiagStr[1])) {
599 OutStr.push_back(DiagStr[1]); // %% -> %.
Chris Lattnerf4c83962008-11-19 06:51:40 +0000600 DiagStr += 2;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000601 continue;
602 }
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000604 // Skip the %.
605 ++DiagStr;
Mike Stump1eb44332009-09-09 15:08:12 +0000606
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000607 // This must be a placeholder for a diagnostic argument. The format for a
608 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
609 // The digit is a number from 0-9 indicating which argument this comes from.
610 // The modifier is a string of digits from the set [-a-z]+, arguments is a
611 // brace enclosed string.
612 const char *Modifier = 0, *Argument = 0;
613 unsigned ModifierLen = 0, ArgumentLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000615 // Check to see if we have a modifier. If so eat it.
616 if (!isdigit(DiagStr[0])) {
617 Modifier = DiagStr;
618 while (DiagStr[0] == '-' ||
619 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
620 ++DiagStr;
621 ModifierLen = DiagStr-Modifier;
Chris Lattnerf4c83962008-11-19 06:51:40 +0000622
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000623 // If we have an argument, get it next.
624 if (DiagStr[0] == '{') {
625 ++DiagStr; // Skip {.
626 Argument = DiagStr;
Mike Stump1eb44332009-09-09 15:08:12 +0000627
John McCall909c1822010-01-14 20:11:39 +0000628 DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
629 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000630 ArgumentLen = DiagStr-Argument;
631 ++DiagStr; // Skip }.
Chris Lattnerf4c83962008-11-19 06:51:40 +0000632 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000633 }
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000635 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner22caddc2008-11-23 09:13:29 +0000636 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000637
David Blaikied6471f72011-09-25 23:23:43 +0000638 DiagnosticsEngine::ArgumentKind Kind = getArgKind(ArgNo);
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000639
640 switch (Kind) {
Chris Lattner08631c52008-11-23 21:45:46 +0000641 // ---- STRINGS ----
David Blaikied6471f72011-09-25 23:23:43 +0000642 case DiagnosticsEngine::ak_std_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000643 const std::string &S = getArgStdStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000644 assert(ModifierLen == 0 && "No modifiers for strings yet");
645 OutStr.append(S.begin(), S.end());
646 break;
647 }
David Blaikied6471f72011-09-25 23:23:43 +0000648 case DiagnosticsEngine::ak_c_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000649 const char *S = getArgCStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000650 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbare46e3542009-04-20 06:13:16 +0000651
652 // Don't crash if get passed a null pointer by accident.
653 if (!S)
654 S = "(null)";
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000656 OutStr.append(S, S + strlen(S));
657 break;
658 }
Chris Lattner08631c52008-11-23 21:45:46 +0000659 // ---- INTEGERS ----
David Blaikied6471f72011-09-25 23:23:43 +0000660 case DiagnosticsEngine::ak_sint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000661 int Val = getArgSInt(ArgNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000663 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCalle53a44b2010-10-14 01:55:31 +0000664 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen,
665 OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000666 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
667 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000668 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCalle53a44b2010-10-14 01:55:31 +0000669 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
670 OutStr);
John McCall3be16b72010-01-14 00:50:32 +0000671 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
672 HandleOrdinalModifier((unsigned)Val, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000673 } else {
674 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbar23e47c62009-10-17 18:12:14 +0000675 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner30bc9652008-11-19 07:22:31 +0000676 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000677 break;
678 }
David Blaikied6471f72011-09-25 23:23:43 +0000679 case DiagnosticsEngine::ak_uint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000680 unsigned Val = getArgUInt(ArgNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000681
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000682 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCall9f286142010-01-13 23:58:20 +0000683 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000684 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
685 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000686 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCalle53a44b2010-10-14 01:55:31 +0000687 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
688 OutStr);
John McCall3be16b72010-01-14 00:50:32 +0000689 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
690 HandleOrdinalModifier(Val, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000691 } else {
692 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbar23e47c62009-10-17 18:12:14 +0000693 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner30bc9652008-11-19 07:22:31 +0000694 }
Chris Lattner22caddc2008-11-23 09:13:29 +0000695 break;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000696 }
Chris Lattner08631c52008-11-23 21:45:46 +0000697 // ---- NAMES and TYPES ----
David Blaikied6471f72011-09-25 23:23:43 +0000698 case DiagnosticsEngine::ak_identifierinfo: {
Chris Lattner08631c52008-11-23 21:45:46 +0000699 const IdentifierInfo *II = getArgIdentifier(ArgNo);
700 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbare46e3542009-04-20 06:13:16 +0000701
702 // Don't crash if get passed a null pointer by accident.
703 if (!II) {
704 const char *S = "(null)";
705 OutStr.append(S, S + strlen(S));
706 continue;
707 }
708
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000709 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
Chris Lattner08631c52008-11-23 21:45:46 +0000710 break;
711 }
David Blaikied6471f72011-09-25 23:23:43 +0000712 case DiagnosticsEngine::ak_qualtype:
713 case DiagnosticsEngine::ak_declarationname:
714 case DiagnosticsEngine::ak_nameddecl:
715 case DiagnosticsEngine::ak_nestednamespec:
716 case DiagnosticsEngine::ak_declcontext:
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000717 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000718 Modifier, ModifierLen,
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000719 Argument, ArgumentLen,
720 FormattedArgs.data(), FormattedArgs.size(),
Chandler Carruth0673cb32011-07-11 17:49:21 +0000721 OutStr, QualTypeVals);
Chris Lattner22caddc2008-11-23 09:13:29 +0000722 break;
Nico Weber7bfaaae2008-08-10 19:59:06 +0000723 }
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000724
725 // Remember this argument info for subsequent formatting operations. Turn
726 // std::strings into a null terminated string to make it be the same case as
727 // all the other ones.
David Blaikied6471f72011-09-25 23:23:43 +0000728 if (Kind != DiagnosticsEngine::ak_std_string)
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000729 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
730 else
David Blaikied6471f72011-09-25 23:23:43 +0000731 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_c_string,
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000732 (intptr_t)getArgStdStr(ArgNo).c_str()));
733
Nico Weber7bfaaae2008-08-10 19:59:06 +0000734 }
Nico Weber7bfaaae2008-08-10 19:59:06 +0000735}
Ted Kremenekcabe6682009-01-23 20:28:53 +0000736
Douglas Gregora88084b2010-02-18 18:08:43 +0000737StoredDiagnostic::StoredDiagnostic() { }
738
David Blaikied6471f72011-09-25 23:23:43 +0000739StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000740 StringRef Message)
Benjamin Kramera6a32e22010-11-19 17:36:51 +0000741 : ID(ID), Level(Level), Loc(), Message(Message) { }
Douglas Gregora88084b2010-02-18 18:08:43 +0000742
David Blaikied6471f72011-09-25 23:23:43 +0000743StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level,
David Blaikie40847cf2011-09-26 01:18:08 +0000744 const Diagnostic &Info)
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000745 : ID(Info.getID()), Level(Level)
746{
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000747 assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
748 "Valid source location without setting a source manager for diagnostic");
749 if (Info.getLocation().isValid())
750 Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
Douglas Gregora88084b2010-02-18 18:08:43 +0000751 llvm::SmallString<64> Message;
752 Info.FormatDiagnostic(Message);
753 this->Message.assign(Message.begin(), Message.end());
754
755 Ranges.reserve(Info.getNumRanges());
756 for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I)
757 Ranges.push_back(Info.getRange(I));
758
Douglas Gregor849b2432010-03-31 17:46:05 +0000759 FixIts.reserve(Info.getNumFixItHints());
760 for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I)
761 FixIts.push_back(Info.getFixItHint(I));
Douglas Gregora88084b2010-02-18 18:08:43 +0000762}
763
David Blaikied6471f72011-09-25 23:23:43 +0000764StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000765 StringRef Message, FullSourceLoc Loc,
Chris Lattner2d3ba4f2011-07-23 17:14:25 +0000766 ArrayRef<CharSourceRange> Ranges,
767 ArrayRef<FixItHint> Fixits)
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000768 : ID(ID), Level(Level), Loc(Loc), Message(Message)
769{
770 this->Ranges.assign(Ranges.begin(), Ranges.end());
771 this->FixIts.assign(FixIts.begin(), FixIts.end());
772}
773
Douglas Gregora88084b2010-02-18 18:08:43 +0000774StoredDiagnostic::~StoredDiagnostic() { }
775
Ted Kremenekcabe6682009-01-23 20:28:53 +0000776/// IncludeInDiagnosticCounts - This method (whose default implementation
777/// returns true) indicates whether the diagnostics handled by this
David Blaikie78ad0b92011-09-25 23:39:51 +0000778/// DiagnosticConsumer should be included in the number of diagnostics
David Blaikied6471f72011-09-25 23:23:43 +0000779/// reported by DiagnosticsEngine.
David Blaikie78ad0b92011-09-25 23:39:51 +0000780bool DiagnosticConsumer::IncludeInDiagnosticCounts() const { return true; }
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000781
782PartialDiagnostic::StorageAllocator::StorageAllocator() {
783 for (unsigned I = 0; I != NumCached; ++I)
784 FreeList[I] = Cached + I;
785 NumFreeListEntries = NumCached;
786}
787
788PartialDiagnostic::StorageAllocator::~StorageAllocator() {
Ted Kremenek03201fb2011-03-21 18:40:07 +0000789 // Don't assert if we are in a CrashRecovery context, as this
790 // invariant may be invalidated during a crash.
Ted Kremenekcd1eecf2011-03-21 18:40:10 +0000791 assert((NumFreeListEntries == NumCached || llvm::CrashRecoveryContext::isRecoveringFromCrash()) && "A partial is on the lamb");
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000792}