blob: 83228ad3c55d90e8c2026402fa347c347e6572db [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"
Richard Trieub3b8bb02015-01-08 01:27:03 +000022#include "llvm/Support/Locale.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000023#include "llvm/Support/raw_ostream.h"
Ted Kremenek84de4a12011-03-21 18:40:07 +000024
Chris Lattner22eb9722006-06-18 05:43:12 +000025using namespace clang;
26
David Blaikie9c902b52011-09-25 23:23:43 +000027static void DummyArgToStringFn(DiagnosticsEngine::ArgumentKind AK, intptr_t QT,
Craig Topper3aa4fb32014-06-12 05:32:35 +000028 StringRef Modifier, StringRef Argument,
Craig Toppere4753502014-06-12 05:32:27 +000029 ArrayRef<DiagnosticsEngine::ArgumentValue> PrevArgs,
30 SmallVectorImpl<char> &Output,
31 void *Cookie,
32 ArrayRef<intptr_t> QualTypeVals) {
33 StringRef Str = "<can't format argument>";
34 Output.append(Str.begin(), Str.end());
Chris Lattner6a2ed6f2008-11-23 09:13:29 +000035}
36
David Blaikie9c902b52011-09-25 23:23:43 +000037DiagnosticsEngine::DiagnosticsEngine(
Alexander Kornienko41c247a2014-11-17 23:46:02 +000038 const IntrusiveRefCntPtr<DiagnosticIDs> &diags, DiagnosticOptions *DiagOpts,
39 DiagnosticConsumer *client, bool ShouldOwnClient)
40 : Diags(diags), DiagOpts(DiagOpts), Client(nullptr), SourceMgr(nullptr) {
41 setClient(client, ShouldOwnClient);
Chris Lattner63ecc502008-11-23 09:21:17 +000042 ArgToStringFn = DummyArgToStringFn;
Craig Topperf1186c52014-05-08 06:41:40 +000043 ArgToStringCookie = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +000044
Douglas Gregor0e119552010-07-31 00:40:00 +000045 AllExtensionsSilenced = 0;
46 IgnoreAllWarnings = false;
47 WarningsAsErrors = false;
Ted Kremenekfbbdced2011-08-18 01:12:56 +000048 EnableAllWarnings = false;
Douglas Gregor0e119552010-07-31 00:40:00 +000049 ErrorsAsFatal = false;
50 SuppressSystemWarnings = false;
51 SuppressAllDiagnostics = false;
Richard Trieu91844232012-06-26 18:18:47 +000052 ElideType = true;
53 PrintTemplateTree = false;
54 ShowColors = false;
Douglas Gregor0e119552010-07-31 00:40:00 +000055 ShowOverloads = Ovl_All;
Alp Tokerac4e8e52014-06-22 21:58:33 +000056 ExtBehavior = diag::Severity::Ignored;
Douglas Gregor0e119552010-07-31 00:40:00 +000057
58 ErrorLimit = 0;
59 TemplateBacktraceLimit = 0;
Richard Smithf6f003a2011-12-16 19:06:07 +000060 ConstexprBacktraceLimit = 0;
Douglas Gregor0e119552010-07-31 00:40:00 +000061
Douglas Gregoraa21cc42010-07-19 21:46:24 +000062 Reset();
Chris Lattnerae411572006-07-05 00:55:08 +000063}
64
Reid Klecknerdccbabf2014-12-17 20:23:11 +000065DiagnosticsEngine::~DiagnosticsEngine() {
66 // If we own the diagnostic client, destroy it first so that it can access the
67 // engine from its destructor.
68 setClient(nullptr);
69}
70
David Blaikiee2eefae2011-09-25 23:39:51 +000071void DiagnosticsEngine::setClient(DiagnosticConsumer *client,
David Blaikie9c902b52011-09-25 23:23:43 +000072 bool ShouldOwnClient) {
Alexander Kornienko41c247a2014-11-17 23:46:02 +000073 Owner.reset(ShouldOwnClient ? client : nullptr);
Douglas Gregor7a964ad2011-01-31 22:04:05 +000074 Client = client;
Douglas Gregor7a964ad2011-01-31 22:04:05 +000075}
Chris Lattnerfb42a182009-07-12 21:18:45 +000076
David Blaikie9c902b52011-09-25 23:23:43 +000077void DiagnosticsEngine::pushMappings(SourceLocation Loc) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +000078 DiagStateOnPushStack.push_back(GetCurDiagState());
Chris Lattnerfb42a182009-07-12 21:18:45 +000079}
80
David Blaikie9c902b52011-09-25 23:23:43 +000081bool DiagnosticsEngine::popMappings(SourceLocation Loc) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +000082 if (DiagStateOnPushStack.empty())
Chris Lattnerfb42a182009-07-12 21:18:45 +000083 return false;
84
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +000085 if (DiagStateOnPushStack.back() != GetCurDiagState()) {
86 // State changed at some point between push/pop.
87 PushDiagStatePoint(DiagStateOnPushStack.back(), Loc);
88 }
89 DiagStateOnPushStack.pop_back();
Chris Lattnerfb42a182009-07-12 21:18:45 +000090 return true;
91}
92
David Blaikie9c902b52011-09-25 23:23:43 +000093void DiagnosticsEngine::Reset() {
Douglas Gregoraa21cc42010-07-19 21:46:24 +000094 ErrorOccurred = false;
DeLesley Hutchins8ecd4912012-12-07 22:53:48 +000095 UncompilableErrorOccurred = false;
Douglas Gregoraa21cc42010-07-19 21:46:24 +000096 FatalErrorOccurred = false;
Douglas Gregor8a60bbe2011-07-06 17:40:26 +000097 UnrecoverableErrorOccurred = false;
Douglas Gregoraa21cc42010-07-19 21:46:24 +000098
99 NumWarnings = 0;
100 NumErrors = 0;
Argyrios Kyrtzidis1fa8b4b2011-07-29 01:25:44 +0000101 TrapNumErrorsOccurred = 0;
102 TrapNumUnrecoverableErrorsOccurred = 0;
Douglas Gregor8a60bbe2011-07-06 17:40:26 +0000103
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000104 CurDiagID = ~0U;
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000105 LastDiagLevel = DiagnosticIDs::Ignored;
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000106 DelayedDiagID = 0;
Argyrios Kyrtzidisbbbeea12011-03-26 18:58:17 +0000107
108 // Clear state related to #pragma diagnostic.
109 DiagStates.clear();
110 DiagStatePoints.clear();
111 DiagStateOnPushStack.clear();
112
113 // Create a DiagState and DiagStatePoint representing diagnostic changes
114 // through command-line.
115 DiagStates.push_back(DiagState());
Richard Smithf995f2c2012-08-14 04:19:29 +0000116 DiagStatePoints.push_back(DiagStatePoint(&DiagStates.back(), FullSourceLoc()));
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000117}
Chris Lattner22eb9722006-06-18 05:43:12 +0000118
David Blaikie9c902b52011-09-25 23:23:43 +0000119void DiagnosticsEngine::SetDelayedDiagnostic(unsigned DiagID, StringRef Arg1,
Chad Rosier849a67b2012-02-07 23:24:49 +0000120 StringRef Arg2) {
Douglas Gregor85795312010-03-22 15:10:57 +0000121 if (DelayedDiagID)
122 return;
123
124 DelayedDiagID = DiagID;
Douglas Gregor96380982010-03-22 15:47:45 +0000125 DelayedDiagArg1 = Arg1.str();
126 DelayedDiagArg2 = Arg2.str();
Douglas Gregor85795312010-03-22 15:10:57 +0000127}
128
David Blaikie9c902b52011-09-25 23:23:43 +0000129void DiagnosticsEngine::ReportDelayed() {
Douglas Gregor85795312010-03-22 15:10:57 +0000130 Report(DelayedDiagID) << DelayedDiagArg1 << DelayedDiagArg2;
131 DelayedDiagID = 0;
132 DelayedDiagArg1.clear();
133 DelayedDiagArg2.clear();
134}
135
David Blaikie9c902b52011-09-25 23:23:43 +0000136DiagnosticsEngine::DiagStatePointsTy::iterator
137DiagnosticsEngine::GetDiagStatePointForLoc(SourceLocation L) const {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000138 assert(!DiagStatePoints.empty());
139 assert(DiagStatePoints.front().Loc.isInvalid() &&
140 "Should have created a DiagStatePoint for command-line");
141
Richard Smith99eff012012-08-17 00:55:32 +0000142 if (!SourceMgr)
143 return DiagStatePoints.end() - 1;
144
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000145 FullSourceLoc Loc(L, *SourceMgr);
146 if (Loc.isInvalid())
147 return DiagStatePoints.end() - 1;
148
149 DiagStatePointsTy::iterator Pos = DiagStatePoints.end();
150 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
151 if (LastStateChangePos.isValid() &&
152 Loc.isBeforeInTranslationUnitThan(LastStateChangePos))
153 Pos = std::upper_bound(DiagStatePoints.begin(), DiagStatePoints.end(),
Craig Topperf1186c52014-05-08 06:41:40 +0000154 DiagStatePoint(nullptr, Loc));
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000155 --Pos;
156 return Pos;
157}
158
Alp Tokerd576e002014-06-12 11:13:52 +0000159void DiagnosticsEngine::setSeverity(diag::kind Diag, diag::Severity Map,
160 SourceLocation L) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000161 assert(Diag < diag::DIAG_UPPER_LIMIT &&
162 "Can only map builtin diagnostics");
163 assert((Diags->isBuiltinWarningOrExtension(Diag) ||
Alp Toker46df1c02014-06-12 10:15:20 +0000164 (Map == diag::Severity::Fatal || Map == diag::Severity::Error)) &&
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000165 "Cannot map errors into warnings!");
166 assert(!DiagStatePoints.empty());
Richard Smith8a0527d2012-08-14 22:37:22 +0000167 assert((L.isInvalid() || SourceMgr) && "No SourceMgr for valid location");
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000168
Richard Smith8a0527d2012-08-14 22:37:22 +0000169 FullSourceLoc Loc = SourceMgr? FullSourceLoc(L, *SourceMgr) : FullSourceLoc();
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000170 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
Chad Rosierd1956e42012-02-03 01:49:51 +0000171 // Don't allow a mapping to a warning override an error/fatal mapping.
Alp Toker46df1c02014-06-12 10:15:20 +0000172 if (Map == diag::Severity::Warning) {
Alp Tokerc726c362014-06-10 09:31:37 +0000173 DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag);
Alp Toker46df1c02014-06-12 10:15:20 +0000174 if (Info.getSeverity() == diag::Severity::Error ||
175 Info.getSeverity() == diag::Severity::Fatal)
Alp Tokerc726c362014-06-10 09:31:37 +0000176 Map = Info.getSeverity();
Chad Rosierd1956e42012-02-03 01:49:51 +0000177 }
Alp Tokerc726c362014-06-10 09:31:37 +0000178 DiagnosticMapping Mapping = makeUserMapping(Map, L);
Daniel Dunbar2fba0972011-10-04 21:17:24 +0000179
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000180 // Common case; setting all the diagnostics of a group in one place.
181 if (Loc.isInvalid() || Loc == LastStateChangePos) {
Alp Tokerc726c362014-06-10 09:31:37 +0000182 GetCurDiagState()->setMapping(Diag, Mapping);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000183 return;
184 }
185
186 // Another common case; modifying diagnostic state in a source location
187 // after the previous one.
188 if ((Loc.isValid() && LastStateChangePos.isInvalid()) ||
189 LastStateChangePos.isBeforeInTranslationUnitThan(Loc)) {
Chris Lattner57540c52011-04-15 05:22:18 +0000190 // A diagnostic pragma occurred, create a new DiagState initialized with
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000191 // the current one and a new DiagStatePoint to record at which location
192 // the new state became active.
193 DiagStates.push_back(*GetCurDiagState());
194 PushDiagStatePoint(&DiagStates.back(), Loc);
Alp Tokerc726c362014-06-10 09:31:37 +0000195 GetCurDiagState()->setMapping(Diag, Mapping);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000196 return;
197 }
198
199 // We allow setting the diagnostic state in random source order for
200 // completeness but it should not be actually happening in normal practice.
201
202 DiagStatePointsTy::iterator Pos = GetDiagStatePointForLoc(Loc);
203 assert(Pos != DiagStatePoints.end());
204
205 // Update all diagnostic states that are active after the given location.
206 for (DiagStatePointsTy::iterator
207 I = Pos+1, E = DiagStatePoints.end(); I != E; ++I) {
Alp Tokerc726c362014-06-10 09:31:37 +0000208 GetCurDiagState()->setMapping(Diag, Mapping);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000209 }
210
211 // If the location corresponds to an existing point, just update its state.
212 if (Pos->Loc == Loc) {
Alp Tokerc726c362014-06-10 09:31:37 +0000213 GetCurDiagState()->setMapping(Diag, Mapping);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000214 return;
215 }
216
217 // Create a new state/point and fit it into the vector of DiagStatePoints
218 // so that the vector is always ordered according to location.
Alp Toker14c8aff2014-01-26 08:12:32 +0000219 assert(Pos->Loc.isBeforeInTranslationUnitThan(Loc));
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000220 DiagStates.push_back(*Pos->State);
221 DiagState *NewState = &DiagStates.back();
Alp Tokerc726c362014-06-10 09:31:37 +0000222 GetCurDiagState()->setMapping(Diag, Mapping);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000223 DiagStatePoints.insert(Pos+1, DiagStatePoint(NewState,
224 FullSourceLoc(Loc, *SourceMgr)));
225}
226
Richard Smith3be1cb22014-08-07 00:24:21 +0000227bool DiagnosticsEngine::setSeverityForGroup(diag::Flavor Flavor,
228 StringRef Group, diag::Severity Map,
Alp Tokerd576e002014-06-12 11:13:52 +0000229 SourceLocation Loc) {
Daniel Dunbard908c122011-09-29 01:47:16 +0000230 // Get the diagnostics in this group.
Hans Wennborgeb7cd662014-08-11 16:05:54 +0000231 SmallVector<diag::kind, 256> GroupDiags;
Richard Smith3be1cb22014-08-07 00:24:21 +0000232 if (Diags->getDiagnosticsInGroup(Flavor, Group, GroupDiags))
Daniel Dunbard908c122011-09-29 01:47:16 +0000233 return true;
234
235 // Set the mapping.
Hans Wennborgeb7cd662014-08-11 16:05:54 +0000236 for (diag::kind Diag : GroupDiags)
237 setSeverity(Diag, Map, Loc);
Daniel Dunbard908c122011-09-29 01:47:16 +0000238
239 return false;
240}
241
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000242bool DiagnosticsEngine::setDiagnosticGroupWarningAsError(StringRef Group,
243 bool Enabled) {
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000244 // If we are enabling this feature, just set the diagnostic mappings to map to
245 // errors.
246 if (Enabled)
Richard Smith3be1cb22014-08-07 00:24:21 +0000247 return setSeverityForGroup(diag::Flavor::WarningOrError, Group,
248 diag::Severity::Error);
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000249
250 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
251 // potentially downgrade anything already mapped to be a warning.
252
253 // Get the diagnostics in this group.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000254 SmallVector<diag::kind, 8> GroupDiags;
Richard Smith3be1cb22014-08-07 00:24:21 +0000255 if (Diags->getDiagnosticsInGroup(diag::Flavor::WarningOrError, Group,
256 GroupDiags))
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000257 return true;
258
259 // Perform the mapping change.
260 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i) {
Alp Tokerc726c362014-06-10 09:31:37 +0000261 DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(GroupDiags[i]);
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000262
Alp Toker46df1c02014-06-12 10:15:20 +0000263 if (Info.getSeverity() == diag::Severity::Error ||
264 Info.getSeverity() == diag::Severity::Fatal)
265 Info.setSeverity(diag::Severity::Warning);
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000266
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000267 Info.setNoWarningAsError(true);
268 }
269
270 return false;
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000271}
272
273bool DiagnosticsEngine::setDiagnosticGroupErrorAsFatal(StringRef Group,
274 bool Enabled) {
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000275 // If we are enabling this feature, just set the diagnostic mappings to map to
276 // fatal errors.
277 if (Enabled)
Richard Smith3be1cb22014-08-07 00:24:21 +0000278 return setSeverityForGroup(diag::Flavor::WarningOrError, Group,
279 diag::Severity::Fatal);
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000280
281 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
282 // potentially downgrade anything already mapped to be an error.
283
284 // Get the diagnostics in this group.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000285 SmallVector<diag::kind, 8> GroupDiags;
Richard Smith3be1cb22014-08-07 00:24:21 +0000286 if (Diags->getDiagnosticsInGroup(diag::Flavor::WarningOrError, Group,
287 GroupDiags))
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000288 return true;
289
290 // Perform the mapping change.
291 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i) {
Alp Tokerc726c362014-06-10 09:31:37 +0000292 DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(GroupDiags[i]);
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000293
Alp Toker46df1c02014-06-12 10:15:20 +0000294 if (Info.getSeverity() == diag::Severity::Fatal)
295 Info.setSeverity(diag::Severity::Error);
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000296
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000297 Info.setNoErrorAsFatal(true);
298 }
299
300 return false;
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000301}
302
Richard Smith3be1cb22014-08-07 00:24:21 +0000303void DiagnosticsEngine::setSeverityForAll(diag::Flavor Flavor,
304 diag::Severity Map,
Alp Tokerd576e002014-06-12 11:13:52 +0000305 SourceLocation Loc) {
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000306 // Get all the diagnostics.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000307 SmallVector<diag::kind, 64> AllDiags;
Richard Smith3be1cb22014-08-07 00:24:21 +0000308 Diags->getAllDiagnostics(Flavor, AllDiags);
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000309
310 // Set the mapping.
311 for (unsigned i = 0, e = AllDiags.size(); i != e; ++i)
312 if (Diags->isBuiltinWarningOrExtension(AllDiags[i]))
Alp Tokerd576e002014-06-12 11:13:52 +0000313 setSeverity(AllDiags[i], Map, Loc);
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000314}
315
David Blaikie9c902b52011-09-25 23:23:43 +0000316void DiagnosticsEngine::Report(const StoredDiagnostic &storedDiag) {
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000317 assert(CurDiagID == ~0U && "Multiple diagnostics in flight at once!");
318
319 CurDiagLoc = storedDiag.getLocation();
320 CurDiagID = storedDiag.getID();
321 NumDiagArgs = 0;
322
Alexander Kornienkod3b4e082014-05-22 19:56:11 +0000323 DiagRanges.clear();
324 DiagRanges.reserve(storedDiag.range_size());
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000325 for (StoredDiagnostic::range_iterator
326 RI = storedDiag.range_begin(),
327 RE = storedDiag.range_end(); RI != RE; ++RI)
Alexander Kornienkod3b4e082014-05-22 19:56:11 +0000328 DiagRanges.push_back(*RI);
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000329
Alexander Kornienkod3b4e082014-05-22 19:56:11 +0000330 DiagFixItHints.clear();
331 DiagFixItHints.reserve(storedDiag.fixit_size());
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000332 for (StoredDiagnostic::fixit_iterator
333 FI = storedDiag.fixit_begin(),
334 FE = storedDiag.fixit_end(); FI != FE; ++FI)
Alexander Kornienkod3b4e082014-05-22 19:56:11 +0000335 DiagFixItHints.push_back(*FI);
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000336
David Blaikiee2eefae2011-09-25 23:39:51 +0000337 assert(Client && "DiagnosticConsumer not set!");
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000338 Level DiagLevel = storedDiag.getLevel();
David Blaikieb5784322011-09-26 01:18:08 +0000339 Diagnostic Info(this, storedDiag.getMessage());
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000340 Client->HandleDiagnostic(DiagLevel, Info);
341 if (Client->IncludeInDiagnosticCounts()) {
David Blaikie9c902b52011-09-25 23:23:43 +0000342 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000343 ++NumWarnings;
344 }
345
346 CurDiagID = ~0U;
347}
348
Jordan Rose6f524ac2012-07-11 16:50:36 +0000349bool DiagnosticsEngine::EmitCurrentDiagnostic(bool Force) {
350 assert(getClient() && "DiagnosticClient not set!");
351
352 bool Emitted;
353 if (Force) {
354 Diagnostic Info(this);
355
356 // Figure out the diagnostic level of this message.
357 DiagnosticIDs::Level DiagLevel
358 = Diags->getDiagnosticLevel(Info.getID(), Info.getLocation(), *this);
359
360 Emitted = (DiagLevel != DiagnosticIDs::Ignored);
361 if (Emitted) {
362 // Emit the diagnostic regardless of suppression level.
363 Diags->EmitDiag(*this, DiagLevel);
364 }
365 } else {
366 // Process the diagnostic, sending the accumulated information to the
367 // DiagnosticConsumer.
368 Emitted = ProcessDiag();
369 }
Douglas Gregor85795312010-03-22 15:10:57 +0000370
371 // Clear out the current diagnostic object.
Daniel Dunbarc7c00892012-03-13 21:02:14 +0000372 unsigned DiagID = CurDiagID;
373 Clear();
Douglas Gregor85795312010-03-22 15:10:57 +0000374
375 // If there was a delayed diagnostic, emit it now.
Jordan Rose6f524ac2012-07-11 16:50:36 +0000376 if (!Force && DelayedDiagID && DelayedDiagID != DiagID)
Daniel Dunbarc7c00892012-03-13 21:02:14 +0000377 ReportDelayed();
Douglas Gregor85795312010-03-22 15:10:57 +0000378
379 return Emitted;
380}
381
Nico Weber4c311642008-08-10 19:59:06 +0000382
David Blaikiee2eefae2011-09-25 23:39:51 +0000383DiagnosticConsumer::~DiagnosticConsumer() {}
Nico Weber4c311642008-08-10 19:59:06 +0000384
David Blaikiee2eefae2011-09-25 23:39:51 +0000385void DiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
David Blaikieb5784322011-09-26 01:18:08 +0000386 const Diagnostic &Info) {
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000387 if (!IncludeInDiagnosticCounts())
388 return;
389
David Blaikie9c902b52011-09-25 23:23:43 +0000390 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000391 ++NumWarnings;
David Blaikie9c902b52011-09-25 23:23:43 +0000392 else if (DiagLevel >= DiagnosticsEngine::Error)
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000393 ++NumErrors;
394}
Chris Lattner23be0672008-11-19 06:51:40 +0000395
Chris Lattner2b786902008-11-21 07:50:02 +0000396/// ModifierIs - Return true if the specified modifier matches specified string.
397template <std::size_t StrLen>
398static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
399 const char (&Str)[StrLen]) {
400 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
401}
402
John McCall8cb7a8a32010-01-14 20:11:39 +0000403/// ScanForward - Scans forward, looking for the given character, skipping
404/// nested clauses and escaped characters.
405static const char *ScanFormat(const char *I, const char *E, char Target) {
406 unsigned Depth = 0;
407
408 for ( ; I != E; ++I) {
409 if (Depth == 0 && *I == Target) return I;
410 if (Depth != 0 && *I == '}') Depth--;
411
412 if (*I == '%') {
413 I++;
414 if (I == E) break;
415
416 // Escaped characters get implicitly skipped here.
417
418 // Format specifier.
Jordan Rosea7d03842013-02-08 22:30:41 +0000419 if (!isDigit(*I) && !isPunctuation(*I)) {
420 for (I++; I != E && !isDigit(*I) && *I != '{'; I++) ;
John McCall8cb7a8a32010-01-14 20:11:39 +0000421 if (I == E) break;
422 if (*I == '{')
423 Depth++;
424 }
425 }
426 }
427 return E;
428}
429
Chris Lattner2b786902008-11-21 07:50:02 +0000430/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
431/// like this: %select{foo|bar|baz}2. This means that the integer argument
432/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
433/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
434/// This is very useful for certain classes of variant diagnostics.
David Blaikieb5784322011-09-26 01:18:08 +0000435static void HandleSelectModifier(const Diagnostic &DInfo, unsigned ValNo,
Chris Lattner2b786902008-11-21 07:50:02 +0000436 const char *Argument, unsigned ArgumentLen,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000437 SmallVectorImpl<char> &OutStr) {
Chris Lattner2b786902008-11-21 07:50:02 +0000438 const char *ArgumentEnd = Argument+ArgumentLen;
Mike Stump11289f42009-09-09 15:08:12 +0000439
Chris Lattner2b786902008-11-21 07:50:02 +0000440 // Skip over 'ValNo' |'s.
441 while (ValNo) {
John McCall8cb7a8a32010-01-14 20:11:39 +0000442 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
Chris Lattner2b786902008-11-21 07:50:02 +0000443 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
444 " larger than the number of options in the diagnostic string!");
445 Argument = NextVal+1; // Skip this string.
446 --ValNo;
447 }
Mike Stump11289f42009-09-09 15:08:12 +0000448
Chris Lattner2b786902008-11-21 07:50:02 +0000449 // Get the end of the value. This is either the } or the |.
John McCall8cb7a8a32010-01-14 20:11:39 +0000450 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
John McCalle4d54322010-01-13 23:58:20 +0000451
452 // Recursively format the result of the select clause into the output string.
453 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000454}
455
456/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
457/// letter 's' to the string if the value is not 1. This is used in cases like
458/// this: "you idiot, you have %4 parameter%s4!".
459static void HandleIntegerSModifier(unsigned ValNo,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000460 SmallVectorImpl<char> &OutStr) {
Chris Lattner2b786902008-11-21 07:50:02 +0000461 if (ValNo != 1)
462 OutStr.push_back('s');
463}
464
John McCall9015cde2010-01-14 00:50:32 +0000465/// HandleOrdinalModifier - Handle the integer 'ord' modifier. This
466/// prints the ordinal form of the given integer, with 1 corresponding
467/// to the first ordinal. Currently this is hard-coded to use the
468/// English form.
469static void HandleOrdinalModifier(unsigned ValNo,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000470 SmallVectorImpl<char> &OutStr) {
John McCall9015cde2010-01-14 00:50:32 +0000471 assert(ValNo != 0 && "ValNo must be strictly positive!");
472
473 llvm::raw_svector_ostream Out(OutStr);
474
475 // We could use text forms for the first N ordinals, but the numeric
476 // forms are actually nicer in diagnostics because they stand out.
Jordan Rosec102b352012-09-22 01:24:42 +0000477 Out << ValNo << llvm::getOrdinalSuffix(ValNo);
John McCall9015cde2010-01-14 00:50:32 +0000478}
479
Chris Lattner2b786902008-11-21 07:50:02 +0000480
Sebastian Redl15b02d22008-11-22 13:44:36 +0000481/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000482static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000483 // Programming 101: Parse a decimal number :-)
484 unsigned Val = 0;
485 while (Start != End && *Start >= '0' && *Start <= '9') {
486 Val *= 10;
487 Val += *Start - '0';
488 ++Start;
489 }
490 return Val;
491}
492
493/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000494static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000495 if (*Start != '[') {
496 unsigned Ref = PluralNumber(Start, End);
497 return Ref == Val;
498 }
499
500 ++Start;
501 unsigned Low = PluralNumber(Start, End);
502 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
503 ++Start;
504 unsigned High = PluralNumber(Start, End);
505 assert(*Start == ']' && "Bad plural expression syntax: expected )");
506 ++Start;
507 return Low <= Val && Val <= High;
508}
509
510/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattner2fe29202009-04-15 17:13:42 +0000511static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000512 // Empty condition?
513 if (*Start == ':')
514 return true;
515
516 while (1) {
517 char C = *Start;
518 if (C == '%') {
519 // Modulo expression
520 ++Start;
521 unsigned Arg = PluralNumber(Start, End);
522 assert(*Start == '=' && "Bad plural expression syntax: expected =");
523 ++Start;
524 unsigned ValMod = ValNo % Arg;
525 if (TestPluralRange(ValMod, Start, End))
526 return true;
527 } else {
Sebastian Redl3ceaf622008-11-27 07:28:14 +0000528 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redl15b02d22008-11-22 13:44:36 +0000529 "Bad plural expression syntax: unexpected character");
530 // Range expression
531 if (TestPluralRange(ValNo, Start, End))
532 return true;
533 }
534
535 // Scan for next or-expr part.
536 Start = std::find(Start, End, ',');
Mike Stump11289f42009-09-09 15:08:12 +0000537 if (Start == End)
Sebastian Redl15b02d22008-11-22 13:44:36 +0000538 break;
539 ++Start;
540 }
541 return false;
542}
543
544/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
545/// for complex plural forms, or in languages where all plurals are complex.
546/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
547/// conditions that are tested in order, the form corresponding to the first
548/// that applies being emitted. The empty condition is always true, making the
549/// last form a default case.
550/// Conditions are simple boolean expressions, where n is the number argument.
551/// Here are the rules.
552/// condition := expression | empty
553/// empty := -> always true
554/// expression := numeric [',' expression] -> logical or
555/// numeric := range -> true if n in range
556/// | '%' number '=' range -> true if n % number in range
557/// range := number
558/// | '[' number ',' number ']' -> ranges are inclusive both ends
559///
560/// Here are some examples from the GNU gettext manual written in this form:
561/// English:
562/// {1:form0|:form1}
563/// Latvian:
564/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
565/// Gaeilge:
566/// {1:form0|2:form1|:form2}
567/// Romanian:
568/// {1:form0|0,%100=[1,19]:form1|:form2}
569/// Lithuanian:
570/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
571/// Russian (requires repeated form):
572/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
573/// Slovak
574/// {1:form0|[2,4]:form1|:form2}
575/// Polish (requires repeated form):
576/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
David Blaikieb5784322011-09-26 01:18:08 +0000577static void HandlePluralModifier(const Diagnostic &DInfo, unsigned ValNo,
Sebastian Redl15b02d22008-11-22 13:44:36 +0000578 const char *Argument, unsigned ArgumentLen,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000579 SmallVectorImpl<char> &OutStr) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000580 const char *ArgumentEnd = Argument + ArgumentLen;
581 while (1) {
582 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
583 const char *ExprEnd = Argument;
584 while (*ExprEnd != ':') {
585 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
586 ++ExprEnd;
587 }
588 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
589 Argument = ExprEnd + 1;
John McCall8cb7a8a32010-01-14 20:11:39 +0000590 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
John McCall43b61682010-10-14 01:55:31 +0000591
592 // Recursively format the result of the plural clause into the
593 // output string.
594 DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000595 return;
596 }
John McCall8cb7a8a32010-01-14 20:11:39 +0000597 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
Sebastian Redl15b02d22008-11-22 13:44:36 +0000598 }
599}
600
Alp Tokera231ad22014-01-06 12:54:18 +0000601/// \brief Returns the friendly description for a token kind that will appear
602/// without quotes in diagnostic messages. These strings may be translatable in
603/// future.
604static const char *getTokenDescForDiagnostic(tok::TokenKind Kind) {
Alp Tokerec543272013-12-24 09:48:30 +0000605 switch (Kind) {
606 case tok::identifier:
607 return "identifier";
608 default:
Craig Topperf1186c52014-05-08 06:41:40 +0000609 return nullptr;
Alp Tokerec543272013-12-24 09:48:30 +0000610 }
611}
Sebastian Redl15b02d22008-11-22 13:44:36 +0000612
Chris Lattner23be0672008-11-19 06:51:40 +0000613/// FormatDiagnostic - Format this diagnostic into a string, substituting the
614/// formal arguments into the %0 slots. The result is appended onto the Str
615/// array.
David Blaikieb5784322011-09-26 01:18:08 +0000616void Diagnostic::
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000617FormatDiagnostic(SmallVectorImpl<char> &OutStr) const {
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000618 if (!StoredDiagMessage.empty()) {
619 OutStr.append(StoredDiagMessage.begin(), StoredDiagMessage.end());
620 return;
621 }
622
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000623 StringRef Diag =
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000624 getDiags()->getDiagnosticIDs()->getDescription(getID());
Mike Stump11289f42009-09-09 15:08:12 +0000625
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000626 FormatDiagnostic(Diag.begin(), Diag.end(), OutStr);
John McCalle4d54322010-01-13 23:58:20 +0000627}
628
David Blaikieb5784322011-09-26 01:18:08 +0000629void Diagnostic::
John McCalle4d54322010-01-13 23:58:20 +0000630FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000631 SmallVectorImpl<char> &OutStr) const {
John McCalle4d54322010-01-13 23:58:20 +0000632
Richard Trieub3b8bb02015-01-08 01:27:03 +0000633 // When the diagnostic string is only "%0", the entire string is being given
634 // by an outside source. Remove unprintable characters from this string
635 // and skip all the other string processing.
636 if (DiagEnd - DiagStr == 2 && DiagStr[0] == '%' && DiagStr[1] == '0' &&
637 getArgKind(0) == DiagnosticsEngine::ak_std_string) {
638 const std::string &S = getArgStdStr(0);
639 for (char c : S) {
640 if (llvm::sys::locale::isPrint(c) || c == '\t') {
641 OutStr.push_back(c);
642 }
643 }
644 return;
645 }
646
Chris Lattnerc243f292009-10-20 05:25:22 +0000647 /// FormattedArgs - Keep track of all of the arguments formatted by
648 /// ConvertArgToString and pass them into subsequent calls to
649 /// ConvertArgToString, allowing the implementation to avoid redundancies in
650 /// obvious cases.
David Blaikie9c902b52011-09-25 23:23:43 +0000651 SmallVector<DiagnosticsEngine::ArgumentValue, 8> FormattedArgs;
Chandler Carruthd5173952011-07-11 17:49:21 +0000652
653 /// QualTypeVals - Pass a vector of arrays so that QualType names can be
654 /// compared to see if more information is needed to be printed.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000655 SmallVector<intptr_t, 2> QualTypeVals;
Richard Trieu91844232012-06-26 18:18:47 +0000656 SmallVector<char, 64> Tree;
657
Chandler Carruthd5173952011-07-11 17:49:21 +0000658 for (unsigned i = 0, e = getNumArgs(); i < e; ++i)
David Blaikie9c902b52011-09-25 23:23:43 +0000659 if (getArgKind(i) == DiagnosticsEngine::ak_qualtype)
Chandler Carruthd5173952011-07-11 17:49:21 +0000660 QualTypeVals.push_back(getRawArg(i));
661
Chris Lattner23be0672008-11-19 06:51:40 +0000662 while (DiagStr != DiagEnd) {
663 if (DiagStr[0] != '%') {
664 // Append non-%0 substrings to Str if we have one.
665 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
666 OutStr.append(DiagStr, StrEnd);
667 DiagStr = StrEnd;
Chris Lattner2b786902008-11-21 07:50:02 +0000668 continue;
Jordan Rosea7d03842013-02-08 22:30:41 +0000669 } else if (isPunctuation(DiagStr[1])) {
John McCall8cb7a8a32010-01-14 20:11:39 +0000670 OutStr.push_back(DiagStr[1]); // %% -> %.
Chris Lattner23be0672008-11-19 06:51:40 +0000671 DiagStr += 2;
Chris Lattner2b786902008-11-21 07:50:02 +0000672 continue;
673 }
Mike Stump11289f42009-09-09 15:08:12 +0000674
Chris Lattner2b786902008-11-21 07:50:02 +0000675 // Skip the %.
676 ++DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000677
Chris Lattner2b786902008-11-21 07:50:02 +0000678 // This must be a placeholder for a diagnostic argument. The format for a
679 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
680 // The digit is a number from 0-9 indicating which argument this comes from.
681 // The modifier is a string of digits from the set [-a-z]+, arguments is a
682 // brace enclosed string.
Craig Topperf1186c52014-05-08 06:41:40 +0000683 const char *Modifier = nullptr, *Argument = nullptr;
Chris Lattner2b786902008-11-21 07:50:02 +0000684 unsigned ModifierLen = 0, ArgumentLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000685
Chris Lattner2b786902008-11-21 07:50:02 +0000686 // Check to see if we have a modifier. If so eat it.
Jordan Rosea7d03842013-02-08 22:30:41 +0000687 if (!isDigit(DiagStr[0])) {
Chris Lattner2b786902008-11-21 07:50:02 +0000688 Modifier = DiagStr;
689 while (DiagStr[0] == '-' ||
690 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
691 ++DiagStr;
692 ModifierLen = DiagStr-Modifier;
Chris Lattner23be0672008-11-19 06:51:40 +0000693
Chris Lattner2b786902008-11-21 07:50:02 +0000694 // If we have an argument, get it next.
695 if (DiagStr[0] == '{') {
696 ++DiagStr; // Skip {.
697 Argument = DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000698
John McCall8cb7a8a32010-01-14 20:11:39 +0000699 DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
700 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
Chris Lattner2b786902008-11-21 07:50:02 +0000701 ArgumentLen = DiagStr-Argument;
702 ++DiagStr; // Skip }.
Chris Lattner23be0672008-11-19 06:51:40 +0000703 }
Chris Lattner2b786902008-11-21 07:50:02 +0000704 }
Mike Stump11289f42009-09-09 15:08:12 +0000705
Jordan Rosea7d03842013-02-08 22:30:41 +0000706 assert(isDigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000707 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattner2b786902008-11-21 07:50:02 +0000708
Richard Trieu91844232012-06-26 18:18:47 +0000709 // Only used for type diffing.
710 unsigned ArgNo2 = ArgNo;
711
David Blaikie9c902b52011-09-25 23:23:43 +0000712 DiagnosticsEngine::ArgumentKind Kind = getArgKind(ArgNo);
Richard Trieu90c31f52013-01-30 20:04:31 +0000713 if (ModifierIs(Modifier, ModifierLen, "diff")) {
Jordan Rosea7d03842013-02-08 22:30:41 +0000714 assert(*DiagStr == ',' && isDigit(*(DiagStr + 1)) &&
Richard Trieu91844232012-06-26 18:18:47 +0000715 "Invalid format for diff modifier");
716 ++DiagStr; // Comma.
717 ArgNo2 = *DiagStr++ - '0';
Richard Trieu90c31f52013-01-30 20:04:31 +0000718 DiagnosticsEngine::ArgumentKind Kind2 = getArgKind(ArgNo2);
719 if (Kind == DiagnosticsEngine::ak_qualtype &&
720 Kind2 == DiagnosticsEngine::ak_qualtype)
721 Kind = DiagnosticsEngine::ak_qualtype_pair;
722 else {
723 // %diff only supports QualTypes. For other kinds of arguments,
724 // use the default printing. For example, if the modifier is:
725 // "%diff{compare $ to $|other text}1,2"
726 // treat it as:
727 // "compare %1 to %2"
728 const char *Pipe = ScanFormat(Argument, Argument + ArgumentLen, '|');
729 const char *FirstDollar = ScanFormat(Argument, Pipe, '$');
730 const char *SecondDollar = ScanFormat(FirstDollar + 1, Pipe, '$');
Filipe Cabecinhased4a00c2013-01-30 22:03:24 +0000731 const char ArgStr1[] = { '%', static_cast<char>('0' + ArgNo) };
732 const char ArgStr2[] = { '%', static_cast<char>('0' + ArgNo2) };
Richard Trieu90c31f52013-01-30 20:04:31 +0000733 FormatDiagnostic(Argument, FirstDollar, OutStr);
734 FormatDiagnostic(ArgStr1, ArgStr1 + 2, OutStr);
735 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
736 FormatDiagnostic(ArgStr2, ArgStr2 + 2, OutStr);
737 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
738 continue;
739 }
Richard Trieu91844232012-06-26 18:18:47 +0000740 }
Chris Lattnerc243f292009-10-20 05:25:22 +0000741
742 switch (Kind) {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000743 // ---- STRINGS ----
David Blaikie9c902b52011-09-25 23:23:43 +0000744 case DiagnosticsEngine::ak_std_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000745 const std::string &S = getArgStdStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000746 assert(ModifierLen == 0 && "No modifiers for strings yet");
747 OutStr.append(S.begin(), S.end());
748 break;
749 }
David Blaikie9c902b52011-09-25 23:23:43 +0000750 case DiagnosticsEngine::ak_c_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000751 const char *S = getArgCStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000752 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000753
754 // Don't crash if get passed a null pointer by accident.
755 if (!S)
756 S = "(null)";
Mike Stump11289f42009-09-09 15:08:12 +0000757
Chris Lattner2b786902008-11-21 07:50:02 +0000758 OutStr.append(S, S + strlen(S));
759 break;
760 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000761 // ---- INTEGERS ----
David Blaikie9c902b52011-09-25 23:23:43 +0000762 case DiagnosticsEngine::ak_sint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000763 int Val = getArgSInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000764
Chris Lattner2b786902008-11-21 07:50:02 +0000765 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCall43b61682010-10-14 01:55:31 +0000766 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen,
767 OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000768 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
769 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000770 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCall43b61682010-10-14 01:55:31 +0000771 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
772 OutStr);
John McCall9015cde2010-01-14 00:50:32 +0000773 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
774 HandleOrdinalModifier((unsigned)Val, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000775 } else {
776 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000777 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000778 }
Chris Lattner2b786902008-11-21 07:50:02 +0000779 break;
780 }
David Blaikie9c902b52011-09-25 23:23:43 +0000781 case DiagnosticsEngine::ak_uint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000782 unsigned Val = getArgUInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000783
Chris Lattner2b786902008-11-21 07:50:02 +0000784 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCalle4d54322010-01-13 23:58:20 +0000785 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000786 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
787 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000788 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCall43b61682010-10-14 01:55:31 +0000789 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
790 OutStr);
John McCall9015cde2010-01-14 00:50:32 +0000791 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
792 HandleOrdinalModifier(Val, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000793 } else {
794 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000795 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000796 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000797 break;
Chris Lattner2b786902008-11-21 07:50:02 +0000798 }
Alp Tokerec543272013-12-24 09:48:30 +0000799 // ---- TOKEN SPELLINGS ----
800 case DiagnosticsEngine::ak_tokenkind: {
801 tok::TokenKind Kind = static_cast<tok::TokenKind>(getRawArg(ArgNo));
802 assert(ModifierLen == 0 && "No modifiers for token kinds yet");
803
804 llvm::raw_svector_ostream Out(OutStr);
Alp Tokera231ad22014-01-06 12:54:18 +0000805 if (const char *S = tok::getPunctuatorSpelling(Kind))
806 // Quoted token spelling for punctuators.
807 Out << '\'' << S << '\'';
808 else if (const char *S = tok::getKeywordSpelling(Kind))
809 // Unquoted token spelling for keywords.
810 Out << S;
811 else if (const char *S = getTokenDescForDiagnostic(Kind))
Alp Tokerec543272013-12-24 09:48:30 +0000812 // Unquoted translatable token name.
813 Out << S;
Alp Tokerec543272013-12-24 09:48:30 +0000814 else if (const char *S = tok::getTokenName(Kind))
815 // Debug name, shouldn't appear in user-facing diagnostics.
816 Out << '<' << S << '>';
817 else
818 Out << "(null)";
819 break;
820 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000821 // ---- NAMES and TYPES ----
David Blaikie9c902b52011-09-25 23:23:43 +0000822 case DiagnosticsEngine::ak_identifierinfo: {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000823 const IdentifierInfo *II = getArgIdentifier(ArgNo);
824 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000825
826 // Don't crash if get passed a null pointer by accident.
827 if (!II) {
828 const char *S = "(null)";
829 OutStr.append(S, S + strlen(S));
830 continue;
831 }
832
Daniel Dunbar07d07852009-10-18 21:17:35 +0000833 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
Chris Lattnere3d20d92008-11-23 21:45:46 +0000834 break;
835 }
David Blaikie9c902b52011-09-25 23:23:43 +0000836 case DiagnosticsEngine::ak_qualtype:
837 case DiagnosticsEngine::ak_declarationname:
838 case DiagnosticsEngine::ak_nameddecl:
839 case DiagnosticsEngine::ak_nestednamespec:
840 case DiagnosticsEngine::ak_declcontext:
Aaron Ballman3e424b52013-12-26 18:30:57 +0000841 case DiagnosticsEngine::ak_attr:
Chris Lattnerc243f292009-10-20 05:25:22 +0000842 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
Craig Topper3aa4fb32014-06-12 05:32:35 +0000843 StringRef(Modifier, ModifierLen),
844 StringRef(Argument, ArgumentLen),
Craig Toppere4753502014-06-12 05:32:27 +0000845 FormattedArgs,
Chandler Carruthd5173952011-07-11 17:49:21 +0000846 OutStr, QualTypeVals);
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000847 break;
Richard Trieu91844232012-06-26 18:18:47 +0000848 case DiagnosticsEngine::ak_qualtype_pair:
849 // Create a struct with all the info needed for printing.
850 TemplateDiffTypes TDT;
851 TDT.FromType = getRawArg(ArgNo);
852 TDT.ToType = getRawArg(ArgNo2);
853 TDT.ElideType = getDiags()->ElideType;
854 TDT.ShowColors = getDiags()->ShowColors;
Richard Trieu50f5f462012-07-10 01:46:04 +0000855 TDT.TemplateDiffUsed = false;
Richard Trieu91844232012-06-26 18:18:47 +0000856 intptr_t val = reinterpret_cast<intptr_t>(&TDT);
857
Richard Trieuc6058442012-06-29 21:12:16 +0000858 const char *ArgumentEnd = Argument + ArgumentLen;
859 const char *Pipe = ScanFormat(Argument, ArgumentEnd, '|');
860
Richard Trieua4056002012-07-13 21:18:32 +0000861 // Print the tree. If this diagnostic already has a tree, skip the
862 // second tree.
863 if (getDiags()->PrintTemplateTree && Tree.empty()) {
Richard Trieu91844232012-06-26 18:18:47 +0000864 TDT.PrintFromType = true;
865 TDT.PrintTree = true;
866 getDiags()->ConvertArgToString(Kind, val,
Craig Topper3aa4fb32014-06-12 05:32:35 +0000867 StringRef(Modifier, ModifierLen),
868 StringRef(Argument, ArgumentLen),
Craig Toppere4753502014-06-12 05:32:27 +0000869 FormattedArgs,
Richard Trieu91844232012-06-26 18:18:47 +0000870 Tree, QualTypeVals);
871 // If there is no tree information, fall back to regular printing.
Richard Trieuc6058442012-06-29 21:12:16 +0000872 if (!Tree.empty()) {
873 FormatDiagnostic(Pipe + 1, ArgumentEnd, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +0000874 break;
Richard Trieuc6058442012-06-29 21:12:16 +0000875 }
Richard Trieu91844232012-06-26 18:18:47 +0000876 }
877
878 // Non-tree printing, also the fall-back when tree printing fails.
879 // The fall-back is triggered when the types compared are not templates.
Richard Trieuc6058442012-06-29 21:12:16 +0000880 const char *FirstDollar = ScanFormat(Argument, ArgumentEnd, '$');
881 const char *SecondDollar = ScanFormat(FirstDollar + 1, ArgumentEnd, '$');
Richard Trieu91844232012-06-26 18:18:47 +0000882
883 // Append before text
Richard Trieuc6058442012-06-29 21:12:16 +0000884 FormatDiagnostic(Argument, FirstDollar, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +0000885
886 // Append first type
887 TDT.PrintTree = false;
888 TDT.PrintFromType = true;
889 getDiags()->ConvertArgToString(Kind, val,
Craig Topper3aa4fb32014-06-12 05:32:35 +0000890 StringRef(Modifier, ModifierLen),
891 StringRef(Argument, ArgumentLen),
Craig Toppere4753502014-06-12 05:32:27 +0000892 FormattedArgs,
Richard Trieu91844232012-06-26 18:18:47 +0000893 OutStr, QualTypeVals);
Richard Trieu50f5f462012-07-10 01:46:04 +0000894 if (!TDT.TemplateDiffUsed)
895 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
896 TDT.FromType));
897
Richard Trieu91844232012-06-26 18:18:47 +0000898 // Append middle text
Richard Trieuc6058442012-06-29 21:12:16 +0000899 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +0000900
901 // Append second type
902 TDT.PrintFromType = false;
903 getDiags()->ConvertArgToString(Kind, val,
Craig Topper3aa4fb32014-06-12 05:32:35 +0000904 StringRef(Modifier, ModifierLen),
905 StringRef(Argument, ArgumentLen),
Craig Toppere4753502014-06-12 05:32:27 +0000906 FormattedArgs,
Richard Trieu91844232012-06-26 18:18:47 +0000907 OutStr, QualTypeVals);
Richard Trieu50f5f462012-07-10 01:46:04 +0000908 if (!TDT.TemplateDiffUsed)
909 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
910 TDT.ToType));
911
Richard Trieu91844232012-06-26 18:18:47 +0000912 // Append end text
Richard Trieuc6058442012-06-29 21:12:16 +0000913 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +0000914 break;
Nico Weber4c311642008-08-10 19:59:06 +0000915 }
Chris Lattnerc243f292009-10-20 05:25:22 +0000916
917 // Remember this argument info for subsequent formatting operations. Turn
918 // std::strings into a null terminated string to make it be the same case as
919 // all the other ones.
Richard Trieu91844232012-06-26 18:18:47 +0000920 if (Kind == DiagnosticsEngine::ak_qualtype_pair)
921 continue;
922 else if (Kind != DiagnosticsEngine::ak_std_string)
Chris Lattnerc243f292009-10-20 05:25:22 +0000923 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
924 else
David Blaikie9c902b52011-09-25 23:23:43 +0000925 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_c_string,
Chris Lattnerc243f292009-10-20 05:25:22 +0000926 (intptr_t)getArgStdStr(ArgNo).c_str()));
927
Nico Weber4c311642008-08-10 19:59:06 +0000928 }
Richard Trieu91844232012-06-26 18:18:47 +0000929
930 // Append the type tree to the end of the diagnostics.
931 OutStr.append(Tree.begin(), Tree.end());
Nico Weber4c311642008-08-10 19:59:06 +0000932}
Ted Kremenekea06ec12009-01-23 20:28:53 +0000933
Douglas Gregor33cdd812010-02-18 18:08:43 +0000934StoredDiagnostic::StoredDiagnostic() { }
935
David Blaikie9c902b52011-09-25 23:23:43 +0000936StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000937 StringRef Message)
Benjamin Kramer929bd682010-11-19 17:36:51 +0000938 : ID(ID), Level(Level), Loc(), Message(Message) { }
Douglas Gregor33cdd812010-02-18 18:08:43 +0000939
David Blaikie9c902b52011-09-25 23:23:43 +0000940StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level,
David Blaikieb5784322011-09-26 01:18:08 +0000941 const Diagnostic &Info)
Douglas Gregora750e8e2010-11-19 16:18:16 +0000942 : ID(Info.getID()), Level(Level)
943{
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000944 assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
945 "Valid source location without setting a source manager for diagnostic");
946 if (Info.getLocation().isValid())
947 Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000948 SmallString<64> Message;
Douglas Gregor33cdd812010-02-18 18:08:43 +0000949 Info.FormatDiagnostic(Message);
950 this->Message.assign(Message.begin(), Message.end());
951
952 Ranges.reserve(Info.getNumRanges());
953 for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I)
954 Ranges.push_back(Info.getRange(I));
955
Douglas Gregora771f462010-03-31 17:46:05 +0000956 FixIts.reserve(Info.getNumFixItHints());
957 for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I)
958 FixIts.push_back(Info.getFixItHint(I));
Douglas Gregor33cdd812010-02-18 18:08:43 +0000959}
960
David Blaikie9c902b52011-09-25 23:23:43 +0000961StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000962 StringRef Message, FullSourceLoc Loc,
Chris Lattner54b16772011-07-23 17:14:25 +0000963 ArrayRef<CharSourceRange> Ranges,
Aaron Ballman234ebd72013-02-24 19:08:10 +0000964 ArrayRef<FixItHint> FixIts)
965 : ID(ID), Level(Level), Loc(Loc), Message(Message),
966 Ranges(Ranges.begin(), Ranges.end()), FixIts(FixIts.begin(), FixIts.end())
Douglas Gregor925296b2011-07-19 16:10:42 +0000967{
Douglas Gregor925296b2011-07-19 16:10:42 +0000968}
969
Douglas Gregor33cdd812010-02-18 18:08:43 +0000970StoredDiagnostic::~StoredDiagnostic() { }
971
Ted Kremenekea06ec12009-01-23 20:28:53 +0000972/// IncludeInDiagnosticCounts - This method (whose default implementation
973/// returns true) indicates whether the diagnostics handled by this
David Blaikiee2eefae2011-09-25 23:39:51 +0000974/// DiagnosticConsumer should be included in the number of diagnostics
David Blaikie9c902b52011-09-25 23:23:43 +0000975/// reported by DiagnosticsEngine.
David Blaikiee2eefae2011-09-25 23:39:51 +0000976bool DiagnosticConsumer::IncludeInDiagnosticCounts() const { return true; }
Douglas Gregor89336232010-03-29 23:34:08 +0000977
David Blaikie68e081d2011-12-20 02:48:34 +0000978void IgnoringDiagConsumer::anchor() { }
979
Douglas Gregor6b930962013-05-03 22:58:43 +0000980ForwardingDiagnosticConsumer::~ForwardingDiagnosticConsumer() {}
981
982void ForwardingDiagnosticConsumer::HandleDiagnostic(
983 DiagnosticsEngine::Level DiagLevel,
984 const Diagnostic &Info) {
985 Target.HandleDiagnostic(DiagLevel, Info);
986}
987
988void ForwardingDiagnosticConsumer::clear() {
989 DiagnosticConsumer::clear();
990 Target.clear();
991}
992
993bool ForwardingDiagnosticConsumer::IncludeInDiagnosticCounts() const {
994 return Target.IncludeInDiagnosticCounts();
995}
996
Benjamin Kramer7ec12c92012-02-07 22:29:24 +0000997PartialDiagnostic::StorageAllocator::StorageAllocator() {
Douglas Gregor89336232010-03-29 23:34:08 +0000998 for (unsigned I = 0; I != NumCached; ++I)
999 FreeList[I] = Cached + I;
1000 NumFreeListEntries = NumCached;
1001}
1002
Benjamin Kramer7ec12c92012-02-07 22:29:24 +00001003PartialDiagnostic::StorageAllocator::~StorageAllocator() {
Chad Rosier849a67b2012-02-07 23:24:49 +00001004 // Don't assert if we are in a CrashRecovery context, as this invariant may
1005 // be invalidated during a crash.
1006 assert((NumFreeListEntries == NumCached ||
1007 llvm::CrashRecoveryContext::isRecoveringFromCrash()) &&
1008 "A partial is on the lamb");
Douglas Gregor89336232010-03-29 23:34:08 +00001009}