blob: 7cf7305827fe44fdc0e7e03a3e68386ed213fb30 [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
Douglas Gregoraea7afd2015-06-24 22:02:08 +000027const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
28 DiagNullabilityKind nullability) {
29 StringRef string;
30 switch (nullability.first) {
31 case NullabilityKind::NonNull:
32 string = nullability.second ? "'nonnull'" : "'_Nonnull'";
33 break;
34
35 case NullabilityKind::Nullable:
36 string = nullability.second ? "'nullable'" : "'_Nullable'";
37 break;
38
39 case NullabilityKind::Unspecified:
40 string = nullability.second ? "'null_unspecified'" : "'_Null_unspecified'";
41 break;
42 }
43
44 DB.AddString(string);
45 return DB;
46}
47
David Blaikie9c902b52011-09-25 23:23:43 +000048static void DummyArgToStringFn(DiagnosticsEngine::ArgumentKind AK, intptr_t QT,
Craig Topper3aa4fb32014-06-12 05:32:35 +000049 StringRef Modifier, StringRef Argument,
Craig Toppere4753502014-06-12 05:32:27 +000050 ArrayRef<DiagnosticsEngine::ArgumentValue> PrevArgs,
51 SmallVectorImpl<char> &Output,
52 void *Cookie,
53 ArrayRef<intptr_t> QualTypeVals) {
54 StringRef Str = "<can't format argument>";
55 Output.append(Str.begin(), Str.end());
Chris Lattner6a2ed6f2008-11-23 09:13:29 +000056}
57
David Blaikie9c902b52011-09-25 23:23:43 +000058DiagnosticsEngine::DiagnosticsEngine(
Alexander Kornienko41c247a2014-11-17 23:46:02 +000059 const IntrusiveRefCntPtr<DiagnosticIDs> &diags, DiagnosticOptions *DiagOpts,
60 DiagnosticConsumer *client, bool ShouldOwnClient)
61 : Diags(diags), DiagOpts(DiagOpts), Client(nullptr), SourceMgr(nullptr) {
62 setClient(client, ShouldOwnClient);
Chris Lattner63ecc502008-11-23 09:21:17 +000063 ArgToStringFn = DummyArgToStringFn;
Craig Topperf1186c52014-05-08 06:41:40 +000064 ArgToStringCookie = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +000065
Douglas Gregor0e119552010-07-31 00:40:00 +000066 AllExtensionsSilenced = 0;
67 IgnoreAllWarnings = false;
68 WarningsAsErrors = false;
Ted Kremenekfbbdced2011-08-18 01:12:56 +000069 EnableAllWarnings = false;
Douglas Gregor0e119552010-07-31 00:40:00 +000070 ErrorsAsFatal = false;
71 SuppressSystemWarnings = false;
72 SuppressAllDiagnostics = false;
Richard Trieu91844232012-06-26 18:18:47 +000073 ElideType = true;
74 PrintTemplateTree = false;
75 ShowColors = false;
Douglas Gregor0e119552010-07-31 00:40:00 +000076 ShowOverloads = Ovl_All;
Alp Tokerac4e8e52014-06-22 21:58:33 +000077 ExtBehavior = diag::Severity::Ignored;
Douglas Gregor0e119552010-07-31 00:40:00 +000078
79 ErrorLimit = 0;
80 TemplateBacktraceLimit = 0;
Richard Smithf6f003a2011-12-16 19:06:07 +000081 ConstexprBacktraceLimit = 0;
Douglas Gregor0e119552010-07-31 00:40:00 +000082
Douglas Gregoraa21cc42010-07-19 21:46:24 +000083 Reset();
Chris Lattnerae411572006-07-05 00:55:08 +000084}
85
Reid Klecknerdccbabf2014-12-17 20:23:11 +000086DiagnosticsEngine::~DiagnosticsEngine() {
87 // If we own the diagnostic client, destroy it first so that it can access the
88 // engine from its destructor.
89 setClient(nullptr);
90}
91
David Blaikiee2eefae2011-09-25 23:39:51 +000092void DiagnosticsEngine::setClient(DiagnosticConsumer *client,
David Blaikie9c902b52011-09-25 23:23:43 +000093 bool ShouldOwnClient) {
Alexander Kornienko41c247a2014-11-17 23:46:02 +000094 Owner.reset(ShouldOwnClient ? client : nullptr);
Douglas Gregor7a964ad2011-01-31 22:04:05 +000095 Client = client;
Douglas Gregor7a964ad2011-01-31 22:04:05 +000096}
Chris Lattnerfb42a182009-07-12 21:18:45 +000097
David Blaikie9c902b52011-09-25 23:23:43 +000098void DiagnosticsEngine::pushMappings(SourceLocation Loc) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +000099 DiagStateOnPushStack.push_back(GetCurDiagState());
Chris Lattnerfb42a182009-07-12 21:18:45 +0000100}
101
David Blaikie9c902b52011-09-25 23:23:43 +0000102bool DiagnosticsEngine::popMappings(SourceLocation Loc) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000103 if (DiagStateOnPushStack.empty())
Chris Lattnerfb42a182009-07-12 21:18:45 +0000104 return false;
105
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000106 if (DiagStateOnPushStack.back() != GetCurDiagState()) {
107 // State changed at some point between push/pop.
108 PushDiagStatePoint(DiagStateOnPushStack.back(), Loc);
109 }
110 DiagStateOnPushStack.pop_back();
Chris Lattnerfb42a182009-07-12 21:18:45 +0000111 return true;
112}
113
David Blaikie9c902b52011-09-25 23:23:43 +0000114void DiagnosticsEngine::Reset() {
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000115 ErrorOccurred = false;
DeLesley Hutchins8ecd4912012-12-07 22:53:48 +0000116 UncompilableErrorOccurred = false;
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000117 FatalErrorOccurred = false;
Douglas Gregor8a60bbe2011-07-06 17:40:26 +0000118 UnrecoverableErrorOccurred = false;
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000119
120 NumWarnings = 0;
121 NumErrors = 0;
Argyrios Kyrtzidis1fa8b4b2011-07-29 01:25:44 +0000122 TrapNumErrorsOccurred = 0;
123 TrapNumUnrecoverableErrorsOccurred = 0;
Douglas Gregor8a60bbe2011-07-06 17:40:26 +0000124
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000125 CurDiagID = ~0U;
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000126 LastDiagLevel = DiagnosticIDs::Ignored;
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000127 DelayedDiagID = 0;
Argyrios Kyrtzidisbbbeea12011-03-26 18:58:17 +0000128
129 // Clear state related to #pragma diagnostic.
130 DiagStates.clear();
131 DiagStatePoints.clear();
132 DiagStateOnPushStack.clear();
133
134 // Create a DiagState and DiagStatePoint representing diagnostic changes
135 // through command-line.
Benjamin Kramer3204b152015-05-29 19:42:19 +0000136 DiagStates.emplace_back();
Richard Smithf995f2c2012-08-14 04:19:29 +0000137 DiagStatePoints.push_back(DiagStatePoint(&DiagStates.back(), FullSourceLoc()));
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000138}
Chris Lattner22eb9722006-06-18 05:43:12 +0000139
David Blaikie9c902b52011-09-25 23:23:43 +0000140void DiagnosticsEngine::SetDelayedDiagnostic(unsigned DiagID, StringRef Arg1,
Chad Rosier849a67b2012-02-07 23:24:49 +0000141 StringRef Arg2) {
Douglas Gregor85795312010-03-22 15:10:57 +0000142 if (DelayedDiagID)
143 return;
144
145 DelayedDiagID = DiagID;
Douglas Gregor96380982010-03-22 15:47:45 +0000146 DelayedDiagArg1 = Arg1.str();
147 DelayedDiagArg2 = Arg2.str();
Douglas Gregor85795312010-03-22 15:10:57 +0000148}
149
David Blaikie9c902b52011-09-25 23:23:43 +0000150void DiagnosticsEngine::ReportDelayed() {
Douglas Gregor85795312010-03-22 15:10:57 +0000151 Report(DelayedDiagID) << DelayedDiagArg1 << DelayedDiagArg2;
152 DelayedDiagID = 0;
153 DelayedDiagArg1.clear();
154 DelayedDiagArg2.clear();
155}
156
David Blaikie9c902b52011-09-25 23:23:43 +0000157DiagnosticsEngine::DiagStatePointsTy::iterator
158DiagnosticsEngine::GetDiagStatePointForLoc(SourceLocation L) const {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000159 assert(!DiagStatePoints.empty());
160 assert(DiagStatePoints.front().Loc.isInvalid() &&
161 "Should have created a DiagStatePoint for command-line");
162
Richard Smith99eff012012-08-17 00:55:32 +0000163 if (!SourceMgr)
164 return DiagStatePoints.end() - 1;
165
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000166 FullSourceLoc Loc(L, *SourceMgr);
167 if (Loc.isInvalid())
168 return DiagStatePoints.end() - 1;
169
170 DiagStatePointsTy::iterator Pos = DiagStatePoints.end();
171 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
172 if (LastStateChangePos.isValid() &&
173 Loc.isBeforeInTranslationUnitThan(LastStateChangePos))
174 Pos = std::upper_bound(DiagStatePoints.begin(), DiagStatePoints.end(),
Craig Topperf1186c52014-05-08 06:41:40 +0000175 DiagStatePoint(nullptr, Loc));
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000176 --Pos;
177 return Pos;
178}
179
Alp Tokerd576e002014-06-12 11:13:52 +0000180void DiagnosticsEngine::setSeverity(diag::kind Diag, diag::Severity Map,
181 SourceLocation L) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000182 assert(Diag < diag::DIAG_UPPER_LIMIT &&
183 "Can only map builtin diagnostics");
184 assert((Diags->isBuiltinWarningOrExtension(Diag) ||
Alp Toker46df1c02014-06-12 10:15:20 +0000185 (Map == diag::Severity::Fatal || Map == diag::Severity::Error)) &&
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000186 "Cannot map errors into warnings!");
187 assert(!DiagStatePoints.empty());
Richard Smith8a0527d2012-08-14 22:37:22 +0000188 assert((L.isInvalid() || SourceMgr) && "No SourceMgr for valid location");
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000189
Richard Smith8a0527d2012-08-14 22:37:22 +0000190 FullSourceLoc Loc = SourceMgr? FullSourceLoc(L, *SourceMgr) : FullSourceLoc();
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000191 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
Chad Rosierd1956e42012-02-03 01:49:51 +0000192 // Don't allow a mapping to a warning override an error/fatal mapping.
Alp Toker46df1c02014-06-12 10:15:20 +0000193 if (Map == diag::Severity::Warning) {
Alp Tokerc726c362014-06-10 09:31:37 +0000194 DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag);
Alp Toker46df1c02014-06-12 10:15:20 +0000195 if (Info.getSeverity() == diag::Severity::Error ||
196 Info.getSeverity() == diag::Severity::Fatal)
Alp Tokerc726c362014-06-10 09:31:37 +0000197 Map = Info.getSeverity();
Chad Rosierd1956e42012-02-03 01:49:51 +0000198 }
Alp Tokerc726c362014-06-10 09:31:37 +0000199 DiagnosticMapping Mapping = makeUserMapping(Map, L);
Daniel Dunbar2fba0972011-10-04 21:17:24 +0000200
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000201 // Common case; setting all the diagnostics of a group in one place.
202 if (Loc.isInvalid() || Loc == LastStateChangePos) {
Alp Tokerc726c362014-06-10 09:31:37 +0000203 GetCurDiagState()->setMapping(Diag, Mapping);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000204 return;
205 }
206
207 // Another common case; modifying diagnostic state in a source location
208 // after the previous one.
209 if ((Loc.isValid() && LastStateChangePos.isInvalid()) ||
210 LastStateChangePos.isBeforeInTranslationUnitThan(Loc)) {
Chris Lattner57540c52011-04-15 05:22:18 +0000211 // A diagnostic pragma occurred, create a new DiagState initialized with
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000212 // the current one and a new DiagStatePoint to record at which location
213 // the new state became active.
214 DiagStates.push_back(*GetCurDiagState());
215 PushDiagStatePoint(&DiagStates.back(), Loc);
Alp Tokerc726c362014-06-10 09:31:37 +0000216 GetCurDiagState()->setMapping(Diag, Mapping);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000217 return;
218 }
219
220 // We allow setting the diagnostic state in random source order for
221 // completeness but it should not be actually happening in normal practice.
222
223 DiagStatePointsTy::iterator Pos = GetDiagStatePointForLoc(Loc);
224 assert(Pos != DiagStatePoints.end());
225
226 // Update all diagnostic states that are active after the given location.
227 for (DiagStatePointsTy::iterator
228 I = Pos+1, E = DiagStatePoints.end(); I != E; ++I) {
Craig Topper5e35e662015-11-26 05:51:54 +0000229 I->State->setMapping(Diag, Mapping);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000230 }
231
232 // If the location corresponds to an existing point, just update its state.
233 if (Pos->Loc == Loc) {
Craig Topper5e35e662015-11-26 05:51:54 +0000234 Pos->State->setMapping(Diag, Mapping);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000235 return;
236 }
237
238 // Create a new state/point and fit it into the vector of DiagStatePoints
239 // so that the vector is always ordered according to location.
Alp Toker14c8aff2014-01-26 08:12:32 +0000240 assert(Pos->Loc.isBeforeInTranslationUnitThan(Loc));
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000241 DiagStates.push_back(*Pos->State);
242 DiagState *NewState = &DiagStates.back();
Craig Topper5e35e662015-11-26 05:51:54 +0000243 NewState->setMapping(Diag, Mapping);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000244 DiagStatePoints.insert(Pos+1, DiagStatePoint(NewState,
245 FullSourceLoc(Loc, *SourceMgr)));
246}
247
Richard Smith3be1cb22014-08-07 00:24:21 +0000248bool DiagnosticsEngine::setSeverityForGroup(diag::Flavor Flavor,
249 StringRef Group, diag::Severity Map,
Alp Tokerd576e002014-06-12 11:13:52 +0000250 SourceLocation Loc) {
Daniel Dunbard908c122011-09-29 01:47:16 +0000251 // Get the diagnostics in this group.
Hans Wennborgeb7cd662014-08-11 16:05:54 +0000252 SmallVector<diag::kind, 256> GroupDiags;
Richard Smith3be1cb22014-08-07 00:24:21 +0000253 if (Diags->getDiagnosticsInGroup(Flavor, Group, GroupDiags))
Daniel Dunbard908c122011-09-29 01:47:16 +0000254 return true;
255
256 // Set the mapping.
Hans Wennborgeb7cd662014-08-11 16:05:54 +0000257 for (diag::kind Diag : GroupDiags)
258 setSeverity(Diag, Map, Loc);
Daniel Dunbard908c122011-09-29 01:47:16 +0000259
260 return false;
261}
262
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000263bool DiagnosticsEngine::setDiagnosticGroupWarningAsError(StringRef Group,
264 bool Enabled) {
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000265 // If we are enabling this feature, just set the diagnostic mappings to map to
266 // errors.
267 if (Enabled)
Richard Smith3be1cb22014-08-07 00:24:21 +0000268 return setSeverityForGroup(diag::Flavor::WarningOrError, Group,
269 diag::Severity::Error);
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000270
271 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
272 // potentially downgrade anything already mapped to be a warning.
273
274 // Get the diagnostics in this group.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000275 SmallVector<diag::kind, 8> GroupDiags;
Richard Smith3be1cb22014-08-07 00:24:21 +0000276 if (Diags->getDiagnosticsInGroup(diag::Flavor::WarningOrError, Group,
277 GroupDiags))
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000278 return true;
279
280 // Perform the mapping change.
Craig Toppera52e2b22015-11-26 05:10:07 +0000281 for (diag::kind Diag : GroupDiags) {
282 DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag);
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000283
Alp Toker46df1c02014-06-12 10:15:20 +0000284 if (Info.getSeverity() == diag::Severity::Error ||
285 Info.getSeverity() == diag::Severity::Fatal)
286 Info.setSeverity(diag::Severity::Warning);
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000287
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000288 Info.setNoWarningAsError(true);
289 }
290
291 return false;
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000292}
293
294bool DiagnosticsEngine::setDiagnosticGroupErrorAsFatal(StringRef Group,
295 bool Enabled) {
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000296 // If we are enabling this feature, just set the diagnostic mappings to map to
297 // fatal errors.
298 if (Enabled)
Richard Smith3be1cb22014-08-07 00:24:21 +0000299 return setSeverityForGroup(diag::Flavor::WarningOrError, Group,
300 diag::Severity::Fatal);
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000301
302 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
303 // potentially downgrade anything already mapped to be an error.
304
305 // Get the diagnostics in this group.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000306 SmallVector<diag::kind, 8> GroupDiags;
Richard Smith3be1cb22014-08-07 00:24:21 +0000307 if (Diags->getDiagnosticsInGroup(diag::Flavor::WarningOrError, Group,
308 GroupDiags))
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000309 return true;
310
311 // Perform the mapping change.
Craig Toppera52e2b22015-11-26 05:10:07 +0000312 for (diag::kind Diag : GroupDiags) {
313 DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag);
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000314
Alp Toker46df1c02014-06-12 10:15:20 +0000315 if (Info.getSeverity() == diag::Severity::Fatal)
316 Info.setSeverity(diag::Severity::Error);
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000317
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000318 Info.setNoErrorAsFatal(true);
319 }
320
321 return false;
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000322}
323
Richard Smith3be1cb22014-08-07 00:24:21 +0000324void DiagnosticsEngine::setSeverityForAll(diag::Flavor Flavor,
325 diag::Severity Map,
Alp Tokerd576e002014-06-12 11:13:52 +0000326 SourceLocation Loc) {
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000327 // Get all the diagnostics.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000328 SmallVector<diag::kind, 64> AllDiags;
Richard Smith3be1cb22014-08-07 00:24:21 +0000329 Diags->getAllDiagnostics(Flavor, AllDiags);
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000330
331 // Set the mapping.
Craig Toppera52e2b22015-11-26 05:10:07 +0000332 for (diag::kind Diag : AllDiags)
333 if (Diags->isBuiltinWarningOrExtension(Diag))
334 setSeverity(Diag, Map, Loc);
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000335}
336
David Blaikie9c902b52011-09-25 23:23:43 +0000337void DiagnosticsEngine::Report(const StoredDiagnostic &storedDiag) {
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000338 assert(CurDiagID == ~0U && "Multiple diagnostics in flight at once!");
339
340 CurDiagLoc = storedDiag.getLocation();
341 CurDiagID = storedDiag.getID();
342 NumDiagArgs = 0;
343
Alexander Kornienkod3b4e082014-05-22 19:56:11 +0000344 DiagRanges.clear();
Benjamin Kramerf367dd92015-06-12 15:31:50 +0000345 DiagRanges.append(storedDiag.range_begin(), storedDiag.range_end());
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000346
Alexander Kornienkod3b4e082014-05-22 19:56:11 +0000347 DiagFixItHints.clear();
Benjamin Kramerf367dd92015-06-12 15:31:50 +0000348 DiagFixItHints.append(storedDiag.fixit_begin(), storedDiag.fixit_end());
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000349
David Blaikiee2eefae2011-09-25 23:39:51 +0000350 assert(Client && "DiagnosticConsumer not set!");
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000351 Level DiagLevel = storedDiag.getLevel();
David Blaikieb5784322011-09-26 01:18:08 +0000352 Diagnostic Info(this, storedDiag.getMessage());
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000353 Client->HandleDiagnostic(DiagLevel, Info);
354 if (Client->IncludeInDiagnosticCounts()) {
David Blaikie9c902b52011-09-25 23:23:43 +0000355 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000356 ++NumWarnings;
357 }
358
359 CurDiagID = ~0U;
360}
361
Jordan Rose6f524ac2012-07-11 16:50:36 +0000362bool DiagnosticsEngine::EmitCurrentDiagnostic(bool Force) {
363 assert(getClient() && "DiagnosticClient not set!");
364
365 bool Emitted;
366 if (Force) {
367 Diagnostic Info(this);
368
369 // Figure out the diagnostic level of this message.
370 DiagnosticIDs::Level DiagLevel
371 = Diags->getDiagnosticLevel(Info.getID(), Info.getLocation(), *this);
372
373 Emitted = (DiagLevel != DiagnosticIDs::Ignored);
374 if (Emitted) {
375 // Emit the diagnostic regardless of suppression level.
376 Diags->EmitDiag(*this, DiagLevel);
377 }
378 } else {
379 // Process the diagnostic, sending the accumulated information to the
380 // DiagnosticConsumer.
381 Emitted = ProcessDiag();
382 }
Douglas Gregor85795312010-03-22 15:10:57 +0000383
384 // Clear out the current diagnostic object.
Daniel Dunbarc7c00892012-03-13 21:02:14 +0000385 unsigned DiagID = CurDiagID;
386 Clear();
Douglas Gregor85795312010-03-22 15:10:57 +0000387
388 // If there was a delayed diagnostic, emit it now.
Jordan Rose6f524ac2012-07-11 16:50:36 +0000389 if (!Force && DelayedDiagID && DelayedDiagID != DiagID)
Daniel Dunbarc7c00892012-03-13 21:02:14 +0000390 ReportDelayed();
Douglas Gregor85795312010-03-22 15:10:57 +0000391
392 return Emitted;
393}
394
Nico Weber4c311642008-08-10 19:59:06 +0000395
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000396DiagnosticConsumer::~DiagnosticConsumer() {}
Nico Weber4c311642008-08-10 19:59:06 +0000397
David Blaikiee2eefae2011-09-25 23:39:51 +0000398void DiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
David Blaikieb5784322011-09-26 01:18:08 +0000399 const Diagnostic &Info) {
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000400 if (!IncludeInDiagnosticCounts())
401 return;
402
David Blaikie9c902b52011-09-25 23:23:43 +0000403 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000404 ++NumWarnings;
David Blaikie9c902b52011-09-25 23:23:43 +0000405 else if (DiagLevel >= DiagnosticsEngine::Error)
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000406 ++NumErrors;
407}
Chris Lattner23be0672008-11-19 06:51:40 +0000408
Chris Lattner2b786902008-11-21 07:50:02 +0000409/// ModifierIs - Return true if the specified modifier matches specified string.
410template <std::size_t StrLen>
411static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
412 const char (&Str)[StrLen]) {
413 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
414}
415
John McCall8cb7a8a32010-01-14 20:11:39 +0000416/// ScanForward - Scans forward, looking for the given character, skipping
417/// nested clauses and escaped characters.
418static const char *ScanFormat(const char *I, const char *E, char Target) {
419 unsigned Depth = 0;
420
421 for ( ; I != E; ++I) {
422 if (Depth == 0 && *I == Target) return I;
423 if (Depth != 0 && *I == '}') Depth--;
424
425 if (*I == '%') {
426 I++;
427 if (I == E) break;
428
429 // Escaped characters get implicitly skipped here.
430
431 // Format specifier.
Jordan Rosea7d03842013-02-08 22:30:41 +0000432 if (!isDigit(*I) && !isPunctuation(*I)) {
433 for (I++; I != E && !isDigit(*I) && *I != '{'; I++) ;
John McCall8cb7a8a32010-01-14 20:11:39 +0000434 if (I == E) break;
435 if (*I == '{')
436 Depth++;
437 }
438 }
439 }
440 return E;
441}
442
Chris Lattner2b786902008-11-21 07:50:02 +0000443/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
444/// like this: %select{foo|bar|baz}2. This means that the integer argument
445/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
446/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
447/// This is very useful for certain classes of variant diagnostics.
David Blaikieb5784322011-09-26 01:18:08 +0000448static void HandleSelectModifier(const Diagnostic &DInfo, unsigned ValNo,
Chris Lattner2b786902008-11-21 07:50:02 +0000449 const char *Argument, unsigned ArgumentLen,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000450 SmallVectorImpl<char> &OutStr) {
Chris Lattner2b786902008-11-21 07:50:02 +0000451 const char *ArgumentEnd = Argument+ArgumentLen;
Mike Stump11289f42009-09-09 15:08:12 +0000452
Chris Lattner2b786902008-11-21 07:50:02 +0000453 // Skip over 'ValNo' |'s.
454 while (ValNo) {
John McCall8cb7a8a32010-01-14 20:11:39 +0000455 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
Chris Lattner2b786902008-11-21 07:50:02 +0000456 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
457 " larger than the number of options in the diagnostic string!");
458 Argument = NextVal+1; // Skip this string.
459 --ValNo;
460 }
Mike Stump11289f42009-09-09 15:08:12 +0000461
Chris Lattner2b786902008-11-21 07:50:02 +0000462 // Get the end of the value. This is either the } or the |.
John McCall8cb7a8a32010-01-14 20:11:39 +0000463 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
John McCalle4d54322010-01-13 23:58:20 +0000464
465 // Recursively format the result of the select clause into the output string.
466 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000467}
468
469/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
470/// letter 's' to the string if the value is not 1. This is used in cases like
471/// this: "you idiot, you have %4 parameter%s4!".
472static void HandleIntegerSModifier(unsigned ValNo,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000473 SmallVectorImpl<char> &OutStr) {
Chris Lattner2b786902008-11-21 07:50:02 +0000474 if (ValNo != 1)
475 OutStr.push_back('s');
476}
477
John McCall9015cde2010-01-14 00:50:32 +0000478/// HandleOrdinalModifier - Handle the integer 'ord' modifier. This
479/// prints the ordinal form of the given integer, with 1 corresponding
480/// to the first ordinal. Currently this is hard-coded to use the
481/// English form.
482static void HandleOrdinalModifier(unsigned ValNo,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000483 SmallVectorImpl<char> &OutStr) {
John McCall9015cde2010-01-14 00:50:32 +0000484 assert(ValNo != 0 && "ValNo must be strictly positive!");
485
486 llvm::raw_svector_ostream Out(OutStr);
487
488 // We could use text forms for the first N ordinals, but the numeric
489 // forms are actually nicer in diagnostics because they stand out.
Jordan Rosec102b352012-09-22 01:24:42 +0000490 Out << ValNo << llvm::getOrdinalSuffix(ValNo);
John McCall9015cde2010-01-14 00:50:32 +0000491}
492
Chris Lattner2b786902008-11-21 07:50:02 +0000493
Sebastian Redl15b02d22008-11-22 13:44:36 +0000494/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000495static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000496 // Programming 101: Parse a decimal number :-)
497 unsigned Val = 0;
498 while (Start != End && *Start >= '0' && *Start <= '9') {
499 Val *= 10;
500 Val += *Start - '0';
501 ++Start;
502 }
503 return Val;
504}
505
506/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000507static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000508 if (*Start != '[') {
509 unsigned Ref = PluralNumber(Start, End);
510 return Ref == Val;
511 }
512
513 ++Start;
514 unsigned Low = PluralNumber(Start, End);
515 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
516 ++Start;
517 unsigned High = PluralNumber(Start, End);
518 assert(*Start == ']' && "Bad plural expression syntax: expected )");
519 ++Start;
520 return Low <= Val && Val <= High;
521}
522
523/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattner2fe29202009-04-15 17:13:42 +0000524static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000525 // Empty condition?
526 if (*Start == ':')
527 return true;
528
529 while (1) {
530 char C = *Start;
531 if (C == '%') {
532 // Modulo expression
533 ++Start;
534 unsigned Arg = PluralNumber(Start, End);
535 assert(*Start == '=' && "Bad plural expression syntax: expected =");
536 ++Start;
537 unsigned ValMod = ValNo % Arg;
538 if (TestPluralRange(ValMod, Start, End))
539 return true;
540 } else {
Sebastian Redl3ceaf622008-11-27 07:28:14 +0000541 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redl15b02d22008-11-22 13:44:36 +0000542 "Bad plural expression syntax: unexpected character");
543 // Range expression
544 if (TestPluralRange(ValNo, Start, End))
545 return true;
546 }
547
548 // Scan for next or-expr part.
549 Start = std::find(Start, End, ',');
Mike Stump11289f42009-09-09 15:08:12 +0000550 if (Start == End)
Sebastian Redl15b02d22008-11-22 13:44:36 +0000551 break;
552 ++Start;
553 }
554 return false;
555}
556
557/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
558/// for complex plural forms, or in languages where all plurals are complex.
559/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
560/// conditions that are tested in order, the form corresponding to the first
561/// that applies being emitted. The empty condition is always true, making the
562/// last form a default case.
563/// Conditions are simple boolean expressions, where n is the number argument.
564/// Here are the rules.
565/// condition := expression | empty
566/// empty := -> always true
567/// expression := numeric [',' expression] -> logical or
568/// numeric := range -> true if n in range
569/// | '%' number '=' range -> true if n % number in range
570/// range := number
571/// | '[' number ',' number ']' -> ranges are inclusive both ends
572///
573/// Here are some examples from the GNU gettext manual written in this form:
574/// English:
575/// {1:form0|:form1}
576/// Latvian:
577/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
578/// Gaeilge:
579/// {1:form0|2:form1|:form2}
580/// Romanian:
581/// {1:form0|0,%100=[1,19]:form1|:form2}
582/// Lithuanian:
583/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
584/// Russian (requires repeated form):
585/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
586/// Slovak
587/// {1:form0|[2,4]:form1|:form2}
588/// Polish (requires repeated form):
589/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
David Blaikieb5784322011-09-26 01:18:08 +0000590static void HandlePluralModifier(const Diagnostic &DInfo, unsigned ValNo,
Sebastian Redl15b02d22008-11-22 13:44:36 +0000591 const char *Argument, unsigned ArgumentLen,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000592 SmallVectorImpl<char> &OutStr) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000593 const char *ArgumentEnd = Argument + ArgumentLen;
594 while (1) {
595 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
596 const char *ExprEnd = Argument;
597 while (*ExprEnd != ':') {
598 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
599 ++ExprEnd;
600 }
601 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
602 Argument = ExprEnd + 1;
John McCall8cb7a8a32010-01-14 20:11:39 +0000603 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
John McCall43b61682010-10-14 01:55:31 +0000604
605 // Recursively format the result of the plural clause into the
606 // output string.
607 DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000608 return;
609 }
John McCall8cb7a8a32010-01-14 20:11:39 +0000610 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
Sebastian Redl15b02d22008-11-22 13:44:36 +0000611 }
612}
613
Alp Tokera231ad22014-01-06 12:54:18 +0000614/// \brief Returns the friendly description for a token kind that will appear
615/// without quotes in diagnostic messages. These strings may be translatable in
616/// future.
617static const char *getTokenDescForDiagnostic(tok::TokenKind Kind) {
Alp Tokerec543272013-12-24 09:48:30 +0000618 switch (Kind) {
619 case tok::identifier:
620 return "identifier";
621 default:
Craig Topperf1186c52014-05-08 06:41:40 +0000622 return nullptr;
Alp Tokerec543272013-12-24 09:48:30 +0000623 }
624}
Sebastian Redl15b02d22008-11-22 13:44:36 +0000625
Chris Lattner23be0672008-11-19 06:51:40 +0000626/// FormatDiagnostic - Format this diagnostic into a string, substituting the
627/// formal arguments into the %0 slots. The result is appended onto the Str
628/// array.
David Blaikieb5784322011-09-26 01:18:08 +0000629void Diagnostic::
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000630FormatDiagnostic(SmallVectorImpl<char> &OutStr) const {
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000631 if (!StoredDiagMessage.empty()) {
632 OutStr.append(StoredDiagMessage.begin(), StoredDiagMessage.end());
633 return;
634 }
635
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000636 StringRef Diag =
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000637 getDiags()->getDiagnosticIDs()->getDescription(getID());
Mike Stump11289f42009-09-09 15:08:12 +0000638
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000639 FormatDiagnostic(Diag.begin(), Diag.end(), OutStr);
John McCalle4d54322010-01-13 23:58:20 +0000640}
641
David Blaikieb5784322011-09-26 01:18:08 +0000642void Diagnostic::
John McCalle4d54322010-01-13 23:58:20 +0000643FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000644 SmallVectorImpl<char> &OutStr) const {
John McCalle4d54322010-01-13 23:58:20 +0000645
Richard Trieub3b8bb02015-01-08 01:27:03 +0000646 // When the diagnostic string is only "%0", the entire string is being given
647 // by an outside source. Remove unprintable characters from this string
648 // and skip all the other string processing.
Richard Trieudcd7bb02015-01-17 00:56:10 +0000649 if (DiagEnd - DiagStr == 2 &&
650 StringRef(DiagStr, DiagEnd - DiagStr).equals("%0") &&
Richard Trieub3b8bb02015-01-08 01:27:03 +0000651 getArgKind(0) == DiagnosticsEngine::ak_std_string) {
652 const std::string &S = getArgStdStr(0);
653 for (char c : S) {
654 if (llvm::sys::locale::isPrint(c) || c == '\t') {
655 OutStr.push_back(c);
656 }
657 }
658 return;
659 }
660
Chris Lattnerc243f292009-10-20 05:25:22 +0000661 /// FormattedArgs - Keep track of all of the arguments formatted by
662 /// ConvertArgToString and pass them into subsequent calls to
663 /// ConvertArgToString, allowing the implementation to avoid redundancies in
664 /// obvious cases.
David Blaikie9c902b52011-09-25 23:23:43 +0000665 SmallVector<DiagnosticsEngine::ArgumentValue, 8> FormattedArgs;
Chandler Carruthd5173952011-07-11 17:49:21 +0000666
667 /// QualTypeVals - Pass a vector of arrays so that QualType names can be
668 /// compared to see if more information is needed to be printed.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000669 SmallVector<intptr_t, 2> QualTypeVals;
Richard Trieu91844232012-06-26 18:18:47 +0000670 SmallVector<char, 64> Tree;
671
Chandler Carruthd5173952011-07-11 17:49:21 +0000672 for (unsigned i = 0, e = getNumArgs(); i < e; ++i)
David Blaikie9c902b52011-09-25 23:23:43 +0000673 if (getArgKind(i) == DiagnosticsEngine::ak_qualtype)
Chandler Carruthd5173952011-07-11 17:49:21 +0000674 QualTypeVals.push_back(getRawArg(i));
675
Chris Lattner23be0672008-11-19 06:51:40 +0000676 while (DiagStr != DiagEnd) {
677 if (DiagStr[0] != '%') {
678 // Append non-%0 substrings to Str if we have one.
679 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
680 OutStr.append(DiagStr, StrEnd);
681 DiagStr = StrEnd;
Chris Lattner2b786902008-11-21 07:50:02 +0000682 continue;
Jordan Rosea7d03842013-02-08 22:30:41 +0000683 } else if (isPunctuation(DiagStr[1])) {
John McCall8cb7a8a32010-01-14 20:11:39 +0000684 OutStr.push_back(DiagStr[1]); // %% -> %.
Chris Lattner23be0672008-11-19 06:51:40 +0000685 DiagStr += 2;
Chris Lattner2b786902008-11-21 07:50:02 +0000686 continue;
687 }
Mike Stump11289f42009-09-09 15:08:12 +0000688
Chris Lattner2b786902008-11-21 07:50:02 +0000689 // Skip the %.
690 ++DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000691
Chris Lattner2b786902008-11-21 07:50:02 +0000692 // This must be a placeholder for a diagnostic argument. The format for a
693 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
694 // The digit is a number from 0-9 indicating which argument this comes from.
695 // The modifier is a string of digits from the set [-a-z]+, arguments is a
696 // brace enclosed string.
Craig Topperf1186c52014-05-08 06:41:40 +0000697 const char *Modifier = nullptr, *Argument = nullptr;
Chris Lattner2b786902008-11-21 07:50:02 +0000698 unsigned ModifierLen = 0, ArgumentLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000699
Chris Lattner2b786902008-11-21 07:50:02 +0000700 // Check to see if we have a modifier. If so eat it.
Jordan Rosea7d03842013-02-08 22:30:41 +0000701 if (!isDigit(DiagStr[0])) {
Chris Lattner2b786902008-11-21 07:50:02 +0000702 Modifier = DiagStr;
703 while (DiagStr[0] == '-' ||
704 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
705 ++DiagStr;
706 ModifierLen = DiagStr-Modifier;
Chris Lattner23be0672008-11-19 06:51:40 +0000707
Chris Lattner2b786902008-11-21 07:50:02 +0000708 // If we have an argument, get it next.
709 if (DiagStr[0] == '{') {
710 ++DiagStr; // Skip {.
711 Argument = DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000712
John McCall8cb7a8a32010-01-14 20:11:39 +0000713 DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
714 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
Chris Lattner2b786902008-11-21 07:50:02 +0000715 ArgumentLen = DiagStr-Argument;
716 ++DiagStr; // Skip }.
Chris Lattner23be0672008-11-19 06:51:40 +0000717 }
Chris Lattner2b786902008-11-21 07:50:02 +0000718 }
Mike Stump11289f42009-09-09 15:08:12 +0000719
Jordan Rosea7d03842013-02-08 22:30:41 +0000720 assert(isDigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000721 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattner2b786902008-11-21 07:50:02 +0000722
Richard Trieu91844232012-06-26 18:18:47 +0000723 // Only used for type diffing.
724 unsigned ArgNo2 = ArgNo;
725
David Blaikie9c902b52011-09-25 23:23:43 +0000726 DiagnosticsEngine::ArgumentKind Kind = getArgKind(ArgNo);
Richard Trieu90c31f52013-01-30 20:04:31 +0000727 if (ModifierIs(Modifier, ModifierLen, "diff")) {
Jordan Rosea7d03842013-02-08 22:30:41 +0000728 assert(*DiagStr == ',' && isDigit(*(DiagStr + 1)) &&
Richard Trieu91844232012-06-26 18:18:47 +0000729 "Invalid format for diff modifier");
730 ++DiagStr; // Comma.
731 ArgNo2 = *DiagStr++ - '0';
Richard Trieu90c31f52013-01-30 20:04:31 +0000732 DiagnosticsEngine::ArgumentKind Kind2 = getArgKind(ArgNo2);
733 if (Kind == DiagnosticsEngine::ak_qualtype &&
734 Kind2 == DiagnosticsEngine::ak_qualtype)
735 Kind = DiagnosticsEngine::ak_qualtype_pair;
736 else {
737 // %diff only supports QualTypes. For other kinds of arguments,
738 // use the default printing. For example, if the modifier is:
739 // "%diff{compare $ to $|other text}1,2"
740 // treat it as:
741 // "compare %1 to %2"
742 const char *Pipe = ScanFormat(Argument, Argument + ArgumentLen, '|');
743 const char *FirstDollar = ScanFormat(Argument, Pipe, '$');
744 const char *SecondDollar = ScanFormat(FirstDollar + 1, Pipe, '$');
Filipe Cabecinhased4a00c2013-01-30 22:03:24 +0000745 const char ArgStr1[] = { '%', static_cast<char>('0' + ArgNo) };
746 const char ArgStr2[] = { '%', static_cast<char>('0' + ArgNo2) };
Richard Trieu90c31f52013-01-30 20:04:31 +0000747 FormatDiagnostic(Argument, FirstDollar, OutStr);
748 FormatDiagnostic(ArgStr1, ArgStr1 + 2, OutStr);
749 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
750 FormatDiagnostic(ArgStr2, ArgStr2 + 2, OutStr);
751 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
752 continue;
753 }
Richard Trieu91844232012-06-26 18:18:47 +0000754 }
Chris Lattnerc243f292009-10-20 05:25:22 +0000755
756 switch (Kind) {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000757 // ---- STRINGS ----
David Blaikie9c902b52011-09-25 23:23:43 +0000758 case DiagnosticsEngine::ak_std_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000759 const std::string &S = getArgStdStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000760 assert(ModifierLen == 0 && "No modifiers for strings yet");
761 OutStr.append(S.begin(), S.end());
762 break;
763 }
David Blaikie9c902b52011-09-25 23:23:43 +0000764 case DiagnosticsEngine::ak_c_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000765 const char *S = getArgCStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000766 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000767
768 // Don't crash if get passed a null pointer by accident.
769 if (!S)
770 S = "(null)";
Mike Stump11289f42009-09-09 15:08:12 +0000771
Chris Lattner2b786902008-11-21 07:50:02 +0000772 OutStr.append(S, S + strlen(S));
773 break;
774 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000775 // ---- INTEGERS ----
David Blaikie9c902b52011-09-25 23:23:43 +0000776 case DiagnosticsEngine::ak_sint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000777 int Val = getArgSInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000778
Chris Lattner2b786902008-11-21 07:50:02 +0000779 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCall43b61682010-10-14 01:55:31 +0000780 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen,
781 OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000782 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
783 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000784 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCall43b61682010-10-14 01:55:31 +0000785 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
786 OutStr);
John McCall9015cde2010-01-14 00:50:32 +0000787 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
788 HandleOrdinalModifier((unsigned)Val, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000789 } else {
790 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000791 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000792 }
Chris Lattner2b786902008-11-21 07:50:02 +0000793 break;
794 }
David Blaikie9c902b52011-09-25 23:23:43 +0000795 case DiagnosticsEngine::ak_uint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000796 unsigned Val = getArgUInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000797
Chris Lattner2b786902008-11-21 07:50:02 +0000798 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCalle4d54322010-01-13 23:58:20 +0000799 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000800 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
801 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000802 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCall43b61682010-10-14 01:55:31 +0000803 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
804 OutStr);
John McCall9015cde2010-01-14 00:50:32 +0000805 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
806 HandleOrdinalModifier(Val, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000807 } else {
808 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000809 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000810 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000811 break;
Chris Lattner2b786902008-11-21 07:50:02 +0000812 }
Alp Tokerec543272013-12-24 09:48:30 +0000813 // ---- TOKEN SPELLINGS ----
814 case DiagnosticsEngine::ak_tokenkind: {
815 tok::TokenKind Kind = static_cast<tok::TokenKind>(getRawArg(ArgNo));
816 assert(ModifierLen == 0 && "No modifiers for token kinds yet");
817
818 llvm::raw_svector_ostream Out(OutStr);
Alp Tokera231ad22014-01-06 12:54:18 +0000819 if (const char *S = tok::getPunctuatorSpelling(Kind))
820 // Quoted token spelling for punctuators.
821 Out << '\'' << S << '\'';
822 else if (const char *S = tok::getKeywordSpelling(Kind))
823 // Unquoted token spelling for keywords.
824 Out << S;
825 else if (const char *S = getTokenDescForDiagnostic(Kind))
Alp Tokerec543272013-12-24 09:48:30 +0000826 // Unquoted translatable token name.
827 Out << S;
Alp Tokerec543272013-12-24 09:48:30 +0000828 else if (const char *S = tok::getTokenName(Kind))
829 // Debug name, shouldn't appear in user-facing diagnostics.
830 Out << '<' << S << '>';
831 else
832 Out << "(null)";
833 break;
834 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000835 // ---- NAMES and TYPES ----
David Blaikie9c902b52011-09-25 23:23:43 +0000836 case DiagnosticsEngine::ak_identifierinfo: {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000837 const IdentifierInfo *II = getArgIdentifier(ArgNo);
838 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000839
840 // Don't crash if get passed a null pointer by accident.
841 if (!II) {
842 const char *S = "(null)";
843 OutStr.append(S, S + strlen(S));
844 continue;
845 }
846
Daniel Dunbar07d07852009-10-18 21:17:35 +0000847 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
Chris Lattnere3d20d92008-11-23 21:45:46 +0000848 break;
849 }
David Blaikie9c902b52011-09-25 23:23:43 +0000850 case DiagnosticsEngine::ak_qualtype:
851 case DiagnosticsEngine::ak_declarationname:
852 case DiagnosticsEngine::ak_nameddecl:
853 case DiagnosticsEngine::ak_nestednamespec:
854 case DiagnosticsEngine::ak_declcontext:
Aaron Ballman3e424b52013-12-26 18:30:57 +0000855 case DiagnosticsEngine::ak_attr:
Chris Lattnerc243f292009-10-20 05:25:22 +0000856 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
Craig Topper3aa4fb32014-06-12 05:32:35 +0000857 StringRef(Modifier, ModifierLen),
858 StringRef(Argument, ArgumentLen),
Craig Toppere4753502014-06-12 05:32:27 +0000859 FormattedArgs,
Chandler Carruthd5173952011-07-11 17:49:21 +0000860 OutStr, QualTypeVals);
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000861 break;
Richard Trieu91844232012-06-26 18:18:47 +0000862 case DiagnosticsEngine::ak_qualtype_pair:
863 // Create a struct with all the info needed for printing.
864 TemplateDiffTypes TDT;
865 TDT.FromType = getRawArg(ArgNo);
866 TDT.ToType = getRawArg(ArgNo2);
867 TDT.ElideType = getDiags()->ElideType;
868 TDT.ShowColors = getDiags()->ShowColors;
Richard Trieu50f5f462012-07-10 01:46:04 +0000869 TDT.TemplateDiffUsed = false;
Richard Trieu91844232012-06-26 18:18:47 +0000870 intptr_t val = reinterpret_cast<intptr_t>(&TDT);
871
Richard Trieuc6058442012-06-29 21:12:16 +0000872 const char *ArgumentEnd = Argument + ArgumentLen;
873 const char *Pipe = ScanFormat(Argument, ArgumentEnd, '|');
874
Richard Trieua4056002012-07-13 21:18:32 +0000875 // Print the tree. If this diagnostic already has a tree, skip the
876 // second tree.
877 if (getDiags()->PrintTemplateTree && Tree.empty()) {
Richard Trieu91844232012-06-26 18:18:47 +0000878 TDT.PrintFromType = true;
879 TDT.PrintTree = true;
880 getDiags()->ConvertArgToString(Kind, val,
Craig Topper3aa4fb32014-06-12 05:32:35 +0000881 StringRef(Modifier, ModifierLen),
882 StringRef(Argument, ArgumentLen),
Craig Toppere4753502014-06-12 05:32:27 +0000883 FormattedArgs,
Richard Trieu91844232012-06-26 18:18:47 +0000884 Tree, QualTypeVals);
885 // If there is no tree information, fall back to regular printing.
Richard Trieuc6058442012-06-29 21:12:16 +0000886 if (!Tree.empty()) {
887 FormatDiagnostic(Pipe + 1, ArgumentEnd, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +0000888 break;
Richard Trieuc6058442012-06-29 21:12:16 +0000889 }
Richard Trieu91844232012-06-26 18:18:47 +0000890 }
891
892 // Non-tree printing, also the fall-back when tree printing fails.
893 // The fall-back is triggered when the types compared are not templates.
Richard Trieuc6058442012-06-29 21:12:16 +0000894 const char *FirstDollar = ScanFormat(Argument, ArgumentEnd, '$');
895 const char *SecondDollar = ScanFormat(FirstDollar + 1, ArgumentEnd, '$');
Richard Trieu91844232012-06-26 18:18:47 +0000896
897 // Append before text
Richard Trieuc6058442012-06-29 21:12:16 +0000898 FormatDiagnostic(Argument, FirstDollar, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +0000899
900 // Append first type
901 TDT.PrintTree = false;
902 TDT.PrintFromType = true;
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.FromType));
911
Richard Trieu91844232012-06-26 18:18:47 +0000912 // Append middle text
Richard Trieuc6058442012-06-29 21:12:16 +0000913 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +0000914
915 // Append second type
916 TDT.PrintFromType = false;
917 getDiags()->ConvertArgToString(Kind, val,
Craig Topper3aa4fb32014-06-12 05:32:35 +0000918 StringRef(Modifier, ModifierLen),
919 StringRef(Argument, ArgumentLen),
Craig Toppere4753502014-06-12 05:32:27 +0000920 FormattedArgs,
Richard Trieu91844232012-06-26 18:18:47 +0000921 OutStr, QualTypeVals);
Richard Trieu50f5f462012-07-10 01:46:04 +0000922 if (!TDT.TemplateDiffUsed)
923 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
924 TDT.ToType));
925
Richard Trieu91844232012-06-26 18:18:47 +0000926 // Append end text
Richard Trieuc6058442012-06-29 21:12:16 +0000927 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +0000928 break;
Nico Weber4c311642008-08-10 19:59:06 +0000929 }
Chris Lattnerc243f292009-10-20 05:25:22 +0000930
931 // Remember this argument info for subsequent formatting operations. Turn
932 // std::strings into a null terminated string to make it be the same case as
933 // all the other ones.
Richard Trieu91844232012-06-26 18:18:47 +0000934 if (Kind == DiagnosticsEngine::ak_qualtype_pair)
935 continue;
936 else if (Kind != DiagnosticsEngine::ak_std_string)
Chris Lattnerc243f292009-10-20 05:25:22 +0000937 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
938 else
David Blaikie9c902b52011-09-25 23:23:43 +0000939 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_c_string,
Chris Lattnerc243f292009-10-20 05:25:22 +0000940 (intptr_t)getArgStdStr(ArgNo).c_str()));
941
Nico Weber4c311642008-08-10 19:59:06 +0000942 }
Richard Trieu91844232012-06-26 18:18:47 +0000943
944 // Append the type tree to the end of the diagnostics.
945 OutStr.append(Tree.begin(), Tree.end());
Nico Weber4c311642008-08-10 19:59:06 +0000946}
Ted Kremenekea06ec12009-01-23 20:28:53 +0000947
David Blaikie9c902b52011-09-25 23:23:43 +0000948StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000949 StringRef Message)
Benjamin Kramer929bd682010-11-19 17:36:51 +0000950 : ID(ID), Level(Level), Loc(), Message(Message) { }
Douglas Gregor33cdd812010-02-18 18:08:43 +0000951
David Blaikie9c902b52011-09-25 23:23:43 +0000952StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level,
David Blaikieb5784322011-09-26 01:18:08 +0000953 const Diagnostic &Info)
Douglas Gregora750e8e2010-11-19 16:18:16 +0000954 : ID(Info.getID()), Level(Level)
955{
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000956 assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
957 "Valid source location without setting a source manager for diagnostic");
958 if (Info.getLocation().isValid())
959 Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000960 SmallString<64> Message;
Douglas Gregor33cdd812010-02-18 18:08:43 +0000961 Info.FormatDiagnostic(Message);
962 this->Message.assign(Message.begin(), Message.end());
Benjamin Kramerf9890422015-02-17 16:48:30 +0000963 this->Ranges.assign(Info.getRanges().begin(), Info.getRanges().end());
964 this->FixIts.assign(Info.getFixItHints().begin(), Info.getFixItHints().end());
Douglas Gregor33cdd812010-02-18 18:08:43 +0000965}
966
David Blaikie9c902b52011-09-25 23:23:43 +0000967StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000968 StringRef Message, FullSourceLoc Loc,
Chris Lattner54b16772011-07-23 17:14:25 +0000969 ArrayRef<CharSourceRange> Ranges,
Aaron Ballman234ebd72013-02-24 19:08:10 +0000970 ArrayRef<FixItHint> FixIts)
971 : ID(ID), Level(Level), Loc(Loc), Message(Message),
972 Ranges(Ranges.begin(), Ranges.end()), FixIts(FixIts.begin(), FixIts.end())
Douglas Gregor925296b2011-07-19 16:10:42 +0000973{
Douglas Gregor925296b2011-07-19 16:10:42 +0000974}
975
Ted Kremenekea06ec12009-01-23 20:28:53 +0000976/// IncludeInDiagnosticCounts - This method (whose default implementation
977/// returns true) indicates whether the diagnostics handled by this
David Blaikiee2eefae2011-09-25 23:39:51 +0000978/// DiagnosticConsumer should be included in the number of diagnostics
David Blaikie9c902b52011-09-25 23:23:43 +0000979/// reported by DiagnosticsEngine.
David Blaikiee2eefae2011-09-25 23:39:51 +0000980bool DiagnosticConsumer::IncludeInDiagnosticCounts() const { return true; }
Douglas Gregor89336232010-03-29 23:34:08 +0000981
David Blaikie68e081d2011-12-20 02:48:34 +0000982void IgnoringDiagConsumer::anchor() { }
983
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000984ForwardingDiagnosticConsumer::~ForwardingDiagnosticConsumer() {}
Douglas Gregor6b930962013-05-03 22:58:43 +0000985
986void ForwardingDiagnosticConsumer::HandleDiagnostic(
987 DiagnosticsEngine::Level DiagLevel,
988 const Diagnostic &Info) {
989 Target.HandleDiagnostic(DiagLevel, Info);
990}
991
992void ForwardingDiagnosticConsumer::clear() {
993 DiagnosticConsumer::clear();
994 Target.clear();
995}
996
997bool ForwardingDiagnosticConsumer::IncludeInDiagnosticCounts() const {
998 return Target.IncludeInDiagnosticCounts();
999}
1000
Benjamin Kramer7ec12c92012-02-07 22:29:24 +00001001PartialDiagnostic::StorageAllocator::StorageAllocator() {
Douglas Gregor89336232010-03-29 23:34:08 +00001002 for (unsigned I = 0; I != NumCached; ++I)
1003 FreeList[I] = Cached + I;
1004 NumFreeListEntries = NumCached;
1005}
1006
Benjamin Kramer7ec12c92012-02-07 22:29:24 +00001007PartialDiagnostic::StorageAllocator::~StorageAllocator() {
Chad Rosier849a67b2012-02-07 23:24:49 +00001008 // Don't assert if we are in a CrashRecovery context, as this invariant may
1009 // be invalidated during a crash.
1010 assert((NumFreeListEntries == NumCached ||
1011 llvm::CrashRecoveryContext::isRecoveringFromCrash()) &&
1012 "A partial is on the lamb");
Douglas Gregor89336232010-03-29 23:34:08 +00001013}