blob: d5ccd64f875bed7cee5e3d1fb5590fc7c0beb66c [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"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000017#include "llvm/ADT/SmallString.h"
Daniel Dunbar23e47c62009-10-17 18:12:14 +000018#include "llvm/Support/raw_ostream.h"
Ted Kremenek03201fb2011-03-21 18:40:07 +000019#include "llvm/Support/CrashRecoveryContext.h"
20
Reid Spencer5f016e22007-07-11 17:01:13 +000021using namespace clang;
22
David Blaikied6471f72011-09-25 23:23:43 +000023static void DummyArgToStringFn(DiagnosticsEngine::ArgumentKind AK, intptr_t QT,
Chris Lattner3fdf4b02008-11-23 09:21:17 +000024 const char *Modifier, unsigned ML,
25 const char *Argument, unsigned ArgLen,
David Blaikied6471f72011-09-25 23:23:43 +000026 const DiagnosticsEngine::ArgumentValue *PrevArgs,
Chris Lattnerb54d8af2009-10-20 05:25:22 +000027 unsigned NumPrevArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +000028 SmallVectorImpl<char> &Output,
Chandler Carruth0673cb32011-07-11 17:49:21 +000029 void *Cookie,
Bill Wendling341785e2012-02-22 09:51:33 +000030 ArrayRef<intptr_t> QualTypeVals) {
Chris Lattner3fdf4b02008-11-23 09:21:17 +000031 const char *Str = "<can't format argument>";
Chris Lattner22caddc2008-11-23 09:13:29 +000032 Output.append(Str, Str+strlen(Str));
33}
34
35
David Blaikied6471f72011-09-25 23:23:43 +000036DiagnosticsEngine::DiagnosticsEngine(
Dylan Noblesmithc93dc782012-02-20 14:00:23 +000037 const IntrusiveRefCntPtr<DiagnosticIDs> &diags,
David Blaikie78ad0b92011-09-25 23:39:51 +000038 DiagnosticConsumer *client, bool ShouldOwnClient)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000039 : Diags(diags), Client(client), OwnsDiagClient(ShouldOwnClient),
40 SourceMgr(0) {
Chris Lattner3fdf4b02008-11-23 09:21:17 +000041 ArgToStringFn = DummyArgToStringFn;
Chris Lattner92dd3862009-02-19 23:53:20 +000042 ArgToStringCookie = 0;
Mike Stump1eb44332009-09-09 15:08:12 +000043
Douglas Gregorcc5888d2010-07-31 00:40:00 +000044 AllExtensionsSilenced = 0;
45 IgnoreAllWarnings = false;
46 WarningsAsErrors = false;
Ted Kremenek1e473cc2011-08-18 01:12:56 +000047 EnableAllWarnings = false;
Douglas Gregorcc5888d2010-07-31 00:40:00 +000048 ErrorsAsFatal = false;
49 SuppressSystemWarnings = false;
50 SuppressAllDiagnostics = false;
Richard Trieu246b6aa2012-06-26 18:18:47 +000051 ElideType = true;
52 PrintTemplateTree = false;
53 ShowColors = false;
Douglas Gregorcc5888d2010-07-31 00:40:00 +000054 ShowOverloads = Ovl_All;
55 ExtBehavior = Ext_Ignore;
56
57 ErrorLimit = 0;
58 TemplateBacktraceLimit = 0;
Richard Smith08d6e032011-12-16 19:06:07 +000059 ConstexprBacktraceLimit = 0;
Douglas Gregorcc5888d2010-07-31 00:40:00 +000060
Douglas Gregorabc563f2010-07-19 21:46:24 +000061 Reset();
Reid Spencer5f016e22007-07-11 17:01:13 +000062}
63
David Blaikied6471f72011-09-25 23:23:43 +000064DiagnosticsEngine::~DiagnosticsEngine() {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000065 if (OwnsDiagClient)
66 delete Client;
Chris Lattner182745a2007-12-02 01:09:57 +000067}
68
David Blaikie78ad0b92011-09-25 23:39:51 +000069void DiagnosticsEngine::setClient(DiagnosticConsumer *client,
David Blaikied6471f72011-09-25 23:23:43 +000070 bool ShouldOwnClient) {
Douglas Gregor4f5e21e2011-01-31 22:04:05 +000071 if (OwnsDiagClient && Client)
72 delete Client;
73
74 Client = client;
75 OwnsDiagClient = ShouldOwnClient;
76}
Chris Lattner04ae2df2009-07-12 21:18:45 +000077
David Blaikied6471f72011-09-25 23:23:43 +000078void DiagnosticsEngine::pushMappings(SourceLocation Loc) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +000079 DiagStateOnPushStack.push_back(GetCurDiagState());
Chris Lattner04ae2df2009-07-12 21:18:45 +000080}
81
David Blaikied6471f72011-09-25 23:23:43 +000082bool DiagnosticsEngine::popMappings(SourceLocation Loc) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +000083 if (DiagStateOnPushStack.empty())
Chris Lattner04ae2df2009-07-12 21:18:45 +000084 return false;
85
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +000086 if (DiagStateOnPushStack.back() != GetCurDiagState()) {
87 // State changed at some point between push/pop.
88 PushDiagStatePoint(DiagStateOnPushStack.back(), Loc);
89 }
90 DiagStateOnPushStack.pop_back();
Chris Lattner04ae2df2009-07-12 21:18:45 +000091 return true;
92}
93
David Blaikied6471f72011-09-25 23:23:43 +000094void DiagnosticsEngine::Reset() {
Douglas Gregorabc563f2010-07-19 21:46:24 +000095 ErrorOccurred = false;
96 FatalErrorOccurred = false;
Douglas Gregor85bea972011-07-06 17:40:26 +000097 UnrecoverableErrorOccurred = false;
Douglas Gregorabc563f2010-07-19 21:46:24 +000098
99 NumWarnings = 0;
100 NumErrors = 0;
101 NumErrorsSuppressed = 0;
Argyrios Kyrtzidisc0a575f2011-07-29 01:25:44 +0000102 TrapNumErrorsOccurred = 0;
103 TrapNumUnrecoverableErrorsOccurred = 0;
Douglas Gregor85bea972011-07-06 17:40:26 +0000104
Douglas Gregorabc563f2010-07-19 21:46:24 +0000105 CurDiagID = ~0U;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000106 // Set LastDiagLevel to an "unset" state. If we set it to 'Ignored', notes
David Blaikied6471f72011-09-25 23:23:43 +0000107 // using a DiagnosticsEngine associated to a translation unit that follow
108 // diagnostics from a DiagnosticsEngine associated to anoter t.u. will not be
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000109 // displayed.
110 LastDiagLevel = (DiagnosticIDs::Level)-1;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000111 DelayedDiagID = 0;
Argyrios Kyrtzidisdc0a2da2011-03-26 18:58:17 +0000112
113 // Clear state related to #pragma diagnostic.
114 DiagStates.clear();
115 DiagStatePoints.clear();
116 DiagStateOnPushStack.clear();
117
118 // Create a DiagState and DiagStatePoint representing diagnostic changes
119 // through command-line.
120 DiagStates.push_back(DiagState());
121 PushDiagStatePoint(&DiagStates.back(), SourceLocation());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000122}
Reid Spencer5f016e22007-07-11 17:01:13 +0000123
David Blaikied6471f72011-09-25 23:23:43 +0000124void DiagnosticsEngine::SetDelayedDiagnostic(unsigned DiagID, StringRef Arg1,
Chad Rosierdfaee492012-02-07 23:24:49 +0000125 StringRef Arg2) {
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000126 if (DelayedDiagID)
127 return;
128
129 DelayedDiagID = DiagID;
Douglas Gregor9e2dac92010-03-22 15:47:45 +0000130 DelayedDiagArg1 = Arg1.str();
131 DelayedDiagArg2 = Arg2.str();
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000132}
133
David Blaikied6471f72011-09-25 23:23:43 +0000134void DiagnosticsEngine::ReportDelayed() {
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000135 Report(DelayedDiagID) << DelayedDiagArg1 << DelayedDiagArg2;
136 DelayedDiagID = 0;
137 DelayedDiagArg1.clear();
138 DelayedDiagArg2.clear();
139}
140
David Blaikied6471f72011-09-25 23:23:43 +0000141DiagnosticsEngine::DiagStatePointsTy::iterator
142DiagnosticsEngine::GetDiagStatePointForLoc(SourceLocation L) const {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000143 assert(!DiagStatePoints.empty());
144 assert(DiagStatePoints.front().Loc.isInvalid() &&
145 "Should have created a DiagStatePoint for command-line");
146
147 FullSourceLoc Loc(L, *SourceMgr);
148 if (Loc.isInvalid())
149 return DiagStatePoints.end() - 1;
150
151 DiagStatePointsTy::iterator Pos = DiagStatePoints.end();
152 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
153 if (LastStateChangePos.isValid() &&
154 Loc.isBeforeInTranslationUnitThan(LastStateChangePos))
155 Pos = std::upper_bound(DiagStatePoints.begin(), DiagStatePoints.end(),
156 DiagStatePoint(0, Loc));
157 --Pos;
158 return Pos;
159}
160
David Blaikied6471f72011-09-25 23:23:43 +0000161void DiagnosticsEngine::setDiagnosticMapping(diag::kind Diag, diag::Mapping Map,
Chad Rosierdfaee492012-02-07 23:24:49 +0000162 SourceLocation L) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000163 assert(Diag < diag::DIAG_UPPER_LIMIT &&
164 "Can only map builtin diagnostics");
165 assert((Diags->isBuiltinWarningOrExtension(Diag) ||
166 (Map == diag::MAP_FATAL || Map == diag::MAP_ERROR)) &&
167 "Cannot map errors into warnings!");
168 assert(!DiagStatePoints.empty());
169
170 FullSourceLoc Loc(L, *SourceMgr);
171 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
Chad Rosier7a0a31c2012-02-03 01:49:51 +0000172 // Don't allow a mapping to a warning override an error/fatal mapping.
173 if (Map == diag::MAP_WARNING) {
174 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(Diag);
175 if (Info.getMapping() == diag::MAP_ERROR ||
176 Info.getMapping() == diag::MAP_FATAL)
177 Map = Info.getMapping();
178 }
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +0000179 DiagnosticMappingInfo MappingInfo = makeMappingInfo(Map, L);
Daniel Dunbar53201a82011-10-04 21:17:24 +0000180
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000181 // Common case; setting all the diagnostics of a group in one place.
182 if (Loc.isInvalid() || Loc == LastStateChangePos) {
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000183 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000184 return;
185 }
186
187 // Another common case; modifying diagnostic state in a source location
188 // after the previous one.
189 if ((Loc.isValid() && LastStateChangePos.isInvalid()) ||
190 LastStateChangePos.isBeforeInTranslationUnitThan(Loc)) {
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000191 // A diagnostic pragma occurred, create a new DiagState initialized with
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000192 // the current one and a new DiagStatePoint to record at which location
193 // the new state became active.
194 DiagStates.push_back(*GetCurDiagState());
195 PushDiagStatePoint(&DiagStates.back(), Loc);
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000196 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000197 return;
198 }
199
200 // We allow setting the diagnostic state in random source order for
201 // completeness but it should not be actually happening in normal practice.
202
203 DiagStatePointsTy::iterator Pos = GetDiagStatePointForLoc(Loc);
204 assert(Pos != DiagStatePoints.end());
205
206 // Update all diagnostic states that are active after the given location.
207 for (DiagStatePointsTy::iterator
208 I = Pos+1, E = DiagStatePoints.end(); I != E; ++I) {
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000209 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000210 }
211
212 // If the location corresponds to an existing point, just update its state.
213 if (Pos->Loc == Loc) {
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000214 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000215 return;
216 }
217
218 // Create a new state/point and fit it into the vector of DiagStatePoints
219 // so that the vector is always ordered according to location.
220 Pos->Loc.isBeforeInTranslationUnitThan(Loc);
221 DiagStates.push_back(*Pos->State);
222 DiagState *NewState = &DiagStates.back();
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000223 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000224 DiagStatePoints.insert(Pos+1, DiagStatePoint(NewState,
225 FullSourceLoc(Loc, *SourceMgr)));
226}
227
Daniel Dunbar3f839462011-09-29 01:47:16 +0000228bool DiagnosticsEngine::setDiagnosticGroupMapping(
229 StringRef Group, diag::Mapping Map, SourceLocation Loc)
230{
231 // Get the diagnostics in this group.
232 llvm::SmallVector<diag::kind, 8> GroupDiags;
233 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
234 return true;
235
236 // Set the mapping.
237 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i)
238 setDiagnosticMapping(GroupDiags[i], Map, Loc);
239
240 return false;
241}
242
Chad Rosier3f225092012-02-07 19:55:45 +0000243void DiagnosticsEngine::setDiagnosticWarningAsError(diag::kind Diag,
244 bool Enabled) {
245 // If we are enabling this feature, just set the diagnostic mappings to map to
246 // errors.
247 if (Enabled)
248 setDiagnosticMapping(Diag, diag::MAP_ERROR, SourceLocation());
249
250 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
251 // potentially downgrade anything already mapped to be a warning.
252 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(Diag);
253
254 if (Info.getMapping() == diag::MAP_ERROR ||
255 Info.getMapping() == diag::MAP_FATAL)
256 Info.setMapping(diag::MAP_WARNING);
257
258 Info.setNoWarningAsError(true);
259}
260
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000261bool DiagnosticsEngine::setDiagnosticGroupWarningAsError(StringRef Group,
262 bool Enabled) {
Daniel Dunbara5e41332011-09-29 01:52:06 +0000263 // If we are enabling this feature, just set the diagnostic mappings to map to
264 // errors.
265 if (Enabled)
266 return setDiagnosticGroupMapping(Group, diag::MAP_ERROR);
267
268 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
269 // potentially downgrade anything already mapped to be a warning.
270
271 // Get the diagnostics in this group.
272 llvm::SmallVector<diag::kind, 8> GroupDiags;
273 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
274 return true;
275
276 // Perform the mapping change.
277 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i) {
278 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(
279 GroupDiags[i]);
280
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000281 if (Info.getMapping() == diag::MAP_ERROR ||
282 Info.getMapping() == diag::MAP_FATAL)
283 Info.setMapping(diag::MAP_WARNING);
284
Daniel Dunbara5e41332011-09-29 01:52:06 +0000285 Info.setNoWarningAsError(true);
286 }
287
288 return false;
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000289}
290
Chad Rosier3f225092012-02-07 19:55:45 +0000291void DiagnosticsEngine::setDiagnosticErrorAsFatal(diag::kind Diag,
292 bool Enabled) {
293 // If we are enabling this feature, just set the diagnostic mappings to map to
294 // errors.
295 if (Enabled)
296 setDiagnosticMapping(Diag, diag::MAP_FATAL, SourceLocation());
297
298 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
299 // potentially downgrade anything already mapped to be a warning.
300 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(Diag);
301
302 if (Info.getMapping() == diag::MAP_FATAL)
303 Info.setMapping(diag::MAP_ERROR);
304
305 Info.setNoErrorAsFatal(true);
306}
307
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000308bool DiagnosticsEngine::setDiagnosticGroupErrorAsFatal(StringRef Group,
309 bool Enabled) {
Daniel Dunbara5e41332011-09-29 01:52:06 +0000310 // If we are enabling this feature, just set the diagnostic mappings to map to
311 // fatal errors.
312 if (Enabled)
313 return setDiagnosticGroupMapping(Group, diag::MAP_FATAL);
314
315 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
316 // potentially downgrade anything already mapped to be an error.
317
318 // Get the diagnostics in this group.
319 llvm::SmallVector<diag::kind, 8> GroupDiags;
320 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
321 return true;
322
323 // Perform the mapping change.
324 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i) {
325 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(
326 GroupDiags[i]);
327
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000328 if (Info.getMapping() == diag::MAP_FATAL)
329 Info.setMapping(diag::MAP_ERROR);
330
Daniel Dunbara5e41332011-09-29 01:52:06 +0000331 Info.setNoErrorAsFatal(true);
332 }
333
334 return false;
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000335}
336
Argyrios Kyrtzidis82e64112012-01-28 04:35:52 +0000337void DiagnosticsEngine::setMappingToAllDiagnostics(diag::Mapping Map,
Argyrios Kyrtzidis11583c72012-01-27 06:15:43 +0000338 SourceLocation Loc) {
339 // Get all the diagnostics.
340 llvm::SmallVector<diag::kind, 64> AllDiags;
341 Diags->getAllDiagnostics(AllDiags);
342
343 // Set the mapping.
344 for (unsigned i = 0, e = AllDiags.size(); i != e; ++i)
345 if (Diags->isBuiltinWarningOrExtension(AllDiags[i]))
346 setDiagnosticMapping(AllDiags[i], Map, Loc);
Argyrios Kyrtzidis11583c72012-01-27 06:15:43 +0000347}
348
David Blaikied6471f72011-09-25 23:23:43 +0000349void DiagnosticsEngine::Report(const StoredDiagnostic &storedDiag) {
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000350 assert(CurDiagID == ~0U && "Multiple diagnostics in flight at once!");
351
352 CurDiagLoc = storedDiag.getLocation();
353 CurDiagID = storedDiag.getID();
354 NumDiagArgs = 0;
355
356 NumDiagRanges = storedDiag.range_size();
Daniel Dunbar981e2792012-03-13 18:21:17 +0000357 assert(NumDiagRanges < DiagnosticsEngine::MaxRanges &&
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000358 "Too many arguments to diagnostic!");
359 unsigned i = 0;
360 for (StoredDiagnostic::range_iterator
361 RI = storedDiag.range_begin(),
362 RE = storedDiag.range_end(); RI != RE; ++RI)
363 DiagRanges[i++] = *RI;
364
Daniel Dunbar981e2792012-03-13 18:21:17 +0000365 assert(NumDiagRanges < DiagnosticsEngine::MaxFixItHints &&
366 "Too many arguments to diagnostic!");
367 NumDiagFixItHints = 0;
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000368 for (StoredDiagnostic::fixit_iterator
369 FI = storedDiag.fixit_begin(),
370 FE = storedDiag.fixit_end(); FI != FE; ++FI)
Daniel Dunbar981e2792012-03-13 18:21:17 +0000371 DiagFixItHints[NumDiagFixItHints++] = *FI;
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000372
David Blaikie78ad0b92011-09-25 23:39:51 +0000373 assert(Client && "DiagnosticConsumer not set!");
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000374 Level DiagLevel = storedDiag.getLevel();
David Blaikie40847cf2011-09-26 01:18:08 +0000375 Diagnostic Info(this, storedDiag.getMessage());
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000376 Client->HandleDiagnostic(DiagLevel, Info);
377 if (Client->IncludeInDiagnosticCounts()) {
David Blaikied6471f72011-09-25 23:23:43 +0000378 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000379 ++NumWarnings;
380 }
381
382 CurDiagID = ~0U;
383}
384
Daniel Dunbar3054f092012-03-13 21:02:14 +0000385bool DiagnosticsEngine::EmitCurrentDiagnostic() {
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000386 // Process the diagnostic, sending the accumulated information to the
David Blaikie78ad0b92011-09-25 23:39:51 +0000387 // DiagnosticConsumer.
Daniel Dunbar3054f092012-03-13 21:02:14 +0000388 bool Emitted = ProcessDiag();
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000389
390 // Clear out the current diagnostic object.
Daniel Dunbar3054f092012-03-13 21:02:14 +0000391 unsigned DiagID = CurDiagID;
392 Clear();
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000393
394 // If there was a delayed diagnostic, emit it now.
Daniel Dunbar3054f092012-03-13 21:02:14 +0000395 if (DelayedDiagID && DelayedDiagID != DiagID)
396 ReportDelayed();
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000397
398 return Emitted;
399}
400
Nico Weber7bfaaae2008-08-10 19:59:06 +0000401
David Blaikie78ad0b92011-09-25 23:39:51 +0000402DiagnosticConsumer::~DiagnosticConsumer() {}
Nico Weber7bfaaae2008-08-10 19:59:06 +0000403
David Blaikie78ad0b92011-09-25 23:39:51 +0000404void DiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
David Blaikie40847cf2011-09-26 01:18:08 +0000405 const Diagnostic &Info) {
Argyrios Kyrtzidisf2224d82010-11-18 20:06:46 +0000406 if (!IncludeInDiagnosticCounts())
407 return;
408
David Blaikied6471f72011-09-25 23:23:43 +0000409 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidisf2224d82010-11-18 20:06:46 +0000410 ++NumWarnings;
David Blaikied6471f72011-09-25 23:23:43 +0000411 else if (DiagLevel >= DiagnosticsEngine::Error)
Argyrios Kyrtzidisf2224d82010-11-18 20:06:46 +0000412 ++NumErrors;
413}
Chris Lattnerf4c83962008-11-19 06:51:40 +0000414
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000415/// ModifierIs - Return true if the specified modifier matches specified string.
416template <std::size_t StrLen>
417static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
418 const char (&Str)[StrLen]) {
419 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
420}
421
John McCall909c1822010-01-14 20:11:39 +0000422/// ScanForward - Scans forward, looking for the given character, skipping
423/// nested clauses and escaped characters.
424static const char *ScanFormat(const char *I, const char *E, char Target) {
425 unsigned Depth = 0;
426
427 for ( ; I != E; ++I) {
428 if (Depth == 0 && *I == Target) return I;
429 if (Depth != 0 && *I == '}') Depth--;
430
431 if (*I == '%') {
432 I++;
433 if (I == E) break;
434
435 // Escaped characters get implicitly skipped here.
436
437 // Format specifier.
438 if (!isdigit(*I) && !ispunct(*I)) {
439 for (I++; I != E && !isdigit(*I) && *I != '{'; I++) ;
440 if (I == E) break;
441 if (*I == '{')
442 Depth++;
443 }
444 }
445 }
446 return E;
447}
448
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000449/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
450/// like this: %select{foo|bar|baz}2. This means that the integer argument
451/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
452/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
453/// This is very useful for certain classes of variant diagnostics.
David Blaikie40847cf2011-09-26 01:18:08 +0000454static void HandleSelectModifier(const Diagnostic &DInfo, unsigned ValNo,
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000455 const char *Argument, unsigned ArgumentLen,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000456 SmallVectorImpl<char> &OutStr) {
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000457 const char *ArgumentEnd = Argument+ArgumentLen;
Mike Stump1eb44332009-09-09 15:08:12 +0000458
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000459 // Skip over 'ValNo' |'s.
460 while (ValNo) {
John McCall909c1822010-01-14 20:11:39 +0000461 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000462 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
463 " larger than the number of options in the diagnostic string!");
464 Argument = NextVal+1; // Skip this string.
465 --ValNo;
466 }
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000468 // Get the end of the value. This is either the } or the |.
John McCall909c1822010-01-14 20:11:39 +0000469 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
John McCall9f286142010-01-13 23:58:20 +0000470
471 // Recursively format the result of the select clause into the output string.
472 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000473}
474
475/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
476/// letter 's' to the string if the value is not 1. This is used in cases like
477/// this: "you idiot, you have %4 parameter%s4!".
478static void HandleIntegerSModifier(unsigned ValNo,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000479 SmallVectorImpl<char> &OutStr) {
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000480 if (ValNo != 1)
481 OutStr.push_back('s');
482}
483
John McCall3be16b72010-01-14 00:50:32 +0000484/// HandleOrdinalModifier - Handle the integer 'ord' modifier. This
485/// prints the ordinal form of the given integer, with 1 corresponding
486/// to the first ordinal. Currently this is hard-coded to use the
487/// English form.
488static void HandleOrdinalModifier(unsigned ValNo,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000489 SmallVectorImpl<char> &OutStr) {
John McCall3be16b72010-01-14 00:50:32 +0000490 assert(ValNo != 0 && "ValNo must be strictly positive!");
491
492 llvm::raw_svector_ostream Out(OutStr);
493
494 // We could use text forms for the first N ordinals, but the numeric
495 // forms are actually nicer in diagnostics because they stand out.
496 Out << ValNo;
497
498 // It is critically important that we do this perfectly for
499 // user-written sequences with over 100 elements.
500 switch (ValNo % 100) {
501 case 11:
502 case 12:
503 case 13:
504 Out << "th"; return;
505 default:
506 switch (ValNo % 10) {
507 case 1: Out << "st"; return;
508 case 2: Out << "nd"; return;
509 case 3: Out << "rd"; return;
510 default: Out << "th"; return;
511 }
512 }
513}
514
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000515
Sebastian Redle4c452c2008-11-22 13:44:36 +0000516/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000517static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000518 // Programming 101: Parse a decimal number :-)
519 unsigned Val = 0;
520 while (Start != End && *Start >= '0' && *Start <= '9') {
521 Val *= 10;
522 Val += *Start - '0';
523 ++Start;
524 }
525 return Val;
526}
527
528/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000529static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000530 if (*Start != '[') {
531 unsigned Ref = PluralNumber(Start, End);
532 return Ref == Val;
533 }
534
535 ++Start;
536 unsigned Low = PluralNumber(Start, End);
537 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
538 ++Start;
539 unsigned High = PluralNumber(Start, End);
540 assert(*Start == ']' && "Bad plural expression syntax: expected )");
541 ++Start;
542 return Low <= Val && Val <= High;
543}
544
545/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000546static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000547 // Empty condition?
548 if (*Start == ':')
549 return true;
550
551 while (1) {
552 char C = *Start;
553 if (C == '%') {
554 // Modulo expression
555 ++Start;
556 unsigned Arg = PluralNumber(Start, End);
557 assert(*Start == '=' && "Bad plural expression syntax: expected =");
558 ++Start;
559 unsigned ValMod = ValNo % Arg;
560 if (TestPluralRange(ValMod, Start, End))
561 return true;
562 } else {
Sebastian Redle2065322008-11-27 07:28:14 +0000563 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redle4c452c2008-11-22 13:44:36 +0000564 "Bad plural expression syntax: unexpected character");
565 // Range expression
566 if (TestPluralRange(ValNo, Start, End))
567 return true;
568 }
569
570 // Scan for next or-expr part.
571 Start = std::find(Start, End, ',');
Mike Stump1eb44332009-09-09 15:08:12 +0000572 if (Start == End)
Sebastian Redle4c452c2008-11-22 13:44:36 +0000573 break;
574 ++Start;
575 }
576 return false;
577}
578
579/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
580/// for complex plural forms, or in languages where all plurals are complex.
581/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
582/// conditions that are tested in order, the form corresponding to the first
583/// that applies being emitted. The empty condition is always true, making the
584/// last form a default case.
585/// Conditions are simple boolean expressions, where n is the number argument.
586/// Here are the rules.
587/// condition := expression | empty
588/// empty := -> always true
589/// expression := numeric [',' expression] -> logical or
590/// numeric := range -> true if n in range
591/// | '%' number '=' range -> true if n % number in range
592/// range := number
593/// | '[' number ',' number ']' -> ranges are inclusive both ends
594///
595/// Here are some examples from the GNU gettext manual written in this form:
596/// English:
597/// {1:form0|:form1}
598/// Latvian:
599/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
600/// Gaeilge:
601/// {1:form0|2:form1|:form2}
602/// Romanian:
603/// {1:form0|0,%100=[1,19]:form1|:form2}
604/// Lithuanian:
605/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
606/// Russian (requires repeated form):
607/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
608/// Slovak
609/// {1:form0|[2,4]:form1|:form2}
610/// Polish (requires repeated form):
611/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
David Blaikie40847cf2011-09-26 01:18:08 +0000612static void HandlePluralModifier(const Diagnostic &DInfo, unsigned ValNo,
Sebastian Redle4c452c2008-11-22 13:44:36 +0000613 const char *Argument, unsigned ArgumentLen,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000614 SmallVectorImpl<char> &OutStr) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000615 const char *ArgumentEnd = Argument + ArgumentLen;
616 while (1) {
617 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
618 const char *ExprEnd = Argument;
619 while (*ExprEnd != ':') {
620 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
621 ++ExprEnd;
622 }
623 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
624 Argument = ExprEnd + 1;
John McCall909c1822010-01-14 20:11:39 +0000625 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
John McCalle53a44b2010-10-14 01:55:31 +0000626
627 // Recursively format the result of the plural clause into the
628 // output string.
629 DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000630 return;
631 }
John McCall909c1822010-01-14 20:11:39 +0000632 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
Sebastian Redle4c452c2008-11-22 13:44:36 +0000633 }
634}
635
636
Chris Lattnerf4c83962008-11-19 06:51:40 +0000637/// FormatDiagnostic - Format this diagnostic into a string, substituting the
638/// formal arguments into the %0 slots. The result is appended onto the Str
639/// array.
David Blaikie40847cf2011-09-26 01:18:08 +0000640void Diagnostic::
Chris Lattner5f9e2722011-07-23 10:55:15 +0000641FormatDiagnostic(SmallVectorImpl<char> &OutStr) const {
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000642 if (!StoredDiagMessage.empty()) {
643 OutStr.append(StoredDiagMessage.begin(), StoredDiagMessage.end());
644 return;
645 }
646
Chris Lattner5f9e2722011-07-23 10:55:15 +0000647 StringRef Diag =
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000648 getDiags()->getDiagnosticIDs()->getDescription(getID());
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000650 FormatDiagnostic(Diag.begin(), Diag.end(), OutStr);
John McCall9f286142010-01-13 23:58:20 +0000651}
652
David Blaikie40847cf2011-09-26 01:18:08 +0000653void Diagnostic::
John McCall9f286142010-01-13 23:58:20 +0000654FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000655 SmallVectorImpl<char> &OutStr) const {
John McCall9f286142010-01-13 23:58:20 +0000656
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000657 /// FormattedArgs - Keep track of all of the arguments formatted by
658 /// ConvertArgToString and pass them into subsequent calls to
659 /// ConvertArgToString, allowing the implementation to avoid redundancies in
660 /// obvious cases.
David Blaikied6471f72011-09-25 23:23:43 +0000661 SmallVector<DiagnosticsEngine::ArgumentValue, 8> FormattedArgs;
Chandler Carruth0673cb32011-07-11 17:49:21 +0000662
663 /// QualTypeVals - Pass a vector of arrays so that QualType names can be
664 /// compared to see if more information is needed to be printed.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000665 SmallVector<intptr_t, 2> QualTypeVals;
Richard Trieu246b6aa2012-06-26 18:18:47 +0000666 SmallVector<char, 64> Tree;
667
Chandler Carruth0673cb32011-07-11 17:49:21 +0000668 for (unsigned i = 0, e = getNumArgs(); i < e; ++i)
David Blaikied6471f72011-09-25 23:23:43 +0000669 if (getArgKind(i) == DiagnosticsEngine::ak_qualtype)
Chandler Carruth0673cb32011-07-11 17:49:21 +0000670 QualTypeVals.push_back(getRawArg(i));
671
Chris Lattnerf4c83962008-11-19 06:51:40 +0000672 while (DiagStr != DiagEnd) {
673 if (DiagStr[0] != '%') {
674 // Append non-%0 substrings to Str if we have one.
675 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
676 OutStr.append(DiagStr, StrEnd);
677 DiagStr = StrEnd;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000678 continue;
John McCall909c1822010-01-14 20:11:39 +0000679 } else if (ispunct(DiagStr[1])) {
680 OutStr.push_back(DiagStr[1]); // %% -> %.
Chris Lattnerf4c83962008-11-19 06:51:40 +0000681 DiagStr += 2;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000682 continue;
683 }
Mike Stump1eb44332009-09-09 15:08:12 +0000684
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000685 // Skip the %.
686 ++DiagStr;
Mike Stump1eb44332009-09-09 15:08:12 +0000687
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000688 // This must be a placeholder for a diagnostic argument. The format for a
689 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
690 // The digit is a number from 0-9 indicating which argument this comes from.
691 // The modifier is a string of digits from the set [-a-z]+, arguments is a
692 // brace enclosed string.
693 const char *Modifier = 0, *Argument = 0;
694 unsigned ModifierLen = 0, ArgumentLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000695
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000696 // Check to see if we have a modifier. If so eat it.
697 if (!isdigit(DiagStr[0])) {
698 Modifier = DiagStr;
699 while (DiagStr[0] == '-' ||
700 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
701 ++DiagStr;
702 ModifierLen = DiagStr-Modifier;
Chris Lattnerf4c83962008-11-19 06:51:40 +0000703
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000704 // If we have an argument, get it next.
705 if (DiagStr[0] == '{') {
706 ++DiagStr; // Skip {.
707 Argument = DiagStr;
Mike Stump1eb44332009-09-09 15:08:12 +0000708
John McCall909c1822010-01-14 20:11:39 +0000709 DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
710 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000711 ArgumentLen = DiagStr-Argument;
712 ++DiagStr; // Skip }.
Chris Lattnerf4c83962008-11-19 06:51:40 +0000713 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000714 }
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000716 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner22caddc2008-11-23 09:13:29 +0000717 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000718
Richard Trieu246b6aa2012-06-26 18:18:47 +0000719 // Only used for type diffing.
720 unsigned ArgNo2 = ArgNo;
721
David Blaikied6471f72011-09-25 23:23:43 +0000722 DiagnosticsEngine::ArgumentKind Kind = getArgKind(ArgNo);
Richard Trieu246b6aa2012-06-26 18:18:47 +0000723 if (Kind == DiagnosticsEngine::ak_qualtype &&
724 ModifierIs(Modifier, ModifierLen, "diff")) {
725 Kind = DiagnosticsEngine::ak_qualtype_pair;
726 assert(*DiagStr == ',' && isdigit(*(DiagStr + 1)) &&
727 "Invalid format for diff modifier");
728 ++DiagStr; // Comma.
729 ArgNo2 = *DiagStr++ - '0';
730 assert(getArgKind(ArgNo2) == DiagnosticsEngine::ak_qualtype &&
731 "Second value of type diff must be a qualtype");
732 }
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000733
734 switch (Kind) {
Chris Lattner08631c52008-11-23 21:45:46 +0000735 // ---- STRINGS ----
David Blaikied6471f72011-09-25 23:23:43 +0000736 case DiagnosticsEngine::ak_std_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000737 const std::string &S = getArgStdStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000738 assert(ModifierLen == 0 && "No modifiers for strings yet");
739 OutStr.append(S.begin(), S.end());
740 break;
741 }
David Blaikied6471f72011-09-25 23:23:43 +0000742 case DiagnosticsEngine::ak_c_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000743 const char *S = getArgCStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000744 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbare46e3542009-04-20 06:13:16 +0000745
746 // Don't crash if get passed a null pointer by accident.
747 if (!S)
748 S = "(null)";
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000750 OutStr.append(S, S + strlen(S));
751 break;
752 }
Chris Lattner08631c52008-11-23 21:45:46 +0000753 // ---- INTEGERS ----
David Blaikied6471f72011-09-25 23:23:43 +0000754 case DiagnosticsEngine::ak_sint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000755 int Val = getArgSInt(ArgNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000757 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCalle53a44b2010-10-14 01:55:31 +0000758 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen,
759 OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000760 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
761 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000762 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCalle53a44b2010-10-14 01:55:31 +0000763 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
764 OutStr);
John McCall3be16b72010-01-14 00:50:32 +0000765 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
766 HandleOrdinalModifier((unsigned)Val, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000767 } else {
768 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbar23e47c62009-10-17 18:12:14 +0000769 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner30bc9652008-11-19 07:22:31 +0000770 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000771 break;
772 }
David Blaikied6471f72011-09-25 23:23:43 +0000773 case DiagnosticsEngine::ak_uint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000774 unsigned Val = getArgUInt(ArgNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000776 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCall9f286142010-01-13 23:58:20 +0000777 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000778 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
779 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000780 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCalle53a44b2010-10-14 01:55:31 +0000781 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
782 OutStr);
John McCall3be16b72010-01-14 00:50:32 +0000783 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
784 HandleOrdinalModifier(Val, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000785 } else {
786 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbar23e47c62009-10-17 18:12:14 +0000787 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner30bc9652008-11-19 07:22:31 +0000788 }
Chris Lattner22caddc2008-11-23 09:13:29 +0000789 break;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000790 }
Chris Lattner08631c52008-11-23 21:45:46 +0000791 // ---- NAMES and TYPES ----
David Blaikied6471f72011-09-25 23:23:43 +0000792 case DiagnosticsEngine::ak_identifierinfo: {
Chris Lattner08631c52008-11-23 21:45:46 +0000793 const IdentifierInfo *II = getArgIdentifier(ArgNo);
794 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbare46e3542009-04-20 06:13:16 +0000795
796 // Don't crash if get passed a null pointer by accident.
797 if (!II) {
798 const char *S = "(null)";
799 OutStr.append(S, S + strlen(S));
800 continue;
801 }
802
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000803 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
Chris Lattner08631c52008-11-23 21:45:46 +0000804 break;
805 }
David Blaikied6471f72011-09-25 23:23:43 +0000806 case DiagnosticsEngine::ak_qualtype:
807 case DiagnosticsEngine::ak_declarationname:
808 case DiagnosticsEngine::ak_nameddecl:
809 case DiagnosticsEngine::ak_nestednamespec:
810 case DiagnosticsEngine::ak_declcontext:
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000811 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000812 Modifier, ModifierLen,
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000813 Argument, ArgumentLen,
814 FormattedArgs.data(), FormattedArgs.size(),
Chandler Carruth0673cb32011-07-11 17:49:21 +0000815 OutStr, QualTypeVals);
Chris Lattner22caddc2008-11-23 09:13:29 +0000816 break;
Richard Trieu246b6aa2012-06-26 18:18:47 +0000817 case DiagnosticsEngine::ak_qualtype_pair:
818 // Create a struct with all the info needed for printing.
819 TemplateDiffTypes TDT;
820 TDT.FromType = getRawArg(ArgNo);
821 TDT.ToType = getRawArg(ArgNo2);
822 TDT.ElideType = getDiags()->ElideType;
823 TDT.ShowColors = getDiags()->ShowColors;
824 intptr_t val = reinterpret_cast<intptr_t>(&TDT);
825
Richard Trieu529cdf42012-06-29 21:12:16 +0000826 const char *ArgumentEnd = Argument + ArgumentLen;
827 const char *Pipe = ScanFormat(Argument, ArgumentEnd, '|');
828
Richard Trieu246b6aa2012-06-26 18:18:47 +0000829 // Print the tree.
830 if (getDiags()->PrintTemplateTree) {
831 TDT.PrintFromType = true;
832 TDT.PrintTree = true;
833 getDiags()->ConvertArgToString(Kind, val,
834 Modifier, ModifierLen,
835 Argument, ArgumentLen,
836 FormattedArgs.data(),
837 FormattedArgs.size(),
838 Tree, QualTypeVals);
839 // If there is no tree information, fall back to regular printing.
Richard Trieu529cdf42012-06-29 21:12:16 +0000840 if (!Tree.empty()) {
841 FormatDiagnostic(Pipe + 1, ArgumentEnd, OutStr);
Richard Trieu246b6aa2012-06-26 18:18:47 +0000842 break;
Richard Trieu529cdf42012-06-29 21:12:16 +0000843 }
Richard Trieu246b6aa2012-06-26 18:18:47 +0000844 }
845
846 // Non-tree printing, also the fall-back when tree printing fails.
847 // The fall-back is triggered when the types compared are not templates.
Richard Trieu529cdf42012-06-29 21:12:16 +0000848 const char *FirstDollar = ScanFormat(Argument, ArgumentEnd, '$');
849 const char *SecondDollar = ScanFormat(FirstDollar + 1, ArgumentEnd, '$');
Richard Trieu246b6aa2012-06-26 18:18:47 +0000850
851 // Append before text
Richard Trieu529cdf42012-06-29 21:12:16 +0000852 FormatDiagnostic(Argument, FirstDollar, OutStr);
Richard Trieu246b6aa2012-06-26 18:18:47 +0000853
854 // Append first type
855 TDT.PrintTree = false;
856 TDT.PrintFromType = true;
857 getDiags()->ConvertArgToString(Kind, val,
858 Modifier, ModifierLen,
859 Argument, ArgumentLen,
860 FormattedArgs.data(), FormattedArgs.size(),
861 OutStr, QualTypeVals);
862 // Append middle text
Richard Trieu529cdf42012-06-29 21:12:16 +0000863 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
Richard Trieu246b6aa2012-06-26 18:18:47 +0000864
865 // Append second type
866 TDT.PrintFromType = false;
867 getDiags()->ConvertArgToString(Kind, val,
868 Modifier, ModifierLen,
869 Argument, ArgumentLen,
870 FormattedArgs.data(), FormattedArgs.size(),
871 OutStr, QualTypeVals);
872 // Append end text
Richard Trieu529cdf42012-06-29 21:12:16 +0000873 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
Richard Trieu246b6aa2012-06-26 18:18:47 +0000874 break;
Nico Weber7bfaaae2008-08-10 19:59:06 +0000875 }
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000876
877 // Remember this argument info for subsequent formatting operations. Turn
878 // std::strings into a null terminated string to make it be the same case as
879 // all the other ones.
Richard Trieu246b6aa2012-06-26 18:18:47 +0000880 if (Kind == DiagnosticsEngine::ak_qualtype_pair)
881 continue;
882 else if (Kind != DiagnosticsEngine::ak_std_string)
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000883 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
884 else
David Blaikied6471f72011-09-25 23:23:43 +0000885 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_c_string,
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000886 (intptr_t)getArgStdStr(ArgNo).c_str()));
887
Nico Weber7bfaaae2008-08-10 19:59:06 +0000888 }
Richard Trieu246b6aa2012-06-26 18:18:47 +0000889
890 // Append the type tree to the end of the diagnostics.
891 OutStr.append(Tree.begin(), Tree.end());
Nico Weber7bfaaae2008-08-10 19:59:06 +0000892}
Ted Kremenekcabe6682009-01-23 20:28:53 +0000893
Douglas Gregora88084b2010-02-18 18:08:43 +0000894StoredDiagnostic::StoredDiagnostic() { }
895
David Blaikied6471f72011-09-25 23:23:43 +0000896StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000897 StringRef Message)
Benjamin Kramera6a32e22010-11-19 17:36:51 +0000898 : ID(ID), Level(Level), Loc(), Message(Message) { }
Douglas Gregora88084b2010-02-18 18:08:43 +0000899
David Blaikied6471f72011-09-25 23:23:43 +0000900StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level,
David Blaikie40847cf2011-09-26 01:18:08 +0000901 const Diagnostic &Info)
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000902 : ID(Info.getID()), Level(Level)
903{
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000904 assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
905 "Valid source location without setting a source manager for diagnostic");
906 if (Info.getLocation().isValid())
907 Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000908 SmallString<64> Message;
Douglas Gregora88084b2010-02-18 18:08:43 +0000909 Info.FormatDiagnostic(Message);
910 this->Message.assign(Message.begin(), Message.end());
911
912 Ranges.reserve(Info.getNumRanges());
913 for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I)
914 Ranges.push_back(Info.getRange(I));
915
Douglas Gregor849b2432010-03-31 17:46:05 +0000916 FixIts.reserve(Info.getNumFixItHints());
917 for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I)
918 FixIts.push_back(Info.getFixItHint(I));
Douglas Gregora88084b2010-02-18 18:08:43 +0000919}
920
David Blaikied6471f72011-09-25 23:23:43 +0000921StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000922 StringRef Message, FullSourceLoc Loc,
Chris Lattner2d3ba4f2011-07-23 17:14:25 +0000923 ArrayRef<CharSourceRange> Ranges,
924 ArrayRef<FixItHint> Fixits)
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000925 : ID(ID), Level(Level), Loc(Loc), Message(Message)
926{
927 this->Ranges.assign(Ranges.begin(), Ranges.end());
928 this->FixIts.assign(FixIts.begin(), FixIts.end());
929}
930
Douglas Gregora88084b2010-02-18 18:08:43 +0000931StoredDiagnostic::~StoredDiagnostic() { }
932
Ted Kremenekcabe6682009-01-23 20:28:53 +0000933/// IncludeInDiagnosticCounts - This method (whose default implementation
934/// returns true) indicates whether the diagnostics handled by this
David Blaikie78ad0b92011-09-25 23:39:51 +0000935/// DiagnosticConsumer should be included in the number of diagnostics
David Blaikied6471f72011-09-25 23:23:43 +0000936/// reported by DiagnosticsEngine.
David Blaikie78ad0b92011-09-25 23:39:51 +0000937bool DiagnosticConsumer::IncludeInDiagnosticCounts() const { return true; }
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000938
David Blaikie99ba9e32011-12-20 02:48:34 +0000939void IgnoringDiagConsumer::anchor() { }
940
Benjamin Kramerd7a3e2c2012-02-07 22:29:24 +0000941PartialDiagnostic::StorageAllocator::StorageAllocator() {
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000942 for (unsigned I = 0; I != NumCached; ++I)
943 FreeList[I] = Cached + I;
944 NumFreeListEntries = NumCached;
945}
946
Benjamin Kramerd7a3e2c2012-02-07 22:29:24 +0000947PartialDiagnostic::StorageAllocator::~StorageAllocator() {
Chad Rosierdfaee492012-02-07 23:24:49 +0000948 // Don't assert if we are in a CrashRecovery context, as this invariant may
949 // be invalidated during a crash.
950 assert((NumFreeListEntries == NumCached ||
951 llvm::CrashRecoveryContext::isRecoveringFromCrash()) &&
952 "A partial is on the lamb");
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000953}