blob: f5eec02138baa0de5bee8cc72b4c0d97cb7cede8 [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"
Douglas Gregor811db4e2012-10-23 22:26:28 +000015#include "clang/Basic/DiagnosticOptions.h"
Chris Lattnerb91fd172008-11-19 07:32:16 +000016#include "clang/Basic/IdentifierTable.h"
Ted Kremenek39a76652010-04-12 19:54:17 +000017#include "clang/Basic/PartialDiagnostic.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000018#include "llvm/ADT/SmallString.h"
Jordan Rosec102b352012-09-22 01:24:42 +000019#include "llvm/ADT/StringExtras.h"
Ted Kremenek84de4a12011-03-21 18:40:07 +000020#include "llvm/Support/CrashRecoveryContext.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "llvm/Support/raw_ostream.h"
Joerg Sonnenberger42cf2682012-08-10 10:58:18 +000022#include <cctype>
Ted Kremenek84de4a12011-03-21 18:40:07 +000023
Chris Lattner22eb9722006-06-18 05:43:12 +000024using namespace clang;
25
David Blaikie9c902b52011-09-25 23:23:43 +000026static void DummyArgToStringFn(DiagnosticsEngine::ArgumentKind AK, intptr_t QT,
Chris Lattner63ecc502008-11-23 09:21:17 +000027 const char *Modifier, unsigned ML,
28 const char *Argument, unsigned ArgLen,
David Blaikie9c902b52011-09-25 23:23:43 +000029 const DiagnosticsEngine::ArgumentValue *PrevArgs,
Chris Lattnerc243f292009-10-20 05:25:22 +000030 unsigned NumPrevArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000031 SmallVectorImpl<char> &Output,
Chandler Carruthd5173952011-07-11 17:49:21 +000032 void *Cookie,
Bill Wendling8eb771d2012-02-22 09:51:33 +000033 ArrayRef<intptr_t> QualTypeVals) {
Chris Lattner63ecc502008-11-23 09:21:17 +000034 const char *Str = "<can't format argument>";
Chris Lattner6a2ed6f2008-11-23 09:13:29 +000035 Output.append(Str, Str+strlen(Str));
36}
37
38
David Blaikie9c902b52011-09-25 23:23:43 +000039DiagnosticsEngine::DiagnosticsEngine(
Dylan Noblesmithc95d8192012-02-20 14:00:23 +000040 const IntrusiveRefCntPtr<DiagnosticIDs> &diags,
Douglas Gregor811db4e2012-10-23 22:26:28 +000041 DiagnosticOptions *DiagOpts,
David Blaikiee2eefae2011-09-25 23:39:51 +000042 DiagnosticConsumer *client, bool ShouldOwnClient)
Douglas Gregor811db4e2012-10-23 22:26:28 +000043 : Diags(diags), DiagOpts(DiagOpts), Client(client),
44 OwnsDiagClient(ShouldOwnClient), SourceMgr(0) {
Chris Lattner63ecc502008-11-23 09:21:17 +000045 ArgToStringFn = DummyArgToStringFn;
Chris Lattnercf868c42009-02-19 23:53:20 +000046 ArgToStringCookie = 0;
Mike Stump11289f42009-09-09 15:08:12 +000047
Douglas Gregor0e119552010-07-31 00:40:00 +000048 AllExtensionsSilenced = 0;
49 IgnoreAllWarnings = false;
50 WarningsAsErrors = false;
Ted Kremenekfbbdced2011-08-18 01:12:56 +000051 EnableAllWarnings = false;
Douglas Gregor0e119552010-07-31 00:40:00 +000052 ErrorsAsFatal = false;
53 SuppressSystemWarnings = false;
54 SuppressAllDiagnostics = false;
Richard Trieu91844232012-06-26 18:18:47 +000055 ElideType = true;
56 PrintTemplateTree = false;
57 ShowColors = false;
Douglas Gregor0e119552010-07-31 00:40:00 +000058 ShowOverloads = Ovl_All;
59 ExtBehavior = Ext_Ignore;
60
61 ErrorLimit = 0;
62 TemplateBacktraceLimit = 0;
Richard Smithf6f003a2011-12-16 19:06:07 +000063 ConstexprBacktraceLimit = 0;
Douglas Gregor0e119552010-07-31 00:40:00 +000064
Douglas Gregoraa21cc42010-07-19 21:46:24 +000065 Reset();
Chris Lattnerae411572006-07-05 00:55:08 +000066}
67
David Blaikie9c902b52011-09-25 23:23:43 +000068DiagnosticsEngine::~DiagnosticsEngine() {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000069 if (OwnsDiagClient)
70 delete Client;
Chris Lattnere6535cf2007-12-02 01:09:57 +000071}
72
David Blaikiee2eefae2011-09-25 23:39:51 +000073void DiagnosticsEngine::setClient(DiagnosticConsumer *client,
David Blaikie9c902b52011-09-25 23:23:43 +000074 bool ShouldOwnClient) {
Douglas Gregor7a964ad2011-01-31 22:04:05 +000075 if (OwnsDiagClient && Client)
76 delete Client;
77
78 Client = client;
79 OwnsDiagClient = ShouldOwnClient;
80}
Chris Lattnerfb42a182009-07-12 21:18:45 +000081
David Blaikie9c902b52011-09-25 23:23:43 +000082void DiagnosticsEngine::pushMappings(SourceLocation Loc) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +000083 DiagStateOnPushStack.push_back(GetCurDiagState());
Chris Lattnerfb42a182009-07-12 21:18:45 +000084}
85
David Blaikie9c902b52011-09-25 23:23:43 +000086bool DiagnosticsEngine::popMappings(SourceLocation Loc) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +000087 if (DiagStateOnPushStack.empty())
Chris Lattnerfb42a182009-07-12 21:18:45 +000088 return false;
89
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +000090 if (DiagStateOnPushStack.back() != GetCurDiagState()) {
91 // State changed at some point between push/pop.
92 PushDiagStatePoint(DiagStateOnPushStack.back(), Loc);
93 }
94 DiagStateOnPushStack.pop_back();
Chris Lattnerfb42a182009-07-12 21:18:45 +000095 return true;
96}
97
David Blaikie9c902b52011-09-25 23:23:43 +000098void DiagnosticsEngine::Reset() {
Douglas Gregoraa21cc42010-07-19 21:46:24 +000099 ErrorOccurred = false;
DeLesley Hutchins8ecd4912012-12-07 22:53:48 +0000100 UncompilableErrorOccurred = false;
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000101 FatalErrorOccurred = false;
Douglas Gregor8a60bbe2011-07-06 17:40:26 +0000102 UnrecoverableErrorOccurred = false;
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000103
104 NumWarnings = 0;
105 NumErrors = 0;
106 NumErrorsSuppressed = 0;
Argyrios Kyrtzidis1fa8b4b2011-07-29 01:25:44 +0000107 TrapNumErrorsOccurred = 0;
108 TrapNumUnrecoverableErrorsOccurred = 0;
Douglas Gregor8a60bbe2011-07-06 17:40:26 +0000109
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000110 CurDiagID = ~0U;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000111 // Set LastDiagLevel to an "unset" state. If we set it to 'Ignored', notes
David Blaikie9c902b52011-09-25 23:23:43 +0000112 // using a DiagnosticsEngine associated to a translation unit that follow
113 // diagnostics from a DiagnosticsEngine associated to anoter t.u. will not be
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000114 // displayed.
115 LastDiagLevel = (DiagnosticIDs::Level)-1;
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000116 DelayedDiagID = 0;
Argyrios Kyrtzidisbbbeea12011-03-26 18:58:17 +0000117
118 // Clear state related to #pragma diagnostic.
119 DiagStates.clear();
120 DiagStatePoints.clear();
121 DiagStateOnPushStack.clear();
122
123 // Create a DiagState and DiagStatePoint representing diagnostic changes
124 // through command-line.
125 DiagStates.push_back(DiagState());
Richard Smithf995f2c2012-08-14 04:19:29 +0000126 DiagStatePoints.push_back(DiagStatePoint(&DiagStates.back(), FullSourceLoc()));
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000127}
Chris Lattner22eb9722006-06-18 05:43:12 +0000128
David Blaikie9c902b52011-09-25 23:23:43 +0000129void DiagnosticsEngine::SetDelayedDiagnostic(unsigned DiagID, StringRef Arg1,
Chad Rosier849a67b2012-02-07 23:24:49 +0000130 StringRef Arg2) {
Douglas Gregor85795312010-03-22 15:10:57 +0000131 if (DelayedDiagID)
132 return;
133
134 DelayedDiagID = DiagID;
Douglas Gregor96380982010-03-22 15:47:45 +0000135 DelayedDiagArg1 = Arg1.str();
136 DelayedDiagArg2 = Arg2.str();
Douglas Gregor85795312010-03-22 15:10:57 +0000137}
138
David Blaikie9c902b52011-09-25 23:23:43 +0000139void DiagnosticsEngine::ReportDelayed() {
Douglas Gregor85795312010-03-22 15:10:57 +0000140 Report(DelayedDiagID) << DelayedDiagArg1 << DelayedDiagArg2;
141 DelayedDiagID = 0;
142 DelayedDiagArg1.clear();
143 DelayedDiagArg2.clear();
144}
145
David Blaikie9c902b52011-09-25 23:23:43 +0000146DiagnosticsEngine::DiagStatePointsTy::iterator
147DiagnosticsEngine::GetDiagStatePointForLoc(SourceLocation L) const {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000148 assert(!DiagStatePoints.empty());
149 assert(DiagStatePoints.front().Loc.isInvalid() &&
150 "Should have created a DiagStatePoint for command-line");
151
Richard Smith99eff012012-08-17 00:55:32 +0000152 if (!SourceMgr)
153 return DiagStatePoints.end() - 1;
154
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000155 FullSourceLoc Loc(L, *SourceMgr);
156 if (Loc.isInvalid())
157 return DiagStatePoints.end() - 1;
158
159 DiagStatePointsTy::iterator Pos = DiagStatePoints.end();
160 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
161 if (LastStateChangePos.isValid() &&
162 Loc.isBeforeInTranslationUnitThan(LastStateChangePos))
163 Pos = std::upper_bound(DiagStatePoints.begin(), DiagStatePoints.end(),
164 DiagStatePoint(0, Loc));
165 --Pos;
166 return Pos;
167}
168
David Blaikie9c902b52011-09-25 23:23:43 +0000169void DiagnosticsEngine::setDiagnosticMapping(diag::kind Diag, diag::Mapping Map,
Chad Rosier849a67b2012-02-07 23:24:49 +0000170 SourceLocation L) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000171 assert(Diag < diag::DIAG_UPPER_LIMIT &&
172 "Can only map builtin diagnostics");
173 assert((Diags->isBuiltinWarningOrExtension(Diag) ||
174 (Map == diag::MAP_FATAL || Map == diag::MAP_ERROR)) &&
175 "Cannot map errors into warnings!");
176 assert(!DiagStatePoints.empty());
Richard Smith8a0527d2012-08-14 22:37:22 +0000177 assert((L.isInvalid() || SourceMgr) && "No SourceMgr for valid location");
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000178
Richard Smith8a0527d2012-08-14 22:37:22 +0000179 FullSourceLoc Loc = SourceMgr? FullSourceLoc(L, *SourceMgr) : FullSourceLoc();
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000180 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
Chad Rosierd1956e42012-02-03 01:49:51 +0000181 // Don't allow a mapping to a warning override an error/fatal mapping.
182 if (Map == diag::MAP_WARNING) {
183 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(Diag);
184 if (Info.getMapping() == diag::MAP_ERROR ||
185 Info.getMapping() == diag::MAP_FATAL)
186 Map = Info.getMapping();
187 }
Argyrios Kyrtzidisc137d0d2011-11-09 01:24:17 +0000188 DiagnosticMappingInfo MappingInfo = makeMappingInfo(Map, L);
Daniel Dunbar2fba0972011-10-04 21:17:24 +0000189
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000190 // Common case; setting all the diagnostics of a group in one place.
191 if (Loc.isInvalid() || Loc == LastStateChangePos) {
Daniel Dunbar458edfa2011-09-29 01:34:47 +0000192 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000193 return;
194 }
195
196 // Another common case; modifying diagnostic state in a source location
197 // after the previous one.
198 if ((Loc.isValid() && LastStateChangePos.isInvalid()) ||
199 LastStateChangePos.isBeforeInTranslationUnitThan(Loc)) {
Chris Lattner57540c52011-04-15 05:22:18 +0000200 // A diagnostic pragma occurred, create a new DiagState initialized with
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000201 // the current one and a new DiagStatePoint to record at which location
202 // the new state became active.
203 DiagStates.push_back(*GetCurDiagState());
204 PushDiagStatePoint(&DiagStates.back(), Loc);
Daniel Dunbar458edfa2011-09-29 01:34:47 +0000205 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000206 return;
207 }
208
209 // We allow setting the diagnostic state in random source order for
210 // completeness but it should not be actually happening in normal practice.
211
212 DiagStatePointsTy::iterator Pos = GetDiagStatePointForLoc(Loc);
213 assert(Pos != DiagStatePoints.end());
214
215 // Update all diagnostic states that are active after the given location.
216 for (DiagStatePointsTy::iterator
217 I = Pos+1, E = DiagStatePoints.end(); I != E; ++I) {
Daniel Dunbar458edfa2011-09-29 01:34:47 +0000218 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000219 }
220
221 // If the location corresponds to an existing point, just update its state.
222 if (Pos->Loc == Loc) {
Daniel Dunbar458edfa2011-09-29 01:34:47 +0000223 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000224 return;
225 }
226
227 // Create a new state/point and fit it into the vector of DiagStatePoints
228 // so that the vector is always ordered according to location.
229 Pos->Loc.isBeforeInTranslationUnitThan(Loc);
230 DiagStates.push_back(*Pos->State);
231 DiagState *NewState = &DiagStates.back();
Daniel Dunbar458edfa2011-09-29 01:34:47 +0000232 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000233 DiagStatePoints.insert(Pos+1, DiagStatePoint(NewState,
234 FullSourceLoc(Loc, *SourceMgr)));
235}
236
Daniel Dunbard908c122011-09-29 01:47:16 +0000237bool DiagnosticsEngine::setDiagnosticGroupMapping(
238 StringRef Group, diag::Mapping Map, SourceLocation Loc)
239{
240 // Get the diagnostics in this group.
241 llvm::SmallVector<diag::kind, 8> GroupDiags;
242 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
243 return true;
244
245 // Set the mapping.
246 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i)
247 setDiagnosticMapping(GroupDiags[i], Map, Loc);
248
249 return false;
250}
251
Chad Rosier4c17fa72012-02-07 19:55:45 +0000252void DiagnosticsEngine::setDiagnosticWarningAsError(diag::kind Diag,
253 bool Enabled) {
254 // If we are enabling this feature, just set the diagnostic mappings to map to
255 // errors.
256 if (Enabled)
257 setDiagnosticMapping(Diag, diag::MAP_ERROR, SourceLocation());
258
259 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
260 // potentially downgrade anything already mapped to be a warning.
261 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(Diag);
262
263 if (Info.getMapping() == diag::MAP_ERROR ||
264 Info.getMapping() == diag::MAP_FATAL)
265 Info.setMapping(diag::MAP_WARNING);
266
267 Info.setNoWarningAsError(true);
268}
269
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000270bool DiagnosticsEngine::setDiagnosticGroupWarningAsError(StringRef Group,
271 bool Enabled) {
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000272 // If we are enabling this feature, just set the diagnostic mappings to map to
273 // errors.
274 if (Enabled)
275 return setDiagnosticGroupMapping(Group, diag::MAP_ERROR);
276
277 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
278 // potentially downgrade anything already mapped to be a warning.
279
280 // Get the diagnostics in this group.
281 llvm::SmallVector<diag::kind, 8> GroupDiags;
282 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
283 return true;
284
285 // Perform the mapping change.
286 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i) {
287 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(
288 GroupDiags[i]);
289
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000290 if (Info.getMapping() == diag::MAP_ERROR ||
291 Info.getMapping() == diag::MAP_FATAL)
292 Info.setMapping(diag::MAP_WARNING);
293
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000294 Info.setNoWarningAsError(true);
295 }
296
297 return false;
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000298}
299
Chad Rosier4c17fa72012-02-07 19:55:45 +0000300void DiagnosticsEngine::setDiagnosticErrorAsFatal(diag::kind Diag,
301 bool Enabled) {
302 // If we are enabling this feature, just set the diagnostic mappings to map to
303 // errors.
304 if (Enabled)
305 setDiagnosticMapping(Diag, diag::MAP_FATAL, SourceLocation());
306
307 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
308 // potentially downgrade anything already mapped to be a warning.
309 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(Diag);
310
311 if (Info.getMapping() == diag::MAP_FATAL)
312 Info.setMapping(diag::MAP_ERROR);
313
314 Info.setNoErrorAsFatal(true);
315}
316
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000317bool DiagnosticsEngine::setDiagnosticGroupErrorAsFatal(StringRef Group,
318 bool Enabled) {
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000319 // If we are enabling this feature, just set the diagnostic mappings to map to
320 // fatal errors.
321 if (Enabled)
322 return setDiagnosticGroupMapping(Group, diag::MAP_FATAL);
323
324 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
325 // potentially downgrade anything already mapped to be an error.
326
327 // Get the diagnostics in this group.
328 llvm::SmallVector<diag::kind, 8> GroupDiags;
329 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
330 return true;
331
332 // Perform the mapping change.
333 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i) {
334 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(
335 GroupDiags[i]);
336
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000337 if (Info.getMapping() == diag::MAP_FATAL)
338 Info.setMapping(diag::MAP_ERROR);
339
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000340 Info.setNoErrorAsFatal(true);
341 }
342
343 return false;
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000344}
345
Argyrios Kyrtzidis059cac42012-01-28 04:35:52 +0000346void DiagnosticsEngine::setMappingToAllDiagnostics(diag::Mapping Map,
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000347 SourceLocation Loc) {
348 // Get all the diagnostics.
349 llvm::SmallVector<diag::kind, 64> AllDiags;
350 Diags->getAllDiagnostics(AllDiags);
351
352 // Set the mapping.
353 for (unsigned i = 0, e = AllDiags.size(); i != e; ++i)
354 if (Diags->isBuiltinWarningOrExtension(AllDiags[i]))
355 setDiagnosticMapping(AllDiags[i], Map, Loc);
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000356}
357
David Blaikie9c902b52011-09-25 23:23:43 +0000358void DiagnosticsEngine::Report(const StoredDiagnostic &storedDiag) {
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000359 assert(CurDiagID == ~0U && "Multiple diagnostics in flight at once!");
360
361 CurDiagLoc = storedDiag.getLocation();
362 CurDiagID = storedDiag.getID();
363 NumDiagArgs = 0;
364
365 NumDiagRanges = storedDiag.range_size();
Daniel Dunbar007b9dc2012-03-13 18:21:17 +0000366 assert(NumDiagRanges < DiagnosticsEngine::MaxRanges &&
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000367 "Too many arguments to diagnostic!");
368 unsigned i = 0;
369 for (StoredDiagnostic::range_iterator
370 RI = storedDiag.range_begin(),
371 RE = storedDiag.range_end(); RI != RE; ++RI)
372 DiagRanges[i++] = *RI;
373
Daniel Dunbar007b9dc2012-03-13 18:21:17 +0000374 assert(NumDiagRanges < DiagnosticsEngine::MaxFixItHints &&
375 "Too many arguments to diagnostic!");
376 NumDiagFixItHints = 0;
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000377 for (StoredDiagnostic::fixit_iterator
378 FI = storedDiag.fixit_begin(),
379 FE = storedDiag.fixit_end(); FI != FE; ++FI)
Daniel Dunbar007b9dc2012-03-13 18:21:17 +0000380 DiagFixItHints[NumDiagFixItHints++] = *FI;
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000381
David Blaikiee2eefae2011-09-25 23:39:51 +0000382 assert(Client && "DiagnosticConsumer not set!");
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000383 Level DiagLevel = storedDiag.getLevel();
David Blaikieb5784322011-09-26 01:18:08 +0000384 Diagnostic Info(this, storedDiag.getMessage());
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000385 Client->HandleDiagnostic(DiagLevel, Info);
386 if (Client->IncludeInDiagnosticCounts()) {
David Blaikie9c902b52011-09-25 23:23:43 +0000387 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000388 ++NumWarnings;
389 }
390
391 CurDiagID = ~0U;
392}
393
Jordan Rose6f524ac2012-07-11 16:50:36 +0000394bool DiagnosticsEngine::EmitCurrentDiagnostic(bool Force) {
395 assert(getClient() && "DiagnosticClient not set!");
396
397 bool Emitted;
398 if (Force) {
399 Diagnostic Info(this);
400
401 // Figure out the diagnostic level of this message.
402 DiagnosticIDs::Level DiagLevel
403 = Diags->getDiagnosticLevel(Info.getID(), Info.getLocation(), *this);
404
405 Emitted = (DiagLevel != DiagnosticIDs::Ignored);
406 if (Emitted) {
407 // Emit the diagnostic regardless of suppression level.
408 Diags->EmitDiag(*this, DiagLevel);
409 }
410 } else {
411 // Process the diagnostic, sending the accumulated information to the
412 // DiagnosticConsumer.
413 Emitted = ProcessDiag();
414 }
Douglas Gregor85795312010-03-22 15:10:57 +0000415
416 // Clear out the current diagnostic object.
Daniel Dunbarc7c00892012-03-13 21:02:14 +0000417 unsigned DiagID = CurDiagID;
418 Clear();
Douglas Gregor85795312010-03-22 15:10:57 +0000419
420 // If there was a delayed diagnostic, emit it now.
Jordan Rose6f524ac2012-07-11 16:50:36 +0000421 if (!Force && DelayedDiagID && DelayedDiagID != DiagID)
Daniel Dunbarc7c00892012-03-13 21:02:14 +0000422 ReportDelayed();
Douglas Gregor85795312010-03-22 15:10:57 +0000423
424 return Emitted;
425}
426
Nico Weber4c311642008-08-10 19:59:06 +0000427
David Blaikiee2eefae2011-09-25 23:39:51 +0000428DiagnosticConsumer::~DiagnosticConsumer() {}
Nico Weber4c311642008-08-10 19:59:06 +0000429
David Blaikiee2eefae2011-09-25 23:39:51 +0000430void DiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
David Blaikieb5784322011-09-26 01:18:08 +0000431 const Diagnostic &Info) {
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000432 if (!IncludeInDiagnosticCounts())
433 return;
434
David Blaikie9c902b52011-09-25 23:23:43 +0000435 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000436 ++NumWarnings;
David Blaikie9c902b52011-09-25 23:23:43 +0000437 else if (DiagLevel >= DiagnosticsEngine::Error)
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000438 ++NumErrors;
439}
Chris Lattner23be0672008-11-19 06:51:40 +0000440
Chris Lattner2b786902008-11-21 07:50:02 +0000441/// ModifierIs - Return true if the specified modifier matches specified string.
442template <std::size_t StrLen>
443static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
444 const char (&Str)[StrLen]) {
445 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
446}
447
John McCall8cb7a8a32010-01-14 20:11:39 +0000448/// ScanForward - Scans forward, looking for the given character, skipping
449/// nested clauses and escaped characters.
450static const char *ScanFormat(const char *I, const char *E, char Target) {
451 unsigned Depth = 0;
452
453 for ( ; I != E; ++I) {
454 if (Depth == 0 && *I == Target) return I;
455 if (Depth != 0 && *I == '}') Depth--;
456
457 if (*I == '%') {
458 I++;
459 if (I == E) break;
460
461 // Escaped characters get implicitly skipped here.
462
463 // Format specifier.
464 if (!isdigit(*I) && !ispunct(*I)) {
465 for (I++; I != E && !isdigit(*I) && *I != '{'; I++) ;
466 if (I == E) break;
467 if (*I == '{')
468 Depth++;
469 }
470 }
471 }
472 return E;
473}
474
Chris Lattner2b786902008-11-21 07:50:02 +0000475/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
476/// like this: %select{foo|bar|baz}2. This means that the integer argument
477/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
478/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
479/// This is very useful for certain classes of variant diagnostics.
David Blaikieb5784322011-09-26 01:18:08 +0000480static void HandleSelectModifier(const Diagnostic &DInfo, unsigned ValNo,
Chris Lattner2b786902008-11-21 07:50:02 +0000481 const char *Argument, unsigned ArgumentLen,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000482 SmallVectorImpl<char> &OutStr) {
Chris Lattner2b786902008-11-21 07:50:02 +0000483 const char *ArgumentEnd = Argument+ArgumentLen;
Mike Stump11289f42009-09-09 15:08:12 +0000484
Chris Lattner2b786902008-11-21 07:50:02 +0000485 // Skip over 'ValNo' |'s.
486 while (ValNo) {
John McCall8cb7a8a32010-01-14 20:11:39 +0000487 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
Chris Lattner2b786902008-11-21 07:50:02 +0000488 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
489 " larger than the number of options in the diagnostic string!");
490 Argument = NextVal+1; // Skip this string.
491 --ValNo;
492 }
Mike Stump11289f42009-09-09 15:08:12 +0000493
Chris Lattner2b786902008-11-21 07:50:02 +0000494 // Get the end of the value. This is either the } or the |.
John McCall8cb7a8a32010-01-14 20:11:39 +0000495 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
John McCalle4d54322010-01-13 23:58:20 +0000496
497 // Recursively format the result of the select clause into the output string.
498 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000499}
500
501/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
502/// letter 's' to the string if the value is not 1. This is used in cases like
503/// this: "you idiot, you have %4 parameter%s4!".
504static void HandleIntegerSModifier(unsigned ValNo,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000505 SmallVectorImpl<char> &OutStr) {
Chris Lattner2b786902008-11-21 07:50:02 +0000506 if (ValNo != 1)
507 OutStr.push_back('s');
508}
509
John McCall9015cde2010-01-14 00:50:32 +0000510/// HandleOrdinalModifier - Handle the integer 'ord' modifier. This
511/// prints the ordinal form of the given integer, with 1 corresponding
512/// to the first ordinal. Currently this is hard-coded to use the
513/// English form.
514static void HandleOrdinalModifier(unsigned ValNo,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000515 SmallVectorImpl<char> &OutStr) {
John McCall9015cde2010-01-14 00:50:32 +0000516 assert(ValNo != 0 && "ValNo must be strictly positive!");
517
518 llvm::raw_svector_ostream Out(OutStr);
519
520 // We could use text forms for the first N ordinals, but the numeric
521 // forms are actually nicer in diagnostics because they stand out.
Jordan Rosec102b352012-09-22 01:24:42 +0000522 Out << ValNo << llvm::getOrdinalSuffix(ValNo);
John McCall9015cde2010-01-14 00:50:32 +0000523}
524
Chris Lattner2b786902008-11-21 07:50:02 +0000525
Sebastian Redl15b02d22008-11-22 13:44:36 +0000526/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000527static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000528 // Programming 101: Parse a decimal number :-)
529 unsigned Val = 0;
530 while (Start != End && *Start >= '0' && *Start <= '9') {
531 Val *= 10;
532 Val += *Start - '0';
533 ++Start;
534 }
535 return Val;
536}
537
538/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000539static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000540 if (*Start != '[') {
541 unsigned Ref = PluralNumber(Start, End);
542 return Ref == Val;
543 }
544
545 ++Start;
546 unsigned Low = PluralNumber(Start, End);
547 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
548 ++Start;
549 unsigned High = PluralNumber(Start, End);
550 assert(*Start == ']' && "Bad plural expression syntax: expected )");
551 ++Start;
552 return Low <= Val && Val <= High;
553}
554
555/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattner2fe29202009-04-15 17:13:42 +0000556static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000557 // Empty condition?
558 if (*Start == ':')
559 return true;
560
561 while (1) {
562 char C = *Start;
563 if (C == '%') {
564 // Modulo expression
565 ++Start;
566 unsigned Arg = PluralNumber(Start, End);
567 assert(*Start == '=' && "Bad plural expression syntax: expected =");
568 ++Start;
569 unsigned ValMod = ValNo % Arg;
570 if (TestPluralRange(ValMod, Start, End))
571 return true;
572 } else {
Sebastian Redl3ceaf622008-11-27 07:28:14 +0000573 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redl15b02d22008-11-22 13:44:36 +0000574 "Bad plural expression syntax: unexpected character");
575 // Range expression
576 if (TestPluralRange(ValNo, Start, End))
577 return true;
578 }
579
580 // Scan for next or-expr part.
581 Start = std::find(Start, End, ',');
Mike Stump11289f42009-09-09 15:08:12 +0000582 if (Start == End)
Sebastian Redl15b02d22008-11-22 13:44:36 +0000583 break;
584 ++Start;
585 }
586 return false;
587}
588
589/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
590/// for complex plural forms, or in languages where all plurals are complex.
591/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
592/// conditions that are tested in order, the form corresponding to the first
593/// that applies being emitted. The empty condition is always true, making the
594/// last form a default case.
595/// Conditions are simple boolean expressions, where n is the number argument.
596/// Here are the rules.
597/// condition := expression | empty
598/// empty := -> always true
599/// expression := numeric [',' expression] -> logical or
600/// numeric := range -> true if n in range
601/// | '%' number '=' range -> true if n % number in range
602/// range := number
603/// | '[' number ',' number ']' -> ranges are inclusive both ends
604///
605/// Here are some examples from the GNU gettext manual written in this form:
606/// English:
607/// {1:form0|:form1}
608/// Latvian:
609/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
610/// Gaeilge:
611/// {1:form0|2:form1|:form2}
612/// Romanian:
613/// {1:form0|0,%100=[1,19]:form1|:form2}
614/// Lithuanian:
615/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
616/// Russian (requires repeated form):
617/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
618/// Slovak
619/// {1:form0|[2,4]:form1|:form2}
620/// Polish (requires repeated form):
621/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
David Blaikieb5784322011-09-26 01:18:08 +0000622static void HandlePluralModifier(const Diagnostic &DInfo, unsigned ValNo,
Sebastian Redl15b02d22008-11-22 13:44:36 +0000623 const char *Argument, unsigned ArgumentLen,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000624 SmallVectorImpl<char> &OutStr) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000625 const char *ArgumentEnd = Argument + ArgumentLen;
626 while (1) {
627 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
628 const char *ExprEnd = Argument;
629 while (*ExprEnd != ':') {
630 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
631 ++ExprEnd;
632 }
633 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
634 Argument = ExprEnd + 1;
John McCall8cb7a8a32010-01-14 20:11:39 +0000635 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
John McCall43b61682010-10-14 01:55:31 +0000636
637 // Recursively format the result of the plural clause into the
638 // output string.
639 DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000640 return;
641 }
John McCall8cb7a8a32010-01-14 20:11:39 +0000642 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
Sebastian Redl15b02d22008-11-22 13:44:36 +0000643 }
644}
645
646
Chris Lattner23be0672008-11-19 06:51:40 +0000647/// FormatDiagnostic - Format this diagnostic into a string, substituting the
648/// formal arguments into the %0 slots. The result is appended onto the Str
649/// array.
David Blaikieb5784322011-09-26 01:18:08 +0000650void Diagnostic::
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000651FormatDiagnostic(SmallVectorImpl<char> &OutStr) const {
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000652 if (!StoredDiagMessage.empty()) {
653 OutStr.append(StoredDiagMessage.begin(), StoredDiagMessage.end());
654 return;
655 }
656
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000657 StringRef Diag =
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000658 getDiags()->getDiagnosticIDs()->getDescription(getID());
Mike Stump11289f42009-09-09 15:08:12 +0000659
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000660 FormatDiagnostic(Diag.begin(), Diag.end(), OutStr);
John McCalle4d54322010-01-13 23:58:20 +0000661}
662
David Blaikieb5784322011-09-26 01:18:08 +0000663void Diagnostic::
John McCalle4d54322010-01-13 23:58:20 +0000664FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000665 SmallVectorImpl<char> &OutStr) const {
John McCalle4d54322010-01-13 23:58:20 +0000666
Chris Lattnerc243f292009-10-20 05:25:22 +0000667 /// FormattedArgs - Keep track of all of the arguments formatted by
668 /// ConvertArgToString and pass them into subsequent calls to
669 /// ConvertArgToString, allowing the implementation to avoid redundancies in
670 /// obvious cases.
David Blaikie9c902b52011-09-25 23:23:43 +0000671 SmallVector<DiagnosticsEngine::ArgumentValue, 8> FormattedArgs;
Chandler Carruthd5173952011-07-11 17:49:21 +0000672
673 /// QualTypeVals - Pass a vector of arrays so that QualType names can be
674 /// compared to see if more information is needed to be printed.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000675 SmallVector<intptr_t, 2> QualTypeVals;
Richard Trieu91844232012-06-26 18:18:47 +0000676 SmallVector<char, 64> Tree;
677
Chandler Carruthd5173952011-07-11 17:49:21 +0000678 for (unsigned i = 0, e = getNumArgs(); i < e; ++i)
David Blaikie9c902b52011-09-25 23:23:43 +0000679 if (getArgKind(i) == DiagnosticsEngine::ak_qualtype)
Chandler Carruthd5173952011-07-11 17:49:21 +0000680 QualTypeVals.push_back(getRawArg(i));
681
Chris Lattner23be0672008-11-19 06:51:40 +0000682 while (DiagStr != DiagEnd) {
683 if (DiagStr[0] != '%') {
684 // Append non-%0 substrings to Str if we have one.
685 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
686 OutStr.append(DiagStr, StrEnd);
687 DiagStr = StrEnd;
Chris Lattner2b786902008-11-21 07:50:02 +0000688 continue;
John McCall8cb7a8a32010-01-14 20:11:39 +0000689 } else if (ispunct(DiagStr[1])) {
690 OutStr.push_back(DiagStr[1]); // %% -> %.
Chris Lattner23be0672008-11-19 06:51:40 +0000691 DiagStr += 2;
Chris Lattner2b786902008-11-21 07:50:02 +0000692 continue;
693 }
Mike Stump11289f42009-09-09 15:08:12 +0000694
Chris Lattner2b786902008-11-21 07:50:02 +0000695 // Skip the %.
696 ++DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000697
Chris Lattner2b786902008-11-21 07:50:02 +0000698 // This must be a placeholder for a diagnostic argument. The format for a
699 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
700 // The digit is a number from 0-9 indicating which argument this comes from.
701 // The modifier is a string of digits from the set [-a-z]+, arguments is a
702 // brace enclosed string.
703 const char *Modifier = 0, *Argument = 0;
704 unsigned ModifierLen = 0, ArgumentLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000705
Chris Lattner2b786902008-11-21 07:50:02 +0000706 // Check to see if we have a modifier. If so eat it.
707 if (!isdigit(DiagStr[0])) {
708 Modifier = DiagStr;
709 while (DiagStr[0] == '-' ||
710 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
711 ++DiagStr;
712 ModifierLen = DiagStr-Modifier;
Chris Lattner23be0672008-11-19 06:51:40 +0000713
Chris Lattner2b786902008-11-21 07:50:02 +0000714 // If we have an argument, get it next.
715 if (DiagStr[0] == '{') {
716 ++DiagStr; // Skip {.
717 Argument = DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000718
John McCall8cb7a8a32010-01-14 20:11:39 +0000719 DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
720 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
Chris Lattner2b786902008-11-21 07:50:02 +0000721 ArgumentLen = DiagStr-Argument;
722 ++DiagStr; // Skip }.
Chris Lattner23be0672008-11-19 06:51:40 +0000723 }
Chris Lattner2b786902008-11-21 07:50:02 +0000724 }
Mike Stump11289f42009-09-09 15:08:12 +0000725
Chris Lattner2b786902008-11-21 07:50:02 +0000726 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000727 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattner2b786902008-11-21 07:50:02 +0000728
Richard Trieu91844232012-06-26 18:18:47 +0000729 // Only used for type diffing.
730 unsigned ArgNo2 = ArgNo;
731
David Blaikie9c902b52011-09-25 23:23:43 +0000732 DiagnosticsEngine::ArgumentKind Kind = getArgKind(ArgNo);
Richard Trieu91844232012-06-26 18:18:47 +0000733 if (Kind == DiagnosticsEngine::ak_qualtype &&
734 ModifierIs(Modifier, ModifierLen, "diff")) {
735 Kind = DiagnosticsEngine::ak_qualtype_pair;
736 assert(*DiagStr == ',' && isdigit(*(DiagStr + 1)) &&
737 "Invalid format for diff modifier");
738 ++DiagStr; // Comma.
739 ArgNo2 = *DiagStr++ - '0';
740 assert(getArgKind(ArgNo2) == DiagnosticsEngine::ak_qualtype &&
741 "Second value of type diff must be a qualtype");
742 }
Chris Lattnerc243f292009-10-20 05:25:22 +0000743
744 switch (Kind) {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000745 // ---- STRINGS ----
David Blaikie9c902b52011-09-25 23:23:43 +0000746 case DiagnosticsEngine::ak_std_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000747 const std::string &S = getArgStdStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000748 assert(ModifierLen == 0 && "No modifiers for strings yet");
749 OutStr.append(S.begin(), S.end());
750 break;
751 }
David Blaikie9c902b52011-09-25 23:23:43 +0000752 case DiagnosticsEngine::ak_c_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000753 const char *S = getArgCStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000754 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000755
756 // Don't crash if get passed a null pointer by accident.
757 if (!S)
758 S = "(null)";
Mike Stump11289f42009-09-09 15:08:12 +0000759
Chris Lattner2b786902008-11-21 07:50:02 +0000760 OutStr.append(S, S + strlen(S));
761 break;
762 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000763 // ---- INTEGERS ----
David Blaikie9c902b52011-09-25 23:23:43 +0000764 case DiagnosticsEngine::ak_sint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000765 int Val = getArgSInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000766
Chris Lattner2b786902008-11-21 07:50:02 +0000767 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCall43b61682010-10-14 01:55:31 +0000768 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen,
769 OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000770 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
771 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000772 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCall43b61682010-10-14 01:55:31 +0000773 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
774 OutStr);
John McCall9015cde2010-01-14 00:50:32 +0000775 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
776 HandleOrdinalModifier((unsigned)Val, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000777 } else {
778 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000779 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000780 }
Chris Lattner2b786902008-11-21 07:50:02 +0000781 break;
782 }
David Blaikie9c902b52011-09-25 23:23:43 +0000783 case DiagnosticsEngine::ak_uint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000784 unsigned Val = getArgUInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000785
Chris Lattner2b786902008-11-21 07:50:02 +0000786 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCalle4d54322010-01-13 23:58:20 +0000787 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000788 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
789 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000790 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCall43b61682010-10-14 01:55:31 +0000791 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
792 OutStr);
John McCall9015cde2010-01-14 00:50:32 +0000793 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
794 HandleOrdinalModifier(Val, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000795 } else {
796 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000797 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000798 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000799 break;
Chris Lattner2b786902008-11-21 07:50:02 +0000800 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000801 // ---- NAMES and TYPES ----
David Blaikie9c902b52011-09-25 23:23:43 +0000802 case DiagnosticsEngine::ak_identifierinfo: {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000803 const IdentifierInfo *II = getArgIdentifier(ArgNo);
804 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000805
806 // Don't crash if get passed a null pointer by accident.
807 if (!II) {
808 const char *S = "(null)";
809 OutStr.append(S, S + strlen(S));
810 continue;
811 }
812
Daniel Dunbar07d07852009-10-18 21:17:35 +0000813 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
Chris Lattnere3d20d92008-11-23 21:45:46 +0000814 break;
815 }
David Blaikie9c902b52011-09-25 23:23:43 +0000816 case DiagnosticsEngine::ak_qualtype:
817 case DiagnosticsEngine::ak_declarationname:
818 case DiagnosticsEngine::ak_nameddecl:
819 case DiagnosticsEngine::ak_nestednamespec:
820 case DiagnosticsEngine::ak_declcontext:
Chris Lattnerc243f292009-10-20 05:25:22 +0000821 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
Chris Lattner63ecc502008-11-23 09:21:17 +0000822 Modifier, ModifierLen,
Chris Lattnerc243f292009-10-20 05:25:22 +0000823 Argument, ArgumentLen,
824 FormattedArgs.data(), FormattedArgs.size(),
Chandler Carruthd5173952011-07-11 17:49:21 +0000825 OutStr, QualTypeVals);
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000826 break;
Richard Trieu91844232012-06-26 18:18:47 +0000827 case DiagnosticsEngine::ak_qualtype_pair:
828 // Create a struct with all the info needed for printing.
829 TemplateDiffTypes TDT;
830 TDT.FromType = getRawArg(ArgNo);
831 TDT.ToType = getRawArg(ArgNo2);
832 TDT.ElideType = getDiags()->ElideType;
833 TDT.ShowColors = getDiags()->ShowColors;
Richard Trieu50f5f462012-07-10 01:46:04 +0000834 TDT.TemplateDiffUsed = false;
Richard Trieu91844232012-06-26 18:18:47 +0000835 intptr_t val = reinterpret_cast<intptr_t>(&TDT);
836
Richard Trieuc6058442012-06-29 21:12:16 +0000837 const char *ArgumentEnd = Argument + ArgumentLen;
838 const char *Pipe = ScanFormat(Argument, ArgumentEnd, '|');
839
Richard Trieua4056002012-07-13 21:18:32 +0000840 // Print the tree. If this diagnostic already has a tree, skip the
841 // second tree.
842 if (getDiags()->PrintTemplateTree && Tree.empty()) {
Richard Trieu91844232012-06-26 18:18:47 +0000843 TDT.PrintFromType = true;
844 TDT.PrintTree = true;
845 getDiags()->ConvertArgToString(Kind, val,
846 Modifier, ModifierLen,
847 Argument, ArgumentLen,
848 FormattedArgs.data(),
849 FormattedArgs.size(),
850 Tree, QualTypeVals);
851 // If there is no tree information, fall back to regular printing.
Richard Trieuc6058442012-06-29 21:12:16 +0000852 if (!Tree.empty()) {
853 FormatDiagnostic(Pipe + 1, ArgumentEnd, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +0000854 break;
Richard Trieuc6058442012-06-29 21:12:16 +0000855 }
Richard Trieu91844232012-06-26 18:18:47 +0000856 }
857
858 // Non-tree printing, also the fall-back when tree printing fails.
859 // The fall-back is triggered when the types compared are not templates.
Richard Trieuc6058442012-06-29 21:12:16 +0000860 const char *FirstDollar = ScanFormat(Argument, ArgumentEnd, '$');
861 const char *SecondDollar = ScanFormat(FirstDollar + 1, ArgumentEnd, '$');
Richard Trieu91844232012-06-26 18:18:47 +0000862
863 // Append before text
Richard Trieuc6058442012-06-29 21:12:16 +0000864 FormatDiagnostic(Argument, FirstDollar, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +0000865
866 // Append first type
867 TDT.PrintTree = false;
868 TDT.PrintFromType = true;
869 getDiags()->ConvertArgToString(Kind, val,
870 Modifier, ModifierLen,
871 Argument, ArgumentLen,
872 FormattedArgs.data(), FormattedArgs.size(),
873 OutStr, QualTypeVals);
Richard Trieu50f5f462012-07-10 01:46:04 +0000874 if (!TDT.TemplateDiffUsed)
875 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
876 TDT.FromType));
877
Richard Trieu91844232012-06-26 18:18:47 +0000878 // Append middle text
Richard Trieuc6058442012-06-29 21:12:16 +0000879 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +0000880
881 // Append second type
882 TDT.PrintFromType = false;
883 getDiags()->ConvertArgToString(Kind, val,
884 Modifier, ModifierLen,
885 Argument, ArgumentLen,
886 FormattedArgs.data(), FormattedArgs.size(),
887 OutStr, QualTypeVals);
Richard Trieu50f5f462012-07-10 01:46:04 +0000888 if (!TDT.TemplateDiffUsed)
889 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
890 TDT.ToType));
891
Richard Trieu91844232012-06-26 18:18:47 +0000892 // Append end text
Richard Trieuc6058442012-06-29 21:12:16 +0000893 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +0000894 break;
Nico Weber4c311642008-08-10 19:59:06 +0000895 }
Chris Lattnerc243f292009-10-20 05:25:22 +0000896
897 // Remember this argument info for subsequent formatting operations. Turn
898 // std::strings into a null terminated string to make it be the same case as
899 // all the other ones.
Richard Trieu91844232012-06-26 18:18:47 +0000900 if (Kind == DiagnosticsEngine::ak_qualtype_pair)
901 continue;
902 else if (Kind != DiagnosticsEngine::ak_std_string)
Chris Lattnerc243f292009-10-20 05:25:22 +0000903 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
904 else
David Blaikie9c902b52011-09-25 23:23:43 +0000905 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_c_string,
Chris Lattnerc243f292009-10-20 05:25:22 +0000906 (intptr_t)getArgStdStr(ArgNo).c_str()));
907
Nico Weber4c311642008-08-10 19:59:06 +0000908 }
Richard Trieu91844232012-06-26 18:18:47 +0000909
910 // Append the type tree to the end of the diagnostics.
911 OutStr.append(Tree.begin(), Tree.end());
Nico Weber4c311642008-08-10 19:59:06 +0000912}
Ted Kremenekea06ec12009-01-23 20:28:53 +0000913
Douglas Gregor33cdd812010-02-18 18:08:43 +0000914StoredDiagnostic::StoredDiagnostic() { }
915
David Blaikie9c902b52011-09-25 23:23:43 +0000916StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000917 StringRef Message)
Benjamin Kramer929bd682010-11-19 17:36:51 +0000918 : ID(ID), Level(Level), Loc(), Message(Message) { }
Douglas Gregor33cdd812010-02-18 18:08:43 +0000919
David Blaikie9c902b52011-09-25 23:23:43 +0000920StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level,
David Blaikieb5784322011-09-26 01:18:08 +0000921 const Diagnostic &Info)
Douglas Gregora750e8e2010-11-19 16:18:16 +0000922 : ID(Info.getID()), Level(Level)
923{
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000924 assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
925 "Valid source location without setting a source manager for diagnostic");
926 if (Info.getLocation().isValid())
927 Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000928 SmallString<64> Message;
Douglas Gregor33cdd812010-02-18 18:08:43 +0000929 Info.FormatDiagnostic(Message);
930 this->Message.assign(Message.begin(), Message.end());
931
932 Ranges.reserve(Info.getNumRanges());
933 for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I)
934 Ranges.push_back(Info.getRange(I));
935
Douglas Gregora771f462010-03-31 17:46:05 +0000936 FixIts.reserve(Info.getNumFixItHints());
937 for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I)
938 FixIts.push_back(Info.getFixItHint(I));
Douglas Gregor33cdd812010-02-18 18:08:43 +0000939}
940
David Blaikie9c902b52011-09-25 23:23:43 +0000941StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000942 StringRef Message, FullSourceLoc Loc,
Chris Lattner54b16772011-07-23 17:14:25 +0000943 ArrayRef<CharSourceRange> Ranges,
944 ArrayRef<FixItHint> Fixits)
Douglas Gregor925296b2011-07-19 16:10:42 +0000945 : ID(ID), Level(Level), Loc(Loc), Message(Message)
946{
947 this->Ranges.assign(Ranges.begin(), Ranges.end());
948 this->FixIts.assign(FixIts.begin(), FixIts.end());
949}
950
Douglas Gregor33cdd812010-02-18 18:08:43 +0000951StoredDiagnostic::~StoredDiagnostic() { }
952
Ted Kremenekea06ec12009-01-23 20:28:53 +0000953/// IncludeInDiagnosticCounts - This method (whose default implementation
954/// returns true) indicates whether the diagnostics handled by this
David Blaikiee2eefae2011-09-25 23:39:51 +0000955/// DiagnosticConsumer should be included in the number of diagnostics
David Blaikie9c902b52011-09-25 23:23:43 +0000956/// reported by DiagnosticsEngine.
David Blaikiee2eefae2011-09-25 23:39:51 +0000957bool DiagnosticConsumer::IncludeInDiagnosticCounts() const { return true; }
Douglas Gregor89336232010-03-29 23:34:08 +0000958
David Blaikie68e081d2011-12-20 02:48:34 +0000959void IgnoringDiagConsumer::anchor() { }
960
Benjamin Kramer7ec12c92012-02-07 22:29:24 +0000961PartialDiagnostic::StorageAllocator::StorageAllocator() {
Douglas Gregor89336232010-03-29 23:34:08 +0000962 for (unsigned I = 0; I != NumCached; ++I)
963 FreeList[I] = Cached + I;
964 NumFreeListEntries = NumCached;
965}
966
Benjamin Kramer7ec12c92012-02-07 22:29:24 +0000967PartialDiagnostic::StorageAllocator::~StorageAllocator() {
Chad Rosier849a67b2012-02-07 23:24:49 +0000968 // Don't assert if we are in a CrashRecovery context, as this invariant may
969 // be invalidated during a crash.
970 assert((NumFreeListEntries == NumCached ||
971 llvm::CrashRecoveryContext::isRecoveringFromCrash()) &&
972 "A partial is on the lamb");
Douglas Gregor89336232010-03-29 23:34:08 +0000973}