blob: 858b70a712348a87efaf59e5b1ea6c2f24cf8676 [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"
Reid Spencer5f016e22007-07-11 17:01:13 +000019using namespace clang;
20
Chris Lattner3fdf4b02008-11-23 09:21:17 +000021static void DummyArgToStringFn(Diagnostic::ArgumentKind AK, intptr_t QT,
22 const char *Modifier, unsigned ML,
23 const char *Argument, unsigned ArgLen,
Chris Lattnerb54d8af2009-10-20 05:25:22 +000024 const Diagnostic::ArgumentValue *PrevArgs,
25 unsigned NumPrevArgs,
Chris Lattner92dd3862009-02-19 23:53:20 +000026 llvm::SmallVectorImpl<char> &Output,
27 void *Cookie) {
Chris Lattner3fdf4b02008-11-23 09:21:17 +000028 const char *Str = "<can't format argument>";
Chris Lattner22caddc2008-11-23 09:13:29 +000029 Output.append(Str, Str+strlen(Str));
30}
31
32
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000033Diagnostic::Diagnostic(const llvm::IntrusiveRefCntPtr<DiagnosticIDs> &diags,
34 DiagnosticClient *client, bool ShouldOwnClient)
35 : Diags(diags), Client(client), OwnsDiagClient(ShouldOwnClient),
36 SourceMgr(0) {
Chris Lattner3fdf4b02008-11-23 09:21:17 +000037 ArgToStringFn = DummyArgToStringFn;
Chris Lattner92dd3862009-02-19 23:53:20 +000038 ArgToStringCookie = 0;
Mike Stump1eb44332009-09-09 15:08:12 +000039
Douglas Gregorcc5888d2010-07-31 00:40:00 +000040 AllExtensionsSilenced = 0;
41 IgnoreAllWarnings = false;
42 WarningsAsErrors = false;
43 ErrorsAsFatal = false;
44 SuppressSystemWarnings = false;
45 SuppressAllDiagnostics = false;
46 ShowOverloads = Ovl_All;
47 ExtBehavior = Ext_Ignore;
48
49 ErrorLimit = 0;
50 TemplateBacktraceLimit = 0;
Douglas Gregorcc5888d2010-07-31 00:40:00 +000051
52 // Set all mappings to 'unset'.
53 DiagMappingsStack.clear();
54 DiagMappingsStack.push_back(DiagMappings());
55
Douglas Gregorabc563f2010-07-19 21:46:24 +000056 Reset();
Reid Spencer5f016e22007-07-11 17:01:13 +000057}
58
Chris Lattner182745a2007-12-02 01:09:57 +000059Diagnostic::~Diagnostic() {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000060 if (OwnsDiagClient)
61 delete Client;
Chris Lattner182745a2007-12-02 01:09:57 +000062}
63
Chris Lattner04ae2df2009-07-12 21:18:45 +000064
65void Diagnostic::pushMappings() {
John Thompsonca2c3e22009-10-23 02:21:17 +000066 // Avoids undefined behavior when the stack has to resize.
67 DiagMappingsStack.reserve(DiagMappingsStack.size() + 1);
Chris Lattner04ae2df2009-07-12 21:18:45 +000068 DiagMappingsStack.push_back(DiagMappingsStack.back());
69}
70
71bool Diagnostic::popMappings() {
72 if (DiagMappingsStack.size() == 1)
73 return false;
74
75 DiagMappingsStack.pop_back();
76 return true;
77}
78
Douglas Gregorabc563f2010-07-19 21:46:24 +000079void Diagnostic::Reset() {
Douglas Gregorabc563f2010-07-19 21:46:24 +000080 ErrorOccurred = false;
81 FatalErrorOccurred = false;
Douglas Gregorabc563f2010-07-19 21:46:24 +000082
83 NumWarnings = 0;
84 NumErrors = 0;
85 NumErrorsSuppressed = 0;
Douglas Gregorabc563f2010-07-19 21:46:24 +000086 CurDiagID = ~0U;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000087 // Set LastDiagLevel to an "unset" state. If we set it to 'Ignored', notes
88 // using a Diagnostic associated to a translation unit that follow
89 // diagnostics from a Diagnostic associated to anoter t.u. will not be
90 // displayed.
91 LastDiagLevel = (DiagnosticIDs::Level)-1;
Douglas Gregorabc563f2010-07-19 21:46:24 +000092 DelayedDiagID = 0;
Douglas Gregorabc563f2010-07-19 21:46:24 +000093}
Reid Spencer5f016e22007-07-11 17:01:13 +000094
Douglas Gregor93ea5cb2010-03-22 15:10:57 +000095void Diagnostic::SetDelayedDiagnostic(unsigned DiagID, llvm::StringRef Arg1,
96 llvm::StringRef Arg2) {
97 if (DelayedDiagID)
98 return;
99
100 DelayedDiagID = DiagID;
Douglas Gregor9e2dac92010-03-22 15:47:45 +0000101 DelayedDiagArg1 = Arg1.str();
102 DelayedDiagArg2 = Arg2.str();
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000103}
104
105void Diagnostic::ReportDelayed() {
106 Report(DelayedDiagID) << DelayedDiagArg1 << DelayedDiagArg2;
107 DelayedDiagID = 0;
108 DelayedDiagArg1.clear();
109 DelayedDiagArg2.clear();
110}
111
Douglas Gregorb5350412010-10-13 17:22:14 +0000112void DiagnosticBuilder::FlushCounts() {
113 DiagObj->NumDiagArgs = NumArgs;
114 DiagObj->NumDiagRanges = NumRanges;
115 DiagObj->NumFixItHints = NumFixItHints;
116}
117
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000118bool DiagnosticBuilder::Emit() {
119 // If DiagObj is null, then its soul was stolen by the copy ctor
120 // or the user called Emit().
121 if (DiagObj == 0) return false;
122
123 // When emitting diagnostics, we set the final argument count into
124 // the Diagnostic object.
Douglas Gregorb5350412010-10-13 17:22:14 +0000125 FlushCounts();
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000126
127 // Process the diagnostic, sending the accumulated information to the
128 // DiagnosticClient.
129 bool Emitted = DiagObj->ProcessDiag();
130
131 // Clear out the current diagnostic object.
Douglas Gregor9e2dac92010-03-22 15:47:45 +0000132 unsigned DiagID = DiagObj->CurDiagID;
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000133 DiagObj->Clear();
134
135 // If there was a delayed diagnostic, emit it now.
Douglas Gregor9e2dac92010-03-22 15:47:45 +0000136 if (DiagObj->DelayedDiagID && DiagObj->DelayedDiagID != DiagID)
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000137 DiagObj->ReportDelayed();
138
139 // This diagnostic is dead.
140 DiagObj = 0;
141
142 return Emitted;
143}
144
Nico Weber7bfaaae2008-08-10 19:59:06 +0000145
Reid Spencer5f016e22007-07-11 17:01:13 +0000146DiagnosticClient::~DiagnosticClient() {}
Nico Weber7bfaaae2008-08-10 19:59:06 +0000147
Argyrios Kyrtzidisf2224d82010-11-18 20:06:46 +0000148void DiagnosticClient::HandleDiagnostic(Diagnostic::Level DiagLevel,
149 const DiagnosticInfo &Info) {
150 if (!IncludeInDiagnosticCounts())
151 return;
152
153 if (DiagLevel == Diagnostic::Warning)
154 ++NumWarnings;
155 else if (DiagLevel >= Diagnostic::Error)
156 ++NumErrors;
157}
Chris Lattnerf4c83962008-11-19 06:51:40 +0000158
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000159/// ModifierIs - Return true if the specified modifier matches specified string.
160template <std::size_t StrLen>
161static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
162 const char (&Str)[StrLen]) {
163 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
164}
165
John McCall909c1822010-01-14 20:11:39 +0000166/// ScanForward - Scans forward, looking for the given character, skipping
167/// nested clauses and escaped characters.
168static const char *ScanFormat(const char *I, const char *E, char Target) {
169 unsigned Depth = 0;
170
171 for ( ; I != E; ++I) {
172 if (Depth == 0 && *I == Target) return I;
173 if (Depth != 0 && *I == '}') Depth--;
174
175 if (*I == '%') {
176 I++;
177 if (I == E) break;
178
179 // Escaped characters get implicitly skipped here.
180
181 // Format specifier.
182 if (!isdigit(*I) && !ispunct(*I)) {
183 for (I++; I != E && !isdigit(*I) && *I != '{'; I++) ;
184 if (I == E) break;
185 if (*I == '{')
186 Depth++;
187 }
188 }
189 }
190 return E;
191}
192
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000193/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
194/// like this: %select{foo|bar|baz}2. This means that the integer argument
195/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
196/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
197/// This is very useful for certain classes of variant diagnostics.
John McCall9f286142010-01-13 23:58:20 +0000198static void HandleSelectModifier(const DiagnosticInfo &DInfo, unsigned ValNo,
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000199 const char *Argument, unsigned ArgumentLen,
200 llvm::SmallVectorImpl<char> &OutStr) {
201 const char *ArgumentEnd = Argument+ArgumentLen;
Mike Stump1eb44332009-09-09 15:08:12 +0000202
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000203 // Skip over 'ValNo' |'s.
204 while (ValNo) {
John McCall909c1822010-01-14 20:11:39 +0000205 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000206 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
207 " larger than the number of options in the diagnostic string!");
208 Argument = NextVal+1; // Skip this string.
209 --ValNo;
210 }
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000212 // Get the end of the value. This is either the } or the |.
John McCall909c1822010-01-14 20:11:39 +0000213 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
John McCall9f286142010-01-13 23:58:20 +0000214
215 // Recursively format the result of the select clause into the output string.
216 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000217}
218
219/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
220/// letter 's' to the string if the value is not 1. This is used in cases like
221/// this: "you idiot, you have %4 parameter%s4!".
222static void HandleIntegerSModifier(unsigned ValNo,
223 llvm::SmallVectorImpl<char> &OutStr) {
224 if (ValNo != 1)
225 OutStr.push_back('s');
226}
227
John McCall3be16b72010-01-14 00:50:32 +0000228/// HandleOrdinalModifier - Handle the integer 'ord' modifier. This
229/// prints the ordinal form of the given integer, with 1 corresponding
230/// to the first ordinal. Currently this is hard-coded to use the
231/// English form.
232static void HandleOrdinalModifier(unsigned ValNo,
233 llvm::SmallVectorImpl<char> &OutStr) {
234 assert(ValNo != 0 && "ValNo must be strictly positive!");
235
236 llvm::raw_svector_ostream Out(OutStr);
237
238 // We could use text forms for the first N ordinals, but the numeric
239 // forms are actually nicer in diagnostics because they stand out.
240 Out << ValNo;
241
242 // It is critically important that we do this perfectly for
243 // user-written sequences with over 100 elements.
244 switch (ValNo % 100) {
245 case 11:
246 case 12:
247 case 13:
248 Out << "th"; return;
249 default:
250 switch (ValNo % 10) {
251 case 1: Out << "st"; return;
252 case 2: Out << "nd"; return;
253 case 3: Out << "rd"; return;
254 default: Out << "th"; return;
255 }
256 }
257}
258
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000259
Sebastian Redle4c452c2008-11-22 13:44:36 +0000260/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000261static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000262 // Programming 101: Parse a decimal number :-)
263 unsigned Val = 0;
264 while (Start != End && *Start >= '0' && *Start <= '9') {
265 Val *= 10;
266 Val += *Start - '0';
267 ++Start;
268 }
269 return Val;
270}
271
272/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000273static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000274 if (*Start != '[') {
275 unsigned Ref = PluralNumber(Start, End);
276 return Ref == Val;
277 }
278
279 ++Start;
280 unsigned Low = PluralNumber(Start, End);
281 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
282 ++Start;
283 unsigned High = PluralNumber(Start, End);
284 assert(*Start == ']' && "Bad plural expression syntax: expected )");
285 ++Start;
286 return Low <= Val && Val <= High;
287}
288
289/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000290static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000291 // Empty condition?
292 if (*Start == ':')
293 return true;
294
295 while (1) {
296 char C = *Start;
297 if (C == '%') {
298 // Modulo expression
299 ++Start;
300 unsigned Arg = PluralNumber(Start, End);
301 assert(*Start == '=' && "Bad plural expression syntax: expected =");
302 ++Start;
303 unsigned ValMod = ValNo % Arg;
304 if (TestPluralRange(ValMod, Start, End))
305 return true;
306 } else {
Sebastian Redle2065322008-11-27 07:28:14 +0000307 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redle4c452c2008-11-22 13:44:36 +0000308 "Bad plural expression syntax: unexpected character");
309 // Range expression
310 if (TestPluralRange(ValNo, Start, End))
311 return true;
312 }
313
314 // Scan for next or-expr part.
315 Start = std::find(Start, End, ',');
Mike Stump1eb44332009-09-09 15:08:12 +0000316 if (Start == End)
Sebastian Redle4c452c2008-11-22 13:44:36 +0000317 break;
318 ++Start;
319 }
320 return false;
321}
322
323/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
324/// for complex plural forms, or in languages where all plurals are complex.
325/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
326/// conditions that are tested in order, the form corresponding to the first
327/// that applies being emitted. The empty condition is always true, making the
328/// last form a default case.
329/// Conditions are simple boolean expressions, where n is the number argument.
330/// Here are the rules.
331/// condition := expression | empty
332/// empty := -> always true
333/// expression := numeric [',' expression] -> logical or
334/// numeric := range -> true if n in range
335/// | '%' number '=' range -> true if n % number in range
336/// range := number
337/// | '[' number ',' number ']' -> ranges are inclusive both ends
338///
339/// Here are some examples from the GNU gettext manual written in this form:
340/// English:
341/// {1:form0|:form1}
342/// Latvian:
343/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
344/// Gaeilge:
345/// {1:form0|2:form1|:form2}
346/// Romanian:
347/// {1:form0|0,%100=[1,19]:form1|:form2}
348/// Lithuanian:
349/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
350/// Russian (requires repeated form):
351/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
352/// Slovak
353/// {1:form0|[2,4]:form1|:form2}
354/// Polish (requires repeated form):
355/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
John McCalle53a44b2010-10-14 01:55:31 +0000356static void HandlePluralModifier(const DiagnosticInfo &DInfo, unsigned ValNo,
Sebastian Redle4c452c2008-11-22 13:44:36 +0000357 const char *Argument, unsigned ArgumentLen,
Chris Lattnerb54b2762009-04-16 05:04:32 +0000358 llvm::SmallVectorImpl<char> &OutStr) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000359 const char *ArgumentEnd = Argument + ArgumentLen;
360 while (1) {
361 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
362 const char *ExprEnd = Argument;
363 while (*ExprEnd != ':') {
364 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
365 ++ExprEnd;
366 }
367 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
368 Argument = ExprEnd + 1;
John McCall909c1822010-01-14 20:11:39 +0000369 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
John McCalle53a44b2010-10-14 01:55:31 +0000370
371 // Recursively format the result of the plural clause into the
372 // output string.
373 DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000374 return;
375 }
John McCall909c1822010-01-14 20:11:39 +0000376 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
Sebastian Redle4c452c2008-11-22 13:44:36 +0000377 }
378}
379
380
Chris Lattnerf4c83962008-11-19 06:51:40 +0000381/// FormatDiagnostic - Format this diagnostic into a string, substituting the
382/// formal arguments into the %0 slots. The result is appended onto the Str
383/// array.
384void DiagnosticInfo::
385FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000386 const char *DiagStr = getDiags()->getDiagnosticIDs()->getDescription(getID());
Chris Lattnerf4c83962008-11-19 06:51:40 +0000387 const char *DiagEnd = DiagStr+strlen(DiagStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000388
John McCall9f286142010-01-13 23:58:20 +0000389 FormatDiagnostic(DiagStr, DiagEnd, OutStr);
390}
391
392void DiagnosticInfo::
393FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
394 llvm::SmallVectorImpl<char> &OutStr) const {
395
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000396 /// FormattedArgs - Keep track of all of the arguments formatted by
397 /// ConvertArgToString and pass them into subsequent calls to
398 /// ConvertArgToString, allowing the implementation to avoid redundancies in
399 /// obvious cases.
400 llvm::SmallVector<Diagnostic::ArgumentValue, 8> FormattedArgs;
401
Chris Lattnerf4c83962008-11-19 06:51:40 +0000402 while (DiagStr != DiagEnd) {
403 if (DiagStr[0] != '%') {
404 // Append non-%0 substrings to Str if we have one.
405 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
406 OutStr.append(DiagStr, StrEnd);
407 DiagStr = StrEnd;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000408 continue;
John McCall909c1822010-01-14 20:11:39 +0000409 } else if (ispunct(DiagStr[1])) {
410 OutStr.push_back(DiagStr[1]); // %% -> %.
Chris Lattnerf4c83962008-11-19 06:51:40 +0000411 DiagStr += 2;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000412 continue;
413 }
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000415 // Skip the %.
416 ++DiagStr;
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000418 // This must be a placeholder for a diagnostic argument. The format for a
419 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
420 // The digit is a number from 0-9 indicating which argument this comes from.
421 // The modifier is a string of digits from the set [-a-z]+, arguments is a
422 // brace enclosed string.
423 const char *Modifier = 0, *Argument = 0;
424 unsigned ModifierLen = 0, ArgumentLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000426 // Check to see if we have a modifier. If so eat it.
427 if (!isdigit(DiagStr[0])) {
428 Modifier = DiagStr;
429 while (DiagStr[0] == '-' ||
430 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
431 ++DiagStr;
432 ModifierLen = DiagStr-Modifier;
Chris Lattnerf4c83962008-11-19 06:51:40 +0000433
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000434 // If we have an argument, get it next.
435 if (DiagStr[0] == '{') {
436 ++DiagStr; // Skip {.
437 Argument = DiagStr;
Mike Stump1eb44332009-09-09 15:08:12 +0000438
John McCall909c1822010-01-14 20:11:39 +0000439 DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
440 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000441 ArgumentLen = DiagStr-Argument;
442 ++DiagStr; // Skip }.
Chris Lattnerf4c83962008-11-19 06:51:40 +0000443 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000444 }
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000446 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner22caddc2008-11-23 09:13:29 +0000447 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000448
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000449 Diagnostic::ArgumentKind Kind = getArgKind(ArgNo);
450
451 switch (Kind) {
Chris Lattner08631c52008-11-23 21:45:46 +0000452 // ---- STRINGS ----
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000453 case Diagnostic::ak_std_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000454 const std::string &S = getArgStdStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000455 assert(ModifierLen == 0 && "No modifiers for strings yet");
456 OutStr.append(S.begin(), S.end());
457 break;
458 }
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000459 case Diagnostic::ak_c_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000460 const char *S = getArgCStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000461 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbare46e3542009-04-20 06:13:16 +0000462
463 // Don't crash if get passed a null pointer by accident.
464 if (!S)
465 S = "(null)";
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000467 OutStr.append(S, S + strlen(S));
468 break;
469 }
Chris Lattner08631c52008-11-23 21:45:46 +0000470 // ---- INTEGERS ----
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000471 case Diagnostic::ak_sint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000472 int Val = getArgSInt(ArgNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000474 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCalle53a44b2010-10-14 01:55:31 +0000475 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen,
476 OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000477 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
478 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000479 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCalle53a44b2010-10-14 01:55:31 +0000480 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
481 OutStr);
John McCall3be16b72010-01-14 00:50:32 +0000482 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
483 HandleOrdinalModifier((unsigned)Val, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000484 } else {
485 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbar23e47c62009-10-17 18:12:14 +0000486 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner30bc9652008-11-19 07:22:31 +0000487 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000488 break;
489 }
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000490 case Diagnostic::ak_uint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000491 unsigned Val = getArgUInt(ArgNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000493 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCall9f286142010-01-13 23:58:20 +0000494 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000495 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
496 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000497 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCalle53a44b2010-10-14 01:55:31 +0000498 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
499 OutStr);
John McCall3be16b72010-01-14 00:50:32 +0000500 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
501 HandleOrdinalModifier(Val, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000502 } else {
503 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbar23e47c62009-10-17 18:12:14 +0000504 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner30bc9652008-11-19 07:22:31 +0000505 }
Chris Lattner22caddc2008-11-23 09:13:29 +0000506 break;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000507 }
Chris Lattner08631c52008-11-23 21:45:46 +0000508 // ---- NAMES and TYPES ----
509 case Diagnostic::ak_identifierinfo: {
Chris Lattner08631c52008-11-23 21:45:46 +0000510 const IdentifierInfo *II = getArgIdentifier(ArgNo);
511 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbare46e3542009-04-20 06:13:16 +0000512
513 // Don't crash if get passed a null pointer by accident.
514 if (!II) {
515 const char *S = "(null)";
516 OutStr.append(S, S + strlen(S));
517 continue;
518 }
519
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000520 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
Chris Lattner08631c52008-11-23 21:45:46 +0000521 break;
522 }
Chris Lattner22caddc2008-11-23 09:13:29 +0000523 case Diagnostic::ak_qualtype:
Chris Lattner011bb4e2008-11-23 20:28:15 +0000524 case Diagnostic::ak_declarationname:
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000525 case Diagnostic::ak_nameddecl:
Douglas Gregordacd4342009-08-26 00:04:55 +0000526 case Diagnostic::ak_nestednamespec:
Douglas Gregor3f093272009-10-13 21:16:44 +0000527 case Diagnostic::ak_declcontext:
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000528 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000529 Modifier, ModifierLen,
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000530 Argument, ArgumentLen,
531 FormattedArgs.data(), FormattedArgs.size(),
532 OutStr);
Chris Lattner22caddc2008-11-23 09:13:29 +0000533 break;
Nico Weber7bfaaae2008-08-10 19:59:06 +0000534 }
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000535
536 // Remember this argument info for subsequent formatting operations. Turn
537 // std::strings into a null terminated string to make it be the same case as
538 // all the other ones.
539 if (Kind != Diagnostic::ak_std_string)
540 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
541 else
542 FormattedArgs.push_back(std::make_pair(Diagnostic::ak_c_string,
543 (intptr_t)getArgStdStr(ArgNo).c_str()));
544
Nico Weber7bfaaae2008-08-10 19:59:06 +0000545 }
Nico Weber7bfaaae2008-08-10 19:59:06 +0000546}
Ted Kremenekcabe6682009-01-23 20:28:53 +0000547
Douglas Gregora88084b2010-02-18 18:08:43 +0000548StoredDiagnostic::StoredDiagnostic() { }
549
550StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level,
551 llvm::StringRef Message)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000552 : Level(Level), Loc(), Message(Message) { }
Douglas Gregora88084b2010-02-18 18:08:43 +0000553
554StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level,
555 const DiagnosticInfo &Info)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000556 : Level(Level) {
557 assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
558 "Valid source location without setting a source manager for diagnostic");
559 if (Info.getLocation().isValid())
560 Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
Douglas Gregora88084b2010-02-18 18:08:43 +0000561 llvm::SmallString<64> Message;
562 Info.FormatDiagnostic(Message);
563 this->Message.assign(Message.begin(), Message.end());
564
565 Ranges.reserve(Info.getNumRanges());
566 for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I)
567 Ranges.push_back(Info.getRange(I));
568
Douglas Gregor849b2432010-03-31 17:46:05 +0000569 FixIts.reserve(Info.getNumFixItHints());
570 for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I)
571 FixIts.push_back(Info.getFixItHint(I));
Douglas Gregora88084b2010-02-18 18:08:43 +0000572}
573
574StoredDiagnostic::~StoredDiagnostic() { }
575
Ted Kremenekcabe6682009-01-23 20:28:53 +0000576/// IncludeInDiagnosticCounts - This method (whose default implementation
577/// returns true) indicates whether the diagnostics handled by this
578/// DiagnosticClient should be included in the number of diagnostics
579/// reported by Diagnostic.
580bool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; }
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000581
582PartialDiagnostic::StorageAllocator::StorageAllocator() {
583 for (unsigned I = 0; I != NumCached; ++I)
584 FreeList[I] = Cached + I;
585 NumFreeListEntries = NumCached;
586}
587
588PartialDiagnostic::StorageAllocator::~StorageAllocator() {
589 assert(NumFreeListEntries == NumCached && "A partial is on the lamb");
590}