blob: 48dad6f2e0ed4d729f41cf102c79cb3d93c01602 [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
Chris Lattner3fdf4b02008-11-23 09:21:17 +000023static void DummyArgToStringFn(Diagnostic::ArgumentKind AK, intptr_t QT,
24 const char *Modifier, unsigned ML,
25 const char *Argument, unsigned ArgLen,
Chris Lattnerb54d8af2009-10-20 05:25:22 +000026 const Diagnostic::ArgumentValue *PrevArgs,
27 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
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000036Diagnostic::Diagnostic(const llvm::IntrusiveRefCntPtr<DiagnosticIDs> &diags,
37 DiagnosticClient *client, bool ShouldOwnClient)
38 : Diags(diags), Client(client), OwnsDiagClient(ShouldOwnClient),
39 SourceMgr(0) {
Chris Lattner3fdf4b02008-11-23 09:21:17 +000040 ArgToStringFn = DummyArgToStringFn;
Chris Lattner92dd3862009-02-19 23:53:20 +000041 ArgToStringCookie = 0;
Mike Stump1eb44332009-09-09 15:08:12 +000042
Douglas Gregorcc5888d2010-07-31 00:40:00 +000043 AllExtensionsSilenced = 0;
44 IgnoreAllWarnings = false;
45 WarningsAsErrors = false;
46 ErrorsAsFatal = false;
47 SuppressSystemWarnings = false;
48 SuppressAllDiagnostics = false;
49 ShowOverloads = Ovl_All;
50 ExtBehavior = Ext_Ignore;
51
52 ErrorLimit = 0;
53 TemplateBacktraceLimit = 0;
Douglas Gregorcc5888d2010-07-31 00:40:00 +000054
Douglas Gregorabc563f2010-07-19 21:46:24 +000055 Reset();
Reid Spencer5f016e22007-07-11 17:01:13 +000056}
57
Chris Lattner182745a2007-12-02 01:09:57 +000058Diagnostic::~Diagnostic() {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000059 if (OwnsDiagClient)
60 delete Client;
Chris Lattner182745a2007-12-02 01:09:57 +000061}
62
Douglas Gregor4f5e21e2011-01-31 22:04:05 +000063void Diagnostic::setClient(DiagnosticClient *client, bool ShouldOwnClient) {
64 if (OwnsDiagClient && Client)
65 delete Client;
66
67 Client = client;
68 OwnsDiagClient = ShouldOwnClient;
69}
Chris Lattner04ae2df2009-07-12 21:18:45 +000070
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +000071void Diagnostic::pushMappings(SourceLocation Loc) {
72 DiagStateOnPushStack.push_back(GetCurDiagState());
Chris Lattner04ae2df2009-07-12 21:18:45 +000073}
74
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +000075bool Diagnostic::popMappings(SourceLocation Loc) {
76 if (DiagStateOnPushStack.empty())
Chris Lattner04ae2df2009-07-12 21:18:45 +000077 return false;
78
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +000079 if (DiagStateOnPushStack.back() != GetCurDiagState()) {
80 // State changed at some point between push/pop.
81 PushDiagStatePoint(DiagStateOnPushStack.back(), Loc);
82 }
83 DiagStateOnPushStack.pop_back();
Chris Lattner04ae2df2009-07-12 21:18:45 +000084 return true;
85}
86
Douglas Gregorabc563f2010-07-19 21:46:24 +000087void Diagnostic::Reset() {
Douglas Gregorabc563f2010-07-19 21:46:24 +000088 ErrorOccurred = false;
89 FatalErrorOccurred = false;
Douglas Gregor85bea972011-07-06 17:40:26 +000090 UnrecoverableErrorOccurred = false;
Douglas Gregorabc563f2010-07-19 21:46:24 +000091
92 NumWarnings = 0;
93 NumErrors = 0;
94 NumErrorsSuppressed = 0;
Douglas Gregor85bea972011-07-06 17:40:26 +000095
Douglas Gregorabc563f2010-07-19 21:46:24 +000096 CurDiagID = ~0U;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000097 // Set LastDiagLevel to an "unset" state. If we set it to 'Ignored', notes
98 // using a Diagnostic associated to a translation unit that follow
99 // diagnostics from a Diagnostic associated to anoter t.u. will not be
100 // displayed.
101 LastDiagLevel = (DiagnosticIDs::Level)-1;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000102 DelayedDiagID = 0;
Argyrios Kyrtzidisdc0a2da2011-03-26 18:58:17 +0000103
104 // Clear state related to #pragma diagnostic.
105 DiagStates.clear();
106 DiagStatePoints.clear();
107 DiagStateOnPushStack.clear();
108
109 // Create a DiagState and DiagStatePoint representing diagnostic changes
110 // through command-line.
111 DiagStates.push_back(DiagState());
112 PushDiagStatePoint(&DiagStates.back(), SourceLocation());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000113}
Reid Spencer5f016e22007-07-11 17:01:13 +0000114
Chris Lattner5f9e2722011-07-23 10:55:15 +0000115void Diagnostic::SetDelayedDiagnostic(unsigned DiagID, StringRef Arg1,
116 StringRef Arg2) {
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000117 if (DelayedDiagID)
118 return;
119
120 DelayedDiagID = DiagID;
Douglas Gregor9e2dac92010-03-22 15:47:45 +0000121 DelayedDiagArg1 = Arg1.str();
122 DelayedDiagArg2 = Arg2.str();
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000123}
124
125void Diagnostic::ReportDelayed() {
126 Report(DelayedDiagID) << DelayedDiagArg1 << DelayedDiagArg2;
127 DelayedDiagID = 0;
128 DelayedDiagArg1.clear();
129 DelayedDiagArg2.clear();
130}
131
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000132Diagnostic::DiagStatePointsTy::iterator
133Diagnostic::GetDiagStatePointForLoc(SourceLocation L) const {
134 assert(!DiagStatePoints.empty());
135 assert(DiagStatePoints.front().Loc.isInvalid() &&
136 "Should have created a DiagStatePoint for command-line");
137
138 FullSourceLoc Loc(L, *SourceMgr);
139 if (Loc.isInvalid())
140 return DiagStatePoints.end() - 1;
141
142 DiagStatePointsTy::iterator Pos = DiagStatePoints.end();
143 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
144 if (LastStateChangePos.isValid() &&
145 Loc.isBeforeInTranslationUnitThan(LastStateChangePos))
146 Pos = std::upper_bound(DiagStatePoints.begin(), DiagStatePoints.end(),
147 DiagStatePoint(0, Loc));
148 --Pos;
149 return Pos;
150}
151
152/// \brief This allows the client to specify that certain
153/// warnings are ignored. Notes can never be mapped, errors can only be
154/// mapped to fatal, and WARNINGs and EXTENSIONs can be mapped arbitrarily.
155///
156/// \param The source location that this change of diagnostic state should
157/// take affect. It can be null if we are setting the latest state.
158void Diagnostic::setDiagnosticMapping(diag::kind Diag, diag::Mapping Map,
159 SourceLocation L) {
160 assert(Diag < diag::DIAG_UPPER_LIMIT &&
161 "Can only map builtin diagnostics");
162 assert((Diags->isBuiltinWarningOrExtension(Diag) ||
163 (Map == diag::MAP_FATAL || Map == diag::MAP_ERROR)) &&
164 "Cannot map errors into warnings!");
165 assert(!DiagStatePoints.empty());
166
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +0000167 bool isPragma = L.isValid();
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000168 FullSourceLoc Loc(L, *SourceMgr);
169 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
170
171 // Common case; setting all the diagnostics of a group in one place.
172 if (Loc.isInvalid() || Loc == LastStateChangePos) {
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +0000173 setDiagnosticMappingInternal(Diag, Map, GetCurDiagState(), true, isPragma);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000174 return;
175 }
176
177 // Another common case; modifying diagnostic state in a source location
178 // after the previous one.
179 if ((Loc.isValid() && LastStateChangePos.isInvalid()) ||
180 LastStateChangePos.isBeforeInTranslationUnitThan(Loc)) {
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000181 // A diagnostic pragma occurred, create a new DiagState initialized with
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000182 // the current one and a new DiagStatePoint to record at which location
183 // the new state became active.
184 DiagStates.push_back(*GetCurDiagState());
185 PushDiagStatePoint(&DiagStates.back(), Loc);
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +0000186 setDiagnosticMappingInternal(Diag, Map, GetCurDiagState(), true, isPragma);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000187 return;
188 }
189
190 // We allow setting the diagnostic state in random source order for
191 // completeness but it should not be actually happening in normal practice.
192
193 DiagStatePointsTy::iterator Pos = GetDiagStatePointForLoc(Loc);
194 assert(Pos != DiagStatePoints.end());
195
196 // Update all diagnostic states that are active after the given location.
197 for (DiagStatePointsTy::iterator
198 I = Pos+1, E = DiagStatePoints.end(); I != E; ++I) {
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +0000199 setDiagnosticMappingInternal(Diag, Map, I->State, true, isPragma);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000200 }
201
202 // If the location corresponds to an existing point, just update its state.
203 if (Pos->Loc == Loc) {
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +0000204 setDiagnosticMappingInternal(Diag, Map, Pos->State, true, isPragma);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000205 return;
206 }
207
208 // Create a new state/point and fit it into the vector of DiagStatePoints
209 // so that the vector is always ordered according to location.
210 Pos->Loc.isBeforeInTranslationUnitThan(Loc);
211 DiagStates.push_back(*Pos->State);
212 DiagState *NewState = &DiagStates.back();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +0000213 setDiagnosticMappingInternal(Diag, Map, NewState, true, isPragma);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000214 DiagStatePoints.insert(Pos+1, DiagStatePoint(NewState,
215 FullSourceLoc(Loc, *SourceMgr)));
216}
217
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000218void Diagnostic::Report(const StoredDiagnostic &storedDiag) {
219 assert(CurDiagID == ~0U && "Multiple diagnostics in flight at once!");
220
221 CurDiagLoc = storedDiag.getLocation();
222 CurDiagID = storedDiag.getID();
223 NumDiagArgs = 0;
224
225 NumDiagRanges = storedDiag.range_size();
226 assert(NumDiagRanges < sizeof(DiagRanges)/sizeof(DiagRanges[0]) &&
227 "Too many arguments to diagnostic!");
228 unsigned i = 0;
229 for (StoredDiagnostic::range_iterator
230 RI = storedDiag.range_begin(),
231 RE = storedDiag.range_end(); RI != RE; ++RI)
232 DiagRanges[i++] = *RI;
233
234 NumFixItHints = storedDiag.fixit_size();
235 assert(NumFixItHints < Diagnostic::MaxFixItHints && "Too many fix-it hints!");
236 i = 0;
237 for (StoredDiagnostic::fixit_iterator
238 FI = storedDiag.fixit_begin(),
239 FE = storedDiag.fixit_end(); FI != FE; ++FI)
240 FixItHints[i++] = *FI;
241
242 assert(Client && "DiagnosticClient not set!");
243 Level DiagLevel = storedDiag.getLevel();
244 DiagnosticInfo Info(this, storedDiag.getMessage());
245 Client->HandleDiagnostic(DiagLevel, Info);
246 if (Client->IncludeInDiagnosticCounts()) {
247 if (DiagLevel == Diagnostic::Warning)
248 ++NumWarnings;
249 }
250
251 CurDiagID = ~0U;
252}
253
Douglas Gregorb5350412010-10-13 17:22:14 +0000254void DiagnosticBuilder::FlushCounts() {
255 DiagObj->NumDiagArgs = NumArgs;
256 DiagObj->NumDiagRanges = NumRanges;
257 DiagObj->NumFixItHints = NumFixItHints;
258}
259
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000260bool DiagnosticBuilder::Emit() {
261 // If DiagObj is null, then its soul was stolen by the copy ctor
262 // or the user called Emit().
263 if (DiagObj == 0) return false;
264
265 // When emitting diagnostics, we set the final argument count into
266 // the Diagnostic object.
Douglas Gregorb5350412010-10-13 17:22:14 +0000267 FlushCounts();
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000268
269 // Process the diagnostic, sending the accumulated information to the
270 // DiagnosticClient.
271 bool Emitted = DiagObj->ProcessDiag();
272
273 // Clear out the current diagnostic object.
Douglas Gregor9e2dac92010-03-22 15:47:45 +0000274 unsigned DiagID = DiagObj->CurDiagID;
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000275 DiagObj->Clear();
276
277 // If there was a delayed diagnostic, emit it now.
Douglas Gregor9e2dac92010-03-22 15:47:45 +0000278 if (DiagObj->DelayedDiagID && DiagObj->DelayedDiagID != DiagID)
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000279 DiagObj->ReportDelayed();
280
281 // This diagnostic is dead.
282 DiagObj = 0;
283
284 return Emitted;
285}
286
Nico Weber7bfaaae2008-08-10 19:59:06 +0000287
Reid Spencer5f016e22007-07-11 17:01:13 +0000288DiagnosticClient::~DiagnosticClient() {}
Nico Weber7bfaaae2008-08-10 19:59:06 +0000289
Argyrios Kyrtzidisf2224d82010-11-18 20:06:46 +0000290void DiagnosticClient::HandleDiagnostic(Diagnostic::Level DiagLevel,
291 const DiagnosticInfo &Info) {
292 if (!IncludeInDiagnosticCounts())
293 return;
294
295 if (DiagLevel == Diagnostic::Warning)
296 ++NumWarnings;
297 else if (DiagLevel >= Diagnostic::Error)
298 ++NumErrors;
299}
Chris Lattnerf4c83962008-11-19 06:51:40 +0000300
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000301/// ModifierIs - Return true if the specified modifier matches specified string.
302template <std::size_t StrLen>
303static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
304 const char (&Str)[StrLen]) {
305 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
306}
307
John McCall909c1822010-01-14 20:11:39 +0000308/// ScanForward - Scans forward, looking for the given character, skipping
309/// nested clauses and escaped characters.
310static const char *ScanFormat(const char *I, const char *E, char Target) {
311 unsigned Depth = 0;
312
313 for ( ; I != E; ++I) {
314 if (Depth == 0 && *I == Target) return I;
315 if (Depth != 0 && *I == '}') Depth--;
316
317 if (*I == '%') {
318 I++;
319 if (I == E) break;
320
321 // Escaped characters get implicitly skipped here.
322
323 // Format specifier.
324 if (!isdigit(*I) && !ispunct(*I)) {
325 for (I++; I != E && !isdigit(*I) && *I != '{'; I++) ;
326 if (I == E) break;
327 if (*I == '{')
328 Depth++;
329 }
330 }
331 }
332 return E;
333}
334
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000335/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
336/// like this: %select{foo|bar|baz}2. This means that the integer argument
337/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
338/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
339/// This is very useful for certain classes of variant diagnostics.
John McCall9f286142010-01-13 23:58:20 +0000340static void HandleSelectModifier(const DiagnosticInfo &DInfo, unsigned ValNo,
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000341 const char *Argument, unsigned ArgumentLen,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000342 SmallVectorImpl<char> &OutStr) {
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000343 const char *ArgumentEnd = Argument+ArgumentLen;
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000345 // Skip over 'ValNo' |'s.
346 while (ValNo) {
John McCall909c1822010-01-14 20:11:39 +0000347 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000348 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
349 " larger than the number of options in the diagnostic string!");
350 Argument = NextVal+1; // Skip this string.
351 --ValNo;
352 }
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000354 // Get the end of the value. This is either the } or the |.
John McCall909c1822010-01-14 20:11:39 +0000355 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
John McCall9f286142010-01-13 23:58:20 +0000356
357 // Recursively format the result of the select clause into the output string.
358 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000359}
360
361/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
362/// letter 's' to the string if the value is not 1. This is used in cases like
363/// this: "you idiot, you have %4 parameter%s4!".
364static void HandleIntegerSModifier(unsigned ValNo,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000365 SmallVectorImpl<char> &OutStr) {
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000366 if (ValNo != 1)
367 OutStr.push_back('s');
368}
369
John McCall3be16b72010-01-14 00:50:32 +0000370/// HandleOrdinalModifier - Handle the integer 'ord' modifier. This
371/// prints the ordinal form of the given integer, with 1 corresponding
372/// to the first ordinal. Currently this is hard-coded to use the
373/// English form.
374static void HandleOrdinalModifier(unsigned ValNo,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000375 SmallVectorImpl<char> &OutStr) {
John McCall3be16b72010-01-14 00:50:32 +0000376 assert(ValNo != 0 && "ValNo must be strictly positive!");
377
378 llvm::raw_svector_ostream Out(OutStr);
379
380 // We could use text forms for the first N ordinals, but the numeric
381 // forms are actually nicer in diagnostics because they stand out.
382 Out << ValNo;
383
384 // It is critically important that we do this perfectly for
385 // user-written sequences with over 100 elements.
386 switch (ValNo % 100) {
387 case 11:
388 case 12:
389 case 13:
390 Out << "th"; return;
391 default:
392 switch (ValNo % 10) {
393 case 1: Out << "st"; return;
394 case 2: Out << "nd"; return;
395 case 3: Out << "rd"; return;
396 default: Out << "th"; return;
397 }
398 }
399}
400
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000401
Sebastian Redle4c452c2008-11-22 13:44:36 +0000402/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000403static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000404 // Programming 101: Parse a decimal number :-)
405 unsigned Val = 0;
406 while (Start != End && *Start >= '0' && *Start <= '9') {
407 Val *= 10;
408 Val += *Start - '0';
409 ++Start;
410 }
411 return Val;
412}
413
414/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000415static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000416 if (*Start != '[') {
417 unsigned Ref = PluralNumber(Start, End);
418 return Ref == Val;
419 }
420
421 ++Start;
422 unsigned Low = PluralNumber(Start, End);
423 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
424 ++Start;
425 unsigned High = PluralNumber(Start, End);
426 assert(*Start == ']' && "Bad plural expression syntax: expected )");
427 ++Start;
428 return Low <= Val && Val <= High;
429}
430
431/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000432static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000433 // Empty condition?
434 if (*Start == ':')
435 return true;
436
437 while (1) {
438 char C = *Start;
439 if (C == '%') {
440 // Modulo expression
441 ++Start;
442 unsigned Arg = PluralNumber(Start, End);
443 assert(*Start == '=' && "Bad plural expression syntax: expected =");
444 ++Start;
445 unsigned ValMod = ValNo % Arg;
446 if (TestPluralRange(ValMod, Start, End))
447 return true;
448 } else {
Sebastian Redle2065322008-11-27 07:28:14 +0000449 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redle4c452c2008-11-22 13:44:36 +0000450 "Bad plural expression syntax: unexpected character");
451 // Range expression
452 if (TestPluralRange(ValNo, Start, End))
453 return true;
454 }
455
456 // Scan for next or-expr part.
457 Start = std::find(Start, End, ',');
Mike Stump1eb44332009-09-09 15:08:12 +0000458 if (Start == End)
Sebastian Redle4c452c2008-11-22 13:44:36 +0000459 break;
460 ++Start;
461 }
462 return false;
463}
464
465/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
466/// for complex plural forms, or in languages where all plurals are complex.
467/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
468/// conditions that are tested in order, the form corresponding to the first
469/// that applies being emitted. The empty condition is always true, making the
470/// last form a default case.
471/// Conditions are simple boolean expressions, where n is the number argument.
472/// Here are the rules.
473/// condition := expression | empty
474/// empty := -> always true
475/// expression := numeric [',' expression] -> logical or
476/// numeric := range -> true if n in range
477/// | '%' number '=' range -> true if n % number in range
478/// range := number
479/// | '[' number ',' number ']' -> ranges are inclusive both ends
480///
481/// Here are some examples from the GNU gettext manual written in this form:
482/// English:
483/// {1:form0|:form1}
484/// Latvian:
485/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
486/// Gaeilge:
487/// {1:form0|2:form1|:form2}
488/// Romanian:
489/// {1:form0|0,%100=[1,19]:form1|:form2}
490/// Lithuanian:
491/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
492/// Russian (requires repeated form):
493/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
494/// Slovak
495/// {1:form0|[2,4]:form1|:form2}
496/// Polish (requires repeated form):
497/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
John McCalle53a44b2010-10-14 01:55:31 +0000498static void HandlePluralModifier(const DiagnosticInfo &DInfo, unsigned ValNo,
Sebastian Redle4c452c2008-11-22 13:44:36 +0000499 const char *Argument, unsigned ArgumentLen,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000500 SmallVectorImpl<char> &OutStr) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000501 const char *ArgumentEnd = Argument + ArgumentLen;
502 while (1) {
503 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
504 const char *ExprEnd = Argument;
505 while (*ExprEnd != ':') {
506 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
507 ++ExprEnd;
508 }
509 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
510 Argument = ExprEnd + 1;
John McCall909c1822010-01-14 20:11:39 +0000511 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
John McCalle53a44b2010-10-14 01:55:31 +0000512
513 // Recursively format the result of the plural clause into the
514 // output string.
515 DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000516 return;
517 }
John McCall909c1822010-01-14 20:11:39 +0000518 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
Sebastian Redle4c452c2008-11-22 13:44:36 +0000519 }
520}
521
522
Chris Lattnerf4c83962008-11-19 06:51:40 +0000523/// FormatDiagnostic - Format this diagnostic into a string, substituting the
524/// formal arguments into the %0 slots. The result is appended onto the Str
525/// array.
526void DiagnosticInfo::
Chris Lattner5f9e2722011-07-23 10:55:15 +0000527FormatDiagnostic(SmallVectorImpl<char> &OutStr) const {
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000528 if (!StoredDiagMessage.empty()) {
529 OutStr.append(StoredDiagMessage.begin(), StoredDiagMessage.end());
530 return;
531 }
532
Chris Lattner5f9e2722011-07-23 10:55:15 +0000533 StringRef Diag =
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000534 getDiags()->getDiagnosticIDs()->getDescription(getID());
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000536 FormatDiagnostic(Diag.begin(), Diag.end(), OutStr);
John McCall9f286142010-01-13 23:58:20 +0000537}
538
539void DiagnosticInfo::
540FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000541 SmallVectorImpl<char> &OutStr) const {
John McCall9f286142010-01-13 23:58:20 +0000542
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000543 /// FormattedArgs - Keep track of all of the arguments formatted by
544 /// ConvertArgToString and pass them into subsequent calls to
545 /// ConvertArgToString, allowing the implementation to avoid redundancies in
546 /// obvious cases.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000547 SmallVector<Diagnostic::ArgumentValue, 8> FormattedArgs;
Chandler Carruth0673cb32011-07-11 17:49:21 +0000548
549 /// QualTypeVals - Pass a vector of arrays so that QualType names can be
550 /// compared to see if more information is needed to be printed.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000551 SmallVector<intptr_t, 2> QualTypeVals;
Chandler Carruth0673cb32011-07-11 17:49:21 +0000552 for (unsigned i = 0, e = getNumArgs(); i < e; ++i)
553 if (getArgKind(i) == Diagnostic::ak_qualtype)
554 QualTypeVals.push_back(getRawArg(i));
555
Chris Lattnerf4c83962008-11-19 06:51:40 +0000556 while (DiagStr != DiagEnd) {
557 if (DiagStr[0] != '%') {
558 // Append non-%0 substrings to Str if we have one.
559 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
560 OutStr.append(DiagStr, StrEnd);
561 DiagStr = StrEnd;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000562 continue;
John McCall909c1822010-01-14 20:11:39 +0000563 } else if (ispunct(DiagStr[1])) {
564 OutStr.push_back(DiagStr[1]); // %% -> %.
Chris Lattnerf4c83962008-11-19 06:51:40 +0000565 DiagStr += 2;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000566 continue;
567 }
Mike Stump1eb44332009-09-09 15:08:12 +0000568
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000569 // Skip the %.
570 ++DiagStr;
Mike Stump1eb44332009-09-09 15:08:12 +0000571
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000572 // This must be a placeholder for a diagnostic argument. The format for a
573 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
574 // The digit is a number from 0-9 indicating which argument this comes from.
575 // The modifier is a string of digits from the set [-a-z]+, arguments is a
576 // brace enclosed string.
577 const char *Modifier = 0, *Argument = 0;
578 unsigned ModifierLen = 0, ArgumentLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000579
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000580 // Check to see if we have a modifier. If so eat it.
581 if (!isdigit(DiagStr[0])) {
582 Modifier = DiagStr;
583 while (DiagStr[0] == '-' ||
584 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
585 ++DiagStr;
586 ModifierLen = DiagStr-Modifier;
Chris Lattnerf4c83962008-11-19 06:51:40 +0000587
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000588 // If we have an argument, get it next.
589 if (DiagStr[0] == '{') {
590 ++DiagStr; // Skip {.
591 Argument = DiagStr;
Mike Stump1eb44332009-09-09 15:08:12 +0000592
John McCall909c1822010-01-14 20:11:39 +0000593 DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
594 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000595 ArgumentLen = DiagStr-Argument;
596 ++DiagStr; // Skip }.
Chris Lattnerf4c83962008-11-19 06:51:40 +0000597 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000598 }
Mike Stump1eb44332009-09-09 15:08:12 +0000599
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000600 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner22caddc2008-11-23 09:13:29 +0000601 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000602
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000603 Diagnostic::ArgumentKind Kind = getArgKind(ArgNo);
604
605 switch (Kind) {
Chris Lattner08631c52008-11-23 21:45:46 +0000606 // ---- STRINGS ----
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000607 case Diagnostic::ak_std_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000608 const std::string &S = getArgStdStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000609 assert(ModifierLen == 0 && "No modifiers for strings yet");
610 OutStr.append(S.begin(), S.end());
611 break;
612 }
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000613 case Diagnostic::ak_c_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000614 const char *S = getArgCStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000615 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbare46e3542009-04-20 06:13:16 +0000616
617 // Don't crash if get passed a null pointer by accident.
618 if (!S)
619 S = "(null)";
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000621 OutStr.append(S, S + strlen(S));
622 break;
623 }
Chris Lattner08631c52008-11-23 21:45:46 +0000624 // ---- INTEGERS ----
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000625 case Diagnostic::ak_sint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000626 int Val = getArgSInt(ArgNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000628 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCalle53a44b2010-10-14 01:55:31 +0000629 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen,
630 OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000631 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
632 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000633 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCalle53a44b2010-10-14 01:55:31 +0000634 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
635 OutStr);
John McCall3be16b72010-01-14 00:50:32 +0000636 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
637 HandleOrdinalModifier((unsigned)Val, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000638 } else {
639 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbar23e47c62009-10-17 18:12:14 +0000640 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner30bc9652008-11-19 07:22:31 +0000641 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000642 break;
643 }
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000644 case Diagnostic::ak_uint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000645 unsigned Val = getArgUInt(ArgNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000646
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000647 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCall9f286142010-01-13 23:58:20 +0000648 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000649 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
650 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000651 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCalle53a44b2010-10-14 01:55:31 +0000652 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
653 OutStr);
John McCall3be16b72010-01-14 00:50:32 +0000654 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
655 HandleOrdinalModifier(Val, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000656 } else {
657 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbar23e47c62009-10-17 18:12:14 +0000658 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner30bc9652008-11-19 07:22:31 +0000659 }
Chris Lattner22caddc2008-11-23 09:13:29 +0000660 break;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000661 }
Chris Lattner08631c52008-11-23 21:45:46 +0000662 // ---- NAMES and TYPES ----
663 case Diagnostic::ak_identifierinfo: {
Chris Lattner08631c52008-11-23 21:45:46 +0000664 const IdentifierInfo *II = getArgIdentifier(ArgNo);
665 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbare46e3542009-04-20 06:13:16 +0000666
667 // Don't crash if get passed a null pointer by accident.
668 if (!II) {
669 const char *S = "(null)";
670 OutStr.append(S, S + strlen(S));
671 continue;
672 }
673
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000674 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
Chris Lattner08631c52008-11-23 21:45:46 +0000675 break;
676 }
Chris Lattner22caddc2008-11-23 09:13:29 +0000677 case Diagnostic::ak_qualtype:
Chris Lattner011bb4e2008-11-23 20:28:15 +0000678 case Diagnostic::ak_declarationname:
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000679 case Diagnostic::ak_nameddecl:
Douglas Gregordacd4342009-08-26 00:04:55 +0000680 case Diagnostic::ak_nestednamespec:
Douglas Gregor3f093272009-10-13 21:16:44 +0000681 case Diagnostic::ak_declcontext:
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000682 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000683 Modifier, ModifierLen,
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000684 Argument, ArgumentLen,
685 FormattedArgs.data(), FormattedArgs.size(),
Chandler Carruth0673cb32011-07-11 17:49:21 +0000686 OutStr, QualTypeVals);
Chris Lattner22caddc2008-11-23 09:13:29 +0000687 break;
Nico Weber7bfaaae2008-08-10 19:59:06 +0000688 }
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000689
690 // Remember this argument info for subsequent formatting operations. Turn
691 // std::strings into a null terminated string to make it be the same case as
692 // all the other ones.
693 if (Kind != Diagnostic::ak_std_string)
694 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
695 else
696 FormattedArgs.push_back(std::make_pair(Diagnostic::ak_c_string,
697 (intptr_t)getArgStdStr(ArgNo).c_str()));
698
Nico Weber7bfaaae2008-08-10 19:59:06 +0000699 }
Nico Weber7bfaaae2008-08-10 19:59:06 +0000700}
Ted Kremenekcabe6682009-01-23 20:28:53 +0000701
Douglas Gregora88084b2010-02-18 18:08:43 +0000702StoredDiagnostic::StoredDiagnostic() { }
703
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000704StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level, unsigned ID,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000705 StringRef Message)
Benjamin Kramera6a32e22010-11-19 17:36:51 +0000706 : ID(ID), Level(Level), Loc(), Message(Message) { }
Douglas Gregora88084b2010-02-18 18:08:43 +0000707
708StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level,
709 const DiagnosticInfo &Info)
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000710 : ID(Info.getID()), Level(Level)
711{
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000712 assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
713 "Valid source location without setting a source manager for diagnostic");
714 if (Info.getLocation().isValid())
715 Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
Douglas Gregora88084b2010-02-18 18:08:43 +0000716 llvm::SmallString<64> Message;
717 Info.FormatDiagnostic(Message);
718 this->Message.assign(Message.begin(), Message.end());
719
720 Ranges.reserve(Info.getNumRanges());
721 for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I)
722 Ranges.push_back(Info.getRange(I));
723
Douglas Gregor849b2432010-03-31 17:46:05 +0000724 FixIts.reserve(Info.getNumFixItHints());
725 for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I)
726 FixIts.push_back(Info.getFixItHint(I));
Douglas Gregora88084b2010-02-18 18:08:43 +0000727}
728
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000729StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level, unsigned ID,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000730 StringRef Message, FullSourceLoc Loc,
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000731 llvm::ArrayRef<CharSourceRange> Ranges,
732 llvm::ArrayRef<FixItHint> Fixits)
733 : ID(ID), Level(Level), Loc(Loc), Message(Message)
734{
735 this->Ranges.assign(Ranges.begin(), Ranges.end());
736 this->FixIts.assign(FixIts.begin(), FixIts.end());
737}
738
Douglas Gregora88084b2010-02-18 18:08:43 +0000739StoredDiagnostic::~StoredDiagnostic() { }
740
Ted Kremenekcabe6682009-01-23 20:28:53 +0000741/// IncludeInDiagnosticCounts - This method (whose default implementation
742/// returns true) indicates whether the diagnostics handled by this
743/// DiagnosticClient should be included in the number of diagnostics
744/// reported by Diagnostic.
745bool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; }
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000746
747PartialDiagnostic::StorageAllocator::StorageAllocator() {
748 for (unsigned I = 0; I != NumCached; ++I)
749 FreeList[I] = Cached + I;
750 NumFreeListEntries = NumCached;
751}
752
753PartialDiagnostic::StorageAllocator::~StorageAllocator() {
Ted Kremenek03201fb2011-03-21 18:40:07 +0000754 // Don't assert if we are in a CrashRecovery context, as this
755 // invariant may be invalidated during a crash.
Ted Kremenekcd1eecf2011-03-21 18:40:10 +0000756 assert((NumFreeListEntries == NumCached || llvm::CrashRecoveryContext::isRecoveringFromCrash()) && "A partial is on the lamb");
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000757}