blob: e68950200fd036db3db15a121f0ee6fbe53a6530 [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"
Daniel Dunbare3633792009-10-17 18:12:14 +000018#include "llvm/Support/raw_ostream.h"
Ted Kremenek84de4a12011-03-21 18:40:07 +000019#include "llvm/Support/CrashRecoveryContext.h"
Joerg Sonnenberger42cf2682012-08-10 10:58:18 +000020#include <cctype>
Ted Kremenek84de4a12011-03-21 18:40:07 +000021
Chris Lattner22eb9722006-06-18 05:43:12 +000022using namespace clang;
23
David Blaikie9c902b52011-09-25 23:23:43 +000024static void DummyArgToStringFn(DiagnosticsEngine::ArgumentKind AK, intptr_t QT,
Chris Lattner63ecc502008-11-23 09:21:17 +000025 const char *Modifier, unsigned ML,
26 const char *Argument, unsigned ArgLen,
David Blaikie9c902b52011-09-25 23:23:43 +000027 const DiagnosticsEngine::ArgumentValue *PrevArgs,
Chris Lattnerc243f292009-10-20 05:25:22 +000028 unsigned NumPrevArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000029 SmallVectorImpl<char> &Output,
Chandler Carruthd5173952011-07-11 17:49:21 +000030 void *Cookie,
Bill Wendling8eb771d2012-02-22 09:51:33 +000031 ArrayRef<intptr_t> QualTypeVals) {
Chris Lattner63ecc502008-11-23 09:21:17 +000032 const char *Str = "<can't format argument>";
Chris Lattner6a2ed6f2008-11-23 09:13:29 +000033 Output.append(Str, Str+strlen(Str));
34}
35
36
David Blaikie9c902b52011-09-25 23:23:43 +000037DiagnosticsEngine::DiagnosticsEngine(
Dylan Noblesmithc95d8192012-02-20 14:00:23 +000038 const IntrusiveRefCntPtr<DiagnosticIDs> &diags,
David Blaikiee2eefae2011-09-25 23:39:51 +000039 DiagnosticConsumer *client, bool ShouldOwnClient)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000040 : Diags(diags), Client(client), OwnsDiagClient(ShouldOwnClient),
41 SourceMgr(0) {
Chris Lattner63ecc502008-11-23 09:21:17 +000042 ArgToStringFn = DummyArgToStringFn;
Chris Lattnercf868c42009-02-19 23:53:20 +000043 ArgToStringCookie = 0;
Mike Stump11289f42009-09-09 15:08:12 +000044
Douglas Gregor0e119552010-07-31 00:40:00 +000045 AllExtensionsSilenced = 0;
46 IgnoreAllWarnings = false;
47 WarningsAsErrors = false;
Ted Kremenekfbbdced2011-08-18 01:12:56 +000048 EnableAllWarnings = false;
Douglas Gregor0e119552010-07-31 00:40:00 +000049 ErrorsAsFatal = false;
50 SuppressSystemWarnings = false;
51 SuppressAllDiagnostics = false;
Richard Trieu91844232012-06-26 18:18:47 +000052 ElideType = true;
53 PrintTemplateTree = false;
54 ShowColors = false;
Douglas Gregor0e119552010-07-31 00:40:00 +000055 ShowOverloads = Ovl_All;
56 ExtBehavior = Ext_Ignore;
57
58 ErrorLimit = 0;
59 TemplateBacktraceLimit = 0;
Richard Smithf6f003a2011-12-16 19:06:07 +000060 ConstexprBacktraceLimit = 0;
Douglas Gregor0e119552010-07-31 00:40:00 +000061
Douglas Gregoraa21cc42010-07-19 21:46:24 +000062 Reset();
Chris Lattnerae411572006-07-05 00:55:08 +000063}
64
David Blaikie9c902b52011-09-25 23:23:43 +000065DiagnosticsEngine::~DiagnosticsEngine() {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000066 if (OwnsDiagClient)
67 delete Client;
Chris Lattnere6535cf2007-12-02 01:09:57 +000068}
69
David Blaikiee2eefae2011-09-25 23:39:51 +000070void DiagnosticsEngine::setClient(DiagnosticConsumer *client,
David Blaikie9c902b52011-09-25 23:23:43 +000071 bool ShouldOwnClient) {
Douglas Gregor7a964ad2011-01-31 22:04:05 +000072 if (OwnsDiagClient && Client)
73 delete Client;
74
75 Client = client;
76 OwnsDiagClient = ShouldOwnClient;
77}
Chris Lattnerfb42a182009-07-12 21:18:45 +000078
David Blaikie9c902b52011-09-25 23:23:43 +000079void DiagnosticsEngine::pushMappings(SourceLocation Loc) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +000080 DiagStateOnPushStack.push_back(GetCurDiagState());
Chris Lattnerfb42a182009-07-12 21:18:45 +000081}
82
David Blaikie9c902b52011-09-25 23:23:43 +000083bool DiagnosticsEngine::popMappings(SourceLocation Loc) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +000084 if (DiagStateOnPushStack.empty())
Chris Lattnerfb42a182009-07-12 21:18:45 +000085 return false;
86
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +000087 if (DiagStateOnPushStack.back() != GetCurDiagState()) {
88 // State changed at some point between push/pop.
89 PushDiagStatePoint(DiagStateOnPushStack.back(), Loc);
90 }
91 DiagStateOnPushStack.pop_back();
Chris Lattnerfb42a182009-07-12 21:18:45 +000092 return true;
93}
94
David Blaikie9c902b52011-09-25 23:23:43 +000095void DiagnosticsEngine::Reset() {
Douglas Gregoraa21cc42010-07-19 21:46:24 +000096 ErrorOccurred = false;
97 FatalErrorOccurred = false;
Douglas Gregor8a60bbe2011-07-06 17:40:26 +000098 UnrecoverableErrorOccurred = false;
Douglas Gregoraa21cc42010-07-19 21:46:24 +000099
100 NumWarnings = 0;
101 NumErrors = 0;
102 NumErrorsSuppressed = 0;
Argyrios Kyrtzidis1fa8b4b2011-07-29 01:25:44 +0000103 TrapNumErrorsOccurred = 0;
104 TrapNumUnrecoverableErrorsOccurred = 0;
Douglas Gregor8a60bbe2011-07-06 17:40:26 +0000105
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000106 CurDiagID = ~0U;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000107 // Set LastDiagLevel to an "unset" state. If we set it to 'Ignored', notes
David Blaikie9c902b52011-09-25 23:23:43 +0000108 // using a DiagnosticsEngine associated to a translation unit that follow
109 // diagnostics from a DiagnosticsEngine associated to anoter t.u. will not be
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000110 // displayed.
111 LastDiagLevel = (DiagnosticIDs::Level)-1;
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000112 DelayedDiagID = 0;
Argyrios Kyrtzidisbbbeea12011-03-26 18:58:17 +0000113
114 // Clear state related to #pragma diagnostic.
115 DiagStates.clear();
116 DiagStatePoints.clear();
117 DiagStateOnPushStack.clear();
118
119 // Create a DiagState and DiagStatePoint representing diagnostic changes
120 // through command-line.
121 DiagStates.push_back(DiagState());
Richard Smithf995f2c2012-08-14 04:19:29 +0000122 DiagStatePoints.push_back(DiagStatePoint(&DiagStates.back(), FullSourceLoc()));
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000123}
Chris Lattner22eb9722006-06-18 05:43:12 +0000124
David Blaikie9c902b52011-09-25 23:23:43 +0000125void DiagnosticsEngine::SetDelayedDiagnostic(unsigned DiagID, StringRef Arg1,
Chad Rosier849a67b2012-02-07 23:24:49 +0000126 StringRef Arg2) {
Douglas Gregor85795312010-03-22 15:10:57 +0000127 if (DelayedDiagID)
128 return;
129
130 DelayedDiagID = DiagID;
Douglas Gregor96380982010-03-22 15:47:45 +0000131 DelayedDiagArg1 = Arg1.str();
132 DelayedDiagArg2 = Arg2.str();
Douglas Gregor85795312010-03-22 15:10:57 +0000133}
134
David Blaikie9c902b52011-09-25 23:23:43 +0000135void DiagnosticsEngine::ReportDelayed() {
Douglas Gregor85795312010-03-22 15:10:57 +0000136 Report(DelayedDiagID) << DelayedDiagArg1 << DelayedDiagArg2;
137 DelayedDiagID = 0;
138 DelayedDiagArg1.clear();
139 DelayedDiagArg2.clear();
140}
141
David Blaikie9c902b52011-09-25 23:23:43 +0000142DiagnosticsEngine::DiagStatePointsTy::iterator
143DiagnosticsEngine::GetDiagStatePointForLoc(SourceLocation L) const {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000144 assert(!DiagStatePoints.empty());
145 assert(DiagStatePoints.front().Loc.isInvalid() &&
146 "Should have created a DiagStatePoint for command-line");
147
148 FullSourceLoc Loc(L, *SourceMgr);
149 if (Loc.isInvalid())
150 return DiagStatePoints.end() - 1;
151
152 DiagStatePointsTy::iterator Pos = DiagStatePoints.end();
153 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
154 if (LastStateChangePos.isValid() &&
155 Loc.isBeforeInTranslationUnitThan(LastStateChangePos))
156 Pos = std::upper_bound(DiagStatePoints.begin(), DiagStatePoints.end(),
157 DiagStatePoint(0, Loc));
158 --Pos;
159 return Pos;
160}
161
David Blaikie9c902b52011-09-25 23:23:43 +0000162void DiagnosticsEngine::setDiagnosticMapping(diag::kind Diag, diag::Mapping Map,
Chad Rosier849a67b2012-02-07 23:24:49 +0000163 SourceLocation L) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000164 assert(Diag < diag::DIAG_UPPER_LIMIT &&
165 "Can only map builtin diagnostics");
166 assert((Diags->isBuiltinWarningOrExtension(Diag) ||
167 (Map == diag::MAP_FATAL || Map == diag::MAP_ERROR)) &&
168 "Cannot map errors into warnings!");
169 assert(!DiagStatePoints.empty());
170
171 FullSourceLoc Loc(L, *SourceMgr);
172 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
Chad Rosierd1956e42012-02-03 01:49:51 +0000173 // Don't allow a mapping to a warning override an error/fatal mapping.
174 if (Map == diag::MAP_WARNING) {
175 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(Diag);
176 if (Info.getMapping() == diag::MAP_ERROR ||
177 Info.getMapping() == diag::MAP_FATAL)
178 Map = Info.getMapping();
179 }
Argyrios Kyrtzidisc137d0d2011-11-09 01:24:17 +0000180 DiagnosticMappingInfo MappingInfo = makeMappingInfo(Map, L);
Daniel Dunbar2fba0972011-10-04 21:17:24 +0000181
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000182 // Common case; setting all the diagnostics of a group in one place.
183 if (Loc.isInvalid() || Loc == LastStateChangePos) {
Daniel Dunbar458edfa2011-09-29 01:34:47 +0000184 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000185 return;
186 }
187
188 // Another common case; modifying diagnostic state in a source location
189 // after the previous one.
190 if ((Loc.isValid() && LastStateChangePos.isInvalid()) ||
191 LastStateChangePos.isBeforeInTranslationUnitThan(Loc)) {
Chris Lattner57540c52011-04-15 05:22:18 +0000192 // A diagnostic pragma occurred, create a new DiagState initialized with
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000193 // the current one and a new DiagStatePoint to record at which location
194 // the new state became active.
195 DiagStates.push_back(*GetCurDiagState());
196 PushDiagStatePoint(&DiagStates.back(), Loc);
Daniel Dunbar458edfa2011-09-29 01:34:47 +0000197 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000198 return;
199 }
200
201 // We allow setting the diagnostic state in random source order for
202 // completeness but it should not be actually happening in normal practice.
203
204 DiagStatePointsTy::iterator Pos = GetDiagStatePointForLoc(Loc);
205 assert(Pos != DiagStatePoints.end());
206
207 // Update all diagnostic states that are active after the given location.
208 for (DiagStatePointsTy::iterator
209 I = Pos+1, E = DiagStatePoints.end(); I != E; ++I) {
Daniel Dunbar458edfa2011-09-29 01:34:47 +0000210 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000211 }
212
213 // If the location corresponds to an existing point, just update its state.
214 if (Pos->Loc == Loc) {
Daniel Dunbar458edfa2011-09-29 01:34:47 +0000215 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000216 return;
217 }
218
219 // Create a new state/point and fit it into the vector of DiagStatePoints
220 // so that the vector is always ordered according to location.
221 Pos->Loc.isBeforeInTranslationUnitThan(Loc);
222 DiagStates.push_back(*Pos->State);
223 DiagState *NewState = &DiagStates.back();
Daniel Dunbar458edfa2011-09-29 01:34:47 +0000224 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000225 DiagStatePoints.insert(Pos+1, DiagStatePoint(NewState,
226 FullSourceLoc(Loc, *SourceMgr)));
227}
228
Daniel Dunbard908c122011-09-29 01:47:16 +0000229bool DiagnosticsEngine::setDiagnosticGroupMapping(
230 StringRef Group, diag::Mapping Map, SourceLocation Loc)
231{
232 // Get the diagnostics in this group.
233 llvm::SmallVector<diag::kind, 8> GroupDiags;
234 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
235 return true;
236
237 // Set the mapping.
238 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i)
239 setDiagnosticMapping(GroupDiags[i], Map, Loc);
240
241 return false;
242}
243
Chad Rosier4c17fa72012-02-07 19:55:45 +0000244void DiagnosticsEngine::setDiagnosticWarningAsError(diag::kind Diag,
245 bool Enabled) {
246 // If we are enabling this feature, just set the diagnostic mappings to map to
247 // errors.
248 if (Enabled)
249 setDiagnosticMapping(Diag, diag::MAP_ERROR, SourceLocation());
250
251 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
252 // potentially downgrade anything already mapped to be a warning.
253 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(Diag);
254
255 if (Info.getMapping() == diag::MAP_ERROR ||
256 Info.getMapping() == diag::MAP_FATAL)
257 Info.setMapping(diag::MAP_WARNING);
258
259 Info.setNoWarningAsError(true);
260}
261
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000262bool DiagnosticsEngine::setDiagnosticGroupWarningAsError(StringRef Group,
263 bool Enabled) {
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000264 // If we are enabling this feature, just set the diagnostic mappings to map to
265 // errors.
266 if (Enabled)
267 return setDiagnosticGroupMapping(Group, diag::MAP_ERROR);
268
269 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
270 // potentially downgrade anything already mapped to be a warning.
271
272 // Get the diagnostics in this group.
273 llvm::SmallVector<diag::kind, 8> GroupDiags;
274 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
275 return true;
276
277 // Perform the mapping change.
278 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i) {
279 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(
280 GroupDiags[i]);
281
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000282 if (Info.getMapping() == diag::MAP_ERROR ||
283 Info.getMapping() == diag::MAP_FATAL)
284 Info.setMapping(diag::MAP_WARNING);
285
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000286 Info.setNoWarningAsError(true);
287 }
288
289 return false;
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000290}
291
Chad Rosier4c17fa72012-02-07 19:55:45 +0000292void DiagnosticsEngine::setDiagnosticErrorAsFatal(diag::kind Diag,
293 bool Enabled) {
294 // If we are enabling this feature, just set the diagnostic mappings to map to
295 // errors.
296 if (Enabled)
297 setDiagnosticMapping(Diag, diag::MAP_FATAL, SourceLocation());
298
299 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
300 // potentially downgrade anything already mapped to be a warning.
301 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(Diag);
302
303 if (Info.getMapping() == diag::MAP_FATAL)
304 Info.setMapping(diag::MAP_ERROR);
305
306 Info.setNoErrorAsFatal(true);
307}
308
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000309bool DiagnosticsEngine::setDiagnosticGroupErrorAsFatal(StringRef Group,
310 bool Enabled) {
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000311 // If we are enabling this feature, just set the diagnostic mappings to map to
312 // fatal errors.
313 if (Enabled)
314 return setDiagnosticGroupMapping(Group, diag::MAP_FATAL);
315
316 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
317 // potentially downgrade anything already mapped to be an error.
318
319 // Get the diagnostics in this group.
320 llvm::SmallVector<diag::kind, 8> GroupDiags;
321 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
322 return true;
323
324 // Perform the mapping change.
325 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i) {
326 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(
327 GroupDiags[i]);
328
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000329 if (Info.getMapping() == diag::MAP_FATAL)
330 Info.setMapping(diag::MAP_ERROR);
331
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000332 Info.setNoErrorAsFatal(true);
333 }
334
335 return false;
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000336}
337
Argyrios Kyrtzidis059cac42012-01-28 04:35:52 +0000338void DiagnosticsEngine::setMappingToAllDiagnostics(diag::Mapping Map,
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000339 SourceLocation Loc) {
340 // Get all the diagnostics.
341 llvm::SmallVector<diag::kind, 64> AllDiags;
342 Diags->getAllDiagnostics(AllDiags);
343
344 // Set the mapping.
345 for (unsigned i = 0, e = AllDiags.size(); i != e; ++i)
346 if (Diags->isBuiltinWarningOrExtension(AllDiags[i]))
347 setDiagnosticMapping(AllDiags[i], Map, Loc);
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000348}
349
David Blaikie9c902b52011-09-25 23:23:43 +0000350void DiagnosticsEngine::Report(const StoredDiagnostic &storedDiag) {
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000351 assert(CurDiagID == ~0U && "Multiple diagnostics in flight at once!");
352
353 CurDiagLoc = storedDiag.getLocation();
354 CurDiagID = storedDiag.getID();
355 NumDiagArgs = 0;
356
357 NumDiagRanges = storedDiag.range_size();
Daniel Dunbar007b9dc2012-03-13 18:21:17 +0000358 assert(NumDiagRanges < DiagnosticsEngine::MaxRanges &&
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000359 "Too many arguments to diagnostic!");
360 unsigned i = 0;
361 for (StoredDiagnostic::range_iterator
362 RI = storedDiag.range_begin(),
363 RE = storedDiag.range_end(); RI != RE; ++RI)
364 DiagRanges[i++] = *RI;
365
Daniel Dunbar007b9dc2012-03-13 18:21:17 +0000366 assert(NumDiagRanges < DiagnosticsEngine::MaxFixItHints &&
367 "Too many arguments to diagnostic!");
368 NumDiagFixItHints = 0;
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000369 for (StoredDiagnostic::fixit_iterator
370 FI = storedDiag.fixit_begin(),
371 FE = storedDiag.fixit_end(); FI != FE; ++FI)
Daniel Dunbar007b9dc2012-03-13 18:21:17 +0000372 DiagFixItHints[NumDiagFixItHints++] = *FI;
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000373
David Blaikiee2eefae2011-09-25 23:39:51 +0000374 assert(Client && "DiagnosticConsumer not set!");
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000375 Level DiagLevel = storedDiag.getLevel();
David Blaikieb5784322011-09-26 01:18:08 +0000376 Diagnostic Info(this, storedDiag.getMessage());
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000377 Client->HandleDiagnostic(DiagLevel, Info);
378 if (Client->IncludeInDiagnosticCounts()) {
David Blaikie9c902b52011-09-25 23:23:43 +0000379 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000380 ++NumWarnings;
381 }
382
383 CurDiagID = ~0U;
384}
385
Jordan Rose6f524ac2012-07-11 16:50:36 +0000386bool DiagnosticsEngine::EmitCurrentDiagnostic(bool Force) {
387 assert(getClient() && "DiagnosticClient not set!");
388
389 bool Emitted;
390 if (Force) {
391 Diagnostic Info(this);
392
393 // Figure out the diagnostic level of this message.
394 DiagnosticIDs::Level DiagLevel
395 = Diags->getDiagnosticLevel(Info.getID(), Info.getLocation(), *this);
396
397 Emitted = (DiagLevel != DiagnosticIDs::Ignored);
398 if (Emitted) {
399 // Emit the diagnostic regardless of suppression level.
400 Diags->EmitDiag(*this, DiagLevel);
401 }
402 } else {
403 // Process the diagnostic, sending the accumulated information to the
404 // DiagnosticConsumer.
405 Emitted = ProcessDiag();
406 }
Douglas Gregor85795312010-03-22 15:10:57 +0000407
408 // Clear out the current diagnostic object.
Daniel Dunbarc7c00892012-03-13 21:02:14 +0000409 unsigned DiagID = CurDiagID;
410 Clear();
Douglas Gregor85795312010-03-22 15:10:57 +0000411
412 // If there was a delayed diagnostic, emit it now.
Jordan Rose6f524ac2012-07-11 16:50:36 +0000413 if (!Force && DelayedDiagID && DelayedDiagID != DiagID)
Daniel Dunbarc7c00892012-03-13 21:02:14 +0000414 ReportDelayed();
Douglas Gregor85795312010-03-22 15:10:57 +0000415
416 return Emitted;
417}
418
Nico Weber4c311642008-08-10 19:59:06 +0000419
David Blaikiee2eefae2011-09-25 23:39:51 +0000420DiagnosticConsumer::~DiagnosticConsumer() {}
Nico Weber4c311642008-08-10 19:59:06 +0000421
David Blaikiee2eefae2011-09-25 23:39:51 +0000422void DiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
David Blaikieb5784322011-09-26 01:18:08 +0000423 const Diagnostic &Info) {
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000424 if (!IncludeInDiagnosticCounts())
425 return;
426
David Blaikie9c902b52011-09-25 23:23:43 +0000427 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000428 ++NumWarnings;
David Blaikie9c902b52011-09-25 23:23:43 +0000429 else if (DiagLevel >= DiagnosticsEngine::Error)
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000430 ++NumErrors;
431}
Chris Lattner23be0672008-11-19 06:51:40 +0000432
Chris Lattner2b786902008-11-21 07:50:02 +0000433/// ModifierIs - Return true if the specified modifier matches specified string.
434template <std::size_t StrLen>
435static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
436 const char (&Str)[StrLen]) {
437 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
438}
439
John McCall8cb7a8a32010-01-14 20:11:39 +0000440/// ScanForward - Scans forward, looking for the given character, skipping
441/// nested clauses and escaped characters.
442static const char *ScanFormat(const char *I, const char *E, char Target) {
443 unsigned Depth = 0;
444
445 for ( ; I != E; ++I) {
446 if (Depth == 0 && *I == Target) return I;
447 if (Depth != 0 && *I == '}') Depth--;
448
449 if (*I == '%') {
450 I++;
451 if (I == E) break;
452
453 // Escaped characters get implicitly skipped here.
454
455 // Format specifier.
456 if (!isdigit(*I) && !ispunct(*I)) {
457 for (I++; I != E && !isdigit(*I) && *I != '{'; I++) ;
458 if (I == E) break;
459 if (*I == '{')
460 Depth++;
461 }
462 }
463 }
464 return E;
465}
466
Chris Lattner2b786902008-11-21 07:50:02 +0000467/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
468/// like this: %select{foo|bar|baz}2. This means that the integer argument
469/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
470/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
471/// This is very useful for certain classes of variant diagnostics.
David Blaikieb5784322011-09-26 01:18:08 +0000472static void HandleSelectModifier(const Diagnostic &DInfo, unsigned ValNo,
Chris Lattner2b786902008-11-21 07:50:02 +0000473 const char *Argument, unsigned ArgumentLen,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000474 SmallVectorImpl<char> &OutStr) {
Chris Lattner2b786902008-11-21 07:50:02 +0000475 const char *ArgumentEnd = Argument+ArgumentLen;
Mike Stump11289f42009-09-09 15:08:12 +0000476
Chris Lattner2b786902008-11-21 07:50:02 +0000477 // Skip over 'ValNo' |'s.
478 while (ValNo) {
John McCall8cb7a8a32010-01-14 20:11:39 +0000479 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
Chris Lattner2b786902008-11-21 07:50:02 +0000480 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
481 " larger than the number of options in the diagnostic string!");
482 Argument = NextVal+1; // Skip this string.
483 --ValNo;
484 }
Mike Stump11289f42009-09-09 15:08:12 +0000485
Chris Lattner2b786902008-11-21 07:50:02 +0000486 // Get the end of the value. This is either the } or the |.
John McCall8cb7a8a32010-01-14 20:11:39 +0000487 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
John McCalle4d54322010-01-13 23:58:20 +0000488
489 // Recursively format the result of the select clause into the output string.
490 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000491}
492
493/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
494/// letter 's' to the string if the value is not 1. This is used in cases like
495/// this: "you idiot, you have %4 parameter%s4!".
496static void HandleIntegerSModifier(unsigned ValNo,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000497 SmallVectorImpl<char> &OutStr) {
Chris Lattner2b786902008-11-21 07:50:02 +0000498 if (ValNo != 1)
499 OutStr.push_back('s');
500}
501
John McCall9015cde2010-01-14 00:50:32 +0000502/// HandleOrdinalModifier - Handle the integer 'ord' modifier. This
503/// prints the ordinal form of the given integer, with 1 corresponding
504/// to the first ordinal. Currently this is hard-coded to use the
505/// English form.
506static void HandleOrdinalModifier(unsigned ValNo,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000507 SmallVectorImpl<char> &OutStr) {
John McCall9015cde2010-01-14 00:50:32 +0000508 assert(ValNo != 0 && "ValNo must be strictly positive!");
509
510 llvm::raw_svector_ostream Out(OutStr);
511
512 // We could use text forms for the first N ordinals, but the numeric
513 // forms are actually nicer in diagnostics because they stand out.
514 Out << ValNo;
515
516 // It is critically important that we do this perfectly for
517 // user-written sequences with over 100 elements.
518 switch (ValNo % 100) {
519 case 11:
520 case 12:
521 case 13:
522 Out << "th"; return;
523 default:
524 switch (ValNo % 10) {
525 case 1: Out << "st"; return;
526 case 2: Out << "nd"; return;
527 case 3: Out << "rd"; return;
528 default: Out << "th"; return;
529 }
530 }
531}
532
Chris Lattner2b786902008-11-21 07:50:02 +0000533
Sebastian Redl15b02d22008-11-22 13:44:36 +0000534/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000535static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000536 // Programming 101: Parse a decimal number :-)
537 unsigned Val = 0;
538 while (Start != End && *Start >= '0' && *Start <= '9') {
539 Val *= 10;
540 Val += *Start - '0';
541 ++Start;
542 }
543 return Val;
544}
545
546/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000547static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000548 if (*Start != '[') {
549 unsigned Ref = PluralNumber(Start, End);
550 return Ref == Val;
551 }
552
553 ++Start;
554 unsigned Low = PluralNumber(Start, End);
555 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
556 ++Start;
557 unsigned High = PluralNumber(Start, End);
558 assert(*Start == ']' && "Bad plural expression syntax: expected )");
559 ++Start;
560 return Low <= Val && Val <= High;
561}
562
563/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattner2fe29202009-04-15 17:13:42 +0000564static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000565 // Empty condition?
566 if (*Start == ':')
567 return true;
568
569 while (1) {
570 char C = *Start;
571 if (C == '%') {
572 // Modulo expression
573 ++Start;
574 unsigned Arg = PluralNumber(Start, End);
575 assert(*Start == '=' && "Bad plural expression syntax: expected =");
576 ++Start;
577 unsigned ValMod = ValNo % Arg;
578 if (TestPluralRange(ValMod, Start, End))
579 return true;
580 } else {
Sebastian Redl3ceaf622008-11-27 07:28:14 +0000581 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redl15b02d22008-11-22 13:44:36 +0000582 "Bad plural expression syntax: unexpected character");
583 // Range expression
584 if (TestPluralRange(ValNo, Start, End))
585 return true;
586 }
587
588 // Scan for next or-expr part.
589 Start = std::find(Start, End, ',');
Mike Stump11289f42009-09-09 15:08:12 +0000590 if (Start == End)
Sebastian Redl15b02d22008-11-22 13:44:36 +0000591 break;
592 ++Start;
593 }
594 return false;
595}
596
597/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
598/// for complex plural forms, or in languages where all plurals are complex.
599/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
600/// conditions that are tested in order, the form corresponding to the first
601/// that applies being emitted. The empty condition is always true, making the
602/// last form a default case.
603/// Conditions are simple boolean expressions, where n is the number argument.
604/// Here are the rules.
605/// condition := expression | empty
606/// empty := -> always true
607/// expression := numeric [',' expression] -> logical or
608/// numeric := range -> true if n in range
609/// | '%' number '=' range -> true if n % number in range
610/// range := number
611/// | '[' number ',' number ']' -> ranges are inclusive both ends
612///
613/// Here are some examples from the GNU gettext manual written in this form:
614/// English:
615/// {1:form0|:form1}
616/// Latvian:
617/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
618/// Gaeilge:
619/// {1:form0|2:form1|:form2}
620/// Romanian:
621/// {1:form0|0,%100=[1,19]:form1|:form2}
622/// Lithuanian:
623/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
624/// Russian (requires repeated form):
625/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
626/// Slovak
627/// {1:form0|[2,4]:form1|:form2}
628/// Polish (requires repeated form):
629/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
David Blaikieb5784322011-09-26 01:18:08 +0000630static void HandlePluralModifier(const Diagnostic &DInfo, unsigned ValNo,
Sebastian Redl15b02d22008-11-22 13:44:36 +0000631 const char *Argument, unsigned ArgumentLen,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000632 SmallVectorImpl<char> &OutStr) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000633 const char *ArgumentEnd = Argument + ArgumentLen;
634 while (1) {
635 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
636 const char *ExprEnd = Argument;
637 while (*ExprEnd != ':') {
638 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
639 ++ExprEnd;
640 }
641 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
642 Argument = ExprEnd + 1;
John McCall8cb7a8a32010-01-14 20:11:39 +0000643 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
John McCall43b61682010-10-14 01:55:31 +0000644
645 // Recursively format the result of the plural clause into the
646 // output string.
647 DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000648 return;
649 }
John McCall8cb7a8a32010-01-14 20:11:39 +0000650 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
Sebastian Redl15b02d22008-11-22 13:44:36 +0000651 }
652}
653
654
Chris Lattner23be0672008-11-19 06:51:40 +0000655/// FormatDiagnostic - Format this diagnostic into a string, substituting the
656/// formal arguments into the %0 slots. The result is appended onto the Str
657/// array.
David Blaikieb5784322011-09-26 01:18:08 +0000658void Diagnostic::
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000659FormatDiagnostic(SmallVectorImpl<char> &OutStr) const {
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000660 if (!StoredDiagMessage.empty()) {
661 OutStr.append(StoredDiagMessage.begin(), StoredDiagMessage.end());
662 return;
663 }
664
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000665 StringRef Diag =
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000666 getDiags()->getDiagnosticIDs()->getDescription(getID());
Mike Stump11289f42009-09-09 15:08:12 +0000667
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000668 FormatDiagnostic(Diag.begin(), Diag.end(), OutStr);
John McCalle4d54322010-01-13 23:58:20 +0000669}
670
David Blaikieb5784322011-09-26 01:18:08 +0000671void Diagnostic::
John McCalle4d54322010-01-13 23:58:20 +0000672FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000673 SmallVectorImpl<char> &OutStr) const {
John McCalle4d54322010-01-13 23:58:20 +0000674
Chris Lattnerc243f292009-10-20 05:25:22 +0000675 /// FormattedArgs - Keep track of all of the arguments formatted by
676 /// ConvertArgToString and pass them into subsequent calls to
677 /// ConvertArgToString, allowing the implementation to avoid redundancies in
678 /// obvious cases.
David Blaikie9c902b52011-09-25 23:23:43 +0000679 SmallVector<DiagnosticsEngine::ArgumentValue, 8> FormattedArgs;
Chandler Carruthd5173952011-07-11 17:49:21 +0000680
681 /// QualTypeVals - Pass a vector of arrays so that QualType names can be
682 /// compared to see if more information is needed to be printed.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000683 SmallVector<intptr_t, 2> QualTypeVals;
Richard Trieu91844232012-06-26 18:18:47 +0000684 SmallVector<char, 64> Tree;
685
Chandler Carruthd5173952011-07-11 17:49:21 +0000686 for (unsigned i = 0, e = getNumArgs(); i < e; ++i)
David Blaikie9c902b52011-09-25 23:23:43 +0000687 if (getArgKind(i) == DiagnosticsEngine::ak_qualtype)
Chandler Carruthd5173952011-07-11 17:49:21 +0000688 QualTypeVals.push_back(getRawArg(i));
689
Chris Lattner23be0672008-11-19 06:51:40 +0000690 while (DiagStr != DiagEnd) {
691 if (DiagStr[0] != '%') {
692 // Append non-%0 substrings to Str if we have one.
693 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
694 OutStr.append(DiagStr, StrEnd);
695 DiagStr = StrEnd;
Chris Lattner2b786902008-11-21 07:50:02 +0000696 continue;
John McCall8cb7a8a32010-01-14 20:11:39 +0000697 } else if (ispunct(DiagStr[1])) {
698 OutStr.push_back(DiagStr[1]); // %% -> %.
Chris Lattner23be0672008-11-19 06:51:40 +0000699 DiagStr += 2;
Chris Lattner2b786902008-11-21 07:50:02 +0000700 continue;
701 }
Mike Stump11289f42009-09-09 15:08:12 +0000702
Chris Lattner2b786902008-11-21 07:50:02 +0000703 // Skip the %.
704 ++DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000705
Chris Lattner2b786902008-11-21 07:50:02 +0000706 // This must be a placeholder for a diagnostic argument. The format for a
707 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
708 // The digit is a number from 0-9 indicating which argument this comes from.
709 // The modifier is a string of digits from the set [-a-z]+, arguments is a
710 // brace enclosed string.
711 const char *Modifier = 0, *Argument = 0;
712 unsigned ModifierLen = 0, ArgumentLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000713
Chris Lattner2b786902008-11-21 07:50:02 +0000714 // Check to see if we have a modifier. If so eat it.
715 if (!isdigit(DiagStr[0])) {
716 Modifier = DiagStr;
717 while (DiagStr[0] == '-' ||
718 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
719 ++DiagStr;
720 ModifierLen = DiagStr-Modifier;
Chris Lattner23be0672008-11-19 06:51:40 +0000721
Chris Lattner2b786902008-11-21 07:50:02 +0000722 // If we have an argument, get it next.
723 if (DiagStr[0] == '{') {
724 ++DiagStr; // Skip {.
725 Argument = DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000726
John McCall8cb7a8a32010-01-14 20:11:39 +0000727 DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
728 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
Chris Lattner2b786902008-11-21 07:50:02 +0000729 ArgumentLen = DiagStr-Argument;
730 ++DiagStr; // Skip }.
Chris Lattner23be0672008-11-19 06:51:40 +0000731 }
Chris Lattner2b786902008-11-21 07:50:02 +0000732 }
Mike Stump11289f42009-09-09 15:08:12 +0000733
Chris Lattner2b786902008-11-21 07:50:02 +0000734 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000735 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattner2b786902008-11-21 07:50:02 +0000736
Richard Trieu91844232012-06-26 18:18:47 +0000737 // Only used for type diffing.
738 unsigned ArgNo2 = ArgNo;
739
David Blaikie9c902b52011-09-25 23:23:43 +0000740 DiagnosticsEngine::ArgumentKind Kind = getArgKind(ArgNo);
Richard Trieu91844232012-06-26 18:18:47 +0000741 if (Kind == DiagnosticsEngine::ak_qualtype &&
742 ModifierIs(Modifier, ModifierLen, "diff")) {
743 Kind = DiagnosticsEngine::ak_qualtype_pair;
744 assert(*DiagStr == ',' && isdigit(*(DiagStr + 1)) &&
745 "Invalid format for diff modifier");
746 ++DiagStr; // Comma.
747 ArgNo2 = *DiagStr++ - '0';
748 assert(getArgKind(ArgNo2) == DiagnosticsEngine::ak_qualtype &&
749 "Second value of type diff must be a qualtype");
750 }
Chris Lattnerc243f292009-10-20 05:25:22 +0000751
752 switch (Kind) {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000753 // ---- STRINGS ----
David Blaikie9c902b52011-09-25 23:23:43 +0000754 case DiagnosticsEngine::ak_std_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000755 const std::string &S = getArgStdStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000756 assert(ModifierLen == 0 && "No modifiers for strings yet");
757 OutStr.append(S.begin(), S.end());
758 break;
759 }
David Blaikie9c902b52011-09-25 23:23:43 +0000760 case DiagnosticsEngine::ak_c_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000761 const char *S = getArgCStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000762 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000763
764 // Don't crash if get passed a null pointer by accident.
765 if (!S)
766 S = "(null)";
Mike Stump11289f42009-09-09 15:08:12 +0000767
Chris Lattner2b786902008-11-21 07:50:02 +0000768 OutStr.append(S, S + strlen(S));
769 break;
770 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000771 // ---- INTEGERS ----
David Blaikie9c902b52011-09-25 23:23:43 +0000772 case DiagnosticsEngine::ak_sint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000773 int Val = getArgSInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000774
Chris Lattner2b786902008-11-21 07:50:02 +0000775 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCall43b61682010-10-14 01:55:31 +0000776 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen,
777 OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000778 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
779 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000780 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCall43b61682010-10-14 01:55:31 +0000781 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
782 OutStr);
John McCall9015cde2010-01-14 00:50:32 +0000783 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
784 HandleOrdinalModifier((unsigned)Val, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000785 } else {
786 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000787 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000788 }
Chris Lattner2b786902008-11-21 07:50:02 +0000789 break;
790 }
David Blaikie9c902b52011-09-25 23:23:43 +0000791 case DiagnosticsEngine::ak_uint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000792 unsigned Val = getArgUInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000793
Chris Lattner2b786902008-11-21 07:50:02 +0000794 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCalle4d54322010-01-13 23:58:20 +0000795 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000796 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
797 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000798 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCall43b61682010-10-14 01:55:31 +0000799 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
800 OutStr);
John McCall9015cde2010-01-14 00:50:32 +0000801 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
802 HandleOrdinalModifier(Val, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000803 } else {
804 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000805 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000806 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000807 break;
Chris Lattner2b786902008-11-21 07:50:02 +0000808 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000809 // ---- NAMES and TYPES ----
David Blaikie9c902b52011-09-25 23:23:43 +0000810 case DiagnosticsEngine::ak_identifierinfo: {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000811 const IdentifierInfo *II = getArgIdentifier(ArgNo);
812 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000813
814 // Don't crash if get passed a null pointer by accident.
815 if (!II) {
816 const char *S = "(null)";
817 OutStr.append(S, S + strlen(S));
818 continue;
819 }
820
Daniel Dunbar07d07852009-10-18 21:17:35 +0000821 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
Chris Lattnere3d20d92008-11-23 21:45:46 +0000822 break;
823 }
David Blaikie9c902b52011-09-25 23:23:43 +0000824 case DiagnosticsEngine::ak_qualtype:
825 case DiagnosticsEngine::ak_declarationname:
826 case DiagnosticsEngine::ak_nameddecl:
827 case DiagnosticsEngine::ak_nestednamespec:
828 case DiagnosticsEngine::ak_declcontext:
Chris Lattnerc243f292009-10-20 05:25:22 +0000829 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
Chris Lattner63ecc502008-11-23 09:21:17 +0000830 Modifier, ModifierLen,
Chris Lattnerc243f292009-10-20 05:25:22 +0000831 Argument, ArgumentLen,
832 FormattedArgs.data(), FormattedArgs.size(),
Chandler Carruthd5173952011-07-11 17:49:21 +0000833 OutStr, QualTypeVals);
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000834 break;
Richard Trieu91844232012-06-26 18:18:47 +0000835 case DiagnosticsEngine::ak_qualtype_pair:
836 // Create a struct with all the info needed for printing.
837 TemplateDiffTypes TDT;
838 TDT.FromType = getRawArg(ArgNo);
839 TDT.ToType = getRawArg(ArgNo2);
840 TDT.ElideType = getDiags()->ElideType;
841 TDT.ShowColors = getDiags()->ShowColors;
Richard Trieu50f5f462012-07-10 01:46:04 +0000842 TDT.TemplateDiffUsed = false;
Richard Trieu91844232012-06-26 18:18:47 +0000843 intptr_t val = reinterpret_cast<intptr_t>(&TDT);
844
Richard Trieuc6058442012-06-29 21:12:16 +0000845 const char *ArgumentEnd = Argument + ArgumentLen;
846 const char *Pipe = ScanFormat(Argument, ArgumentEnd, '|');
847
Richard Trieua4056002012-07-13 21:18:32 +0000848 // Print the tree. If this diagnostic already has a tree, skip the
849 // second tree.
850 if (getDiags()->PrintTemplateTree && Tree.empty()) {
Richard Trieu91844232012-06-26 18:18:47 +0000851 TDT.PrintFromType = true;
852 TDT.PrintTree = true;
853 getDiags()->ConvertArgToString(Kind, val,
854 Modifier, ModifierLen,
855 Argument, ArgumentLen,
856 FormattedArgs.data(),
857 FormattedArgs.size(),
858 Tree, QualTypeVals);
859 // If there is no tree information, fall back to regular printing.
Richard Trieuc6058442012-06-29 21:12:16 +0000860 if (!Tree.empty()) {
861 FormatDiagnostic(Pipe + 1, ArgumentEnd, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +0000862 break;
Richard Trieuc6058442012-06-29 21:12:16 +0000863 }
Richard Trieu91844232012-06-26 18:18:47 +0000864 }
865
866 // Non-tree printing, also the fall-back when tree printing fails.
867 // The fall-back is triggered when the types compared are not templates.
Richard Trieuc6058442012-06-29 21:12:16 +0000868 const char *FirstDollar = ScanFormat(Argument, ArgumentEnd, '$');
869 const char *SecondDollar = ScanFormat(FirstDollar + 1, ArgumentEnd, '$');
Richard Trieu91844232012-06-26 18:18:47 +0000870
871 // Append before text
Richard Trieuc6058442012-06-29 21:12:16 +0000872 FormatDiagnostic(Argument, FirstDollar, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +0000873
874 // Append first type
875 TDT.PrintTree = false;
876 TDT.PrintFromType = true;
877 getDiags()->ConvertArgToString(Kind, val,
878 Modifier, ModifierLen,
879 Argument, ArgumentLen,
880 FormattedArgs.data(), FormattedArgs.size(),
881 OutStr, QualTypeVals);
Richard Trieu50f5f462012-07-10 01:46:04 +0000882 if (!TDT.TemplateDiffUsed)
883 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
884 TDT.FromType));
885
Richard Trieu91844232012-06-26 18:18:47 +0000886 // Append middle text
Richard Trieuc6058442012-06-29 21:12:16 +0000887 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +0000888
889 // Append second type
890 TDT.PrintFromType = false;
891 getDiags()->ConvertArgToString(Kind, val,
892 Modifier, ModifierLen,
893 Argument, ArgumentLen,
894 FormattedArgs.data(), FormattedArgs.size(),
895 OutStr, QualTypeVals);
Richard Trieu50f5f462012-07-10 01:46:04 +0000896 if (!TDT.TemplateDiffUsed)
897 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
898 TDT.ToType));
899
Richard Trieu91844232012-06-26 18:18:47 +0000900 // Append end text
Richard Trieuc6058442012-06-29 21:12:16 +0000901 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +0000902 break;
Nico Weber4c311642008-08-10 19:59:06 +0000903 }
Chris Lattnerc243f292009-10-20 05:25:22 +0000904
905 // Remember this argument info for subsequent formatting operations. Turn
906 // std::strings into a null terminated string to make it be the same case as
907 // all the other ones.
Richard Trieu91844232012-06-26 18:18:47 +0000908 if (Kind == DiagnosticsEngine::ak_qualtype_pair)
909 continue;
910 else if (Kind != DiagnosticsEngine::ak_std_string)
Chris Lattnerc243f292009-10-20 05:25:22 +0000911 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
912 else
David Blaikie9c902b52011-09-25 23:23:43 +0000913 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_c_string,
Chris Lattnerc243f292009-10-20 05:25:22 +0000914 (intptr_t)getArgStdStr(ArgNo).c_str()));
915
Nico Weber4c311642008-08-10 19:59:06 +0000916 }
Richard Trieu91844232012-06-26 18:18:47 +0000917
918 // Append the type tree to the end of the diagnostics.
919 OutStr.append(Tree.begin(), Tree.end());
Nico Weber4c311642008-08-10 19:59:06 +0000920}
Ted Kremenekea06ec12009-01-23 20:28:53 +0000921
Douglas Gregor33cdd812010-02-18 18:08:43 +0000922StoredDiagnostic::StoredDiagnostic() { }
923
David Blaikie9c902b52011-09-25 23:23:43 +0000924StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000925 StringRef Message)
Benjamin Kramer929bd682010-11-19 17:36:51 +0000926 : ID(ID), Level(Level), Loc(), Message(Message) { }
Douglas Gregor33cdd812010-02-18 18:08:43 +0000927
David Blaikie9c902b52011-09-25 23:23:43 +0000928StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level,
David Blaikieb5784322011-09-26 01:18:08 +0000929 const Diagnostic &Info)
Douglas Gregora750e8e2010-11-19 16:18:16 +0000930 : ID(Info.getID()), Level(Level)
931{
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000932 assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
933 "Valid source location without setting a source manager for diagnostic");
934 if (Info.getLocation().isValid())
935 Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000936 SmallString<64> Message;
Douglas Gregor33cdd812010-02-18 18:08:43 +0000937 Info.FormatDiagnostic(Message);
938 this->Message.assign(Message.begin(), Message.end());
939
940 Ranges.reserve(Info.getNumRanges());
941 for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I)
942 Ranges.push_back(Info.getRange(I));
943
Douglas Gregora771f462010-03-31 17:46:05 +0000944 FixIts.reserve(Info.getNumFixItHints());
945 for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I)
946 FixIts.push_back(Info.getFixItHint(I));
Douglas Gregor33cdd812010-02-18 18:08:43 +0000947}
948
David Blaikie9c902b52011-09-25 23:23:43 +0000949StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000950 StringRef Message, FullSourceLoc Loc,
Chris Lattner54b16772011-07-23 17:14:25 +0000951 ArrayRef<CharSourceRange> Ranges,
952 ArrayRef<FixItHint> Fixits)
Douglas Gregor925296b2011-07-19 16:10:42 +0000953 : ID(ID), Level(Level), Loc(Loc), Message(Message)
954{
955 this->Ranges.assign(Ranges.begin(), Ranges.end());
956 this->FixIts.assign(FixIts.begin(), FixIts.end());
957}
958
Douglas Gregor33cdd812010-02-18 18:08:43 +0000959StoredDiagnostic::~StoredDiagnostic() { }
960
Ted Kremenekea06ec12009-01-23 20:28:53 +0000961/// IncludeInDiagnosticCounts - This method (whose default implementation
962/// returns true) indicates whether the diagnostics handled by this
David Blaikiee2eefae2011-09-25 23:39:51 +0000963/// DiagnosticConsumer should be included in the number of diagnostics
David Blaikie9c902b52011-09-25 23:23:43 +0000964/// reported by DiagnosticsEngine.
David Blaikiee2eefae2011-09-25 23:39:51 +0000965bool DiagnosticConsumer::IncludeInDiagnosticCounts() const { return true; }
Douglas Gregor89336232010-03-29 23:34:08 +0000966
David Blaikie68e081d2011-12-20 02:48:34 +0000967void IgnoringDiagConsumer::anchor() { }
968
Benjamin Kramer7ec12c92012-02-07 22:29:24 +0000969PartialDiagnostic::StorageAllocator::StorageAllocator() {
Douglas Gregor89336232010-03-29 23:34:08 +0000970 for (unsigned I = 0; I != NumCached; ++I)
971 FreeList[I] = Cached + I;
972 NumFreeListEntries = NumCached;
973}
974
Benjamin Kramer7ec12c92012-02-07 22:29:24 +0000975PartialDiagnostic::StorageAllocator::~StorageAllocator() {
Chad Rosier849a67b2012-02-07 23:24:49 +0000976 // Don't assert if we are in a CrashRecovery context, as this invariant may
977 // be invalidated during a crash.
978 assert((NumFreeListEntries == NumCached ||
979 llvm::CrashRecoveryContext::isRecoveringFromCrash()) &&
980 "A partial is on the lamb");
Douglas Gregor89336232010-03-29 23:34:08 +0000981}