blob: a26f49764c3d0d019a2ed0f41612dc3d98ba298a [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
Chris Lattnerf4c83962008-11-19 06:51:40 +0000148
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000149/// ModifierIs - Return true if the specified modifier matches specified string.
150template <std::size_t StrLen>
151static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
152 const char (&Str)[StrLen]) {
153 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
154}
155
John McCall909c1822010-01-14 20:11:39 +0000156/// ScanForward - Scans forward, looking for the given character, skipping
157/// nested clauses and escaped characters.
158static const char *ScanFormat(const char *I, const char *E, char Target) {
159 unsigned Depth = 0;
160
161 for ( ; I != E; ++I) {
162 if (Depth == 0 && *I == Target) return I;
163 if (Depth != 0 && *I == '}') Depth--;
164
165 if (*I == '%') {
166 I++;
167 if (I == E) break;
168
169 // Escaped characters get implicitly skipped here.
170
171 // Format specifier.
172 if (!isdigit(*I) && !ispunct(*I)) {
173 for (I++; I != E && !isdigit(*I) && *I != '{'; I++) ;
174 if (I == E) break;
175 if (*I == '{')
176 Depth++;
177 }
178 }
179 }
180 return E;
181}
182
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000183/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
184/// like this: %select{foo|bar|baz}2. This means that the integer argument
185/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
186/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
187/// This is very useful for certain classes of variant diagnostics.
John McCall9f286142010-01-13 23:58:20 +0000188static void HandleSelectModifier(const DiagnosticInfo &DInfo, unsigned ValNo,
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000189 const char *Argument, unsigned ArgumentLen,
190 llvm::SmallVectorImpl<char> &OutStr) {
191 const char *ArgumentEnd = Argument+ArgumentLen;
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000193 // Skip over 'ValNo' |'s.
194 while (ValNo) {
John McCall909c1822010-01-14 20:11:39 +0000195 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000196 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
197 " larger than the number of options in the diagnostic string!");
198 Argument = NextVal+1; // Skip this string.
199 --ValNo;
200 }
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000202 // Get the end of the value. This is either the } or the |.
John McCall909c1822010-01-14 20:11:39 +0000203 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
John McCall9f286142010-01-13 23:58:20 +0000204
205 // Recursively format the result of the select clause into the output string.
206 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000207}
208
209/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
210/// letter 's' to the string if the value is not 1. This is used in cases like
211/// this: "you idiot, you have %4 parameter%s4!".
212static void HandleIntegerSModifier(unsigned ValNo,
213 llvm::SmallVectorImpl<char> &OutStr) {
214 if (ValNo != 1)
215 OutStr.push_back('s');
216}
217
John McCall3be16b72010-01-14 00:50:32 +0000218/// HandleOrdinalModifier - Handle the integer 'ord' modifier. This
219/// prints the ordinal form of the given integer, with 1 corresponding
220/// to the first ordinal. Currently this is hard-coded to use the
221/// English form.
222static void HandleOrdinalModifier(unsigned ValNo,
223 llvm::SmallVectorImpl<char> &OutStr) {
224 assert(ValNo != 0 && "ValNo must be strictly positive!");
225
226 llvm::raw_svector_ostream Out(OutStr);
227
228 // We could use text forms for the first N ordinals, but the numeric
229 // forms are actually nicer in diagnostics because they stand out.
230 Out << ValNo;
231
232 // It is critically important that we do this perfectly for
233 // user-written sequences with over 100 elements.
234 switch (ValNo % 100) {
235 case 11:
236 case 12:
237 case 13:
238 Out << "th"; return;
239 default:
240 switch (ValNo % 10) {
241 case 1: Out << "st"; return;
242 case 2: Out << "nd"; return;
243 case 3: Out << "rd"; return;
244 default: Out << "th"; return;
245 }
246 }
247}
248
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000249
Sebastian Redle4c452c2008-11-22 13:44:36 +0000250/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000251static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000252 // Programming 101: Parse a decimal number :-)
253 unsigned Val = 0;
254 while (Start != End && *Start >= '0' && *Start <= '9') {
255 Val *= 10;
256 Val += *Start - '0';
257 ++Start;
258 }
259 return Val;
260}
261
262/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000263static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000264 if (*Start != '[') {
265 unsigned Ref = PluralNumber(Start, End);
266 return Ref == Val;
267 }
268
269 ++Start;
270 unsigned Low = PluralNumber(Start, End);
271 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
272 ++Start;
273 unsigned High = PluralNumber(Start, End);
274 assert(*Start == ']' && "Bad plural expression syntax: expected )");
275 ++Start;
276 return Low <= Val && Val <= High;
277}
278
279/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000280static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000281 // Empty condition?
282 if (*Start == ':')
283 return true;
284
285 while (1) {
286 char C = *Start;
287 if (C == '%') {
288 // Modulo expression
289 ++Start;
290 unsigned Arg = PluralNumber(Start, End);
291 assert(*Start == '=' && "Bad plural expression syntax: expected =");
292 ++Start;
293 unsigned ValMod = ValNo % Arg;
294 if (TestPluralRange(ValMod, Start, End))
295 return true;
296 } else {
Sebastian Redle2065322008-11-27 07:28:14 +0000297 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redle4c452c2008-11-22 13:44:36 +0000298 "Bad plural expression syntax: unexpected character");
299 // Range expression
300 if (TestPluralRange(ValNo, Start, End))
301 return true;
302 }
303
304 // Scan for next or-expr part.
305 Start = std::find(Start, End, ',');
Mike Stump1eb44332009-09-09 15:08:12 +0000306 if (Start == End)
Sebastian Redle4c452c2008-11-22 13:44:36 +0000307 break;
308 ++Start;
309 }
310 return false;
311}
312
313/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
314/// for complex plural forms, or in languages where all plurals are complex.
315/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
316/// conditions that are tested in order, the form corresponding to the first
317/// that applies being emitted. The empty condition is always true, making the
318/// last form a default case.
319/// Conditions are simple boolean expressions, where n is the number argument.
320/// Here are the rules.
321/// condition := expression | empty
322/// empty := -> always true
323/// expression := numeric [',' expression] -> logical or
324/// numeric := range -> true if n in range
325/// | '%' number '=' range -> true if n % number in range
326/// range := number
327/// | '[' number ',' number ']' -> ranges are inclusive both ends
328///
329/// Here are some examples from the GNU gettext manual written in this form:
330/// English:
331/// {1:form0|:form1}
332/// Latvian:
333/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
334/// Gaeilge:
335/// {1:form0|2:form1|:form2}
336/// Romanian:
337/// {1:form0|0,%100=[1,19]:form1|:form2}
338/// Lithuanian:
339/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
340/// Russian (requires repeated form):
341/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
342/// Slovak
343/// {1:form0|[2,4]:form1|:form2}
344/// Polish (requires repeated form):
345/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
John McCalle53a44b2010-10-14 01:55:31 +0000346static void HandlePluralModifier(const DiagnosticInfo &DInfo, unsigned ValNo,
Sebastian Redle4c452c2008-11-22 13:44:36 +0000347 const char *Argument, unsigned ArgumentLen,
Chris Lattnerb54b2762009-04-16 05:04:32 +0000348 llvm::SmallVectorImpl<char> &OutStr) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000349 const char *ArgumentEnd = Argument + ArgumentLen;
350 while (1) {
351 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
352 const char *ExprEnd = Argument;
353 while (*ExprEnd != ':') {
354 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
355 ++ExprEnd;
356 }
357 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
358 Argument = ExprEnd + 1;
John McCall909c1822010-01-14 20:11:39 +0000359 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
John McCalle53a44b2010-10-14 01:55:31 +0000360
361 // Recursively format the result of the plural clause into the
362 // output string.
363 DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000364 return;
365 }
John McCall909c1822010-01-14 20:11:39 +0000366 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
Sebastian Redle4c452c2008-11-22 13:44:36 +0000367 }
368}
369
370
Chris Lattnerf4c83962008-11-19 06:51:40 +0000371/// FormatDiagnostic - Format this diagnostic into a string, substituting the
372/// formal arguments into the %0 slots. The result is appended onto the Str
373/// array.
374void DiagnosticInfo::
375FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000376 const char *DiagStr = getDiags()->getDiagnosticIDs()->getDescription(getID());
Chris Lattnerf4c83962008-11-19 06:51:40 +0000377 const char *DiagEnd = DiagStr+strlen(DiagStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000378
John McCall9f286142010-01-13 23:58:20 +0000379 FormatDiagnostic(DiagStr, DiagEnd, OutStr);
380}
381
382void DiagnosticInfo::
383FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
384 llvm::SmallVectorImpl<char> &OutStr) const {
385
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000386 /// FormattedArgs - Keep track of all of the arguments formatted by
387 /// ConvertArgToString and pass them into subsequent calls to
388 /// ConvertArgToString, allowing the implementation to avoid redundancies in
389 /// obvious cases.
390 llvm::SmallVector<Diagnostic::ArgumentValue, 8> FormattedArgs;
391
Chris Lattnerf4c83962008-11-19 06:51:40 +0000392 while (DiagStr != DiagEnd) {
393 if (DiagStr[0] != '%') {
394 // Append non-%0 substrings to Str if we have one.
395 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
396 OutStr.append(DiagStr, StrEnd);
397 DiagStr = StrEnd;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000398 continue;
John McCall909c1822010-01-14 20:11:39 +0000399 } else if (ispunct(DiagStr[1])) {
400 OutStr.push_back(DiagStr[1]); // %% -> %.
Chris Lattnerf4c83962008-11-19 06:51:40 +0000401 DiagStr += 2;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000402 continue;
403 }
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000405 // Skip the %.
406 ++DiagStr;
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000408 // This must be a placeholder for a diagnostic argument. The format for a
409 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
410 // The digit is a number from 0-9 indicating which argument this comes from.
411 // The modifier is a string of digits from the set [-a-z]+, arguments is a
412 // brace enclosed string.
413 const char *Modifier = 0, *Argument = 0;
414 unsigned ModifierLen = 0, ArgumentLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000416 // Check to see if we have a modifier. If so eat it.
417 if (!isdigit(DiagStr[0])) {
418 Modifier = DiagStr;
419 while (DiagStr[0] == '-' ||
420 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
421 ++DiagStr;
422 ModifierLen = DiagStr-Modifier;
Chris Lattnerf4c83962008-11-19 06:51:40 +0000423
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000424 // If we have an argument, get it next.
425 if (DiagStr[0] == '{') {
426 ++DiagStr; // Skip {.
427 Argument = DiagStr;
Mike Stump1eb44332009-09-09 15:08:12 +0000428
John McCall909c1822010-01-14 20:11:39 +0000429 DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
430 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000431 ArgumentLen = DiagStr-Argument;
432 ++DiagStr; // Skip }.
Chris Lattnerf4c83962008-11-19 06:51:40 +0000433 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000434 }
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000436 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner22caddc2008-11-23 09:13:29 +0000437 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000438
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000439 Diagnostic::ArgumentKind Kind = getArgKind(ArgNo);
440
441 switch (Kind) {
Chris Lattner08631c52008-11-23 21:45:46 +0000442 // ---- STRINGS ----
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000443 case Diagnostic::ak_std_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000444 const std::string &S = getArgStdStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000445 assert(ModifierLen == 0 && "No modifiers for strings yet");
446 OutStr.append(S.begin(), S.end());
447 break;
448 }
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000449 case Diagnostic::ak_c_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000450 const char *S = getArgCStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000451 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbare46e3542009-04-20 06:13:16 +0000452
453 // Don't crash if get passed a null pointer by accident.
454 if (!S)
455 S = "(null)";
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000457 OutStr.append(S, S + strlen(S));
458 break;
459 }
Chris Lattner08631c52008-11-23 21:45:46 +0000460 // ---- INTEGERS ----
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000461 case Diagnostic::ak_sint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000462 int Val = getArgSInt(ArgNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000464 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCalle53a44b2010-10-14 01:55:31 +0000465 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen,
466 OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000467 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
468 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000469 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCalle53a44b2010-10-14 01:55:31 +0000470 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
471 OutStr);
John McCall3be16b72010-01-14 00:50:32 +0000472 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
473 HandleOrdinalModifier((unsigned)Val, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000474 } else {
475 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbar23e47c62009-10-17 18:12:14 +0000476 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner30bc9652008-11-19 07:22:31 +0000477 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000478 break;
479 }
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000480 case Diagnostic::ak_uint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000481 unsigned Val = getArgUInt(ArgNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000483 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCall9f286142010-01-13 23:58:20 +0000484 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000485 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
486 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000487 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCalle53a44b2010-10-14 01:55:31 +0000488 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
489 OutStr);
John McCall3be16b72010-01-14 00:50:32 +0000490 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
491 HandleOrdinalModifier(Val, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000492 } else {
493 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbar23e47c62009-10-17 18:12:14 +0000494 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner30bc9652008-11-19 07:22:31 +0000495 }
Chris Lattner22caddc2008-11-23 09:13:29 +0000496 break;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000497 }
Chris Lattner08631c52008-11-23 21:45:46 +0000498 // ---- NAMES and TYPES ----
499 case Diagnostic::ak_identifierinfo: {
Chris Lattner08631c52008-11-23 21:45:46 +0000500 const IdentifierInfo *II = getArgIdentifier(ArgNo);
501 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbare46e3542009-04-20 06:13:16 +0000502
503 // Don't crash if get passed a null pointer by accident.
504 if (!II) {
505 const char *S = "(null)";
506 OutStr.append(S, S + strlen(S));
507 continue;
508 }
509
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000510 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
Chris Lattner08631c52008-11-23 21:45:46 +0000511 break;
512 }
Chris Lattner22caddc2008-11-23 09:13:29 +0000513 case Diagnostic::ak_qualtype:
Chris Lattner011bb4e2008-11-23 20:28:15 +0000514 case Diagnostic::ak_declarationname:
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000515 case Diagnostic::ak_nameddecl:
Douglas Gregordacd4342009-08-26 00:04:55 +0000516 case Diagnostic::ak_nestednamespec:
Douglas Gregor3f093272009-10-13 21:16:44 +0000517 case Diagnostic::ak_declcontext:
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000518 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000519 Modifier, ModifierLen,
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000520 Argument, ArgumentLen,
521 FormattedArgs.data(), FormattedArgs.size(),
522 OutStr);
Chris Lattner22caddc2008-11-23 09:13:29 +0000523 break;
Nico Weber7bfaaae2008-08-10 19:59:06 +0000524 }
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000525
526 // Remember this argument info for subsequent formatting operations. Turn
527 // std::strings into a null terminated string to make it be the same case as
528 // all the other ones.
529 if (Kind != Diagnostic::ak_std_string)
530 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
531 else
532 FormattedArgs.push_back(std::make_pair(Diagnostic::ak_c_string,
533 (intptr_t)getArgStdStr(ArgNo).c_str()));
534
Nico Weber7bfaaae2008-08-10 19:59:06 +0000535 }
Nico Weber7bfaaae2008-08-10 19:59:06 +0000536}
Ted Kremenekcabe6682009-01-23 20:28:53 +0000537
Douglas Gregora88084b2010-02-18 18:08:43 +0000538StoredDiagnostic::StoredDiagnostic() { }
539
540StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level,
541 llvm::StringRef Message)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000542 : Level(Level), Loc(), Message(Message) { }
Douglas Gregora88084b2010-02-18 18:08:43 +0000543
544StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level,
545 const DiagnosticInfo &Info)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000546 : Level(Level) {
547 assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
548 "Valid source location without setting a source manager for diagnostic");
549 if (Info.getLocation().isValid())
550 Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
Douglas Gregora88084b2010-02-18 18:08:43 +0000551 llvm::SmallString<64> Message;
552 Info.FormatDiagnostic(Message);
553 this->Message.assign(Message.begin(), Message.end());
554
555 Ranges.reserve(Info.getNumRanges());
556 for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I)
557 Ranges.push_back(Info.getRange(I));
558
Douglas Gregor849b2432010-03-31 17:46:05 +0000559 FixIts.reserve(Info.getNumFixItHints());
560 for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I)
561 FixIts.push_back(Info.getFixItHint(I));
Douglas Gregora88084b2010-02-18 18:08:43 +0000562}
563
564StoredDiagnostic::~StoredDiagnostic() { }
565
Ted Kremenekcabe6682009-01-23 20:28:53 +0000566/// IncludeInDiagnosticCounts - This method (whose default implementation
567/// returns true) indicates whether the diagnostics handled by this
568/// DiagnosticClient should be included in the number of diagnostics
569/// reported by Diagnostic.
570bool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; }
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000571
572PartialDiagnostic::StorageAllocator::StorageAllocator() {
573 for (unsigned I = 0; I != NumCached; ++I)
574 FreeList[I] = Cached + I;
575 NumFreeListEntries = NumCached;
576}
577
578PartialDiagnostic::StorageAllocator::~StorageAllocator() {
579 assert(NumFreeListEntries == NumCached && "A partial is on the lamb");
580}