blob: f7d5d87b367edddcab16597e8e1009d747ce0e34 [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- Diagnostic.cpp - C Language Family Diagnostic Handling -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner22eb9722006-06-18 05:43:12 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Diagnostic-related interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Ted Kremenek39a76652010-04-12 19:54:17 +000014#include "clang/Basic/Diagnostic.h"
Chris Lattnerb91fd172008-11-19 07:32:16 +000015#include "clang/Basic/IdentifierTable.h"
Ted Kremenek39a76652010-04-12 19:54:17 +000016#include "clang/Basic/PartialDiagnostic.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000017#include "llvm/ADT/SmallString.h"
Daniel Dunbare3633792009-10-17 18:12:14 +000018#include "llvm/Support/raw_ostream.h"
Ted Kremenek84de4a12011-03-21 18:40:07 +000019#include "llvm/Support/CrashRecoveryContext.h"
20
Chris Lattner22eb9722006-06-18 05:43:12 +000021using namespace clang;
22
David Blaikie9c902b52011-09-25 23:23:43 +000023static void DummyArgToStringFn(DiagnosticsEngine::ArgumentKind AK, intptr_t QT,
Chris Lattner63ecc502008-11-23 09:21:17 +000024 const char *Modifier, unsigned ML,
25 const char *Argument, unsigned ArgLen,
David Blaikie9c902b52011-09-25 23:23:43 +000026 const DiagnosticsEngine::ArgumentValue *PrevArgs,
Chris Lattnerc243f292009-10-20 05:25:22 +000027 unsigned NumPrevArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000028 SmallVectorImpl<char> &Output,
Chandler Carruthd5173952011-07-11 17:49:21 +000029 void *Cookie,
Bill Wendling8eb771d2012-02-22 09:51:33 +000030 ArrayRef<intptr_t> QualTypeVals) {
Chris Lattner63ecc502008-11-23 09:21:17 +000031 const char *Str = "<can't format argument>";
Chris Lattner6a2ed6f2008-11-23 09:13:29 +000032 Output.append(Str, Str+strlen(Str));
33}
34
35
David Blaikie9c902b52011-09-25 23:23:43 +000036DiagnosticsEngine::DiagnosticsEngine(
Dylan Noblesmithc95d8192012-02-20 14:00:23 +000037 const IntrusiveRefCntPtr<DiagnosticIDs> &diags,
David Blaikiee2eefae2011-09-25 23:39:51 +000038 DiagnosticConsumer *client, bool ShouldOwnClient)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000039 : Diags(diags), Client(client), OwnsDiagClient(ShouldOwnClient),
40 SourceMgr(0) {
Chris Lattner63ecc502008-11-23 09:21:17 +000041 ArgToStringFn = DummyArgToStringFn;
Chris Lattnercf868c42009-02-19 23:53:20 +000042 ArgToStringCookie = 0;
Mike Stump11289f42009-09-09 15:08:12 +000043
Douglas Gregor0e119552010-07-31 00:40:00 +000044 AllExtensionsSilenced = 0;
45 IgnoreAllWarnings = false;
46 WarningsAsErrors = false;
Ted Kremenekfbbdced2011-08-18 01:12:56 +000047 EnableAllWarnings = false;
Douglas Gregor0e119552010-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 Smithf6f003a2011-12-16 19:06:07 +000056 ConstexprBacktraceLimit = 0;
Douglas Gregor0e119552010-07-31 00:40:00 +000057
Douglas Gregoraa21cc42010-07-19 21:46:24 +000058 Reset();
Chris Lattnerae411572006-07-05 00:55:08 +000059}
60
David Blaikie9c902b52011-09-25 23:23:43 +000061DiagnosticsEngine::~DiagnosticsEngine() {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000062 if (OwnsDiagClient)
63 delete Client;
Chris Lattnere6535cf2007-12-02 01:09:57 +000064}
65
David Blaikiee2eefae2011-09-25 23:39:51 +000066void DiagnosticsEngine::setClient(DiagnosticConsumer *client,
David Blaikie9c902b52011-09-25 23:23:43 +000067 bool ShouldOwnClient) {
Douglas Gregor7a964ad2011-01-31 22:04:05 +000068 if (OwnsDiagClient && Client)
69 delete Client;
70
71 Client = client;
72 OwnsDiagClient = ShouldOwnClient;
73}
Chris Lattnerfb42a182009-07-12 21:18:45 +000074
David Blaikie9c902b52011-09-25 23:23:43 +000075void DiagnosticsEngine::pushMappings(SourceLocation Loc) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +000076 DiagStateOnPushStack.push_back(GetCurDiagState());
Chris Lattnerfb42a182009-07-12 21:18:45 +000077}
78
David Blaikie9c902b52011-09-25 23:23:43 +000079bool DiagnosticsEngine::popMappings(SourceLocation Loc) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +000080 if (DiagStateOnPushStack.empty())
Chris Lattnerfb42a182009-07-12 21:18:45 +000081 return false;
82
Argyrios Kyrtzidis1cb0de12010-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 Lattnerfb42a182009-07-12 21:18:45 +000088 return true;
89}
90
David Blaikie9c902b52011-09-25 23:23:43 +000091void DiagnosticsEngine::Reset() {
Douglas Gregoraa21cc42010-07-19 21:46:24 +000092 ErrorOccurred = false;
93 FatalErrorOccurred = false;
Douglas Gregor8a60bbe2011-07-06 17:40:26 +000094 UnrecoverableErrorOccurred = false;
Douglas Gregoraa21cc42010-07-19 21:46:24 +000095
96 NumWarnings = 0;
97 NumErrors = 0;
98 NumErrorsSuppressed = 0;
Argyrios Kyrtzidis1fa8b4b2011-07-29 01:25:44 +000099 TrapNumErrorsOccurred = 0;
100 TrapNumUnrecoverableErrorsOccurred = 0;
Douglas Gregor8a60bbe2011-07-06 17:40:26 +0000101
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000102 CurDiagID = ~0U;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000103 // Set LastDiagLevel to an "unset" state. If we set it to 'Ignored', notes
David Blaikie9c902b52011-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 Kyrtzidisd0040642010-11-18 20:06:41 +0000106 // displayed.
107 LastDiagLevel = (DiagnosticIDs::Level)-1;
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000108 DelayedDiagID = 0;
Argyrios Kyrtzidisbbbeea12011-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 Gregoraa21cc42010-07-19 21:46:24 +0000119}
Chris Lattner22eb9722006-06-18 05:43:12 +0000120
David Blaikie9c902b52011-09-25 23:23:43 +0000121void DiagnosticsEngine::SetDelayedDiagnostic(unsigned DiagID, StringRef Arg1,
Chad Rosier849a67b2012-02-07 23:24:49 +0000122 StringRef Arg2) {
Douglas Gregor85795312010-03-22 15:10:57 +0000123 if (DelayedDiagID)
124 return;
125
126 DelayedDiagID = DiagID;
Douglas Gregor96380982010-03-22 15:47:45 +0000127 DelayedDiagArg1 = Arg1.str();
128 DelayedDiagArg2 = Arg2.str();
Douglas Gregor85795312010-03-22 15:10:57 +0000129}
130
David Blaikie9c902b52011-09-25 23:23:43 +0000131void DiagnosticsEngine::ReportDelayed() {
Douglas Gregor85795312010-03-22 15:10:57 +0000132 Report(DelayedDiagID) << DelayedDiagArg1 << DelayedDiagArg2;
133 DelayedDiagID = 0;
134 DelayedDiagArg1.clear();
135 DelayedDiagArg2.clear();
136}
137
David Blaikie9c902b52011-09-25 23:23:43 +0000138DiagnosticsEngine::DiagStatePointsTy::iterator
139DiagnosticsEngine::GetDiagStatePointForLoc(SourceLocation L) const {
Argyrios Kyrtzidis1cb0de12010-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 Blaikie9c902b52011-09-25 23:23:43 +0000164void DiagnosticsEngine::setDiagnosticMapping(diag::kind Diag, diag::Mapping Map,
Chad Rosier849a67b2012-02-07 23:24:49 +0000165 SourceLocation L) {
Argyrios Kyrtzidis1cb0de12010-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 Rosierd1956e42012-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 Kyrtzidisc137d0d2011-11-09 01:24:17 +0000182 DiagnosticMappingInfo MappingInfo = makeMappingInfo(Map, L);
Daniel Dunbar2fba0972011-10-04 21:17:24 +0000183
Argyrios Kyrtzidis1cb0de12010-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 Dunbar458edfa2011-09-29 01:34:47 +0000186 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis1cb0de12010-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 Lattner57540c52011-04-15 05:22:18 +0000194 // A diagnostic pragma occurred, create a new DiagState initialized with
Argyrios Kyrtzidis1cb0de12010-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 Dunbar458edfa2011-09-29 01:34:47 +0000199 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis1cb0de12010-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 Dunbar458edfa2011-09-29 01:34:47 +0000212 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis1cb0de12010-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 Dunbar458edfa2011-09-29 01:34:47 +0000217 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis1cb0de12010-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 Dunbar458edfa2011-09-29 01:34:47 +0000226 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000227 DiagStatePoints.insert(Pos+1, DiagStatePoint(NewState,
228 FullSourceLoc(Loc, *SourceMgr)));
229}
230
Daniel Dunbard908c122011-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 Rosier4c17fa72012-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 Dunbarc2e5ca62011-09-29 00:53:47 +0000264bool DiagnosticsEngine::setDiagnosticGroupWarningAsError(StringRef Group,
265 bool Enabled) {
Daniel Dunbarfffcf212011-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 Dunbar58d0af62011-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 Dunbarfffcf212011-09-29 01:52:06 +0000288 Info.setNoWarningAsError(true);
289 }
290
291 return false;
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000292}
293
Chad Rosier4c17fa72012-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 Dunbarc2e5ca62011-09-29 00:53:47 +0000311bool DiagnosticsEngine::setDiagnosticGroupErrorAsFatal(StringRef Group,
312 bool Enabled) {
Daniel Dunbarfffcf212011-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 Dunbar58d0af62011-09-29 01:58:05 +0000331 if (Info.getMapping() == diag::MAP_FATAL)
332 Info.setMapping(diag::MAP_ERROR);
333
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000334 Info.setNoErrorAsFatal(true);
335 }
336
337 return false;
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000338}
339
Argyrios Kyrtzidis059cac42012-01-28 04:35:52 +0000340void DiagnosticsEngine::setMappingToAllDiagnostics(diag::Mapping Map,
Argyrios Kyrtzidis9ffada92012-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 Kyrtzidis9ffada92012-01-27 06:15:43 +0000350}
351
David Blaikie9c902b52011-09-25 23:23:43 +0000352void DiagnosticsEngine::Report(const StoredDiagnostic &storedDiag) {
Argyrios Kyrtzidise9af37d2011-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();
Daniel Dunbar007b9dc2012-03-13 18:21:17 +0000360 assert(NumDiagRanges < DiagnosticsEngine::MaxRanges &&
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000361 "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
Daniel Dunbar007b9dc2012-03-13 18:21:17 +0000368 assert(NumDiagRanges < DiagnosticsEngine::MaxFixItHints &&
369 "Too many arguments to diagnostic!");
370 NumDiagFixItHints = 0;
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000371 for (StoredDiagnostic::fixit_iterator
372 FI = storedDiag.fixit_begin(),
373 FE = storedDiag.fixit_end(); FI != FE; ++FI)
Daniel Dunbar007b9dc2012-03-13 18:21:17 +0000374 DiagFixItHints[NumDiagFixItHints++] = *FI;
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000375
David Blaikiee2eefae2011-09-25 23:39:51 +0000376 assert(Client && "DiagnosticConsumer not set!");
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000377 Level DiagLevel = storedDiag.getLevel();
David Blaikieb5784322011-09-26 01:18:08 +0000378 Diagnostic Info(this, storedDiag.getMessage());
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000379 Client->HandleDiagnostic(DiagLevel, Info);
380 if (Client->IncludeInDiagnosticCounts()) {
David Blaikie9c902b52011-09-25 23:23:43 +0000381 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000382 ++NumWarnings;
383 }
384
385 CurDiagID = ~0U;
386}
387
Daniel Dunbarc7c00892012-03-13 21:02:14 +0000388bool DiagnosticsEngine::EmitCurrentDiagnostic() {
Douglas Gregor85795312010-03-22 15:10:57 +0000389 // Process the diagnostic, sending the accumulated information to the
David Blaikiee2eefae2011-09-25 23:39:51 +0000390 // DiagnosticConsumer.
Daniel Dunbarc7c00892012-03-13 21:02:14 +0000391 bool Emitted = ProcessDiag();
Douglas Gregor85795312010-03-22 15:10:57 +0000392
393 // Clear out the current diagnostic object.
Daniel Dunbarc7c00892012-03-13 21:02:14 +0000394 unsigned DiagID = CurDiagID;
395 Clear();
Douglas Gregor85795312010-03-22 15:10:57 +0000396
397 // If there was a delayed diagnostic, emit it now.
Daniel Dunbarc7c00892012-03-13 21:02:14 +0000398 if (DelayedDiagID && DelayedDiagID != DiagID)
399 ReportDelayed();
Douglas Gregor85795312010-03-22 15:10:57 +0000400
401 return Emitted;
402}
403
Nico Weber4c311642008-08-10 19:59:06 +0000404
David Blaikiee2eefae2011-09-25 23:39:51 +0000405DiagnosticConsumer::~DiagnosticConsumer() {}
Nico Weber4c311642008-08-10 19:59:06 +0000406
David Blaikiee2eefae2011-09-25 23:39:51 +0000407void DiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
David Blaikieb5784322011-09-26 01:18:08 +0000408 const Diagnostic &Info) {
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000409 if (!IncludeInDiagnosticCounts())
410 return;
411
David Blaikie9c902b52011-09-25 23:23:43 +0000412 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000413 ++NumWarnings;
David Blaikie9c902b52011-09-25 23:23:43 +0000414 else if (DiagLevel >= DiagnosticsEngine::Error)
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000415 ++NumErrors;
416}
Chris Lattner23be0672008-11-19 06:51:40 +0000417
Chris Lattner2b786902008-11-21 07:50:02 +0000418/// ModifierIs - Return true if the specified modifier matches specified string.
419template <std::size_t StrLen>
420static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
421 const char (&Str)[StrLen]) {
422 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
423}
424
John McCall8cb7a8a32010-01-14 20:11:39 +0000425/// ScanForward - Scans forward, looking for the given character, skipping
426/// nested clauses and escaped characters.
427static const char *ScanFormat(const char *I, const char *E, char Target) {
428 unsigned Depth = 0;
429
430 for ( ; I != E; ++I) {
431 if (Depth == 0 && *I == Target) return I;
432 if (Depth != 0 && *I == '}') Depth--;
433
434 if (*I == '%') {
435 I++;
436 if (I == E) break;
437
438 // Escaped characters get implicitly skipped here.
439
440 // Format specifier.
441 if (!isdigit(*I) && !ispunct(*I)) {
442 for (I++; I != E && !isdigit(*I) && *I != '{'; I++) ;
443 if (I == E) break;
444 if (*I == '{')
445 Depth++;
446 }
447 }
448 }
449 return E;
450}
451
Chris Lattner2b786902008-11-21 07:50:02 +0000452/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
453/// like this: %select{foo|bar|baz}2. This means that the integer argument
454/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
455/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
456/// This is very useful for certain classes of variant diagnostics.
David Blaikieb5784322011-09-26 01:18:08 +0000457static void HandleSelectModifier(const Diagnostic &DInfo, unsigned ValNo,
Chris Lattner2b786902008-11-21 07:50:02 +0000458 const char *Argument, unsigned ArgumentLen,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000459 SmallVectorImpl<char> &OutStr) {
Chris Lattner2b786902008-11-21 07:50:02 +0000460 const char *ArgumentEnd = Argument+ArgumentLen;
Mike Stump11289f42009-09-09 15:08:12 +0000461
Chris Lattner2b786902008-11-21 07:50:02 +0000462 // Skip over 'ValNo' |'s.
463 while (ValNo) {
John McCall8cb7a8a32010-01-14 20:11:39 +0000464 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
Chris Lattner2b786902008-11-21 07:50:02 +0000465 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
466 " larger than the number of options in the diagnostic string!");
467 Argument = NextVal+1; // Skip this string.
468 --ValNo;
469 }
Mike Stump11289f42009-09-09 15:08:12 +0000470
Chris Lattner2b786902008-11-21 07:50:02 +0000471 // Get the end of the value. This is either the } or the |.
John McCall8cb7a8a32010-01-14 20:11:39 +0000472 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
John McCalle4d54322010-01-13 23:58:20 +0000473
474 // Recursively format the result of the select clause into the output string.
475 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000476}
477
478/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
479/// letter 's' to the string if the value is not 1. This is used in cases like
480/// this: "you idiot, you have %4 parameter%s4!".
481static void HandleIntegerSModifier(unsigned ValNo,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000482 SmallVectorImpl<char> &OutStr) {
Chris Lattner2b786902008-11-21 07:50:02 +0000483 if (ValNo != 1)
484 OutStr.push_back('s');
485}
486
John McCall9015cde2010-01-14 00:50:32 +0000487/// HandleOrdinalModifier - Handle the integer 'ord' modifier. This
488/// prints the ordinal form of the given integer, with 1 corresponding
489/// to the first ordinal. Currently this is hard-coded to use the
490/// English form.
491static void HandleOrdinalModifier(unsigned ValNo,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000492 SmallVectorImpl<char> &OutStr) {
John McCall9015cde2010-01-14 00:50:32 +0000493 assert(ValNo != 0 && "ValNo must be strictly positive!");
494
495 llvm::raw_svector_ostream Out(OutStr);
496
497 // We could use text forms for the first N ordinals, but the numeric
498 // forms are actually nicer in diagnostics because they stand out.
499 Out << ValNo;
500
501 // It is critically important that we do this perfectly for
502 // user-written sequences with over 100 elements.
503 switch (ValNo % 100) {
504 case 11:
505 case 12:
506 case 13:
507 Out << "th"; return;
508 default:
509 switch (ValNo % 10) {
510 case 1: Out << "st"; return;
511 case 2: Out << "nd"; return;
512 case 3: Out << "rd"; return;
513 default: Out << "th"; return;
514 }
515 }
516}
517
Chris Lattner2b786902008-11-21 07:50:02 +0000518
Sebastian Redl15b02d22008-11-22 13:44:36 +0000519/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000520static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000521 // Programming 101: Parse a decimal number :-)
522 unsigned Val = 0;
523 while (Start != End && *Start >= '0' && *Start <= '9') {
524 Val *= 10;
525 Val += *Start - '0';
526 ++Start;
527 }
528 return Val;
529}
530
531/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000532static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000533 if (*Start != '[') {
534 unsigned Ref = PluralNumber(Start, End);
535 return Ref == Val;
536 }
537
538 ++Start;
539 unsigned Low = PluralNumber(Start, End);
540 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
541 ++Start;
542 unsigned High = PluralNumber(Start, End);
543 assert(*Start == ']' && "Bad plural expression syntax: expected )");
544 ++Start;
545 return Low <= Val && Val <= High;
546}
547
548/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattner2fe29202009-04-15 17:13:42 +0000549static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000550 // Empty condition?
551 if (*Start == ':')
552 return true;
553
554 while (1) {
555 char C = *Start;
556 if (C == '%') {
557 // Modulo expression
558 ++Start;
559 unsigned Arg = PluralNumber(Start, End);
560 assert(*Start == '=' && "Bad plural expression syntax: expected =");
561 ++Start;
562 unsigned ValMod = ValNo % Arg;
563 if (TestPluralRange(ValMod, Start, End))
564 return true;
565 } else {
Sebastian Redl3ceaf622008-11-27 07:28:14 +0000566 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redl15b02d22008-11-22 13:44:36 +0000567 "Bad plural expression syntax: unexpected character");
568 // Range expression
569 if (TestPluralRange(ValNo, Start, End))
570 return true;
571 }
572
573 // Scan for next or-expr part.
574 Start = std::find(Start, End, ',');
Mike Stump11289f42009-09-09 15:08:12 +0000575 if (Start == End)
Sebastian Redl15b02d22008-11-22 13:44:36 +0000576 break;
577 ++Start;
578 }
579 return false;
580}
581
582/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
583/// for complex plural forms, or in languages where all plurals are complex.
584/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
585/// conditions that are tested in order, the form corresponding to the first
586/// that applies being emitted. The empty condition is always true, making the
587/// last form a default case.
588/// Conditions are simple boolean expressions, where n is the number argument.
589/// Here are the rules.
590/// condition := expression | empty
591/// empty := -> always true
592/// expression := numeric [',' expression] -> logical or
593/// numeric := range -> true if n in range
594/// | '%' number '=' range -> true if n % number in range
595/// range := number
596/// | '[' number ',' number ']' -> ranges are inclusive both ends
597///
598/// Here are some examples from the GNU gettext manual written in this form:
599/// English:
600/// {1:form0|:form1}
601/// Latvian:
602/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
603/// Gaeilge:
604/// {1:form0|2:form1|:form2}
605/// Romanian:
606/// {1:form0|0,%100=[1,19]:form1|:form2}
607/// Lithuanian:
608/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
609/// Russian (requires repeated form):
610/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
611/// Slovak
612/// {1:form0|[2,4]:form1|:form2}
613/// Polish (requires repeated form):
614/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
David Blaikieb5784322011-09-26 01:18:08 +0000615static void HandlePluralModifier(const Diagnostic &DInfo, unsigned ValNo,
Sebastian Redl15b02d22008-11-22 13:44:36 +0000616 const char *Argument, unsigned ArgumentLen,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000617 SmallVectorImpl<char> &OutStr) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000618 const char *ArgumentEnd = Argument + ArgumentLen;
619 while (1) {
620 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
621 const char *ExprEnd = Argument;
622 while (*ExprEnd != ':') {
623 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
624 ++ExprEnd;
625 }
626 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
627 Argument = ExprEnd + 1;
John McCall8cb7a8a32010-01-14 20:11:39 +0000628 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
John McCall43b61682010-10-14 01:55:31 +0000629
630 // Recursively format the result of the plural clause into the
631 // output string.
632 DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000633 return;
634 }
John McCall8cb7a8a32010-01-14 20:11:39 +0000635 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
Sebastian Redl15b02d22008-11-22 13:44:36 +0000636 }
637}
638
639
Chris Lattner23be0672008-11-19 06:51:40 +0000640/// FormatDiagnostic - Format this diagnostic into a string, substituting the
641/// formal arguments into the %0 slots. The result is appended onto the Str
642/// array.
David Blaikieb5784322011-09-26 01:18:08 +0000643void Diagnostic::
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000644FormatDiagnostic(SmallVectorImpl<char> &OutStr) const {
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000645 if (!StoredDiagMessage.empty()) {
646 OutStr.append(StoredDiagMessage.begin(), StoredDiagMessage.end());
647 return;
648 }
649
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000650 StringRef Diag =
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000651 getDiags()->getDiagnosticIDs()->getDescription(getID());
Mike Stump11289f42009-09-09 15:08:12 +0000652
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000653 FormatDiagnostic(Diag.begin(), Diag.end(), OutStr);
John McCalle4d54322010-01-13 23:58:20 +0000654}
655
David Blaikieb5784322011-09-26 01:18:08 +0000656void Diagnostic::
John McCalle4d54322010-01-13 23:58:20 +0000657FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000658 SmallVectorImpl<char> &OutStr) const {
John McCalle4d54322010-01-13 23:58:20 +0000659
Chris Lattnerc243f292009-10-20 05:25:22 +0000660 /// FormattedArgs - Keep track of all of the arguments formatted by
661 /// ConvertArgToString and pass them into subsequent calls to
662 /// ConvertArgToString, allowing the implementation to avoid redundancies in
663 /// obvious cases.
David Blaikie9c902b52011-09-25 23:23:43 +0000664 SmallVector<DiagnosticsEngine::ArgumentValue, 8> FormattedArgs;
Chandler Carruthd5173952011-07-11 17:49:21 +0000665
666 /// QualTypeVals - Pass a vector of arrays so that QualType names can be
667 /// compared to see if more information is needed to be printed.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000668 SmallVector<intptr_t, 2> QualTypeVals;
Chandler Carruthd5173952011-07-11 17:49:21 +0000669 for (unsigned i = 0, e = getNumArgs(); i < e; ++i)
David Blaikie9c902b52011-09-25 23:23:43 +0000670 if (getArgKind(i) == DiagnosticsEngine::ak_qualtype)
Chandler Carruthd5173952011-07-11 17:49:21 +0000671 QualTypeVals.push_back(getRawArg(i));
672
Chris Lattner23be0672008-11-19 06:51:40 +0000673 while (DiagStr != DiagEnd) {
674 if (DiagStr[0] != '%') {
675 // Append non-%0 substrings to Str if we have one.
676 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
677 OutStr.append(DiagStr, StrEnd);
678 DiagStr = StrEnd;
Chris Lattner2b786902008-11-21 07:50:02 +0000679 continue;
John McCall8cb7a8a32010-01-14 20:11:39 +0000680 } else if (ispunct(DiagStr[1])) {
681 OutStr.push_back(DiagStr[1]); // %% -> %.
Chris Lattner23be0672008-11-19 06:51:40 +0000682 DiagStr += 2;
Chris Lattner2b786902008-11-21 07:50:02 +0000683 continue;
684 }
Mike Stump11289f42009-09-09 15:08:12 +0000685
Chris Lattner2b786902008-11-21 07:50:02 +0000686 // Skip the %.
687 ++DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000688
Chris Lattner2b786902008-11-21 07:50:02 +0000689 // This must be a placeholder for a diagnostic argument. The format for a
690 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
691 // The digit is a number from 0-9 indicating which argument this comes from.
692 // The modifier is a string of digits from the set [-a-z]+, arguments is a
693 // brace enclosed string.
694 const char *Modifier = 0, *Argument = 0;
695 unsigned ModifierLen = 0, ArgumentLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000696
Chris Lattner2b786902008-11-21 07:50:02 +0000697 // Check to see if we have a modifier. If so eat it.
698 if (!isdigit(DiagStr[0])) {
699 Modifier = DiagStr;
700 while (DiagStr[0] == '-' ||
701 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
702 ++DiagStr;
703 ModifierLen = DiagStr-Modifier;
Chris Lattner23be0672008-11-19 06:51:40 +0000704
Chris Lattner2b786902008-11-21 07:50:02 +0000705 // If we have an argument, get it next.
706 if (DiagStr[0] == '{') {
707 ++DiagStr; // Skip {.
708 Argument = DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000709
John McCall8cb7a8a32010-01-14 20:11:39 +0000710 DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
711 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
Chris Lattner2b786902008-11-21 07:50:02 +0000712 ArgumentLen = DiagStr-Argument;
713 ++DiagStr; // Skip }.
Chris Lattner23be0672008-11-19 06:51:40 +0000714 }
Chris Lattner2b786902008-11-21 07:50:02 +0000715 }
Mike Stump11289f42009-09-09 15:08:12 +0000716
Chris Lattner2b786902008-11-21 07:50:02 +0000717 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000718 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattner2b786902008-11-21 07:50:02 +0000719
David Blaikie9c902b52011-09-25 23:23:43 +0000720 DiagnosticsEngine::ArgumentKind Kind = getArgKind(ArgNo);
Chris Lattnerc243f292009-10-20 05:25:22 +0000721
722 switch (Kind) {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000723 // ---- STRINGS ----
David Blaikie9c902b52011-09-25 23:23:43 +0000724 case DiagnosticsEngine::ak_std_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000725 const std::string &S = getArgStdStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000726 assert(ModifierLen == 0 && "No modifiers for strings yet");
727 OutStr.append(S.begin(), S.end());
728 break;
729 }
David Blaikie9c902b52011-09-25 23:23:43 +0000730 case DiagnosticsEngine::ak_c_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000731 const char *S = getArgCStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000732 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000733
734 // Don't crash if get passed a null pointer by accident.
735 if (!S)
736 S = "(null)";
Mike Stump11289f42009-09-09 15:08:12 +0000737
Chris Lattner2b786902008-11-21 07:50:02 +0000738 OutStr.append(S, S + strlen(S));
739 break;
740 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000741 // ---- INTEGERS ----
David Blaikie9c902b52011-09-25 23:23:43 +0000742 case DiagnosticsEngine::ak_sint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000743 int Val = getArgSInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000744
Chris Lattner2b786902008-11-21 07:50:02 +0000745 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCall43b61682010-10-14 01:55:31 +0000746 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen,
747 OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000748 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
749 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000750 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCall43b61682010-10-14 01:55:31 +0000751 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
752 OutStr);
John McCall9015cde2010-01-14 00:50:32 +0000753 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
754 HandleOrdinalModifier((unsigned)Val, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000755 } else {
756 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000757 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000758 }
Chris Lattner2b786902008-11-21 07:50:02 +0000759 break;
760 }
David Blaikie9c902b52011-09-25 23:23:43 +0000761 case DiagnosticsEngine::ak_uint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000762 unsigned Val = getArgUInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000763
Chris Lattner2b786902008-11-21 07:50:02 +0000764 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCalle4d54322010-01-13 23:58:20 +0000765 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000766 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
767 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000768 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCall43b61682010-10-14 01:55:31 +0000769 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
770 OutStr);
John McCall9015cde2010-01-14 00:50:32 +0000771 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
772 HandleOrdinalModifier(Val, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000773 } else {
774 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000775 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000776 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000777 break;
Chris Lattner2b786902008-11-21 07:50:02 +0000778 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000779 // ---- NAMES and TYPES ----
David Blaikie9c902b52011-09-25 23:23:43 +0000780 case DiagnosticsEngine::ak_identifierinfo: {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000781 const IdentifierInfo *II = getArgIdentifier(ArgNo);
782 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000783
784 // Don't crash if get passed a null pointer by accident.
785 if (!II) {
786 const char *S = "(null)";
787 OutStr.append(S, S + strlen(S));
788 continue;
789 }
790
Daniel Dunbar07d07852009-10-18 21:17:35 +0000791 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
Chris Lattnere3d20d92008-11-23 21:45:46 +0000792 break;
793 }
David Blaikie9c902b52011-09-25 23:23:43 +0000794 case DiagnosticsEngine::ak_qualtype:
795 case DiagnosticsEngine::ak_declarationname:
796 case DiagnosticsEngine::ak_nameddecl:
797 case DiagnosticsEngine::ak_nestednamespec:
798 case DiagnosticsEngine::ak_declcontext:
Chris Lattnerc243f292009-10-20 05:25:22 +0000799 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
Chris Lattner63ecc502008-11-23 09:21:17 +0000800 Modifier, ModifierLen,
Chris Lattnerc243f292009-10-20 05:25:22 +0000801 Argument, ArgumentLen,
802 FormattedArgs.data(), FormattedArgs.size(),
Chandler Carruthd5173952011-07-11 17:49:21 +0000803 OutStr, QualTypeVals);
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000804 break;
Nico Weber4c311642008-08-10 19:59:06 +0000805 }
Chris Lattnerc243f292009-10-20 05:25:22 +0000806
807 // Remember this argument info for subsequent formatting operations. Turn
808 // std::strings into a null terminated string to make it be the same case as
809 // all the other ones.
David Blaikie9c902b52011-09-25 23:23:43 +0000810 if (Kind != DiagnosticsEngine::ak_std_string)
Chris Lattnerc243f292009-10-20 05:25:22 +0000811 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
812 else
David Blaikie9c902b52011-09-25 23:23:43 +0000813 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_c_string,
Chris Lattnerc243f292009-10-20 05:25:22 +0000814 (intptr_t)getArgStdStr(ArgNo).c_str()));
815
Nico Weber4c311642008-08-10 19:59:06 +0000816 }
Nico Weber4c311642008-08-10 19:59:06 +0000817}
Ted Kremenekea06ec12009-01-23 20:28:53 +0000818
Douglas Gregor33cdd812010-02-18 18:08:43 +0000819StoredDiagnostic::StoredDiagnostic() { }
820
David Blaikie9c902b52011-09-25 23:23:43 +0000821StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000822 StringRef Message)
Benjamin Kramer929bd682010-11-19 17:36:51 +0000823 : ID(ID), Level(Level), Loc(), Message(Message) { }
Douglas Gregor33cdd812010-02-18 18:08:43 +0000824
David Blaikie9c902b52011-09-25 23:23:43 +0000825StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level,
David Blaikieb5784322011-09-26 01:18:08 +0000826 const Diagnostic &Info)
Douglas Gregora750e8e2010-11-19 16:18:16 +0000827 : ID(Info.getID()), Level(Level)
828{
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000829 assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
830 "Valid source location without setting a source manager for diagnostic");
831 if (Info.getLocation().isValid())
832 Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000833 SmallString<64> Message;
Douglas Gregor33cdd812010-02-18 18:08:43 +0000834 Info.FormatDiagnostic(Message);
835 this->Message.assign(Message.begin(), Message.end());
836
837 Ranges.reserve(Info.getNumRanges());
838 for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I)
839 Ranges.push_back(Info.getRange(I));
840
Douglas Gregora771f462010-03-31 17:46:05 +0000841 FixIts.reserve(Info.getNumFixItHints());
842 for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I)
843 FixIts.push_back(Info.getFixItHint(I));
Douglas Gregor33cdd812010-02-18 18:08:43 +0000844}
845
David Blaikie9c902b52011-09-25 23:23:43 +0000846StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000847 StringRef Message, FullSourceLoc Loc,
Chris Lattner54b16772011-07-23 17:14:25 +0000848 ArrayRef<CharSourceRange> Ranges,
849 ArrayRef<FixItHint> Fixits)
Douglas Gregor925296b2011-07-19 16:10:42 +0000850 : ID(ID), Level(Level), Loc(Loc), Message(Message)
851{
852 this->Ranges.assign(Ranges.begin(), Ranges.end());
853 this->FixIts.assign(FixIts.begin(), FixIts.end());
854}
855
Douglas Gregor33cdd812010-02-18 18:08:43 +0000856StoredDiagnostic::~StoredDiagnostic() { }
857
Ted Kremenekea06ec12009-01-23 20:28:53 +0000858/// IncludeInDiagnosticCounts - This method (whose default implementation
859/// returns true) indicates whether the diagnostics handled by this
David Blaikiee2eefae2011-09-25 23:39:51 +0000860/// DiagnosticConsumer should be included in the number of diagnostics
David Blaikie9c902b52011-09-25 23:23:43 +0000861/// reported by DiagnosticsEngine.
David Blaikiee2eefae2011-09-25 23:39:51 +0000862bool DiagnosticConsumer::IncludeInDiagnosticCounts() const { return true; }
Douglas Gregor89336232010-03-29 23:34:08 +0000863
David Blaikie68e081d2011-12-20 02:48:34 +0000864void IgnoringDiagConsumer::anchor() { }
865
Benjamin Kramer7ec12c92012-02-07 22:29:24 +0000866PartialDiagnostic::StorageAllocator::StorageAllocator() {
Douglas Gregor89336232010-03-29 23:34:08 +0000867 for (unsigned I = 0; I != NumCached; ++I)
868 FreeList[I] = Cached + I;
869 NumFreeListEntries = NumCached;
870}
871
Benjamin Kramer7ec12c92012-02-07 22:29:24 +0000872PartialDiagnostic::StorageAllocator::~StorageAllocator() {
Chad Rosier849a67b2012-02-07 23:24:49 +0000873 // Don't assert if we are in a CrashRecovery context, as this invariant may
874 // be invalidated during a crash.
875 assert((NumFreeListEntries == NumCached ||
876 llvm::CrashRecoveryContext::isRecoveringFromCrash()) &&
877 "A partial is on the lamb");
Douglas Gregor89336232010-03-29 23:34:08 +0000878}