blob: 13d25249015970052cf1d99751e6497dd53ad6ef [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
Alexander Kornienkod3b4e082014-05-22 19:56:11 +0000326 DiagRanges.clear();
327 DiagRanges.reserve(storedDiag.range_size());
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000328 for (StoredDiagnostic::range_iterator
329 RI = storedDiag.range_begin(),
330 RE = storedDiag.range_end(); RI != RE; ++RI)
Alexander Kornienkod3b4e082014-05-22 19:56:11 +0000331 DiagRanges.push_back(*RI);
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000332
Alexander Kornienkod3b4e082014-05-22 19:56:11 +0000333 DiagFixItHints.clear();
334 DiagFixItHints.reserve(storedDiag.fixit_size());
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000335 for (StoredDiagnostic::fixit_iterator
336 FI = storedDiag.fixit_begin(),
337 FE = storedDiag.fixit_end(); FI != FE; ++FI)
Alexander Kornienkod3b4e082014-05-22 19:56:11 +0000338 DiagFixItHints.push_back(*FI);
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000339
David Blaikiee2eefae2011-09-25 23:39:51 +0000340 assert(Client && "DiagnosticConsumer not set!");
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000341 Level DiagLevel = storedDiag.getLevel();
David Blaikieb5784322011-09-26 01:18:08 +0000342 Diagnostic Info(this, storedDiag.getMessage());
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000343 Client->HandleDiagnostic(DiagLevel, Info);
344 if (Client->IncludeInDiagnosticCounts()) {
David Blaikie9c902b52011-09-25 23:23:43 +0000345 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000346 ++NumWarnings;
347 }
348
349 CurDiagID = ~0U;
350}
351
Jordan Rose6f524ac2012-07-11 16:50:36 +0000352bool DiagnosticsEngine::EmitCurrentDiagnostic(bool Force) {
353 assert(getClient() && "DiagnosticClient not set!");
354
355 bool Emitted;
356 if (Force) {
357 Diagnostic Info(this);
358
359 // Figure out the diagnostic level of this message.
360 DiagnosticIDs::Level DiagLevel
361 = Diags->getDiagnosticLevel(Info.getID(), Info.getLocation(), *this);
362
363 Emitted = (DiagLevel != DiagnosticIDs::Ignored);
364 if (Emitted) {
365 // Emit the diagnostic regardless of suppression level.
366 Diags->EmitDiag(*this, DiagLevel);
367 }
368 } else {
369 // Process the diagnostic, sending the accumulated information to the
370 // DiagnosticConsumer.
371 Emitted = ProcessDiag();
372 }
Douglas Gregor85795312010-03-22 15:10:57 +0000373
374 // Clear out the current diagnostic object.
Daniel Dunbarc7c00892012-03-13 21:02:14 +0000375 unsigned DiagID = CurDiagID;
376 Clear();
Douglas Gregor85795312010-03-22 15:10:57 +0000377
378 // If there was a delayed diagnostic, emit it now.
Jordan Rose6f524ac2012-07-11 16:50:36 +0000379 if (!Force && DelayedDiagID && DelayedDiagID != DiagID)
Daniel Dunbarc7c00892012-03-13 21:02:14 +0000380 ReportDelayed();
Douglas Gregor85795312010-03-22 15:10:57 +0000381
382 return Emitted;
383}
384
Nico Weber4c311642008-08-10 19:59:06 +0000385
David Blaikiee2eefae2011-09-25 23:39:51 +0000386DiagnosticConsumer::~DiagnosticConsumer() {}
Nico Weber4c311642008-08-10 19:59:06 +0000387
David Blaikiee2eefae2011-09-25 23:39:51 +0000388void DiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
David Blaikieb5784322011-09-26 01:18:08 +0000389 const Diagnostic &Info) {
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000390 if (!IncludeInDiagnosticCounts())
391 return;
392
David Blaikie9c902b52011-09-25 23:23:43 +0000393 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000394 ++NumWarnings;
David Blaikie9c902b52011-09-25 23:23:43 +0000395 else if (DiagLevel >= DiagnosticsEngine::Error)
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000396 ++NumErrors;
397}
Chris Lattner23be0672008-11-19 06:51:40 +0000398
Chris Lattner2b786902008-11-21 07:50:02 +0000399/// ModifierIs - Return true if the specified modifier matches specified string.
400template <std::size_t StrLen>
401static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
402 const char (&Str)[StrLen]) {
403 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
404}
405
John McCall8cb7a8a32010-01-14 20:11:39 +0000406/// ScanForward - Scans forward, looking for the given character, skipping
407/// nested clauses and escaped characters.
408static const char *ScanFormat(const char *I, const char *E, char Target) {
409 unsigned Depth = 0;
410
411 for ( ; I != E; ++I) {
412 if (Depth == 0 && *I == Target) return I;
413 if (Depth != 0 && *I == '}') Depth--;
414
415 if (*I == '%') {
416 I++;
417 if (I == E) break;
418
419 // Escaped characters get implicitly skipped here.
420
421 // Format specifier.
Jordan Rosea7d03842013-02-08 22:30:41 +0000422 if (!isDigit(*I) && !isPunctuation(*I)) {
423 for (I++; I != E && !isDigit(*I) && *I != '{'; I++) ;
John McCall8cb7a8a32010-01-14 20:11:39 +0000424 if (I == E) break;
425 if (*I == '{')
426 Depth++;
427 }
428 }
429 }
430 return E;
431}
432
Chris Lattner2b786902008-11-21 07:50:02 +0000433/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
434/// like this: %select{foo|bar|baz}2. This means that the integer argument
435/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
436/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
437/// This is very useful for certain classes of variant diagnostics.
David Blaikieb5784322011-09-26 01:18:08 +0000438static void HandleSelectModifier(const Diagnostic &DInfo, unsigned ValNo,
Chris Lattner2b786902008-11-21 07:50:02 +0000439 const char *Argument, unsigned ArgumentLen,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000440 SmallVectorImpl<char> &OutStr) {
Chris Lattner2b786902008-11-21 07:50:02 +0000441 const char *ArgumentEnd = Argument+ArgumentLen;
Mike Stump11289f42009-09-09 15:08:12 +0000442
Chris Lattner2b786902008-11-21 07:50:02 +0000443 // Skip over 'ValNo' |'s.
444 while (ValNo) {
John McCall8cb7a8a32010-01-14 20:11:39 +0000445 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
Chris Lattner2b786902008-11-21 07:50:02 +0000446 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
447 " larger than the number of options in the diagnostic string!");
448 Argument = NextVal+1; // Skip this string.
449 --ValNo;
450 }
Mike Stump11289f42009-09-09 15:08:12 +0000451
Chris Lattner2b786902008-11-21 07:50:02 +0000452 // Get the end of the value. This is either the } or the |.
John McCall8cb7a8a32010-01-14 20:11:39 +0000453 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
John McCalle4d54322010-01-13 23:58:20 +0000454
455 // Recursively format the result of the select clause into the output string.
456 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000457}
458
459/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
460/// letter 's' to the string if the value is not 1. This is used in cases like
461/// this: "you idiot, you have %4 parameter%s4!".
462static void HandleIntegerSModifier(unsigned ValNo,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000463 SmallVectorImpl<char> &OutStr) {
Chris Lattner2b786902008-11-21 07:50:02 +0000464 if (ValNo != 1)
465 OutStr.push_back('s');
466}
467
John McCall9015cde2010-01-14 00:50:32 +0000468/// HandleOrdinalModifier - Handle the integer 'ord' modifier. This
469/// prints the ordinal form of the given integer, with 1 corresponding
470/// to the first ordinal. Currently this is hard-coded to use the
471/// English form.
472static void HandleOrdinalModifier(unsigned ValNo,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000473 SmallVectorImpl<char> &OutStr) {
John McCall9015cde2010-01-14 00:50:32 +0000474 assert(ValNo != 0 && "ValNo must be strictly positive!");
475
476 llvm::raw_svector_ostream Out(OutStr);
477
478 // We could use text forms for the first N ordinals, but the numeric
479 // forms are actually nicer in diagnostics because they stand out.
Jordan Rosec102b352012-09-22 01:24:42 +0000480 Out << ValNo << llvm::getOrdinalSuffix(ValNo);
John McCall9015cde2010-01-14 00:50:32 +0000481}
482
Chris Lattner2b786902008-11-21 07:50:02 +0000483
Sebastian Redl15b02d22008-11-22 13:44:36 +0000484/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000485static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000486 // Programming 101: Parse a decimal number :-)
487 unsigned Val = 0;
488 while (Start != End && *Start >= '0' && *Start <= '9') {
489 Val *= 10;
490 Val += *Start - '0';
491 ++Start;
492 }
493 return Val;
494}
495
496/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000497static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000498 if (*Start != '[') {
499 unsigned Ref = PluralNumber(Start, End);
500 return Ref == Val;
501 }
502
503 ++Start;
504 unsigned Low = PluralNumber(Start, End);
505 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
506 ++Start;
507 unsigned High = PluralNumber(Start, End);
508 assert(*Start == ']' && "Bad plural expression syntax: expected )");
509 ++Start;
510 return Low <= Val && Val <= High;
511}
512
513/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattner2fe29202009-04-15 17:13:42 +0000514static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000515 // Empty condition?
516 if (*Start == ':')
517 return true;
518
519 while (1) {
520 char C = *Start;
521 if (C == '%') {
522 // Modulo expression
523 ++Start;
524 unsigned Arg = PluralNumber(Start, End);
525 assert(*Start == '=' && "Bad plural expression syntax: expected =");
526 ++Start;
527 unsigned ValMod = ValNo % Arg;
528 if (TestPluralRange(ValMod, Start, End))
529 return true;
530 } else {
Sebastian Redl3ceaf622008-11-27 07:28:14 +0000531 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redl15b02d22008-11-22 13:44:36 +0000532 "Bad plural expression syntax: unexpected character");
533 // Range expression
534 if (TestPluralRange(ValNo, Start, End))
535 return true;
536 }
537
538 // Scan for next or-expr part.
539 Start = std::find(Start, End, ',');
Mike Stump11289f42009-09-09 15:08:12 +0000540 if (Start == End)
Sebastian Redl15b02d22008-11-22 13:44:36 +0000541 break;
542 ++Start;
543 }
544 return false;
545}
546
547/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
548/// for complex plural forms, or in languages where all plurals are complex.
549/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
550/// conditions that are tested in order, the form corresponding to the first
551/// that applies being emitted. The empty condition is always true, making the
552/// last form a default case.
553/// Conditions are simple boolean expressions, where n is the number argument.
554/// Here are the rules.
555/// condition := expression | empty
556/// empty := -> always true
557/// expression := numeric [',' expression] -> logical or
558/// numeric := range -> true if n in range
559/// | '%' number '=' range -> true if n % number in range
560/// range := number
561/// | '[' number ',' number ']' -> ranges are inclusive both ends
562///
563/// Here are some examples from the GNU gettext manual written in this form:
564/// English:
565/// {1:form0|:form1}
566/// Latvian:
567/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
568/// Gaeilge:
569/// {1:form0|2:form1|:form2}
570/// Romanian:
571/// {1:form0|0,%100=[1,19]:form1|:form2}
572/// Lithuanian:
573/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
574/// Russian (requires repeated form):
575/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
576/// Slovak
577/// {1:form0|[2,4]:form1|:form2}
578/// Polish (requires repeated form):
579/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
David Blaikieb5784322011-09-26 01:18:08 +0000580static void HandlePluralModifier(const Diagnostic &DInfo, unsigned ValNo,
Sebastian Redl15b02d22008-11-22 13:44:36 +0000581 const char *Argument, unsigned ArgumentLen,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000582 SmallVectorImpl<char> &OutStr) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000583 const char *ArgumentEnd = Argument + ArgumentLen;
584 while (1) {
585 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
586 const char *ExprEnd = Argument;
587 while (*ExprEnd != ':') {
588 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
589 ++ExprEnd;
590 }
591 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
592 Argument = ExprEnd + 1;
John McCall8cb7a8a32010-01-14 20:11:39 +0000593 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
John McCall43b61682010-10-14 01:55:31 +0000594
595 // Recursively format the result of the plural clause into the
596 // output string.
597 DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000598 return;
599 }
John McCall8cb7a8a32010-01-14 20:11:39 +0000600 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
Sebastian Redl15b02d22008-11-22 13:44:36 +0000601 }
602}
603
Alp Tokera231ad22014-01-06 12:54:18 +0000604/// \brief Returns the friendly description for a token kind that will appear
605/// without quotes in diagnostic messages. These strings may be translatable in
606/// future.
607static const char *getTokenDescForDiagnostic(tok::TokenKind Kind) {
Alp Tokerec543272013-12-24 09:48:30 +0000608 switch (Kind) {
609 case tok::identifier:
610 return "identifier";
611 default:
Craig Topperf1186c52014-05-08 06:41:40 +0000612 return nullptr;
Alp Tokerec543272013-12-24 09:48:30 +0000613 }
614}
Sebastian Redl15b02d22008-11-22 13:44:36 +0000615
Chris Lattner23be0672008-11-19 06:51:40 +0000616/// FormatDiagnostic - Format this diagnostic into a string, substituting the
617/// formal arguments into the %0 slots. The result is appended onto the Str
618/// array.
David Blaikieb5784322011-09-26 01:18:08 +0000619void Diagnostic::
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000620FormatDiagnostic(SmallVectorImpl<char> &OutStr) const {
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000621 if (!StoredDiagMessage.empty()) {
622 OutStr.append(StoredDiagMessage.begin(), StoredDiagMessage.end());
623 return;
624 }
625
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000626 StringRef Diag =
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000627 getDiags()->getDiagnosticIDs()->getDescription(getID());
Mike Stump11289f42009-09-09 15:08:12 +0000628
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000629 FormatDiagnostic(Diag.begin(), Diag.end(), OutStr);
John McCalle4d54322010-01-13 23:58:20 +0000630}
631
David Blaikieb5784322011-09-26 01:18:08 +0000632void Diagnostic::
John McCalle4d54322010-01-13 23:58:20 +0000633FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000634 SmallVectorImpl<char> &OutStr) const {
John McCalle4d54322010-01-13 23:58:20 +0000635
Chris Lattnerc243f292009-10-20 05:25:22 +0000636 /// FormattedArgs - Keep track of all of the arguments formatted by
637 /// ConvertArgToString and pass them into subsequent calls to
638 /// ConvertArgToString, allowing the implementation to avoid redundancies in
639 /// obvious cases.
David Blaikie9c902b52011-09-25 23:23:43 +0000640 SmallVector<DiagnosticsEngine::ArgumentValue, 8> FormattedArgs;
Chandler Carruthd5173952011-07-11 17:49:21 +0000641
642 /// QualTypeVals - Pass a vector of arrays so that QualType names can be
643 /// compared to see if more information is needed to be printed.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000644 SmallVector<intptr_t, 2> QualTypeVals;
Richard Trieu91844232012-06-26 18:18:47 +0000645 SmallVector<char, 64> Tree;
646
Chandler Carruthd5173952011-07-11 17:49:21 +0000647 for (unsigned i = 0, e = getNumArgs(); i < e; ++i)
David Blaikie9c902b52011-09-25 23:23:43 +0000648 if (getArgKind(i) == DiagnosticsEngine::ak_qualtype)
Chandler Carruthd5173952011-07-11 17:49:21 +0000649 QualTypeVals.push_back(getRawArg(i));
650
Chris Lattner23be0672008-11-19 06:51:40 +0000651 while (DiagStr != DiagEnd) {
652 if (DiagStr[0] != '%') {
653 // Append non-%0 substrings to Str if we have one.
654 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
655 OutStr.append(DiagStr, StrEnd);
656 DiagStr = StrEnd;
Chris Lattner2b786902008-11-21 07:50:02 +0000657 continue;
Jordan Rosea7d03842013-02-08 22:30:41 +0000658 } else if (isPunctuation(DiagStr[1])) {
John McCall8cb7a8a32010-01-14 20:11:39 +0000659 OutStr.push_back(DiagStr[1]); // %% -> %.
Chris Lattner23be0672008-11-19 06:51:40 +0000660 DiagStr += 2;
Chris Lattner2b786902008-11-21 07:50:02 +0000661 continue;
662 }
Mike Stump11289f42009-09-09 15:08:12 +0000663
Chris Lattner2b786902008-11-21 07:50:02 +0000664 // Skip the %.
665 ++DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000666
Chris Lattner2b786902008-11-21 07:50:02 +0000667 // This must be a placeholder for a diagnostic argument. The format for a
668 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
669 // The digit is a number from 0-9 indicating which argument this comes from.
670 // The modifier is a string of digits from the set [-a-z]+, arguments is a
671 // brace enclosed string.
Craig Topperf1186c52014-05-08 06:41:40 +0000672 const char *Modifier = nullptr, *Argument = nullptr;
Chris Lattner2b786902008-11-21 07:50:02 +0000673 unsigned ModifierLen = 0, ArgumentLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000674
Chris Lattner2b786902008-11-21 07:50:02 +0000675 // Check to see if we have a modifier. If so eat it.
Jordan Rosea7d03842013-02-08 22:30:41 +0000676 if (!isDigit(DiagStr[0])) {
Chris Lattner2b786902008-11-21 07:50:02 +0000677 Modifier = DiagStr;
678 while (DiagStr[0] == '-' ||
679 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
680 ++DiagStr;
681 ModifierLen = DiagStr-Modifier;
Chris Lattner23be0672008-11-19 06:51:40 +0000682
Chris Lattner2b786902008-11-21 07:50:02 +0000683 // If we have an argument, get it next.
684 if (DiagStr[0] == '{') {
685 ++DiagStr; // Skip {.
686 Argument = DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000687
John McCall8cb7a8a32010-01-14 20:11:39 +0000688 DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
689 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
Chris Lattner2b786902008-11-21 07:50:02 +0000690 ArgumentLen = DiagStr-Argument;
691 ++DiagStr; // Skip }.
Chris Lattner23be0672008-11-19 06:51:40 +0000692 }
Chris Lattner2b786902008-11-21 07:50:02 +0000693 }
Mike Stump11289f42009-09-09 15:08:12 +0000694
Jordan Rosea7d03842013-02-08 22:30:41 +0000695 assert(isDigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000696 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattner2b786902008-11-21 07:50:02 +0000697
Richard Trieu91844232012-06-26 18:18:47 +0000698 // Only used for type diffing.
699 unsigned ArgNo2 = ArgNo;
700
David Blaikie9c902b52011-09-25 23:23:43 +0000701 DiagnosticsEngine::ArgumentKind Kind = getArgKind(ArgNo);
Richard Trieu90c31f52013-01-30 20:04:31 +0000702 if (ModifierIs(Modifier, ModifierLen, "diff")) {
Jordan Rosea7d03842013-02-08 22:30:41 +0000703 assert(*DiagStr == ',' && isDigit(*(DiagStr + 1)) &&
Richard Trieu91844232012-06-26 18:18:47 +0000704 "Invalid format for diff modifier");
705 ++DiagStr; // Comma.
706 ArgNo2 = *DiagStr++ - '0';
Richard Trieu90c31f52013-01-30 20:04:31 +0000707 DiagnosticsEngine::ArgumentKind Kind2 = getArgKind(ArgNo2);
708 if (Kind == DiagnosticsEngine::ak_qualtype &&
709 Kind2 == DiagnosticsEngine::ak_qualtype)
710 Kind = DiagnosticsEngine::ak_qualtype_pair;
711 else {
712 // %diff only supports QualTypes. For other kinds of arguments,
713 // use the default printing. For example, if the modifier is:
714 // "%diff{compare $ to $|other text}1,2"
715 // treat it as:
716 // "compare %1 to %2"
717 const char *Pipe = ScanFormat(Argument, Argument + ArgumentLen, '|');
718 const char *FirstDollar = ScanFormat(Argument, Pipe, '$');
719 const char *SecondDollar = ScanFormat(FirstDollar + 1, Pipe, '$');
Filipe Cabecinhased4a00c2013-01-30 22:03:24 +0000720 const char ArgStr1[] = { '%', static_cast<char>('0' + ArgNo) };
721 const char ArgStr2[] = { '%', static_cast<char>('0' + ArgNo2) };
Richard Trieu90c31f52013-01-30 20:04:31 +0000722 FormatDiagnostic(Argument, FirstDollar, OutStr);
723 FormatDiagnostic(ArgStr1, ArgStr1 + 2, OutStr);
724 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
725 FormatDiagnostic(ArgStr2, ArgStr2 + 2, OutStr);
726 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
727 continue;
728 }
Richard Trieu91844232012-06-26 18:18:47 +0000729 }
Chris Lattnerc243f292009-10-20 05:25:22 +0000730
731 switch (Kind) {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000732 // ---- STRINGS ----
David Blaikie9c902b52011-09-25 23:23:43 +0000733 case DiagnosticsEngine::ak_std_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000734 const std::string &S = getArgStdStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000735 assert(ModifierLen == 0 && "No modifiers for strings yet");
736 OutStr.append(S.begin(), S.end());
737 break;
738 }
David Blaikie9c902b52011-09-25 23:23:43 +0000739 case DiagnosticsEngine::ak_c_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000740 const char *S = getArgCStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000741 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000742
743 // Don't crash if get passed a null pointer by accident.
744 if (!S)
745 S = "(null)";
Mike Stump11289f42009-09-09 15:08:12 +0000746
Chris Lattner2b786902008-11-21 07:50:02 +0000747 OutStr.append(S, S + strlen(S));
748 break;
749 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000750 // ---- INTEGERS ----
David Blaikie9c902b52011-09-25 23:23:43 +0000751 case DiagnosticsEngine::ak_sint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000752 int Val = getArgSInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000753
Chris Lattner2b786902008-11-21 07:50:02 +0000754 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCall43b61682010-10-14 01:55:31 +0000755 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen,
756 OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000757 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
758 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000759 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCall43b61682010-10-14 01:55:31 +0000760 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
761 OutStr);
John McCall9015cde2010-01-14 00:50:32 +0000762 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
763 HandleOrdinalModifier((unsigned)Val, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000764 } else {
765 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000766 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000767 }
Chris Lattner2b786902008-11-21 07:50:02 +0000768 break;
769 }
David Blaikie9c902b52011-09-25 23:23:43 +0000770 case DiagnosticsEngine::ak_uint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000771 unsigned Val = getArgUInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000772
Chris Lattner2b786902008-11-21 07:50:02 +0000773 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCalle4d54322010-01-13 23:58:20 +0000774 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000775 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
776 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000777 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCall43b61682010-10-14 01:55:31 +0000778 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
779 OutStr);
John McCall9015cde2010-01-14 00:50:32 +0000780 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
781 HandleOrdinalModifier(Val, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000782 } else {
783 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000784 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000785 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000786 break;
Chris Lattner2b786902008-11-21 07:50:02 +0000787 }
Alp Tokerec543272013-12-24 09:48:30 +0000788 // ---- TOKEN SPELLINGS ----
789 case DiagnosticsEngine::ak_tokenkind: {
790 tok::TokenKind Kind = static_cast<tok::TokenKind>(getRawArg(ArgNo));
791 assert(ModifierLen == 0 && "No modifiers for token kinds yet");
792
793 llvm::raw_svector_ostream Out(OutStr);
Alp Tokera231ad22014-01-06 12:54:18 +0000794 if (const char *S = tok::getPunctuatorSpelling(Kind))
795 // Quoted token spelling for punctuators.
796 Out << '\'' << S << '\'';
797 else if (const char *S = tok::getKeywordSpelling(Kind))
798 // Unquoted token spelling for keywords.
799 Out << S;
800 else if (const char *S = getTokenDescForDiagnostic(Kind))
Alp Tokerec543272013-12-24 09:48:30 +0000801 // Unquoted translatable token name.
802 Out << S;
Alp Tokerec543272013-12-24 09:48:30 +0000803 else if (const char *S = tok::getTokenName(Kind))
804 // Debug name, shouldn't appear in user-facing diagnostics.
805 Out << '<' << S << '>';
806 else
807 Out << "(null)";
808 break;
809 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000810 // ---- NAMES and TYPES ----
David Blaikie9c902b52011-09-25 23:23:43 +0000811 case DiagnosticsEngine::ak_identifierinfo: {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000812 const IdentifierInfo *II = getArgIdentifier(ArgNo);
813 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000814
815 // Don't crash if get passed a null pointer by accident.
816 if (!II) {
817 const char *S = "(null)";
818 OutStr.append(S, S + strlen(S));
819 continue;
820 }
821
Daniel Dunbar07d07852009-10-18 21:17:35 +0000822 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
Chris Lattnere3d20d92008-11-23 21:45:46 +0000823 break;
824 }
David Blaikie9c902b52011-09-25 23:23:43 +0000825 case DiagnosticsEngine::ak_qualtype:
826 case DiagnosticsEngine::ak_declarationname:
827 case DiagnosticsEngine::ak_nameddecl:
828 case DiagnosticsEngine::ak_nestednamespec:
829 case DiagnosticsEngine::ak_declcontext:
Aaron Ballman3e424b52013-12-26 18:30:57 +0000830 case DiagnosticsEngine::ak_attr:
Chris Lattnerc243f292009-10-20 05:25:22 +0000831 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
Chris Lattner63ecc502008-11-23 09:21:17 +0000832 Modifier, ModifierLen,
Chris Lattnerc243f292009-10-20 05:25:22 +0000833 Argument, ArgumentLen,
834 FormattedArgs.data(), FormattedArgs.size(),
Chandler Carruthd5173952011-07-11 17:49:21 +0000835 OutStr, QualTypeVals);
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000836 break;
Richard Trieu91844232012-06-26 18:18:47 +0000837 case DiagnosticsEngine::ak_qualtype_pair:
838 // Create a struct with all the info needed for printing.
839 TemplateDiffTypes TDT;
840 TDT.FromType = getRawArg(ArgNo);
841 TDT.ToType = getRawArg(ArgNo2);
842 TDT.ElideType = getDiags()->ElideType;
843 TDT.ShowColors = getDiags()->ShowColors;
Richard Trieu50f5f462012-07-10 01:46:04 +0000844 TDT.TemplateDiffUsed = false;
Richard Trieu91844232012-06-26 18:18:47 +0000845 intptr_t val = reinterpret_cast<intptr_t>(&TDT);
846
Richard Trieuc6058442012-06-29 21:12:16 +0000847 const char *ArgumentEnd = Argument + ArgumentLen;
848 const char *Pipe = ScanFormat(Argument, ArgumentEnd, '|');
849
Richard Trieua4056002012-07-13 21:18:32 +0000850 // Print the tree. If this diagnostic already has a tree, skip the
851 // second tree.
852 if (getDiags()->PrintTemplateTree && Tree.empty()) {
Richard Trieu91844232012-06-26 18:18:47 +0000853 TDT.PrintFromType = true;
854 TDT.PrintTree = true;
855 getDiags()->ConvertArgToString(Kind, val,
856 Modifier, ModifierLen,
857 Argument, ArgumentLen,
858 FormattedArgs.data(),
859 FormattedArgs.size(),
860 Tree, QualTypeVals);
861 // If there is no tree information, fall back to regular printing.
Richard Trieuc6058442012-06-29 21:12:16 +0000862 if (!Tree.empty()) {
863 FormatDiagnostic(Pipe + 1, ArgumentEnd, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +0000864 break;
Richard Trieuc6058442012-06-29 21:12:16 +0000865 }
Richard Trieu91844232012-06-26 18:18:47 +0000866 }
867
868 // Non-tree printing, also the fall-back when tree printing fails.
869 // The fall-back is triggered when the types compared are not templates.
Richard Trieuc6058442012-06-29 21:12:16 +0000870 const char *FirstDollar = ScanFormat(Argument, ArgumentEnd, '$');
871 const char *SecondDollar = ScanFormat(FirstDollar + 1, ArgumentEnd, '$');
Richard Trieu91844232012-06-26 18:18:47 +0000872
873 // Append before text
Richard Trieuc6058442012-06-29 21:12:16 +0000874 FormatDiagnostic(Argument, FirstDollar, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +0000875
876 // Append first type
877 TDT.PrintTree = false;
878 TDT.PrintFromType = true;
879 getDiags()->ConvertArgToString(Kind, val,
880 Modifier, ModifierLen,
881 Argument, ArgumentLen,
882 FormattedArgs.data(), FormattedArgs.size(),
883 OutStr, QualTypeVals);
Richard Trieu50f5f462012-07-10 01:46:04 +0000884 if (!TDT.TemplateDiffUsed)
885 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
886 TDT.FromType));
887
Richard Trieu91844232012-06-26 18:18:47 +0000888 // Append middle text
Richard Trieuc6058442012-06-29 21:12:16 +0000889 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +0000890
891 // Append second type
892 TDT.PrintFromType = false;
893 getDiags()->ConvertArgToString(Kind, val,
894 Modifier, ModifierLen,
895 Argument, ArgumentLen,
896 FormattedArgs.data(), FormattedArgs.size(),
897 OutStr, QualTypeVals);
Richard Trieu50f5f462012-07-10 01:46:04 +0000898 if (!TDT.TemplateDiffUsed)
899 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
900 TDT.ToType));
901
Richard Trieu91844232012-06-26 18:18:47 +0000902 // Append end text
Richard Trieuc6058442012-06-29 21:12:16 +0000903 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +0000904 break;
Nico Weber4c311642008-08-10 19:59:06 +0000905 }
Chris Lattnerc243f292009-10-20 05:25:22 +0000906
907 // Remember this argument info for subsequent formatting operations. Turn
908 // std::strings into a null terminated string to make it be the same case as
909 // all the other ones.
Richard Trieu91844232012-06-26 18:18:47 +0000910 if (Kind == DiagnosticsEngine::ak_qualtype_pair)
911 continue;
912 else if (Kind != DiagnosticsEngine::ak_std_string)
Chris Lattnerc243f292009-10-20 05:25:22 +0000913 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
914 else
David Blaikie9c902b52011-09-25 23:23:43 +0000915 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_c_string,
Chris Lattnerc243f292009-10-20 05:25:22 +0000916 (intptr_t)getArgStdStr(ArgNo).c_str()));
917
Nico Weber4c311642008-08-10 19:59:06 +0000918 }
Richard Trieu91844232012-06-26 18:18:47 +0000919
920 // Append the type tree to the end of the diagnostics.
921 OutStr.append(Tree.begin(), Tree.end());
Nico Weber4c311642008-08-10 19:59:06 +0000922}
Ted Kremenekea06ec12009-01-23 20:28:53 +0000923
Douglas Gregor33cdd812010-02-18 18:08:43 +0000924StoredDiagnostic::StoredDiagnostic() { }
925
David Blaikie9c902b52011-09-25 23:23:43 +0000926StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000927 StringRef Message)
Benjamin Kramer929bd682010-11-19 17:36:51 +0000928 : ID(ID), Level(Level), Loc(), Message(Message) { }
Douglas Gregor33cdd812010-02-18 18:08:43 +0000929
David Blaikie9c902b52011-09-25 23:23:43 +0000930StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level,
David Blaikieb5784322011-09-26 01:18:08 +0000931 const Diagnostic &Info)
Douglas Gregora750e8e2010-11-19 16:18:16 +0000932 : ID(Info.getID()), Level(Level)
933{
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000934 assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
935 "Valid source location without setting a source manager for diagnostic");
936 if (Info.getLocation().isValid())
937 Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000938 SmallString<64> Message;
Douglas Gregor33cdd812010-02-18 18:08:43 +0000939 Info.FormatDiagnostic(Message);
940 this->Message.assign(Message.begin(), Message.end());
941
942 Ranges.reserve(Info.getNumRanges());
943 for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I)
944 Ranges.push_back(Info.getRange(I));
945
Douglas Gregora771f462010-03-31 17:46:05 +0000946 FixIts.reserve(Info.getNumFixItHints());
947 for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I)
948 FixIts.push_back(Info.getFixItHint(I));
Douglas Gregor33cdd812010-02-18 18:08:43 +0000949}
950
David Blaikie9c902b52011-09-25 23:23:43 +0000951StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000952 StringRef Message, FullSourceLoc Loc,
Chris Lattner54b16772011-07-23 17:14:25 +0000953 ArrayRef<CharSourceRange> Ranges,
Aaron Ballman234ebd72013-02-24 19:08:10 +0000954 ArrayRef<FixItHint> FixIts)
955 : ID(ID), Level(Level), Loc(Loc), Message(Message),
956 Ranges(Ranges.begin(), Ranges.end()), FixIts(FixIts.begin(), FixIts.end())
Douglas Gregor925296b2011-07-19 16:10:42 +0000957{
Douglas Gregor925296b2011-07-19 16:10:42 +0000958}
959
Douglas Gregor33cdd812010-02-18 18:08:43 +0000960StoredDiagnostic::~StoredDiagnostic() { }
961
Ted Kremenekea06ec12009-01-23 20:28:53 +0000962/// IncludeInDiagnosticCounts - This method (whose default implementation
963/// returns true) indicates whether the diagnostics handled by this
David Blaikiee2eefae2011-09-25 23:39:51 +0000964/// DiagnosticConsumer should be included in the number of diagnostics
David Blaikie9c902b52011-09-25 23:23:43 +0000965/// reported by DiagnosticsEngine.
David Blaikiee2eefae2011-09-25 23:39:51 +0000966bool DiagnosticConsumer::IncludeInDiagnosticCounts() const { return true; }
Douglas Gregor89336232010-03-29 23:34:08 +0000967
David Blaikie68e081d2011-12-20 02:48:34 +0000968void IgnoringDiagConsumer::anchor() { }
969
Douglas Gregor6b930962013-05-03 22:58:43 +0000970ForwardingDiagnosticConsumer::~ForwardingDiagnosticConsumer() {}
971
972void ForwardingDiagnosticConsumer::HandleDiagnostic(
973 DiagnosticsEngine::Level DiagLevel,
974 const Diagnostic &Info) {
975 Target.HandleDiagnostic(DiagLevel, Info);
976}
977
978void ForwardingDiagnosticConsumer::clear() {
979 DiagnosticConsumer::clear();
980 Target.clear();
981}
982
983bool ForwardingDiagnosticConsumer::IncludeInDiagnosticCounts() const {
984 return Target.IncludeInDiagnosticCounts();
985}
986
Benjamin Kramer7ec12c92012-02-07 22:29:24 +0000987PartialDiagnostic::StorageAllocator::StorageAllocator() {
Douglas Gregor89336232010-03-29 23:34:08 +0000988 for (unsigned I = 0; I != NumCached; ++I)
989 FreeList[I] = Cached + I;
990 NumFreeListEntries = NumCached;
991}
992
Benjamin Kramer7ec12c92012-02-07 22:29:24 +0000993PartialDiagnostic::StorageAllocator::~StorageAllocator() {
Chad Rosier849a67b2012-02-07 23:24:49 +0000994 // Don't assert if we are in a CrashRecovery context, as this invariant may
995 // be invalidated during a crash.
996 assert((NumFreeListEntries == NumCached ||
997 llvm::CrashRecoveryContext::isRecoveringFromCrash()) &&
998 "A partial is on the lamb");
Douglas Gregor89336232010-03-29 23:34:08 +0000999}