blob: dbcf74529cad8541e3ef52878a2e16a8c6d314d8 [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;
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
David Blaikied6471f72011-09-25 23:23:43 +0000158void DiagnosticsEngine::setDiagnosticMapping(diag::kind Diag, diag::Mapping Map,
Chad Rosierdfaee492012-02-07 23:24:49 +0000159 SourceLocation L) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000160 assert(Diag < diag::DIAG_UPPER_LIMIT &&
161 "Can only map builtin diagnostics");
162 assert((Diags->isBuiltinWarningOrExtension(Diag) ||
163 (Map == diag::MAP_FATAL || Map == diag::MAP_ERROR)) &&
164 "Cannot map errors into warnings!");
165 assert(!DiagStatePoints.empty());
166
167 FullSourceLoc Loc(L, *SourceMgr);
168 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
Chad Rosier7a0a31c2012-02-03 01:49:51 +0000169 // Don't allow a mapping to a warning override an error/fatal mapping.
170 if (Map == diag::MAP_WARNING) {
171 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(Diag);
172 if (Info.getMapping() == diag::MAP_ERROR ||
173 Info.getMapping() == diag::MAP_FATAL)
174 Map = Info.getMapping();
175 }
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +0000176 DiagnosticMappingInfo MappingInfo = makeMappingInfo(Map, L);
Daniel Dunbar53201a82011-10-04 21:17:24 +0000177
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000178 // Common case; setting all the diagnostics of a group in one place.
179 if (Loc.isInvalid() || Loc == LastStateChangePos) {
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000180 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000181 return;
182 }
183
184 // Another common case; modifying diagnostic state in a source location
185 // after the previous one.
186 if ((Loc.isValid() && LastStateChangePos.isInvalid()) ||
187 LastStateChangePos.isBeforeInTranslationUnitThan(Loc)) {
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000188 // A diagnostic pragma occurred, create a new DiagState initialized with
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000189 // the current one and a new DiagStatePoint to record at which location
190 // the new state became active.
191 DiagStates.push_back(*GetCurDiagState());
192 PushDiagStatePoint(&DiagStates.back(), Loc);
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000193 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000194 return;
195 }
196
197 // We allow setting the diagnostic state in random source order for
198 // completeness but it should not be actually happening in normal practice.
199
200 DiagStatePointsTy::iterator Pos = GetDiagStatePointForLoc(Loc);
201 assert(Pos != DiagStatePoints.end());
202
203 // Update all diagnostic states that are active after the given location.
204 for (DiagStatePointsTy::iterator
205 I = Pos+1, E = DiagStatePoints.end(); I != E; ++I) {
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000206 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000207 }
208
209 // If the location corresponds to an existing point, just update its state.
210 if (Pos->Loc == Loc) {
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000211 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000212 return;
213 }
214
215 // Create a new state/point and fit it into the vector of DiagStatePoints
216 // so that the vector is always ordered according to location.
217 Pos->Loc.isBeforeInTranslationUnitThan(Loc);
218 DiagStates.push_back(*Pos->State);
219 DiagState *NewState = &DiagStates.back();
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000220 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000221 DiagStatePoints.insert(Pos+1, DiagStatePoint(NewState,
222 FullSourceLoc(Loc, *SourceMgr)));
223}
224
Daniel Dunbar3f839462011-09-29 01:47:16 +0000225bool DiagnosticsEngine::setDiagnosticGroupMapping(
226 StringRef Group, diag::Mapping Map, SourceLocation Loc)
227{
228 // Get the diagnostics in this group.
229 llvm::SmallVector<diag::kind, 8> GroupDiags;
230 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
231 return true;
232
233 // Set the mapping.
234 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i)
235 setDiagnosticMapping(GroupDiags[i], Map, Loc);
236
237 return false;
238}
239
Chad Rosier3f225092012-02-07 19:55:45 +0000240void DiagnosticsEngine::setDiagnosticWarningAsError(diag::kind Diag,
241 bool Enabled) {
242 // If we are enabling this feature, just set the diagnostic mappings to map to
243 // errors.
244 if (Enabled)
245 setDiagnosticMapping(Diag, diag::MAP_ERROR, SourceLocation());
246
247 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
248 // potentially downgrade anything already mapped to be a warning.
249 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(Diag);
250
251 if (Info.getMapping() == diag::MAP_ERROR ||
252 Info.getMapping() == diag::MAP_FATAL)
253 Info.setMapping(diag::MAP_WARNING);
254
255 Info.setNoWarningAsError(true);
256}
257
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000258bool DiagnosticsEngine::setDiagnosticGroupWarningAsError(StringRef Group,
259 bool Enabled) {
Daniel Dunbara5e41332011-09-29 01:52:06 +0000260 // If we are enabling this feature, just set the diagnostic mappings to map to
261 // errors.
262 if (Enabled)
263 return setDiagnosticGroupMapping(Group, diag::MAP_ERROR);
264
265 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
266 // potentially downgrade anything already mapped to be a warning.
267
268 // Get the diagnostics in this group.
269 llvm::SmallVector<diag::kind, 8> GroupDiags;
270 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
271 return true;
272
273 // Perform the mapping change.
274 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i) {
275 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(
276 GroupDiags[i]);
277
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000278 if (Info.getMapping() == diag::MAP_ERROR ||
279 Info.getMapping() == diag::MAP_FATAL)
280 Info.setMapping(diag::MAP_WARNING);
281
Daniel Dunbara5e41332011-09-29 01:52:06 +0000282 Info.setNoWarningAsError(true);
283 }
284
285 return false;
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000286}
287
Chad Rosier3f225092012-02-07 19:55:45 +0000288void DiagnosticsEngine::setDiagnosticErrorAsFatal(diag::kind Diag,
289 bool Enabled) {
290 // If we are enabling this feature, just set the diagnostic mappings to map to
291 // errors.
292 if (Enabled)
293 setDiagnosticMapping(Diag, diag::MAP_FATAL, SourceLocation());
294
295 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
296 // potentially downgrade anything already mapped to be a warning.
297 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(Diag);
298
299 if (Info.getMapping() == diag::MAP_FATAL)
300 Info.setMapping(diag::MAP_ERROR);
301
302 Info.setNoErrorAsFatal(true);
303}
304
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000305bool DiagnosticsEngine::setDiagnosticGroupErrorAsFatal(StringRef Group,
306 bool Enabled) {
Daniel Dunbara5e41332011-09-29 01:52:06 +0000307 // If we are enabling this feature, just set the diagnostic mappings to map to
308 // fatal errors.
309 if (Enabled)
310 return setDiagnosticGroupMapping(Group, diag::MAP_FATAL);
311
312 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
313 // potentially downgrade anything already mapped to be an error.
314
315 // Get the diagnostics in this group.
316 llvm::SmallVector<diag::kind, 8> GroupDiags;
317 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
318 return true;
319
320 // Perform the mapping change.
321 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i) {
322 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(
323 GroupDiags[i]);
324
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000325 if (Info.getMapping() == diag::MAP_FATAL)
326 Info.setMapping(diag::MAP_ERROR);
327
Daniel Dunbara5e41332011-09-29 01:52:06 +0000328 Info.setNoErrorAsFatal(true);
329 }
330
331 return false;
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000332}
333
Argyrios Kyrtzidis82e64112012-01-28 04:35:52 +0000334void DiagnosticsEngine::setMappingToAllDiagnostics(diag::Mapping Map,
Argyrios Kyrtzidis11583c72012-01-27 06:15:43 +0000335 SourceLocation Loc) {
336 // Get all the diagnostics.
337 llvm::SmallVector<diag::kind, 64> AllDiags;
338 Diags->getAllDiagnostics(AllDiags);
339
340 // Set the mapping.
341 for (unsigned i = 0, e = AllDiags.size(); i != e; ++i)
342 if (Diags->isBuiltinWarningOrExtension(AllDiags[i]))
343 setDiagnosticMapping(AllDiags[i], Map, Loc);
Argyrios Kyrtzidis11583c72012-01-27 06:15:43 +0000344}
345
David Blaikied6471f72011-09-25 23:23:43 +0000346void DiagnosticsEngine::Report(const StoredDiagnostic &storedDiag) {
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000347 assert(CurDiagID == ~0U && "Multiple diagnostics in flight at once!");
348
349 CurDiagLoc = storedDiag.getLocation();
350 CurDiagID = storedDiag.getID();
351 NumDiagArgs = 0;
352
353 NumDiagRanges = storedDiag.range_size();
Daniel Dunbar981e2792012-03-13 18:21:17 +0000354 assert(NumDiagRanges < DiagnosticsEngine::MaxRanges &&
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000355 "Too many arguments to diagnostic!");
356 unsigned i = 0;
357 for (StoredDiagnostic::range_iterator
358 RI = storedDiag.range_begin(),
359 RE = storedDiag.range_end(); RI != RE; ++RI)
360 DiagRanges[i++] = *RI;
361
Daniel Dunbar981e2792012-03-13 18:21:17 +0000362 assert(NumDiagRanges < DiagnosticsEngine::MaxFixItHints &&
363 "Too many arguments to diagnostic!");
364 NumDiagFixItHints = 0;
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000365 for (StoredDiagnostic::fixit_iterator
366 FI = storedDiag.fixit_begin(),
367 FE = storedDiag.fixit_end(); FI != FE; ++FI)
Daniel Dunbar981e2792012-03-13 18:21:17 +0000368 DiagFixItHints[NumDiagFixItHints++] = *FI;
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000369
David Blaikie78ad0b92011-09-25 23:39:51 +0000370 assert(Client && "DiagnosticConsumer not set!");
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000371 Level DiagLevel = storedDiag.getLevel();
David Blaikie40847cf2011-09-26 01:18:08 +0000372 Diagnostic Info(this, storedDiag.getMessage());
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000373 Client->HandleDiagnostic(DiagLevel, Info);
374 if (Client->IncludeInDiagnosticCounts()) {
David Blaikied6471f72011-09-25 23:23:43 +0000375 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000376 ++NumWarnings;
377 }
378
379 CurDiagID = ~0U;
380}
381
Daniel Dunbar3054f092012-03-13 21:02:14 +0000382bool DiagnosticsEngine::EmitCurrentDiagnostic() {
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000383 // Process the diagnostic, sending the accumulated information to the
David Blaikie78ad0b92011-09-25 23:39:51 +0000384 // DiagnosticConsumer.
Daniel Dunbar3054f092012-03-13 21:02:14 +0000385 bool Emitted = ProcessDiag();
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000386
387 // Clear out the current diagnostic object.
Daniel Dunbar3054f092012-03-13 21:02:14 +0000388 unsigned DiagID = CurDiagID;
389 Clear();
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000390
391 // If there was a delayed diagnostic, emit it now.
Daniel Dunbar3054f092012-03-13 21:02:14 +0000392 if (DelayedDiagID && DelayedDiagID != DiagID)
393 ReportDelayed();
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000394
395 return Emitted;
396}
397
Nico Weber7bfaaae2008-08-10 19:59:06 +0000398
David Blaikie78ad0b92011-09-25 23:39:51 +0000399DiagnosticConsumer::~DiagnosticConsumer() {}
Nico Weber7bfaaae2008-08-10 19:59:06 +0000400
David Blaikie78ad0b92011-09-25 23:39:51 +0000401void DiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
David Blaikie40847cf2011-09-26 01:18:08 +0000402 const Diagnostic &Info) {
Argyrios Kyrtzidisf2224d82010-11-18 20:06:46 +0000403 if (!IncludeInDiagnosticCounts())
404 return;
405
David Blaikied6471f72011-09-25 23:23:43 +0000406 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidisf2224d82010-11-18 20:06:46 +0000407 ++NumWarnings;
David Blaikied6471f72011-09-25 23:23:43 +0000408 else if (DiagLevel >= DiagnosticsEngine::Error)
Argyrios Kyrtzidisf2224d82010-11-18 20:06:46 +0000409 ++NumErrors;
410}
Chris Lattnerf4c83962008-11-19 06:51:40 +0000411
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000412/// ModifierIs - Return true if the specified modifier matches specified string.
413template <std::size_t StrLen>
414static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
415 const char (&Str)[StrLen]) {
416 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
417}
418
John McCall909c1822010-01-14 20:11:39 +0000419/// ScanForward - Scans forward, looking for the given character, skipping
420/// nested clauses and escaped characters.
421static const char *ScanFormat(const char *I, const char *E, char Target) {
422 unsigned Depth = 0;
423
424 for ( ; I != E; ++I) {
425 if (Depth == 0 && *I == Target) return I;
426 if (Depth != 0 && *I == '}') Depth--;
427
428 if (*I == '%') {
429 I++;
430 if (I == E) break;
431
432 // Escaped characters get implicitly skipped here.
433
434 // Format specifier.
435 if (!isdigit(*I) && !ispunct(*I)) {
436 for (I++; I != E && !isdigit(*I) && *I != '{'; I++) ;
437 if (I == E) break;
438 if (*I == '{')
439 Depth++;
440 }
441 }
442 }
443 return E;
444}
445
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000446/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
447/// like this: %select{foo|bar|baz}2. This means that the integer argument
448/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
449/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
450/// This is very useful for certain classes of variant diagnostics.
David Blaikie40847cf2011-09-26 01:18:08 +0000451static void HandleSelectModifier(const Diagnostic &DInfo, unsigned ValNo,
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000452 const char *Argument, unsigned ArgumentLen,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000453 SmallVectorImpl<char> &OutStr) {
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000454 const char *ArgumentEnd = Argument+ArgumentLen;
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000456 // Skip over 'ValNo' |'s.
457 while (ValNo) {
John McCall909c1822010-01-14 20:11:39 +0000458 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000459 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
460 " larger than the number of options in the diagnostic string!");
461 Argument = NextVal+1; // Skip this string.
462 --ValNo;
463 }
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000465 // Get the end of the value. This is either the } or the |.
John McCall909c1822010-01-14 20:11:39 +0000466 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
John McCall9f286142010-01-13 23:58:20 +0000467
468 // Recursively format the result of the select clause into the output string.
469 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000470}
471
472/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
473/// letter 's' to the string if the value is not 1. This is used in cases like
474/// this: "you idiot, you have %4 parameter%s4!".
475static void HandleIntegerSModifier(unsigned ValNo,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000476 SmallVectorImpl<char> &OutStr) {
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000477 if (ValNo != 1)
478 OutStr.push_back('s');
479}
480
John McCall3be16b72010-01-14 00:50:32 +0000481/// HandleOrdinalModifier - Handle the integer 'ord' modifier. This
482/// prints the ordinal form of the given integer, with 1 corresponding
483/// to the first ordinal. Currently this is hard-coded to use the
484/// English form.
485static void HandleOrdinalModifier(unsigned ValNo,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000486 SmallVectorImpl<char> &OutStr) {
John McCall3be16b72010-01-14 00:50:32 +0000487 assert(ValNo != 0 && "ValNo must be strictly positive!");
488
489 llvm::raw_svector_ostream Out(OutStr);
490
491 // We could use text forms for the first N ordinals, but the numeric
492 // forms are actually nicer in diagnostics because they stand out.
493 Out << ValNo;
494
495 // It is critically important that we do this perfectly for
496 // user-written sequences with over 100 elements.
497 switch (ValNo % 100) {
498 case 11:
499 case 12:
500 case 13:
501 Out << "th"; return;
502 default:
503 switch (ValNo % 10) {
504 case 1: Out << "st"; return;
505 case 2: Out << "nd"; return;
506 case 3: Out << "rd"; return;
507 default: Out << "th"; return;
508 }
509 }
510}
511
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000512
Sebastian Redle4c452c2008-11-22 13:44:36 +0000513/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000514static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000515 // Programming 101: Parse a decimal number :-)
516 unsigned Val = 0;
517 while (Start != End && *Start >= '0' && *Start <= '9') {
518 Val *= 10;
519 Val += *Start - '0';
520 ++Start;
521 }
522 return Val;
523}
524
525/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000526static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000527 if (*Start != '[') {
528 unsigned Ref = PluralNumber(Start, End);
529 return Ref == Val;
530 }
531
532 ++Start;
533 unsigned Low = PluralNumber(Start, End);
534 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
535 ++Start;
536 unsigned High = PluralNumber(Start, End);
537 assert(*Start == ']' && "Bad plural expression syntax: expected )");
538 ++Start;
539 return Low <= Val && Val <= High;
540}
541
542/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000543static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000544 // Empty condition?
545 if (*Start == ':')
546 return true;
547
548 while (1) {
549 char C = *Start;
550 if (C == '%') {
551 // Modulo expression
552 ++Start;
553 unsigned Arg = PluralNumber(Start, End);
554 assert(*Start == '=' && "Bad plural expression syntax: expected =");
555 ++Start;
556 unsigned ValMod = ValNo % Arg;
557 if (TestPluralRange(ValMod, Start, End))
558 return true;
559 } else {
Sebastian Redle2065322008-11-27 07:28:14 +0000560 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redle4c452c2008-11-22 13:44:36 +0000561 "Bad plural expression syntax: unexpected character");
562 // Range expression
563 if (TestPluralRange(ValNo, Start, End))
564 return true;
565 }
566
567 // Scan for next or-expr part.
568 Start = std::find(Start, End, ',');
Mike Stump1eb44332009-09-09 15:08:12 +0000569 if (Start == End)
Sebastian Redle4c452c2008-11-22 13:44:36 +0000570 break;
571 ++Start;
572 }
573 return false;
574}
575
576/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
577/// for complex plural forms, or in languages where all plurals are complex.
578/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
579/// conditions that are tested in order, the form corresponding to the first
580/// that applies being emitted. The empty condition is always true, making the
581/// last form a default case.
582/// Conditions are simple boolean expressions, where n is the number argument.
583/// Here are the rules.
584/// condition := expression | empty
585/// empty := -> always true
586/// expression := numeric [',' expression] -> logical or
587/// numeric := range -> true if n in range
588/// | '%' number '=' range -> true if n % number in range
589/// range := number
590/// | '[' number ',' number ']' -> ranges are inclusive both ends
591///
592/// Here are some examples from the GNU gettext manual written in this form:
593/// English:
594/// {1:form0|:form1}
595/// Latvian:
596/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
597/// Gaeilge:
598/// {1:form0|2:form1|:form2}
599/// Romanian:
600/// {1:form0|0,%100=[1,19]:form1|:form2}
601/// Lithuanian:
602/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
603/// Russian (requires repeated form):
604/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
605/// Slovak
606/// {1:form0|[2,4]:form1|:form2}
607/// Polish (requires repeated form):
608/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
David Blaikie40847cf2011-09-26 01:18:08 +0000609static void HandlePluralModifier(const Diagnostic &DInfo, unsigned ValNo,
Sebastian Redle4c452c2008-11-22 13:44:36 +0000610 const char *Argument, unsigned ArgumentLen,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000611 SmallVectorImpl<char> &OutStr) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000612 const char *ArgumentEnd = Argument + ArgumentLen;
613 while (1) {
614 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
615 const char *ExprEnd = Argument;
616 while (*ExprEnd != ':') {
617 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
618 ++ExprEnd;
619 }
620 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
621 Argument = ExprEnd + 1;
John McCall909c1822010-01-14 20:11:39 +0000622 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
John McCalle53a44b2010-10-14 01:55:31 +0000623
624 // Recursively format the result of the plural clause into the
625 // output string.
626 DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000627 return;
628 }
John McCall909c1822010-01-14 20:11:39 +0000629 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
Sebastian Redle4c452c2008-11-22 13:44:36 +0000630 }
631}
632
633
Chris Lattnerf4c83962008-11-19 06:51:40 +0000634/// FormatDiagnostic - Format this diagnostic into a string, substituting the
635/// formal arguments into the %0 slots. The result is appended onto the Str
636/// array.
David Blaikie40847cf2011-09-26 01:18:08 +0000637void Diagnostic::
Chris Lattner5f9e2722011-07-23 10:55:15 +0000638FormatDiagnostic(SmallVectorImpl<char> &OutStr) const {
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000639 if (!StoredDiagMessage.empty()) {
640 OutStr.append(StoredDiagMessage.begin(), StoredDiagMessage.end());
641 return;
642 }
643
Chris Lattner5f9e2722011-07-23 10:55:15 +0000644 StringRef Diag =
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000645 getDiags()->getDiagnosticIDs()->getDescription(getID());
Mike Stump1eb44332009-09-09 15:08:12 +0000646
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000647 FormatDiagnostic(Diag.begin(), Diag.end(), OutStr);
John McCall9f286142010-01-13 23:58:20 +0000648}
649
David Blaikie40847cf2011-09-26 01:18:08 +0000650void Diagnostic::
John McCall9f286142010-01-13 23:58:20 +0000651FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000652 SmallVectorImpl<char> &OutStr) const {
John McCall9f286142010-01-13 23:58:20 +0000653
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000654 /// FormattedArgs - Keep track of all of the arguments formatted by
655 /// ConvertArgToString and pass them into subsequent calls to
656 /// ConvertArgToString, allowing the implementation to avoid redundancies in
657 /// obvious cases.
David Blaikied6471f72011-09-25 23:23:43 +0000658 SmallVector<DiagnosticsEngine::ArgumentValue, 8> FormattedArgs;
Chandler Carruth0673cb32011-07-11 17:49:21 +0000659
660 /// QualTypeVals - Pass a vector of arrays so that QualType names can be
661 /// compared to see if more information is needed to be printed.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000662 SmallVector<intptr_t, 2> QualTypeVals;
Chandler Carruth0673cb32011-07-11 17:49:21 +0000663 for (unsigned i = 0, e = getNumArgs(); i < e; ++i)
David Blaikied6471f72011-09-25 23:23:43 +0000664 if (getArgKind(i) == DiagnosticsEngine::ak_qualtype)
Chandler Carruth0673cb32011-07-11 17:49:21 +0000665 QualTypeVals.push_back(getRawArg(i));
666
Chris Lattnerf4c83962008-11-19 06:51:40 +0000667 while (DiagStr != DiagEnd) {
668 if (DiagStr[0] != '%') {
669 // Append non-%0 substrings to Str if we have one.
670 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
671 OutStr.append(DiagStr, StrEnd);
672 DiagStr = StrEnd;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000673 continue;
John McCall909c1822010-01-14 20:11:39 +0000674 } else if (ispunct(DiagStr[1])) {
675 OutStr.push_back(DiagStr[1]); // %% -> %.
Chris Lattnerf4c83962008-11-19 06:51:40 +0000676 DiagStr += 2;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000677 continue;
678 }
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000680 // Skip the %.
681 ++DiagStr;
Mike Stump1eb44332009-09-09 15:08:12 +0000682
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000683 // This must be a placeholder for a diagnostic argument. The format for a
684 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
685 // The digit is a number from 0-9 indicating which argument this comes from.
686 // The modifier is a string of digits from the set [-a-z]+, arguments is a
687 // brace enclosed string.
688 const char *Modifier = 0, *Argument = 0;
689 unsigned ModifierLen = 0, ArgumentLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000691 // Check to see if we have a modifier. If so eat it.
692 if (!isdigit(DiagStr[0])) {
693 Modifier = DiagStr;
694 while (DiagStr[0] == '-' ||
695 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
696 ++DiagStr;
697 ModifierLen = DiagStr-Modifier;
Chris Lattnerf4c83962008-11-19 06:51:40 +0000698
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000699 // If we have an argument, get it next.
700 if (DiagStr[0] == '{') {
701 ++DiagStr; // Skip {.
702 Argument = DiagStr;
Mike Stump1eb44332009-09-09 15:08:12 +0000703
John McCall909c1822010-01-14 20:11:39 +0000704 DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
705 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000706 ArgumentLen = DiagStr-Argument;
707 ++DiagStr; // Skip }.
Chris Lattnerf4c83962008-11-19 06:51:40 +0000708 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000709 }
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000711 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner22caddc2008-11-23 09:13:29 +0000712 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000713
David Blaikied6471f72011-09-25 23:23:43 +0000714 DiagnosticsEngine::ArgumentKind Kind = getArgKind(ArgNo);
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000715
716 switch (Kind) {
Chris Lattner08631c52008-11-23 21:45:46 +0000717 // ---- STRINGS ----
David Blaikied6471f72011-09-25 23:23:43 +0000718 case DiagnosticsEngine::ak_std_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000719 const std::string &S = getArgStdStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000720 assert(ModifierLen == 0 && "No modifiers for strings yet");
721 OutStr.append(S.begin(), S.end());
722 break;
723 }
David Blaikied6471f72011-09-25 23:23:43 +0000724 case DiagnosticsEngine::ak_c_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000725 const char *S = getArgCStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000726 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbare46e3542009-04-20 06:13:16 +0000727
728 // Don't crash if get passed a null pointer by accident.
729 if (!S)
730 S = "(null)";
Mike Stump1eb44332009-09-09 15:08:12 +0000731
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000732 OutStr.append(S, S + strlen(S));
733 break;
734 }
Chris Lattner08631c52008-11-23 21:45:46 +0000735 // ---- INTEGERS ----
David Blaikied6471f72011-09-25 23:23:43 +0000736 case DiagnosticsEngine::ak_sint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000737 int Val = getArgSInt(ArgNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000739 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCalle53a44b2010-10-14 01:55:31 +0000740 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen,
741 OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000742 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
743 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000744 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCalle53a44b2010-10-14 01:55:31 +0000745 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
746 OutStr);
John McCall3be16b72010-01-14 00:50:32 +0000747 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
748 HandleOrdinalModifier((unsigned)Val, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000749 } else {
750 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbar23e47c62009-10-17 18:12:14 +0000751 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner30bc9652008-11-19 07:22:31 +0000752 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000753 break;
754 }
David Blaikied6471f72011-09-25 23:23:43 +0000755 case DiagnosticsEngine::ak_uint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000756 unsigned Val = getArgUInt(ArgNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000758 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCall9f286142010-01-13 23:58:20 +0000759 HandleSelectModifier(*this, Val, Argument, ArgumentLen, 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(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 Lattner22caddc2008-11-23 09:13:29 +0000771 break;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000772 }
Chris Lattner08631c52008-11-23 21:45:46 +0000773 // ---- NAMES and TYPES ----
David Blaikied6471f72011-09-25 23:23:43 +0000774 case DiagnosticsEngine::ak_identifierinfo: {
Chris Lattner08631c52008-11-23 21:45:46 +0000775 const IdentifierInfo *II = getArgIdentifier(ArgNo);
776 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbare46e3542009-04-20 06:13:16 +0000777
778 // Don't crash if get passed a null pointer by accident.
779 if (!II) {
780 const char *S = "(null)";
781 OutStr.append(S, S + strlen(S));
782 continue;
783 }
784
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000785 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
Chris Lattner08631c52008-11-23 21:45:46 +0000786 break;
787 }
David Blaikied6471f72011-09-25 23:23:43 +0000788 case DiagnosticsEngine::ak_qualtype:
789 case DiagnosticsEngine::ak_declarationname:
790 case DiagnosticsEngine::ak_nameddecl:
791 case DiagnosticsEngine::ak_nestednamespec:
792 case DiagnosticsEngine::ak_declcontext:
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000793 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000794 Modifier, ModifierLen,
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000795 Argument, ArgumentLen,
796 FormattedArgs.data(), FormattedArgs.size(),
Chandler Carruth0673cb32011-07-11 17:49:21 +0000797 OutStr, QualTypeVals);
Chris Lattner22caddc2008-11-23 09:13:29 +0000798 break;
Nico Weber7bfaaae2008-08-10 19:59:06 +0000799 }
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000800
801 // Remember this argument info for subsequent formatting operations. Turn
802 // std::strings into a null terminated string to make it be the same case as
803 // all the other ones.
David Blaikied6471f72011-09-25 23:23:43 +0000804 if (Kind != DiagnosticsEngine::ak_std_string)
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000805 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
806 else
David Blaikied6471f72011-09-25 23:23:43 +0000807 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_c_string,
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000808 (intptr_t)getArgStdStr(ArgNo).c_str()));
809
Nico Weber7bfaaae2008-08-10 19:59:06 +0000810 }
Nico Weber7bfaaae2008-08-10 19:59:06 +0000811}
Ted Kremenekcabe6682009-01-23 20:28:53 +0000812
Douglas Gregora88084b2010-02-18 18:08:43 +0000813StoredDiagnostic::StoredDiagnostic() { }
814
David Blaikied6471f72011-09-25 23:23:43 +0000815StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000816 StringRef Message)
Benjamin Kramera6a32e22010-11-19 17:36:51 +0000817 : ID(ID), Level(Level), Loc(), Message(Message) { }
Douglas Gregora88084b2010-02-18 18:08:43 +0000818
David Blaikied6471f72011-09-25 23:23:43 +0000819StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level,
David Blaikie40847cf2011-09-26 01:18:08 +0000820 const Diagnostic &Info)
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000821 : ID(Info.getID()), Level(Level)
822{
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000823 assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
824 "Valid source location without setting a source manager for diagnostic");
825 if (Info.getLocation().isValid())
826 Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000827 SmallString<64> Message;
Douglas Gregora88084b2010-02-18 18:08:43 +0000828 Info.FormatDiagnostic(Message);
829 this->Message.assign(Message.begin(), Message.end());
830
831 Ranges.reserve(Info.getNumRanges());
832 for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I)
833 Ranges.push_back(Info.getRange(I));
834
Douglas Gregor849b2432010-03-31 17:46:05 +0000835 FixIts.reserve(Info.getNumFixItHints());
836 for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I)
837 FixIts.push_back(Info.getFixItHint(I));
Douglas Gregora88084b2010-02-18 18:08:43 +0000838}
839
David Blaikied6471f72011-09-25 23:23:43 +0000840StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000841 StringRef Message, FullSourceLoc Loc,
Chris Lattner2d3ba4f2011-07-23 17:14:25 +0000842 ArrayRef<CharSourceRange> Ranges,
843 ArrayRef<FixItHint> Fixits)
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000844 : ID(ID), Level(Level), Loc(Loc), Message(Message)
845{
846 this->Ranges.assign(Ranges.begin(), Ranges.end());
847 this->FixIts.assign(FixIts.begin(), FixIts.end());
848}
849
Douglas Gregora88084b2010-02-18 18:08:43 +0000850StoredDiagnostic::~StoredDiagnostic() { }
851
Ted Kremenekcabe6682009-01-23 20:28:53 +0000852/// IncludeInDiagnosticCounts - This method (whose default implementation
853/// returns true) indicates whether the diagnostics handled by this
David Blaikie78ad0b92011-09-25 23:39:51 +0000854/// DiagnosticConsumer should be included in the number of diagnostics
David Blaikied6471f72011-09-25 23:23:43 +0000855/// reported by DiagnosticsEngine.
David Blaikie78ad0b92011-09-25 23:39:51 +0000856bool DiagnosticConsumer::IncludeInDiagnosticCounts() const { return true; }
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000857
David Blaikie99ba9e32011-12-20 02:48:34 +0000858void IgnoringDiagConsumer::anchor() { }
859
Benjamin Kramerd7a3e2c2012-02-07 22:29:24 +0000860PartialDiagnostic::StorageAllocator::StorageAllocator() {
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000861 for (unsigned I = 0; I != NumCached; ++I)
862 FreeList[I] = Cached + I;
863 NumFreeListEntries = NumCached;
864}
865
Benjamin Kramerd7a3e2c2012-02-07 22:29:24 +0000866PartialDiagnostic::StorageAllocator::~StorageAllocator() {
Chad Rosierdfaee492012-02-07 23:24:49 +0000867 // Don't assert if we are in a CrashRecovery context, as this invariant may
868 // be invalidated during a crash.
869 assert((NumFreeListEntries == NumCached ||
870 llvm::CrashRecoveryContext::isRecoveringFromCrash()) &&
871 "A partial is on the lamb");
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000872}