blob: 45d4b539e8c2168a17ab3cf03439ca8eb5094fa1 [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;
Ted Kremenek1e473cc2011-08-18 01:12:56 +000051 EnableAllWarnings = false;
Douglas Gregorcc5888d2010-07-31 00:40:00 +000052 ErrorsAsFatal = false;
53 SuppressSystemWarnings = false;
54 SuppressAllDiagnostics = false;
Richard Trieu246b6aa2012-06-26 18:18:47 +000055 ElideType = true;
56 PrintTemplateTree = false;
57 ShowColors = false;
Douglas Gregorcc5888d2010-07-31 00:40:00 +000058 ShowOverloads = Ovl_All;
59 ExtBehavior = Ext_Ignore;
60
61 ErrorLimit = 0;
62 TemplateBacktraceLimit = 0;
Richard Smith08d6e032011-12-16 19:06:07 +000063 ConstexprBacktraceLimit = 0;
Douglas Gregorcc5888d2010-07-31 00:40:00 +000064
Douglas Gregorabc563f2010-07-19 21:46:24 +000065 Reset();
Reid Spencer5f016e22007-07-11 17:01:13 +000066}
67
David Blaikied6471f72011-09-25 23:23:43 +000068DiagnosticsEngine::~DiagnosticsEngine() {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000069 if (OwnsDiagClient)
70 delete Client;
Chris Lattner182745a2007-12-02 01:09:57 +000071}
72
David Blaikie78ad0b92011-09-25 23:39:51 +000073void DiagnosticsEngine::setClient(DiagnosticConsumer *client,
David Blaikied6471f72011-09-25 23:23:43 +000074 bool ShouldOwnClient) {
Douglas Gregor4f5e21e2011-01-31 22:04:05 +000075 if (OwnsDiagClient && Client)
76 delete Client;
77
78 Client = client;
79 OwnsDiagClient = ShouldOwnClient;
80}
Chris Lattner04ae2df2009-07-12 21:18:45 +000081
David Blaikied6471f72011-09-25 23:23:43 +000082void DiagnosticsEngine::pushMappings(SourceLocation Loc) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +000083 DiagStateOnPushStack.push_back(GetCurDiagState());
Chris Lattner04ae2df2009-07-12 21:18:45 +000084}
85
David Blaikied6471f72011-09-25 23:23:43 +000086bool DiagnosticsEngine::popMappings(SourceLocation Loc) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +000087 if (DiagStateOnPushStack.empty())
Chris Lattner04ae2df2009-07-12 21:18:45 +000088 return false;
89
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +000090 if (DiagStateOnPushStack.back() != GetCurDiagState()) {
91 // State changed at some point between push/pop.
92 PushDiagStatePoint(DiagStateOnPushStack.back(), Loc);
93 }
94 DiagStateOnPushStack.pop_back();
Chris Lattner04ae2df2009-07-12 21:18:45 +000095 return true;
96}
97
David Blaikied6471f72011-09-25 23:23:43 +000098void DiagnosticsEngine::Reset() {
Douglas Gregorabc563f2010-07-19 21:46:24 +000099 ErrorOccurred = false;
DeLesley Hutchins12f37e42012-12-07 22:53:48 +0000100 UncompilableErrorOccurred = false;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000101 FatalErrorOccurred = false;
Douglas Gregor85bea972011-07-06 17:40:26 +0000102 UnrecoverableErrorOccurred = false;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000103
104 NumWarnings = 0;
105 NumErrors = 0;
106 NumErrorsSuppressed = 0;
Argyrios Kyrtzidisc0a575f2011-07-29 01:25:44 +0000107 TrapNumErrorsOccurred = 0;
108 TrapNumUnrecoverableErrorsOccurred = 0;
Douglas Gregor85bea972011-07-06 17:40:26 +0000109
Douglas Gregorabc563f2010-07-19 21:46:24 +0000110 CurDiagID = ~0U;
Richard Smith5b9268f2012-12-20 02:22:15 +0000111 LastDiagLevel = DiagnosticIDs::Ignored;
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.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000237 SmallVector<diag::kind, 8> GroupDiags;
Daniel Dunbar3f839462011-09-29 01:47:16 +0000238 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.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000277 SmallVector<diag::kind, 8> GroupDiags;
Daniel Dunbara5e41332011-09-29 01:52:06 +0000278 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.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000324 SmallVector<diag::kind, 8> GroupDiags;
Daniel Dunbara5e41332011-09-29 01:52:06 +0000325 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.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000345 SmallVector<diag::kind, 64> AllDiags;
Argyrios Kyrtzidis11583c72012-01-27 06:15:43 +0000346 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.
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000460 if (!isDigit(*I) && !isPunctuation(*I)) {
461 for (I++; I != E && !isDigit(*I) && *I != '{'; I++) ;
John McCall909c1822010-01-14 20:11:39 +0000462 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.
Jordan Rose615a0922012-09-22 01:24:42 +0000518 Out << ValNo << llvm::getOrdinalSuffix(ValNo);
John McCall3be16b72010-01-14 00:50:32 +0000519}
520
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000521
Sebastian Redle4c452c2008-11-22 13:44:36 +0000522/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000523static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000524 // Programming 101: Parse a decimal number :-)
525 unsigned Val = 0;
526 while (Start != End && *Start >= '0' && *Start <= '9') {
527 Val *= 10;
528 Val += *Start - '0';
529 ++Start;
530 }
531 return Val;
532}
533
534/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000535static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000536 if (*Start != '[') {
537 unsigned Ref = PluralNumber(Start, End);
538 return Ref == Val;
539 }
540
541 ++Start;
542 unsigned Low = PluralNumber(Start, End);
543 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
544 ++Start;
545 unsigned High = PluralNumber(Start, End);
546 assert(*Start == ']' && "Bad plural expression syntax: expected )");
547 ++Start;
548 return Low <= Val && Val <= High;
549}
550
551/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000552static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000553 // Empty condition?
554 if (*Start == ':')
555 return true;
556
557 while (1) {
558 char C = *Start;
559 if (C == '%') {
560 // Modulo expression
561 ++Start;
562 unsigned Arg = PluralNumber(Start, End);
563 assert(*Start == '=' && "Bad plural expression syntax: expected =");
564 ++Start;
565 unsigned ValMod = ValNo % Arg;
566 if (TestPluralRange(ValMod, Start, End))
567 return true;
568 } else {
Sebastian Redle2065322008-11-27 07:28:14 +0000569 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redle4c452c2008-11-22 13:44:36 +0000570 "Bad plural expression syntax: unexpected character");
571 // Range expression
572 if (TestPluralRange(ValNo, Start, End))
573 return true;
574 }
575
576 // Scan for next or-expr part.
577 Start = std::find(Start, End, ',');
Mike Stump1eb44332009-09-09 15:08:12 +0000578 if (Start == End)
Sebastian Redle4c452c2008-11-22 13:44:36 +0000579 break;
580 ++Start;
581 }
582 return false;
583}
584
585/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
586/// for complex plural forms, or in languages where all plurals are complex.
587/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
588/// conditions that are tested in order, the form corresponding to the first
589/// that applies being emitted. The empty condition is always true, making the
590/// last form a default case.
591/// Conditions are simple boolean expressions, where n is the number argument.
592/// Here are the rules.
593/// condition := expression | empty
594/// empty := -> always true
595/// expression := numeric [',' expression] -> logical or
596/// numeric := range -> true if n in range
597/// | '%' number '=' range -> true if n % number in range
598/// range := number
599/// | '[' number ',' number ']' -> ranges are inclusive both ends
600///
601/// Here are some examples from the GNU gettext manual written in this form:
602/// English:
603/// {1:form0|:form1}
604/// Latvian:
605/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
606/// Gaeilge:
607/// {1:form0|2:form1|:form2}
608/// Romanian:
609/// {1:form0|0,%100=[1,19]:form1|:form2}
610/// Lithuanian:
611/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
612/// Russian (requires repeated form):
613/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
614/// Slovak
615/// {1:form0|[2,4]:form1|:form2}
616/// Polish (requires repeated form):
617/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
David Blaikie40847cf2011-09-26 01:18:08 +0000618static void HandlePluralModifier(const Diagnostic &DInfo, unsigned ValNo,
Sebastian Redle4c452c2008-11-22 13:44:36 +0000619 const char *Argument, unsigned ArgumentLen,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000620 SmallVectorImpl<char> &OutStr) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000621 const char *ArgumentEnd = Argument + ArgumentLen;
622 while (1) {
623 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
624 const char *ExprEnd = Argument;
625 while (*ExprEnd != ':') {
626 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
627 ++ExprEnd;
628 }
629 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
630 Argument = ExprEnd + 1;
John McCall909c1822010-01-14 20:11:39 +0000631 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
John McCalle53a44b2010-10-14 01:55:31 +0000632
633 // Recursively format the result of the plural clause into the
634 // output string.
635 DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000636 return;
637 }
John McCall909c1822010-01-14 20:11:39 +0000638 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
Sebastian Redle4c452c2008-11-22 13:44:36 +0000639 }
640}
641
642
Chris Lattnerf4c83962008-11-19 06:51:40 +0000643/// FormatDiagnostic - Format this diagnostic into a string, substituting the
644/// formal arguments into the %0 slots. The result is appended onto the Str
645/// array.
David Blaikie40847cf2011-09-26 01:18:08 +0000646void Diagnostic::
Chris Lattner5f9e2722011-07-23 10:55:15 +0000647FormatDiagnostic(SmallVectorImpl<char> &OutStr) const {
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000648 if (!StoredDiagMessage.empty()) {
649 OutStr.append(StoredDiagMessage.begin(), StoredDiagMessage.end());
650 return;
651 }
652
Chris Lattner5f9e2722011-07-23 10:55:15 +0000653 StringRef Diag =
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000654 getDiags()->getDiagnosticIDs()->getDescription(getID());
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000656 FormatDiagnostic(Diag.begin(), Diag.end(), OutStr);
John McCall9f286142010-01-13 23:58:20 +0000657}
658
David Blaikie40847cf2011-09-26 01:18:08 +0000659void Diagnostic::
John McCall9f286142010-01-13 23:58:20 +0000660FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000661 SmallVectorImpl<char> &OutStr) const {
John McCall9f286142010-01-13 23:58:20 +0000662
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000663 /// FormattedArgs - Keep track of all of the arguments formatted by
664 /// ConvertArgToString and pass them into subsequent calls to
665 /// ConvertArgToString, allowing the implementation to avoid redundancies in
666 /// obvious cases.
David Blaikied6471f72011-09-25 23:23:43 +0000667 SmallVector<DiagnosticsEngine::ArgumentValue, 8> FormattedArgs;
Chandler Carruth0673cb32011-07-11 17:49:21 +0000668
669 /// QualTypeVals - Pass a vector of arrays so that QualType names can be
670 /// compared to see if more information is needed to be printed.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000671 SmallVector<intptr_t, 2> QualTypeVals;
Richard Trieu246b6aa2012-06-26 18:18:47 +0000672 SmallVector<char, 64> Tree;
673
Chandler Carruth0673cb32011-07-11 17:49:21 +0000674 for (unsigned i = 0, e = getNumArgs(); i < e; ++i)
David Blaikied6471f72011-09-25 23:23:43 +0000675 if (getArgKind(i) == DiagnosticsEngine::ak_qualtype)
Chandler Carruth0673cb32011-07-11 17:49:21 +0000676 QualTypeVals.push_back(getRawArg(i));
677
Chris Lattnerf4c83962008-11-19 06:51:40 +0000678 while (DiagStr != DiagEnd) {
679 if (DiagStr[0] != '%') {
680 // Append non-%0 substrings to Str if we have one.
681 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
682 OutStr.append(DiagStr, StrEnd);
683 DiagStr = StrEnd;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000684 continue;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000685 } else if (isPunctuation(DiagStr[1])) {
John McCall909c1822010-01-14 20:11:39 +0000686 OutStr.push_back(DiagStr[1]); // %% -> %.
Chris Lattnerf4c83962008-11-19 06:51:40 +0000687 DiagStr += 2;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000688 continue;
689 }
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000691 // Skip the %.
692 ++DiagStr;
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000694 // This must be a placeholder for a diagnostic argument. The format for a
695 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
696 // The digit is a number from 0-9 indicating which argument this comes from.
697 // The modifier is a string of digits from the set [-a-z]+, arguments is a
698 // brace enclosed string.
699 const char *Modifier = 0, *Argument = 0;
700 unsigned ModifierLen = 0, ArgumentLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000701
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000702 // Check to see if we have a modifier. If so eat it.
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000703 if (!isDigit(DiagStr[0])) {
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000704 Modifier = DiagStr;
705 while (DiagStr[0] == '-' ||
706 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
707 ++DiagStr;
708 ModifierLen = DiagStr-Modifier;
Chris Lattnerf4c83962008-11-19 06:51:40 +0000709
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000710 // If we have an argument, get it next.
711 if (DiagStr[0] == '{') {
712 ++DiagStr; // Skip {.
713 Argument = DiagStr;
Mike Stump1eb44332009-09-09 15:08:12 +0000714
John McCall909c1822010-01-14 20:11:39 +0000715 DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
716 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000717 ArgumentLen = DiagStr-Argument;
718 ++DiagStr; // Skip }.
Chris Lattnerf4c83962008-11-19 06:51:40 +0000719 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000720 }
Mike Stump1eb44332009-09-09 15:08:12 +0000721
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000722 assert(isDigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner22caddc2008-11-23 09:13:29 +0000723 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000724
Richard Trieu246b6aa2012-06-26 18:18:47 +0000725 // Only used for type diffing.
726 unsigned ArgNo2 = ArgNo;
727
David Blaikied6471f72011-09-25 23:23:43 +0000728 DiagnosticsEngine::ArgumentKind Kind = getArgKind(ArgNo);
Richard Trieu877761c2013-01-30 20:04:31 +0000729 if (ModifierIs(Modifier, ModifierLen, "diff")) {
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000730 assert(*DiagStr == ',' && isDigit(*(DiagStr + 1)) &&
Richard Trieu246b6aa2012-06-26 18:18:47 +0000731 "Invalid format for diff modifier");
732 ++DiagStr; // Comma.
733 ArgNo2 = *DiagStr++ - '0';
Richard Trieu877761c2013-01-30 20:04:31 +0000734 DiagnosticsEngine::ArgumentKind Kind2 = getArgKind(ArgNo2);
735 if (Kind == DiagnosticsEngine::ak_qualtype &&
736 Kind2 == DiagnosticsEngine::ak_qualtype)
737 Kind = DiagnosticsEngine::ak_qualtype_pair;
738 else {
739 // %diff only supports QualTypes. For other kinds of arguments,
740 // use the default printing. For example, if the modifier is:
741 // "%diff{compare $ to $|other text}1,2"
742 // treat it as:
743 // "compare %1 to %2"
744 const char *Pipe = ScanFormat(Argument, Argument + ArgumentLen, '|');
745 const char *FirstDollar = ScanFormat(Argument, Pipe, '$');
746 const char *SecondDollar = ScanFormat(FirstDollar + 1, Pipe, '$');
Filipe Cabecinhas566f0632013-01-30 22:03:24 +0000747 const char ArgStr1[] = { '%', static_cast<char>('0' + ArgNo) };
748 const char ArgStr2[] = { '%', static_cast<char>('0' + ArgNo2) };
Richard Trieu877761c2013-01-30 20:04:31 +0000749 FormatDiagnostic(Argument, FirstDollar, OutStr);
750 FormatDiagnostic(ArgStr1, ArgStr1 + 2, OutStr);
751 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
752 FormatDiagnostic(ArgStr2, ArgStr2 + 2, OutStr);
753 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
754 continue;
755 }
Richard Trieu246b6aa2012-06-26 18:18:47 +0000756 }
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000757
758 switch (Kind) {
Chris Lattner08631c52008-11-23 21:45:46 +0000759 // ---- STRINGS ----
David Blaikied6471f72011-09-25 23:23:43 +0000760 case DiagnosticsEngine::ak_std_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000761 const std::string &S = getArgStdStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000762 assert(ModifierLen == 0 && "No modifiers for strings yet");
763 OutStr.append(S.begin(), S.end());
764 break;
765 }
David Blaikied6471f72011-09-25 23:23:43 +0000766 case DiagnosticsEngine::ak_c_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000767 const char *S = getArgCStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000768 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbare46e3542009-04-20 06:13:16 +0000769
770 // Don't crash if get passed a null pointer by accident.
771 if (!S)
772 S = "(null)";
Mike Stump1eb44332009-09-09 15:08:12 +0000773
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000774 OutStr.append(S, S + strlen(S));
775 break;
776 }
Chris Lattner08631c52008-11-23 21:45:46 +0000777 // ---- INTEGERS ----
David Blaikied6471f72011-09-25 23:23:43 +0000778 case DiagnosticsEngine::ak_sint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000779 int Val = getArgSInt(ArgNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000780
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000781 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCalle53a44b2010-10-14 01:55:31 +0000782 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen,
783 OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000784 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
785 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000786 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCalle53a44b2010-10-14 01:55:31 +0000787 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
788 OutStr);
John McCall3be16b72010-01-14 00:50:32 +0000789 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
790 HandleOrdinalModifier((unsigned)Val, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000791 } else {
792 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbar23e47c62009-10-17 18:12:14 +0000793 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner30bc9652008-11-19 07:22:31 +0000794 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000795 break;
796 }
David Blaikied6471f72011-09-25 23:23:43 +0000797 case DiagnosticsEngine::ak_uint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000798 unsigned Val = getArgUInt(ArgNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000800 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCall9f286142010-01-13 23:58:20 +0000801 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000802 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
803 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000804 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCalle53a44b2010-10-14 01:55:31 +0000805 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
806 OutStr);
John McCall3be16b72010-01-14 00:50:32 +0000807 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
808 HandleOrdinalModifier(Val, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000809 } else {
810 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbar23e47c62009-10-17 18:12:14 +0000811 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner30bc9652008-11-19 07:22:31 +0000812 }
Chris Lattner22caddc2008-11-23 09:13:29 +0000813 break;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000814 }
Chris Lattner08631c52008-11-23 21:45:46 +0000815 // ---- NAMES and TYPES ----
David Blaikied6471f72011-09-25 23:23:43 +0000816 case DiagnosticsEngine::ak_identifierinfo: {
Chris Lattner08631c52008-11-23 21:45:46 +0000817 const IdentifierInfo *II = getArgIdentifier(ArgNo);
818 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbare46e3542009-04-20 06:13:16 +0000819
820 // Don't crash if get passed a null pointer by accident.
821 if (!II) {
822 const char *S = "(null)";
823 OutStr.append(S, S + strlen(S));
824 continue;
825 }
826
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000827 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
Chris Lattner08631c52008-11-23 21:45:46 +0000828 break;
829 }
David Blaikied6471f72011-09-25 23:23:43 +0000830 case DiagnosticsEngine::ak_qualtype:
831 case DiagnosticsEngine::ak_declarationname:
832 case DiagnosticsEngine::ak_nameddecl:
833 case DiagnosticsEngine::ak_nestednamespec:
834 case DiagnosticsEngine::ak_declcontext:
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000835 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000836 Modifier, ModifierLen,
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000837 Argument, ArgumentLen,
838 FormattedArgs.data(), FormattedArgs.size(),
Chandler Carruth0673cb32011-07-11 17:49:21 +0000839 OutStr, QualTypeVals);
Chris Lattner22caddc2008-11-23 09:13:29 +0000840 break;
Richard Trieu246b6aa2012-06-26 18:18:47 +0000841 case DiagnosticsEngine::ak_qualtype_pair:
842 // Create a struct with all the info needed for printing.
843 TemplateDiffTypes TDT;
844 TDT.FromType = getRawArg(ArgNo);
845 TDT.ToType = getRawArg(ArgNo2);
846 TDT.ElideType = getDiags()->ElideType;
847 TDT.ShowColors = getDiags()->ShowColors;
Richard Trieu5409d282012-07-10 01:46:04 +0000848 TDT.TemplateDiffUsed = false;
Richard Trieu246b6aa2012-06-26 18:18:47 +0000849 intptr_t val = reinterpret_cast<intptr_t>(&TDT);
850
Richard Trieu529cdf42012-06-29 21:12:16 +0000851 const char *ArgumentEnd = Argument + ArgumentLen;
852 const char *Pipe = ScanFormat(Argument, ArgumentEnd, '|');
853
Richard Trieu55619772012-07-13 21:18:32 +0000854 // Print the tree. If this diagnostic already has a tree, skip the
855 // second tree.
856 if (getDiags()->PrintTemplateTree && Tree.empty()) {
Richard Trieu246b6aa2012-06-26 18:18:47 +0000857 TDT.PrintFromType = true;
858 TDT.PrintTree = true;
859 getDiags()->ConvertArgToString(Kind, val,
860 Modifier, ModifierLen,
861 Argument, ArgumentLen,
862 FormattedArgs.data(),
863 FormattedArgs.size(),
864 Tree, QualTypeVals);
865 // If there is no tree information, fall back to regular printing.
Richard Trieu529cdf42012-06-29 21:12:16 +0000866 if (!Tree.empty()) {
867 FormatDiagnostic(Pipe + 1, ArgumentEnd, OutStr);
Richard Trieu246b6aa2012-06-26 18:18:47 +0000868 break;
Richard Trieu529cdf42012-06-29 21:12:16 +0000869 }
Richard Trieu246b6aa2012-06-26 18:18:47 +0000870 }
871
872 // Non-tree printing, also the fall-back when tree printing fails.
873 // The fall-back is triggered when the types compared are not templates.
Richard Trieu529cdf42012-06-29 21:12:16 +0000874 const char *FirstDollar = ScanFormat(Argument, ArgumentEnd, '$');
875 const char *SecondDollar = ScanFormat(FirstDollar + 1, ArgumentEnd, '$');
Richard Trieu246b6aa2012-06-26 18:18:47 +0000876
877 // Append before text
Richard Trieu529cdf42012-06-29 21:12:16 +0000878 FormatDiagnostic(Argument, FirstDollar, OutStr);
Richard Trieu246b6aa2012-06-26 18:18:47 +0000879
880 // Append first type
881 TDT.PrintTree = false;
882 TDT.PrintFromType = true;
883 getDiags()->ConvertArgToString(Kind, val,
884 Modifier, ModifierLen,
885 Argument, ArgumentLen,
886 FormattedArgs.data(), FormattedArgs.size(),
887 OutStr, QualTypeVals);
Richard Trieu5409d282012-07-10 01:46:04 +0000888 if (!TDT.TemplateDiffUsed)
889 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
890 TDT.FromType));
891
Richard Trieu246b6aa2012-06-26 18:18:47 +0000892 // Append middle text
Richard Trieu529cdf42012-06-29 21:12:16 +0000893 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
Richard Trieu246b6aa2012-06-26 18:18:47 +0000894
895 // Append second type
896 TDT.PrintFromType = false;
897 getDiags()->ConvertArgToString(Kind, val,
898 Modifier, ModifierLen,
899 Argument, ArgumentLen,
900 FormattedArgs.data(), FormattedArgs.size(),
901 OutStr, QualTypeVals);
Richard Trieu5409d282012-07-10 01:46:04 +0000902 if (!TDT.TemplateDiffUsed)
903 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
904 TDT.ToType));
905
Richard Trieu246b6aa2012-06-26 18:18:47 +0000906 // Append end text
Richard Trieu529cdf42012-06-29 21:12:16 +0000907 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
Richard Trieu246b6aa2012-06-26 18:18:47 +0000908 break;
Nico Weber7bfaaae2008-08-10 19:59:06 +0000909 }
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000910
911 // Remember this argument info for subsequent formatting operations. Turn
912 // std::strings into a null terminated string to make it be the same case as
913 // all the other ones.
Richard Trieu246b6aa2012-06-26 18:18:47 +0000914 if (Kind == DiagnosticsEngine::ak_qualtype_pair)
915 continue;
916 else if (Kind != DiagnosticsEngine::ak_std_string)
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000917 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
918 else
David Blaikied6471f72011-09-25 23:23:43 +0000919 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_c_string,
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000920 (intptr_t)getArgStdStr(ArgNo).c_str()));
921
Nico Weber7bfaaae2008-08-10 19:59:06 +0000922 }
Richard Trieu246b6aa2012-06-26 18:18:47 +0000923
924 // Append the type tree to the end of the diagnostics.
925 OutStr.append(Tree.begin(), Tree.end());
Nico Weber7bfaaae2008-08-10 19:59:06 +0000926}
Ted Kremenekcabe6682009-01-23 20:28:53 +0000927
Douglas Gregora88084b2010-02-18 18:08:43 +0000928StoredDiagnostic::StoredDiagnostic() { }
929
David Blaikied6471f72011-09-25 23:23:43 +0000930StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000931 StringRef Message)
Benjamin Kramera6a32e22010-11-19 17:36:51 +0000932 : ID(ID), Level(Level), Loc(), Message(Message) { }
Douglas Gregora88084b2010-02-18 18:08:43 +0000933
David Blaikied6471f72011-09-25 23:23:43 +0000934StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level,
David Blaikie40847cf2011-09-26 01:18:08 +0000935 const Diagnostic &Info)
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000936 : ID(Info.getID()), Level(Level)
937{
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000938 assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
939 "Valid source location without setting a source manager for diagnostic");
940 if (Info.getLocation().isValid())
941 Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000942 SmallString<64> Message;
Douglas Gregora88084b2010-02-18 18:08:43 +0000943 Info.FormatDiagnostic(Message);
944 this->Message.assign(Message.begin(), Message.end());
945
946 Ranges.reserve(Info.getNumRanges());
947 for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I)
948 Ranges.push_back(Info.getRange(I));
949
Douglas Gregor849b2432010-03-31 17:46:05 +0000950 FixIts.reserve(Info.getNumFixItHints());
951 for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I)
952 FixIts.push_back(Info.getFixItHint(I));
Douglas Gregora88084b2010-02-18 18:08:43 +0000953}
954
David Blaikied6471f72011-09-25 23:23:43 +0000955StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000956 StringRef Message, FullSourceLoc Loc,
Chris Lattner2d3ba4f2011-07-23 17:14:25 +0000957 ArrayRef<CharSourceRange> Ranges,
Aaron Ballmanf9067c62013-02-24 19:08:10 +0000958 ArrayRef<FixItHint> FixIts)
959 : ID(ID), Level(Level), Loc(Loc), Message(Message),
960 Ranges(Ranges.begin(), Ranges.end()), FixIts(FixIts.begin(), FixIts.end())
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000961{
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000962}
963
Douglas Gregora88084b2010-02-18 18:08:43 +0000964StoredDiagnostic::~StoredDiagnostic() { }
965
Ted Kremenekcabe6682009-01-23 20:28:53 +0000966/// IncludeInDiagnosticCounts - This method (whose default implementation
967/// returns true) indicates whether the diagnostics handled by this
David Blaikie78ad0b92011-09-25 23:39:51 +0000968/// DiagnosticConsumer should be included in the number of diagnostics
David Blaikied6471f72011-09-25 23:23:43 +0000969/// reported by DiagnosticsEngine.
David Blaikie78ad0b92011-09-25 23:39:51 +0000970bool DiagnosticConsumer::IncludeInDiagnosticCounts() const { return true; }
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000971
David Blaikie99ba9e32011-12-20 02:48:34 +0000972void IgnoringDiagConsumer::anchor() { }
973
Douglas Gregora4a90ca2013-05-03 22:58:43 +0000974ForwardingDiagnosticConsumer::~ForwardingDiagnosticConsumer() {}
975
976void ForwardingDiagnosticConsumer::HandleDiagnostic(
977 DiagnosticsEngine::Level DiagLevel,
978 const Diagnostic &Info) {
979 Target.HandleDiagnostic(DiagLevel, Info);
980}
981
982void ForwardingDiagnosticConsumer::clear() {
983 DiagnosticConsumer::clear();
984 Target.clear();
985}
986
987bool ForwardingDiagnosticConsumer::IncludeInDiagnosticCounts() const {
988 return Target.IncludeInDiagnosticCounts();
989}
990
Benjamin Kramerd7a3e2c2012-02-07 22:29:24 +0000991PartialDiagnostic::StorageAllocator::StorageAllocator() {
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000992 for (unsigned I = 0; I != NumCached; ++I)
993 FreeList[I] = Cached + I;
994 NumFreeListEntries = NumCached;
995}
996
Benjamin Kramerd7a3e2c2012-02-07 22:29:24 +0000997PartialDiagnostic::StorageAllocator::~StorageAllocator() {
Chad Rosierdfaee492012-02-07 23:24:49 +0000998 // Don't assert if we are in a CrashRecovery context, as this invariant may
999 // be invalidated during a crash.
1000 assert((NumFreeListEntries == NumCached ||
1001 llvm::CrashRecoveryContext::isRecoveringFromCrash()) &&
1002 "A partial is on the lamb");
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00001003}