blob: 8cef9d5b19ad99b4b246adad264718042b75a638 [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
Jordan Rosea7d03842013-02-08 22:30:41 +000014#include "clang/Basic/CharInfo.h"
Ted Kremenek39a76652010-04-12 19:54:17 +000015#include "clang/Basic/Diagnostic.h"
Douglas Gregor811db4e2012-10-23 22:26:28 +000016#include "clang/Basic/DiagnosticOptions.h"
Chris Lattnerb91fd172008-11-19 07:32:16 +000017#include "clang/Basic/IdentifierTable.h"
Ted Kremenek39a76652010-04-12 19:54:17 +000018#include "clang/Basic/PartialDiagnostic.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000019#include "llvm/ADT/SmallString.h"
Jordan Rosec102b352012-09-22 01:24:42 +000020#include "llvm/ADT/StringExtras.h"
Ted Kremenek84de4a12011-03-21 18:40:07 +000021#include "llvm/Support/CrashRecoveryContext.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "llvm/Support/raw_ostream.h"
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),
Craig Topperf1186c52014-05-08 06:41:40 +000044 OwnsDiagClient(ShouldOwnClient), SourceMgr(nullptr) {
Chris Lattner63ecc502008-11-23 09:21:17 +000045 ArgToStringFn = DummyArgToStringFn;
Craig Topperf1186c52014-05-08 06:41:40 +000046 ArgToStringCookie = nullptr;
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;
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000111 LastDiagLevel = DiagnosticIDs::Ignored;
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
Richard Smith99eff012012-08-17 00:55:32 +0000148 if (!SourceMgr)
149 return DiagStatePoints.end() - 1;
150
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000151 FullSourceLoc Loc(L, *SourceMgr);
152 if (Loc.isInvalid())
153 return DiagStatePoints.end() - 1;
154
155 DiagStatePointsTy::iterator Pos = DiagStatePoints.end();
156 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
157 if (LastStateChangePos.isValid() &&
158 Loc.isBeforeInTranslationUnitThan(LastStateChangePos))
159 Pos = std::upper_bound(DiagStatePoints.begin(), DiagStatePoints.end(),
Craig Topperf1186c52014-05-08 06:41:40 +0000160 DiagStatePoint(nullptr, Loc));
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000161 --Pos;
162 return Pos;
163}
164
David Blaikie9c902b52011-09-25 23:23:43 +0000165void DiagnosticsEngine::setDiagnosticMapping(diag::kind Diag, diag::Mapping Map,
Chad Rosier849a67b2012-02-07 23:24:49 +0000166 SourceLocation L) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000167 assert(Diag < diag::DIAG_UPPER_LIMIT &&
168 "Can only map builtin diagnostics");
169 assert((Diags->isBuiltinWarningOrExtension(Diag) ||
170 (Map == diag::MAP_FATAL || Map == diag::MAP_ERROR)) &&
171 "Cannot map errors into warnings!");
172 assert(!DiagStatePoints.empty());
Richard Smith8a0527d2012-08-14 22:37:22 +0000173 assert((L.isInvalid() || SourceMgr) && "No SourceMgr for valid location");
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000174
Richard Smith8a0527d2012-08-14 22:37:22 +0000175 FullSourceLoc Loc = SourceMgr? FullSourceLoc(L, *SourceMgr) : FullSourceLoc();
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000176 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
Chad Rosierd1956e42012-02-03 01:49:51 +0000177 // Don't allow a mapping to a warning override an error/fatal mapping.
178 if (Map == diag::MAP_WARNING) {
179 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(Diag);
180 if (Info.getMapping() == diag::MAP_ERROR ||
181 Info.getMapping() == diag::MAP_FATAL)
182 Map = Info.getMapping();
183 }
Argyrios Kyrtzidisc137d0d2011-11-09 01:24:17 +0000184 DiagnosticMappingInfo MappingInfo = makeMappingInfo(Map, L);
Daniel Dunbar2fba0972011-10-04 21:17:24 +0000185
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000186 // Common case; setting all the diagnostics of a group in one place.
187 if (Loc.isInvalid() || Loc == LastStateChangePos) {
Daniel Dunbar458edfa2011-09-29 01:34:47 +0000188 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000189 return;
190 }
191
192 // Another common case; modifying diagnostic state in a source location
193 // after the previous one.
194 if ((Loc.isValid() && LastStateChangePos.isInvalid()) ||
195 LastStateChangePos.isBeforeInTranslationUnitThan(Loc)) {
Chris Lattner57540c52011-04-15 05:22:18 +0000196 // A diagnostic pragma occurred, create a new DiagState initialized with
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000197 // the current one and a new DiagStatePoint to record at which location
198 // the new state became active.
199 DiagStates.push_back(*GetCurDiagState());
200 PushDiagStatePoint(&DiagStates.back(), Loc);
Daniel Dunbar458edfa2011-09-29 01:34:47 +0000201 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000202 return;
203 }
204
205 // We allow setting the diagnostic state in random source order for
206 // completeness but it should not be actually happening in normal practice.
207
208 DiagStatePointsTy::iterator Pos = GetDiagStatePointForLoc(Loc);
209 assert(Pos != DiagStatePoints.end());
210
211 // Update all diagnostic states that are active after the given location.
212 for (DiagStatePointsTy::iterator
213 I = Pos+1, E = DiagStatePoints.end(); I != E; ++I) {
Daniel Dunbar458edfa2011-09-29 01:34:47 +0000214 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000215 }
216
217 // If the location corresponds to an existing point, just update its state.
218 if (Pos->Loc == Loc) {
Daniel Dunbar458edfa2011-09-29 01:34:47 +0000219 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000220 return;
221 }
222
223 // Create a new state/point and fit it into the vector of DiagStatePoints
224 // so that the vector is always ordered according to location.
Alp Toker14c8aff2014-01-26 08:12:32 +0000225 assert(Pos->Loc.isBeforeInTranslationUnitThan(Loc));
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000226 DiagStates.push_back(*Pos->State);
227 DiagState *NewState = &DiagStates.back();
Daniel Dunbar458edfa2011-09-29 01:34:47 +0000228 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000229 DiagStatePoints.insert(Pos+1, DiagStatePoint(NewState,
230 FullSourceLoc(Loc, *SourceMgr)));
231}
232
Daniel Dunbard908c122011-09-29 01:47:16 +0000233bool DiagnosticsEngine::setDiagnosticGroupMapping(
234 StringRef Group, diag::Mapping Map, SourceLocation Loc)
235{
236 // Get the diagnostics in this group.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000237 SmallVector<diag::kind, 8> GroupDiags;
Daniel Dunbard908c122011-09-29 01:47:16 +0000238 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
239 return true;
240
241 // Set the mapping.
242 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i)
243 setDiagnosticMapping(GroupDiags[i], Map, Loc);
244
245 return false;
246}
247
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000248bool DiagnosticsEngine::setDiagnosticGroupWarningAsError(StringRef Group,
249 bool Enabled) {
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000250 // If we are enabling this feature, just set the diagnostic mappings to map to
251 // errors.
252 if (Enabled)
253 return setDiagnosticGroupMapping(Group, diag::MAP_ERROR);
254
255 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
256 // potentially downgrade anything already mapped to be a warning.
257
258 // Get the diagnostics in this group.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000259 SmallVector<diag::kind, 8> GroupDiags;
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000260 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
261 return true;
262
263 // Perform the mapping change.
264 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i) {
265 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(
266 GroupDiags[i]);
267
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000268 if (Info.getMapping() == diag::MAP_ERROR ||
269 Info.getMapping() == diag::MAP_FATAL)
270 Info.setMapping(diag::MAP_WARNING);
271
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000272 Info.setNoWarningAsError(true);
273 }
274
275 return false;
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000276}
277
278bool DiagnosticsEngine::setDiagnosticGroupErrorAsFatal(StringRef Group,
279 bool Enabled) {
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000280 // If we are enabling this feature, just set the diagnostic mappings to map to
281 // fatal errors.
282 if (Enabled)
283 return setDiagnosticGroupMapping(Group, diag::MAP_FATAL);
284
285 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
286 // potentially downgrade anything already mapped to be an error.
287
288 // Get the diagnostics in this group.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000289 SmallVector<diag::kind, 8> GroupDiags;
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000290 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
291 return true;
292
293 // Perform the mapping change.
294 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i) {
295 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(
296 GroupDiags[i]);
297
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000298 if (Info.getMapping() == diag::MAP_FATAL)
299 Info.setMapping(diag::MAP_ERROR);
300
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000301 Info.setNoErrorAsFatal(true);
302 }
303
304 return false;
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000305}
306
Argyrios Kyrtzidis059cac42012-01-28 04:35:52 +0000307void DiagnosticsEngine::setMappingToAllDiagnostics(diag::Mapping Map,
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000308 SourceLocation Loc) {
309 // Get all the diagnostics.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000310 SmallVector<diag::kind, 64> AllDiags;
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000311 Diags->getAllDiagnostics(AllDiags);
312
313 // Set the mapping.
314 for (unsigned i = 0, e = AllDiags.size(); i != e; ++i)
315 if (Diags->isBuiltinWarningOrExtension(AllDiags[i]))
316 setDiagnosticMapping(AllDiags[i], Map, Loc);
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000317}
318
David Blaikie9c902b52011-09-25 23:23:43 +0000319void DiagnosticsEngine::Report(const StoredDiagnostic &storedDiag) {
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000320 assert(CurDiagID == ~0U && "Multiple diagnostics in flight at once!");
321
322 CurDiagLoc = storedDiag.getLocation();
323 CurDiagID = storedDiag.getID();
324 NumDiagArgs = 0;
325
326 NumDiagRanges = storedDiag.range_size();
Daniel Dunbar007b9dc2012-03-13 18:21:17 +0000327 assert(NumDiagRanges < DiagnosticsEngine::MaxRanges &&
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000328 "Too many arguments to diagnostic!");
329 unsigned i = 0;
330 for (StoredDiagnostic::range_iterator
331 RI = storedDiag.range_begin(),
332 RE = storedDiag.range_end(); RI != RE; ++RI)
333 DiagRanges[i++] = *RI;
334
Daniel Dunbar007b9dc2012-03-13 18:21:17 +0000335 assert(NumDiagRanges < DiagnosticsEngine::MaxFixItHints &&
336 "Too many arguments to diagnostic!");
337 NumDiagFixItHints = 0;
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000338 for (StoredDiagnostic::fixit_iterator
339 FI = storedDiag.fixit_begin(),
340 FE = storedDiag.fixit_end(); FI != FE; ++FI)
Daniel Dunbar007b9dc2012-03-13 18:21:17 +0000341 DiagFixItHints[NumDiagFixItHints++] = *FI;
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000342
David Blaikiee2eefae2011-09-25 23:39:51 +0000343 assert(Client && "DiagnosticConsumer not set!");
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000344 Level DiagLevel = storedDiag.getLevel();
David Blaikieb5784322011-09-26 01:18:08 +0000345 Diagnostic Info(this, storedDiag.getMessage());
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000346 Client->HandleDiagnostic(DiagLevel, Info);
347 if (Client->IncludeInDiagnosticCounts()) {
David Blaikie9c902b52011-09-25 23:23:43 +0000348 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000349 ++NumWarnings;
350 }
351
352 CurDiagID = ~0U;
353}
354
Jordan Rose6f524ac2012-07-11 16:50:36 +0000355bool DiagnosticsEngine::EmitCurrentDiagnostic(bool Force) {
356 assert(getClient() && "DiagnosticClient not set!");
357
358 bool Emitted;
359 if (Force) {
360 Diagnostic Info(this);
361
362 // Figure out the diagnostic level of this message.
363 DiagnosticIDs::Level DiagLevel
364 = Diags->getDiagnosticLevel(Info.getID(), Info.getLocation(), *this);
365
366 Emitted = (DiagLevel != DiagnosticIDs::Ignored);
367 if (Emitted) {
368 // Emit the diagnostic regardless of suppression level.
369 Diags->EmitDiag(*this, DiagLevel);
370 }
371 } else {
372 // Process the diagnostic, sending the accumulated information to the
373 // DiagnosticConsumer.
374 Emitted = ProcessDiag();
375 }
Douglas Gregor85795312010-03-22 15:10:57 +0000376
377 // Clear out the current diagnostic object.
Daniel Dunbarc7c00892012-03-13 21:02:14 +0000378 unsigned DiagID = CurDiagID;
379 Clear();
Douglas Gregor85795312010-03-22 15:10:57 +0000380
381 // If there was a delayed diagnostic, emit it now.
Jordan Rose6f524ac2012-07-11 16:50:36 +0000382 if (!Force && DelayedDiagID && DelayedDiagID != DiagID)
Daniel Dunbarc7c00892012-03-13 21:02:14 +0000383 ReportDelayed();
Douglas Gregor85795312010-03-22 15:10:57 +0000384
385 return Emitted;
386}
387
Nico Weber4c311642008-08-10 19:59:06 +0000388
David Blaikiee2eefae2011-09-25 23:39:51 +0000389DiagnosticConsumer::~DiagnosticConsumer() {}
Nico Weber4c311642008-08-10 19:59:06 +0000390
David Blaikiee2eefae2011-09-25 23:39:51 +0000391void DiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
David Blaikieb5784322011-09-26 01:18:08 +0000392 const Diagnostic &Info) {
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000393 if (!IncludeInDiagnosticCounts())
394 return;
395
David Blaikie9c902b52011-09-25 23:23:43 +0000396 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000397 ++NumWarnings;
David Blaikie9c902b52011-09-25 23:23:43 +0000398 else if (DiagLevel >= DiagnosticsEngine::Error)
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000399 ++NumErrors;
400}
Chris Lattner23be0672008-11-19 06:51:40 +0000401
Chris Lattner2b786902008-11-21 07:50:02 +0000402/// ModifierIs - Return true if the specified modifier matches specified string.
403template <std::size_t StrLen>
404static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
405 const char (&Str)[StrLen]) {
406 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
407}
408
John McCall8cb7a8a32010-01-14 20:11:39 +0000409/// ScanForward - Scans forward, looking for the given character, skipping
410/// nested clauses and escaped characters.
411static const char *ScanFormat(const char *I, const char *E, char Target) {
412 unsigned Depth = 0;
413
414 for ( ; I != E; ++I) {
415 if (Depth == 0 && *I == Target) return I;
416 if (Depth != 0 && *I == '}') Depth--;
417
418 if (*I == '%') {
419 I++;
420 if (I == E) break;
421
422 // Escaped characters get implicitly skipped here.
423
424 // Format specifier.
Jordan Rosea7d03842013-02-08 22:30:41 +0000425 if (!isDigit(*I) && !isPunctuation(*I)) {
426 for (I++; I != E && !isDigit(*I) && *I != '{'; I++) ;
John McCall8cb7a8a32010-01-14 20:11:39 +0000427 if (I == E) break;
428 if (*I == '{')
429 Depth++;
430 }
431 }
432 }
433 return E;
434}
435
Chris Lattner2b786902008-11-21 07:50:02 +0000436/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
437/// like this: %select{foo|bar|baz}2. This means that the integer argument
438/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
439/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
440/// This is very useful for certain classes of variant diagnostics.
David Blaikieb5784322011-09-26 01:18:08 +0000441static void HandleSelectModifier(const Diagnostic &DInfo, unsigned ValNo,
Chris Lattner2b786902008-11-21 07:50:02 +0000442 const char *Argument, unsigned ArgumentLen,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000443 SmallVectorImpl<char> &OutStr) {
Chris Lattner2b786902008-11-21 07:50:02 +0000444 const char *ArgumentEnd = Argument+ArgumentLen;
Mike Stump11289f42009-09-09 15:08:12 +0000445
Chris Lattner2b786902008-11-21 07:50:02 +0000446 // Skip over 'ValNo' |'s.
447 while (ValNo) {
John McCall8cb7a8a32010-01-14 20:11:39 +0000448 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
Chris Lattner2b786902008-11-21 07:50:02 +0000449 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
450 " larger than the number of options in the diagnostic string!");
451 Argument = NextVal+1; // Skip this string.
452 --ValNo;
453 }
Mike Stump11289f42009-09-09 15:08:12 +0000454
Chris Lattner2b786902008-11-21 07:50:02 +0000455 // Get the end of the value. This is either the } or the |.
John McCall8cb7a8a32010-01-14 20:11:39 +0000456 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
John McCalle4d54322010-01-13 23:58:20 +0000457
458 // Recursively format the result of the select clause into the output string.
459 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000460}
461
462/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
463/// letter 's' to the string if the value is not 1. This is used in cases like
464/// this: "you idiot, you have %4 parameter%s4!".
465static void HandleIntegerSModifier(unsigned ValNo,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000466 SmallVectorImpl<char> &OutStr) {
Chris Lattner2b786902008-11-21 07:50:02 +0000467 if (ValNo != 1)
468 OutStr.push_back('s');
469}
470
John McCall9015cde2010-01-14 00:50:32 +0000471/// HandleOrdinalModifier - Handle the integer 'ord' modifier. This
472/// prints the ordinal form of the given integer, with 1 corresponding
473/// to the first ordinal. Currently this is hard-coded to use the
474/// English form.
475static void HandleOrdinalModifier(unsigned ValNo,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000476 SmallVectorImpl<char> &OutStr) {
John McCall9015cde2010-01-14 00:50:32 +0000477 assert(ValNo != 0 && "ValNo must be strictly positive!");
478
479 llvm::raw_svector_ostream Out(OutStr);
480
481 // We could use text forms for the first N ordinals, but the numeric
482 // forms are actually nicer in diagnostics because they stand out.
Jordan Rosec102b352012-09-22 01:24:42 +0000483 Out << ValNo << llvm::getOrdinalSuffix(ValNo);
John McCall9015cde2010-01-14 00:50:32 +0000484}
485
Chris Lattner2b786902008-11-21 07:50:02 +0000486
Sebastian Redl15b02d22008-11-22 13:44:36 +0000487/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000488static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000489 // Programming 101: Parse a decimal number :-)
490 unsigned Val = 0;
491 while (Start != End && *Start >= '0' && *Start <= '9') {
492 Val *= 10;
493 Val += *Start - '0';
494 ++Start;
495 }
496 return Val;
497}
498
499/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000500static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000501 if (*Start != '[') {
502 unsigned Ref = PluralNumber(Start, End);
503 return Ref == Val;
504 }
505
506 ++Start;
507 unsigned Low = PluralNumber(Start, End);
508 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
509 ++Start;
510 unsigned High = PluralNumber(Start, End);
511 assert(*Start == ']' && "Bad plural expression syntax: expected )");
512 ++Start;
513 return Low <= Val && Val <= High;
514}
515
516/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattner2fe29202009-04-15 17:13:42 +0000517static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000518 // Empty condition?
519 if (*Start == ':')
520 return true;
521
522 while (1) {
523 char C = *Start;
524 if (C == '%') {
525 // Modulo expression
526 ++Start;
527 unsigned Arg = PluralNumber(Start, End);
528 assert(*Start == '=' && "Bad plural expression syntax: expected =");
529 ++Start;
530 unsigned ValMod = ValNo % Arg;
531 if (TestPluralRange(ValMod, Start, End))
532 return true;
533 } else {
Sebastian Redl3ceaf622008-11-27 07:28:14 +0000534 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redl15b02d22008-11-22 13:44:36 +0000535 "Bad plural expression syntax: unexpected character");
536 // Range expression
537 if (TestPluralRange(ValNo, Start, End))
538 return true;
539 }
540
541 // Scan for next or-expr part.
542 Start = std::find(Start, End, ',');
Mike Stump11289f42009-09-09 15:08:12 +0000543 if (Start == End)
Sebastian Redl15b02d22008-11-22 13:44:36 +0000544 break;
545 ++Start;
546 }
547 return false;
548}
549
550/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
551/// for complex plural forms, or in languages where all plurals are complex.
552/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
553/// conditions that are tested in order, the form corresponding to the first
554/// that applies being emitted. The empty condition is always true, making the
555/// last form a default case.
556/// Conditions are simple boolean expressions, where n is the number argument.
557/// Here are the rules.
558/// condition := expression | empty
559/// empty := -> always true
560/// expression := numeric [',' expression] -> logical or
561/// numeric := range -> true if n in range
562/// | '%' number '=' range -> true if n % number in range
563/// range := number
564/// | '[' number ',' number ']' -> ranges are inclusive both ends
565///
566/// Here are some examples from the GNU gettext manual written in this form:
567/// English:
568/// {1:form0|:form1}
569/// Latvian:
570/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
571/// Gaeilge:
572/// {1:form0|2:form1|:form2}
573/// Romanian:
574/// {1:form0|0,%100=[1,19]:form1|:form2}
575/// Lithuanian:
576/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
577/// Russian (requires repeated form):
578/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
579/// Slovak
580/// {1:form0|[2,4]:form1|:form2}
581/// Polish (requires repeated form):
582/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
David Blaikieb5784322011-09-26 01:18:08 +0000583static void HandlePluralModifier(const Diagnostic &DInfo, unsigned ValNo,
Sebastian Redl15b02d22008-11-22 13:44:36 +0000584 const char *Argument, unsigned ArgumentLen,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000585 SmallVectorImpl<char> &OutStr) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000586 const char *ArgumentEnd = Argument + ArgumentLen;
587 while (1) {
588 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
589 const char *ExprEnd = Argument;
590 while (*ExprEnd != ':') {
591 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
592 ++ExprEnd;
593 }
594 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
595 Argument = ExprEnd + 1;
John McCall8cb7a8a32010-01-14 20:11:39 +0000596 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
John McCall43b61682010-10-14 01:55:31 +0000597
598 // Recursively format the result of the plural clause into the
599 // output string.
600 DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000601 return;
602 }
John McCall8cb7a8a32010-01-14 20:11:39 +0000603 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
Sebastian Redl15b02d22008-11-22 13:44:36 +0000604 }
605}
606
Alp Tokera231ad22014-01-06 12:54:18 +0000607/// \brief Returns the friendly description for a token kind that will appear
608/// without quotes in diagnostic messages. These strings may be translatable in
609/// future.
610static const char *getTokenDescForDiagnostic(tok::TokenKind Kind) {
Alp Tokerec543272013-12-24 09:48:30 +0000611 switch (Kind) {
612 case tok::identifier:
613 return "identifier";
614 default:
Craig Topperf1186c52014-05-08 06:41:40 +0000615 return nullptr;
Alp Tokerec543272013-12-24 09:48:30 +0000616 }
617}
Sebastian Redl15b02d22008-11-22 13:44:36 +0000618
Chris Lattner23be0672008-11-19 06:51:40 +0000619/// FormatDiagnostic - Format this diagnostic into a string, substituting the
620/// formal arguments into the %0 slots. The result is appended onto the Str
621/// array.
David Blaikieb5784322011-09-26 01:18:08 +0000622void Diagnostic::
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000623FormatDiagnostic(SmallVectorImpl<char> &OutStr) const {
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000624 if (!StoredDiagMessage.empty()) {
625 OutStr.append(StoredDiagMessage.begin(), StoredDiagMessage.end());
626 return;
627 }
628
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000629 StringRef Diag =
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000630 getDiags()->getDiagnosticIDs()->getDescription(getID());
Mike Stump11289f42009-09-09 15:08:12 +0000631
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000632 FormatDiagnostic(Diag.begin(), Diag.end(), OutStr);
John McCalle4d54322010-01-13 23:58:20 +0000633}
634
David Blaikieb5784322011-09-26 01:18:08 +0000635void Diagnostic::
John McCalle4d54322010-01-13 23:58:20 +0000636FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000637 SmallVectorImpl<char> &OutStr) const {
John McCalle4d54322010-01-13 23:58:20 +0000638
Chris Lattnerc243f292009-10-20 05:25:22 +0000639 /// FormattedArgs - Keep track of all of the arguments formatted by
640 /// ConvertArgToString and pass them into subsequent calls to
641 /// ConvertArgToString, allowing the implementation to avoid redundancies in
642 /// obvious cases.
David Blaikie9c902b52011-09-25 23:23:43 +0000643 SmallVector<DiagnosticsEngine::ArgumentValue, 8> FormattedArgs;
Chandler Carruthd5173952011-07-11 17:49:21 +0000644
645 /// QualTypeVals - Pass a vector of arrays so that QualType names can be
646 /// compared to see if more information is needed to be printed.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000647 SmallVector<intptr_t, 2> QualTypeVals;
Richard Trieu91844232012-06-26 18:18:47 +0000648 SmallVector<char, 64> Tree;
649
Chandler Carruthd5173952011-07-11 17:49:21 +0000650 for (unsigned i = 0, e = getNumArgs(); i < e; ++i)
David Blaikie9c902b52011-09-25 23:23:43 +0000651 if (getArgKind(i) == DiagnosticsEngine::ak_qualtype)
Chandler Carruthd5173952011-07-11 17:49:21 +0000652 QualTypeVals.push_back(getRawArg(i));
653
Chris Lattner23be0672008-11-19 06:51:40 +0000654 while (DiagStr != DiagEnd) {
655 if (DiagStr[0] != '%') {
656 // Append non-%0 substrings to Str if we have one.
657 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
658 OutStr.append(DiagStr, StrEnd);
659 DiagStr = StrEnd;
Chris Lattner2b786902008-11-21 07:50:02 +0000660 continue;
Jordan Rosea7d03842013-02-08 22:30:41 +0000661 } else if (isPunctuation(DiagStr[1])) {
John McCall8cb7a8a32010-01-14 20:11:39 +0000662 OutStr.push_back(DiagStr[1]); // %% -> %.
Chris Lattner23be0672008-11-19 06:51:40 +0000663 DiagStr += 2;
Chris Lattner2b786902008-11-21 07:50:02 +0000664 continue;
665 }
Mike Stump11289f42009-09-09 15:08:12 +0000666
Chris Lattner2b786902008-11-21 07:50:02 +0000667 // Skip the %.
668 ++DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000669
Chris Lattner2b786902008-11-21 07:50:02 +0000670 // This must be a placeholder for a diagnostic argument. The format for a
671 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
672 // The digit is a number from 0-9 indicating which argument this comes from.
673 // The modifier is a string of digits from the set [-a-z]+, arguments is a
674 // brace enclosed string.
Craig Topperf1186c52014-05-08 06:41:40 +0000675 const char *Modifier = nullptr, *Argument = nullptr;
Chris Lattner2b786902008-11-21 07:50:02 +0000676 unsigned ModifierLen = 0, ArgumentLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000677
Chris Lattner2b786902008-11-21 07:50:02 +0000678 // Check to see if we have a modifier. If so eat it.
Jordan Rosea7d03842013-02-08 22:30:41 +0000679 if (!isDigit(DiagStr[0])) {
Chris Lattner2b786902008-11-21 07:50:02 +0000680 Modifier = DiagStr;
681 while (DiagStr[0] == '-' ||
682 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
683 ++DiagStr;
684 ModifierLen = DiagStr-Modifier;
Chris Lattner23be0672008-11-19 06:51:40 +0000685
Chris Lattner2b786902008-11-21 07:50:02 +0000686 // If we have an argument, get it next.
687 if (DiagStr[0] == '{') {
688 ++DiagStr; // Skip {.
689 Argument = DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000690
John McCall8cb7a8a32010-01-14 20:11:39 +0000691 DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
692 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
Chris Lattner2b786902008-11-21 07:50:02 +0000693 ArgumentLen = DiagStr-Argument;
694 ++DiagStr; // Skip }.
Chris Lattner23be0672008-11-19 06:51:40 +0000695 }
Chris Lattner2b786902008-11-21 07:50:02 +0000696 }
Mike Stump11289f42009-09-09 15:08:12 +0000697
Jordan Rosea7d03842013-02-08 22:30:41 +0000698 assert(isDigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000699 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattner2b786902008-11-21 07:50:02 +0000700
Richard Trieu91844232012-06-26 18:18:47 +0000701 // Only used for type diffing.
702 unsigned ArgNo2 = ArgNo;
703
David Blaikie9c902b52011-09-25 23:23:43 +0000704 DiagnosticsEngine::ArgumentKind Kind = getArgKind(ArgNo);
Richard Trieu90c31f52013-01-30 20:04:31 +0000705 if (ModifierIs(Modifier, ModifierLen, "diff")) {
Jordan Rosea7d03842013-02-08 22:30:41 +0000706 assert(*DiagStr == ',' && isDigit(*(DiagStr + 1)) &&
Richard Trieu91844232012-06-26 18:18:47 +0000707 "Invalid format for diff modifier");
708 ++DiagStr; // Comma.
709 ArgNo2 = *DiagStr++ - '0';
Richard Trieu90c31f52013-01-30 20:04:31 +0000710 DiagnosticsEngine::ArgumentKind Kind2 = getArgKind(ArgNo2);
711 if (Kind == DiagnosticsEngine::ak_qualtype &&
712 Kind2 == DiagnosticsEngine::ak_qualtype)
713 Kind = DiagnosticsEngine::ak_qualtype_pair;
714 else {
715 // %diff only supports QualTypes. For other kinds of arguments,
716 // use the default printing. For example, if the modifier is:
717 // "%diff{compare $ to $|other text}1,2"
718 // treat it as:
719 // "compare %1 to %2"
720 const char *Pipe = ScanFormat(Argument, Argument + ArgumentLen, '|');
721 const char *FirstDollar = ScanFormat(Argument, Pipe, '$');
722 const char *SecondDollar = ScanFormat(FirstDollar + 1, Pipe, '$');
Filipe Cabecinhased4a00c2013-01-30 22:03:24 +0000723 const char ArgStr1[] = { '%', static_cast<char>('0' + ArgNo) };
724 const char ArgStr2[] = { '%', static_cast<char>('0' + ArgNo2) };
Richard Trieu90c31f52013-01-30 20:04:31 +0000725 FormatDiagnostic(Argument, FirstDollar, OutStr);
726 FormatDiagnostic(ArgStr1, ArgStr1 + 2, OutStr);
727 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
728 FormatDiagnostic(ArgStr2, ArgStr2 + 2, OutStr);
729 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
730 continue;
731 }
Richard Trieu91844232012-06-26 18:18:47 +0000732 }
Chris Lattnerc243f292009-10-20 05:25:22 +0000733
734 switch (Kind) {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000735 // ---- STRINGS ----
David Blaikie9c902b52011-09-25 23:23:43 +0000736 case DiagnosticsEngine::ak_std_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000737 const std::string &S = getArgStdStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000738 assert(ModifierLen == 0 && "No modifiers for strings yet");
739 OutStr.append(S.begin(), S.end());
740 break;
741 }
David Blaikie9c902b52011-09-25 23:23:43 +0000742 case DiagnosticsEngine::ak_c_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000743 const char *S = getArgCStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000744 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000745
746 // Don't crash if get passed a null pointer by accident.
747 if (!S)
748 S = "(null)";
Mike Stump11289f42009-09-09 15:08:12 +0000749
Chris Lattner2b786902008-11-21 07:50:02 +0000750 OutStr.append(S, S + strlen(S));
751 break;
752 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000753 // ---- INTEGERS ----
David Blaikie9c902b52011-09-25 23:23:43 +0000754 case DiagnosticsEngine::ak_sint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000755 int Val = getArgSInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000756
Chris Lattner2b786902008-11-21 07:50:02 +0000757 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCall43b61682010-10-14 01:55:31 +0000758 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen,
759 OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000760 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
761 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000762 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCall43b61682010-10-14 01:55:31 +0000763 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
764 OutStr);
John McCall9015cde2010-01-14 00:50:32 +0000765 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
766 HandleOrdinalModifier((unsigned)Val, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000767 } else {
768 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000769 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000770 }
Chris Lattner2b786902008-11-21 07:50:02 +0000771 break;
772 }
David Blaikie9c902b52011-09-25 23:23:43 +0000773 case DiagnosticsEngine::ak_uint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000774 unsigned Val = getArgUInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000775
Chris Lattner2b786902008-11-21 07:50:02 +0000776 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCalle4d54322010-01-13 23:58:20 +0000777 HandleSelectModifier(*this, Val, Argument, ArgumentLen, 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(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 Lattner6a2ed6f2008-11-23 09:13:29 +0000789 break;
Chris Lattner2b786902008-11-21 07:50:02 +0000790 }
Alp Tokerec543272013-12-24 09:48:30 +0000791 // ---- TOKEN SPELLINGS ----
792 case DiagnosticsEngine::ak_tokenkind: {
793 tok::TokenKind Kind = static_cast<tok::TokenKind>(getRawArg(ArgNo));
794 assert(ModifierLen == 0 && "No modifiers for token kinds yet");
795
796 llvm::raw_svector_ostream Out(OutStr);
Alp Tokera231ad22014-01-06 12:54:18 +0000797 if (const char *S = tok::getPunctuatorSpelling(Kind))
798 // Quoted token spelling for punctuators.
799 Out << '\'' << S << '\'';
800 else if (const char *S = tok::getKeywordSpelling(Kind))
801 // Unquoted token spelling for keywords.
802 Out << S;
803 else if (const char *S = getTokenDescForDiagnostic(Kind))
Alp Tokerec543272013-12-24 09:48:30 +0000804 // Unquoted translatable token name.
805 Out << S;
Alp Tokerec543272013-12-24 09:48:30 +0000806 else if (const char *S = tok::getTokenName(Kind))
807 // Debug name, shouldn't appear in user-facing diagnostics.
808 Out << '<' << S << '>';
809 else
810 Out << "(null)";
811 break;
812 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000813 // ---- NAMES and TYPES ----
David Blaikie9c902b52011-09-25 23:23:43 +0000814 case DiagnosticsEngine::ak_identifierinfo: {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000815 const IdentifierInfo *II = getArgIdentifier(ArgNo);
816 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000817
818 // Don't crash if get passed a null pointer by accident.
819 if (!II) {
820 const char *S = "(null)";
821 OutStr.append(S, S + strlen(S));
822 continue;
823 }
824
Daniel Dunbar07d07852009-10-18 21:17:35 +0000825 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
Chris Lattnere3d20d92008-11-23 21:45:46 +0000826 break;
827 }
David Blaikie9c902b52011-09-25 23:23:43 +0000828 case DiagnosticsEngine::ak_qualtype:
829 case DiagnosticsEngine::ak_declarationname:
830 case DiagnosticsEngine::ak_nameddecl:
831 case DiagnosticsEngine::ak_nestednamespec:
832 case DiagnosticsEngine::ak_declcontext:
Aaron Ballman3e424b52013-12-26 18:30:57 +0000833 case DiagnosticsEngine::ak_attr:
Chris Lattnerc243f292009-10-20 05:25:22 +0000834 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
Chris Lattner63ecc502008-11-23 09:21:17 +0000835 Modifier, ModifierLen,
Chris Lattnerc243f292009-10-20 05:25:22 +0000836 Argument, ArgumentLen,
837 FormattedArgs.data(), FormattedArgs.size(),
Chandler Carruthd5173952011-07-11 17:49:21 +0000838 OutStr, QualTypeVals);
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000839 break;
Richard Trieu91844232012-06-26 18:18:47 +0000840 case DiagnosticsEngine::ak_qualtype_pair:
841 // Create a struct with all the info needed for printing.
842 TemplateDiffTypes TDT;
843 TDT.FromType = getRawArg(ArgNo);
844 TDT.ToType = getRawArg(ArgNo2);
845 TDT.ElideType = getDiags()->ElideType;
846 TDT.ShowColors = getDiags()->ShowColors;
Richard Trieu50f5f462012-07-10 01:46:04 +0000847 TDT.TemplateDiffUsed = false;
Richard Trieu91844232012-06-26 18:18:47 +0000848 intptr_t val = reinterpret_cast<intptr_t>(&TDT);
849
Richard Trieuc6058442012-06-29 21:12:16 +0000850 const char *ArgumentEnd = Argument + ArgumentLen;
851 const char *Pipe = ScanFormat(Argument, ArgumentEnd, '|');
852
Richard Trieua4056002012-07-13 21:18:32 +0000853 // Print the tree. If this diagnostic already has a tree, skip the
854 // second tree.
855 if (getDiags()->PrintTemplateTree && Tree.empty()) {
Richard Trieu91844232012-06-26 18:18:47 +0000856 TDT.PrintFromType = true;
857 TDT.PrintTree = true;
858 getDiags()->ConvertArgToString(Kind, val,
859 Modifier, ModifierLen,
860 Argument, ArgumentLen,
861 FormattedArgs.data(),
862 FormattedArgs.size(),
863 Tree, QualTypeVals);
864 // If there is no tree information, fall back to regular printing.
Richard Trieuc6058442012-06-29 21:12:16 +0000865 if (!Tree.empty()) {
866 FormatDiagnostic(Pipe + 1, ArgumentEnd, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +0000867 break;
Richard Trieuc6058442012-06-29 21:12:16 +0000868 }
Richard Trieu91844232012-06-26 18:18:47 +0000869 }
870
871 // Non-tree printing, also the fall-back when tree printing fails.
872 // The fall-back is triggered when the types compared are not templates.
Richard Trieuc6058442012-06-29 21:12:16 +0000873 const char *FirstDollar = ScanFormat(Argument, ArgumentEnd, '$');
874 const char *SecondDollar = ScanFormat(FirstDollar + 1, ArgumentEnd, '$');
Richard Trieu91844232012-06-26 18:18:47 +0000875
876 // Append before text
Richard Trieuc6058442012-06-29 21:12:16 +0000877 FormatDiagnostic(Argument, FirstDollar, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +0000878
879 // Append first type
880 TDT.PrintTree = false;
881 TDT.PrintFromType = true;
882 getDiags()->ConvertArgToString(Kind, val,
883 Modifier, ModifierLen,
884 Argument, ArgumentLen,
885 FormattedArgs.data(), FormattedArgs.size(),
886 OutStr, QualTypeVals);
Richard Trieu50f5f462012-07-10 01:46:04 +0000887 if (!TDT.TemplateDiffUsed)
888 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
889 TDT.FromType));
890
Richard Trieu91844232012-06-26 18:18:47 +0000891 // Append middle text
Richard Trieuc6058442012-06-29 21:12:16 +0000892 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +0000893
894 // Append second type
895 TDT.PrintFromType = false;
896 getDiags()->ConvertArgToString(Kind, val,
897 Modifier, ModifierLen,
898 Argument, ArgumentLen,
899 FormattedArgs.data(), FormattedArgs.size(),
900 OutStr, QualTypeVals);
Richard Trieu50f5f462012-07-10 01:46:04 +0000901 if (!TDT.TemplateDiffUsed)
902 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
903 TDT.ToType));
904
Richard Trieu91844232012-06-26 18:18:47 +0000905 // Append end text
Richard Trieuc6058442012-06-29 21:12:16 +0000906 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +0000907 break;
Nico Weber4c311642008-08-10 19:59:06 +0000908 }
Chris Lattnerc243f292009-10-20 05:25:22 +0000909
910 // Remember this argument info for subsequent formatting operations. Turn
911 // std::strings into a null terminated string to make it be the same case as
912 // all the other ones.
Richard Trieu91844232012-06-26 18:18:47 +0000913 if (Kind == DiagnosticsEngine::ak_qualtype_pair)
914 continue;
915 else if (Kind != DiagnosticsEngine::ak_std_string)
Chris Lattnerc243f292009-10-20 05:25:22 +0000916 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
917 else
David Blaikie9c902b52011-09-25 23:23:43 +0000918 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_c_string,
Chris Lattnerc243f292009-10-20 05:25:22 +0000919 (intptr_t)getArgStdStr(ArgNo).c_str()));
920
Nico Weber4c311642008-08-10 19:59:06 +0000921 }
Richard Trieu91844232012-06-26 18:18:47 +0000922
923 // Append the type tree to the end of the diagnostics.
924 OutStr.append(Tree.begin(), Tree.end());
Nico Weber4c311642008-08-10 19:59:06 +0000925}
Ted Kremenekea06ec12009-01-23 20:28:53 +0000926
Douglas Gregor33cdd812010-02-18 18:08:43 +0000927StoredDiagnostic::StoredDiagnostic() { }
928
David Blaikie9c902b52011-09-25 23:23:43 +0000929StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000930 StringRef Message)
Benjamin Kramer929bd682010-11-19 17:36:51 +0000931 : ID(ID), Level(Level), Loc(), Message(Message) { }
Douglas Gregor33cdd812010-02-18 18:08:43 +0000932
David Blaikie9c902b52011-09-25 23:23:43 +0000933StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level,
David Blaikieb5784322011-09-26 01:18:08 +0000934 const Diagnostic &Info)
Douglas Gregora750e8e2010-11-19 16:18:16 +0000935 : ID(Info.getID()), Level(Level)
936{
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000937 assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
938 "Valid source location without setting a source manager for diagnostic");
939 if (Info.getLocation().isValid())
940 Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000941 SmallString<64> Message;
Douglas Gregor33cdd812010-02-18 18:08:43 +0000942 Info.FormatDiagnostic(Message);
943 this->Message.assign(Message.begin(), Message.end());
944
945 Ranges.reserve(Info.getNumRanges());
946 for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I)
947 Ranges.push_back(Info.getRange(I));
948
Douglas Gregora771f462010-03-31 17:46:05 +0000949 FixIts.reserve(Info.getNumFixItHints());
950 for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I)
951 FixIts.push_back(Info.getFixItHint(I));
Douglas Gregor33cdd812010-02-18 18:08:43 +0000952}
953
David Blaikie9c902b52011-09-25 23:23:43 +0000954StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000955 StringRef Message, FullSourceLoc Loc,
Chris Lattner54b16772011-07-23 17:14:25 +0000956 ArrayRef<CharSourceRange> Ranges,
Aaron Ballman234ebd72013-02-24 19:08:10 +0000957 ArrayRef<FixItHint> FixIts)
958 : ID(ID), Level(Level), Loc(Loc), Message(Message),
959 Ranges(Ranges.begin(), Ranges.end()), FixIts(FixIts.begin(), FixIts.end())
Douglas Gregor925296b2011-07-19 16:10:42 +0000960{
Douglas Gregor925296b2011-07-19 16:10:42 +0000961}
962
Douglas Gregor33cdd812010-02-18 18:08:43 +0000963StoredDiagnostic::~StoredDiagnostic() { }
964
Ted Kremenekea06ec12009-01-23 20:28:53 +0000965/// IncludeInDiagnosticCounts - This method (whose default implementation
966/// returns true) indicates whether the diagnostics handled by this
David Blaikiee2eefae2011-09-25 23:39:51 +0000967/// DiagnosticConsumer should be included in the number of diagnostics
David Blaikie9c902b52011-09-25 23:23:43 +0000968/// reported by DiagnosticsEngine.
David Blaikiee2eefae2011-09-25 23:39:51 +0000969bool DiagnosticConsumer::IncludeInDiagnosticCounts() const { return true; }
Douglas Gregor89336232010-03-29 23:34:08 +0000970
David Blaikie68e081d2011-12-20 02:48:34 +0000971void IgnoringDiagConsumer::anchor() { }
972
Douglas Gregor6b930962013-05-03 22:58:43 +0000973ForwardingDiagnosticConsumer::~ForwardingDiagnosticConsumer() {}
974
975void ForwardingDiagnosticConsumer::HandleDiagnostic(
976 DiagnosticsEngine::Level DiagLevel,
977 const Diagnostic &Info) {
978 Target.HandleDiagnostic(DiagLevel, Info);
979}
980
981void ForwardingDiagnosticConsumer::clear() {
982 DiagnosticConsumer::clear();
983 Target.clear();
984}
985
986bool ForwardingDiagnosticConsumer::IncludeInDiagnosticCounts() const {
987 return Target.IncludeInDiagnosticCounts();
988}
989
Benjamin Kramer7ec12c92012-02-07 22:29:24 +0000990PartialDiagnostic::StorageAllocator::StorageAllocator() {
Douglas Gregor89336232010-03-29 23:34:08 +0000991 for (unsigned I = 0; I != NumCached; ++I)
992 FreeList[I] = Cached + I;
993 NumFreeListEntries = NumCached;
994}
995
Benjamin Kramer7ec12c92012-02-07 22:29:24 +0000996PartialDiagnostic::StorageAllocator::~StorageAllocator() {
Chad Rosier849a67b2012-02-07 23:24:49 +0000997 // Don't assert if we are in a CrashRecovery context, as this invariant may
998 // be invalidated during a crash.
999 assert((NumFreeListEntries == NumCached ||
1000 llvm::CrashRecoveryContext::isRecoveringFromCrash()) &&
1001 "A partial is on the lamb");
Douglas Gregor89336232010-03-29 23:34:08 +00001002}