blob: c2631bba3fab8b9a0c12c60b334c58c48dd70347 [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,
Chris Lattner5f9e2722011-07-23 10:55:15 +000030 SmallVectorImpl<intptr_t> &QualTypeVals) {
Chris Lattner3fdf4b02008-11-23 09:21:17 +000031 const char *Str = "<can't format argument>";
Chris Lattner22caddc2008-11-23 09:13:29 +000032 Output.append(Str, Str+strlen(Str));
33}
34
35
David Blaikied6471f72011-09-25 23:23:43 +000036DiagnosticsEngine::DiagnosticsEngine(
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;
51 ShowOverloads = Ovl_All;
52 ExtBehavior = Ext_Ignore;
53
54 ErrorLimit = 0;
55 TemplateBacktraceLimit = 0;
Richard Smith08d6e032011-12-16 19:06:07 +000056 ConstexprBacktraceLimit = 0;
Douglas Gregorcc5888d2010-07-31 00:40:00 +000057
Douglas Gregorabc563f2010-07-19 21:46:24 +000058 Reset();
Reid Spencer5f016e22007-07-11 17:01:13 +000059}
60
David Blaikied6471f72011-09-25 23:23:43 +000061DiagnosticsEngine::~DiagnosticsEngine() {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000062 if (OwnsDiagClient)
63 delete Client;
Chris Lattner182745a2007-12-02 01:09:57 +000064}
65
David Blaikie78ad0b92011-09-25 23:39:51 +000066void DiagnosticsEngine::setClient(DiagnosticConsumer *client,
David Blaikied6471f72011-09-25 23:23:43 +000067 bool ShouldOwnClient) {
Douglas Gregor4f5e21e2011-01-31 22:04:05 +000068 if (OwnsDiagClient && Client)
69 delete Client;
70
71 Client = client;
72 OwnsDiagClient = ShouldOwnClient;
73}
Chris Lattner04ae2df2009-07-12 21:18:45 +000074
David Blaikied6471f72011-09-25 23:23:43 +000075void DiagnosticsEngine::pushMappings(SourceLocation Loc) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +000076 DiagStateOnPushStack.push_back(GetCurDiagState());
Chris Lattner04ae2df2009-07-12 21:18:45 +000077}
78
David Blaikied6471f72011-09-25 23:23:43 +000079bool DiagnosticsEngine::popMappings(SourceLocation Loc) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +000080 if (DiagStateOnPushStack.empty())
Chris Lattner04ae2df2009-07-12 21:18:45 +000081 return false;
82
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +000083 if (DiagStateOnPushStack.back() != GetCurDiagState()) {
84 // State changed at some point between push/pop.
85 PushDiagStatePoint(DiagStateOnPushStack.back(), Loc);
86 }
87 DiagStateOnPushStack.pop_back();
Chris Lattner04ae2df2009-07-12 21:18:45 +000088 return true;
89}
90
David Blaikied6471f72011-09-25 23:23:43 +000091void DiagnosticsEngine::Reset() {
Douglas Gregorabc563f2010-07-19 21:46:24 +000092 ErrorOccurred = false;
93 FatalErrorOccurred = false;
Douglas Gregor85bea972011-07-06 17:40:26 +000094 UnrecoverableErrorOccurred = false;
Douglas Gregorabc563f2010-07-19 21:46:24 +000095
96 NumWarnings = 0;
97 NumErrors = 0;
98 NumErrorsSuppressed = 0;
Argyrios Kyrtzidisc0a575f2011-07-29 01:25:44 +000099 TrapNumErrorsOccurred = 0;
100 TrapNumUnrecoverableErrorsOccurred = 0;
Douglas Gregor85bea972011-07-06 17:40:26 +0000101
Douglas Gregorabc563f2010-07-19 21:46:24 +0000102 CurDiagID = ~0U;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000103 // Set LastDiagLevel to an "unset" state. If we set it to 'Ignored', notes
David Blaikied6471f72011-09-25 23:23:43 +0000104 // using a DiagnosticsEngine associated to a translation unit that follow
105 // diagnostics from a DiagnosticsEngine associated to anoter t.u. will not be
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000106 // displayed.
107 LastDiagLevel = (DiagnosticIDs::Level)-1;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000108 DelayedDiagID = 0;
Argyrios Kyrtzidisdc0a2da2011-03-26 18:58:17 +0000109
110 // Clear state related to #pragma diagnostic.
111 DiagStates.clear();
112 DiagStatePoints.clear();
113 DiagStateOnPushStack.clear();
114
115 // Create a DiagState and DiagStatePoint representing diagnostic changes
116 // through command-line.
117 DiagStates.push_back(DiagState());
118 PushDiagStatePoint(&DiagStates.back(), SourceLocation());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000119}
Reid Spencer5f016e22007-07-11 17:01:13 +0000120
David Blaikied6471f72011-09-25 23:23:43 +0000121void DiagnosticsEngine::SetDelayedDiagnostic(unsigned DiagID, StringRef Arg1,
Chad Rosierdfaee492012-02-07 23:24:49 +0000122 StringRef Arg2) {
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000123 if (DelayedDiagID)
124 return;
125
126 DelayedDiagID = DiagID;
Douglas Gregor9e2dac92010-03-22 15:47:45 +0000127 DelayedDiagArg1 = Arg1.str();
128 DelayedDiagArg2 = Arg2.str();
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000129}
130
David Blaikied6471f72011-09-25 23:23:43 +0000131void DiagnosticsEngine::ReportDelayed() {
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000132 Report(DelayedDiagID) << DelayedDiagArg1 << DelayedDiagArg2;
133 DelayedDiagID = 0;
134 DelayedDiagArg1.clear();
135 DelayedDiagArg2.clear();
136}
137
David Blaikied6471f72011-09-25 23:23:43 +0000138DiagnosticsEngine::DiagStatePointsTy::iterator
139DiagnosticsEngine::GetDiagStatePointForLoc(SourceLocation L) const {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000140 assert(!DiagStatePoints.empty());
141 assert(DiagStatePoints.front().Loc.isInvalid() &&
142 "Should have created a DiagStatePoint for command-line");
143
144 FullSourceLoc Loc(L, *SourceMgr);
145 if (Loc.isInvalid())
146 return DiagStatePoints.end() - 1;
147
148 DiagStatePointsTy::iterator Pos = DiagStatePoints.end();
149 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
150 if (LastStateChangePos.isValid() &&
151 Loc.isBeforeInTranslationUnitThan(LastStateChangePos))
152 Pos = std::upper_bound(DiagStatePoints.begin(), DiagStatePoints.end(),
153 DiagStatePoint(0, Loc));
154 --Pos;
155 return Pos;
156}
157
158/// \brief This allows the client to specify that certain
159/// warnings are ignored. Notes can never be mapped, errors can only be
160/// mapped to fatal, and WARNINGs and EXTENSIONs can be mapped arbitrarily.
161///
162/// \param The source location that this change of diagnostic state should
163/// take affect. It can be null if we are setting the latest state.
David Blaikied6471f72011-09-25 23:23:43 +0000164void DiagnosticsEngine::setDiagnosticMapping(diag::kind Diag, diag::Mapping Map,
Chad Rosierdfaee492012-02-07 23:24:49 +0000165 SourceLocation L) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000166 assert(Diag < diag::DIAG_UPPER_LIMIT &&
167 "Can only map builtin diagnostics");
168 assert((Diags->isBuiltinWarningOrExtension(Diag) ||
169 (Map == diag::MAP_FATAL || Map == diag::MAP_ERROR)) &&
170 "Cannot map errors into warnings!");
171 assert(!DiagStatePoints.empty());
172
173 FullSourceLoc Loc(L, *SourceMgr);
174 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
Chad Rosier7a0a31c2012-02-03 01:49:51 +0000175 // Don't allow a mapping to a warning override an error/fatal mapping.
176 if (Map == diag::MAP_WARNING) {
177 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(Diag);
178 if (Info.getMapping() == diag::MAP_ERROR ||
179 Info.getMapping() == diag::MAP_FATAL)
180 Map = Info.getMapping();
181 }
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +0000182 DiagnosticMappingInfo MappingInfo = makeMappingInfo(Map, L);
Daniel Dunbar53201a82011-10-04 21:17:24 +0000183
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000184 // Common case; setting all the diagnostics of a group in one place.
185 if (Loc.isInvalid() || Loc == LastStateChangePos) {
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000186 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000187 return;
188 }
189
190 // Another common case; modifying diagnostic state in a source location
191 // after the previous one.
192 if ((Loc.isValid() && LastStateChangePos.isInvalid()) ||
193 LastStateChangePos.isBeforeInTranslationUnitThan(Loc)) {
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000194 // A diagnostic pragma occurred, create a new DiagState initialized with
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000195 // the current one and a new DiagStatePoint to record at which location
196 // the new state became active.
197 DiagStates.push_back(*GetCurDiagState());
198 PushDiagStatePoint(&DiagStates.back(), Loc);
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000199 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000200 return;
201 }
202
203 // We allow setting the diagnostic state in random source order for
204 // completeness but it should not be actually happening in normal practice.
205
206 DiagStatePointsTy::iterator Pos = GetDiagStatePointForLoc(Loc);
207 assert(Pos != DiagStatePoints.end());
208
209 // Update all diagnostic states that are active after the given location.
210 for (DiagStatePointsTy::iterator
211 I = Pos+1, E = DiagStatePoints.end(); I != E; ++I) {
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000212 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000213 }
214
215 // If the location corresponds to an existing point, just update its state.
216 if (Pos->Loc == Loc) {
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000217 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000218 return;
219 }
220
221 // Create a new state/point and fit it into the vector of DiagStatePoints
222 // so that the vector is always ordered according to location.
223 Pos->Loc.isBeforeInTranslationUnitThan(Loc);
224 DiagStates.push_back(*Pos->State);
225 DiagState *NewState = &DiagStates.back();
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000226 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000227 DiagStatePoints.insert(Pos+1, DiagStatePoint(NewState,
228 FullSourceLoc(Loc, *SourceMgr)));
229}
230
Daniel Dunbar3f839462011-09-29 01:47:16 +0000231bool DiagnosticsEngine::setDiagnosticGroupMapping(
232 StringRef Group, diag::Mapping Map, SourceLocation Loc)
233{
234 // Get the diagnostics in this group.
235 llvm::SmallVector<diag::kind, 8> GroupDiags;
236 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
237 return true;
238
239 // Set the mapping.
240 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i)
241 setDiagnosticMapping(GroupDiags[i], Map, Loc);
242
243 return false;
244}
245
Chad Rosier3f225092012-02-07 19:55:45 +0000246void DiagnosticsEngine::setDiagnosticWarningAsError(diag::kind Diag,
247 bool Enabled) {
248 // If we are enabling this feature, just set the diagnostic mappings to map to
249 // errors.
250 if (Enabled)
251 setDiagnosticMapping(Diag, diag::MAP_ERROR, SourceLocation());
252
253 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
254 // potentially downgrade anything already mapped to be a warning.
255 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(Diag);
256
257 if (Info.getMapping() == diag::MAP_ERROR ||
258 Info.getMapping() == diag::MAP_FATAL)
259 Info.setMapping(diag::MAP_WARNING);
260
261 Info.setNoWarningAsError(true);
262}
263
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000264bool DiagnosticsEngine::setDiagnosticGroupWarningAsError(StringRef Group,
265 bool Enabled) {
Daniel Dunbara5e41332011-09-29 01:52:06 +0000266 // If we are enabling this feature, just set the diagnostic mappings to map to
267 // errors.
268 if (Enabled)
269 return setDiagnosticGroupMapping(Group, diag::MAP_ERROR);
270
271 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
272 // potentially downgrade anything already mapped to be a warning.
273
274 // Get the diagnostics in this group.
275 llvm::SmallVector<diag::kind, 8> GroupDiags;
276 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
277 return true;
278
279 // Perform the mapping change.
280 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i) {
281 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(
282 GroupDiags[i]);
283
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000284 if (Info.getMapping() == diag::MAP_ERROR ||
285 Info.getMapping() == diag::MAP_FATAL)
286 Info.setMapping(diag::MAP_WARNING);
287
Daniel Dunbara5e41332011-09-29 01:52:06 +0000288 Info.setNoWarningAsError(true);
289 }
290
291 return false;
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000292}
293
Chad Rosier3f225092012-02-07 19:55:45 +0000294void DiagnosticsEngine::setDiagnosticErrorAsFatal(diag::kind Diag,
295 bool Enabled) {
296 // If we are enabling this feature, just set the diagnostic mappings to map to
297 // errors.
298 if (Enabled)
299 setDiagnosticMapping(Diag, diag::MAP_FATAL, SourceLocation());
300
301 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
302 // potentially downgrade anything already mapped to be a warning.
303 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(Diag);
304
305 if (Info.getMapping() == diag::MAP_FATAL)
306 Info.setMapping(diag::MAP_ERROR);
307
308 Info.setNoErrorAsFatal(true);
309}
310
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000311bool DiagnosticsEngine::setDiagnosticGroupErrorAsFatal(StringRef Group,
312 bool Enabled) {
Daniel Dunbara5e41332011-09-29 01:52:06 +0000313 // If we are enabling this feature, just set the diagnostic mappings to map to
314 // fatal errors.
315 if (Enabled)
316 return setDiagnosticGroupMapping(Group, diag::MAP_FATAL);
317
318 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
319 // potentially downgrade anything already mapped to be an error.
320
321 // Get the diagnostics in this group.
322 llvm::SmallVector<diag::kind, 8> GroupDiags;
323 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
324 return true;
325
326 // Perform the mapping change.
327 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i) {
328 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(
329 GroupDiags[i]);
330
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000331 if (Info.getMapping() == diag::MAP_FATAL)
332 Info.setMapping(diag::MAP_ERROR);
333
Daniel Dunbara5e41332011-09-29 01:52:06 +0000334 Info.setNoErrorAsFatal(true);
335 }
336
337 return false;
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000338}
339
Argyrios Kyrtzidis82e64112012-01-28 04:35:52 +0000340void DiagnosticsEngine::setMappingToAllDiagnostics(diag::Mapping Map,
Argyrios Kyrtzidis11583c72012-01-27 06:15:43 +0000341 SourceLocation Loc) {
342 // Get all the diagnostics.
343 llvm::SmallVector<diag::kind, 64> AllDiags;
344 Diags->getAllDiagnostics(AllDiags);
345
346 // Set the mapping.
347 for (unsigned i = 0, e = AllDiags.size(); i != e; ++i)
348 if (Diags->isBuiltinWarningOrExtension(AllDiags[i]))
349 setDiagnosticMapping(AllDiags[i], Map, Loc);
Argyrios Kyrtzidis11583c72012-01-27 06:15:43 +0000350}
351
David Blaikied6471f72011-09-25 23:23:43 +0000352void DiagnosticsEngine::Report(const StoredDiagnostic &storedDiag) {
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000353 assert(CurDiagID == ~0U && "Multiple diagnostics in flight at once!");
354
355 CurDiagLoc = storedDiag.getLocation();
356 CurDiagID = storedDiag.getID();
357 NumDiagArgs = 0;
358
359 NumDiagRanges = storedDiag.range_size();
360 assert(NumDiagRanges < sizeof(DiagRanges)/sizeof(DiagRanges[0]) &&
361 "Too many arguments to diagnostic!");
362 unsigned i = 0;
363 for (StoredDiagnostic::range_iterator
364 RI = storedDiag.range_begin(),
365 RE = storedDiag.range_end(); RI != RE; ++RI)
366 DiagRanges[i++] = *RI;
367
Argyrios Kyrtzidiscbf46a02012-02-03 05:58:22 +0000368 FixItHints.clear();
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000369 for (StoredDiagnostic::fixit_iterator
370 FI = storedDiag.fixit_begin(),
371 FE = storedDiag.fixit_end(); FI != FE; ++FI)
Argyrios Kyrtzidiscbf46a02012-02-03 05:58:22 +0000372 FixItHints.push_back(*FI);
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000373
David Blaikie78ad0b92011-09-25 23:39:51 +0000374 assert(Client && "DiagnosticConsumer not set!");
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000375 Level DiagLevel = storedDiag.getLevel();
David Blaikie40847cf2011-09-26 01:18:08 +0000376 Diagnostic Info(this, storedDiag.getMessage());
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000377 Client->HandleDiagnostic(DiagLevel, Info);
378 if (Client->IncludeInDiagnosticCounts()) {
David Blaikied6471f72011-09-25 23:23:43 +0000379 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000380 ++NumWarnings;
381 }
382
383 CurDiagID = ~0U;
384}
385
Douglas Gregorb5350412010-10-13 17:22:14 +0000386void DiagnosticBuilder::FlushCounts() {
387 DiagObj->NumDiagArgs = NumArgs;
388 DiagObj->NumDiagRanges = NumRanges;
Douglas Gregorb5350412010-10-13 17:22:14 +0000389}
390
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000391bool DiagnosticBuilder::Emit() {
392 // If DiagObj is null, then its soul was stolen by the copy ctor
393 // or the user called Emit().
394 if (DiagObj == 0) return false;
395
396 // When emitting diagnostics, we set the final argument count into
David Blaikied6471f72011-09-25 23:23:43 +0000397 // the DiagnosticsEngine object.
Douglas Gregorb5350412010-10-13 17:22:14 +0000398 FlushCounts();
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000399
400 // Process the diagnostic, sending the accumulated information to the
David Blaikie78ad0b92011-09-25 23:39:51 +0000401 // DiagnosticConsumer.
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000402 bool Emitted = DiagObj->ProcessDiag();
403
404 // Clear out the current diagnostic object.
Douglas Gregor9e2dac92010-03-22 15:47:45 +0000405 unsigned DiagID = DiagObj->CurDiagID;
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000406 DiagObj->Clear();
407
408 // If there was a delayed diagnostic, emit it now.
Douglas Gregor9e2dac92010-03-22 15:47:45 +0000409 if (DiagObj->DelayedDiagID && DiagObj->DelayedDiagID != DiagID)
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000410 DiagObj->ReportDelayed();
411
412 // This diagnostic is dead.
413 DiagObj = 0;
414
415 return Emitted;
416}
417
Nico Weber7bfaaae2008-08-10 19:59:06 +0000418
David Blaikie78ad0b92011-09-25 23:39:51 +0000419DiagnosticConsumer::~DiagnosticConsumer() {}
Nico Weber7bfaaae2008-08-10 19:59:06 +0000420
David Blaikie78ad0b92011-09-25 23:39:51 +0000421void DiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
David Blaikie40847cf2011-09-26 01:18:08 +0000422 const Diagnostic &Info) {
Argyrios Kyrtzidisf2224d82010-11-18 20:06:46 +0000423 if (!IncludeInDiagnosticCounts())
424 return;
425
David Blaikied6471f72011-09-25 23:23:43 +0000426 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidisf2224d82010-11-18 20:06:46 +0000427 ++NumWarnings;
David Blaikied6471f72011-09-25 23:23:43 +0000428 else if (DiagLevel >= DiagnosticsEngine::Error)
Argyrios Kyrtzidisf2224d82010-11-18 20:06:46 +0000429 ++NumErrors;
430}
Chris Lattnerf4c83962008-11-19 06:51:40 +0000431
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000432/// ModifierIs - Return true if the specified modifier matches specified string.
433template <std::size_t StrLen>
434static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
435 const char (&Str)[StrLen]) {
436 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
437}
438
John McCall909c1822010-01-14 20:11:39 +0000439/// ScanForward - Scans forward, looking for the given character, skipping
440/// nested clauses and escaped characters.
441static const char *ScanFormat(const char *I, const char *E, char Target) {
442 unsigned Depth = 0;
443
444 for ( ; I != E; ++I) {
445 if (Depth == 0 && *I == Target) return I;
446 if (Depth != 0 && *I == '}') Depth--;
447
448 if (*I == '%') {
449 I++;
450 if (I == E) break;
451
452 // Escaped characters get implicitly skipped here.
453
454 // Format specifier.
455 if (!isdigit(*I) && !ispunct(*I)) {
456 for (I++; I != E && !isdigit(*I) && *I != '{'; I++) ;
457 if (I == E) break;
458 if (*I == '{')
459 Depth++;
460 }
461 }
462 }
463 return E;
464}
465
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000466/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
467/// like this: %select{foo|bar|baz}2. This means that the integer argument
468/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
469/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
470/// This is very useful for certain classes of variant diagnostics.
David Blaikie40847cf2011-09-26 01:18:08 +0000471static void HandleSelectModifier(const Diagnostic &DInfo, unsigned ValNo,
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000472 const char *Argument, unsigned ArgumentLen,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000473 SmallVectorImpl<char> &OutStr) {
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000474 const char *ArgumentEnd = Argument+ArgumentLen;
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000476 // Skip over 'ValNo' |'s.
477 while (ValNo) {
John McCall909c1822010-01-14 20:11:39 +0000478 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000479 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
480 " larger than the number of options in the diagnostic string!");
481 Argument = NextVal+1; // Skip this string.
482 --ValNo;
483 }
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000485 // Get the end of the value. This is either the } or the |.
John McCall909c1822010-01-14 20:11:39 +0000486 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
John McCall9f286142010-01-13 23:58:20 +0000487
488 // Recursively format the result of the select clause into the output string.
489 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000490}
491
492/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
493/// letter 's' to the string if the value is not 1. This is used in cases like
494/// this: "you idiot, you have %4 parameter%s4!".
495static void HandleIntegerSModifier(unsigned ValNo,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000496 SmallVectorImpl<char> &OutStr) {
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000497 if (ValNo != 1)
498 OutStr.push_back('s');
499}
500
John McCall3be16b72010-01-14 00:50:32 +0000501/// HandleOrdinalModifier - Handle the integer 'ord' modifier. This
502/// prints the ordinal form of the given integer, with 1 corresponding
503/// to the first ordinal. Currently this is hard-coded to use the
504/// English form.
505static void HandleOrdinalModifier(unsigned ValNo,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000506 SmallVectorImpl<char> &OutStr) {
John McCall3be16b72010-01-14 00:50:32 +0000507 assert(ValNo != 0 && "ValNo must be strictly positive!");
508
509 llvm::raw_svector_ostream Out(OutStr);
510
511 // We could use text forms for the first N ordinals, but the numeric
512 // forms are actually nicer in diagnostics because they stand out.
513 Out << ValNo;
514
515 // It is critically important that we do this perfectly for
516 // user-written sequences with over 100 elements.
517 switch (ValNo % 100) {
518 case 11:
519 case 12:
520 case 13:
521 Out << "th"; return;
522 default:
523 switch (ValNo % 10) {
524 case 1: Out << "st"; return;
525 case 2: Out << "nd"; return;
526 case 3: Out << "rd"; return;
527 default: Out << "th"; return;
528 }
529 }
530}
531
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000532
Sebastian Redle4c452c2008-11-22 13:44:36 +0000533/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000534static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000535 // Programming 101: Parse a decimal number :-)
536 unsigned Val = 0;
537 while (Start != End && *Start >= '0' && *Start <= '9') {
538 Val *= 10;
539 Val += *Start - '0';
540 ++Start;
541 }
542 return Val;
543}
544
545/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000546static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000547 if (*Start != '[') {
548 unsigned Ref = PluralNumber(Start, End);
549 return Ref == Val;
550 }
551
552 ++Start;
553 unsigned Low = PluralNumber(Start, End);
554 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
555 ++Start;
556 unsigned High = PluralNumber(Start, End);
557 assert(*Start == ']' && "Bad plural expression syntax: expected )");
558 ++Start;
559 return Low <= Val && Val <= High;
560}
561
562/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000563static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000564 // Empty condition?
565 if (*Start == ':')
566 return true;
567
568 while (1) {
569 char C = *Start;
570 if (C == '%') {
571 // Modulo expression
572 ++Start;
573 unsigned Arg = PluralNumber(Start, End);
574 assert(*Start == '=' && "Bad plural expression syntax: expected =");
575 ++Start;
576 unsigned ValMod = ValNo % Arg;
577 if (TestPluralRange(ValMod, Start, End))
578 return true;
579 } else {
Sebastian Redle2065322008-11-27 07:28:14 +0000580 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redle4c452c2008-11-22 13:44:36 +0000581 "Bad plural expression syntax: unexpected character");
582 // Range expression
583 if (TestPluralRange(ValNo, Start, End))
584 return true;
585 }
586
587 // Scan for next or-expr part.
588 Start = std::find(Start, End, ',');
Mike Stump1eb44332009-09-09 15:08:12 +0000589 if (Start == End)
Sebastian Redle4c452c2008-11-22 13:44:36 +0000590 break;
591 ++Start;
592 }
593 return false;
594}
595
596/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
597/// for complex plural forms, or in languages where all plurals are complex.
598/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
599/// conditions that are tested in order, the form corresponding to the first
600/// that applies being emitted. The empty condition is always true, making the
601/// last form a default case.
602/// Conditions are simple boolean expressions, where n is the number argument.
603/// Here are the rules.
604/// condition := expression | empty
605/// empty := -> always true
606/// expression := numeric [',' expression] -> logical or
607/// numeric := range -> true if n in range
608/// | '%' number '=' range -> true if n % number in range
609/// range := number
610/// | '[' number ',' number ']' -> ranges are inclusive both ends
611///
612/// Here are some examples from the GNU gettext manual written in this form:
613/// English:
614/// {1:form0|:form1}
615/// Latvian:
616/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
617/// Gaeilge:
618/// {1:form0|2:form1|:form2}
619/// Romanian:
620/// {1:form0|0,%100=[1,19]:form1|:form2}
621/// Lithuanian:
622/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
623/// Russian (requires repeated form):
624/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
625/// Slovak
626/// {1:form0|[2,4]:form1|:form2}
627/// Polish (requires repeated form):
628/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
David Blaikie40847cf2011-09-26 01:18:08 +0000629static void HandlePluralModifier(const Diagnostic &DInfo, unsigned ValNo,
Sebastian Redle4c452c2008-11-22 13:44:36 +0000630 const char *Argument, unsigned ArgumentLen,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000631 SmallVectorImpl<char> &OutStr) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000632 const char *ArgumentEnd = Argument + ArgumentLen;
633 while (1) {
634 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
635 const char *ExprEnd = Argument;
636 while (*ExprEnd != ':') {
637 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
638 ++ExprEnd;
639 }
640 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
641 Argument = ExprEnd + 1;
John McCall909c1822010-01-14 20:11:39 +0000642 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
John McCalle53a44b2010-10-14 01:55:31 +0000643
644 // Recursively format the result of the plural clause into the
645 // output string.
646 DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000647 return;
648 }
John McCall909c1822010-01-14 20:11:39 +0000649 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
Sebastian Redle4c452c2008-11-22 13:44:36 +0000650 }
651}
652
653
Chris Lattnerf4c83962008-11-19 06:51:40 +0000654/// FormatDiagnostic - Format this diagnostic into a string, substituting the
655/// formal arguments into the %0 slots. The result is appended onto the Str
656/// array.
David Blaikie40847cf2011-09-26 01:18:08 +0000657void Diagnostic::
Chris Lattner5f9e2722011-07-23 10:55:15 +0000658FormatDiagnostic(SmallVectorImpl<char> &OutStr) const {
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000659 if (!StoredDiagMessage.empty()) {
660 OutStr.append(StoredDiagMessage.begin(), StoredDiagMessage.end());
661 return;
662 }
663
Chris Lattner5f9e2722011-07-23 10:55:15 +0000664 StringRef Diag =
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000665 getDiags()->getDiagnosticIDs()->getDescription(getID());
Mike Stump1eb44332009-09-09 15:08:12 +0000666
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000667 FormatDiagnostic(Diag.begin(), Diag.end(), OutStr);
John McCall9f286142010-01-13 23:58:20 +0000668}
669
David Blaikie40847cf2011-09-26 01:18:08 +0000670void Diagnostic::
John McCall9f286142010-01-13 23:58:20 +0000671FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000672 SmallVectorImpl<char> &OutStr) const {
John McCall9f286142010-01-13 23:58:20 +0000673
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000674 /// FormattedArgs - Keep track of all of the arguments formatted by
675 /// ConvertArgToString and pass them into subsequent calls to
676 /// ConvertArgToString, allowing the implementation to avoid redundancies in
677 /// obvious cases.
David Blaikied6471f72011-09-25 23:23:43 +0000678 SmallVector<DiagnosticsEngine::ArgumentValue, 8> FormattedArgs;
Chandler Carruth0673cb32011-07-11 17:49:21 +0000679
680 /// QualTypeVals - Pass a vector of arrays so that QualType names can be
681 /// compared to see if more information is needed to be printed.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000682 SmallVector<intptr_t, 2> QualTypeVals;
Chandler Carruth0673cb32011-07-11 17:49:21 +0000683 for (unsigned i = 0, e = getNumArgs(); i < e; ++i)
David Blaikied6471f72011-09-25 23:23:43 +0000684 if (getArgKind(i) == DiagnosticsEngine::ak_qualtype)
Chandler Carruth0673cb32011-07-11 17:49:21 +0000685 QualTypeVals.push_back(getRawArg(i));
686
Chris Lattnerf4c83962008-11-19 06:51:40 +0000687 while (DiagStr != DiagEnd) {
688 if (DiagStr[0] != '%') {
689 // Append non-%0 substrings to Str if we have one.
690 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
691 OutStr.append(DiagStr, StrEnd);
692 DiagStr = StrEnd;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000693 continue;
John McCall909c1822010-01-14 20:11:39 +0000694 } else if (ispunct(DiagStr[1])) {
695 OutStr.push_back(DiagStr[1]); // %% -> %.
Chris Lattnerf4c83962008-11-19 06:51:40 +0000696 DiagStr += 2;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000697 continue;
698 }
Mike Stump1eb44332009-09-09 15:08:12 +0000699
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000700 // Skip the %.
701 ++DiagStr;
Mike Stump1eb44332009-09-09 15:08:12 +0000702
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000703 // This must be a placeholder for a diagnostic argument. The format for a
704 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
705 // The digit is a number from 0-9 indicating which argument this comes from.
706 // The modifier is a string of digits from the set [-a-z]+, arguments is a
707 // brace enclosed string.
708 const char *Modifier = 0, *Argument = 0;
709 unsigned ModifierLen = 0, ArgumentLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000711 // Check to see if we have a modifier. If so eat it.
712 if (!isdigit(DiagStr[0])) {
713 Modifier = DiagStr;
714 while (DiagStr[0] == '-' ||
715 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
716 ++DiagStr;
717 ModifierLen = DiagStr-Modifier;
Chris Lattnerf4c83962008-11-19 06:51:40 +0000718
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000719 // If we have an argument, get it next.
720 if (DiagStr[0] == '{') {
721 ++DiagStr; // Skip {.
722 Argument = DiagStr;
Mike Stump1eb44332009-09-09 15:08:12 +0000723
John McCall909c1822010-01-14 20:11:39 +0000724 DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
725 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000726 ArgumentLen = DiagStr-Argument;
727 ++DiagStr; // Skip }.
Chris Lattnerf4c83962008-11-19 06:51:40 +0000728 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000729 }
Mike Stump1eb44332009-09-09 15:08:12 +0000730
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000731 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner22caddc2008-11-23 09:13:29 +0000732 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000733
David Blaikied6471f72011-09-25 23:23:43 +0000734 DiagnosticsEngine::ArgumentKind Kind = getArgKind(ArgNo);
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000735
736 switch (Kind) {
Chris Lattner08631c52008-11-23 21:45:46 +0000737 // ---- STRINGS ----
David Blaikied6471f72011-09-25 23:23:43 +0000738 case DiagnosticsEngine::ak_std_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000739 const std::string &S = getArgStdStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000740 assert(ModifierLen == 0 && "No modifiers for strings yet");
741 OutStr.append(S.begin(), S.end());
742 break;
743 }
David Blaikied6471f72011-09-25 23:23:43 +0000744 case DiagnosticsEngine::ak_c_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000745 const char *S = getArgCStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000746 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbare46e3542009-04-20 06:13:16 +0000747
748 // Don't crash if get passed a null pointer by accident.
749 if (!S)
750 S = "(null)";
Mike Stump1eb44332009-09-09 15:08:12 +0000751
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000752 OutStr.append(S, S + strlen(S));
753 break;
754 }
Chris Lattner08631c52008-11-23 21:45:46 +0000755 // ---- INTEGERS ----
David Blaikied6471f72011-09-25 23:23:43 +0000756 case DiagnosticsEngine::ak_sint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000757 int Val = getArgSInt(ArgNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000759 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCalle53a44b2010-10-14 01:55:31 +0000760 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen,
761 OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000762 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
763 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000764 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCalle53a44b2010-10-14 01:55:31 +0000765 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
766 OutStr);
John McCall3be16b72010-01-14 00:50:32 +0000767 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
768 HandleOrdinalModifier((unsigned)Val, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000769 } else {
770 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbar23e47c62009-10-17 18:12:14 +0000771 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner30bc9652008-11-19 07:22:31 +0000772 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000773 break;
774 }
David Blaikied6471f72011-09-25 23:23:43 +0000775 case DiagnosticsEngine::ak_uint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000776 unsigned Val = getArgUInt(ArgNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000777
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000778 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCall9f286142010-01-13 23:58:20 +0000779 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000780 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
781 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000782 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCalle53a44b2010-10-14 01:55:31 +0000783 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
784 OutStr);
John McCall3be16b72010-01-14 00:50:32 +0000785 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
786 HandleOrdinalModifier(Val, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000787 } else {
788 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbar23e47c62009-10-17 18:12:14 +0000789 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner30bc9652008-11-19 07:22:31 +0000790 }
Chris Lattner22caddc2008-11-23 09:13:29 +0000791 break;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000792 }
Chris Lattner08631c52008-11-23 21:45:46 +0000793 // ---- NAMES and TYPES ----
David Blaikied6471f72011-09-25 23:23:43 +0000794 case DiagnosticsEngine::ak_identifierinfo: {
Chris Lattner08631c52008-11-23 21:45:46 +0000795 const IdentifierInfo *II = getArgIdentifier(ArgNo);
796 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbare46e3542009-04-20 06:13:16 +0000797
798 // Don't crash if get passed a null pointer by accident.
799 if (!II) {
800 const char *S = "(null)";
801 OutStr.append(S, S + strlen(S));
802 continue;
803 }
804
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000805 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
Chris Lattner08631c52008-11-23 21:45:46 +0000806 break;
807 }
David Blaikied6471f72011-09-25 23:23:43 +0000808 case DiagnosticsEngine::ak_qualtype:
809 case DiagnosticsEngine::ak_declarationname:
810 case DiagnosticsEngine::ak_nameddecl:
811 case DiagnosticsEngine::ak_nestednamespec:
812 case DiagnosticsEngine::ak_declcontext:
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000813 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000814 Modifier, ModifierLen,
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000815 Argument, ArgumentLen,
816 FormattedArgs.data(), FormattedArgs.size(),
Chandler Carruth0673cb32011-07-11 17:49:21 +0000817 OutStr, QualTypeVals);
Chris Lattner22caddc2008-11-23 09:13:29 +0000818 break;
Nico Weber7bfaaae2008-08-10 19:59:06 +0000819 }
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000820
821 // Remember this argument info for subsequent formatting operations. Turn
822 // std::strings into a null terminated string to make it be the same case as
823 // all the other ones.
David Blaikied6471f72011-09-25 23:23:43 +0000824 if (Kind != DiagnosticsEngine::ak_std_string)
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000825 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
826 else
David Blaikied6471f72011-09-25 23:23:43 +0000827 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_c_string,
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000828 (intptr_t)getArgStdStr(ArgNo).c_str()));
829
Nico Weber7bfaaae2008-08-10 19:59:06 +0000830 }
Nico Weber7bfaaae2008-08-10 19:59:06 +0000831}
Ted Kremenekcabe6682009-01-23 20:28:53 +0000832
Douglas Gregora88084b2010-02-18 18:08:43 +0000833StoredDiagnostic::StoredDiagnostic() { }
834
David Blaikied6471f72011-09-25 23:23:43 +0000835StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000836 StringRef Message)
Benjamin Kramera6a32e22010-11-19 17:36:51 +0000837 : ID(ID), Level(Level), Loc(), Message(Message) { }
Douglas Gregora88084b2010-02-18 18:08:43 +0000838
David Blaikied6471f72011-09-25 23:23:43 +0000839StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level,
David Blaikie40847cf2011-09-26 01:18:08 +0000840 const Diagnostic &Info)
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000841 : ID(Info.getID()), Level(Level)
842{
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000843 assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
844 "Valid source location without setting a source manager for diagnostic");
845 if (Info.getLocation().isValid())
846 Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000847 SmallString<64> Message;
Douglas Gregora88084b2010-02-18 18:08:43 +0000848 Info.FormatDiagnostic(Message);
849 this->Message.assign(Message.begin(), Message.end());
850
851 Ranges.reserve(Info.getNumRanges());
852 for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I)
853 Ranges.push_back(Info.getRange(I));
854
Douglas Gregor849b2432010-03-31 17:46:05 +0000855 FixIts.reserve(Info.getNumFixItHints());
856 for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I)
857 FixIts.push_back(Info.getFixItHint(I));
Douglas Gregora88084b2010-02-18 18:08:43 +0000858}
859
David Blaikied6471f72011-09-25 23:23:43 +0000860StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000861 StringRef Message, FullSourceLoc Loc,
Chris Lattner2d3ba4f2011-07-23 17:14:25 +0000862 ArrayRef<CharSourceRange> Ranges,
863 ArrayRef<FixItHint> Fixits)
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000864 : ID(ID), Level(Level), Loc(Loc), Message(Message)
865{
866 this->Ranges.assign(Ranges.begin(), Ranges.end());
867 this->FixIts.assign(FixIts.begin(), FixIts.end());
868}
869
Douglas Gregora88084b2010-02-18 18:08:43 +0000870StoredDiagnostic::~StoredDiagnostic() { }
871
Ted Kremenekcabe6682009-01-23 20:28:53 +0000872/// IncludeInDiagnosticCounts - This method (whose default implementation
873/// returns true) indicates whether the diagnostics handled by this
David Blaikie78ad0b92011-09-25 23:39:51 +0000874/// DiagnosticConsumer should be included in the number of diagnostics
David Blaikied6471f72011-09-25 23:23:43 +0000875/// reported by DiagnosticsEngine.
David Blaikie78ad0b92011-09-25 23:39:51 +0000876bool DiagnosticConsumer::IncludeInDiagnosticCounts() const { return true; }
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000877
David Blaikie99ba9e32011-12-20 02:48:34 +0000878void IgnoringDiagConsumer::anchor() { }
879
Benjamin Kramerd7a3e2c2012-02-07 22:29:24 +0000880PartialDiagnostic::StorageAllocator::StorageAllocator() {
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000881 for (unsigned I = 0; I != NumCached; ++I)
882 FreeList[I] = Cached + I;
883 NumFreeListEntries = NumCached;
884}
885
Benjamin Kramerd7a3e2c2012-02-07 22:29:24 +0000886PartialDiagnostic::StorageAllocator::~StorageAllocator() {
Chad Rosierdfaee492012-02-07 23:24:49 +0000887 // Don't assert if we are in a CrashRecovery context, as this invariant may
888 // be invalidated during a crash.
889 assert((NumFreeListEntries == NumCached ||
890 llvm::CrashRecoveryContext::isRecoveringFromCrash()) &&
891 "A partial is on the lamb");
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000892}