blob: 5eff86c2b8841576478618d6214b51c3ec935ae9 [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
Jordan Rose3f6f51e2013-02-08 22:30:41 +000014#include "clang/Basic/CharInfo.h"
Ted Kremenekec55c942010-04-12 19:54:17 +000015#include "clang/Basic/Diagnostic.h"
Douglas Gregor02c23eb2012-10-23 22:26:28 +000016#include "clang/Basic/DiagnosticOptions.h"
Chris Lattner43b628c2008-11-19 07:32:16 +000017#include "clang/Basic/IdentifierTable.h"
Ted Kremenekec55c942010-04-12 19:54:17 +000018#include "clang/Basic/PartialDiagnostic.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000019#include "llvm/ADT/SmallString.h"
Jordan Rose615a0922012-09-22 01:24:42 +000020#include "llvm/ADT/StringExtras.h"
Ted Kremenek03201fb2011-03-21 18:40:07 +000021#include "llvm/Support/CrashRecoveryContext.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000022#include "llvm/Support/raw_ostream.h"
Ted Kremenek03201fb2011-03-21 18:40:07 +000023
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
David Blaikied6471f72011-09-25 23:23:43 +000026static void DummyArgToStringFn(DiagnosticsEngine::ArgumentKind AK, intptr_t QT,
Chris Lattner3fdf4b02008-11-23 09:21:17 +000027 const char *Modifier, unsigned ML,
28 const char *Argument, unsigned ArgLen,
David Blaikied6471f72011-09-25 23:23:43 +000029 const DiagnosticsEngine::ArgumentValue *PrevArgs,
Chris Lattnerb54d8af2009-10-20 05:25:22 +000030 unsigned NumPrevArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +000031 SmallVectorImpl<char> &Output,
Chandler Carruth0673cb32011-07-11 17:49:21 +000032 void *Cookie,
Bill Wendling341785e2012-02-22 09:51:33 +000033 ArrayRef<intptr_t> QualTypeVals) {
Chris Lattner3fdf4b02008-11-23 09:21:17 +000034 const char *Str = "<can't format argument>";
Chris Lattner22caddc2008-11-23 09:13:29 +000035 Output.append(Str, Str+strlen(Str));
36}
37
38
David Blaikied6471f72011-09-25 23:23:43 +000039DiagnosticsEngine::DiagnosticsEngine(
Dylan Noblesmithc93dc782012-02-20 14:00:23 +000040 const IntrusiveRefCntPtr<DiagnosticIDs> &diags,
Douglas Gregor02c23eb2012-10-23 22:26:28 +000041 DiagnosticOptions *DiagOpts,
David Blaikie78ad0b92011-09-25 23:39:51 +000042 DiagnosticConsumer *client, bool ShouldOwnClient)
Douglas Gregor02c23eb2012-10-23 22:26:28 +000043 : Diags(diags), DiagOpts(DiagOpts), Client(client),
44 OwnsDiagClient(ShouldOwnClient), SourceMgr(0) {
Chris Lattner3fdf4b02008-11-23 09:21:17 +000045 ArgToStringFn = DummyArgToStringFn;
Chris Lattner92dd3862009-02-19 23:53:20 +000046 ArgToStringCookie = 0;
Mike Stump1eb44332009-09-09 15:08:12 +000047
Douglas Gregorcc5888d2010-07-31 00:40:00 +000048 AllExtensionsSilenced = 0;
49 IgnoreAllWarnings = false;
50 WarningsAsErrors = false;
Rafael Espindola363f99d2013-05-03 15:01:36 +000051 WarnOnSpellCheck = false;
Ted Kremenek1e473cc2011-08-18 01:12:56 +000052 EnableAllWarnings = false;
Douglas Gregorcc5888d2010-07-31 00:40:00 +000053 ErrorsAsFatal = false;
54 SuppressSystemWarnings = false;
55 SuppressAllDiagnostics = false;
Richard Trieu246b6aa2012-06-26 18:18:47 +000056 ElideType = true;
57 PrintTemplateTree = false;
58 ShowColors = false;
Douglas Gregorcc5888d2010-07-31 00:40:00 +000059 ShowOverloads = Ovl_All;
60 ExtBehavior = Ext_Ignore;
61
62 ErrorLimit = 0;
63 TemplateBacktraceLimit = 0;
Richard Smith08d6e032011-12-16 19:06:07 +000064 ConstexprBacktraceLimit = 0;
Douglas Gregorcc5888d2010-07-31 00:40:00 +000065
Douglas Gregorabc563f2010-07-19 21:46:24 +000066 Reset();
Reid Spencer5f016e22007-07-11 17:01:13 +000067}
68
David Blaikied6471f72011-09-25 23:23:43 +000069DiagnosticsEngine::~DiagnosticsEngine() {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000070 if (OwnsDiagClient)
71 delete Client;
Chris Lattner182745a2007-12-02 01:09:57 +000072}
73
David Blaikie78ad0b92011-09-25 23:39:51 +000074void DiagnosticsEngine::setClient(DiagnosticConsumer *client,
David Blaikied6471f72011-09-25 23:23:43 +000075 bool ShouldOwnClient) {
Douglas Gregor4f5e21e2011-01-31 22:04:05 +000076 if (OwnsDiagClient && Client)
77 delete Client;
78
79 Client = client;
80 OwnsDiagClient = ShouldOwnClient;
81}
Chris Lattner04ae2df2009-07-12 21:18:45 +000082
David Blaikied6471f72011-09-25 23:23:43 +000083void DiagnosticsEngine::pushMappings(SourceLocation Loc) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +000084 DiagStateOnPushStack.push_back(GetCurDiagState());
Chris Lattner04ae2df2009-07-12 21:18:45 +000085}
86
David Blaikied6471f72011-09-25 23:23:43 +000087bool DiagnosticsEngine::popMappings(SourceLocation Loc) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +000088 if (DiagStateOnPushStack.empty())
Chris Lattner04ae2df2009-07-12 21:18:45 +000089 return false;
90
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +000091 if (DiagStateOnPushStack.back() != GetCurDiagState()) {
92 // State changed at some point between push/pop.
93 PushDiagStatePoint(DiagStateOnPushStack.back(), Loc);
94 }
95 DiagStateOnPushStack.pop_back();
Chris Lattner04ae2df2009-07-12 21:18:45 +000096 return true;
97}
98
David Blaikied6471f72011-09-25 23:23:43 +000099void DiagnosticsEngine::Reset() {
Douglas Gregorabc563f2010-07-19 21:46:24 +0000100 ErrorOccurred = false;
DeLesley Hutchins12f37e42012-12-07 22:53:48 +0000101 UncompilableErrorOccurred = false;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000102 FatalErrorOccurred = false;
Douglas Gregor85bea972011-07-06 17:40:26 +0000103 UnrecoverableErrorOccurred = false;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000104
105 NumWarnings = 0;
106 NumErrors = 0;
107 NumErrorsSuppressed = 0;
Argyrios Kyrtzidisc0a575f2011-07-29 01:25:44 +0000108 TrapNumErrorsOccurred = 0;
109 TrapNumUnrecoverableErrorsOccurred = 0;
Douglas Gregor85bea972011-07-06 17:40:26 +0000110
Douglas Gregorabc563f2010-07-19 21:46:24 +0000111 CurDiagID = ~0U;
Richard Smith5b9268f2012-12-20 02:22:15 +0000112 LastDiagLevel = DiagnosticIDs::Ignored;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000113 DelayedDiagID = 0;
Argyrios Kyrtzidisdc0a2da2011-03-26 18:58:17 +0000114
115 // Clear state related to #pragma diagnostic.
116 DiagStates.clear();
117 DiagStatePoints.clear();
118 DiagStateOnPushStack.clear();
119
120 // Create a DiagState and DiagStatePoint representing diagnostic changes
121 // through command-line.
122 DiagStates.push_back(DiagState());
Richard Smith00aae522012-08-14 04:19:29 +0000123 DiagStatePoints.push_back(DiagStatePoint(&DiagStates.back(), FullSourceLoc()));
Douglas Gregorabc563f2010-07-19 21:46:24 +0000124}
Reid Spencer5f016e22007-07-11 17:01:13 +0000125
David Blaikied6471f72011-09-25 23:23:43 +0000126void DiagnosticsEngine::SetDelayedDiagnostic(unsigned DiagID, StringRef Arg1,
Chad Rosierdfaee492012-02-07 23:24:49 +0000127 StringRef Arg2) {
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000128 if (DelayedDiagID)
129 return;
130
131 DelayedDiagID = DiagID;
Douglas Gregor9e2dac92010-03-22 15:47:45 +0000132 DelayedDiagArg1 = Arg1.str();
133 DelayedDiagArg2 = Arg2.str();
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000134}
135
David Blaikied6471f72011-09-25 23:23:43 +0000136void DiagnosticsEngine::ReportDelayed() {
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000137 Report(DelayedDiagID) << DelayedDiagArg1 << DelayedDiagArg2;
138 DelayedDiagID = 0;
139 DelayedDiagArg1.clear();
140 DelayedDiagArg2.clear();
141}
142
David Blaikied6471f72011-09-25 23:23:43 +0000143DiagnosticsEngine::DiagStatePointsTy::iterator
144DiagnosticsEngine::GetDiagStatePointForLoc(SourceLocation L) const {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000145 assert(!DiagStatePoints.empty());
146 assert(DiagStatePoints.front().Loc.isInvalid() &&
147 "Should have created a DiagStatePoint for command-line");
148
Richard Smith9e63dc52012-08-17 00:55:32 +0000149 if (!SourceMgr)
150 return DiagStatePoints.end() - 1;
151
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000152 FullSourceLoc Loc(L, *SourceMgr);
153 if (Loc.isInvalid())
154 return DiagStatePoints.end() - 1;
155
156 DiagStatePointsTy::iterator Pos = DiagStatePoints.end();
157 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
158 if (LastStateChangePos.isValid() &&
159 Loc.isBeforeInTranslationUnitThan(LastStateChangePos))
160 Pos = std::upper_bound(DiagStatePoints.begin(), DiagStatePoints.end(),
161 DiagStatePoint(0, Loc));
162 --Pos;
163 return Pos;
164}
165
David Blaikied6471f72011-09-25 23:23:43 +0000166void DiagnosticsEngine::setDiagnosticMapping(diag::kind Diag, diag::Mapping Map,
Chad Rosierdfaee492012-02-07 23:24:49 +0000167 SourceLocation L) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000168 assert(Diag < diag::DIAG_UPPER_LIMIT &&
169 "Can only map builtin diagnostics");
170 assert((Diags->isBuiltinWarningOrExtension(Diag) ||
171 (Map == diag::MAP_FATAL || Map == diag::MAP_ERROR)) &&
172 "Cannot map errors into warnings!");
173 assert(!DiagStatePoints.empty());
Richard Smithc95ad002012-08-14 22:37:22 +0000174 assert((L.isInvalid() || SourceMgr) && "No SourceMgr for valid location");
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000175
Richard Smithc95ad002012-08-14 22:37:22 +0000176 FullSourceLoc Loc = SourceMgr? FullSourceLoc(L, *SourceMgr) : FullSourceLoc();
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000177 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
Chad Rosier7a0a31c2012-02-03 01:49:51 +0000178 // Don't allow a mapping to a warning override an error/fatal mapping.
179 if (Map == diag::MAP_WARNING) {
180 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(Diag);
181 if (Info.getMapping() == diag::MAP_ERROR ||
182 Info.getMapping() == diag::MAP_FATAL)
183 Map = Info.getMapping();
184 }
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +0000185 DiagnosticMappingInfo MappingInfo = makeMappingInfo(Map, L);
Daniel Dunbar53201a82011-10-04 21:17:24 +0000186
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000187 // Common case; setting all the diagnostics of a group in one place.
188 if (Loc.isInvalid() || Loc == LastStateChangePos) {
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000189 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000190 return;
191 }
192
193 // Another common case; modifying diagnostic state in a source location
194 // after the previous one.
195 if ((Loc.isValid() && LastStateChangePos.isInvalid()) ||
196 LastStateChangePos.isBeforeInTranslationUnitThan(Loc)) {
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000197 // A diagnostic pragma occurred, create a new DiagState initialized with
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000198 // the current one and a new DiagStatePoint to record at which location
199 // the new state became active.
200 DiagStates.push_back(*GetCurDiagState());
201 PushDiagStatePoint(&DiagStates.back(), Loc);
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000202 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000203 return;
204 }
205
206 // We allow setting the diagnostic state in random source order for
207 // completeness but it should not be actually happening in normal practice.
208
209 DiagStatePointsTy::iterator Pos = GetDiagStatePointForLoc(Loc);
210 assert(Pos != DiagStatePoints.end());
211
212 // Update all diagnostic states that are active after the given location.
213 for (DiagStatePointsTy::iterator
214 I = Pos+1, E = DiagStatePoints.end(); I != E; ++I) {
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000215 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000216 }
217
218 // If the location corresponds to an existing point, just update its state.
219 if (Pos->Loc == Loc) {
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000220 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000221 return;
222 }
223
224 // Create a new state/point and fit it into the vector of DiagStatePoints
225 // so that the vector is always ordered according to location.
226 Pos->Loc.isBeforeInTranslationUnitThan(Loc);
227 DiagStates.push_back(*Pos->State);
228 DiagState *NewState = &DiagStates.back();
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000229 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000230 DiagStatePoints.insert(Pos+1, DiagStatePoint(NewState,
231 FullSourceLoc(Loc, *SourceMgr)));
232}
233
Daniel Dunbar3f839462011-09-29 01:47:16 +0000234bool DiagnosticsEngine::setDiagnosticGroupMapping(
235 StringRef Group, diag::Mapping Map, SourceLocation Loc)
236{
237 // Get the diagnostics in this group.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000238 SmallVector<diag::kind, 8> GroupDiags;
Daniel Dunbar3f839462011-09-29 01:47:16 +0000239 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
240 return true;
241
242 // Set the mapping.
243 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i)
244 setDiagnosticMapping(GroupDiags[i], Map, Loc);
245
246 return false;
247}
248
Chad Rosier3f225092012-02-07 19:55:45 +0000249void DiagnosticsEngine::setDiagnosticWarningAsError(diag::kind Diag,
250 bool Enabled) {
251 // If we are enabling this feature, just set the diagnostic mappings to map to
252 // errors.
253 if (Enabled)
254 setDiagnosticMapping(Diag, diag::MAP_ERROR, SourceLocation());
255
256 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
257 // potentially downgrade anything already mapped to be a warning.
258 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(Diag);
259
260 if (Info.getMapping() == diag::MAP_ERROR ||
261 Info.getMapping() == diag::MAP_FATAL)
262 Info.setMapping(diag::MAP_WARNING);
263
264 Info.setNoWarningAsError(true);
265}
266
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000267bool DiagnosticsEngine::setDiagnosticGroupWarningAsError(StringRef Group,
268 bool Enabled) {
Daniel Dunbara5e41332011-09-29 01:52:06 +0000269 // If we are enabling this feature, just set the diagnostic mappings to map to
270 // errors.
271 if (Enabled)
272 return setDiagnosticGroupMapping(Group, diag::MAP_ERROR);
273
274 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
275 // potentially downgrade anything already mapped to be a warning.
276
277 // Get the diagnostics in this group.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000278 SmallVector<diag::kind, 8> GroupDiags;
Daniel Dunbara5e41332011-09-29 01:52:06 +0000279 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
280 return true;
281
282 // Perform the mapping change.
283 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i) {
284 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(
285 GroupDiags[i]);
286
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000287 if (Info.getMapping() == diag::MAP_ERROR ||
288 Info.getMapping() == diag::MAP_FATAL)
289 Info.setMapping(diag::MAP_WARNING);
290
Daniel Dunbara5e41332011-09-29 01:52:06 +0000291 Info.setNoWarningAsError(true);
292 }
293
294 return false;
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000295}
296
Chad Rosier3f225092012-02-07 19:55:45 +0000297void DiagnosticsEngine::setDiagnosticErrorAsFatal(diag::kind Diag,
298 bool Enabled) {
299 // If we are enabling this feature, just set the diagnostic mappings to map to
300 // errors.
301 if (Enabled)
302 setDiagnosticMapping(Diag, diag::MAP_FATAL, SourceLocation());
303
304 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
305 // potentially downgrade anything already mapped to be a warning.
306 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(Diag);
307
308 if (Info.getMapping() == diag::MAP_FATAL)
309 Info.setMapping(diag::MAP_ERROR);
310
311 Info.setNoErrorAsFatal(true);
312}
313
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000314bool DiagnosticsEngine::setDiagnosticGroupErrorAsFatal(StringRef Group,
315 bool Enabled) {
Daniel Dunbara5e41332011-09-29 01:52:06 +0000316 // If we are enabling this feature, just set the diagnostic mappings to map to
317 // fatal errors.
318 if (Enabled)
319 return setDiagnosticGroupMapping(Group, diag::MAP_FATAL);
320
321 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
322 // potentially downgrade anything already mapped to be an error.
323
324 // Get the diagnostics in this group.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000325 SmallVector<diag::kind, 8> GroupDiags;
Daniel Dunbara5e41332011-09-29 01:52:06 +0000326 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
327 return true;
328
329 // Perform the mapping change.
330 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i) {
331 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(
332 GroupDiags[i]);
333
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000334 if (Info.getMapping() == diag::MAP_FATAL)
335 Info.setMapping(diag::MAP_ERROR);
336
Daniel Dunbara5e41332011-09-29 01:52:06 +0000337 Info.setNoErrorAsFatal(true);
338 }
339
340 return false;
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000341}
342
Argyrios Kyrtzidis82e64112012-01-28 04:35:52 +0000343void DiagnosticsEngine::setMappingToAllDiagnostics(diag::Mapping Map,
Argyrios Kyrtzidis11583c72012-01-27 06:15:43 +0000344 SourceLocation Loc) {
345 // Get all the diagnostics.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000346 SmallVector<diag::kind, 64> AllDiags;
Argyrios Kyrtzidis11583c72012-01-27 06:15:43 +0000347 Diags->getAllDiagnostics(AllDiags);
348
349 // Set the mapping.
350 for (unsigned i = 0, e = AllDiags.size(); i != e; ++i)
351 if (Diags->isBuiltinWarningOrExtension(AllDiags[i]))
352 setDiagnosticMapping(AllDiags[i], Map, Loc);
Argyrios Kyrtzidis11583c72012-01-27 06:15:43 +0000353}
354
David Blaikied6471f72011-09-25 23:23:43 +0000355void DiagnosticsEngine::Report(const StoredDiagnostic &storedDiag) {
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000356 assert(CurDiagID == ~0U && "Multiple diagnostics in flight at once!");
357
358 CurDiagLoc = storedDiag.getLocation();
359 CurDiagID = storedDiag.getID();
360 NumDiagArgs = 0;
361
362 NumDiagRanges = storedDiag.range_size();
Daniel Dunbar981e2792012-03-13 18:21:17 +0000363 assert(NumDiagRanges < DiagnosticsEngine::MaxRanges &&
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000364 "Too many arguments to diagnostic!");
365 unsigned i = 0;
366 for (StoredDiagnostic::range_iterator
367 RI = storedDiag.range_begin(),
368 RE = storedDiag.range_end(); RI != RE; ++RI)
369 DiagRanges[i++] = *RI;
370
Daniel Dunbar981e2792012-03-13 18:21:17 +0000371 assert(NumDiagRanges < DiagnosticsEngine::MaxFixItHints &&
372 "Too many arguments to diagnostic!");
373 NumDiagFixItHints = 0;
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000374 for (StoredDiagnostic::fixit_iterator
375 FI = storedDiag.fixit_begin(),
376 FE = storedDiag.fixit_end(); FI != FE; ++FI)
Daniel Dunbar981e2792012-03-13 18:21:17 +0000377 DiagFixItHints[NumDiagFixItHints++] = *FI;
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000378
David Blaikie78ad0b92011-09-25 23:39:51 +0000379 assert(Client && "DiagnosticConsumer not set!");
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000380 Level DiagLevel = storedDiag.getLevel();
David Blaikie40847cf2011-09-26 01:18:08 +0000381 Diagnostic Info(this, storedDiag.getMessage());
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000382 Client->HandleDiagnostic(DiagLevel, Info);
383 if (Client->IncludeInDiagnosticCounts()) {
David Blaikied6471f72011-09-25 23:23:43 +0000384 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000385 ++NumWarnings;
386 }
387
388 CurDiagID = ~0U;
389}
390
Jordan Rosec6d64a22012-07-11 16:50:36 +0000391bool DiagnosticsEngine::EmitCurrentDiagnostic(bool Force) {
392 assert(getClient() && "DiagnosticClient not set!");
393
394 bool Emitted;
395 if (Force) {
396 Diagnostic Info(this);
397
398 // Figure out the diagnostic level of this message.
399 DiagnosticIDs::Level DiagLevel
400 = Diags->getDiagnosticLevel(Info.getID(), Info.getLocation(), *this);
401
402 Emitted = (DiagLevel != DiagnosticIDs::Ignored);
403 if (Emitted) {
404 // Emit the diagnostic regardless of suppression level.
405 Diags->EmitDiag(*this, DiagLevel);
406 }
407 } else {
408 // Process the diagnostic, sending the accumulated information to the
409 // DiagnosticConsumer.
410 Emitted = ProcessDiag();
411 }
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000412
413 // Clear out the current diagnostic object.
Daniel Dunbar3054f092012-03-13 21:02:14 +0000414 unsigned DiagID = CurDiagID;
415 Clear();
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000416
417 // If there was a delayed diagnostic, emit it now.
Jordan Rosec6d64a22012-07-11 16:50:36 +0000418 if (!Force && DelayedDiagID && DelayedDiagID != DiagID)
Daniel Dunbar3054f092012-03-13 21:02:14 +0000419 ReportDelayed();
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000420
421 return Emitted;
422}
423
Nico Weber7bfaaae2008-08-10 19:59:06 +0000424
David Blaikie78ad0b92011-09-25 23:39:51 +0000425DiagnosticConsumer::~DiagnosticConsumer() {}
Nico Weber7bfaaae2008-08-10 19:59:06 +0000426
David Blaikie78ad0b92011-09-25 23:39:51 +0000427void DiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
David Blaikie40847cf2011-09-26 01:18:08 +0000428 const Diagnostic &Info) {
Argyrios Kyrtzidisf2224d82010-11-18 20:06:46 +0000429 if (!IncludeInDiagnosticCounts())
430 return;
431
David Blaikied6471f72011-09-25 23:23:43 +0000432 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidisf2224d82010-11-18 20:06:46 +0000433 ++NumWarnings;
David Blaikied6471f72011-09-25 23:23:43 +0000434 else if (DiagLevel >= DiagnosticsEngine::Error)
Argyrios Kyrtzidisf2224d82010-11-18 20:06:46 +0000435 ++NumErrors;
436}
Chris Lattnerf4c83962008-11-19 06:51:40 +0000437
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000438/// ModifierIs - Return true if the specified modifier matches specified string.
439template <std::size_t StrLen>
440static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
441 const char (&Str)[StrLen]) {
442 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
443}
444
John McCall909c1822010-01-14 20:11:39 +0000445/// ScanForward - Scans forward, looking for the given character, skipping
446/// nested clauses and escaped characters.
447static const char *ScanFormat(const char *I, const char *E, char Target) {
448 unsigned Depth = 0;
449
450 for ( ; I != E; ++I) {
451 if (Depth == 0 && *I == Target) return I;
452 if (Depth != 0 && *I == '}') Depth--;
453
454 if (*I == '%') {
455 I++;
456 if (I == E) break;
457
458 // Escaped characters get implicitly skipped here.
459
460 // Format specifier.
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000461 if (!isDigit(*I) && !isPunctuation(*I)) {
462 for (I++; I != E && !isDigit(*I) && *I != '{'; I++) ;
John McCall909c1822010-01-14 20:11:39 +0000463 if (I == E) break;
464 if (*I == '{')
465 Depth++;
466 }
467 }
468 }
469 return E;
470}
471
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000472/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
473/// like this: %select{foo|bar|baz}2. This means that the integer argument
474/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
475/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
476/// This is very useful for certain classes of variant diagnostics.
David Blaikie40847cf2011-09-26 01:18:08 +0000477static void HandleSelectModifier(const Diagnostic &DInfo, unsigned ValNo,
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000478 const char *Argument, unsigned ArgumentLen,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000479 SmallVectorImpl<char> &OutStr) {
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000480 const char *ArgumentEnd = Argument+ArgumentLen;
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000482 // Skip over 'ValNo' |'s.
483 while (ValNo) {
John McCall909c1822010-01-14 20:11:39 +0000484 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000485 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
486 " larger than the number of options in the diagnostic string!");
487 Argument = NextVal+1; // Skip this string.
488 --ValNo;
489 }
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000491 // Get the end of the value. This is either the } or the |.
John McCall909c1822010-01-14 20:11:39 +0000492 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
John McCall9f286142010-01-13 23:58:20 +0000493
494 // Recursively format the result of the select clause into the output string.
495 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000496}
497
498/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
499/// letter 's' to the string if the value is not 1. This is used in cases like
500/// this: "you idiot, you have %4 parameter%s4!".
501static void HandleIntegerSModifier(unsigned ValNo,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000502 SmallVectorImpl<char> &OutStr) {
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000503 if (ValNo != 1)
504 OutStr.push_back('s');
505}
506
John McCall3be16b72010-01-14 00:50:32 +0000507/// HandleOrdinalModifier - Handle the integer 'ord' modifier. This
508/// prints the ordinal form of the given integer, with 1 corresponding
509/// to the first ordinal. Currently this is hard-coded to use the
510/// English form.
511static void HandleOrdinalModifier(unsigned ValNo,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000512 SmallVectorImpl<char> &OutStr) {
John McCall3be16b72010-01-14 00:50:32 +0000513 assert(ValNo != 0 && "ValNo must be strictly positive!");
514
515 llvm::raw_svector_ostream Out(OutStr);
516
517 // We could use text forms for the first N ordinals, but the numeric
518 // forms are actually nicer in diagnostics because they stand out.
Jordan Rose615a0922012-09-22 01:24:42 +0000519 Out << ValNo << llvm::getOrdinalSuffix(ValNo);
John McCall3be16b72010-01-14 00:50:32 +0000520}
521
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000522
Sebastian Redle4c452c2008-11-22 13:44:36 +0000523/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000524static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000525 // Programming 101: Parse a decimal number :-)
526 unsigned Val = 0;
527 while (Start != End && *Start >= '0' && *Start <= '9') {
528 Val *= 10;
529 Val += *Start - '0';
530 ++Start;
531 }
532 return Val;
533}
534
535/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000536static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000537 if (*Start != '[') {
538 unsigned Ref = PluralNumber(Start, End);
539 return Ref == Val;
540 }
541
542 ++Start;
543 unsigned Low = PluralNumber(Start, End);
544 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
545 ++Start;
546 unsigned High = PluralNumber(Start, End);
547 assert(*Start == ']' && "Bad plural expression syntax: expected )");
548 ++Start;
549 return Low <= Val && Val <= High;
550}
551
552/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000553static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000554 // Empty condition?
555 if (*Start == ':')
556 return true;
557
558 while (1) {
559 char C = *Start;
560 if (C == '%') {
561 // Modulo expression
562 ++Start;
563 unsigned Arg = PluralNumber(Start, End);
564 assert(*Start == '=' && "Bad plural expression syntax: expected =");
565 ++Start;
566 unsigned ValMod = ValNo % Arg;
567 if (TestPluralRange(ValMod, Start, End))
568 return true;
569 } else {
Sebastian Redle2065322008-11-27 07:28:14 +0000570 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redle4c452c2008-11-22 13:44:36 +0000571 "Bad plural expression syntax: unexpected character");
572 // Range expression
573 if (TestPluralRange(ValNo, Start, End))
574 return true;
575 }
576
577 // Scan for next or-expr part.
578 Start = std::find(Start, End, ',');
Mike Stump1eb44332009-09-09 15:08:12 +0000579 if (Start == End)
Sebastian Redle4c452c2008-11-22 13:44:36 +0000580 break;
581 ++Start;
582 }
583 return false;
584}
585
586/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
587/// for complex plural forms, or in languages where all plurals are complex.
588/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
589/// conditions that are tested in order, the form corresponding to the first
590/// that applies being emitted. The empty condition is always true, making the
591/// last form a default case.
592/// Conditions are simple boolean expressions, where n is the number argument.
593/// Here are the rules.
594/// condition := expression | empty
595/// empty := -> always true
596/// expression := numeric [',' expression] -> logical or
597/// numeric := range -> true if n in range
598/// | '%' number '=' range -> true if n % number in range
599/// range := number
600/// | '[' number ',' number ']' -> ranges are inclusive both ends
601///
602/// Here are some examples from the GNU gettext manual written in this form:
603/// English:
604/// {1:form0|:form1}
605/// Latvian:
606/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
607/// Gaeilge:
608/// {1:form0|2:form1|:form2}
609/// Romanian:
610/// {1:form0|0,%100=[1,19]:form1|:form2}
611/// Lithuanian:
612/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
613/// Russian (requires repeated form):
614/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
615/// Slovak
616/// {1:form0|[2,4]:form1|:form2}
617/// Polish (requires repeated form):
618/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
David Blaikie40847cf2011-09-26 01:18:08 +0000619static void HandlePluralModifier(const Diagnostic &DInfo, unsigned ValNo,
Sebastian Redle4c452c2008-11-22 13:44:36 +0000620 const char *Argument, unsigned ArgumentLen,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000621 SmallVectorImpl<char> &OutStr) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000622 const char *ArgumentEnd = Argument + ArgumentLen;
623 while (1) {
624 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
625 const char *ExprEnd = Argument;
626 while (*ExprEnd != ':') {
627 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
628 ++ExprEnd;
629 }
630 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
631 Argument = ExprEnd + 1;
John McCall909c1822010-01-14 20:11:39 +0000632 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
John McCalle53a44b2010-10-14 01:55:31 +0000633
634 // Recursively format the result of the plural clause into the
635 // output string.
636 DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000637 return;
638 }
John McCall909c1822010-01-14 20:11:39 +0000639 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
Sebastian Redle4c452c2008-11-22 13:44:36 +0000640 }
641}
642
643
Chris Lattnerf4c83962008-11-19 06:51:40 +0000644/// FormatDiagnostic - Format this diagnostic into a string, substituting the
645/// formal arguments into the %0 slots. The result is appended onto the Str
646/// array.
David Blaikie40847cf2011-09-26 01:18:08 +0000647void Diagnostic::
Chris Lattner5f9e2722011-07-23 10:55:15 +0000648FormatDiagnostic(SmallVectorImpl<char> &OutStr) const {
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000649 if (!StoredDiagMessage.empty()) {
650 OutStr.append(StoredDiagMessage.begin(), StoredDiagMessage.end());
651 return;
652 }
653
Chris Lattner5f9e2722011-07-23 10:55:15 +0000654 StringRef Diag =
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000655 getDiags()->getDiagnosticIDs()->getDescription(getID());
Mike Stump1eb44332009-09-09 15:08:12 +0000656
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000657 FormatDiagnostic(Diag.begin(), Diag.end(), OutStr);
John McCall9f286142010-01-13 23:58:20 +0000658}
659
David Blaikie40847cf2011-09-26 01:18:08 +0000660void Diagnostic::
John McCall9f286142010-01-13 23:58:20 +0000661FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000662 SmallVectorImpl<char> &OutStr) const {
John McCall9f286142010-01-13 23:58:20 +0000663
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000664 /// FormattedArgs - Keep track of all of the arguments formatted by
665 /// ConvertArgToString and pass them into subsequent calls to
666 /// ConvertArgToString, allowing the implementation to avoid redundancies in
667 /// obvious cases.
David Blaikied6471f72011-09-25 23:23:43 +0000668 SmallVector<DiagnosticsEngine::ArgumentValue, 8> FormattedArgs;
Chandler Carruth0673cb32011-07-11 17:49:21 +0000669
670 /// QualTypeVals - Pass a vector of arrays so that QualType names can be
671 /// compared to see if more information is needed to be printed.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000672 SmallVector<intptr_t, 2> QualTypeVals;
Richard Trieu246b6aa2012-06-26 18:18:47 +0000673 SmallVector<char, 64> Tree;
674
Chandler Carruth0673cb32011-07-11 17:49:21 +0000675 for (unsigned i = 0, e = getNumArgs(); i < e; ++i)
David Blaikied6471f72011-09-25 23:23:43 +0000676 if (getArgKind(i) == DiagnosticsEngine::ak_qualtype)
Chandler Carruth0673cb32011-07-11 17:49:21 +0000677 QualTypeVals.push_back(getRawArg(i));
678
Chris Lattnerf4c83962008-11-19 06:51:40 +0000679 while (DiagStr != DiagEnd) {
680 if (DiagStr[0] != '%') {
681 // Append non-%0 substrings to Str if we have one.
682 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
683 OutStr.append(DiagStr, StrEnd);
684 DiagStr = StrEnd;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000685 continue;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000686 } else if (isPunctuation(DiagStr[1])) {
John McCall909c1822010-01-14 20:11:39 +0000687 OutStr.push_back(DiagStr[1]); // %% -> %.
Chris Lattnerf4c83962008-11-19 06:51:40 +0000688 DiagStr += 2;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000689 continue;
690 }
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000692 // Skip the %.
693 ++DiagStr;
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000695 // This must be a placeholder for a diagnostic argument. The format for a
696 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
697 // The digit is a number from 0-9 indicating which argument this comes from.
698 // The modifier is a string of digits from the set [-a-z]+, arguments is a
699 // brace enclosed string.
700 const char *Modifier = 0, *Argument = 0;
701 unsigned ModifierLen = 0, ArgumentLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000702
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000703 // Check to see if we have a modifier. If so eat it.
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000704 if (!isDigit(DiagStr[0])) {
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000705 Modifier = DiagStr;
706 while (DiagStr[0] == '-' ||
707 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
708 ++DiagStr;
709 ModifierLen = DiagStr-Modifier;
Chris Lattnerf4c83962008-11-19 06:51:40 +0000710
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000711 // If we have an argument, get it next.
712 if (DiagStr[0] == '{') {
713 ++DiagStr; // Skip {.
714 Argument = DiagStr;
Mike Stump1eb44332009-09-09 15:08:12 +0000715
John McCall909c1822010-01-14 20:11:39 +0000716 DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
717 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000718 ArgumentLen = DiagStr-Argument;
719 ++DiagStr; // Skip }.
Chris Lattnerf4c83962008-11-19 06:51:40 +0000720 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000721 }
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000723 assert(isDigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner22caddc2008-11-23 09:13:29 +0000724 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000725
Richard Trieu246b6aa2012-06-26 18:18:47 +0000726 // Only used for type diffing.
727 unsigned ArgNo2 = ArgNo;
728
David Blaikied6471f72011-09-25 23:23:43 +0000729 DiagnosticsEngine::ArgumentKind Kind = getArgKind(ArgNo);
Richard Trieu877761c2013-01-30 20:04:31 +0000730 if (ModifierIs(Modifier, ModifierLen, "diff")) {
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000731 assert(*DiagStr == ',' && isDigit(*(DiagStr + 1)) &&
Richard Trieu246b6aa2012-06-26 18:18:47 +0000732 "Invalid format for diff modifier");
733 ++DiagStr; // Comma.
734 ArgNo2 = *DiagStr++ - '0';
Richard Trieu877761c2013-01-30 20:04:31 +0000735 DiagnosticsEngine::ArgumentKind Kind2 = getArgKind(ArgNo2);
736 if (Kind == DiagnosticsEngine::ak_qualtype &&
737 Kind2 == DiagnosticsEngine::ak_qualtype)
738 Kind = DiagnosticsEngine::ak_qualtype_pair;
739 else {
740 // %diff only supports QualTypes. For other kinds of arguments,
741 // use the default printing. For example, if the modifier is:
742 // "%diff{compare $ to $|other text}1,2"
743 // treat it as:
744 // "compare %1 to %2"
745 const char *Pipe = ScanFormat(Argument, Argument + ArgumentLen, '|');
746 const char *FirstDollar = ScanFormat(Argument, Pipe, '$');
747 const char *SecondDollar = ScanFormat(FirstDollar + 1, Pipe, '$');
Filipe Cabecinhas566f0632013-01-30 22:03:24 +0000748 const char ArgStr1[] = { '%', static_cast<char>('0' + ArgNo) };
749 const char ArgStr2[] = { '%', static_cast<char>('0' + ArgNo2) };
Richard Trieu877761c2013-01-30 20:04:31 +0000750 FormatDiagnostic(Argument, FirstDollar, OutStr);
751 FormatDiagnostic(ArgStr1, ArgStr1 + 2, OutStr);
752 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
753 FormatDiagnostic(ArgStr2, ArgStr2 + 2, OutStr);
754 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
755 continue;
756 }
Richard Trieu246b6aa2012-06-26 18:18:47 +0000757 }
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000758
759 switch (Kind) {
Chris Lattner08631c52008-11-23 21:45:46 +0000760 // ---- STRINGS ----
David Blaikied6471f72011-09-25 23:23:43 +0000761 case DiagnosticsEngine::ak_std_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000762 const std::string &S = getArgStdStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000763 assert(ModifierLen == 0 && "No modifiers for strings yet");
764 OutStr.append(S.begin(), S.end());
765 break;
766 }
David Blaikied6471f72011-09-25 23:23:43 +0000767 case DiagnosticsEngine::ak_c_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000768 const char *S = getArgCStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000769 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbare46e3542009-04-20 06:13:16 +0000770
771 // Don't crash if get passed a null pointer by accident.
772 if (!S)
773 S = "(null)";
Mike Stump1eb44332009-09-09 15:08:12 +0000774
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000775 OutStr.append(S, S + strlen(S));
776 break;
777 }
Chris Lattner08631c52008-11-23 21:45:46 +0000778 // ---- INTEGERS ----
David Blaikied6471f72011-09-25 23:23:43 +0000779 case DiagnosticsEngine::ak_sint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000780 int Val = getArgSInt(ArgNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000781
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000782 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCalle53a44b2010-10-14 01:55:31 +0000783 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen,
784 OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000785 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
786 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000787 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCalle53a44b2010-10-14 01:55:31 +0000788 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
789 OutStr);
John McCall3be16b72010-01-14 00:50:32 +0000790 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
791 HandleOrdinalModifier((unsigned)Val, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000792 } else {
793 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbar23e47c62009-10-17 18:12:14 +0000794 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner30bc9652008-11-19 07:22:31 +0000795 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000796 break;
797 }
David Blaikied6471f72011-09-25 23:23:43 +0000798 case DiagnosticsEngine::ak_uint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000799 unsigned Val = getArgUInt(ArgNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000801 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCall9f286142010-01-13 23:58:20 +0000802 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000803 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
804 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000805 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCalle53a44b2010-10-14 01:55:31 +0000806 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
807 OutStr);
John McCall3be16b72010-01-14 00:50:32 +0000808 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
809 HandleOrdinalModifier(Val, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000810 } else {
811 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbar23e47c62009-10-17 18:12:14 +0000812 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner30bc9652008-11-19 07:22:31 +0000813 }
Chris Lattner22caddc2008-11-23 09:13:29 +0000814 break;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000815 }
Chris Lattner08631c52008-11-23 21:45:46 +0000816 // ---- NAMES and TYPES ----
David Blaikied6471f72011-09-25 23:23:43 +0000817 case DiagnosticsEngine::ak_identifierinfo: {
Chris Lattner08631c52008-11-23 21:45:46 +0000818 const IdentifierInfo *II = getArgIdentifier(ArgNo);
819 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbare46e3542009-04-20 06:13:16 +0000820
821 // Don't crash if get passed a null pointer by accident.
822 if (!II) {
823 const char *S = "(null)";
824 OutStr.append(S, S + strlen(S));
825 continue;
826 }
827
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000828 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
Chris Lattner08631c52008-11-23 21:45:46 +0000829 break;
830 }
David Blaikied6471f72011-09-25 23:23:43 +0000831 case DiagnosticsEngine::ak_qualtype:
832 case DiagnosticsEngine::ak_declarationname:
833 case DiagnosticsEngine::ak_nameddecl:
834 case DiagnosticsEngine::ak_nestednamespec:
835 case DiagnosticsEngine::ak_declcontext:
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000836 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000837 Modifier, ModifierLen,
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000838 Argument, ArgumentLen,
839 FormattedArgs.data(), FormattedArgs.size(),
Chandler Carruth0673cb32011-07-11 17:49:21 +0000840 OutStr, QualTypeVals);
Chris Lattner22caddc2008-11-23 09:13:29 +0000841 break;
Richard Trieu246b6aa2012-06-26 18:18:47 +0000842 case DiagnosticsEngine::ak_qualtype_pair:
843 // Create a struct with all the info needed for printing.
844 TemplateDiffTypes TDT;
845 TDT.FromType = getRawArg(ArgNo);
846 TDT.ToType = getRawArg(ArgNo2);
847 TDT.ElideType = getDiags()->ElideType;
848 TDT.ShowColors = getDiags()->ShowColors;
Richard Trieu5409d282012-07-10 01:46:04 +0000849 TDT.TemplateDiffUsed = false;
Richard Trieu246b6aa2012-06-26 18:18:47 +0000850 intptr_t val = reinterpret_cast<intptr_t>(&TDT);
851
Richard Trieu529cdf42012-06-29 21:12:16 +0000852 const char *ArgumentEnd = Argument + ArgumentLen;
853 const char *Pipe = ScanFormat(Argument, ArgumentEnd, '|');
854
Richard Trieu55619772012-07-13 21:18:32 +0000855 // Print the tree. If this diagnostic already has a tree, skip the
856 // second tree.
857 if (getDiags()->PrintTemplateTree && Tree.empty()) {
Richard Trieu246b6aa2012-06-26 18:18:47 +0000858 TDT.PrintFromType = true;
859 TDT.PrintTree = true;
860 getDiags()->ConvertArgToString(Kind, val,
861 Modifier, ModifierLen,
862 Argument, ArgumentLen,
863 FormattedArgs.data(),
864 FormattedArgs.size(),
865 Tree, QualTypeVals);
866 // If there is no tree information, fall back to regular printing.
Richard Trieu529cdf42012-06-29 21:12:16 +0000867 if (!Tree.empty()) {
868 FormatDiagnostic(Pipe + 1, ArgumentEnd, OutStr);
Richard Trieu246b6aa2012-06-26 18:18:47 +0000869 break;
Richard Trieu529cdf42012-06-29 21:12:16 +0000870 }
Richard Trieu246b6aa2012-06-26 18:18:47 +0000871 }
872
873 // Non-tree printing, also the fall-back when tree printing fails.
874 // The fall-back is triggered when the types compared are not templates.
Richard Trieu529cdf42012-06-29 21:12:16 +0000875 const char *FirstDollar = ScanFormat(Argument, ArgumentEnd, '$');
876 const char *SecondDollar = ScanFormat(FirstDollar + 1, ArgumentEnd, '$');
Richard Trieu246b6aa2012-06-26 18:18:47 +0000877
878 // Append before text
Richard Trieu529cdf42012-06-29 21:12:16 +0000879 FormatDiagnostic(Argument, FirstDollar, OutStr);
Richard Trieu246b6aa2012-06-26 18:18:47 +0000880
881 // Append first type
882 TDT.PrintTree = false;
883 TDT.PrintFromType = true;
884 getDiags()->ConvertArgToString(Kind, val,
885 Modifier, ModifierLen,
886 Argument, ArgumentLen,
887 FormattedArgs.data(), FormattedArgs.size(),
888 OutStr, QualTypeVals);
Richard Trieu5409d282012-07-10 01:46:04 +0000889 if (!TDT.TemplateDiffUsed)
890 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
891 TDT.FromType));
892
Richard Trieu246b6aa2012-06-26 18:18:47 +0000893 // Append middle text
Richard Trieu529cdf42012-06-29 21:12:16 +0000894 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
Richard Trieu246b6aa2012-06-26 18:18:47 +0000895
896 // Append second type
897 TDT.PrintFromType = false;
898 getDiags()->ConvertArgToString(Kind, val,
899 Modifier, ModifierLen,
900 Argument, ArgumentLen,
901 FormattedArgs.data(), FormattedArgs.size(),
902 OutStr, QualTypeVals);
Richard Trieu5409d282012-07-10 01:46:04 +0000903 if (!TDT.TemplateDiffUsed)
904 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
905 TDT.ToType));
906
Richard Trieu246b6aa2012-06-26 18:18:47 +0000907 // Append end text
Richard Trieu529cdf42012-06-29 21:12:16 +0000908 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
Richard Trieu246b6aa2012-06-26 18:18:47 +0000909 break;
Nico Weber7bfaaae2008-08-10 19:59:06 +0000910 }
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000911
912 // Remember this argument info for subsequent formatting operations. Turn
913 // std::strings into a null terminated string to make it be the same case as
914 // all the other ones.
Richard Trieu246b6aa2012-06-26 18:18:47 +0000915 if (Kind == DiagnosticsEngine::ak_qualtype_pair)
916 continue;
917 else if (Kind != DiagnosticsEngine::ak_std_string)
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000918 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
919 else
David Blaikied6471f72011-09-25 23:23:43 +0000920 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_c_string,
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000921 (intptr_t)getArgStdStr(ArgNo).c_str()));
922
Nico Weber7bfaaae2008-08-10 19:59:06 +0000923 }
Richard Trieu246b6aa2012-06-26 18:18:47 +0000924
925 // Append the type tree to the end of the diagnostics.
926 OutStr.append(Tree.begin(), Tree.end());
Nico Weber7bfaaae2008-08-10 19:59:06 +0000927}
Ted Kremenekcabe6682009-01-23 20:28:53 +0000928
Douglas Gregora88084b2010-02-18 18:08:43 +0000929StoredDiagnostic::StoredDiagnostic() { }
930
David Blaikied6471f72011-09-25 23:23:43 +0000931StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000932 StringRef Message)
Benjamin Kramera6a32e22010-11-19 17:36:51 +0000933 : ID(ID), Level(Level), Loc(), Message(Message) { }
Douglas Gregora88084b2010-02-18 18:08:43 +0000934
David Blaikied6471f72011-09-25 23:23:43 +0000935StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level,
David Blaikie40847cf2011-09-26 01:18:08 +0000936 const Diagnostic &Info)
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000937 : ID(Info.getID()), Level(Level)
938{
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000939 assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
940 "Valid source location without setting a source manager for diagnostic");
941 if (Info.getLocation().isValid())
942 Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000943 SmallString<64> Message;
Douglas Gregora88084b2010-02-18 18:08:43 +0000944 Info.FormatDiagnostic(Message);
945 this->Message.assign(Message.begin(), Message.end());
946
947 Ranges.reserve(Info.getNumRanges());
948 for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I)
949 Ranges.push_back(Info.getRange(I));
950
Douglas Gregor849b2432010-03-31 17:46:05 +0000951 FixIts.reserve(Info.getNumFixItHints());
952 for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I)
953 FixIts.push_back(Info.getFixItHint(I));
Douglas Gregora88084b2010-02-18 18:08:43 +0000954}
955
David Blaikied6471f72011-09-25 23:23:43 +0000956StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000957 StringRef Message, FullSourceLoc Loc,
Chris Lattner2d3ba4f2011-07-23 17:14:25 +0000958 ArrayRef<CharSourceRange> Ranges,
Aaron Ballmanf9067c62013-02-24 19:08:10 +0000959 ArrayRef<FixItHint> FixIts)
960 : ID(ID), Level(Level), Loc(Loc), Message(Message),
961 Ranges(Ranges.begin(), Ranges.end()), FixIts(FixIts.begin(), FixIts.end())
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000962{
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000963}
964
Douglas Gregora88084b2010-02-18 18:08:43 +0000965StoredDiagnostic::~StoredDiagnostic() { }
966
Ted Kremenekcabe6682009-01-23 20:28:53 +0000967/// IncludeInDiagnosticCounts - This method (whose default implementation
968/// returns true) indicates whether the diagnostics handled by this
David Blaikie78ad0b92011-09-25 23:39:51 +0000969/// DiagnosticConsumer should be included in the number of diagnostics
David Blaikied6471f72011-09-25 23:23:43 +0000970/// reported by DiagnosticsEngine.
David Blaikie78ad0b92011-09-25 23:39:51 +0000971bool DiagnosticConsumer::IncludeInDiagnosticCounts() const { return true; }
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000972
David Blaikie99ba9e32011-12-20 02:48:34 +0000973void IgnoringDiagConsumer::anchor() { }
974
Douglas Gregora4a90ca2013-05-03 22:58:43 +0000975ForwardingDiagnosticConsumer::~ForwardingDiagnosticConsumer() {}
976
977void ForwardingDiagnosticConsumer::HandleDiagnostic(
978 DiagnosticsEngine::Level DiagLevel,
979 const Diagnostic &Info) {
980 Target.HandleDiagnostic(DiagLevel, Info);
981}
982
983void ForwardingDiagnosticConsumer::clear() {
984 DiagnosticConsumer::clear();
985 Target.clear();
986}
987
988bool ForwardingDiagnosticConsumer::IncludeInDiagnosticCounts() const {
989 return Target.IncludeInDiagnosticCounts();
990}
991
992DiagnosticConsumer *
993ForwardingDiagnosticConsumer::clone(DiagnosticsEngine &Diags) const {
994 return new ForwardingDiagnosticConsumer(Target);
995}
996
Benjamin Kramerd7a3e2c2012-02-07 22:29:24 +0000997PartialDiagnostic::StorageAllocator::StorageAllocator() {
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000998 for (unsigned I = 0; I != NumCached; ++I)
999 FreeList[I] = Cached + I;
1000 NumFreeListEntries = NumCached;
1001}
1002
Benjamin Kramerd7a3e2c2012-02-07 22:29:24 +00001003PartialDiagnostic::StorageAllocator::~StorageAllocator() {
Chad Rosierdfaee492012-02-07 23:24:49 +00001004 // Don't assert if we are in a CrashRecovery context, as this invariant may
1005 // be invalidated during a crash.
1006 assert((NumFreeListEntries == NumCached ||
1007 llvm::CrashRecoveryContext::isRecoveringFromCrash()) &&
1008 "A partial is on the lamb");
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00001009}