blob: 8065b2d98f32ab9e02aa62f04bc6b38513410aad [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"
Joerg Sonnenberger7094dee2012-08-10 10:58:18 +000020#include <cctype>
Ted Kremenek03201fb2011-03-21 18:40:07 +000021
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
David Blaikied6471f72011-09-25 23:23:43 +000024static void DummyArgToStringFn(DiagnosticsEngine::ArgumentKind AK, intptr_t QT,
Chris Lattner3fdf4b02008-11-23 09:21:17 +000025 const char *Modifier, unsigned ML,
26 const char *Argument, unsigned ArgLen,
David Blaikied6471f72011-09-25 23:23:43 +000027 const DiagnosticsEngine::ArgumentValue *PrevArgs,
Chris Lattnerb54d8af2009-10-20 05:25:22 +000028 unsigned NumPrevArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +000029 SmallVectorImpl<char> &Output,
Chandler Carruth0673cb32011-07-11 17:49:21 +000030 void *Cookie,
Bill Wendling341785e2012-02-22 09:51:33 +000031 ArrayRef<intptr_t> QualTypeVals) {
Chris Lattner3fdf4b02008-11-23 09:21:17 +000032 const char *Str = "<can't format argument>";
Chris Lattner22caddc2008-11-23 09:13:29 +000033 Output.append(Str, Str+strlen(Str));
34}
35
36
David Blaikied6471f72011-09-25 23:23:43 +000037DiagnosticsEngine::DiagnosticsEngine(
Dylan Noblesmithc93dc782012-02-20 14:00:23 +000038 const IntrusiveRefCntPtr<DiagnosticIDs> &diags,
David Blaikie78ad0b92011-09-25 23:39:51 +000039 DiagnosticConsumer *client, bool ShouldOwnClient)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000040 : Diags(diags), Client(client), OwnsDiagClient(ShouldOwnClient),
41 SourceMgr(0) {
Chris Lattner3fdf4b02008-11-23 09:21:17 +000042 ArgToStringFn = DummyArgToStringFn;
Chris Lattner92dd3862009-02-19 23:53:20 +000043 ArgToStringCookie = 0;
Mike Stump1eb44332009-09-09 15:08:12 +000044
Douglas Gregorcc5888d2010-07-31 00:40:00 +000045 AllExtensionsSilenced = 0;
46 IgnoreAllWarnings = false;
47 WarningsAsErrors = false;
Ted Kremenek1e473cc2011-08-18 01:12:56 +000048 EnableAllWarnings = false;
Douglas Gregorcc5888d2010-07-31 00:40:00 +000049 ErrorsAsFatal = false;
50 SuppressSystemWarnings = false;
51 SuppressAllDiagnostics = false;
Richard Trieu246b6aa2012-06-26 18:18:47 +000052 ElideType = true;
53 PrintTemplateTree = false;
54 ShowColors = false;
Douglas Gregorcc5888d2010-07-31 00:40:00 +000055 ShowOverloads = Ovl_All;
56 ExtBehavior = Ext_Ignore;
57
58 ErrorLimit = 0;
59 TemplateBacktraceLimit = 0;
Richard Smith08d6e032011-12-16 19:06:07 +000060 ConstexprBacktraceLimit = 0;
Douglas Gregorcc5888d2010-07-31 00:40:00 +000061
Douglas Gregorabc563f2010-07-19 21:46:24 +000062 Reset();
Reid Spencer5f016e22007-07-11 17:01:13 +000063}
64
David Blaikied6471f72011-09-25 23:23:43 +000065DiagnosticsEngine::~DiagnosticsEngine() {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000066 if (OwnsDiagClient)
67 delete Client;
Chris Lattner182745a2007-12-02 01:09:57 +000068}
69
David Blaikie78ad0b92011-09-25 23:39:51 +000070void DiagnosticsEngine::setClient(DiagnosticConsumer *client,
David Blaikied6471f72011-09-25 23:23:43 +000071 bool ShouldOwnClient) {
Douglas Gregor4f5e21e2011-01-31 22:04:05 +000072 if (OwnsDiagClient && Client)
73 delete Client;
74
75 Client = client;
76 OwnsDiagClient = ShouldOwnClient;
77}
Chris Lattner04ae2df2009-07-12 21:18:45 +000078
David Blaikied6471f72011-09-25 23:23:43 +000079void DiagnosticsEngine::pushMappings(SourceLocation Loc) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +000080 DiagStateOnPushStack.push_back(GetCurDiagState());
Chris Lattner04ae2df2009-07-12 21:18:45 +000081}
82
David Blaikied6471f72011-09-25 23:23:43 +000083bool DiagnosticsEngine::popMappings(SourceLocation Loc) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +000084 if (DiagStateOnPushStack.empty())
Chris Lattner04ae2df2009-07-12 21:18:45 +000085 return false;
86
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +000087 if (DiagStateOnPushStack.back() != GetCurDiagState()) {
88 // State changed at some point between push/pop.
89 PushDiagStatePoint(DiagStateOnPushStack.back(), Loc);
90 }
91 DiagStateOnPushStack.pop_back();
Chris Lattner04ae2df2009-07-12 21:18:45 +000092 return true;
93}
94
David Blaikied6471f72011-09-25 23:23:43 +000095void DiagnosticsEngine::Reset() {
Douglas Gregorabc563f2010-07-19 21:46:24 +000096 ErrorOccurred = false;
97 FatalErrorOccurred = false;
Douglas Gregor85bea972011-07-06 17:40:26 +000098 UnrecoverableErrorOccurred = false;
Douglas Gregorabc563f2010-07-19 21:46:24 +000099
100 NumWarnings = 0;
101 NumErrors = 0;
102 NumErrorsSuppressed = 0;
Argyrios Kyrtzidisc0a575f2011-07-29 01:25:44 +0000103 TrapNumErrorsOccurred = 0;
104 TrapNumUnrecoverableErrorsOccurred = 0;
Douglas Gregor85bea972011-07-06 17:40:26 +0000105
Douglas Gregorabc563f2010-07-19 21:46:24 +0000106 CurDiagID = ~0U;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000107 // Set LastDiagLevel to an "unset" state. If we set it to 'Ignored', notes
David Blaikied6471f72011-09-25 23:23:43 +0000108 // using a DiagnosticsEngine associated to a translation unit that follow
109 // diagnostics from a DiagnosticsEngine associated to anoter t.u. will not be
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000110 // displayed.
111 LastDiagLevel = (DiagnosticIDs::Level)-1;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000112 DelayedDiagID = 0;
Argyrios Kyrtzidisdc0a2da2011-03-26 18:58:17 +0000113
114 // Clear state related to #pragma diagnostic.
115 DiagStates.clear();
116 DiagStatePoints.clear();
117 DiagStateOnPushStack.clear();
118
119 // Create a DiagState and DiagStatePoint representing diagnostic changes
120 // through command-line.
121 DiagStates.push_back(DiagState());
Richard Smith00aae522012-08-14 04:19:29 +0000122 DiagStatePoints.push_back(DiagStatePoint(&DiagStates.back(), FullSourceLoc()));
Douglas Gregorabc563f2010-07-19 21:46:24 +0000123}
Reid Spencer5f016e22007-07-11 17:01:13 +0000124
David Blaikied6471f72011-09-25 23:23:43 +0000125void DiagnosticsEngine::SetDelayedDiagnostic(unsigned DiagID, StringRef Arg1,
Chad Rosierdfaee492012-02-07 23:24:49 +0000126 StringRef Arg2) {
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000127 if (DelayedDiagID)
128 return;
129
130 DelayedDiagID = DiagID;
Douglas Gregor9e2dac92010-03-22 15:47:45 +0000131 DelayedDiagArg1 = Arg1.str();
132 DelayedDiagArg2 = Arg2.str();
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000133}
134
David Blaikied6471f72011-09-25 23:23:43 +0000135void DiagnosticsEngine::ReportDelayed() {
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000136 Report(DelayedDiagID) << DelayedDiagArg1 << DelayedDiagArg2;
137 DelayedDiagID = 0;
138 DelayedDiagArg1.clear();
139 DelayedDiagArg2.clear();
140}
141
David Blaikied6471f72011-09-25 23:23:43 +0000142DiagnosticsEngine::DiagStatePointsTy::iterator
143DiagnosticsEngine::GetDiagStatePointForLoc(SourceLocation L) const {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000144 assert(!DiagStatePoints.empty());
145 assert(DiagStatePoints.front().Loc.isInvalid() &&
146 "Should have created a DiagStatePoint for command-line");
147
Richard Smith9e63dc52012-08-17 00:55:32 +0000148 if (!SourceMgr)
149 return DiagStatePoints.end() - 1;
150
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000151 FullSourceLoc Loc(L, *SourceMgr);
152 if (Loc.isInvalid())
153 return DiagStatePoints.end() - 1;
154
155 DiagStatePointsTy::iterator Pos = DiagStatePoints.end();
156 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
157 if (LastStateChangePos.isValid() &&
158 Loc.isBeforeInTranslationUnitThan(LastStateChangePos))
159 Pos = std::upper_bound(DiagStatePoints.begin(), DiagStatePoints.end(),
160 DiagStatePoint(0, Loc));
161 --Pos;
162 return Pos;
163}
164
David Blaikied6471f72011-09-25 23:23:43 +0000165void DiagnosticsEngine::setDiagnosticMapping(diag::kind Diag, diag::Mapping Map,
Chad Rosierdfaee492012-02-07 23:24:49 +0000166 SourceLocation L) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000167 assert(Diag < diag::DIAG_UPPER_LIMIT &&
168 "Can only map builtin diagnostics");
169 assert((Diags->isBuiltinWarningOrExtension(Diag) ||
170 (Map == diag::MAP_FATAL || Map == diag::MAP_ERROR)) &&
171 "Cannot map errors into warnings!");
172 assert(!DiagStatePoints.empty());
Richard Smithc95ad002012-08-14 22:37:22 +0000173 assert((L.isInvalid() || SourceMgr) && "No SourceMgr for valid location");
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000174
Richard Smithc95ad002012-08-14 22:37:22 +0000175 FullSourceLoc Loc = SourceMgr? FullSourceLoc(L, *SourceMgr) : FullSourceLoc();
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000176 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
Chad Rosier7a0a31c2012-02-03 01:49:51 +0000177 // Don't allow a mapping to a warning override an error/fatal mapping.
178 if (Map == diag::MAP_WARNING) {
179 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(Diag);
180 if (Info.getMapping() == diag::MAP_ERROR ||
181 Info.getMapping() == diag::MAP_FATAL)
182 Map = Info.getMapping();
183 }
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +0000184 DiagnosticMappingInfo MappingInfo = makeMappingInfo(Map, L);
Daniel Dunbar53201a82011-10-04 21:17:24 +0000185
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000186 // Common case; setting all the diagnostics of a group in one place.
187 if (Loc.isInvalid() || Loc == LastStateChangePos) {
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000188 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000189 return;
190 }
191
192 // Another common case; modifying diagnostic state in a source location
193 // after the previous one.
194 if ((Loc.isValid() && LastStateChangePos.isInvalid()) ||
195 LastStateChangePos.isBeforeInTranslationUnitThan(Loc)) {
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000196 // A diagnostic pragma occurred, create a new DiagState initialized with
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000197 // the current one and a new DiagStatePoint to record at which location
198 // the new state became active.
199 DiagStates.push_back(*GetCurDiagState());
200 PushDiagStatePoint(&DiagStates.back(), Loc);
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000201 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000202 return;
203 }
204
205 // We allow setting the diagnostic state in random source order for
206 // completeness but it should not be actually happening in normal practice.
207
208 DiagStatePointsTy::iterator Pos = GetDiagStatePointForLoc(Loc);
209 assert(Pos != DiagStatePoints.end());
210
211 // Update all diagnostic states that are active after the given location.
212 for (DiagStatePointsTy::iterator
213 I = Pos+1, E = DiagStatePoints.end(); I != E; ++I) {
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000214 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000215 }
216
217 // If the location corresponds to an existing point, just update its state.
218 if (Pos->Loc == Loc) {
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000219 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000220 return;
221 }
222
223 // Create a new state/point and fit it into the vector of DiagStatePoints
224 // so that the vector is always ordered according to location.
225 Pos->Loc.isBeforeInTranslationUnitThan(Loc);
226 DiagStates.push_back(*Pos->State);
227 DiagState *NewState = &DiagStates.back();
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000228 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000229 DiagStatePoints.insert(Pos+1, DiagStatePoint(NewState,
230 FullSourceLoc(Loc, *SourceMgr)));
231}
232
Daniel Dunbar3f839462011-09-29 01:47:16 +0000233bool DiagnosticsEngine::setDiagnosticGroupMapping(
234 StringRef Group, diag::Mapping Map, SourceLocation Loc)
235{
236 // Get the diagnostics in this group.
237 llvm::SmallVector<diag::kind, 8> GroupDiags;
238 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
239 return true;
240
241 // Set the mapping.
242 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i)
243 setDiagnosticMapping(GroupDiags[i], Map, Loc);
244
245 return false;
246}
247
Chad Rosier3f225092012-02-07 19:55:45 +0000248void DiagnosticsEngine::setDiagnosticWarningAsError(diag::kind Diag,
249 bool Enabled) {
250 // If we are enabling this feature, just set the diagnostic mappings to map to
251 // errors.
252 if (Enabled)
253 setDiagnosticMapping(Diag, diag::MAP_ERROR, SourceLocation());
254
255 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
256 // potentially downgrade anything already mapped to be a warning.
257 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(Diag);
258
259 if (Info.getMapping() == diag::MAP_ERROR ||
260 Info.getMapping() == diag::MAP_FATAL)
261 Info.setMapping(diag::MAP_WARNING);
262
263 Info.setNoWarningAsError(true);
264}
265
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000266bool DiagnosticsEngine::setDiagnosticGroupWarningAsError(StringRef Group,
267 bool Enabled) {
Daniel Dunbara5e41332011-09-29 01:52:06 +0000268 // If we are enabling this feature, just set the diagnostic mappings to map to
269 // errors.
270 if (Enabled)
271 return setDiagnosticGroupMapping(Group, diag::MAP_ERROR);
272
273 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
274 // potentially downgrade anything already mapped to be a warning.
275
276 // Get the diagnostics in this group.
277 llvm::SmallVector<diag::kind, 8> GroupDiags;
278 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
279 return true;
280
281 // Perform the mapping change.
282 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i) {
283 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(
284 GroupDiags[i]);
285
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000286 if (Info.getMapping() == diag::MAP_ERROR ||
287 Info.getMapping() == diag::MAP_FATAL)
288 Info.setMapping(diag::MAP_WARNING);
289
Daniel Dunbara5e41332011-09-29 01:52:06 +0000290 Info.setNoWarningAsError(true);
291 }
292
293 return false;
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000294}
295
Chad Rosier3f225092012-02-07 19:55:45 +0000296void DiagnosticsEngine::setDiagnosticErrorAsFatal(diag::kind Diag,
297 bool Enabled) {
298 // If we are enabling this feature, just set the diagnostic mappings to map to
299 // errors.
300 if (Enabled)
301 setDiagnosticMapping(Diag, diag::MAP_FATAL, SourceLocation());
302
303 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
304 // potentially downgrade anything already mapped to be a warning.
305 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(Diag);
306
307 if (Info.getMapping() == diag::MAP_FATAL)
308 Info.setMapping(diag::MAP_ERROR);
309
310 Info.setNoErrorAsFatal(true);
311}
312
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000313bool DiagnosticsEngine::setDiagnosticGroupErrorAsFatal(StringRef Group,
314 bool Enabled) {
Daniel Dunbara5e41332011-09-29 01:52:06 +0000315 // If we are enabling this feature, just set the diagnostic mappings to map to
316 // fatal errors.
317 if (Enabled)
318 return setDiagnosticGroupMapping(Group, diag::MAP_FATAL);
319
320 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
321 // potentially downgrade anything already mapped to be an error.
322
323 // Get the diagnostics in this group.
324 llvm::SmallVector<diag::kind, 8> GroupDiags;
325 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
326 return true;
327
328 // Perform the mapping change.
329 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i) {
330 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(
331 GroupDiags[i]);
332
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000333 if (Info.getMapping() == diag::MAP_FATAL)
334 Info.setMapping(diag::MAP_ERROR);
335
Daniel Dunbara5e41332011-09-29 01:52:06 +0000336 Info.setNoErrorAsFatal(true);
337 }
338
339 return false;
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000340}
341
Argyrios Kyrtzidis82e64112012-01-28 04:35:52 +0000342void DiagnosticsEngine::setMappingToAllDiagnostics(diag::Mapping Map,
Argyrios Kyrtzidis11583c72012-01-27 06:15:43 +0000343 SourceLocation Loc) {
344 // Get all the diagnostics.
345 llvm::SmallVector<diag::kind, 64> AllDiags;
346 Diags->getAllDiagnostics(AllDiags);
347
348 // Set the mapping.
349 for (unsigned i = 0, e = AllDiags.size(); i != e; ++i)
350 if (Diags->isBuiltinWarningOrExtension(AllDiags[i]))
351 setDiagnosticMapping(AllDiags[i], Map, Loc);
Argyrios Kyrtzidis11583c72012-01-27 06:15:43 +0000352}
353
David Blaikied6471f72011-09-25 23:23:43 +0000354void DiagnosticsEngine::Report(const StoredDiagnostic &storedDiag) {
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000355 assert(CurDiagID == ~0U && "Multiple diagnostics in flight at once!");
356
357 CurDiagLoc = storedDiag.getLocation();
358 CurDiagID = storedDiag.getID();
359 NumDiagArgs = 0;
360
361 NumDiagRanges = storedDiag.range_size();
Daniel Dunbar981e2792012-03-13 18:21:17 +0000362 assert(NumDiagRanges < DiagnosticsEngine::MaxRanges &&
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000363 "Too many arguments to diagnostic!");
364 unsigned i = 0;
365 for (StoredDiagnostic::range_iterator
366 RI = storedDiag.range_begin(),
367 RE = storedDiag.range_end(); RI != RE; ++RI)
368 DiagRanges[i++] = *RI;
369
Daniel Dunbar981e2792012-03-13 18:21:17 +0000370 assert(NumDiagRanges < DiagnosticsEngine::MaxFixItHints &&
371 "Too many arguments to diagnostic!");
372 NumDiagFixItHints = 0;
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000373 for (StoredDiagnostic::fixit_iterator
374 FI = storedDiag.fixit_begin(),
375 FE = storedDiag.fixit_end(); FI != FE; ++FI)
Daniel Dunbar981e2792012-03-13 18:21:17 +0000376 DiagFixItHints[NumDiagFixItHints++] = *FI;
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000377
David Blaikie78ad0b92011-09-25 23:39:51 +0000378 assert(Client && "DiagnosticConsumer not set!");
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000379 Level DiagLevel = storedDiag.getLevel();
David Blaikie40847cf2011-09-26 01:18:08 +0000380 Diagnostic Info(this, storedDiag.getMessage());
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000381 Client->HandleDiagnostic(DiagLevel, Info);
382 if (Client->IncludeInDiagnosticCounts()) {
David Blaikied6471f72011-09-25 23:23:43 +0000383 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000384 ++NumWarnings;
385 }
386
387 CurDiagID = ~0U;
388}
389
Jordan Rosec6d64a22012-07-11 16:50:36 +0000390bool DiagnosticsEngine::EmitCurrentDiagnostic(bool Force) {
391 assert(getClient() && "DiagnosticClient not set!");
392
393 bool Emitted;
394 if (Force) {
395 Diagnostic Info(this);
396
397 // Figure out the diagnostic level of this message.
398 DiagnosticIDs::Level DiagLevel
399 = Diags->getDiagnosticLevel(Info.getID(), Info.getLocation(), *this);
400
401 Emitted = (DiagLevel != DiagnosticIDs::Ignored);
402 if (Emitted) {
403 // Emit the diagnostic regardless of suppression level.
404 Diags->EmitDiag(*this, DiagLevel);
405 }
406 } else {
407 // Process the diagnostic, sending the accumulated information to the
408 // DiagnosticConsumer.
409 Emitted = ProcessDiag();
410 }
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000411
412 // Clear out the current diagnostic object.
Daniel Dunbar3054f092012-03-13 21:02:14 +0000413 unsigned DiagID = CurDiagID;
414 Clear();
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000415
416 // If there was a delayed diagnostic, emit it now.
Jordan Rosec6d64a22012-07-11 16:50:36 +0000417 if (!Force && DelayedDiagID && DelayedDiagID != DiagID)
Daniel Dunbar3054f092012-03-13 21:02:14 +0000418 ReportDelayed();
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000419
420 return Emitted;
421}
422
Nico Weber7bfaaae2008-08-10 19:59:06 +0000423
David Blaikie78ad0b92011-09-25 23:39:51 +0000424DiagnosticConsumer::~DiagnosticConsumer() {}
Nico Weber7bfaaae2008-08-10 19:59:06 +0000425
David Blaikie78ad0b92011-09-25 23:39:51 +0000426void DiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
David Blaikie40847cf2011-09-26 01:18:08 +0000427 const Diagnostic &Info) {
Argyrios Kyrtzidisf2224d82010-11-18 20:06:46 +0000428 if (!IncludeInDiagnosticCounts())
429 return;
430
David Blaikied6471f72011-09-25 23:23:43 +0000431 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidisf2224d82010-11-18 20:06:46 +0000432 ++NumWarnings;
David Blaikied6471f72011-09-25 23:23:43 +0000433 else if (DiagLevel >= DiagnosticsEngine::Error)
Argyrios Kyrtzidisf2224d82010-11-18 20:06:46 +0000434 ++NumErrors;
435}
Chris Lattnerf4c83962008-11-19 06:51:40 +0000436
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000437/// ModifierIs - Return true if the specified modifier matches specified string.
438template <std::size_t StrLen>
439static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
440 const char (&Str)[StrLen]) {
441 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
442}
443
John McCall909c1822010-01-14 20:11:39 +0000444/// ScanForward - Scans forward, looking for the given character, skipping
445/// nested clauses and escaped characters.
446static const char *ScanFormat(const char *I, const char *E, char Target) {
447 unsigned Depth = 0;
448
449 for ( ; I != E; ++I) {
450 if (Depth == 0 && *I == Target) return I;
451 if (Depth != 0 && *I == '}') Depth--;
452
453 if (*I == '%') {
454 I++;
455 if (I == E) break;
456
457 // Escaped characters get implicitly skipped here.
458
459 // Format specifier.
460 if (!isdigit(*I) && !ispunct(*I)) {
461 for (I++; I != E && !isdigit(*I) && *I != '{'; I++) ;
462 if (I == E) break;
463 if (*I == '{')
464 Depth++;
465 }
466 }
467 }
468 return E;
469}
470
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000471/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
472/// like this: %select{foo|bar|baz}2. This means that the integer argument
473/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
474/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
475/// This is very useful for certain classes of variant diagnostics.
David Blaikie40847cf2011-09-26 01:18:08 +0000476static void HandleSelectModifier(const Diagnostic &DInfo, unsigned ValNo,
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000477 const char *Argument, unsigned ArgumentLen,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000478 SmallVectorImpl<char> &OutStr) {
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000479 const char *ArgumentEnd = Argument+ArgumentLen;
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000481 // Skip over 'ValNo' |'s.
482 while (ValNo) {
John McCall909c1822010-01-14 20:11:39 +0000483 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000484 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
485 " larger than the number of options in the diagnostic string!");
486 Argument = NextVal+1; // Skip this string.
487 --ValNo;
488 }
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000490 // Get the end of the value. This is either the } or the |.
John McCall909c1822010-01-14 20:11:39 +0000491 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
John McCall9f286142010-01-13 23:58:20 +0000492
493 // Recursively format the result of the select clause into the output string.
494 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000495}
496
497/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
498/// letter 's' to the string if the value is not 1. This is used in cases like
499/// this: "you idiot, you have %4 parameter%s4!".
500static void HandleIntegerSModifier(unsigned ValNo,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000501 SmallVectorImpl<char> &OutStr) {
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000502 if (ValNo != 1)
503 OutStr.push_back('s');
504}
505
John McCall3be16b72010-01-14 00:50:32 +0000506/// HandleOrdinalModifier - Handle the integer 'ord' modifier. This
507/// prints the ordinal form of the given integer, with 1 corresponding
508/// to the first ordinal. Currently this is hard-coded to use the
509/// English form.
510static void HandleOrdinalModifier(unsigned ValNo,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000511 SmallVectorImpl<char> &OutStr) {
John McCall3be16b72010-01-14 00:50:32 +0000512 assert(ValNo != 0 && "ValNo must be strictly positive!");
513
514 llvm::raw_svector_ostream Out(OutStr);
515
516 // We could use text forms for the first N ordinals, but the numeric
517 // forms are actually nicer in diagnostics because they stand out.
518 Out << ValNo;
519
520 // It is critically important that we do this perfectly for
521 // user-written sequences with over 100 elements.
522 switch (ValNo % 100) {
523 case 11:
524 case 12:
525 case 13:
526 Out << "th"; return;
527 default:
528 switch (ValNo % 10) {
529 case 1: Out << "st"; return;
530 case 2: Out << "nd"; return;
531 case 3: Out << "rd"; return;
532 default: Out << "th"; return;
533 }
534 }
535}
536
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000537
Sebastian Redle4c452c2008-11-22 13:44:36 +0000538/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000539static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000540 // Programming 101: Parse a decimal number :-)
541 unsigned Val = 0;
542 while (Start != End && *Start >= '0' && *Start <= '9') {
543 Val *= 10;
544 Val += *Start - '0';
545 ++Start;
546 }
547 return Val;
548}
549
550/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000551static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000552 if (*Start != '[') {
553 unsigned Ref = PluralNumber(Start, End);
554 return Ref == Val;
555 }
556
557 ++Start;
558 unsigned Low = PluralNumber(Start, End);
559 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
560 ++Start;
561 unsigned High = PluralNumber(Start, End);
562 assert(*Start == ']' && "Bad plural expression syntax: expected )");
563 ++Start;
564 return Low <= Val && Val <= High;
565}
566
567/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000568static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000569 // Empty condition?
570 if (*Start == ':')
571 return true;
572
573 while (1) {
574 char C = *Start;
575 if (C == '%') {
576 // Modulo expression
577 ++Start;
578 unsigned Arg = PluralNumber(Start, End);
579 assert(*Start == '=' && "Bad plural expression syntax: expected =");
580 ++Start;
581 unsigned ValMod = ValNo % Arg;
582 if (TestPluralRange(ValMod, Start, End))
583 return true;
584 } else {
Sebastian Redle2065322008-11-27 07:28:14 +0000585 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redle4c452c2008-11-22 13:44:36 +0000586 "Bad plural expression syntax: unexpected character");
587 // Range expression
588 if (TestPluralRange(ValNo, Start, End))
589 return true;
590 }
591
592 // Scan for next or-expr part.
593 Start = std::find(Start, End, ',');
Mike Stump1eb44332009-09-09 15:08:12 +0000594 if (Start == End)
Sebastian Redle4c452c2008-11-22 13:44:36 +0000595 break;
596 ++Start;
597 }
598 return false;
599}
600
601/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
602/// for complex plural forms, or in languages where all plurals are complex.
603/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
604/// conditions that are tested in order, the form corresponding to the first
605/// that applies being emitted. The empty condition is always true, making the
606/// last form a default case.
607/// Conditions are simple boolean expressions, where n is the number argument.
608/// Here are the rules.
609/// condition := expression | empty
610/// empty := -> always true
611/// expression := numeric [',' expression] -> logical or
612/// numeric := range -> true if n in range
613/// | '%' number '=' range -> true if n % number in range
614/// range := number
615/// | '[' number ',' number ']' -> ranges are inclusive both ends
616///
617/// Here are some examples from the GNU gettext manual written in this form:
618/// English:
619/// {1:form0|:form1}
620/// Latvian:
621/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
622/// Gaeilge:
623/// {1:form0|2:form1|:form2}
624/// Romanian:
625/// {1:form0|0,%100=[1,19]:form1|:form2}
626/// Lithuanian:
627/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
628/// Russian (requires repeated form):
629/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
630/// Slovak
631/// {1:form0|[2,4]:form1|:form2}
632/// Polish (requires repeated form):
633/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
David Blaikie40847cf2011-09-26 01:18:08 +0000634static void HandlePluralModifier(const Diagnostic &DInfo, unsigned ValNo,
Sebastian Redle4c452c2008-11-22 13:44:36 +0000635 const char *Argument, unsigned ArgumentLen,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000636 SmallVectorImpl<char> &OutStr) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000637 const char *ArgumentEnd = Argument + ArgumentLen;
638 while (1) {
639 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
640 const char *ExprEnd = Argument;
641 while (*ExprEnd != ':') {
642 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
643 ++ExprEnd;
644 }
645 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
646 Argument = ExprEnd + 1;
John McCall909c1822010-01-14 20:11:39 +0000647 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
John McCalle53a44b2010-10-14 01:55:31 +0000648
649 // Recursively format the result of the plural clause into the
650 // output string.
651 DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000652 return;
653 }
John McCall909c1822010-01-14 20:11:39 +0000654 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
Sebastian Redle4c452c2008-11-22 13:44:36 +0000655 }
656}
657
658
Chris Lattnerf4c83962008-11-19 06:51:40 +0000659/// FormatDiagnostic - Format this diagnostic into a string, substituting the
660/// formal arguments into the %0 slots. The result is appended onto the Str
661/// array.
David Blaikie40847cf2011-09-26 01:18:08 +0000662void Diagnostic::
Chris Lattner5f9e2722011-07-23 10:55:15 +0000663FormatDiagnostic(SmallVectorImpl<char> &OutStr) const {
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000664 if (!StoredDiagMessage.empty()) {
665 OutStr.append(StoredDiagMessage.begin(), StoredDiagMessage.end());
666 return;
667 }
668
Chris Lattner5f9e2722011-07-23 10:55:15 +0000669 StringRef Diag =
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000670 getDiags()->getDiagnosticIDs()->getDescription(getID());
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000672 FormatDiagnostic(Diag.begin(), Diag.end(), OutStr);
John McCall9f286142010-01-13 23:58:20 +0000673}
674
David Blaikie40847cf2011-09-26 01:18:08 +0000675void Diagnostic::
John McCall9f286142010-01-13 23:58:20 +0000676FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000677 SmallVectorImpl<char> &OutStr) const {
John McCall9f286142010-01-13 23:58:20 +0000678
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000679 /// FormattedArgs - Keep track of all of the arguments formatted by
680 /// ConvertArgToString and pass them into subsequent calls to
681 /// ConvertArgToString, allowing the implementation to avoid redundancies in
682 /// obvious cases.
David Blaikied6471f72011-09-25 23:23:43 +0000683 SmallVector<DiagnosticsEngine::ArgumentValue, 8> FormattedArgs;
Chandler Carruth0673cb32011-07-11 17:49:21 +0000684
685 /// QualTypeVals - Pass a vector of arrays so that QualType names can be
686 /// compared to see if more information is needed to be printed.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000687 SmallVector<intptr_t, 2> QualTypeVals;
Richard Trieu246b6aa2012-06-26 18:18:47 +0000688 SmallVector<char, 64> Tree;
689
Chandler Carruth0673cb32011-07-11 17:49:21 +0000690 for (unsigned i = 0, e = getNumArgs(); i < e; ++i)
David Blaikied6471f72011-09-25 23:23:43 +0000691 if (getArgKind(i) == DiagnosticsEngine::ak_qualtype)
Chandler Carruth0673cb32011-07-11 17:49:21 +0000692 QualTypeVals.push_back(getRawArg(i));
693
Chris Lattnerf4c83962008-11-19 06:51:40 +0000694 while (DiagStr != DiagEnd) {
695 if (DiagStr[0] != '%') {
696 // Append non-%0 substrings to Str if we have one.
697 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
698 OutStr.append(DiagStr, StrEnd);
699 DiagStr = StrEnd;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000700 continue;
John McCall909c1822010-01-14 20:11:39 +0000701 } else if (ispunct(DiagStr[1])) {
702 OutStr.push_back(DiagStr[1]); // %% -> %.
Chris Lattnerf4c83962008-11-19 06:51:40 +0000703 DiagStr += 2;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000704 continue;
705 }
Mike Stump1eb44332009-09-09 15:08:12 +0000706
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000707 // Skip the %.
708 ++DiagStr;
Mike Stump1eb44332009-09-09 15:08:12 +0000709
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000710 // This must be a placeholder for a diagnostic argument. The format for a
711 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
712 // The digit is a number from 0-9 indicating which argument this comes from.
713 // The modifier is a string of digits from the set [-a-z]+, arguments is a
714 // brace enclosed string.
715 const char *Modifier = 0, *Argument = 0;
716 unsigned ModifierLen = 0, ArgumentLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000718 // Check to see if we have a modifier. If so eat it.
719 if (!isdigit(DiagStr[0])) {
720 Modifier = DiagStr;
721 while (DiagStr[0] == '-' ||
722 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
723 ++DiagStr;
724 ModifierLen = DiagStr-Modifier;
Chris Lattnerf4c83962008-11-19 06:51:40 +0000725
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000726 // If we have an argument, get it next.
727 if (DiagStr[0] == '{') {
728 ++DiagStr; // Skip {.
729 Argument = DiagStr;
Mike Stump1eb44332009-09-09 15:08:12 +0000730
John McCall909c1822010-01-14 20:11:39 +0000731 DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
732 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000733 ArgumentLen = DiagStr-Argument;
734 ++DiagStr; // Skip }.
Chris Lattnerf4c83962008-11-19 06:51:40 +0000735 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000736 }
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000738 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner22caddc2008-11-23 09:13:29 +0000739 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000740
Richard Trieu246b6aa2012-06-26 18:18:47 +0000741 // Only used for type diffing.
742 unsigned ArgNo2 = ArgNo;
743
David Blaikied6471f72011-09-25 23:23:43 +0000744 DiagnosticsEngine::ArgumentKind Kind = getArgKind(ArgNo);
Richard Trieu246b6aa2012-06-26 18:18:47 +0000745 if (Kind == DiagnosticsEngine::ak_qualtype &&
746 ModifierIs(Modifier, ModifierLen, "diff")) {
747 Kind = DiagnosticsEngine::ak_qualtype_pair;
748 assert(*DiagStr == ',' && isdigit(*(DiagStr + 1)) &&
749 "Invalid format for diff modifier");
750 ++DiagStr; // Comma.
751 ArgNo2 = *DiagStr++ - '0';
752 assert(getArgKind(ArgNo2) == DiagnosticsEngine::ak_qualtype &&
753 "Second value of type diff must be a qualtype");
754 }
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000755
756 switch (Kind) {
Chris Lattner08631c52008-11-23 21:45:46 +0000757 // ---- STRINGS ----
David Blaikied6471f72011-09-25 23:23:43 +0000758 case DiagnosticsEngine::ak_std_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000759 const std::string &S = getArgStdStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000760 assert(ModifierLen == 0 && "No modifiers for strings yet");
761 OutStr.append(S.begin(), S.end());
762 break;
763 }
David Blaikied6471f72011-09-25 23:23:43 +0000764 case DiagnosticsEngine::ak_c_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000765 const char *S = getArgCStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000766 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbare46e3542009-04-20 06:13:16 +0000767
768 // Don't crash if get passed a null pointer by accident.
769 if (!S)
770 S = "(null)";
Mike Stump1eb44332009-09-09 15:08:12 +0000771
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000772 OutStr.append(S, S + strlen(S));
773 break;
774 }
Chris Lattner08631c52008-11-23 21:45:46 +0000775 // ---- INTEGERS ----
David Blaikied6471f72011-09-25 23:23:43 +0000776 case DiagnosticsEngine::ak_sint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000777 int Val = getArgSInt(ArgNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000779 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCalle53a44b2010-10-14 01:55:31 +0000780 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen,
781 OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000782 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
783 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000784 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCalle53a44b2010-10-14 01:55:31 +0000785 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
786 OutStr);
John McCall3be16b72010-01-14 00:50:32 +0000787 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
788 HandleOrdinalModifier((unsigned)Val, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000789 } else {
790 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbar23e47c62009-10-17 18:12:14 +0000791 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner30bc9652008-11-19 07:22:31 +0000792 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000793 break;
794 }
David Blaikied6471f72011-09-25 23:23:43 +0000795 case DiagnosticsEngine::ak_uint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000796 unsigned Val = getArgUInt(ArgNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000798 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCall9f286142010-01-13 23:58:20 +0000799 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000800 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
801 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000802 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCalle53a44b2010-10-14 01:55:31 +0000803 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
804 OutStr);
John McCall3be16b72010-01-14 00:50:32 +0000805 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
806 HandleOrdinalModifier(Val, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000807 } else {
808 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbar23e47c62009-10-17 18:12:14 +0000809 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner30bc9652008-11-19 07:22:31 +0000810 }
Chris Lattner22caddc2008-11-23 09:13:29 +0000811 break;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000812 }
Chris Lattner08631c52008-11-23 21:45:46 +0000813 // ---- NAMES and TYPES ----
David Blaikied6471f72011-09-25 23:23:43 +0000814 case DiagnosticsEngine::ak_identifierinfo: {
Chris Lattner08631c52008-11-23 21:45:46 +0000815 const IdentifierInfo *II = getArgIdentifier(ArgNo);
816 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbare46e3542009-04-20 06:13:16 +0000817
818 // Don't crash if get passed a null pointer by accident.
819 if (!II) {
820 const char *S = "(null)";
821 OutStr.append(S, S + strlen(S));
822 continue;
823 }
824
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000825 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
Chris Lattner08631c52008-11-23 21:45:46 +0000826 break;
827 }
David Blaikied6471f72011-09-25 23:23:43 +0000828 case DiagnosticsEngine::ak_qualtype:
829 case DiagnosticsEngine::ak_declarationname:
830 case DiagnosticsEngine::ak_nameddecl:
831 case DiagnosticsEngine::ak_nestednamespec:
832 case DiagnosticsEngine::ak_declcontext:
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000833 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000834 Modifier, ModifierLen,
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000835 Argument, ArgumentLen,
836 FormattedArgs.data(), FormattedArgs.size(),
Chandler Carruth0673cb32011-07-11 17:49:21 +0000837 OutStr, QualTypeVals);
Chris Lattner22caddc2008-11-23 09:13:29 +0000838 break;
Richard Trieu246b6aa2012-06-26 18:18:47 +0000839 case DiagnosticsEngine::ak_qualtype_pair:
840 // Create a struct with all the info needed for printing.
841 TemplateDiffTypes TDT;
842 TDT.FromType = getRawArg(ArgNo);
843 TDT.ToType = getRawArg(ArgNo2);
844 TDT.ElideType = getDiags()->ElideType;
845 TDT.ShowColors = getDiags()->ShowColors;
Richard Trieu5409d282012-07-10 01:46:04 +0000846 TDT.TemplateDiffUsed = false;
Richard Trieu246b6aa2012-06-26 18:18:47 +0000847 intptr_t val = reinterpret_cast<intptr_t>(&TDT);
848
Richard Trieu529cdf42012-06-29 21:12:16 +0000849 const char *ArgumentEnd = Argument + ArgumentLen;
850 const char *Pipe = ScanFormat(Argument, ArgumentEnd, '|');
851
Richard Trieu55619772012-07-13 21:18:32 +0000852 // Print the tree. If this diagnostic already has a tree, skip the
853 // second tree.
854 if (getDiags()->PrintTemplateTree && Tree.empty()) {
Richard Trieu246b6aa2012-06-26 18:18:47 +0000855 TDT.PrintFromType = true;
856 TDT.PrintTree = true;
857 getDiags()->ConvertArgToString(Kind, val,
858 Modifier, ModifierLen,
859 Argument, ArgumentLen,
860 FormattedArgs.data(),
861 FormattedArgs.size(),
862 Tree, QualTypeVals);
863 // If there is no tree information, fall back to regular printing.
Richard Trieu529cdf42012-06-29 21:12:16 +0000864 if (!Tree.empty()) {
865 FormatDiagnostic(Pipe + 1, ArgumentEnd, OutStr);
Richard Trieu246b6aa2012-06-26 18:18:47 +0000866 break;
Richard Trieu529cdf42012-06-29 21:12:16 +0000867 }
Richard Trieu246b6aa2012-06-26 18:18:47 +0000868 }
869
870 // Non-tree printing, also the fall-back when tree printing fails.
871 // The fall-back is triggered when the types compared are not templates.
Richard Trieu529cdf42012-06-29 21:12:16 +0000872 const char *FirstDollar = ScanFormat(Argument, ArgumentEnd, '$');
873 const char *SecondDollar = ScanFormat(FirstDollar + 1, ArgumentEnd, '$');
Richard Trieu246b6aa2012-06-26 18:18:47 +0000874
875 // Append before text
Richard Trieu529cdf42012-06-29 21:12:16 +0000876 FormatDiagnostic(Argument, FirstDollar, OutStr);
Richard Trieu246b6aa2012-06-26 18:18:47 +0000877
878 // Append first type
879 TDT.PrintTree = false;
880 TDT.PrintFromType = true;
881 getDiags()->ConvertArgToString(Kind, val,
882 Modifier, ModifierLen,
883 Argument, ArgumentLen,
884 FormattedArgs.data(), FormattedArgs.size(),
885 OutStr, QualTypeVals);
Richard Trieu5409d282012-07-10 01:46:04 +0000886 if (!TDT.TemplateDiffUsed)
887 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
888 TDT.FromType));
889
Richard Trieu246b6aa2012-06-26 18:18:47 +0000890 // Append middle text
Richard Trieu529cdf42012-06-29 21:12:16 +0000891 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
Richard Trieu246b6aa2012-06-26 18:18:47 +0000892
893 // Append second type
894 TDT.PrintFromType = false;
895 getDiags()->ConvertArgToString(Kind, val,
896 Modifier, ModifierLen,
897 Argument, ArgumentLen,
898 FormattedArgs.data(), FormattedArgs.size(),
899 OutStr, QualTypeVals);
Richard Trieu5409d282012-07-10 01:46:04 +0000900 if (!TDT.TemplateDiffUsed)
901 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
902 TDT.ToType));
903
Richard Trieu246b6aa2012-06-26 18:18:47 +0000904 // Append end text
Richard Trieu529cdf42012-06-29 21:12:16 +0000905 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
Richard Trieu246b6aa2012-06-26 18:18:47 +0000906 break;
Nico Weber7bfaaae2008-08-10 19:59:06 +0000907 }
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000908
909 // Remember this argument info for subsequent formatting operations. Turn
910 // std::strings into a null terminated string to make it be the same case as
911 // all the other ones.
Richard Trieu246b6aa2012-06-26 18:18:47 +0000912 if (Kind == DiagnosticsEngine::ak_qualtype_pair)
913 continue;
914 else if (Kind != DiagnosticsEngine::ak_std_string)
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000915 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
916 else
David Blaikied6471f72011-09-25 23:23:43 +0000917 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_c_string,
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000918 (intptr_t)getArgStdStr(ArgNo).c_str()));
919
Nico Weber7bfaaae2008-08-10 19:59:06 +0000920 }
Richard Trieu246b6aa2012-06-26 18:18:47 +0000921
922 // Append the type tree to the end of the diagnostics.
923 OutStr.append(Tree.begin(), Tree.end());
Nico Weber7bfaaae2008-08-10 19:59:06 +0000924}
Ted Kremenekcabe6682009-01-23 20:28:53 +0000925
Douglas Gregora88084b2010-02-18 18:08:43 +0000926StoredDiagnostic::StoredDiagnostic() { }
927
David Blaikied6471f72011-09-25 23:23:43 +0000928StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000929 StringRef Message)
Benjamin Kramera6a32e22010-11-19 17:36:51 +0000930 : ID(ID), Level(Level), Loc(), Message(Message) { }
Douglas Gregora88084b2010-02-18 18:08:43 +0000931
David Blaikied6471f72011-09-25 23:23:43 +0000932StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level,
David Blaikie40847cf2011-09-26 01:18:08 +0000933 const Diagnostic &Info)
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000934 : ID(Info.getID()), Level(Level)
935{
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000936 assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
937 "Valid source location without setting a source manager for diagnostic");
938 if (Info.getLocation().isValid())
939 Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000940 SmallString<64> Message;
Douglas Gregora88084b2010-02-18 18:08:43 +0000941 Info.FormatDiagnostic(Message);
942 this->Message.assign(Message.begin(), Message.end());
943
944 Ranges.reserve(Info.getNumRanges());
945 for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I)
946 Ranges.push_back(Info.getRange(I));
947
Douglas Gregor849b2432010-03-31 17:46:05 +0000948 FixIts.reserve(Info.getNumFixItHints());
949 for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I)
950 FixIts.push_back(Info.getFixItHint(I));
Douglas Gregora88084b2010-02-18 18:08:43 +0000951}
952
David Blaikied6471f72011-09-25 23:23:43 +0000953StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000954 StringRef Message, FullSourceLoc Loc,
Chris Lattner2d3ba4f2011-07-23 17:14:25 +0000955 ArrayRef<CharSourceRange> Ranges,
956 ArrayRef<FixItHint> Fixits)
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000957 : ID(ID), Level(Level), Loc(Loc), Message(Message)
958{
959 this->Ranges.assign(Ranges.begin(), Ranges.end());
960 this->FixIts.assign(FixIts.begin(), FixIts.end());
961}
962
Douglas Gregora88084b2010-02-18 18:08:43 +0000963StoredDiagnostic::~StoredDiagnostic() { }
964
Ted Kremenekcabe6682009-01-23 20:28:53 +0000965/// IncludeInDiagnosticCounts - This method (whose default implementation
966/// returns true) indicates whether the diagnostics handled by this
David Blaikie78ad0b92011-09-25 23:39:51 +0000967/// DiagnosticConsumer should be included in the number of diagnostics
David Blaikied6471f72011-09-25 23:23:43 +0000968/// reported by DiagnosticsEngine.
David Blaikie78ad0b92011-09-25 23:39:51 +0000969bool DiagnosticConsumer::IncludeInDiagnosticCounts() const { return true; }
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000970
David Blaikie99ba9e32011-12-20 02:48:34 +0000971void IgnoringDiagConsumer::anchor() { }
972
Benjamin Kramerd7a3e2c2012-02-07 22:29:24 +0000973PartialDiagnostic::StorageAllocator::StorageAllocator() {
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000974 for (unsigned I = 0; I != NumCached; ++I)
975 FreeList[I] = Cached + I;
976 NumFreeListEntries = NumCached;
977}
978
Benjamin Kramerd7a3e2c2012-02-07 22:29:24 +0000979PartialDiagnostic::StorageAllocator::~StorageAllocator() {
Chad Rosierdfaee492012-02-07 23:24:49 +0000980 // Don't assert if we are in a CrashRecovery context, as this invariant may
981 // be invalidated during a crash.
982 assert((NumFreeListEntries == NumCached ||
983 llvm::CrashRecoveryContext::isRecoveringFromCrash()) &&
984 "A partial is on the lamb");
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000985}