blob: 6cf3c5c2eb95e5edb05b29571cda0ed2743a504f [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
Alp Tokerc726c362014-06-10 09:31:37 +0000165void DiagnosticsEngine::setDiagnosticMapping(diag::kind Diag,
166 diag::Severity Map,
Chad Rosier849a67b2012-02-07 23:24:49 +0000167 SourceLocation L) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000168 assert(Diag < diag::DIAG_UPPER_LIMIT &&
169 "Can only map builtin diagnostics");
170 assert((Diags->isBuiltinWarningOrExtension(Diag) ||
171 (Map == diag::MAP_FATAL || Map == diag::MAP_ERROR)) &&
172 "Cannot map errors into warnings!");
173 assert(!DiagStatePoints.empty());
Richard Smith8a0527d2012-08-14 22:37:22 +0000174 assert((L.isInvalid() || SourceMgr) && "No SourceMgr for valid location");
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000175
Richard Smith8a0527d2012-08-14 22:37:22 +0000176 FullSourceLoc Loc = SourceMgr? FullSourceLoc(L, *SourceMgr) : FullSourceLoc();
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000177 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
Chad Rosierd1956e42012-02-03 01:49:51 +0000178 // Don't allow a mapping to a warning override an error/fatal mapping.
179 if (Map == diag::MAP_WARNING) {
Alp Tokerc726c362014-06-10 09:31:37 +0000180 DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag);
181 if (Info.getSeverity() == diag::MAP_ERROR ||
182 Info.getSeverity() == diag::MAP_FATAL)
183 Map = Info.getSeverity();
Chad Rosierd1956e42012-02-03 01:49:51 +0000184 }
Alp Tokerc726c362014-06-10 09:31:37 +0000185 DiagnosticMapping Mapping = makeUserMapping(Map, L);
Daniel Dunbar2fba0972011-10-04 21:17:24 +0000186
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000187 // Common case; setting all the diagnostics of a group in one place.
188 if (Loc.isInvalid() || Loc == LastStateChangePos) {
Alp Tokerc726c362014-06-10 09:31:37 +0000189 GetCurDiagState()->setMapping(Diag, Mapping);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000190 return;
191 }
192
193 // Another common case; modifying diagnostic state in a source location
194 // after the previous one.
195 if ((Loc.isValid() && LastStateChangePos.isInvalid()) ||
196 LastStateChangePos.isBeforeInTranslationUnitThan(Loc)) {
Chris Lattner57540c52011-04-15 05:22:18 +0000197 // A diagnostic pragma occurred, create a new DiagState initialized with
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000198 // the current one and a new DiagStatePoint to record at which location
199 // the new state became active.
200 DiagStates.push_back(*GetCurDiagState());
201 PushDiagStatePoint(&DiagStates.back(), Loc);
Alp Tokerc726c362014-06-10 09:31:37 +0000202 GetCurDiagState()->setMapping(Diag, Mapping);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000203 return;
204 }
205
206 // We allow setting the diagnostic state in random source order for
207 // completeness but it should not be actually happening in normal practice.
208
209 DiagStatePointsTy::iterator Pos = GetDiagStatePointForLoc(Loc);
210 assert(Pos != DiagStatePoints.end());
211
212 // Update all diagnostic states that are active after the given location.
213 for (DiagStatePointsTy::iterator
214 I = Pos+1, E = DiagStatePoints.end(); I != E; ++I) {
Alp Tokerc726c362014-06-10 09:31:37 +0000215 GetCurDiagState()->setMapping(Diag, Mapping);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000216 }
217
218 // If the location corresponds to an existing point, just update its state.
219 if (Pos->Loc == Loc) {
Alp Tokerc726c362014-06-10 09:31:37 +0000220 GetCurDiagState()->setMapping(Diag, Mapping);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000221 return;
222 }
223
224 // Create a new state/point and fit it into the vector of DiagStatePoints
225 // so that the vector is always ordered according to location.
Alp Toker14c8aff2014-01-26 08:12:32 +0000226 assert(Pos->Loc.isBeforeInTranslationUnitThan(Loc));
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000227 DiagStates.push_back(*Pos->State);
228 DiagState *NewState = &DiagStates.back();
Alp Tokerc726c362014-06-10 09:31:37 +0000229 GetCurDiagState()->setMapping(Diag, Mapping);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000230 DiagStatePoints.insert(Pos+1, DiagStatePoint(NewState,
231 FullSourceLoc(Loc, *SourceMgr)));
232}
233
Alp Tokerc726c362014-06-10 09:31:37 +0000234bool DiagnosticsEngine::setDiagnosticGroupMapping(StringRef Group,
235 diag::Severity Map,
236 SourceLocation Loc) {
Daniel Dunbard908c122011-09-29 01:47:16 +0000237 // Get the diagnostics in this group.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000238 SmallVector<diag::kind, 8> GroupDiags;
Daniel Dunbard908c122011-09-29 01:47:16 +0000239 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
240 return true;
241
242 // Set the mapping.
243 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i)
244 setDiagnosticMapping(GroupDiags[i], Map, Loc);
245
246 return false;
247}
248
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000249bool DiagnosticsEngine::setDiagnosticGroupWarningAsError(StringRef Group,
250 bool Enabled) {
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000251 // If we are enabling this feature, just set the diagnostic mappings to map to
252 // errors.
253 if (Enabled)
254 return setDiagnosticGroupMapping(Group, diag::MAP_ERROR);
255
256 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
257 // potentially downgrade anything already mapped to be a warning.
258
259 // Get the diagnostics in this group.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000260 SmallVector<diag::kind, 8> GroupDiags;
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000261 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
262 return true;
263
264 // Perform the mapping change.
265 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i) {
Alp Tokerc726c362014-06-10 09:31:37 +0000266 DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(GroupDiags[i]);
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000267
Alp Tokerc726c362014-06-10 09:31:37 +0000268 if (Info.getSeverity() == diag::MAP_ERROR ||
269 Info.getSeverity() == diag::MAP_FATAL)
270 Info.setSeverity(diag::MAP_WARNING);
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000271
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) {
Alp Tokerc726c362014-06-10 09:31:37 +0000295 DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(GroupDiags[i]);
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000296
Alp Tokerc726c362014-06-10 09:31:37 +0000297 if (Info.getSeverity() == diag::MAP_FATAL)
298 Info.setSeverity(diag::MAP_ERROR);
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000299
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000300 Info.setNoErrorAsFatal(true);
301 }
302
303 return false;
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000304}
305
Alp Tokerc726c362014-06-10 09:31:37 +0000306void DiagnosticsEngine::setMappingForAllDiagnostics(diag::Severity Map,
307 SourceLocation Loc) {
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000308 // Get all the diagnostics.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000309 SmallVector<diag::kind, 64> AllDiags;
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000310 Diags->getAllDiagnostics(AllDiags);
311
312 // Set the mapping.
313 for (unsigned i = 0, e = AllDiags.size(); i != e; ++i)
314 if (Diags->isBuiltinWarningOrExtension(AllDiags[i]))
315 setDiagnosticMapping(AllDiags[i], Map, Loc);
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000316}
317
David Blaikie9c902b52011-09-25 23:23:43 +0000318void DiagnosticsEngine::Report(const StoredDiagnostic &storedDiag) {
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000319 assert(CurDiagID == ~0U && "Multiple diagnostics in flight at once!");
320
321 CurDiagLoc = storedDiag.getLocation();
322 CurDiagID = storedDiag.getID();
323 NumDiagArgs = 0;
324
Alexander Kornienkod3b4e082014-05-22 19:56:11 +0000325 DiagRanges.clear();
326 DiagRanges.reserve(storedDiag.range_size());
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000327 for (StoredDiagnostic::range_iterator
328 RI = storedDiag.range_begin(),
329 RE = storedDiag.range_end(); RI != RE; ++RI)
Alexander Kornienkod3b4e082014-05-22 19:56:11 +0000330 DiagRanges.push_back(*RI);
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000331
Alexander Kornienkod3b4e082014-05-22 19:56:11 +0000332 DiagFixItHints.clear();
333 DiagFixItHints.reserve(storedDiag.fixit_size());
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000334 for (StoredDiagnostic::fixit_iterator
335 FI = storedDiag.fixit_begin(),
336 FE = storedDiag.fixit_end(); FI != FE; ++FI)
Alexander Kornienkod3b4e082014-05-22 19:56:11 +0000337 DiagFixItHints.push_back(*FI);
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000338
David Blaikiee2eefae2011-09-25 23:39:51 +0000339 assert(Client && "DiagnosticConsumer not set!");
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000340 Level DiagLevel = storedDiag.getLevel();
David Blaikieb5784322011-09-26 01:18:08 +0000341 Diagnostic Info(this, storedDiag.getMessage());
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000342 Client->HandleDiagnostic(DiagLevel, Info);
343 if (Client->IncludeInDiagnosticCounts()) {
David Blaikie9c902b52011-09-25 23:23:43 +0000344 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000345 ++NumWarnings;
346 }
347
348 CurDiagID = ~0U;
349}
350
Jordan Rose6f524ac2012-07-11 16:50:36 +0000351bool DiagnosticsEngine::EmitCurrentDiagnostic(bool Force) {
352 assert(getClient() && "DiagnosticClient not set!");
353
354 bool Emitted;
355 if (Force) {
356 Diagnostic Info(this);
357
358 // Figure out the diagnostic level of this message.
359 DiagnosticIDs::Level DiagLevel
360 = Diags->getDiagnosticLevel(Info.getID(), Info.getLocation(), *this);
361
362 Emitted = (DiagLevel != DiagnosticIDs::Ignored);
363 if (Emitted) {
364 // Emit the diagnostic regardless of suppression level.
365 Diags->EmitDiag(*this, DiagLevel);
366 }
367 } else {
368 // Process the diagnostic, sending the accumulated information to the
369 // DiagnosticConsumer.
370 Emitted = ProcessDiag();
371 }
Douglas Gregor85795312010-03-22 15:10:57 +0000372
373 // Clear out the current diagnostic object.
Daniel Dunbarc7c00892012-03-13 21:02:14 +0000374 unsigned DiagID = CurDiagID;
375 Clear();
Douglas Gregor85795312010-03-22 15:10:57 +0000376
377 // If there was a delayed diagnostic, emit it now.
Jordan Rose6f524ac2012-07-11 16:50:36 +0000378 if (!Force && DelayedDiagID && DelayedDiagID != DiagID)
Daniel Dunbarc7c00892012-03-13 21:02:14 +0000379 ReportDelayed();
Douglas Gregor85795312010-03-22 15:10:57 +0000380
381 return Emitted;
382}
383
Nico Weber4c311642008-08-10 19:59:06 +0000384
David Blaikiee2eefae2011-09-25 23:39:51 +0000385DiagnosticConsumer::~DiagnosticConsumer() {}
Nico Weber4c311642008-08-10 19:59:06 +0000386
David Blaikiee2eefae2011-09-25 23:39:51 +0000387void DiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
David Blaikieb5784322011-09-26 01:18:08 +0000388 const Diagnostic &Info) {
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000389 if (!IncludeInDiagnosticCounts())
390 return;
391
David Blaikie9c902b52011-09-25 23:23:43 +0000392 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000393 ++NumWarnings;
David Blaikie9c902b52011-09-25 23:23:43 +0000394 else if (DiagLevel >= DiagnosticsEngine::Error)
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000395 ++NumErrors;
396}
Chris Lattner23be0672008-11-19 06:51:40 +0000397
Chris Lattner2b786902008-11-21 07:50:02 +0000398/// ModifierIs - Return true if the specified modifier matches specified string.
399template <std::size_t StrLen>
400static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
401 const char (&Str)[StrLen]) {
402 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
403}
404
John McCall8cb7a8a32010-01-14 20:11:39 +0000405/// ScanForward - Scans forward, looking for the given character, skipping
406/// nested clauses and escaped characters.
407static const char *ScanFormat(const char *I, const char *E, char Target) {
408 unsigned Depth = 0;
409
410 for ( ; I != E; ++I) {
411 if (Depth == 0 && *I == Target) return I;
412 if (Depth != 0 && *I == '}') Depth--;
413
414 if (*I == '%') {
415 I++;
416 if (I == E) break;
417
418 // Escaped characters get implicitly skipped here.
419
420 // Format specifier.
Jordan Rosea7d03842013-02-08 22:30:41 +0000421 if (!isDigit(*I) && !isPunctuation(*I)) {
422 for (I++; I != E && !isDigit(*I) && *I != '{'; I++) ;
John McCall8cb7a8a32010-01-14 20:11:39 +0000423 if (I == E) break;
424 if (*I == '{')
425 Depth++;
426 }
427 }
428 }
429 return E;
430}
431
Chris Lattner2b786902008-11-21 07:50:02 +0000432/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
433/// like this: %select{foo|bar|baz}2. This means that the integer argument
434/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
435/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
436/// This is very useful for certain classes of variant diagnostics.
David Blaikieb5784322011-09-26 01:18:08 +0000437static void HandleSelectModifier(const Diagnostic &DInfo, unsigned ValNo,
Chris Lattner2b786902008-11-21 07:50:02 +0000438 const char *Argument, unsigned ArgumentLen,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000439 SmallVectorImpl<char> &OutStr) {
Chris Lattner2b786902008-11-21 07:50:02 +0000440 const char *ArgumentEnd = Argument+ArgumentLen;
Mike Stump11289f42009-09-09 15:08:12 +0000441
Chris Lattner2b786902008-11-21 07:50:02 +0000442 // Skip over 'ValNo' |'s.
443 while (ValNo) {
John McCall8cb7a8a32010-01-14 20:11:39 +0000444 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
Chris Lattner2b786902008-11-21 07:50:02 +0000445 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
446 " larger than the number of options in the diagnostic string!");
447 Argument = NextVal+1; // Skip this string.
448 --ValNo;
449 }
Mike Stump11289f42009-09-09 15:08:12 +0000450
Chris Lattner2b786902008-11-21 07:50:02 +0000451 // Get the end of the value. This is either the } or the |.
John McCall8cb7a8a32010-01-14 20:11:39 +0000452 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
John McCalle4d54322010-01-13 23:58:20 +0000453
454 // Recursively format the result of the select clause into the output string.
455 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000456}
457
458/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
459/// letter 's' to the string if the value is not 1. This is used in cases like
460/// this: "you idiot, you have %4 parameter%s4!".
461static void HandleIntegerSModifier(unsigned ValNo,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000462 SmallVectorImpl<char> &OutStr) {
Chris Lattner2b786902008-11-21 07:50:02 +0000463 if (ValNo != 1)
464 OutStr.push_back('s');
465}
466
John McCall9015cde2010-01-14 00:50:32 +0000467/// HandleOrdinalModifier - Handle the integer 'ord' modifier. This
468/// prints the ordinal form of the given integer, with 1 corresponding
469/// to the first ordinal. Currently this is hard-coded to use the
470/// English form.
471static void HandleOrdinalModifier(unsigned ValNo,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000472 SmallVectorImpl<char> &OutStr) {
John McCall9015cde2010-01-14 00:50:32 +0000473 assert(ValNo != 0 && "ValNo must be strictly positive!");
474
475 llvm::raw_svector_ostream Out(OutStr);
476
477 // We could use text forms for the first N ordinals, but the numeric
478 // forms are actually nicer in diagnostics because they stand out.
Jordan Rosec102b352012-09-22 01:24:42 +0000479 Out << ValNo << llvm::getOrdinalSuffix(ValNo);
John McCall9015cde2010-01-14 00:50:32 +0000480}
481
Chris Lattner2b786902008-11-21 07:50:02 +0000482
Sebastian Redl15b02d22008-11-22 13:44:36 +0000483/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000484static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000485 // Programming 101: Parse a decimal number :-)
486 unsigned Val = 0;
487 while (Start != End && *Start >= '0' && *Start <= '9') {
488 Val *= 10;
489 Val += *Start - '0';
490 ++Start;
491 }
492 return Val;
493}
494
495/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000496static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000497 if (*Start != '[') {
498 unsigned Ref = PluralNumber(Start, End);
499 return Ref == Val;
500 }
501
502 ++Start;
503 unsigned Low = PluralNumber(Start, End);
504 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
505 ++Start;
506 unsigned High = PluralNumber(Start, End);
507 assert(*Start == ']' && "Bad plural expression syntax: expected )");
508 ++Start;
509 return Low <= Val && Val <= High;
510}
511
512/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattner2fe29202009-04-15 17:13:42 +0000513static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000514 // Empty condition?
515 if (*Start == ':')
516 return true;
517
518 while (1) {
519 char C = *Start;
520 if (C == '%') {
521 // Modulo expression
522 ++Start;
523 unsigned Arg = PluralNumber(Start, End);
524 assert(*Start == '=' && "Bad plural expression syntax: expected =");
525 ++Start;
526 unsigned ValMod = ValNo % Arg;
527 if (TestPluralRange(ValMod, Start, End))
528 return true;
529 } else {
Sebastian Redl3ceaf622008-11-27 07:28:14 +0000530 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redl15b02d22008-11-22 13:44:36 +0000531 "Bad plural expression syntax: unexpected character");
532 // Range expression
533 if (TestPluralRange(ValNo, Start, End))
534 return true;
535 }
536
537 // Scan for next or-expr part.
538 Start = std::find(Start, End, ',');
Mike Stump11289f42009-09-09 15:08:12 +0000539 if (Start == End)
Sebastian Redl15b02d22008-11-22 13:44:36 +0000540 break;
541 ++Start;
542 }
543 return false;
544}
545
546/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
547/// for complex plural forms, or in languages where all plurals are complex.
548/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
549/// conditions that are tested in order, the form corresponding to the first
550/// that applies being emitted. The empty condition is always true, making the
551/// last form a default case.
552/// Conditions are simple boolean expressions, where n is the number argument.
553/// Here are the rules.
554/// condition := expression | empty
555/// empty := -> always true
556/// expression := numeric [',' expression] -> logical or
557/// numeric := range -> true if n in range
558/// | '%' number '=' range -> true if n % number in range
559/// range := number
560/// | '[' number ',' number ']' -> ranges are inclusive both ends
561///
562/// Here are some examples from the GNU gettext manual written in this form:
563/// English:
564/// {1:form0|:form1}
565/// Latvian:
566/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
567/// Gaeilge:
568/// {1:form0|2:form1|:form2}
569/// Romanian:
570/// {1:form0|0,%100=[1,19]:form1|:form2}
571/// Lithuanian:
572/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
573/// Russian (requires repeated form):
574/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
575/// Slovak
576/// {1:form0|[2,4]:form1|:form2}
577/// Polish (requires repeated form):
578/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
David Blaikieb5784322011-09-26 01:18:08 +0000579static void HandlePluralModifier(const Diagnostic &DInfo, unsigned ValNo,
Sebastian Redl15b02d22008-11-22 13:44:36 +0000580 const char *Argument, unsigned ArgumentLen,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000581 SmallVectorImpl<char> &OutStr) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000582 const char *ArgumentEnd = Argument + ArgumentLen;
583 while (1) {
584 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
585 const char *ExprEnd = Argument;
586 while (*ExprEnd != ':') {
587 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
588 ++ExprEnd;
589 }
590 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
591 Argument = ExprEnd + 1;
John McCall8cb7a8a32010-01-14 20:11:39 +0000592 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
John McCall43b61682010-10-14 01:55:31 +0000593
594 // Recursively format the result of the plural clause into the
595 // output string.
596 DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000597 return;
598 }
John McCall8cb7a8a32010-01-14 20:11:39 +0000599 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
Sebastian Redl15b02d22008-11-22 13:44:36 +0000600 }
601}
602
Alp Tokera231ad22014-01-06 12:54:18 +0000603/// \brief Returns the friendly description for a token kind that will appear
604/// without quotes in diagnostic messages. These strings may be translatable in
605/// future.
606static const char *getTokenDescForDiagnostic(tok::TokenKind Kind) {
Alp Tokerec543272013-12-24 09:48:30 +0000607 switch (Kind) {
608 case tok::identifier:
609 return "identifier";
610 default:
Craig Topperf1186c52014-05-08 06:41:40 +0000611 return nullptr;
Alp Tokerec543272013-12-24 09:48:30 +0000612 }
613}
Sebastian Redl15b02d22008-11-22 13:44:36 +0000614
Chris Lattner23be0672008-11-19 06:51:40 +0000615/// FormatDiagnostic - Format this diagnostic into a string, substituting the
616/// formal arguments into the %0 slots. The result is appended onto the Str
617/// array.
David Blaikieb5784322011-09-26 01:18:08 +0000618void Diagnostic::
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000619FormatDiagnostic(SmallVectorImpl<char> &OutStr) const {
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000620 if (!StoredDiagMessage.empty()) {
621 OutStr.append(StoredDiagMessage.begin(), StoredDiagMessage.end());
622 return;
623 }
624
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000625 StringRef Diag =
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000626 getDiags()->getDiagnosticIDs()->getDescription(getID());
Mike Stump11289f42009-09-09 15:08:12 +0000627
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000628 FormatDiagnostic(Diag.begin(), Diag.end(), OutStr);
John McCalle4d54322010-01-13 23:58:20 +0000629}
630
David Blaikieb5784322011-09-26 01:18:08 +0000631void Diagnostic::
John McCalle4d54322010-01-13 23:58:20 +0000632FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000633 SmallVectorImpl<char> &OutStr) const {
John McCalle4d54322010-01-13 23:58:20 +0000634
Chris Lattnerc243f292009-10-20 05:25:22 +0000635 /// FormattedArgs - Keep track of all of the arguments formatted by
636 /// ConvertArgToString and pass them into subsequent calls to
637 /// ConvertArgToString, allowing the implementation to avoid redundancies in
638 /// obvious cases.
David Blaikie9c902b52011-09-25 23:23:43 +0000639 SmallVector<DiagnosticsEngine::ArgumentValue, 8> FormattedArgs;
Chandler Carruthd5173952011-07-11 17:49:21 +0000640
641 /// QualTypeVals - Pass a vector of arrays so that QualType names can be
642 /// compared to see if more information is needed to be printed.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000643 SmallVector<intptr_t, 2> QualTypeVals;
Richard Trieu91844232012-06-26 18:18:47 +0000644 SmallVector<char, 64> Tree;
645
Chandler Carruthd5173952011-07-11 17:49:21 +0000646 for (unsigned i = 0, e = getNumArgs(); i < e; ++i)
David Blaikie9c902b52011-09-25 23:23:43 +0000647 if (getArgKind(i) == DiagnosticsEngine::ak_qualtype)
Chandler Carruthd5173952011-07-11 17:49:21 +0000648 QualTypeVals.push_back(getRawArg(i));
649
Chris Lattner23be0672008-11-19 06:51:40 +0000650 while (DiagStr != DiagEnd) {
651 if (DiagStr[0] != '%') {
652 // Append non-%0 substrings to Str if we have one.
653 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
654 OutStr.append(DiagStr, StrEnd);
655 DiagStr = StrEnd;
Chris Lattner2b786902008-11-21 07:50:02 +0000656 continue;
Jordan Rosea7d03842013-02-08 22:30:41 +0000657 } else if (isPunctuation(DiagStr[1])) {
John McCall8cb7a8a32010-01-14 20:11:39 +0000658 OutStr.push_back(DiagStr[1]); // %% -> %.
Chris Lattner23be0672008-11-19 06:51:40 +0000659 DiagStr += 2;
Chris Lattner2b786902008-11-21 07:50:02 +0000660 continue;
661 }
Mike Stump11289f42009-09-09 15:08:12 +0000662
Chris Lattner2b786902008-11-21 07:50:02 +0000663 // Skip the %.
664 ++DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000665
Chris Lattner2b786902008-11-21 07:50:02 +0000666 // This must be a placeholder for a diagnostic argument. The format for a
667 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
668 // The digit is a number from 0-9 indicating which argument this comes from.
669 // The modifier is a string of digits from the set [-a-z]+, arguments is a
670 // brace enclosed string.
Craig Topperf1186c52014-05-08 06:41:40 +0000671 const char *Modifier = nullptr, *Argument = nullptr;
Chris Lattner2b786902008-11-21 07:50:02 +0000672 unsigned ModifierLen = 0, ArgumentLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000673
Chris Lattner2b786902008-11-21 07:50:02 +0000674 // Check to see if we have a modifier. If so eat it.
Jordan Rosea7d03842013-02-08 22:30:41 +0000675 if (!isDigit(DiagStr[0])) {
Chris Lattner2b786902008-11-21 07:50:02 +0000676 Modifier = DiagStr;
677 while (DiagStr[0] == '-' ||
678 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
679 ++DiagStr;
680 ModifierLen = DiagStr-Modifier;
Chris Lattner23be0672008-11-19 06:51:40 +0000681
Chris Lattner2b786902008-11-21 07:50:02 +0000682 // If we have an argument, get it next.
683 if (DiagStr[0] == '{') {
684 ++DiagStr; // Skip {.
685 Argument = DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000686
John McCall8cb7a8a32010-01-14 20:11:39 +0000687 DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
688 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
Chris Lattner2b786902008-11-21 07:50:02 +0000689 ArgumentLen = DiagStr-Argument;
690 ++DiagStr; // Skip }.
Chris Lattner23be0672008-11-19 06:51:40 +0000691 }
Chris Lattner2b786902008-11-21 07:50:02 +0000692 }
Mike Stump11289f42009-09-09 15:08:12 +0000693
Jordan Rosea7d03842013-02-08 22:30:41 +0000694 assert(isDigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000695 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattner2b786902008-11-21 07:50:02 +0000696
Richard Trieu91844232012-06-26 18:18:47 +0000697 // Only used for type diffing.
698 unsigned ArgNo2 = ArgNo;
699
David Blaikie9c902b52011-09-25 23:23:43 +0000700 DiagnosticsEngine::ArgumentKind Kind = getArgKind(ArgNo);
Richard Trieu90c31f52013-01-30 20:04:31 +0000701 if (ModifierIs(Modifier, ModifierLen, "diff")) {
Jordan Rosea7d03842013-02-08 22:30:41 +0000702 assert(*DiagStr == ',' && isDigit(*(DiagStr + 1)) &&
Richard Trieu91844232012-06-26 18:18:47 +0000703 "Invalid format for diff modifier");
704 ++DiagStr; // Comma.
705 ArgNo2 = *DiagStr++ - '0';
Richard Trieu90c31f52013-01-30 20:04:31 +0000706 DiagnosticsEngine::ArgumentKind Kind2 = getArgKind(ArgNo2);
707 if (Kind == DiagnosticsEngine::ak_qualtype &&
708 Kind2 == DiagnosticsEngine::ak_qualtype)
709 Kind = DiagnosticsEngine::ak_qualtype_pair;
710 else {
711 // %diff only supports QualTypes. For other kinds of arguments,
712 // use the default printing. For example, if the modifier is:
713 // "%diff{compare $ to $|other text}1,2"
714 // treat it as:
715 // "compare %1 to %2"
716 const char *Pipe = ScanFormat(Argument, Argument + ArgumentLen, '|');
717 const char *FirstDollar = ScanFormat(Argument, Pipe, '$');
718 const char *SecondDollar = ScanFormat(FirstDollar + 1, Pipe, '$');
Filipe Cabecinhased4a00c2013-01-30 22:03:24 +0000719 const char ArgStr1[] = { '%', static_cast<char>('0' + ArgNo) };
720 const char ArgStr2[] = { '%', static_cast<char>('0' + ArgNo2) };
Richard Trieu90c31f52013-01-30 20:04:31 +0000721 FormatDiagnostic(Argument, FirstDollar, OutStr);
722 FormatDiagnostic(ArgStr1, ArgStr1 + 2, OutStr);
723 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
724 FormatDiagnostic(ArgStr2, ArgStr2 + 2, OutStr);
725 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
726 continue;
727 }
Richard Trieu91844232012-06-26 18:18:47 +0000728 }
Chris Lattnerc243f292009-10-20 05:25:22 +0000729
730 switch (Kind) {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000731 // ---- STRINGS ----
David Blaikie9c902b52011-09-25 23:23:43 +0000732 case DiagnosticsEngine::ak_std_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000733 const std::string &S = getArgStdStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000734 assert(ModifierLen == 0 && "No modifiers for strings yet");
735 OutStr.append(S.begin(), S.end());
736 break;
737 }
David Blaikie9c902b52011-09-25 23:23:43 +0000738 case DiagnosticsEngine::ak_c_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000739 const char *S = getArgCStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000740 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000741
742 // Don't crash if get passed a null pointer by accident.
743 if (!S)
744 S = "(null)";
Mike Stump11289f42009-09-09 15:08:12 +0000745
Chris Lattner2b786902008-11-21 07:50:02 +0000746 OutStr.append(S, S + strlen(S));
747 break;
748 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000749 // ---- INTEGERS ----
David Blaikie9c902b52011-09-25 23:23:43 +0000750 case DiagnosticsEngine::ak_sint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000751 int Val = getArgSInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000752
Chris Lattner2b786902008-11-21 07:50:02 +0000753 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCall43b61682010-10-14 01:55:31 +0000754 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen,
755 OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000756 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
757 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000758 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCall43b61682010-10-14 01:55:31 +0000759 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
760 OutStr);
John McCall9015cde2010-01-14 00:50:32 +0000761 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
762 HandleOrdinalModifier((unsigned)Val, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000763 } else {
764 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000765 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000766 }
Chris Lattner2b786902008-11-21 07:50:02 +0000767 break;
768 }
David Blaikie9c902b52011-09-25 23:23:43 +0000769 case DiagnosticsEngine::ak_uint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000770 unsigned Val = getArgUInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000771
Chris Lattner2b786902008-11-21 07:50:02 +0000772 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCalle4d54322010-01-13 23:58:20 +0000773 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000774 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
775 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000776 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCall43b61682010-10-14 01:55:31 +0000777 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
778 OutStr);
John McCall9015cde2010-01-14 00:50:32 +0000779 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
780 HandleOrdinalModifier(Val, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000781 } else {
782 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000783 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000784 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000785 break;
Chris Lattner2b786902008-11-21 07:50:02 +0000786 }
Alp Tokerec543272013-12-24 09:48:30 +0000787 // ---- TOKEN SPELLINGS ----
788 case DiagnosticsEngine::ak_tokenkind: {
789 tok::TokenKind Kind = static_cast<tok::TokenKind>(getRawArg(ArgNo));
790 assert(ModifierLen == 0 && "No modifiers for token kinds yet");
791
792 llvm::raw_svector_ostream Out(OutStr);
Alp Tokera231ad22014-01-06 12:54:18 +0000793 if (const char *S = tok::getPunctuatorSpelling(Kind))
794 // Quoted token spelling for punctuators.
795 Out << '\'' << S << '\'';
796 else if (const char *S = tok::getKeywordSpelling(Kind))
797 // Unquoted token spelling for keywords.
798 Out << S;
799 else if (const char *S = getTokenDescForDiagnostic(Kind))
Alp Tokerec543272013-12-24 09:48:30 +0000800 // Unquoted translatable token name.
801 Out << S;
Alp Tokerec543272013-12-24 09:48:30 +0000802 else if (const char *S = tok::getTokenName(Kind))
803 // Debug name, shouldn't appear in user-facing diagnostics.
804 Out << '<' << S << '>';
805 else
806 Out << "(null)";
807 break;
808 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000809 // ---- NAMES and TYPES ----
David Blaikie9c902b52011-09-25 23:23:43 +0000810 case DiagnosticsEngine::ak_identifierinfo: {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000811 const IdentifierInfo *II = getArgIdentifier(ArgNo);
812 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000813
814 // Don't crash if get passed a null pointer by accident.
815 if (!II) {
816 const char *S = "(null)";
817 OutStr.append(S, S + strlen(S));
818 continue;
819 }
820
Daniel Dunbar07d07852009-10-18 21:17:35 +0000821 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
Chris Lattnere3d20d92008-11-23 21:45:46 +0000822 break;
823 }
David Blaikie9c902b52011-09-25 23:23:43 +0000824 case DiagnosticsEngine::ak_qualtype:
825 case DiagnosticsEngine::ak_declarationname:
826 case DiagnosticsEngine::ak_nameddecl:
827 case DiagnosticsEngine::ak_nestednamespec:
828 case DiagnosticsEngine::ak_declcontext:
Aaron Ballman3e424b52013-12-26 18:30:57 +0000829 case DiagnosticsEngine::ak_attr:
Chris Lattnerc243f292009-10-20 05:25:22 +0000830 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
Chris Lattner63ecc502008-11-23 09:21:17 +0000831 Modifier, ModifierLen,
Chris Lattnerc243f292009-10-20 05:25:22 +0000832 Argument, ArgumentLen,
833 FormattedArgs.data(), FormattedArgs.size(),
Chandler Carruthd5173952011-07-11 17:49:21 +0000834 OutStr, QualTypeVals);
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000835 break;
Richard Trieu91844232012-06-26 18:18:47 +0000836 case DiagnosticsEngine::ak_qualtype_pair:
837 // Create a struct with all the info needed for printing.
838 TemplateDiffTypes TDT;
839 TDT.FromType = getRawArg(ArgNo);
840 TDT.ToType = getRawArg(ArgNo2);
841 TDT.ElideType = getDiags()->ElideType;
842 TDT.ShowColors = getDiags()->ShowColors;
Richard Trieu50f5f462012-07-10 01:46:04 +0000843 TDT.TemplateDiffUsed = false;
Richard Trieu91844232012-06-26 18:18:47 +0000844 intptr_t val = reinterpret_cast<intptr_t>(&TDT);
845
Richard Trieuc6058442012-06-29 21:12:16 +0000846 const char *ArgumentEnd = Argument + ArgumentLen;
847 const char *Pipe = ScanFormat(Argument, ArgumentEnd, '|');
848
Richard Trieua4056002012-07-13 21:18:32 +0000849 // Print the tree. If this diagnostic already has a tree, skip the
850 // second tree.
851 if (getDiags()->PrintTemplateTree && Tree.empty()) {
Richard Trieu91844232012-06-26 18:18:47 +0000852 TDT.PrintFromType = true;
853 TDT.PrintTree = true;
854 getDiags()->ConvertArgToString(Kind, val,
855 Modifier, ModifierLen,
856 Argument, ArgumentLen,
857 FormattedArgs.data(),
858 FormattedArgs.size(),
859 Tree, QualTypeVals);
860 // If there is no tree information, fall back to regular printing.
Richard Trieuc6058442012-06-29 21:12:16 +0000861 if (!Tree.empty()) {
862 FormatDiagnostic(Pipe + 1, ArgumentEnd, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +0000863 break;
Richard Trieuc6058442012-06-29 21:12:16 +0000864 }
Richard Trieu91844232012-06-26 18:18:47 +0000865 }
866
867 // Non-tree printing, also the fall-back when tree printing fails.
868 // The fall-back is triggered when the types compared are not templates.
Richard Trieuc6058442012-06-29 21:12:16 +0000869 const char *FirstDollar = ScanFormat(Argument, ArgumentEnd, '$');
870 const char *SecondDollar = ScanFormat(FirstDollar + 1, ArgumentEnd, '$');
Richard Trieu91844232012-06-26 18:18:47 +0000871
872 // Append before text
Richard Trieuc6058442012-06-29 21:12:16 +0000873 FormatDiagnostic(Argument, FirstDollar, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +0000874
875 // Append first type
876 TDT.PrintTree = false;
877 TDT.PrintFromType = true;
878 getDiags()->ConvertArgToString(Kind, val,
879 Modifier, ModifierLen,
880 Argument, ArgumentLen,
881 FormattedArgs.data(), FormattedArgs.size(),
882 OutStr, QualTypeVals);
Richard Trieu50f5f462012-07-10 01:46:04 +0000883 if (!TDT.TemplateDiffUsed)
884 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
885 TDT.FromType));
886
Richard Trieu91844232012-06-26 18:18:47 +0000887 // Append middle text
Richard Trieuc6058442012-06-29 21:12:16 +0000888 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +0000889
890 // Append second type
891 TDT.PrintFromType = false;
892 getDiags()->ConvertArgToString(Kind, val,
893 Modifier, ModifierLen,
894 Argument, ArgumentLen,
895 FormattedArgs.data(), FormattedArgs.size(),
896 OutStr, QualTypeVals);
Richard Trieu50f5f462012-07-10 01:46:04 +0000897 if (!TDT.TemplateDiffUsed)
898 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
899 TDT.ToType));
900
Richard Trieu91844232012-06-26 18:18:47 +0000901 // Append end text
Richard Trieuc6058442012-06-29 21:12:16 +0000902 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +0000903 break;
Nico Weber4c311642008-08-10 19:59:06 +0000904 }
Chris Lattnerc243f292009-10-20 05:25:22 +0000905
906 // Remember this argument info for subsequent formatting operations. Turn
907 // std::strings into a null terminated string to make it be the same case as
908 // all the other ones.
Richard Trieu91844232012-06-26 18:18:47 +0000909 if (Kind == DiagnosticsEngine::ak_qualtype_pair)
910 continue;
911 else if (Kind != DiagnosticsEngine::ak_std_string)
Chris Lattnerc243f292009-10-20 05:25:22 +0000912 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
913 else
David Blaikie9c902b52011-09-25 23:23:43 +0000914 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_c_string,
Chris Lattnerc243f292009-10-20 05:25:22 +0000915 (intptr_t)getArgStdStr(ArgNo).c_str()));
916
Nico Weber4c311642008-08-10 19:59:06 +0000917 }
Richard Trieu91844232012-06-26 18:18:47 +0000918
919 // Append the type tree to the end of the diagnostics.
920 OutStr.append(Tree.begin(), Tree.end());
Nico Weber4c311642008-08-10 19:59:06 +0000921}
Ted Kremenekea06ec12009-01-23 20:28:53 +0000922
Douglas Gregor33cdd812010-02-18 18:08:43 +0000923StoredDiagnostic::StoredDiagnostic() { }
924
David Blaikie9c902b52011-09-25 23:23:43 +0000925StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000926 StringRef Message)
Benjamin Kramer929bd682010-11-19 17:36:51 +0000927 : ID(ID), Level(Level), Loc(), Message(Message) { }
Douglas Gregor33cdd812010-02-18 18:08:43 +0000928
David Blaikie9c902b52011-09-25 23:23:43 +0000929StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level,
David Blaikieb5784322011-09-26 01:18:08 +0000930 const Diagnostic &Info)
Douglas Gregora750e8e2010-11-19 16:18:16 +0000931 : ID(Info.getID()), Level(Level)
932{
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000933 assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
934 "Valid source location without setting a source manager for diagnostic");
935 if (Info.getLocation().isValid())
936 Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000937 SmallString<64> Message;
Douglas Gregor33cdd812010-02-18 18:08:43 +0000938 Info.FormatDiagnostic(Message);
939 this->Message.assign(Message.begin(), Message.end());
940
941 Ranges.reserve(Info.getNumRanges());
942 for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I)
943 Ranges.push_back(Info.getRange(I));
944
Douglas Gregora771f462010-03-31 17:46:05 +0000945 FixIts.reserve(Info.getNumFixItHints());
946 for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I)
947 FixIts.push_back(Info.getFixItHint(I));
Douglas Gregor33cdd812010-02-18 18:08:43 +0000948}
949
David Blaikie9c902b52011-09-25 23:23:43 +0000950StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000951 StringRef Message, FullSourceLoc Loc,
Chris Lattner54b16772011-07-23 17:14:25 +0000952 ArrayRef<CharSourceRange> Ranges,
Aaron Ballman234ebd72013-02-24 19:08:10 +0000953 ArrayRef<FixItHint> FixIts)
954 : ID(ID), Level(Level), Loc(Loc), Message(Message),
955 Ranges(Ranges.begin(), Ranges.end()), FixIts(FixIts.begin(), FixIts.end())
Douglas Gregor925296b2011-07-19 16:10:42 +0000956{
Douglas Gregor925296b2011-07-19 16:10:42 +0000957}
958
Douglas Gregor33cdd812010-02-18 18:08:43 +0000959StoredDiagnostic::~StoredDiagnostic() { }
960
Ted Kremenekea06ec12009-01-23 20:28:53 +0000961/// IncludeInDiagnosticCounts - This method (whose default implementation
962/// returns true) indicates whether the diagnostics handled by this
David Blaikiee2eefae2011-09-25 23:39:51 +0000963/// DiagnosticConsumer should be included in the number of diagnostics
David Blaikie9c902b52011-09-25 23:23:43 +0000964/// reported by DiagnosticsEngine.
David Blaikiee2eefae2011-09-25 23:39:51 +0000965bool DiagnosticConsumer::IncludeInDiagnosticCounts() const { return true; }
Douglas Gregor89336232010-03-29 23:34:08 +0000966
David Blaikie68e081d2011-12-20 02:48:34 +0000967void IgnoringDiagConsumer::anchor() { }
968
Douglas Gregor6b930962013-05-03 22:58:43 +0000969ForwardingDiagnosticConsumer::~ForwardingDiagnosticConsumer() {}
970
971void ForwardingDiagnosticConsumer::HandleDiagnostic(
972 DiagnosticsEngine::Level DiagLevel,
973 const Diagnostic &Info) {
974 Target.HandleDiagnostic(DiagLevel, Info);
975}
976
977void ForwardingDiagnosticConsumer::clear() {
978 DiagnosticConsumer::clear();
979 Target.clear();
980}
981
982bool ForwardingDiagnosticConsumer::IncludeInDiagnosticCounts() const {
983 return Target.IncludeInDiagnosticCounts();
984}
985
Benjamin Kramer7ec12c92012-02-07 22:29:24 +0000986PartialDiagnostic::StorageAllocator::StorageAllocator() {
Douglas Gregor89336232010-03-29 23:34:08 +0000987 for (unsigned I = 0; I != NumCached; ++I)
988 FreeList[I] = Cached + I;
989 NumFreeListEntries = NumCached;
990}
991
Benjamin Kramer7ec12c92012-02-07 22:29:24 +0000992PartialDiagnostic::StorageAllocator::~StorageAllocator() {
Chad Rosier849a67b2012-02-07 23:24:49 +0000993 // Don't assert if we are in a CrashRecovery context, as this invariant may
994 // be invalidated during a crash.
995 assert((NumFreeListEntries == NumCached ||
996 llvm::CrashRecoveryContext::isRecoveringFromCrash()) &&
997 "A partial is on the lamb");
Douglas Gregor89336232010-03-29 23:34:08 +0000998}