blob: 893eed041f5c7e49e882a57e9f99feb08ef4ad41 [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- Diagnostic.cpp - C Language Family Diagnostic Handling -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner22eb9722006-06-18 05:43:12 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Diagnostic-related interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Ted Kremenek39a76652010-04-12 19:54:17 +000014#include "clang/Basic/Diagnostic.h"
Chris Lattnerb91fd172008-11-19 07:32:16 +000015#include "clang/Basic/IdentifierTable.h"
Ted Kremenek39a76652010-04-12 19:54:17 +000016#include "clang/Basic/PartialDiagnostic.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000017#include "llvm/ADT/SmallString.h"
Jordan Rosec102b352012-09-22 01:24:42 +000018#include "llvm/ADT/StringExtras.h"
Daniel Dunbare3633792009-10-17 18:12:14 +000019#include "llvm/Support/raw_ostream.h"
Ted Kremenek84de4a12011-03-21 18:40:07 +000020#include "llvm/Support/CrashRecoveryContext.h"
Joerg Sonnenberger42cf2682012-08-10 10:58:18 +000021#include <cctype>
Ted Kremenek84de4a12011-03-21 18:40:07 +000022
Chris Lattner22eb9722006-06-18 05:43:12 +000023using namespace clang;
24
David Blaikie9c902b52011-09-25 23:23:43 +000025static void DummyArgToStringFn(DiagnosticsEngine::ArgumentKind AK, intptr_t QT,
Chris Lattner63ecc502008-11-23 09:21:17 +000026 const char *Modifier, unsigned ML,
27 const char *Argument, unsigned ArgLen,
David Blaikie9c902b52011-09-25 23:23:43 +000028 const DiagnosticsEngine::ArgumentValue *PrevArgs,
Chris Lattnerc243f292009-10-20 05:25:22 +000029 unsigned NumPrevArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000030 SmallVectorImpl<char> &Output,
Chandler Carruthd5173952011-07-11 17:49:21 +000031 void *Cookie,
Bill Wendling8eb771d2012-02-22 09:51:33 +000032 ArrayRef<intptr_t> QualTypeVals) {
Chris Lattner63ecc502008-11-23 09:21:17 +000033 const char *Str = "<can't format argument>";
Chris Lattner6a2ed6f2008-11-23 09:13:29 +000034 Output.append(Str, Str+strlen(Str));
35}
36
37
David Blaikie9c902b52011-09-25 23:23:43 +000038DiagnosticsEngine::DiagnosticsEngine(
Dylan Noblesmithc95d8192012-02-20 14:00:23 +000039 const IntrusiveRefCntPtr<DiagnosticIDs> &diags,
David Blaikiee2eefae2011-09-25 23:39:51 +000040 DiagnosticConsumer *client, bool ShouldOwnClient)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000041 : Diags(diags), Client(client), OwnsDiagClient(ShouldOwnClient),
42 SourceMgr(0) {
Chris Lattner63ecc502008-11-23 09:21:17 +000043 ArgToStringFn = DummyArgToStringFn;
Chris Lattnercf868c42009-02-19 23:53:20 +000044 ArgToStringCookie = 0;
Mike Stump11289f42009-09-09 15:08:12 +000045
Douglas Gregor0e119552010-07-31 00:40:00 +000046 AllExtensionsSilenced = 0;
47 IgnoreAllWarnings = false;
48 WarningsAsErrors = false;
Ted Kremenekfbbdced2011-08-18 01:12:56 +000049 EnableAllWarnings = false;
Douglas Gregor0e119552010-07-31 00:40:00 +000050 ErrorsAsFatal = false;
51 SuppressSystemWarnings = false;
52 SuppressAllDiagnostics = false;
Richard Trieu91844232012-06-26 18:18:47 +000053 ElideType = true;
54 PrintTemplateTree = false;
55 ShowColors = false;
Douglas Gregor0e119552010-07-31 00:40:00 +000056 ShowOverloads = Ovl_All;
57 ExtBehavior = Ext_Ignore;
58
59 ErrorLimit = 0;
60 TemplateBacktraceLimit = 0;
Richard Smithf6f003a2011-12-16 19:06:07 +000061 ConstexprBacktraceLimit = 0;
Douglas Gregor0e119552010-07-31 00:40:00 +000062
Douglas Gregoraa21cc42010-07-19 21:46:24 +000063 Reset();
Chris Lattnerae411572006-07-05 00:55:08 +000064}
65
David Blaikie9c902b52011-09-25 23:23:43 +000066DiagnosticsEngine::~DiagnosticsEngine() {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000067 if (OwnsDiagClient)
68 delete Client;
Chris Lattnere6535cf2007-12-02 01:09:57 +000069}
70
David Blaikiee2eefae2011-09-25 23:39:51 +000071void DiagnosticsEngine::setClient(DiagnosticConsumer *client,
David Blaikie9c902b52011-09-25 23:23:43 +000072 bool ShouldOwnClient) {
Douglas Gregor7a964ad2011-01-31 22:04:05 +000073 if (OwnsDiagClient && Client)
74 delete Client;
75
76 Client = client;
77 OwnsDiagClient = ShouldOwnClient;
78}
Chris Lattnerfb42a182009-07-12 21:18:45 +000079
David Blaikie9c902b52011-09-25 23:23:43 +000080void DiagnosticsEngine::pushMappings(SourceLocation Loc) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +000081 DiagStateOnPushStack.push_back(GetCurDiagState());
Chris Lattnerfb42a182009-07-12 21:18:45 +000082}
83
David Blaikie9c902b52011-09-25 23:23:43 +000084bool DiagnosticsEngine::popMappings(SourceLocation Loc) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +000085 if (DiagStateOnPushStack.empty())
Chris Lattnerfb42a182009-07-12 21:18:45 +000086 return false;
87
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +000088 if (DiagStateOnPushStack.back() != GetCurDiagState()) {
89 // State changed at some point between push/pop.
90 PushDiagStatePoint(DiagStateOnPushStack.back(), Loc);
91 }
92 DiagStateOnPushStack.pop_back();
Chris Lattnerfb42a182009-07-12 21:18:45 +000093 return true;
94}
95
David Blaikie9c902b52011-09-25 23:23:43 +000096void DiagnosticsEngine::Reset() {
Douglas Gregoraa21cc42010-07-19 21:46:24 +000097 ErrorOccurred = false;
98 FatalErrorOccurred = false;
Douglas Gregor8a60bbe2011-07-06 17:40:26 +000099 UnrecoverableErrorOccurred = false;
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000100
101 NumWarnings = 0;
102 NumErrors = 0;
103 NumErrorsSuppressed = 0;
Argyrios Kyrtzidis1fa8b4b2011-07-29 01:25:44 +0000104 TrapNumErrorsOccurred = 0;
105 TrapNumUnrecoverableErrorsOccurred = 0;
Douglas Gregor8a60bbe2011-07-06 17:40:26 +0000106
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000107 CurDiagID = ~0U;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000108 // Set LastDiagLevel to an "unset" state. If we set it to 'Ignored', notes
David Blaikie9c902b52011-09-25 23:23:43 +0000109 // using a DiagnosticsEngine associated to a translation unit that follow
110 // diagnostics from a DiagnosticsEngine associated to anoter t.u. will not be
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000111 // displayed.
112 LastDiagLevel = (DiagnosticIDs::Level)-1;
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000113 DelayedDiagID = 0;
Argyrios Kyrtzidisbbbeea12011-03-26 18:58:17 +0000114
115 // Clear state related to #pragma diagnostic.
116 DiagStates.clear();
117 DiagStatePoints.clear();
118 DiagStateOnPushStack.clear();
119
120 // Create a DiagState and DiagStatePoint representing diagnostic changes
121 // through command-line.
122 DiagStates.push_back(DiagState());
Richard Smithf995f2c2012-08-14 04:19:29 +0000123 DiagStatePoints.push_back(DiagStatePoint(&DiagStates.back(), FullSourceLoc()));
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000124}
Chris Lattner22eb9722006-06-18 05:43:12 +0000125
David Blaikie9c902b52011-09-25 23:23:43 +0000126void DiagnosticsEngine::SetDelayedDiagnostic(unsigned DiagID, StringRef Arg1,
Chad Rosier849a67b2012-02-07 23:24:49 +0000127 StringRef Arg2) {
Douglas Gregor85795312010-03-22 15:10:57 +0000128 if (DelayedDiagID)
129 return;
130
131 DelayedDiagID = DiagID;
Douglas Gregor96380982010-03-22 15:47:45 +0000132 DelayedDiagArg1 = Arg1.str();
133 DelayedDiagArg2 = Arg2.str();
Douglas Gregor85795312010-03-22 15:10:57 +0000134}
135
David Blaikie9c902b52011-09-25 23:23:43 +0000136void DiagnosticsEngine::ReportDelayed() {
Douglas Gregor85795312010-03-22 15:10:57 +0000137 Report(DelayedDiagID) << DelayedDiagArg1 << DelayedDiagArg2;
138 DelayedDiagID = 0;
139 DelayedDiagArg1.clear();
140 DelayedDiagArg2.clear();
141}
142
David Blaikie9c902b52011-09-25 23:23:43 +0000143DiagnosticsEngine::DiagStatePointsTy::iterator
144DiagnosticsEngine::GetDiagStatePointForLoc(SourceLocation L) const {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000145 assert(!DiagStatePoints.empty());
146 assert(DiagStatePoints.front().Loc.isInvalid() &&
147 "Should have created a DiagStatePoint for command-line");
148
Richard Smith99eff012012-08-17 00:55:32 +0000149 if (!SourceMgr)
150 return DiagStatePoints.end() - 1;
151
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000152 FullSourceLoc Loc(L, *SourceMgr);
153 if (Loc.isInvalid())
154 return DiagStatePoints.end() - 1;
155
156 DiagStatePointsTy::iterator Pos = DiagStatePoints.end();
157 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
158 if (LastStateChangePos.isValid() &&
159 Loc.isBeforeInTranslationUnitThan(LastStateChangePos))
160 Pos = std::upper_bound(DiagStatePoints.begin(), DiagStatePoints.end(),
161 DiagStatePoint(0, Loc));
162 --Pos;
163 return Pos;
164}
165
David Blaikie9c902b52011-09-25 23:23:43 +0000166void DiagnosticsEngine::setDiagnosticMapping(diag::kind Diag, diag::Mapping Map,
Chad Rosier849a67b2012-02-07 23:24:49 +0000167 SourceLocation L) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000168 assert(Diag < diag::DIAG_UPPER_LIMIT &&
169 "Can only map builtin diagnostics");
170 assert((Diags->isBuiltinWarningOrExtension(Diag) ||
171 (Map == diag::MAP_FATAL || Map == diag::MAP_ERROR)) &&
172 "Cannot map errors into warnings!");
173 assert(!DiagStatePoints.empty());
Richard Smith8a0527d2012-08-14 22:37:22 +0000174 assert((L.isInvalid() || SourceMgr) && "No SourceMgr for valid location");
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000175
Richard Smith8a0527d2012-08-14 22:37:22 +0000176 FullSourceLoc Loc = SourceMgr? FullSourceLoc(L, *SourceMgr) : FullSourceLoc();
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000177 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
Chad Rosierd1956e42012-02-03 01:49:51 +0000178 // Don't allow a mapping to a warning override an error/fatal mapping.
179 if (Map == diag::MAP_WARNING) {
180 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(Diag);
181 if (Info.getMapping() == diag::MAP_ERROR ||
182 Info.getMapping() == diag::MAP_FATAL)
183 Map = Info.getMapping();
184 }
Argyrios Kyrtzidisc137d0d2011-11-09 01:24:17 +0000185 DiagnosticMappingInfo MappingInfo = makeMappingInfo(Map, L);
Daniel Dunbar2fba0972011-10-04 21:17:24 +0000186
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000187 // Common case; setting all the diagnostics of a group in one place.
188 if (Loc.isInvalid() || Loc == LastStateChangePos) {
Daniel Dunbar458edfa2011-09-29 01:34:47 +0000189 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000190 return;
191 }
192
193 // Another common case; modifying diagnostic state in a source location
194 // after the previous one.
195 if ((Loc.isValid() && LastStateChangePos.isInvalid()) ||
196 LastStateChangePos.isBeforeInTranslationUnitThan(Loc)) {
Chris Lattner57540c52011-04-15 05:22:18 +0000197 // A diagnostic pragma occurred, create a new DiagState initialized with
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000198 // the current one and a new DiagStatePoint to record at which location
199 // the new state became active.
200 DiagStates.push_back(*GetCurDiagState());
201 PushDiagStatePoint(&DiagStates.back(), Loc);
Daniel Dunbar458edfa2011-09-29 01:34:47 +0000202 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000203 return;
204 }
205
206 // We allow setting the diagnostic state in random source order for
207 // completeness but it should not be actually happening in normal practice.
208
209 DiagStatePointsTy::iterator Pos = GetDiagStatePointForLoc(Loc);
210 assert(Pos != DiagStatePoints.end());
211
212 // Update all diagnostic states that are active after the given location.
213 for (DiagStatePointsTy::iterator
214 I = Pos+1, E = DiagStatePoints.end(); I != E; ++I) {
Daniel Dunbar458edfa2011-09-29 01:34:47 +0000215 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000216 }
217
218 // If the location corresponds to an existing point, just update its state.
219 if (Pos->Loc == Loc) {
Daniel Dunbar458edfa2011-09-29 01:34:47 +0000220 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000221 return;
222 }
223
224 // Create a new state/point and fit it into the vector of DiagStatePoints
225 // so that the vector is always ordered according to location.
226 Pos->Loc.isBeforeInTranslationUnitThan(Loc);
227 DiagStates.push_back(*Pos->State);
228 DiagState *NewState = &DiagStates.back();
Daniel Dunbar458edfa2011-09-29 01:34:47 +0000229 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000230 DiagStatePoints.insert(Pos+1, DiagStatePoint(NewState,
231 FullSourceLoc(Loc, *SourceMgr)));
232}
233
Daniel Dunbard908c122011-09-29 01:47:16 +0000234bool DiagnosticsEngine::setDiagnosticGroupMapping(
235 StringRef Group, diag::Mapping Map, SourceLocation Loc)
236{
237 // Get the diagnostics in this group.
238 llvm::SmallVector<diag::kind, 8> GroupDiags;
239 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
240 return true;
241
242 // Set the mapping.
243 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i)
244 setDiagnosticMapping(GroupDiags[i], Map, Loc);
245
246 return false;
247}
248
Chad Rosier4c17fa72012-02-07 19:55:45 +0000249void DiagnosticsEngine::setDiagnosticWarningAsError(diag::kind Diag,
250 bool Enabled) {
251 // If we are enabling this feature, just set the diagnostic mappings to map to
252 // errors.
253 if (Enabled)
254 setDiagnosticMapping(Diag, diag::MAP_ERROR, SourceLocation());
255
256 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
257 // potentially downgrade anything already mapped to be a warning.
258 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(Diag);
259
260 if (Info.getMapping() == diag::MAP_ERROR ||
261 Info.getMapping() == diag::MAP_FATAL)
262 Info.setMapping(diag::MAP_WARNING);
263
264 Info.setNoWarningAsError(true);
265}
266
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000267bool DiagnosticsEngine::setDiagnosticGroupWarningAsError(StringRef Group,
268 bool Enabled) {
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000269 // If we are enabling this feature, just set the diagnostic mappings to map to
270 // errors.
271 if (Enabled)
272 return setDiagnosticGroupMapping(Group, diag::MAP_ERROR);
273
274 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
275 // potentially downgrade anything already mapped to be a warning.
276
277 // Get the diagnostics in this group.
278 llvm::SmallVector<diag::kind, 8> GroupDiags;
279 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
280 return true;
281
282 // Perform the mapping change.
283 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i) {
284 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(
285 GroupDiags[i]);
286
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000287 if (Info.getMapping() == diag::MAP_ERROR ||
288 Info.getMapping() == diag::MAP_FATAL)
289 Info.setMapping(diag::MAP_WARNING);
290
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000291 Info.setNoWarningAsError(true);
292 }
293
294 return false;
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000295}
296
Chad Rosier4c17fa72012-02-07 19:55:45 +0000297void DiagnosticsEngine::setDiagnosticErrorAsFatal(diag::kind Diag,
298 bool Enabled) {
299 // If we are enabling this feature, just set the diagnostic mappings to map to
300 // errors.
301 if (Enabled)
302 setDiagnosticMapping(Diag, diag::MAP_FATAL, SourceLocation());
303
304 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
305 // potentially downgrade anything already mapped to be a warning.
306 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(Diag);
307
308 if (Info.getMapping() == diag::MAP_FATAL)
309 Info.setMapping(diag::MAP_ERROR);
310
311 Info.setNoErrorAsFatal(true);
312}
313
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000314bool DiagnosticsEngine::setDiagnosticGroupErrorAsFatal(StringRef Group,
315 bool Enabled) {
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000316 // If we are enabling this feature, just set the diagnostic mappings to map to
317 // fatal errors.
318 if (Enabled)
319 return setDiagnosticGroupMapping(Group, diag::MAP_FATAL);
320
321 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
322 // potentially downgrade anything already mapped to be an error.
323
324 // Get the diagnostics in this group.
325 llvm::SmallVector<diag::kind, 8> GroupDiags;
326 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
327 return true;
328
329 // Perform the mapping change.
330 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i) {
331 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(
332 GroupDiags[i]);
333
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000334 if (Info.getMapping() == diag::MAP_FATAL)
335 Info.setMapping(diag::MAP_ERROR);
336
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000337 Info.setNoErrorAsFatal(true);
338 }
339
340 return false;
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000341}
342
Argyrios Kyrtzidis059cac42012-01-28 04:35:52 +0000343void DiagnosticsEngine::setMappingToAllDiagnostics(diag::Mapping Map,
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000344 SourceLocation Loc) {
345 // Get all the diagnostics.
346 llvm::SmallVector<diag::kind, 64> AllDiags;
347 Diags->getAllDiagnostics(AllDiags);
348
349 // Set the mapping.
350 for (unsigned i = 0, e = AllDiags.size(); i != e; ++i)
351 if (Diags->isBuiltinWarningOrExtension(AllDiags[i]))
352 setDiagnosticMapping(AllDiags[i], Map, Loc);
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000353}
354
David Blaikie9c902b52011-09-25 23:23:43 +0000355void DiagnosticsEngine::Report(const StoredDiagnostic &storedDiag) {
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000356 assert(CurDiagID == ~0U && "Multiple diagnostics in flight at once!");
357
358 CurDiagLoc = storedDiag.getLocation();
359 CurDiagID = storedDiag.getID();
360 NumDiagArgs = 0;
361
362 NumDiagRanges = storedDiag.range_size();
Daniel Dunbar007b9dc2012-03-13 18:21:17 +0000363 assert(NumDiagRanges < DiagnosticsEngine::MaxRanges &&
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000364 "Too many arguments to diagnostic!");
365 unsigned i = 0;
366 for (StoredDiagnostic::range_iterator
367 RI = storedDiag.range_begin(),
368 RE = storedDiag.range_end(); RI != RE; ++RI)
369 DiagRanges[i++] = *RI;
370
Daniel Dunbar007b9dc2012-03-13 18:21:17 +0000371 assert(NumDiagRanges < DiagnosticsEngine::MaxFixItHints &&
372 "Too many arguments to diagnostic!");
373 NumDiagFixItHints = 0;
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000374 for (StoredDiagnostic::fixit_iterator
375 FI = storedDiag.fixit_begin(),
376 FE = storedDiag.fixit_end(); FI != FE; ++FI)
Daniel Dunbar007b9dc2012-03-13 18:21:17 +0000377 DiagFixItHints[NumDiagFixItHints++] = *FI;
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000378
David Blaikiee2eefae2011-09-25 23:39:51 +0000379 assert(Client && "DiagnosticConsumer not set!");
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000380 Level DiagLevel = storedDiag.getLevel();
David Blaikieb5784322011-09-26 01:18:08 +0000381 Diagnostic Info(this, storedDiag.getMessage());
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000382 Client->HandleDiagnostic(DiagLevel, Info);
383 if (Client->IncludeInDiagnosticCounts()) {
David Blaikie9c902b52011-09-25 23:23:43 +0000384 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000385 ++NumWarnings;
386 }
387
388 CurDiagID = ~0U;
389}
390
Jordan Rose6f524ac2012-07-11 16:50:36 +0000391bool DiagnosticsEngine::EmitCurrentDiagnostic(bool Force) {
392 assert(getClient() && "DiagnosticClient not set!");
393
394 bool Emitted;
395 if (Force) {
396 Diagnostic Info(this);
397
398 // Figure out the diagnostic level of this message.
399 DiagnosticIDs::Level DiagLevel
400 = Diags->getDiagnosticLevel(Info.getID(), Info.getLocation(), *this);
401
402 Emitted = (DiagLevel != DiagnosticIDs::Ignored);
403 if (Emitted) {
404 // Emit the diagnostic regardless of suppression level.
405 Diags->EmitDiag(*this, DiagLevel);
406 }
407 } else {
408 // Process the diagnostic, sending the accumulated information to the
409 // DiagnosticConsumer.
410 Emitted = ProcessDiag();
411 }
Douglas Gregor85795312010-03-22 15:10:57 +0000412
413 // Clear out the current diagnostic object.
Daniel Dunbarc7c00892012-03-13 21:02:14 +0000414 unsigned DiagID = CurDiagID;
415 Clear();
Douglas Gregor85795312010-03-22 15:10:57 +0000416
417 // If there was a delayed diagnostic, emit it now.
Jordan Rose6f524ac2012-07-11 16:50:36 +0000418 if (!Force && DelayedDiagID && DelayedDiagID != DiagID)
Daniel Dunbarc7c00892012-03-13 21:02:14 +0000419 ReportDelayed();
Douglas Gregor85795312010-03-22 15:10:57 +0000420
421 return Emitted;
422}
423
Nico Weber4c311642008-08-10 19:59:06 +0000424
David Blaikiee2eefae2011-09-25 23:39:51 +0000425DiagnosticConsumer::~DiagnosticConsumer() {}
Nico Weber4c311642008-08-10 19:59:06 +0000426
David Blaikiee2eefae2011-09-25 23:39:51 +0000427void DiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
David Blaikieb5784322011-09-26 01:18:08 +0000428 const Diagnostic &Info) {
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000429 if (!IncludeInDiagnosticCounts())
430 return;
431
David Blaikie9c902b52011-09-25 23:23:43 +0000432 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000433 ++NumWarnings;
David Blaikie9c902b52011-09-25 23:23:43 +0000434 else if (DiagLevel >= DiagnosticsEngine::Error)
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000435 ++NumErrors;
436}
Chris Lattner23be0672008-11-19 06:51:40 +0000437
Chris Lattner2b786902008-11-21 07:50:02 +0000438/// ModifierIs - Return true if the specified modifier matches specified string.
439template <std::size_t StrLen>
440static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
441 const char (&Str)[StrLen]) {
442 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
443}
444
John McCall8cb7a8a32010-01-14 20:11:39 +0000445/// ScanForward - Scans forward, looking for the given character, skipping
446/// nested clauses and escaped characters.
447static const char *ScanFormat(const char *I, const char *E, char Target) {
448 unsigned Depth = 0;
449
450 for ( ; I != E; ++I) {
451 if (Depth == 0 && *I == Target) return I;
452 if (Depth != 0 && *I == '}') Depth--;
453
454 if (*I == '%') {
455 I++;
456 if (I == E) break;
457
458 // Escaped characters get implicitly skipped here.
459
460 // Format specifier.
461 if (!isdigit(*I) && !ispunct(*I)) {
462 for (I++; I != E && !isdigit(*I) && *I != '{'; I++) ;
463 if (I == E) break;
464 if (*I == '{')
465 Depth++;
466 }
467 }
468 }
469 return E;
470}
471
Chris Lattner2b786902008-11-21 07:50:02 +0000472/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
473/// like this: %select{foo|bar|baz}2. This means that the integer argument
474/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
475/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
476/// This is very useful for certain classes of variant diagnostics.
David Blaikieb5784322011-09-26 01:18:08 +0000477static void HandleSelectModifier(const Diagnostic &DInfo, unsigned ValNo,
Chris Lattner2b786902008-11-21 07:50:02 +0000478 const char *Argument, unsigned ArgumentLen,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000479 SmallVectorImpl<char> &OutStr) {
Chris Lattner2b786902008-11-21 07:50:02 +0000480 const char *ArgumentEnd = Argument+ArgumentLen;
Mike Stump11289f42009-09-09 15:08:12 +0000481
Chris Lattner2b786902008-11-21 07:50:02 +0000482 // Skip over 'ValNo' |'s.
483 while (ValNo) {
John McCall8cb7a8a32010-01-14 20:11:39 +0000484 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
Chris Lattner2b786902008-11-21 07:50:02 +0000485 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
486 " larger than the number of options in the diagnostic string!");
487 Argument = NextVal+1; // Skip this string.
488 --ValNo;
489 }
Mike Stump11289f42009-09-09 15:08:12 +0000490
Chris Lattner2b786902008-11-21 07:50:02 +0000491 // Get the end of the value. This is either the } or the |.
John McCall8cb7a8a32010-01-14 20:11:39 +0000492 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
John McCalle4d54322010-01-13 23:58:20 +0000493
494 // Recursively format the result of the select clause into the output string.
495 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000496}
497
498/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
499/// letter 's' to the string if the value is not 1. This is used in cases like
500/// this: "you idiot, you have %4 parameter%s4!".
501static void HandleIntegerSModifier(unsigned ValNo,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000502 SmallVectorImpl<char> &OutStr) {
Chris Lattner2b786902008-11-21 07:50:02 +0000503 if (ValNo != 1)
504 OutStr.push_back('s');
505}
506
John McCall9015cde2010-01-14 00:50:32 +0000507/// HandleOrdinalModifier - Handle the integer 'ord' modifier. This
508/// prints the ordinal form of the given integer, with 1 corresponding
509/// to the first ordinal. Currently this is hard-coded to use the
510/// English form.
511static void HandleOrdinalModifier(unsigned ValNo,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000512 SmallVectorImpl<char> &OutStr) {
John McCall9015cde2010-01-14 00:50:32 +0000513 assert(ValNo != 0 && "ValNo must be strictly positive!");
514
515 llvm::raw_svector_ostream Out(OutStr);
516
517 // We could use text forms for the first N ordinals, but the numeric
518 // forms are actually nicer in diagnostics because they stand out.
Jordan Rosec102b352012-09-22 01:24:42 +0000519 Out << ValNo << llvm::getOrdinalSuffix(ValNo);
John McCall9015cde2010-01-14 00:50:32 +0000520}
521
Chris Lattner2b786902008-11-21 07:50:02 +0000522
Sebastian Redl15b02d22008-11-22 13:44:36 +0000523/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000524static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000525 // Programming 101: Parse a decimal number :-)
526 unsigned Val = 0;
527 while (Start != End && *Start >= '0' && *Start <= '9') {
528 Val *= 10;
529 Val += *Start - '0';
530 ++Start;
531 }
532 return Val;
533}
534
535/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000536static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000537 if (*Start != '[') {
538 unsigned Ref = PluralNumber(Start, End);
539 return Ref == Val;
540 }
541
542 ++Start;
543 unsigned Low = PluralNumber(Start, End);
544 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
545 ++Start;
546 unsigned High = PluralNumber(Start, End);
547 assert(*Start == ']' && "Bad plural expression syntax: expected )");
548 ++Start;
549 return Low <= Val && Val <= High;
550}
551
552/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattner2fe29202009-04-15 17:13:42 +0000553static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000554 // Empty condition?
555 if (*Start == ':')
556 return true;
557
558 while (1) {
559 char C = *Start;
560 if (C == '%') {
561 // Modulo expression
562 ++Start;
563 unsigned Arg = PluralNumber(Start, End);
564 assert(*Start == '=' && "Bad plural expression syntax: expected =");
565 ++Start;
566 unsigned ValMod = ValNo % Arg;
567 if (TestPluralRange(ValMod, Start, End))
568 return true;
569 } else {
Sebastian Redl3ceaf622008-11-27 07:28:14 +0000570 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redl15b02d22008-11-22 13:44:36 +0000571 "Bad plural expression syntax: unexpected character");
572 // Range expression
573 if (TestPluralRange(ValNo, Start, End))
574 return true;
575 }
576
577 // Scan for next or-expr part.
578 Start = std::find(Start, End, ',');
Mike Stump11289f42009-09-09 15:08:12 +0000579 if (Start == End)
Sebastian Redl15b02d22008-11-22 13:44:36 +0000580 break;
581 ++Start;
582 }
583 return false;
584}
585
586/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
587/// for complex plural forms, or in languages where all plurals are complex.
588/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
589/// conditions that are tested in order, the form corresponding to the first
590/// that applies being emitted. The empty condition is always true, making the
591/// last form a default case.
592/// Conditions are simple boolean expressions, where n is the number argument.
593/// Here are the rules.
594/// condition := expression | empty
595/// empty := -> always true
596/// expression := numeric [',' expression] -> logical or
597/// numeric := range -> true if n in range
598/// | '%' number '=' range -> true if n % number in range
599/// range := number
600/// | '[' number ',' number ']' -> ranges are inclusive both ends
601///
602/// Here are some examples from the GNU gettext manual written in this form:
603/// English:
604/// {1:form0|:form1}
605/// Latvian:
606/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
607/// Gaeilge:
608/// {1:form0|2:form1|:form2}
609/// Romanian:
610/// {1:form0|0,%100=[1,19]:form1|:form2}
611/// Lithuanian:
612/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
613/// Russian (requires repeated form):
614/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
615/// Slovak
616/// {1:form0|[2,4]:form1|:form2}
617/// Polish (requires repeated form):
618/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
David Blaikieb5784322011-09-26 01:18:08 +0000619static void HandlePluralModifier(const Diagnostic &DInfo, unsigned ValNo,
Sebastian Redl15b02d22008-11-22 13:44:36 +0000620 const char *Argument, unsigned ArgumentLen,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000621 SmallVectorImpl<char> &OutStr) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000622 const char *ArgumentEnd = Argument + ArgumentLen;
623 while (1) {
624 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
625 const char *ExprEnd = Argument;
626 while (*ExprEnd != ':') {
627 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
628 ++ExprEnd;
629 }
630 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
631 Argument = ExprEnd + 1;
John McCall8cb7a8a32010-01-14 20:11:39 +0000632 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
John McCall43b61682010-10-14 01:55:31 +0000633
634 // Recursively format the result of the plural clause into the
635 // output string.
636 DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000637 return;
638 }
John McCall8cb7a8a32010-01-14 20:11:39 +0000639 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
Sebastian Redl15b02d22008-11-22 13:44:36 +0000640 }
641}
642
643
Chris Lattner23be0672008-11-19 06:51:40 +0000644/// FormatDiagnostic - Format this diagnostic into a string, substituting the
645/// formal arguments into the %0 slots. The result is appended onto the Str
646/// array.
David Blaikieb5784322011-09-26 01:18:08 +0000647void Diagnostic::
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000648FormatDiagnostic(SmallVectorImpl<char> &OutStr) const {
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000649 if (!StoredDiagMessage.empty()) {
650 OutStr.append(StoredDiagMessage.begin(), StoredDiagMessage.end());
651 return;
652 }
653
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000654 StringRef Diag =
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000655 getDiags()->getDiagnosticIDs()->getDescription(getID());
Mike Stump11289f42009-09-09 15:08:12 +0000656
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000657 FormatDiagnostic(Diag.begin(), Diag.end(), OutStr);
John McCalle4d54322010-01-13 23:58:20 +0000658}
659
David Blaikieb5784322011-09-26 01:18:08 +0000660void Diagnostic::
John McCalle4d54322010-01-13 23:58:20 +0000661FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000662 SmallVectorImpl<char> &OutStr) const {
John McCalle4d54322010-01-13 23:58:20 +0000663
Chris Lattnerc243f292009-10-20 05:25:22 +0000664 /// FormattedArgs - Keep track of all of the arguments formatted by
665 /// ConvertArgToString and pass them into subsequent calls to
666 /// ConvertArgToString, allowing the implementation to avoid redundancies in
667 /// obvious cases.
David Blaikie9c902b52011-09-25 23:23:43 +0000668 SmallVector<DiagnosticsEngine::ArgumentValue, 8> FormattedArgs;
Chandler Carruthd5173952011-07-11 17:49:21 +0000669
670 /// QualTypeVals - Pass a vector of arrays so that QualType names can be
671 /// compared to see if more information is needed to be printed.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000672 SmallVector<intptr_t, 2> QualTypeVals;
Richard Trieu91844232012-06-26 18:18:47 +0000673 SmallVector<char, 64> Tree;
674
Chandler Carruthd5173952011-07-11 17:49:21 +0000675 for (unsigned i = 0, e = getNumArgs(); i < e; ++i)
David Blaikie9c902b52011-09-25 23:23:43 +0000676 if (getArgKind(i) == DiagnosticsEngine::ak_qualtype)
Chandler Carruthd5173952011-07-11 17:49:21 +0000677 QualTypeVals.push_back(getRawArg(i));
678
Chris Lattner23be0672008-11-19 06:51:40 +0000679 while (DiagStr != DiagEnd) {
680 if (DiagStr[0] != '%') {
681 // Append non-%0 substrings to Str if we have one.
682 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
683 OutStr.append(DiagStr, StrEnd);
684 DiagStr = StrEnd;
Chris Lattner2b786902008-11-21 07:50:02 +0000685 continue;
John McCall8cb7a8a32010-01-14 20:11:39 +0000686 } else if (ispunct(DiagStr[1])) {
687 OutStr.push_back(DiagStr[1]); // %% -> %.
Chris Lattner23be0672008-11-19 06:51:40 +0000688 DiagStr += 2;
Chris Lattner2b786902008-11-21 07:50:02 +0000689 continue;
690 }
Mike Stump11289f42009-09-09 15:08:12 +0000691
Chris Lattner2b786902008-11-21 07:50:02 +0000692 // Skip the %.
693 ++DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000694
Chris Lattner2b786902008-11-21 07:50:02 +0000695 // This must be a placeholder for a diagnostic argument. The format for a
696 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
697 // The digit is a number from 0-9 indicating which argument this comes from.
698 // The modifier is a string of digits from the set [-a-z]+, arguments is a
699 // brace enclosed string.
700 const char *Modifier = 0, *Argument = 0;
701 unsigned ModifierLen = 0, ArgumentLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000702
Chris Lattner2b786902008-11-21 07:50:02 +0000703 // Check to see if we have a modifier. If so eat it.
704 if (!isdigit(DiagStr[0])) {
705 Modifier = DiagStr;
706 while (DiagStr[0] == '-' ||
707 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
708 ++DiagStr;
709 ModifierLen = DiagStr-Modifier;
Chris Lattner23be0672008-11-19 06:51:40 +0000710
Chris Lattner2b786902008-11-21 07:50:02 +0000711 // If we have an argument, get it next.
712 if (DiagStr[0] == '{') {
713 ++DiagStr; // Skip {.
714 Argument = DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000715
John McCall8cb7a8a32010-01-14 20:11:39 +0000716 DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
717 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
Chris Lattner2b786902008-11-21 07:50:02 +0000718 ArgumentLen = DiagStr-Argument;
719 ++DiagStr; // Skip }.
Chris Lattner23be0672008-11-19 06:51:40 +0000720 }
Chris Lattner2b786902008-11-21 07:50:02 +0000721 }
Mike Stump11289f42009-09-09 15:08:12 +0000722
Chris Lattner2b786902008-11-21 07:50:02 +0000723 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000724 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattner2b786902008-11-21 07:50:02 +0000725
Richard Trieu91844232012-06-26 18:18:47 +0000726 // Only used for type diffing.
727 unsigned ArgNo2 = ArgNo;
728
David Blaikie9c902b52011-09-25 23:23:43 +0000729 DiagnosticsEngine::ArgumentKind Kind = getArgKind(ArgNo);
Richard Trieu91844232012-06-26 18:18:47 +0000730 if (Kind == DiagnosticsEngine::ak_qualtype &&
731 ModifierIs(Modifier, ModifierLen, "diff")) {
732 Kind = DiagnosticsEngine::ak_qualtype_pair;
733 assert(*DiagStr == ',' && isdigit(*(DiagStr + 1)) &&
734 "Invalid format for diff modifier");
735 ++DiagStr; // Comma.
736 ArgNo2 = *DiagStr++ - '0';
737 assert(getArgKind(ArgNo2) == DiagnosticsEngine::ak_qualtype &&
738 "Second value of type diff must be a qualtype");
739 }
Chris Lattnerc243f292009-10-20 05:25:22 +0000740
741 switch (Kind) {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000742 // ---- STRINGS ----
David Blaikie9c902b52011-09-25 23:23:43 +0000743 case DiagnosticsEngine::ak_std_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000744 const std::string &S = getArgStdStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000745 assert(ModifierLen == 0 && "No modifiers for strings yet");
746 OutStr.append(S.begin(), S.end());
747 break;
748 }
David Blaikie9c902b52011-09-25 23:23:43 +0000749 case DiagnosticsEngine::ak_c_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000750 const char *S = getArgCStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000751 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000752
753 // Don't crash if get passed a null pointer by accident.
754 if (!S)
755 S = "(null)";
Mike Stump11289f42009-09-09 15:08:12 +0000756
Chris Lattner2b786902008-11-21 07:50:02 +0000757 OutStr.append(S, S + strlen(S));
758 break;
759 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000760 // ---- INTEGERS ----
David Blaikie9c902b52011-09-25 23:23:43 +0000761 case DiagnosticsEngine::ak_sint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000762 int Val = getArgSInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000763
Chris Lattner2b786902008-11-21 07:50:02 +0000764 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCall43b61682010-10-14 01:55:31 +0000765 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen,
766 OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000767 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
768 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000769 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCall43b61682010-10-14 01:55:31 +0000770 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
771 OutStr);
John McCall9015cde2010-01-14 00:50:32 +0000772 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
773 HandleOrdinalModifier((unsigned)Val, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000774 } else {
775 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000776 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000777 }
Chris Lattner2b786902008-11-21 07:50:02 +0000778 break;
779 }
David Blaikie9c902b52011-09-25 23:23:43 +0000780 case DiagnosticsEngine::ak_uint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000781 unsigned Val = getArgUInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000782
Chris Lattner2b786902008-11-21 07:50:02 +0000783 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCalle4d54322010-01-13 23:58:20 +0000784 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000785 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
786 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000787 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCall43b61682010-10-14 01:55:31 +0000788 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
789 OutStr);
John McCall9015cde2010-01-14 00:50:32 +0000790 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
791 HandleOrdinalModifier(Val, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000792 } else {
793 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000794 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000795 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000796 break;
Chris Lattner2b786902008-11-21 07:50:02 +0000797 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000798 // ---- NAMES and TYPES ----
David Blaikie9c902b52011-09-25 23:23:43 +0000799 case DiagnosticsEngine::ak_identifierinfo: {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000800 const IdentifierInfo *II = getArgIdentifier(ArgNo);
801 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000802
803 // Don't crash if get passed a null pointer by accident.
804 if (!II) {
805 const char *S = "(null)";
806 OutStr.append(S, S + strlen(S));
807 continue;
808 }
809
Daniel Dunbar07d07852009-10-18 21:17:35 +0000810 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
Chris Lattnere3d20d92008-11-23 21:45:46 +0000811 break;
812 }
David Blaikie9c902b52011-09-25 23:23:43 +0000813 case DiagnosticsEngine::ak_qualtype:
814 case DiagnosticsEngine::ak_declarationname:
815 case DiagnosticsEngine::ak_nameddecl:
816 case DiagnosticsEngine::ak_nestednamespec:
817 case DiagnosticsEngine::ak_declcontext:
Chris Lattnerc243f292009-10-20 05:25:22 +0000818 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
Chris Lattner63ecc502008-11-23 09:21:17 +0000819 Modifier, ModifierLen,
Chris Lattnerc243f292009-10-20 05:25:22 +0000820 Argument, ArgumentLen,
821 FormattedArgs.data(), FormattedArgs.size(),
Chandler Carruthd5173952011-07-11 17:49:21 +0000822 OutStr, QualTypeVals);
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000823 break;
Richard Trieu91844232012-06-26 18:18:47 +0000824 case DiagnosticsEngine::ak_qualtype_pair:
825 // Create a struct with all the info needed for printing.
826 TemplateDiffTypes TDT;
827 TDT.FromType = getRawArg(ArgNo);
828 TDT.ToType = getRawArg(ArgNo2);
829 TDT.ElideType = getDiags()->ElideType;
830 TDT.ShowColors = getDiags()->ShowColors;
Richard Trieu50f5f462012-07-10 01:46:04 +0000831 TDT.TemplateDiffUsed = false;
Richard Trieu91844232012-06-26 18:18:47 +0000832 intptr_t val = reinterpret_cast<intptr_t>(&TDT);
833
Richard Trieuc6058442012-06-29 21:12:16 +0000834 const char *ArgumentEnd = Argument + ArgumentLen;
835 const char *Pipe = ScanFormat(Argument, ArgumentEnd, '|');
836
Richard Trieua4056002012-07-13 21:18:32 +0000837 // Print the tree. If this diagnostic already has a tree, skip the
838 // second tree.
839 if (getDiags()->PrintTemplateTree && Tree.empty()) {
Richard Trieu91844232012-06-26 18:18:47 +0000840 TDT.PrintFromType = true;
841 TDT.PrintTree = true;
842 getDiags()->ConvertArgToString(Kind, val,
843 Modifier, ModifierLen,
844 Argument, ArgumentLen,
845 FormattedArgs.data(),
846 FormattedArgs.size(),
847 Tree, QualTypeVals);
848 // If there is no tree information, fall back to regular printing.
Richard Trieuc6058442012-06-29 21:12:16 +0000849 if (!Tree.empty()) {
850 FormatDiagnostic(Pipe + 1, ArgumentEnd, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +0000851 break;
Richard Trieuc6058442012-06-29 21:12:16 +0000852 }
Richard Trieu91844232012-06-26 18:18:47 +0000853 }
854
855 // Non-tree printing, also the fall-back when tree printing fails.
856 // The fall-back is triggered when the types compared are not templates.
Richard Trieuc6058442012-06-29 21:12:16 +0000857 const char *FirstDollar = ScanFormat(Argument, ArgumentEnd, '$');
858 const char *SecondDollar = ScanFormat(FirstDollar + 1, ArgumentEnd, '$');
Richard Trieu91844232012-06-26 18:18:47 +0000859
860 // Append before text
Richard Trieuc6058442012-06-29 21:12:16 +0000861 FormatDiagnostic(Argument, FirstDollar, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +0000862
863 // Append first type
864 TDT.PrintTree = false;
865 TDT.PrintFromType = true;
866 getDiags()->ConvertArgToString(Kind, val,
867 Modifier, ModifierLen,
868 Argument, ArgumentLen,
869 FormattedArgs.data(), FormattedArgs.size(),
870 OutStr, QualTypeVals);
Richard Trieu50f5f462012-07-10 01:46:04 +0000871 if (!TDT.TemplateDiffUsed)
872 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
873 TDT.FromType));
874
Richard Trieu91844232012-06-26 18:18:47 +0000875 // Append middle text
Richard Trieuc6058442012-06-29 21:12:16 +0000876 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +0000877
878 // Append second type
879 TDT.PrintFromType = false;
880 getDiags()->ConvertArgToString(Kind, val,
881 Modifier, ModifierLen,
882 Argument, ArgumentLen,
883 FormattedArgs.data(), FormattedArgs.size(),
884 OutStr, QualTypeVals);
Richard Trieu50f5f462012-07-10 01:46:04 +0000885 if (!TDT.TemplateDiffUsed)
886 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
887 TDT.ToType));
888
Richard Trieu91844232012-06-26 18:18:47 +0000889 // Append end text
Richard Trieuc6058442012-06-29 21:12:16 +0000890 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +0000891 break;
Nico Weber4c311642008-08-10 19:59:06 +0000892 }
Chris Lattnerc243f292009-10-20 05:25:22 +0000893
894 // Remember this argument info for subsequent formatting operations. Turn
895 // std::strings into a null terminated string to make it be the same case as
896 // all the other ones.
Richard Trieu91844232012-06-26 18:18:47 +0000897 if (Kind == DiagnosticsEngine::ak_qualtype_pair)
898 continue;
899 else if (Kind != DiagnosticsEngine::ak_std_string)
Chris Lattnerc243f292009-10-20 05:25:22 +0000900 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
901 else
David Blaikie9c902b52011-09-25 23:23:43 +0000902 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_c_string,
Chris Lattnerc243f292009-10-20 05:25:22 +0000903 (intptr_t)getArgStdStr(ArgNo).c_str()));
904
Nico Weber4c311642008-08-10 19:59:06 +0000905 }
Richard Trieu91844232012-06-26 18:18:47 +0000906
907 // Append the type tree to the end of the diagnostics.
908 OutStr.append(Tree.begin(), Tree.end());
Nico Weber4c311642008-08-10 19:59:06 +0000909}
Ted Kremenekea06ec12009-01-23 20:28:53 +0000910
Douglas Gregor33cdd812010-02-18 18:08:43 +0000911StoredDiagnostic::StoredDiagnostic() { }
912
David Blaikie9c902b52011-09-25 23:23:43 +0000913StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000914 StringRef Message)
Benjamin Kramer929bd682010-11-19 17:36:51 +0000915 : ID(ID), Level(Level), Loc(), Message(Message) { }
Douglas Gregor33cdd812010-02-18 18:08:43 +0000916
David Blaikie9c902b52011-09-25 23:23:43 +0000917StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level,
David Blaikieb5784322011-09-26 01:18:08 +0000918 const Diagnostic &Info)
Douglas Gregora750e8e2010-11-19 16:18:16 +0000919 : ID(Info.getID()), Level(Level)
920{
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000921 assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
922 "Valid source location without setting a source manager for diagnostic");
923 if (Info.getLocation().isValid())
924 Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000925 SmallString<64> Message;
Douglas Gregor33cdd812010-02-18 18:08:43 +0000926 Info.FormatDiagnostic(Message);
927 this->Message.assign(Message.begin(), Message.end());
928
929 Ranges.reserve(Info.getNumRanges());
930 for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I)
931 Ranges.push_back(Info.getRange(I));
932
Douglas Gregora771f462010-03-31 17:46:05 +0000933 FixIts.reserve(Info.getNumFixItHints());
934 for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I)
935 FixIts.push_back(Info.getFixItHint(I));
Douglas Gregor33cdd812010-02-18 18:08:43 +0000936}
937
David Blaikie9c902b52011-09-25 23:23:43 +0000938StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000939 StringRef Message, FullSourceLoc Loc,
Chris Lattner54b16772011-07-23 17:14:25 +0000940 ArrayRef<CharSourceRange> Ranges,
941 ArrayRef<FixItHint> Fixits)
Douglas Gregor925296b2011-07-19 16:10:42 +0000942 : ID(ID), Level(Level), Loc(Loc), Message(Message)
943{
944 this->Ranges.assign(Ranges.begin(), Ranges.end());
945 this->FixIts.assign(FixIts.begin(), FixIts.end());
946}
947
Douglas Gregor33cdd812010-02-18 18:08:43 +0000948StoredDiagnostic::~StoredDiagnostic() { }
949
Ted Kremenekea06ec12009-01-23 20:28:53 +0000950/// IncludeInDiagnosticCounts - This method (whose default implementation
951/// returns true) indicates whether the diagnostics handled by this
David Blaikiee2eefae2011-09-25 23:39:51 +0000952/// DiagnosticConsumer should be included in the number of diagnostics
David Blaikie9c902b52011-09-25 23:23:43 +0000953/// reported by DiagnosticsEngine.
David Blaikiee2eefae2011-09-25 23:39:51 +0000954bool DiagnosticConsumer::IncludeInDiagnosticCounts() const { return true; }
Douglas Gregor89336232010-03-29 23:34:08 +0000955
David Blaikie68e081d2011-12-20 02:48:34 +0000956void IgnoringDiagConsumer::anchor() { }
957
Benjamin Kramer7ec12c92012-02-07 22:29:24 +0000958PartialDiagnostic::StorageAllocator::StorageAllocator() {
Douglas Gregor89336232010-03-29 23:34:08 +0000959 for (unsigned I = 0; I != NumCached; ++I)
960 FreeList[I] = Cached + I;
961 NumFreeListEntries = NumCached;
962}
963
Benjamin Kramer7ec12c92012-02-07 22:29:24 +0000964PartialDiagnostic::StorageAllocator::~StorageAllocator() {
Chad Rosier849a67b2012-02-07 23:24:49 +0000965 // Don't assert if we are in a CrashRecovery context, as this invariant may
966 // be invalidated during a crash.
967 assert((NumFreeListEntries == NumCached ||
968 llvm::CrashRecoveryContext::isRecoveringFromCrash()) &&
969 "A partial is on the lamb");
Douglas Gregor89336232010-03-29 23:34:08 +0000970}