blob: 2c49c2f7606b97a76968d1f8e4959cff34146e65 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Diagnostic.cpp - C Language Family Diagnostic Handling -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Diagnostic-related interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Ted Kremenekec55c942010-04-12 19:54:17 +000014#include "clang/Basic/Diagnostic.h"
Chris Lattner43b628c2008-11-19 07:32:16 +000015#include "clang/Basic/IdentifierTable.h"
Ted Kremenekec55c942010-04-12 19:54:17 +000016#include "clang/Basic/PartialDiagnostic.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000017#include "llvm/ADT/SmallString.h"
Daniel Dunbar23e47c62009-10-17 18:12:14 +000018#include "llvm/Support/raw_ostream.h"
Ted Kremenek03201fb2011-03-21 18:40:07 +000019#include "llvm/Support/CrashRecoveryContext.h"
Joerg Sonnenberger7094dee2012-08-10 10:58:18 +000020#include <cctype>
Ted Kremenek03201fb2011-03-21 18:40:07 +000021
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
David Blaikied6471f72011-09-25 23:23:43 +000024static void DummyArgToStringFn(DiagnosticsEngine::ArgumentKind AK, intptr_t QT,
Chris Lattner3fdf4b02008-11-23 09:21:17 +000025 const char *Modifier, unsigned ML,
26 const char *Argument, unsigned ArgLen,
David Blaikied6471f72011-09-25 23:23:43 +000027 const DiagnosticsEngine::ArgumentValue *PrevArgs,
Chris Lattnerb54d8af2009-10-20 05:25:22 +000028 unsigned NumPrevArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +000029 SmallVectorImpl<char> &Output,
Chandler Carruth0673cb32011-07-11 17:49:21 +000030 void *Cookie,
Bill Wendling341785e2012-02-22 09:51:33 +000031 ArrayRef<intptr_t> QualTypeVals) {
Chris Lattner3fdf4b02008-11-23 09:21:17 +000032 const char *Str = "<can't format argument>";
Chris Lattner22caddc2008-11-23 09:13:29 +000033 Output.append(Str, Str+strlen(Str));
34}
35
36
David Blaikied6471f72011-09-25 23:23:43 +000037DiagnosticsEngine::DiagnosticsEngine(
Dylan Noblesmithc93dc782012-02-20 14:00:23 +000038 const IntrusiveRefCntPtr<DiagnosticIDs> &diags,
David Blaikie78ad0b92011-09-25 23:39:51 +000039 DiagnosticConsumer *client, bool ShouldOwnClient)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000040 : Diags(diags), Client(client), OwnsDiagClient(ShouldOwnClient),
41 SourceMgr(0) {
Chris Lattner3fdf4b02008-11-23 09:21:17 +000042 ArgToStringFn = DummyArgToStringFn;
Chris Lattner92dd3862009-02-19 23:53:20 +000043 ArgToStringCookie = 0;
Mike Stump1eb44332009-09-09 15:08:12 +000044
Douglas Gregorcc5888d2010-07-31 00:40:00 +000045 AllExtensionsSilenced = 0;
46 IgnoreAllWarnings = false;
47 WarningsAsErrors = false;
Ted Kremenek1e473cc2011-08-18 01:12:56 +000048 EnableAllWarnings = false;
Douglas Gregorcc5888d2010-07-31 00:40:00 +000049 ErrorsAsFatal = false;
50 SuppressSystemWarnings = false;
51 SuppressAllDiagnostics = false;
Richard Trieu246b6aa2012-06-26 18:18:47 +000052 ElideType = true;
53 PrintTemplateTree = false;
54 ShowColors = false;
Douglas Gregorcc5888d2010-07-31 00:40:00 +000055 ShowOverloads = Ovl_All;
56 ExtBehavior = Ext_Ignore;
57
58 ErrorLimit = 0;
59 TemplateBacktraceLimit = 0;
Richard Smith08d6e032011-12-16 19:06:07 +000060 ConstexprBacktraceLimit = 0;
Douglas Gregorcc5888d2010-07-31 00:40:00 +000061
Douglas Gregorabc563f2010-07-19 21:46:24 +000062 Reset();
Reid Spencer5f016e22007-07-11 17:01:13 +000063}
64
David Blaikied6471f72011-09-25 23:23:43 +000065DiagnosticsEngine::~DiagnosticsEngine() {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000066 if (OwnsDiagClient)
67 delete Client;
Chris Lattner182745a2007-12-02 01:09:57 +000068}
69
David Blaikie78ad0b92011-09-25 23:39:51 +000070void DiagnosticsEngine::setClient(DiagnosticConsumer *client,
David Blaikied6471f72011-09-25 23:23:43 +000071 bool ShouldOwnClient) {
Douglas Gregor4f5e21e2011-01-31 22:04:05 +000072 if (OwnsDiagClient && Client)
73 delete Client;
74
75 Client = client;
76 OwnsDiagClient = ShouldOwnClient;
77}
Chris Lattner04ae2df2009-07-12 21:18:45 +000078
David Blaikied6471f72011-09-25 23:23:43 +000079void DiagnosticsEngine::pushMappings(SourceLocation Loc) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +000080 DiagStateOnPushStack.push_back(GetCurDiagState());
Chris Lattner04ae2df2009-07-12 21:18:45 +000081}
82
David Blaikied6471f72011-09-25 23:23:43 +000083bool DiagnosticsEngine::popMappings(SourceLocation Loc) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +000084 if (DiagStateOnPushStack.empty())
Chris Lattner04ae2df2009-07-12 21:18:45 +000085 return false;
86
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +000087 if (DiagStateOnPushStack.back() != GetCurDiagState()) {
88 // State changed at some point between push/pop.
89 PushDiagStatePoint(DiagStateOnPushStack.back(), Loc);
90 }
91 DiagStateOnPushStack.pop_back();
Chris Lattner04ae2df2009-07-12 21:18:45 +000092 return true;
93}
94
David Blaikied6471f72011-09-25 23:23:43 +000095void DiagnosticsEngine::Reset() {
Douglas Gregorabc563f2010-07-19 21:46:24 +000096 ErrorOccurred = false;
97 FatalErrorOccurred = false;
Douglas Gregor85bea972011-07-06 17:40:26 +000098 UnrecoverableErrorOccurred = false;
Douglas Gregorabc563f2010-07-19 21:46:24 +000099
100 NumWarnings = 0;
101 NumErrors = 0;
102 NumErrorsSuppressed = 0;
Argyrios Kyrtzidisc0a575f2011-07-29 01:25:44 +0000103 TrapNumErrorsOccurred = 0;
104 TrapNumUnrecoverableErrorsOccurred = 0;
Douglas Gregor85bea972011-07-06 17:40:26 +0000105
Douglas Gregorabc563f2010-07-19 21:46:24 +0000106 CurDiagID = ~0U;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000107 // Set LastDiagLevel to an "unset" state. If we set it to 'Ignored', notes
David Blaikied6471f72011-09-25 23:23:43 +0000108 // using a DiagnosticsEngine associated to a translation unit that follow
109 // diagnostics from a DiagnosticsEngine associated to anoter t.u. will not be
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000110 // displayed.
111 LastDiagLevel = (DiagnosticIDs::Level)-1;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000112 DelayedDiagID = 0;
Argyrios Kyrtzidisdc0a2da2011-03-26 18:58:17 +0000113
114 // Clear state related to #pragma diagnostic.
115 DiagStates.clear();
116 DiagStatePoints.clear();
117 DiagStateOnPushStack.clear();
118
119 // Create a DiagState and DiagStatePoint representing diagnostic changes
120 // through command-line.
121 DiagStates.push_back(DiagState());
Richard Smith00aae522012-08-14 04:19:29 +0000122 DiagStatePoints.push_back(DiagStatePoint(&DiagStates.back(), FullSourceLoc()));
Douglas Gregorabc563f2010-07-19 21:46:24 +0000123}
Reid Spencer5f016e22007-07-11 17:01:13 +0000124
David Blaikied6471f72011-09-25 23:23:43 +0000125void DiagnosticsEngine::SetDelayedDiagnostic(unsigned DiagID, StringRef Arg1,
Chad Rosierdfaee492012-02-07 23:24:49 +0000126 StringRef Arg2) {
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000127 if (DelayedDiagID)
128 return;
129
130 DelayedDiagID = DiagID;
Douglas Gregor9e2dac92010-03-22 15:47:45 +0000131 DelayedDiagArg1 = Arg1.str();
132 DelayedDiagArg2 = Arg2.str();
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000133}
134
David Blaikied6471f72011-09-25 23:23:43 +0000135void DiagnosticsEngine::ReportDelayed() {
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000136 Report(DelayedDiagID) << DelayedDiagArg1 << DelayedDiagArg2;
137 DelayedDiagID = 0;
138 DelayedDiagArg1.clear();
139 DelayedDiagArg2.clear();
140}
141
David Blaikied6471f72011-09-25 23:23:43 +0000142DiagnosticsEngine::DiagStatePointsTy::iterator
143DiagnosticsEngine::GetDiagStatePointForLoc(SourceLocation L) const {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000144 assert(!DiagStatePoints.empty());
145 assert(DiagStatePoints.front().Loc.isInvalid() &&
146 "Should have created a DiagStatePoint for command-line");
147
148 FullSourceLoc Loc(L, *SourceMgr);
149 if (Loc.isInvalid())
150 return DiagStatePoints.end() - 1;
151
152 DiagStatePointsTy::iterator Pos = DiagStatePoints.end();
153 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
154 if (LastStateChangePos.isValid() &&
155 Loc.isBeforeInTranslationUnitThan(LastStateChangePos))
156 Pos = std::upper_bound(DiagStatePoints.begin(), DiagStatePoints.end(),
157 DiagStatePoint(0, Loc));
158 --Pos;
159 return Pos;
160}
161
David Blaikied6471f72011-09-25 23:23:43 +0000162void DiagnosticsEngine::setDiagnosticMapping(diag::kind Diag, diag::Mapping Map,
Chad Rosierdfaee492012-02-07 23:24:49 +0000163 SourceLocation L) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000164 assert(Diag < diag::DIAG_UPPER_LIMIT &&
165 "Can only map builtin diagnostics");
166 assert((Diags->isBuiltinWarningOrExtension(Diag) ||
167 (Map == diag::MAP_FATAL || Map == diag::MAP_ERROR)) &&
168 "Cannot map errors into warnings!");
169 assert(!DiagStatePoints.empty());
Richard Smithc95ad002012-08-14 22:37:22 +0000170 assert((L.isInvalid() || SourceMgr) && "No SourceMgr for valid location");
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000171
Richard Smithc95ad002012-08-14 22:37:22 +0000172 FullSourceLoc Loc = SourceMgr? FullSourceLoc(L, *SourceMgr) : FullSourceLoc();
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000173 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
Chad Rosier7a0a31c2012-02-03 01:49:51 +0000174 // Don't allow a mapping to a warning override an error/fatal mapping.
175 if (Map == diag::MAP_WARNING) {
176 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(Diag);
177 if (Info.getMapping() == diag::MAP_ERROR ||
178 Info.getMapping() == diag::MAP_FATAL)
179 Map = Info.getMapping();
180 }
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +0000181 DiagnosticMappingInfo MappingInfo = makeMappingInfo(Map, L);
Daniel Dunbar53201a82011-10-04 21:17:24 +0000182
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000183 // Common case; setting all the diagnostics of a group in one place.
184 if (Loc.isInvalid() || Loc == LastStateChangePos) {
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000185 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000186 return;
187 }
188
189 // Another common case; modifying diagnostic state in a source location
190 // after the previous one.
191 if ((Loc.isValid() && LastStateChangePos.isInvalid()) ||
192 LastStateChangePos.isBeforeInTranslationUnitThan(Loc)) {
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000193 // A diagnostic pragma occurred, create a new DiagState initialized with
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000194 // the current one and a new DiagStatePoint to record at which location
195 // the new state became active.
196 DiagStates.push_back(*GetCurDiagState());
197 PushDiagStatePoint(&DiagStates.back(), Loc);
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000198 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000199 return;
200 }
201
202 // We allow setting the diagnostic state in random source order for
203 // completeness but it should not be actually happening in normal practice.
204
205 DiagStatePointsTy::iterator Pos = GetDiagStatePointForLoc(Loc);
206 assert(Pos != DiagStatePoints.end());
207
208 // Update all diagnostic states that are active after the given location.
209 for (DiagStatePointsTy::iterator
210 I = Pos+1, E = DiagStatePoints.end(); I != E; ++I) {
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000211 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000212 }
213
214 // If the location corresponds to an existing point, just update its state.
215 if (Pos->Loc == Loc) {
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000216 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000217 return;
218 }
219
220 // Create a new state/point and fit it into the vector of DiagStatePoints
221 // so that the vector is always ordered according to location.
222 Pos->Loc.isBeforeInTranslationUnitThan(Loc);
223 DiagStates.push_back(*Pos->State);
224 DiagState *NewState = &DiagStates.back();
Daniel Dunbar09ea68d2011-09-29 01:34:47 +0000225 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000226 DiagStatePoints.insert(Pos+1, DiagStatePoint(NewState,
227 FullSourceLoc(Loc, *SourceMgr)));
228}
229
Daniel Dunbar3f839462011-09-29 01:47:16 +0000230bool DiagnosticsEngine::setDiagnosticGroupMapping(
231 StringRef Group, diag::Mapping Map, SourceLocation Loc)
232{
233 // Get the diagnostics in this group.
234 llvm::SmallVector<diag::kind, 8> GroupDiags;
235 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
236 return true;
237
238 // Set the mapping.
239 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i)
240 setDiagnosticMapping(GroupDiags[i], Map, Loc);
241
242 return false;
243}
244
Chad Rosier3f225092012-02-07 19:55:45 +0000245void DiagnosticsEngine::setDiagnosticWarningAsError(diag::kind Diag,
246 bool Enabled) {
247 // If we are enabling this feature, just set the diagnostic mappings to map to
248 // errors.
249 if (Enabled)
250 setDiagnosticMapping(Diag, diag::MAP_ERROR, SourceLocation());
251
252 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
253 // potentially downgrade anything already mapped to be a warning.
254 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(Diag);
255
256 if (Info.getMapping() == diag::MAP_ERROR ||
257 Info.getMapping() == diag::MAP_FATAL)
258 Info.setMapping(diag::MAP_WARNING);
259
260 Info.setNoWarningAsError(true);
261}
262
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000263bool DiagnosticsEngine::setDiagnosticGroupWarningAsError(StringRef Group,
264 bool Enabled) {
Daniel Dunbara5e41332011-09-29 01:52:06 +0000265 // If we are enabling this feature, just set the diagnostic mappings to map to
266 // errors.
267 if (Enabled)
268 return setDiagnosticGroupMapping(Group, diag::MAP_ERROR);
269
270 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
271 // potentially downgrade anything already mapped to be a warning.
272
273 // Get the diagnostics in this group.
274 llvm::SmallVector<diag::kind, 8> GroupDiags;
275 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
276 return true;
277
278 // Perform the mapping change.
279 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i) {
280 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(
281 GroupDiags[i]);
282
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000283 if (Info.getMapping() == diag::MAP_ERROR ||
284 Info.getMapping() == diag::MAP_FATAL)
285 Info.setMapping(diag::MAP_WARNING);
286
Daniel Dunbara5e41332011-09-29 01:52:06 +0000287 Info.setNoWarningAsError(true);
288 }
289
290 return false;
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000291}
292
Chad Rosier3f225092012-02-07 19:55:45 +0000293void DiagnosticsEngine::setDiagnosticErrorAsFatal(diag::kind Diag,
294 bool Enabled) {
295 // If we are enabling this feature, just set the diagnostic mappings to map to
296 // errors.
297 if (Enabled)
298 setDiagnosticMapping(Diag, diag::MAP_FATAL, SourceLocation());
299
300 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
301 // potentially downgrade anything already mapped to be a warning.
302 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(Diag);
303
304 if (Info.getMapping() == diag::MAP_FATAL)
305 Info.setMapping(diag::MAP_ERROR);
306
307 Info.setNoErrorAsFatal(true);
308}
309
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000310bool DiagnosticsEngine::setDiagnosticGroupErrorAsFatal(StringRef Group,
311 bool Enabled) {
Daniel Dunbara5e41332011-09-29 01:52:06 +0000312 // If we are enabling this feature, just set the diagnostic mappings to map to
313 // fatal errors.
314 if (Enabled)
315 return setDiagnosticGroupMapping(Group, diag::MAP_FATAL);
316
317 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
318 // potentially downgrade anything already mapped to be an error.
319
320 // Get the diagnostics in this group.
321 llvm::SmallVector<diag::kind, 8> GroupDiags;
322 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
323 return true;
324
325 // Perform the mapping change.
326 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i) {
327 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(
328 GroupDiags[i]);
329
Daniel Dunbarbe1aa412011-09-29 01:58:05 +0000330 if (Info.getMapping() == diag::MAP_FATAL)
331 Info.setMapping(diag::MAP_ERROR);
332
Daniel Dunbara5e41332011-09-29 01:52:06 +0000333 Info.setNoErrorAsFatal(true);
334 }
335
336 return false;
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000337}
338
Argyrios Kyrtzidis82e64112012-01-28 04:35:52 +0000339void DiagnosticsEngine::setMappingToAllDiagnostics(diag::Mapping Map,
Argyrios Kyrtzidis11583c72012-01-27 06:15:43 +0000340 SourceLocation Loc) {
341 // Get all the diagnostics.
342 llvm::SmallVector<diag::kind, 64> AllDiags;
343 Diags->getAllDiagnostics(AllDiags);
344
345 // Set the mapping.
346 for (unsigned i = 0, e = AllDiags.size(); i != e; ++i)
347 if (Diags->isBuiltinWarningOrExtension(AllDiags[i]))
348 setDiagnosticMapping(AllDiags[i], Map, Loc);
Argyrios Kyrtzidis11583c72012-01-27 06:15:43 +0000349}
350
David Blaikied6471f72011-09-25 23:23:43 +0000351void DiagnosticsEngine::Report(const StoredDiagnostic &storedDiag) {
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000352 assert(CurDiagID == ~0U && "Multiple diagnostics in flight at once!");
353
354 CurDiagLoc = storedDiag.getLocation();
355 CurDiagID = storedDiag.getID();
356 NumDiagArgs = 0;
357
358 NumDiagRanges = storedDiag.range_size();
Daniel Dunbar981e2792012-03-13 18:21:17 +0000359 assert(NumDiagRanges < DiagnosticsEngine::MaxRanges &&
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000360 "Too many arguments to diagnostic!");
361 unsigned i = 0;
362 for (StoredDiagnostic::range_iterator
363 RI = storedDiag.range_begin(),
364 RE = storedDiag.range_end(); RI != RE; ++RI)
365 DiagRanges[i++] = *RI;
366
Daniel Dunbar981e2792012-03-13 18:21:17 +0000367 assert(NumDiagRanges < DiagnosticsEngine::MaxFixItHints &&
368 "Too many arguments to diagnostic!");
369 NumDiagFixItHints = 0;
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000370 for (StoredDiagnostic::fixit_iterator
371 FI = storedDiag.fixit_begin(),
372 FE = storedDiag.fixit_end(); FI != FE; ++FI)
Daniel Dunbar981e2792012-03-13 18:21:17 +0000373 DiagFixItHints[NumDiagFixItHints++] = *FI;
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000374
David Blaikie78ad0b92011-09-25 23:39:51 +0000375 assert(Client && "DiagnosticConsumer not set!");
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000376 Level DiagLevel = storedDiag.getLevel();
David Blaikie40847cf2011-09-26 01:18:08 +0000377 Diagnostic Info(this, storedDiag.getMessage());
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000378 Client->HandleDiagnostic(DiagLevel, Info);
379 if (Client->IncludeInDiagnosticCounts()) {
David Blaikied6471f72011-09-25 23:23:43 +0000380 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000381 ++NumWarnings;
382 }
383
384 CurDiagID = ~0U;
385}
386
Jordan Rosec6d64a22012-07-11 16:50:36 +0000387bool DiagnosticsEngine::EmitCurrentDiagnostic(bool Force) {
388 assert(getClient() && "DiagnosticClient not set!");
389
390 bool Emitted;
391 if (Force) {
392 Diagnostic Info(this);
393
394 // Figure out the diagnostic level of this message.
395 DiagnosticIDs::Level DiagLevel
396 = Diags->getDiagnosticLevel(Info.getID(), Info.getLocation(), *this);
397
398 Emitted = (DiagLevel != DiagnosticIDs::Ignored);
399 if (Emitted) {
400 // Emit the diagnostic regardless of suppression level.
401 Diags->EmitDiag(*this, DiagLevel);
402 }
403 } else {
404 // Process the diagnostic, sending the accumulated information to the
405 // DiagnosticConsumer.
406 Emitted = ProcessDiag();
407 }
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000408
409 // Clear out the current diagnostic object.
Daniel Dunbar3054f092012-03-13 21:02:14 +0000410 unsigned DiagID = CurDiagID;
411 Clear();
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000412
413 // If there was a delayed diagnostic, emit it now.
Jordan Rosec6d64a22012-07-11 16:50:36 +0000414 if (!Force && DelayedDiagID && DelayedDiagID != DiagID)
Daniel Dunbar3054f092012-03-13 21:02:14 +0000415 ReportDelayed();
Douglas Gregor93ea5cb2010-03-22 15:10:57 +0000416
417 return Emitted;
418}
419
Nico Weber7bfaaae2008-08-10 19:59:06 +0000420
David Blaikie78ad0b92011-09-25 23:39:51 +0000421DiagnosticConsumer::~DiagnosticConsumer() {}
Nico Weber7bfaaae2008-08-10 19:59:06 +0000422
David Blaikie78ad0b92011-09-25 23:39:51 +0000423void DiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
David Blaikie40847cf2011-09-26 01:18:08 +0000424 const Diagnostic &Info) {
Argyrios Kyrtzidisf2224d82010-11-18 20:06:46 +0000425 if (!IncludeInDiagnosticCounts())
426 return;
427
David Blaikied6471f72011-09-25 23:23:43 +0000428 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidisf2224d82010-11-18 20:06:46 +0000429 ++NumWarnings;
David Blaikied6471f72011-09-25 23:23:43 +0000430 else if (DiagLevel >= DiagnosticsEngine::Error)
Argyrios Kyrtzidisf2224d82010-11-18 20:06:46 +0000431 ++NumErrors;
432}
Chris Lattnerf4c83962008-11-19 06:51:40 +0000433
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000434/// ModifierIs - Return true if the specified modifier matches specified string.
435template <std::size_t StrLen>
436static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
437 const char (&Str)[StrLen]) {
438 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
439}
440
John McCall909c1822010-01-14 20:11:39 +0000441/// ScanForward - Scans forward, looking for the given character, skipping
442/// nested clauses and escaped characters.
443static const char *ScanFormat(const char *I, const char *E, char Target) {
444 unsigned Depth = 0;
445
446 for ( ; I != E; ++I) {
447 if (Depth == 0 && *I == Target) return I;
448 if (Depth != 0 && *I == '}') Depth--;
449
450 if (*I == '%') {
451 I++;
452 if (I == E) break;
453
454 // Escaped characters get implicitly skipped here.
455
456 // Format specifier.
457 if (!isdigit(*I) && !ispunct(*I)) {
458 for (I++; I != E && !isdigit(*I) && *I != '{'; I++) ;
459 if (I == E) break;
460 if (*I == '{')
461 Depth++;
462 }
463 }
464 }
465 return E;
466}
467
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000468/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
469/// like this: %select{foo|bar|baz}2. This means that the integer argument
470/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
471/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
472/// This is very useful for certain classes of variant diagnostics.
David Blaikie40847cf2011-09-26 01:18:08 +0000473static void HandleSelectModifier(const Diagnostic &DInfo, unsigned ValNo,
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000474 const char *Argument, unsigned ArgumentLen,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000475 SmallVectorImpl<char> &OutStr) {
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000476 const char *ArgumentEnd = Argument+ArgumentLen;
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000478 // Skip over 'ValNo' |'s.
479 while (ValNo) {
John McCall909c1822010-01-14 20:11:39 +0000480 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000481 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
482 " larger than the number of options in the diagnostic string!");
483 Argument = NextVal+1; // Skip this string.
484 --ValNo;
485 }
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000487 // Get the end of the value. This is either the } or the |.
John McCall909c1822010-01-14 20:11:39 +0000488 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
John McCall9f286142010-01-13 23:58:20 +0000489
490 // Recursively format the result of the select clause into the output string.
491 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000492}
493
494/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
495/// letter 's' to the string if the value is not 1. This is used in cases like
496/// this: "you idiot, you have %4 parameter%s4!".
497static void HandleIntegerSModifier(unsigned ValNo,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000498 SmallVectorImpl<char> &OutStr) {
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000499 if (ValNo != 1)
500 OutStr.push_back('s');
501}
502
John McCall3be16b72010-01-14 00:50:32 +0000503/// HandleOrdinalModifier - Handle the integer 'ord' modifier. This
504/// prints the ordinal form of the given integer, with 1 corresponding
505/// to the first ordinal. Currently this is hard-coded to use the
506/// English form.
507static void HandleOrdinalModifier(unsigned ValNo,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000508 SmallVectorImpl<char> &OutStr) {
John McCall3be16b72010-01-14 00:50:32 +0000509 assert(ValNo != 0 && "ValNo must be strictly positive!");
510
511 llvm::raw_svector_ostream Out(OutStr);
512
513 // We could use text forms for the first N ordinals, but the numeric
514 // forms are actually nicer in diagnostics because they stand out.
515 Out << ValNo;
516
517 // It is critically important that we do this perfectly for
518 // user-written sequences with over 100 elements.
519 switch (ValNo % 100) {
520 case 11:
521 case 12:
522 case 13:
523 Out << "th"; return;
524 default:
525 switch (ValNo % 10) {
526 case 1: Out << "st"; return;
527 case 2: Out << "nd"; return;
528 case 3: Out << "rd"; return;
529 default: Out << "th"; return;
530 }
531 }
532}
533
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000534
Sebastian Redle4c452c2008-11-22 13:44:36 +0000535/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000536static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000537 // Programming 101: Parse a decimal number :-)
538 unsigned Val = 0;
539 while (Start != End && *Start >= '0' && *Start <= '9') {
540 Val *= 10;
541 Val += *Start - '0';
542 ++Start;
543 }
544 return Val;
545}
546
547/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000548static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000549 if (*Start != '[') {
550 unsigned Ref = PluralNumber(Start, End);
551 return Ref == Val;
552 }
553
554 ++Start;
555 unsigned Low = PluralNumber(Start, End);
556 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
557 ++Start;
558 unsigned High = PluralNumber(Start, End);
559 assert(*Start == ']' && "Bad plural expression syntax: expected )");
560 ++Start;
561 return Low <= Val && Val <= High;
562}
563
564/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattnerd2aa7c92009-04-15 17:13:42 +0000565static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000566 // Empty condition?
567 if (*Start == ':')
568 return true;
569
570 while (1) {
571 char C = *Start;
572 if (C == '%') {
573 // Modulo expression
574 ++Start;
575 unsigned Arg = PluralNumber(Start, End);
576 assert(*Start == '=' && "Bad plural expression syntax: expected =");
577 ++Start;
578 unsigned ValMod = ValNo % Arg;
579 if (TestPluralRange(ValMod, Start, End))
580 return true;
581 } else {
Sebastian Redle2065322008-11-27 07:28:14 +0000582 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redle4c452c2008-11-22 13:44:36 +0000583 "Bad plural expression syntax: unexpected character");
584 // Range expression
585 if (TestPluralRange(ValNo, Start, End))
586 return true;
587 }
588
589 // Scan for next or-expr part.
590 Start = std::find(Start, End, ',');
Mike Stump1eb44332009-09-09 15:08:12 +0000591 if (Start == End)
Sebastian Redle4c452c2008-11-22 13:44:36 +0000592 break;
593 ++Start;
594 }
595 return false;
596}
597
598/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
599/// for complex plural forms, or in languages where all plurals are complex.
600/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
601/// conditions that are tested in order, the form corresponding to the first
602/// that applies being emitted. The empty condition is always true, making the
603/// last form a default case.
604/// Conditions are simple boolean expressions, where n is the number argument.
605/// Here are the rules.
606/// condition := expression | empty
607/// empty := -> always true
608/// expression := numeric [',' expression] -> logical or
609/// numeric := range -> true if n in range
610/// | '%' number '=' range -> true if n % number in range
611/// range := number
612/// | '[' number ',' number ']' -> ranges are inclusive both ends
613///
614/// Here are some examples from the GNU gettext manual written in this form:
615/// English:
616/// {1:form0|:form1}
617/// Latvian:
618/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
619/// Gaeilge:
620/// {1:form0|2:form1|:form2}
621/// Romanian:
622/// {1:form0|0,%100=[1,19]:form1|:form2}
623/// Lithuanian:
624/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
625/// Russian (requires repeated form):
626/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
627/// Slovak
628/// {1:form0|[2,4]:form1|:form2}
629/// Polish (requires repeated form):
630/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
David Blaikie40847cf2011-09-26 01:18:08 +0000631static void HandlePluralModifier(const Diagnostic &DInfo, unsigned ValNo,
Sebastian Redle4c452c2008-11-22 13:44:36 +0000632 const char *Argument, unsigned ArgumentLen,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000633 SmallVectorImpl<char> &OutStr) {
Sebastian Redle4c452c2008-11-22 13:44:36 +0000634 const char *ArgumentEnd = Argument + ArgumentLen;
635 while (1) {
636 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
637 const char *ExprEnd = Argument;
638 while (*ExprEnd != ':') {
639 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
640 ++ExprEnd;
641 }
642 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
643 Argument = ExprEnd + 1;
John McCall909c1822010-01-14 20:11:39 +0000644 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
John McCalle53a44b2010-10-14 01:55:31 +0000645
646 // Recursively format the result of the plural clause into the
647 // output string.
648 DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000649 return;
650 }
John McCall909c1822010-01-14 20:11:39 +0000651 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
Sebastian Redle4c452c2008-11-22 13:44:36 +0000652 }
653}
654
655
Chris Lattnerf4c83962008-11-19 06:51:40 +0000656/// FormatDiagnostic - Format this diagnostic into a string, substituting the
657/// formal arguments into the %0 slots. The result is appended onto the Str
658/// array.
David Blaikie40847cf2011-09-26 01:18:08 +0000659void Diagnostic::
Chris Lattner5f9e2722011-07-23 10:55:15 +0000660FormatDiagnostic(SmallVectorImpl<char> &OutStr) const {
Argyrios Kyrtzidise59abb52011-05-05 07:54:59 +0000661 if (!StoredDiagMessage.empty()) {
662 OutStr.append(StoredDiagMessage.begin(), StoredDiagMessage.end());
663 return;
664 }
665
Chris Lattner5f9e2722011-07-23 10:55:15 +0000666 StringRef Diag =
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000667 getDiags()->getDiagnosticIDs()->getDescription(getID());
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000669 FormatDiagnostic(Diag.begin(), Diag.end(), OutStr);
John McCall9f286142010-01-13 23:58:20 +0000670}
671
David Blaikie40847cf2011-09-26 01:18:08 +0000672void Diagnostic::
John McCall9f286142010-01-13 23:58:20 +0000673FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000674 SmallVectorImpl<char> &OutStr) const {
John McCall9f286142010-01-13 23:58:20 +0000675
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000676 /// FormattedArgs - Keep track of all of the arguments formatted by
677 /// ConvertArgToString and pass them into subsequent calls to
678 /// ConvertArgToString, allowing the implementation to avoid redundancies in
679 /// obvious cases.
David Blaikied6471f72011-09-25 23:23:43 +0000680 SmallVector<DiagnosticsEngine::ArgumentValue, 8> FormattedArgs;
Chandler Carruth0673cb32011-07-11 17:49:21 +0000681
682 /// QualTypeVals - Pass a vector of arrays so that QualType names can be
683 /// compared to see if more information is needed to be printed.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000684 SmallVector<intptr_t, 2> QualTypeVals;
Richard Trieu246b6aa2012-06-26 18:18:47 +0000685 SmallVector<char, 64> Tree;
686
Chandler Carruth0673cb32011-07-11 17:49:21 +0000687 for (unsigned i = 0, e = getNumArgs(); i < e; ++i)
David Blaikied6471f72011-09-25 23:23:43 +0000688 if (getArgKind(i) == DiagnosticsEngine::ak_qualtype)
Chandler Carruth0673cb32011-07-11 17:49:21 +0000689 QualTypeVals.push_back(getRawArg(i));
690
Chris Lattnerf4c83962008-11-19 06:51:40 +0000691 while (DiagStr != DiagEnd) {
692 if (DiagStr[0] != '%') {
693 // Append non-%0 substrings to Str if we have one.
694 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
695 OutStr.append(DiagStr, StrEnd);
696 DiagStr = StrEnd;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000697 continue;
John McCall909c1822010-01-14 20:11:39 +0000698 } else if (ispunct(DiagStr[1])) {
699 OutStr.push_back(DiagStr[1]); // %% -> %.
Chris Lattnerf4c83962008-11-19 06:51:40 +0000700 DiagStr += 2;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000701 continue;
702 }
Mike Stump1eb44332009-09-09 15:08:12 +0000703
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000704 // Skip the %.
705 ++DiagStr;
Mike Stump1eb44332009-09-09 15:08:12 +0000706
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000707 // This must be a placeholder for a diagnostic argument. The format for a
708 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
709 // The digit is a number from 0-9 indicating which argument this comes from.
710 // The modifier is a string of digits from the set [-a-z]+, arguments is a
711 // brace enclosed string.
712 const char *Modifier = 0, *Argument = 0;
713 unsigned ModifierLen = 0, ArgumentLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000715 // Check to see if we have a modifier. If so eat it.
716 if (!isdigit(DiagStr[0])) {
717 Modifier = DiagStr;
718 while (DiagStr[0] == '-' ||
719 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
720 ++DiagStr;
721 ModifierLen = DiagStr-Modifier;
Chris Lattnerf4c83962008-11-19 06:51:40 +0000722
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000723 // If we have an argument, get it next.
724 if (DiagStr[0] == '{') {
725 ++DiagStr; // Skip {.
726 Argument = DiagStr;
Mike Stump1eb44332009-09-09 15:08:12 +0000727
John McCall909c1822010-01-14 20:11:39 +0000728 DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
729 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000730 ArgumentLen = DiagStr-Argument;
731 ++DiagStr; // Skip }.
Chris Lattnerf4c83962008-11-19 06:51:40 +0000732 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000733 }
Mike Stump1eb44332009-09-09 15:08:12 +0000734
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000735 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner22caddc2008-11-23 09:13:29 +0000736 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000737
Richard Trieu246b6aa2012-06-26 18:18:47 +0000738 // Only used for type diffing.
739 unsigned ArgNo2 = ArgNo;
740
David Blaikied6471f72011-09-25 23:23:43 +0000741 DiagnosticsEngine::ArgumentKind Kind = getArgKind(ArgNo);
Richard Trieu246b6aa2012-06-26 18:18:47 +0000742 if (Kind == DiagnosticsEngine::ak_qualtype &&
743 ModifierIs(Modifier, ModifierLen, "diff")) {
744 Kind = DiagnosticsEngine::ak_qualtype_pair;
745 assert(*DiagStr == ',' && isdigit(*(DiagStr + 1)) &&
746 "Invalid format for diff modifier");
747 ++DiagStr; // Comma.
748 ArgNo2 = *DiagStr++ - '0';
749 assert(getArgKind(ArgNo2) == DiagnosticsEngine::ak_qualtype &&
750 "Second value of type diff must be a qualtype");
751 }
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000752
753 switch (Kind) {
Chris Lattner08631c52008-11-23 21:45:46 +0000754 // ---- STRINGS ----
David Blaikied6471f72011-09-25 23:23:43 +0000755 case DiagnosticsEngine::ak_std_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000756 const std::string &S = getArgStdStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000757 assert(ModifierLen == 0 && "No modifiers for strings yet");
758 OutStr.append(S.begin(), S.end());
759 break;
760 }
David Blaikied6471f72011-09-25 23:23:43 +0000761 case DiagnosticsEngine::ak_c_string: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000762 const char *S = getArgCStr(ArgNo);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000763 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbare46e3542009-04-20 06:13:16 +0000764
765 // Don't crash if get passed a null pointer by accident.
766 if (!S)
767 S = "(null)";
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000769 OutStr.append(S, S + strlen(S));
770 break;
771 }
Chris Lattner08631c52008-11-23 21:45:46 +0000772 // ---- INTEGERS ----
David Blaikied6471f72011-09-25 23:23:43 +0000773 case DiagnosticsEngine::ak_sint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000774 int Val = getArgSInt(ArgNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000776 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCalle53a44b2010-10-14 01:55:31 +0000777 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen,
778 OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000779 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
780 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000781 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCalle53a44b2010-10-14 01:55:31 +0000782 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
783 OutStr);
John McCall3be16b72010-01-14 00:50:32 +0000784 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
785 HandleOrdinalModifier((unsigned)Val, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000786 } else {
787 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbar23e47c62009-10-17 18:12:14 +0000788 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner30bc9652008-11-19 07:22:31 +0000789 }
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000790 break;
791 }
David Blaikied6471f72011-09-25 23:23:43 +0000792 case DiagnosticsEngine::ak_uint: {
Chris Lattner22caddc2008-11-23 09:13:29 +0000793 unsigned Val = getArgUInt(ArgNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000795 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCall9f286142010-01-13 23:58:20 +0000796 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000797 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
798 HandleIntegerSModifier(Val, OutStr);
Sebastian Redle4c452c2008-11-22 13:44:36 +0000799 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCalle53a44b2010-10-14 01:55:31 +0000800 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
801 OutStr);
John McCall3be16b72010-01-14 00:50:32 +0000802 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
803 HandleOrdinalModifier(Val, OutStr);
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000804 } else {
805 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbar23e47c62009-10-17 18:12:14 +0000806 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner30bc9652008-11-19 07:22:31 +0000807 }
Chris Lattner22caddc2008-11-23 09:13:29 +0000808 break;
Chris Lattneraf7ae4e2008-11-21 07:50:02 +0000809 }
Chris Lattner08631c52008-11-23 21:45:46 +0000810 // ---- NAMES and TYPES ----
David Blaikied6471f72011-09-25 23:23:43 +0000811 case DiagnosticsEngine::ak_identifierinfo: {
Chris Lattner08631c52008-11-23 21:45:46 +0000812 const IdentifierInfo *II = getArgIdentifier(ArgNo);
813 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbare46e3542009-04-20 06:13:16 +0000814
815 // Don't crash if get passed a null pointer by accident.
816 if (!II) {
817 const char *S = "(null)";
818 OutStr.append(S, S + strlen(S));
819 continue;
820 }
821
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000822 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
Chris Lattner08631c52008-11-23 21:45:46 +0000823 break;
824 }
David Blaikied6471f72011-09-25 23:23:43 +0000825 case DiagnosticsEngine::ak_qualtype:
826 case DiagnosticsEngine::ak_declarationname:
827 case DiagnosticsEngine::ak_nameddecl:
828 case DiagnosticsEngine::ak_nestednamespec:
829 case DiagnosticsEngine::ak_declcontext:
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000830 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000831 Modifier, ModifierLen,
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000832 Argument, ArgumentLen,
833 FormattedArgs.data(), FormattedArgs.size(),
Chandler Carruth0673cb32011-07-11 17:49:21 +0000834 OutStr, QualTypeVals);
Chris Lattner22caddc2008-11-23 09:13:29 +0000835 break;
Richard Trieu246b6aa2012-06-26 18:18:47 +0000836 case DiagnosticsEngine::ak_qualtype_pair:
837 // Create a struct with all the info needed for printing.
838 TemplateDiffTypes TDT;
839 TDT.FromType = getRawArg(ArgNo);
840 TDT.ToType = getRawArg(ArgNo2);
841 TDT.ElideType = getDiags()->ElideType;
842 TDT.ShowColors = getDiags()->ShowColors;
Richard Trieu5409d282012-07-10 01:46:04 +0000843 TDT.TemplateDiffUsed = false;
Richard Trieu246b6aa2012-06-26 18:18:47 +0000844 intptr_t val = reinterpret_cast<intptr_t>(&TDT);
845
Richard Trieu529cdf42012-06-29 21:12:16 +0000846 const char *ArgumentEnd = Argument + ArgumentLen;
847 const char *Pipe = ScanFormat(Argument, ArgumentEnd, '|');
848
Richard Trieu55619772012-07-13 21:18:32 +0000849 // Print the tree. If this diagnostic already has a tree, skip the
850 // second tree.
851 if (getDiags()->PrintTemplateTree && Tree.empty()) {
Richard Trieu246b6aa2012-06-26 18:18:47 +0000852 TDT.PrintFromType = true;
853 TDT.PrintTree = true;
854 getDiags()->ConvertArgToString(Kind, val,
855 Modifier, ModifierLen,
856 Argument, ArgumentLen,
857 FormattedArgs.data(),
858 FormattedArgs.size(),
859 Tree, QualTypeVals);
860 // If there is no tree information, fall back to regular printing.
Richard Trieu529cdf42012-06-29 21:12:16 +0000861 if (!Tree.empty()) {
862 FormatDiagnostic(Pipe + 1, ArgumentEnd, OutStr);
Richard Trieu246b6aa2012-06-26 18:18:47 +0000863 break;
Richard Trieu529cdf42012-06-29 21:12:16 +0000864 }
Richard Trieu246b6aa2012-06-26 18:18:47 +0000865 }
866
867 // Non-tree printing, also the fall-back when tree printing fails.
868 // The fall-back is triggered when the types compared are not templates.
Richard Trieu529cdf42012-06-29 21:12:16 +0000869 const char *FirstDollar = ScanFormat(Argument, ArgumentEnd, '$');
870 const char *SecondDollar = ScanFormat(FirstDollar + 1, ArgumentEnd, '$');
Richard Trieu246b6aa2012-06-26 18:18:47 +0000871
872 // Append before text
Richard Trieu529cdf42012-06-29 21:12:16 +0000873 FormatDiagnostic(Argument, FirstDollar, OutStr);
Richard Trieu246b6aa2012-06-26 18:18:47 +0000874
875 // Append first type
876 TDT.PrintTree = false;
877 TDT.PrintFromType = true;
878 getDiags()->ConvertArgToString(Kind, val,
879 Modifier, ModifierLen,
880 Argument, ArgumentLen,
881 FormattedArgs.data(), FormattedArgs.size(),
882 OutStr, QualTypeVals);
Richard Trieu5409d282012-07-10 01:46:04 +0000883 if (!TDT.TemplateDiffUsed)
884 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
885 TDT.FromType));
886
Richard Trieu246b6aa2012-06-26 18:18:47 +0000887 // Append middle text
Richard Trieu529cdf42012-06-29 21:12:16 +0000888 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
Richard Trieu246b6aa2012-06-26 18:18:47 +0000889
890 // Append second type
891 TDT.PrintFromType = false;
892 getDiags()->ConvertArgToString(Kind, val,
893 Modifier, ModifierLen,
894 Argument, ArgumentLen,
895 FormattedArgs.data(), FormattedArgs.size(),
896 OutStr, QualTypeVals);
Richard Trieu5409d282012-07-10 01:46:04 +0000897 if (!TDT.TemplateDiffUsed)
898 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
899 TDT.ToType));
900
Richard Trieu246b6aa2012-06-26 18:18:47 +0000901 // Append end text
Richard Trieu529cdf42012-06-29 21:12:16 +0000902 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
Richard Trieu246b6aa2012-06-26 18:18:47 +0000903 break;
Nico Weber7bfaaae2008-08-10 19:59:06 +0000904 }
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000905
906 // Remember this argument info for subsequent formatting operations. Turn
907 // std::strings into a null terminated string to make it be the same case as
908 // all the other ones.
Richard Trieu246b6aa2012-06-26 18:18:47 +0000909 if (Kind == DiagnosticsEngine::ak_qualtype_pair)
910 continue;
911 else if (Kind != DiagnosticsEngine::ak_std_string)
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000912 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
913 else
David Blaikied6471f72011-09-25 23:23:43 +0000914 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_c_string,
Chris Lattnerb54d8af2009-10-20 05:25:22 +0000915 (intptr_t)getArgStdStr(ArgNo).c_str()));
916
Nico Weber7bfaaae2008-08-10 19:59:06 +0000917 }
Richard Trieu246b6aa2012-06-26 18:18:47 +0000918
919 // Append the type tree to the end of the diagnostics.
920 OutStr.append(Tree.begin(), Tree.end());
Nico Weber7bfaaae2008-08-10 19:59:06 +0000921}
Ted Kremenekcabe6682009-01-23 20:28:53 +0000922
Douglas Gregora88084b2010-02-18 18:08:43 +0000923StoredDiagnostic::StoredDiagnostic() { }
924
David Blaikied6471f72011-09-25 23:23:43 +0000925StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000926 StringRef Message)
Benjamin Kramera6a32e22010-11-19 17:36:51 +0000927 : ID(ID), Level(Level), Loc(), Message(Message) { }
Douglas Gregora88084b2010-02-18 18:08:43 +0000928
David Blaikied6471f72011-09-25 23:23:43 +0000929StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level,
David Blaikie40847cf2011-09-26 01:18:08 +0000930 const Diagnostic &Info)
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000931 : ID(Info.getID()), Level(Level)
932{
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000933 assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
934 "Valid source location without setting a source manager for diagnostic");
935 if (Info.getLocation().isValid())
936 Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000937 SmallString<64> Message;
Douglas Gregora88084b2010-02-18 18:08:43 +0000938 Info.FormatDiagnostic(Message);
939 this->Message.assign(Message.begin(), Message.end());
940
941 Ranges.reserve(Info.getNumRanges());
942 for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I)
943 Ranges.push_back(Info.getRange(I));
944
Douglas Gregor849b2432010-03-31 17:46:05 +0000945 FixIts.reserve(Info.getNumFixItHints());
946 for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I)
947 FixIts.push_back(Info.getFixItHint(I));
Douglas Gregora88084b2010-02-18 18:08:43 +0000948}
949
David Blaikied6471f72011-09-25 23:23:43 +0000950StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000951 StringRef Message, FullSourceLoc Loc,
Chris Lattner2d3ba4f2011-07-23 17:14:25 +0000952 ArrayRef<CharSourceRange> Ranges,
953 ArrayRef<FixItHint> Fixits)
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000954 : ID(ID), Level(Level), Loc(Loc), Message(Message)
955{
956 this->Ranges.assign(Ranges.begin(), Ranges.end());
957 this->FixIts.assign(FixIts.begin(), FixIts.end());
958}
959
Douglas Gregora88084b2010-02-18 18:08:43 +0000960StoredDiagnostic::~StoredDiagnostic() { }
961
Ted Kremenekcabe6682009-01-23 20:28:53 +0000962/// IncludeInDiagnosticCounts - This method (whose default implementation
963/// returns true) indicates whether the diagnostics handled by this
David Blaikie78ad0b92011-09-25 23:39:51 +0000964/// DiagnosticConsumer should be included in the number of diagnostics
David Blaikied6471f72011-09-25 23:23:43 +0000965/// reported by DiagnosticsEngine.
David Blaikie78ad0b92011-09-25 23:39:51 +0000966bool DiagnosticConsumer::IncludeInDiagnosticCounts() const { return true; }
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000967
David Blaikie99ba9e32011-12-20 02:48:34 +0000968void IgnoringDiagConsumer::anchor() { }
969
Benjamin Kramerd7a3e2c2012-02-07 22:29:24 +0000970PartialDiagnostic::StorageAllocator::StorageAllocator() {
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000971 for (unsigned I = 0; I != NumCached; ++I)
972 FreeList[I] = Cached + I;
973 NumFreeListEntries = NumCached;
974}
975
Benjamin Kramerd7a3e2c2012-02-07 22:29:24 +0000976PartialDiagnostic::StorageAllocator::~StorageAllocator() {
Chad Rosierdfaee492012-02-07 23:24:49 +0000977 // Don't assert if we are in a CrashRecovery context, as this invariant may
978 // be invalidated during a crash.
979 assert((NumFreeListEntries == NumCached ||
980 llvm::CrashRecoveryContext::isRecoveringFromCrash()) &&
981 "A partial is on the lamb");
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000982}