blob: 661eabf9bc7cbbec193d768f075ede2cae48f612 [file] [log] [blame]
Eugene Zelenko25cae5a22018-02-16 23:40:07 +00001//===- Diagnostic.cpp - C Language Family Diagnostic Handling -------------===//
Chris Lattner22eb9722006-06-18 05:43:12 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner22eb9722006-06-18 05:43:12 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Diagnostic-related interfaces.
10//
11//===----------------------------------------------------------------------===//
12
Ted Kremenek39a76652010-04-12 19:54:17 +000013#include "clang/Basic/Diagnostic.h"
Alex Lorenzd0e27262017-08-25 15:48:00 +000014#include "clang/Basic/CharInfo.h"
15#include "clang/Basic/DiagnosticError.h"
Eugene Zelenko25cae5a22018-02-16 23:40:07 +000016#include "clang/Basic/DiagnosticIDs.h"
Douglas Gregor811db4e2012-10-23 22:26:28 +000017#include "clang/Basic/DiagnosticOptions.h"
Chris Lattnerb91fd172008-11-19 07:32:16 +000018#include "clang/Basic/IdentifierTable.h"
Ted Kremenek39a76652010-04-12 19:54:17 +000019#include "clang/Basic/PartialDiagnostic.h"
Eugene Zelenko25cae5a22018-02-16 23:40:07 +000020#include "clang/Basic/SourceLocation.h"
Benjamin Kramerbc9ef592017-01-18 15:50:26 +000021#include "clang/Basic/SourceManager.h"
Eugene Zelenko25cae5a22018-02-16 23:40:07 +000022#include "clang/Basic/Specifiers.h"
23#include "clang/Basic/TokenKinds.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000024#include "llvm/ADT/SmallString.h"
Eugene Zelenko25cae5a22018-02-16 23:40:07 +000025#include "llvm/ADT/SmallVector.h"
Jordan Rosec102b352012-09-22 01:24:42 +000026#include "llvm/ADT/StringExtras.h"
Eugene Zelenko25cae5a22018-02-16 23:40:07 +000027#include "llvm/ADT/StringRef.h"
Ted Kremenek84de4a12011-03-21 18:40:07 +000028#include "llvm/Support/CrashRecoveryContext.h"
Richard Trieub3b8bb02015-01-08 01:27:03 +000029#include "llvm/Support/Locale.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000030#include "llvm/Support/raw_ostream.h"
Eugene Zelenko25cae5a22018-02-16 23:40:07 +000031#include <algorithm>
32#include <cassert>
33#include <cstddef>
34#include <cstdint>
35#include <cstring>
36#include <limits>
37#include <string>
38#include <utility>
39#include <vector>
Ted Kremenek84de4a12011-03-21 18:40:07 +000040
Chris Lattner22eb9722006-06-18 05:43:12 +000041using namespace clang;
42
Douglas Gregoraea7afd2015-06-24 22:02:08 +000043const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
44 DiagNullabilityKind nullability) {
45 StringRef string;
46 switch (nullability.first) {
47 case NullabilityKind::NonNull:
48 string = nullability.second ? "'nonnull'" : "'_Nonnull'";
49 break;
50
51 case NullabilityKind::Nullable:
52 string = nullability.second ? "'nullable'" : "'_Nullable'";
53 break;
54
55 case NullabilityKind::Unspecified:
56 string = nullability.second ? "'null_unspecified'" : "'_Null_unspecified'";
57 break;
58 }
59
60 DB.AddString(string);
61 return DB;
62}
63
Reid Kleckner76221c72020-04-06 10:32:16 -070064const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
65 llvm::Error &&E) {
66 DB.AddString(toString(std::move(E)));
67 return DB;
68}
69
David Blaikie9c902b52011-09-25 23:23:43 +000070static void DummyArgToStringFn(DiagnosticsEngine::ArgumentKind AK, intptr_t QT,
Craig Topper3aa4fb32014-06-12 05:32:35 +000071 StringRef Modifier, StringRef Argument,
Craig Toppere4753502014-06-12 05:32:27 +000072 ArrayRef<DiagnosticsEngine::ArgumentValue> PrevArgs,
73 SmallVectorImpl<char> &Output,
74 void *Cookie,
75 ArrayRef<intptr_t> QualTypeVals) {
76 StringRef Str = "<can't format argument>";
77 Output.append(Str.begin(), Str.end());
Chris Lattner6a2ed6f2008-11-23 09:13:29 +000078}
79
Nico Weber8321ad92018-01-17 02:55:27 +000080DiagnosticsEngine::DiagnosticsEngine(
81 IntrusiveRefCntPtr<DiagnosticIDs> diags,
82 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, DiagnosticConsumer *client,
83 bool ShouldOwnClient)
Eugene Zelenko25cae5a22018-02-16 23:40:07 +000084 : Diags(std::move(diags)), DiagOpts(std::move(DiagOpts)) {
Alexander Kornienko41c247a2014-11-17 23:46:02 +000085 setClient(client, ShouldOwnClient);
Chris Lattner63ecc502008-11-23 09:21:17 +000086 ArgToStringFn = DummyArgToStringFn;
Douglas Gregor0e119552010-07-31 00:40:00 +000087
Douglas Gregoraa21cc42010-07-19 21:46:24 +000088 Reset();
Chris Lattnerae411572006-07-05 00:55:08 +000089}
90
Reid Klecknerdccbabf2014-12-17 20:23:11 +000091DiagnosticsEngine::~DiagnosticsEngine() {
92 // If we own the diagnostic client, destroy it first so that it can access the
93 // engine from its destructor.
94 setClient(nullptr);
95}
96
Fangrui Song2f553202018-12-01 01:43:05 +000097void DiagnosticsEngine::dump() const {
98 DiagStatesByLoc.dump(*SourceMgr);
99}
100
101void DiagnosticsEngine::dump(StringRef DiagName) const {
102 DiagStatesByLoc.dump(*SourceMgr, DiagName);
103}
104
David Blaikiee2eefae2011-09-25 23:39:51 +0000105void DiagnosticsEngine::setClient(DiagnosticConsumer *client,
David Blaikie9c902b52011-09-25 23:23:43 +0000106 bool ShouldOwnClient) {
Alexander Kornienko41c247a2014-11-17 23:46:02 +0000107 Owner.reset(ShouldOwnClient ? client : nullptr);
Douglas Gregor7a964ad2011-01-31 22:04:05 +0000108 Client = client;
Douglas Gregor7a964ad2011-01-31 22:04:05 +0000109}
Chris Lattnerfb42a182009-07-12 21:18:45 +0000110
David Blaikie9c902b52011-09-25 23:23:43 +0000111void DiagnosticsEngine::pushMappings(SourceLocation Loc) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000112 DiagStateOnPushStack.push_back(GetCurDiagState());
Chris Lattnerfb42a182009-07-12 21:18:45 +0000113}
114
David Blaikie9c902b52011-09-25 23:23:43 +0000115bool DiagnosticsEngine::popMappings(SourceLocation Loc) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000116 if (DiagStateOnPushStack.empty())
Chris Lattnerfb42a182009-07-12 21:18:45 +0000117 return false;
118
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000119 if (DiagStateOnPushStack.back() != GetCurDiagState()) {
120 // State changed at some point between push/pop.
121 PushDiagStatePoint(DiagStateOnPushStack.back(), Loc);
122 }
123 DiagStateOnPushStack.pop_back();
Chris Lattnerfb42a182009-07-12 21:18:45 +0000124 return true;
125}
126
David Blaikie9c902b52011-09-25 23:23:43 +0000127void DiagnosticsEngine::Reset() {
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000128 ErrorOccurred = false;
DeLesley Hutchins8ecd4912012-12-07 22:53:48 +0000129 UncompilableErrorOccurred = false;
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000130 FatalErrorOccurred = false;
Douglas Gregor8a60bbe2011-07-06 17:40:26 +0000131 UnrecoverableErrorOccurred = false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000132
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000133 NumWarnings = 0;
134 NumErrors = 0;
Argyrios Kyrtzidis1fa8b4b2011-07-29 01:25:44 +0000135 TrapNumErrorsOccurred = 0;
136 TrapNumUnrecoverableErrorsOccurred = 0;
Fangrui Song6907ce22018-07-30 19:24:48 +0000137
Eugene Zelenko25cae5a22018-02-16 23:40:07 +0000138 CurDiagID = std::numeric_limits<unsigned>::max();
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000139 LastDiagLevel = DiagnosticIDs::Ignored;
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000140 DelayedDiagID = 0;
Argyrios Kyrtzidisbbbeea12011-03-26 18:58:17 +0000141
142 // Clear state related to #pragma diagnostic.
143 DiagStates.clear();
Richard Smithd230de22017-01-26 01:01:01 +0000144 DiagStatesByLoc.clear();
Argyrios Kyrtzidisbbbeea12011-03-26 18:58:17 +0000145 DiagStateOnPushStack.clear();
146
147 // Create a DiagState and DiagStatePoint representing diagnostic changes
148 // through command-line.
Benjamin Kramer3204b152015-05-29 19:42:19 +0000149 DiagStates.emplace_back();
Richard Smithd230de22017-01-26 01:01:01 +0000150 DiagStatesByLoc.appendFirst(&DiagStates.back());
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000151}
Chris Lattner22eb9722006-06-18 05:43:12 +0000152
David Blaikie9c902b52011-09-25 23:23:43 +0000153void DiagnosticsEngine::SetDelayedDiagnostic(unsigned DiagID, StringRef Arg1,
Duncan P. N. Exon Smitheef69022019-11-10 11:17:42 -0800154 StringRef Arg2, StringRef Arg3) {
Douglas Gregor85795312010-03-22 15:10:57 +0000155 if (DelayedDiagID)
156 return;
157
158 DelayedDiagID = DiagID;
Douglas Gregor96380982010-03-22 15:47:45 +0000159 DelayedDiagArg1 = Arg1.str();
160 DelayedDiagArg2 = Arg2.str();
Duncan P. N. Exon Smitheef69022019-11-10 11:17:42 -0800161 DelayedDiagArg3 = Arg3.str();
Douglas Gregor85795312010-03-22 15:10:57 +0000162}
163
David Blaikie9c902b52011-09-25 23:23:43 +0000164void DiagnosticsEngine::ReportDelayed() {
Alex Lorenzce4518f2017-05-04 13:56:51 +0000165 unsigned ID = DelayedDiagID;
Douglas Gregor85795312010-03-22 15:10:57 +0000166 DelayedDiagID = 0;
Duncan P. N. Exon Smitheef69022019-11-10 11:17:42 -0800167 Report(ID) << DelayedDiagArg1 << DelayedDiagArg2 << DelayedDiagArg3;
Douglas Gregor85795312010-03-22 15:10:57 +0000168}
169
Eugene Zelenko25cae5a22018-02-16 23:40:07 +0000170void DiagnosticsEngine::DiagStateMap::appendFirst(DiagState *State) {
Richard Smithd230de22017-01-26 01:01:01 +0000171 assert(Files.empty() && "not first");
172 FirstDiagState = CurDiagState = State;
173 CurDiagStateLoc = SourceLocation();
Benjamin Kramerbc9ef592017-01-18 15:50:26 +0000174}
175
Richard Smithd230de22017-01-26 01:01:01 +0000176void DiagnosticsEngine::DiagStateMap::append(SourceManager &SrcMgr,
177 SourceLocation Loc,
178 DiagState *State) {
179 CurDiagState = State;
180 CurDiagStateLoc = Loc;
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000181
Richard Smithd230de22017-01-26 01:01:01 +0000182 std::pair<FileID, unsigned> Decomp = SrcMgr.getDecomposedLoc(Loc);
183 unsigned Offset = Decomp.second;
184 for (File *F = getFile(SrcMgr, Decomp.first); F;
185 Offset = F->ParentOffset, F = F->Parent) {
186 F->HasLocalTransitions = true;
187 auto &Last = F->StateTransitions.back();
188 assert(Last.Offset <= Offset && "state transitions added out of order");
Richard Smith99eff012012-08-17 00:55:32 +0000189
Richard Smithd230de22017-01-26 01:01:01 +0000190 if (Last.Offset == Offset) {
191 if (Last.State == State)
192 break;
193 Last.State = State;
194 continue;
195 }
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000196
Richard Smithd230de22017-01-26 01:01:01 +0000197 F->StateTransitions.push_back({State, Offset});
198 }
199}
200
201DiagnosticsEngine::DiagState *
202DiagnosticsEngine::DiagStateMap::lookup(SourceManager &SrcMgr,
203 SourceLocation Loc) const {
204 // Common case: we have not seen any diagnostic pragmas.
205 if (Files.empty())
206 return FirstDiagState;
207
208 std::pair<FileID, unsigned> Decomp = SrcMgr.getDecomposedLoc(Loc);
209 const File *F = getFile(SrcMgr, Decomp.first);
210 return F->lookup(Decomp.second);
211}
212
213DiagnosticsEngine::DiagState *
214DiagnosticsEngine::DiagStateMap::File::lookup(unsigned Offset) const {
Fangrui Song7264a472019-07-03 08:13:17 +0000215 auto OnePastIt =
216 llvm::partition_point(StateTransitions, [=](const DiagStatePoint &P) {
217 return P.Offset <= Offset;
Richard Smithd230de22017-01-26 01:01:01 +0000218 });
219 assert(OnePastIt != StateTransitions.begin() && "missing initial state");
220 return OnePastIt[-1].State;
221}
222
223DiagnosticsEngine::DiagStateMap::File *
224DiagnosticsEngine::DiagStateMap::getFile(SourceManager &SrcMgr,
225 FileID ID) const {
226 // Get or insert the File for this ID.
227 auto Range = Files.equal_range(ID);
228 if (Range.first != Range.second)
229 return &Range.first->second;
230 auto &F = Files.insert(Range.first, std::make_pair(ID, File()))->second;
231
232 // We created a new File; look up the diagnostic state at the start of it and
233 // initialize it.
234 if (ID.isValid()) {
235 std::pair<FileID, unsigned> Decomp = SrcMgr.getDecomposedIncludedLoc(ID);
236 F.Parent = getFile(SrcMgr, Decomp.first);
237 F.ParentOffset = Decomp.second;
238 F.StateTransitions.push_back({F.Parent->lookup(Decomp.second), 0});
239 } else {
240 // This is the (imaginary) root file into which we pretend all top-level
241 // files are included; it descends from the initial state.
242 //
243 // FIXME: This doesn't guarantee that we use the same ordering as
244 // isBeforeInTranslationUnit in the cases where someone invented another
245 // top-level file and added diagnostic pragmas to it. See the code at the
246 // end of isBeforeInTranslationUnit for the quirks it deals with.
247 F.StateTransitions.push_back({FirstDiagState, 0});
248 }
249 return &F;
250}
251
Richard Smith6c2b5a82018-02-09 01:15:13 +0000252void DiagnosticsEngine::DiagStateMap::dump(SourceManager &SrcMgr,
253 StringRef DiagName) const {
254 llvm::errs() << "diagnostic state at ";
Stephen Kelly3124ce72018-08-15 20:32:06 +0000255 CurDiagStateLoc.print(llvm::errs(), SrcMgr);
Richard Smith6c2b5a82018-02-09 01:15:13 +0000256 llvm::errs() << ": " << CurDiagState << "\n";
257
258 for (auto &F : Files) {
259 FileID ID = F.first;
260 File &File = F.second;
261
262 bool PrintedOuterHeading = false;
263 auto PrintOuterHeading = [&] {
264 if (PrintedOuterHeading) return;
265 PrintedOuterHeading = true;
266
267 llvm::errs() << "File " << &File << " <FileID " << ID.getHashValue()
268 << ">: " << SrcMgr.getBuffer(ID)->getBufferIdentifier();
269 if (F.second.Parent) {
270 std::pair<FileID, unsigned> Decomp =
271 SrcMgr.getDecomposedIncludedLoc(ID);
272 assert(File.ParentOffset == Decomp.second);
273 llvm::errs() << " parent " << File.Parent << " <FileID "
274 << Decomp.first.getHashValue() << "> ";
275 SrcMgr.getLocForStartOfFile(Decomp.first)
276 .getLocWithOffset(Decomp.second)
Stephen Kelly3124ce72018-08-15 20:32:06 +0000277 .print(llvm::errs(), SrcMgr);
Richard Smith6c2b5a82018-02-09 01:15:13 +0000278 }
279 if (File.HasLocalTransitions)
280 llvm::errs() << " has_local_transitions";
281 llvm::errs() << "\n";
282 };
283
284 if (DiagName.empty())
285 PrintOuterHeading();
286
287 for (DiagStatePoint &Transition : File.StateTransitions) {
288 bool PrintedInnerHeading = false;
289 auto PrintInnerHeading = [&] {
290 if (PrintedInnerHeading) return;
291 PrintedInnerHeading = true;
292
293 PrintOuterHeading();
294 llvm::errs() << " ";
295 SrcMgr.getLocForStartOfFile(ID)
296 .getLocWithOffset(Transition.Offset)
Stephen Kelly3124ce72018-08-15 20:32:06 +0000297 .print(llvm::errs(), SrcMgr);
Richard Smith6c2b5a82018-02-09 01:15:13 +0000298 llvm::errs() << ": state " << Transition.State << ":\n";
299 };
300
301 if (DiagName.empty())
302 PrintInnerHeading();
303
304 for (auto &Mapping : *Transition.State) {
305 StringRef Option =
306 DiagnosticIDs::getWarningOptionForDiag(Mapping.first);
307 if (!DiagName.empty() && DiagName != Option)
308 continue;
309
310 PrintInnerHeading();
311 llvm::errs() << " ";
312 if (Option.empty())
313 llvm::errs() << "<unknown " << Mapping.first << ">";
314 else
315 llvm::errs() << Option;
316 llvm::errs() << ": ";
317
318 switch (Mapping.second.getSeverity()) {
319 case diag::Severity::Ignored: llvm::errs() << "ignored"; break;
320 case diag::Severity::Remark: llvm::errs() << "remark"; break;
321 case diag::Severity::Warning: llvm::errs() << "warning"; break;
322 case diag::Severity::Error: llvm::errs() << "error"; break;
323 case diag::Severity::Fatal: llvm::errs() << "fatal"; break;
324 }
325
326 if (!Mapping.second.isUser())
327 llvm::errs() << " default";
328 if (Mapping.second.isPragma())
329 llvm::errs() << " pragma";
330 if (Mapping.second.hasNoWarningAsError())
331 llvm::errs() << " no-error";
332 if (Mapping.second.hasNoErrorAsFatal())
333 llvm::errs() << " no-fatal";
334 if (Mapping.second.wasUpgradedFromWarning())
335 llvm::errs() << " overruled";
336 llvm::errs() << "\n";
337 }
338 }
339 }
340}
341
Richard Smithd230de22017-01-26 01:01:01 +0000342void DiagnosticsEngine::PushDiagStatePoint(DiagState *State,
343 SourceLocation Loc) {
344 assert(Loc.isValid() && "Adding invalid loc point");
345 DiagStatesByLoc.append(*SourceMgr, Loc, State);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000346}
347
Alp Tokerd576e002014-06-12 11:13:52 +0000348void DiagnosticsEngine::setSeverity(diag::kind Diag, diag::Severity Map,
349 SourceLocation L) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000350 assert(Diag < diag::DIAG_UPPER_LIMIT &&
351 "Can only map builtin diagnostics");
352 assert((Diags->isBuiltinWarningOrExtension(Diag) ||
Alp Toker46df1c02014-06-12 10:15:20 +0000353 (Map == diag::Severity::Fatal || Map == diag::Severity::Error)) &&
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000354 "Cannot map errors into warnings!");
Richard Smith8a0527d2012-08-14 22:37:22 +0000355 assert((L.isInvalid() || SourceMgr) && "No SourceMgr for valid location");
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000356
Chad Rosierd1956e42012-02-03 01:49:51 +0000357 // Don't allow a mapping to a warning override an error/fatal mapping.
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +0000358 bool WasUpgradedFromWarning = false;
Alp Toker46df1c02014-06-12 10:15:20 +0000359 if (Map == diag::Severity::Warning) {
Alp Tokerc726c362014-06-10 09:31:37 +0000360 DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag);
Alp Toker46df1c02014-06-12 10:15:20 +0000361 if (Info.getSeverity() == diag::Severity::Error ||
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +0000362 Info.getSeverity() == diag::Severity::Fatal) {
Alp Tokerc726c362014-06-10 09:31:37 +0000363 Map = Info.getSeverity();
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +0000364 WasUpgradedFromWarning = true;
365 }
Chad Rosierd1956e42012-02-03 01:49:51 +0000366 }
Alp Tokerc726c362014-06-10 09:31:37 +0000367 DiagnosticMapping Mapping = makeUserMapping(Map, L);
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +0000368 Mapping.setUpgradedFromWarning(WasUpgradedFromWarning);
Daniel Dunbar2fba0972011-10-04 21:17:24 +0000369
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000370 // Common case; setting all the diagnostics of a group in one place.
Richard Smithd230de22017-01-26 01:01:01 +0000371 if ((L.isInvalid() || L == DiagStatesByLoc.getCurDiagStateLoc()) &&
372 DiagStatesByLoc.getCurDiagState()) {
373 // FIXME: This is theoretically wrong: if the current state is shared with
374 // some other location (via push/pop) we will change the state for that
375 // other location as well. This cannot currently happen, as we can't update
376 // the diagnostic state at the same location at which we pop.
377 DiagStatesByLoc.getCurDiagState()->setMapping(Diag, Mapping);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000378 return;
379 }
380
Richard Smithd230de22017-01-26 01:01:01 +0000381 // A diagnostic pragma occurred, create a new DiagState initialized with
382 // the current one and a new DiagStatePoint to record at which location
383 // the new state became active.
384 DiagStates.push_back(*GetCurDiagState());
385 DiagStates.back().setMapping(Diag, Mapping);
386 PushDiagStatePoint(&DiagStates.back(), L);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000387}
388
Richard Smith3be1cb22014-08-07 00:24:21 +0000389bool DiagnosticsEngine::setSeverityForGroup(diag::Flavor Flavor,
390 StringRef Group, diag::Severity Map,
Alp Tokerd576e002014-06-12 11:13:52 +0000391 SourceLocation Loc) {
Daniel Dunbard908c122011-09-29 01:47:16 +0000392 // Get the diagnostics in this group.
Hans Wennborgeb7cd662014-08-11 16:05:54 +0000393 SmallVector<diag::kind, 256> GroupDiags;
Richard Smith3be1cb22014-08-07 00:24:21 +0000394 if (Diags->getDiagnosticsInGroup(Flavor, Group, GroupDiags))
Daniel Dunbard908c122011-09-29 01:47:16 +0000395 return true;
396
397 // Set the mapping.
Hans Wennborgeb7cd662014-08-11 16:05:54 +0000398 for (diag::kind Diag : GroupDiags)
399 setSeverity(Diag, Map, Loc);
Daniel Dunbard908c122011-09-29 01:47:16 +0000400
401 return false;
402}
403
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000404bool DiagnosticsEngine::setDiagnosticGroupWarningAsError(StringRef Group,
405 bool Enabled) {
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000406 // If we are enabling this feature, just set the diagnostic mappings to map to
407 // errors.
408 if (Enabled)
Richard Smith3be1cb22014-08-07 00:24:21 +0000409 return setSeverityForGroup(diag::Flavor::WarningOrError, Group,
410 diag::Severity::Error);
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000411
412 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
413 // potentially downgrade anything already mapped to be a warning.
414
415 // Get the diagnostics in this group.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000416 SmallVector<diag::kind, 8> GroupDiags;
Richard Smith3be1cb22014-08-07 00:24:21 +0000417 if (Diags->getDiagnosticsInGroup(diag::Flavor::WarningOrError, Group,
418 GroupDiags))
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000419 return true;
420
421 // Perform the mapping change.
Craig Toppera52e2b22015-11-26 05:10:07 +0000422 for (diag::kind Diag : GroupDiags) {
423 DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag);
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000424
Alp Toker46df1c02014-06-12 10:15:20 +0000425 if (Info.getSeverity() == diag::Severity::Error ||
426 Info.getSeverity() == diag::Severity::Fatal)
427 Info.setSeverity(diag::Severity::Warning);
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000428
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000429 Info.setNoWarningAsError(true);
430 }
431
432 return false;
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000433}
434
435bool DiagnosticsEngine::setDiagnosticGroupErrorAsFatal(StringRef Group,
436 bool Enabled) {
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000437 // If we are enabling this feature, just set the diagnostic mappings to map to
438 // fatal errors.
439 if (Enabled)
Richard Smith3be1cb22014-08-07 00:24:21 +0000440 return setSeverityForGroup(diag::Flavor::WarningOrError, Group,
441 diag::Severity::Fatal);
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000442
Richard Smithe37391c2017-05-03 00:28:49 +0000443 // Otherwise, we want to set the diagnostic mapping's "no Wfatal-errors" bit,
444 // and potentially downgrade anything already mapped to be a fatal error.
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000445
446 // Get the diagnostics in this group.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000447 SmallVector<diag::kind, 8> GroupDiags;
Richard Smith3be1cb22014-08-07 00:24:21 +0000448 if (Diags->getDiagnosticsInGroup(diag::Flavor::WarningOrError, Group,
449 GroupDiags))
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000450 return true;
451
452 // Perform the mapping change.
Craig Toppera52e2b22015-11-26 05:10:07 +0000453 for (diag::kind Diag : GroupDiags) {
454 DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag);
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000455
Alp Toker46df1c02014-06-12 10:15:20 +0000456 if (Info.getSeverity() == diag::Severity::Fatal)
457 Info.setSeverity(diag::Severity::Error);
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000458
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000459 Info.setNoErrorAsFatal(true);
460 }
461
462 return false;
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000463}
464
Richard Smith3be1cb22014-08-07 00:24:21 +0000465void DiagnosticsEngine::setSeverityForAll(diag::Flavor Flavor,
466 diag::Severity Map,
Alp Tokerd576e002014-06-12 11:13:52 +0000467 SourceLocation Loc) {
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000468 // Get all the diagnostics.
Gabor Horvath53b5c132017-12-20 16:55:41 +0000469 std::vector<diag::kind> AllDiags;
Gabor Horvath328d3af2017-11-14 12:14:49 +0000470 DiagnosticIDs::getAllDiagnostics(Flavor, AllDiags);
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000471
472 // Set the mapping.
Craig Toppera52e2b22015-11-26 05:10:07 +0000473 for (diag::kind Diag : AllDiags)
474 if (Diags->isBuiltinWarningOrExtension(Diag))
475 setSeverity(Diag, Map, Loc);
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000476}
477
David Blaikie9c902b52011-09-25 23:23:43 +0000478void DiagnosticsEngine::Report(const StoredDiagnostic &storedDiag) {
Eugene Zelenko25cae5a22018-02-16 23:40:07 +0000479 assert(CurDiagID == std::numeric_limits<unsigned>::max() &&
480 "Multiple diagnostics in flight at once!");
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000481
482 CurDiagLoc = storedDiag.getLocation();
483 CurDiagID = storedDiag.getID();
484 NumDiagArgs = 0;
485
Alexander Kornienkod3b4e082014-05-22 19:56:11 +0000486 DiagRanges.clear();
Benjamin Kramerf367dd92015-06-12 15:31:50 +0000487 DiagRanges.append(storedDiag.range_begin(), storedDiag.range_end());
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000488
Alexander Kornienkod3b4e082014-05-22 19:56:11 +0000489 DiagFixItHints.clear();
Benjamin Kramerf367dd92015-06-12 15:31:50 +0000490 DiagFixItHints.append(storedDiag.fixit_begin(), storedDiag.fixit_end());
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000491
David Blaikiee2eefae2011-09-25 23:39:51 +0000492 assert(Client && "DiagnosticConsumer not set!");
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000493 Level DiagLevel = storedDiag.getLevel();
David Blaikieb5784322011-09-26 01:18:08 +0000494 Diagnostic Info(this, storedDiag.getMessage());
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000495 Client->HandleDiagnostic(DiagLevel, Info);
496 if (Client->IncludeInDiagnosticCounts()) {
David Blaikie9c902b52011-09-25 23:23:43 +0000497 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000498 ++NumWarnings;
499 }
500
Eugene Zelenko25cae5a22018-02-16 23:40:07 +0000501 CurDiagID = std::numeric_limits<unsigned>::max();
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000502}
503
Jordan Rose6f524ac2012-07-11 16:50:36 +0000504bool DiagnosticsEngine::EmitCurrentDiagnostic(bool Force) {
505 assert(getClient() && "DiagnosticClient not set!");
506
507 bool Emitted;
508 if (Force) {
509 Diagnostic Info(this);
510
511 // Figure out the diagnostic level of this message.
512 DiagnosticIDs::Level DiagLevel
513 = Diags->getDiagnosticLevel(Info.getID(), Info.getLocation(), *this);
514
515 Emitted = (DiagLevel != DiagnosticIDs::Ignored);
516 if (Emitted) {
517 // Emit the diagnostic regardless of suppression level.
518 Diags->EmitDiag(*this, DiagLevel);
519 }
520 } else {
521 // Process the diagnostic, sending the accumulated information to the
522 // DiagnosticConsumer.
523 Emitted = ProcessDiag();
524 }
Douglas Gregor85795312010-03-22 15:10:57 +0000525
526 // Clear out the current diagnostic object.
Daniel Dunbarc7c00892012-03-13 21:02:14 +0000527 Clear();
Douglas Gregor85795312010-03-22 15:10:57 +0000528
529 // If there was a delayed diagnostic, emit it now.
Alex Lorenzce4518f2017-05-04 13:56:51 +0000530 if (!Force && DelayedDiagID)
Daniel Dunbarc7c00892012-03-13 21:02:14 +0000531 ReportDelayed();
Douglas Gregor85795312010-03-22 15:10:57 +0000532
533 return Emitted;
534}
535
Eugene Zelenko25cae5a22018-02-16 23:40:07 +0000536DiagnosticConsumer::~DiagnosticConsumer() = default;
Nico Weber4c311642008-08-10 19:59:06 +0000537
David Blaikiee2eefae2011-09-25 23:39:51 +0000538void DiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
David Blaikieb5784322011-09-26 01:18:08 +0000539 const Diagnostic &Info) {
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000540 if (!IncludeInDiagnosticCounts())
541 return;
542
David Blaikie9c902b52011-09-25 23:23:43 +0000543 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000544 ++NumWarnings;
David Blaikie9c902b52011-09-25 23:23:43 +0000545 else if (DiagLevel >= DiagnosticsEngine::Error)
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000546 ++NumErrors;
547}
Chris Lattner23be0672008-11-19 06:51:40 +0000548
Chris Lattner2b786902008-11-21 07:50:02 +0000549/// ModifierIs - Return true if the specified modifier matches specified string.
550template <std::size_t StrLen>
551static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
552 const char (&Str)[StrLen]) {
Eugene Zelenko25cae5a22018-02-16 23:40:07 +0000553 return StrLen-1 == ModifierLen && memcmp(Modifier, Str, StrLen-1) == 0;
Chris Lattner2b786902008-11-21 07:50:02 +0000554}
555
John McCall8cb7a8a32010-01-14 20:11:39 +0000556/// ScanForward - Scans forward, looking for the given character, skipping
557/// nested clauses and escaped characters.
558static const char *ScanFormat(const char *I, const char *E, char Target) {
559 unsigned Depth = 0;
560
561 for ( ; I != E; ++I) {
562 if (Depth == 0 && *I == Target) return I;
563 if (Depth != 0 && *I == '}') Depth--;
564
565 if (*I == '%') {
566 I++;
567 if (I == E) break;
568
569 // Escaped characters get implicitly skipped here.
570
571 // Format specifier.
Jordan Rosea7d03842013-02-08 22:30:41 +0000572 if (!isDigit(*I) && !isPunctuation(*I)) {
573 for (I++; I != E && !isDigit(*I) && *I != '{'; I++) ;
John McCall8cb7a8a32010-01-14 20:11:39 +0000574 if (I == E) break;
575 if (*I == '{')
576 Depth++;
577 }
578 }
579 }
580 return E;
581}
582
Chris Lattner2b786902008-11-21 07:50:02 +0000583/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
584/// like this: %select{foo|bar|baz}2. This means that the integer argument
585/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
586/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
587/// This is very useful for certain classes of variant diagnostics.
David Blaikieb5784322011-09-26 01:18:08 +0000588static void HandleSelectModifier(const Diagnostic &DInfo, unsigned ValNo,
Chris Lattner2b786902008-11-21 07:50:02 +0000589 const char *Argument, unsigned ArgumentLen,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000590 SmallVectorImpl<char> &OutStr) {
Chris Lattner2b786902008-11-21 07:50:02 +0000591 const char *ArgumentEnd = Argument+ArgumentLen;
Mike Stump11289f42009-09-09 15:08:12 +0000592
Chris Lattner2b786902008-11-21 07:50:02 +0000593 // Skip over 'ValNo' |'s.
594 while (ValNo) {
John McCall8cb7a8a32010-01-14 20:11:39 +0000595 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
Chris Lattner2b786902008-11-21 07:50:02 +0000596 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
597 " larger than the number of options in the diagnostic string!");
598 Argument = NextVal+1; // Skip this string.
599 --ValNo;
600 }
Mike Stump11289f42009-09-09 15:08:12 +0000601
Chris Lattner2b786902008-11-21 07:50:02 +0000602 // Get the end of the value. This is either the } or the |.
John McCall8cb7a8a32010-01-14 20:11:39 +0000603 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
John McCalle4d54322010-01-13 23:58:20 +0000604
605 // Recursively format the result of the select clause into the output string.
606 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000607}
608
609/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
610/// letter 's' to the string if the value is not 1. This is used in cases like
611/// this: "you idiot, you have %4 parameter%s4!".
612static void HandleIntegerSModifier(unsigned ValNo,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000613 SmallVectorImpl<char> &OutStr) {
Chris Lattner2b786902008-11-21 07:50:02 +0000614 if (ValNo != 1)
615 OutStr.push_back('s');
616}
617
John McCall9015cde2010-01-14 00:50:32 +0000618/// HandleOrdinalModifier - Handle the integer 'ord' modifier. This
619/// prints the ordinal form of the given integer, with 1 corresponding
620/// to the first ordinal. Currently this is hard-coded to use the
621/// English form.
622static void HandleOrdinalModifier(unsigned ValNo,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000623 SmallVectorImpl<char> &OutStr) {
John McCall9015cde2010-01-14 00:50:32 +0000624 assert(ValNo != 0 && "ValNo must be strictly positive!");
625
626 llvm::raw_svector_ostream Out(OutStr);
627
628 // We could use text forms for the first N ordinals, but the numeric
629 // forms are actually nicer in diagnostics because they stand out.
Jordan Rosec102b352012-09-22 01:24:42 +0000630 Out << ValNo << llvm::getOrdinalSuffix(ValNo);
John McCall9015cde2010-01-14 00:50:32 +0000631}
632
Sebastian Redl15b02d22008-11-22 13:44:36 +0000633/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000634static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000635 // Programming 101: Parse a decimal number :-)
636 unsigned Val = 0;
637 while (Start != End && *Start >= '0' && *Start <= '9') {
638 Val *= 10;
639 Val += *Start - '0';
640 ++Start;
641 }
642 return Val;
643}
644
645/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000646static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000647 if (*Start != '[') {
648 unsigned Ref = PluralNumber(Start, End);
649 return Ref == Val;
650 }
651
652 ++Start;
653 unsigned Low = PluralNumber(Start, End);
654 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
655 ++Start;
656 unsigned High = PluralNumber(Start, End);
657 assert(*Start == ']' && "Bad plural expression syntax: expected )");
658 ++Start;
659 return Low <= Val && Val <= High;
660}
661
662/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattner2fe29202009-04-15 17:13:42 +0000663static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000664 // Empty condition?
665 if (*Start == ':')
666 return true;
667
Eugene Zelenko25cae5a22018-02-16 23:40:07 +0000668 while (true) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000669 char C = *Start;
670 if (C == '%') {
671 // Modulo expression
672 ++Start;
673 unsigned Arg = PluralNumber(Start, End);
674 assert(*Start == '=' && "Bad plural expression syntax: expected =");
675 ++Start;
676 unsigned ValMod = ValNo % Arg;
677 if (TestPluralRange(ValMod, Start, End))
678 return true;
679 } else {
Sebastian Redl3ceaf622008-11-27 07:28:14 +0000680 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redl15b02d22008-11-22 13:44:36 +0000681 "Bad plural expression syntax: unexpected character");
682 // Range expression
683 if (TestPluralRange(ValNo, Start, End))
684 return true;
685 }
686
687 // Scan for next or-expr part.
688 Start = std::find(Start, End, ',');
Mike Stump11289f42009-09-09 15:08:12 +0000689 if (Start == End)
Sebastian Redl15b02d22008-11-22 13:44:36 +0000690 break;
691 ++Start;
692 }
693 return false;
694}
695
696/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
697/// for complex plural forms, or in languages where all plurals are complex.
698/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
699/// conditions that are tested in order, the form corresponding to the first
700/// that applies being emitted. The empty condition is always true, making the
701/// last form a default case.
702/// Conditions are simple boolean expressions, where n is the number argument.
703/// Here are the rules.
704/// condition := expression | empty
705/// empty := -> always true
706/// expression := numeric [',' expression] -> logical or
707/// numeric := range -> true if n in range
708/// | '%' number '=' range -> true if n % number in range
709/// range := number
710/// | '[' number ',' number ']' -> ranges are inclusive both ends
711///
712/// Here are some examples from the GNU gettext manual written in this form:
713/// English:
714/// {1:form0|:form1}
715/// Latvian:
716/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
717/// Gaeilge:
718/// {1:form0|2:form1|:form2}
719/// Romanian:
720/// {1:form0|0,%100=[1,19]:form1|:form2}
721/// Lithuanian:
722/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
723/// Russian (requires repeated form):
724/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
725/// Slovak
726/// {1:form0|[2,4]:form1|:form2}
727/// Polish (requires repeated form):
728/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
David Blaikieb5784322011-09-26 01:18:08 +0000729static void HandlePluralModifier(const Diagnostic &DInfo, unsigned ValNo,
Sebastian Redl15b02d22008-11-22 13:44:36 +0000730 const char *Argument, unsigned ArgumentLen,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000731 SmallVectorImpl<char> &OutStr) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000732 const char *ArgumentEnd = Argument + ArgumentLen;
Eugene Zelenko25cae5a22018-02-16 23:40:07 +0000733 while (true) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000734 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
735 const char *ExprEnd = Argument;
736 while (*ExprEnd != ':') {
737 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
738 ++ExprEnd;
739 }
740 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
741 Argument = ExprEnd + 1;
John McCall8cb7a8a32010-01-14 20:11:39 +0000742 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
John McCall43b61682010-10-14 01:55:31 +0000743
744 // Recursively format the result of the plural clause into the
745 // output string.
746 DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000747 return;
748 }
John McCall8cb7a8a32010-01-14 20:11:39 +0000749 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
Sebastian Redl15b02d22008-11-22 13:44:36 +0000750 }
751}
752
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000753/// Returns the friendly description for a token kind that will appear
Alp Tokera231ad22014-01-06 12:54:18 +0000754/// without quotes in diagnostic messages. These strings may be translatable in
755/// future.
756static const char *getTokenDescForDiagnostic(tok::TokenKind Kind) {
Alp Tokerec543272013-12-24 09:48:30 +0000757 switch (Kind) {
758 case tok::identifier:
759 return "identifier";
760 default:
Craig Topperf1186c52014-05-08 06:41:40 +0000761 return nullptr;
Alp Tokerec543272013-12-24 09:48:30 +0000762 }
763}
Sebastian Redl15b02d22008-11-22 13:44:36 +0000764
Chris Lattner23be0672008-11-19 06:51:40 +0000765/// FormatDiagnostic - Format this diagnostic into a string, substituting the
766/// formal arguments into the %0 slots. The result is appended onto the Str
767/// array.
David Blaikieb5784322011-09-26 01:18:08 +0000768void Diagnostic::
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000769FormatDiagnostic(SmallVectorImpl<char> &OutStr) const {
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000770 if (!StoredDiagMessage.empty()) {
771 OutStr.append(StoredDiagMessage.begin(), StoredDiagMessage.end());
772 return;
773 }
774
Fangrui Song6907ce22018-07-30 19:24:48 +0000775 StringRef Diag =
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000776 getDiags()->getDiagnosticIDs()->getDescription(getID());
Mike Stump11289f42009-09-09 15:08:12 +0000777
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000778 FormatDiagnostic(Diag.begin(), Diag.end(), OutStr);
John McCalle4d54322010-01-13 23:58:20 +0000779}
780
David Blaikieb5784322011-09-26 01:18:08 +0000781void Diagnostic::
John McCalle4d54322010-01-13 23:58:20 +0000782FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000783 SmallVectorImpl<char> &OutStr) const {
Richard Trieub3b8bb02015-01-08 01:27:03 +0000784 // When the diagnostic string is only "%0", the entire string is being given
785 // by an outside source. Remove unprintable characters from this string
786 // and skip all the other string processing.
Richard Trieudcd7bb02015-01-17 00:56:10 +0000787 if (DiagEnd - DiagStr == 2 &&
788 StringRef(DiagStr, DiagEnd - DiagStr).equals("%0") &&
Richard Trieub3b8bb02015-01-08 01:27:03 +0000789 getArgKind(0) == DiagnosticsEngine::ak_std_string) {
790 const std::string &S = getArgStdStr(0);
791 for (char c : S) {
792 if (llvm::sys::locale::isPrint(c) || c == '\t') {
793 OutStr.push_back(c);
794 }
795 }
796 return;
797 }
798
Chris Lattnerc243f292009-10-20 05:25:22 +0000799 /// FormattedArgs - Keep track of all of the arguments formatted by
800 /// ConvertArgToString and pass them into subsequent calls to
801 /// ConvertArgToString, allowing the implementation to avoid redundancies in
802 /// obvious cases.
David Blaikie9c902b52011-09-25 23:23:43 +0000803 SmallVector<DiagnosticsEngine::ArgumentValue, 8> FormattedArgs;
Chandler Carruthd5173952011-07-11 17:49:21 +0000804
805 /// QualTypeVals - Pass a vector of arrays so that QualType names can be
806 /// compared to see if more information is needed to be printed.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000807 SmallVector<intptr_t, 2> QualTypeVals;
Richard Trieu91844232012-06-26 18:18:47 +0000808 SmallVector<char, 64> Tree;
809
Chandler Carruthd5173952011-07-11 17:49:21 +0000810 for (unsigned i = 0, e = getNumArgs(); i < e; ++i)
David Blaikie9c902b52011-09-25 23:23:43 +0000811 if (getArgKind(i) == DiagnosticsEngine::ak_qualtype)
Chandler Carruthd5173952011-07-11 17:49:21 +0000812 QualTypeVals.push_back(getRawArg(i));
813
Chris Lattner23be0672008-11-19 06:51:40 +0000814 while (DiagStr != DiagEnd) {
815 if (DiagStr[0] != '%') {
816 // Append non-%0 substrings to Str if we have one.
817 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
818 OutStr.append(DiagStr, StrEnd);
819 DiagStr = StrEnd;
Chris Lattner2b786902008-11-21 07:50:02 +0000820 continue;
Jordan Rosea7d03842013-02-08 22:30:41 +0000821 } else if (isPunctuation(DiagStr[1])) {
John McCall8cb7a8a32010-01-14 20:11:39 +0000822 OutStr.push_back(DiagStr[1]); // %% -> %.
Chris Lattner23be0672008-11-19 06:51:40 +0000823 DiagStr += 2;
Chris Lattner2b786902008-11-21 07:50:02 +0000824 continue;
825 }
Mike Stump11289f42009-09-09 15:08:12 +0000826
Chris Lattner2b786902008-11-21 07:50:02 +0000827 // Skip the %.
828 ++DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000829
Chris Lattner2b786902008-11-21 07:50:02 +0000830 // This must be a placeholder for a diagnostic argument. The format for a
831 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
832 // The digit is a number from 0-9 indicating which argument this comes from.
833 // The modifier is a string of digits from the set [-a-z]+, arguments is a
834 // brace enclosed string.
Craig Topperf1186c52014-05-08 06:41:40 +0000835 const char *Modifier = nullptr, *Argument = nullptr;
Chris Lattner2b786902008-11-21 07:50:02 +0000836 unsigned ModifierLen = 0, ArgumentLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000837
Chris Lattner2b786902008-11-21 07:50:02 +0000838 // Check to see if we have a modifier. If so eat it.
Jordan Rosea7d03842013-02-08 22:30:41 +0000839 if (!isDigit(DiagStr[0])) {
Chris Lattner2b786902008-11-21 07:50:02 +0000840 Modifier = DiagStr;
841 while (DiagStr[0] == '-' ||
842 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
843 ++DiagStr;
844 ModifierLen = DiagStr-Modifier;
Chris Lattner23be0672008-11-19 06:51:40 +0000845
Chris Lattner2b786902008-11-21 07:50:02 +0000846 // If we have an argument, get it next.
847 if (DiagStr[0] == '{') {
848 ++DiagStr; // Skip {.
849 Argument = DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000850
John McCall8cb7a8a32010-01-14 20:11:39 +0000851 DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
852 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
Chris Lattner2b786902008-11-21 07:50:02 +0000853 ArgumentLen = DiagStr-Argument;
854 ++DiagStr; // Skip }.
Chris Lattner23be0672008-11-19 06:51:40 +0000855 }
Chris Lattner2b786902008-11-21 07:50:02 +0000856 }
Mike Stump11289f42009-09-09 15:08:12 +0000857
Jordan Rosea7d03842013-02-08 22:30:41 +0000858 assert(isDigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000859 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattner2b786902008-11-21 07:50:02 +0000860
Richard Trieu91844232012-06-26 18:18:47 +0000861 // Only used for type diffing.
862 unsigned ArgNo2 = ArgNo;
863
David Blaikie9c902b52011-09-25 23:23:43 +0000864 DiagnosticsEngine::ArgumentKind Kind = getArgKind(ArgNo);
Richard Trieu90c31f52013-01-30 20:04:31 +0000865 if (ModifierIs(Modifier, ModifierLen, "diff")) {
Jordan Rosea7d03842013-02-08 22:30:41 +0000866 assert(*DiagStr == ',' && isDigit(*(DiagStr + 1)) &&
Richard Trieu91844232012-06-26 18:18:47 +0000867 "Invalid format for diff modifier");
868 ++DiagStr; // Comma.
869 ArgNo2 = *DiagStr++ - '0';
Richard Trieu90c31f52013-01-30 20:04:31 +0000870 DiagnosticsEngine::ArgumentKind Kind2 = getArgKind(ArgNo2);
871 if (Kind == DiagnosticsEngine::ak_qualtype &&
872 Kind2 == DiagnosticsEngine::ak_qualtype)
873 Kind = DiagnosticsEngine::ak_qualtype_pair;
874 else {
875 // %diff only supports QualTypes. For other kinds of arguments,
876 // use the default printing. For example, if the modifier is:
877 // "%diff{compare $ to $|other text}1,2"
878 // treat it as:
879 // "compare %1 to %2"
Chandler Carruth8df65e42016-12-23 05:19:47 +0000880 const char *ArgumentEnd = Argument + ArgumentLen;
881 const char *Pipe = ScanFormat(Argument, ArgumentEnd, '|');
882 assert(ScanFormat(Pipe + 1, ArgumentEnd, '|') == ArgumentEnd &&
883 "Found too many '|'s in a %diff modifier!");
Richard Trieu90c31f52013-01-30 20:04:31 +0000884 const char *FirstDollar = ScanFormat(Argument, Pipe, '$');
885 const char *SecondDollar = ScanFormat(FirstDollar + 1, Pipe, '$');
Filipe Cabecinhased4a00c2013-01-30 22:03:24 +0000886 const char ArgStr1[] = { '%', static_cast<char>('0' + ArgNo) };
887 const char ArgStr2[] = { '%', static_cast<char>('0' + ArgNo2) };
Richard Trieu90c31f52013-01-30 20:04:31 +0000888 FormatDiagnostic(Argument, FirstDollar, OutStr);
889 FormatDiagnostic(ArgStr1, ArgStr1 + 2, OutStr);
890 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
891 FormatDiagnostic(ArgStr2, ArgStr2 + 2, OutStr);
892 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
893 continue;
894 }
Richard Trieu91844232012-06-26 18:18:47 +0000895 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000896
Chris Lattnerc243f292009-10-20 05:25:22 +0000897 switch (Kind) {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000898 // ---- STRINGS ----
David Blaikie9c902b52011-09-25 23:23:43 +0000899 case DiagnosticsEngine::ak_std_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000900 const std::string &S = getArgStdStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000901 assert(ModifierLen == 0 && "No modifiers for strings yet");
902 OutStr.append(S.begin(), S.end());
903 break;
904 }
David Blaikie9c902b52011-09-25 23:23:43 +0000905 case DiagnosticsEngine::ak_c_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000906 const char *S = getArgCStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000907 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000908
909 // Don't crash if get passed a null pointer by accident.
910 if (!S)
911 S = "(null)";
Mike Stump11289f42009-09-09 15:08:12 +0000912
Chris Lattner2b786902008-11-21 07:50:02 +0000913 OutStr.append(S, S + strlen(S));
914 break;
915 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000916 // ---- INTEGERS ----
David Blaikie9c902b52011-09-25 23:23:43 +0000917 case DiagnosticsEngine::ak_sint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000918 int Val = getArgSInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000919
Chris Lattner2b786902008-11-21 07:50:02 +0000920 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCall43b61682010-10-14 01:55:31 +0000921 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen,
922 OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000923 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
924 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000925 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCall43b61682010-10-14 01:55:31 +0000926 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
927 OutStr);
John McCall9015cde2010-01-14 00:50:32 +0000928 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
929 HandleOrdinalModifier((unsigned)Val, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000930 } else {
931 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000932 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000933 }
Chris Lattner2b786902008-11-21 07:50:02 +0000934 break;
935 }
David Blaikie9c902b52011-09-25 23:23:43 +0000936 case DiagnosticsEngine::ak_uint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000937 unsigned Val = getArgUInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000938
Chris Lattner2b786902008-11-21 07:50:02 +0000939 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCalle4d54322010-01-13 23:58:20 +0000940 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000941 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
942 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000943 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCall43b61682010-10-14 01:55:31 +0000944 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
945 OutStr);
John McCall9015cde2010-01-14 00:50:32 +0000946 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
947 HandleOrdinalModifier(Val, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000948 } else {
949 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000950 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000951 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000952 break;
Chris Lattner2b786902008-11-21 07:50:02 +0000953 }
Alp Tokerec543272013-12-24 09:48:30 +0000954 // ---- TOKEN SPELLINGS ----
955 case DiagnosticsEngine::ak_tokenkind: {
956 tok::TokenKind Kind = static_cast<tok::TokenKind>(getRawArg(ArgNo));
957 assert(ModifierLen == 0 && "No modifiers for token kinds yet");
958
959 llvm::raw_svector_ostream Out(OutStr);
Alp Tokera231ad22014-01-06 12:54:18 +0000960 if (const char *S = tok::getPunctuatorSpelling(Kind))
961 // Quoted token spelling for punctuators.
962 Out << '\'' << S << '\'';
963 else if (const char *S = tok::getKeywordSpelling(Kind))
964 // Unquoted token spelling for keywords.
965 Out << S;
966 else if (const char *S = getTokenDescForDiagnostic(Kind))
Alp Tokerec543272013-12-24 09:48:30 +0000967 // Unquoted translatable token name.
968 Out << S;
Alp Tokerec543272013-12-24 09:48:30 +0000969 else if (const char *S = tok::getTokenName(Kind))
970 // Debug name, shouldn't appear in user-facing diagnostics.
971 Out << '<' << S << '>';
972 else
973 Out << "(null)";
974 break;
975 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000976 // ---- NAMES and TYPES ----
David Blaikie9c902b52011-09-25 23:23:43 +0000977 case DiagnosticsEngine::ak_identifierinfo: {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000978 const IdentifierInfo *II = getArgIdentifier(ArgNo);
979 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000980
981 // Don't crash if get passed a null pointer by accident.
982 if (!II) {
983 const char *S = "(null)";
984 OutStr.append(S, S + strlen(S));
985 continue;
986 }
987
Daniel Dunbar07d07852009-10-18 21:17:35 +0000988 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
Chris Lattnere3d20d92008-11-23 21:45:46 +0000989 break;
990 }
Anastasia Stulovaed8dadb2019-12-13 12:30:59 +0000991 case DiagnosticsEngine::ak_addrspace:
Anastasia Stulova4cebc9d2019-01-04 11:50:36 +0000992 case DiagnosticsEngine::ak_qual:
David Blaikie9c902b52011-09-25 23:23:43 +0000993 case DiagnosticsEngine::ak_qualtype:
994 case DiagnosticsEngine::ak_declarationname:
995 case DiagnosticsEngine::ak_nameddecl:
996 case DiagnosticsEngine::ak_nestednamespec:
997 case DiagnosticsEngine::ak_declcontext:
Aaron Ballman3e424b52013-12-26 18:30:57 +0000998 case DiagnosticsEngine::ak_attr:
Chris Lattnerc243f292009-10-20 05:25:22 +0000999 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
Craig Topper3aa4fb32014-06-12 05:32:35 +00001000 StringRef(Modifier, ModifierLen),
1001 StringRef(Argument, ArgumentLen),
Craig Toppere4753502014-06-12 05:32:27 +00001002 FormattedArgs,
Chandler Carruthd5173952011-07-11 17:49:21 +00001003 OutStr, QualTypeVals);
Chris Lattner6a2ed6f2008-11-23 09:13:29 +00001004 break;
Eugene Zelenko25cae5a22018-02-16 23:40:07 +00001005 case DiagnosticsEngine::ak_qualtype_pair: {
Richard Trieu91844232012-06-26 18:18:47 +00001006 // Create a struct with all the info needed for printing.
1007 TemplateDiffTypes TDT;
1008 TDT.FromType = getRawArg(ArgNo);
1009 TDT.ToType = getRawArg(ArgNo2);
1010 TDT.ElideType = getDiags()->ElideType;
1011 TDT.ShowColors = getDiags()->ShowColors;
Richard Trieu50f5f462012-07-10 01:46:04 +00001012 TDT.TemplateDiffUsed = false;
Richard Trieu91844232012-06-26 18:18:47 +00001013 intptr_t val = reinterpret_cast<intptr_t>(&TDT);
1014
Richard Trieuc6058442012-06-29 21:12:16 +00001015 const char *ArgumentEnd = Argument + ArgumentLen;
1016 const char *Pipe = ScanFormat(Argument, ArgumentEnd, '|');
1017
Richard Trieua4056002012-07-13 21:18:32 +00001018 // Print the tree. If this diagnostic already has a tree, skip the
1019 // second tree.
1020 if (getDiags()->PrintTemplateTree && Tree.empty()) {
Richard Trieu91844232012-06-26 18:18:47 +00001021 TDT.PrintFromType = true;
1022 TDT.PrintTree = true;
1023 getDiags()->ConvertArgToString(Kind, val,
Craig Topper3aa4fb32014-06-12 05:32:35 +00001024 StringRef(Modifier, ModifierLen),
1025 StringRef(Argument, ArgumentLen),
Craig Toppere4753502014-06-12 05:32:27 +00001026 FormattedArgs,
Richard Trieu91844232012-06-26 18:18:47 +00001027 Tree, QualTypeVals);
1028 // If there is no tree information, fall back to regular printing.
Richard Trieuc6058442012-06-29 21:12:16 +00001029 if (!Tree.empty()) {
1030 FormatDiagnostic(Pipe + 1, ArgumentEnd, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +00001031 break;
Richard Trieuc6058442012-06-29 21:12:16 +00001032 }
Richard Trieu91844232012-06-26 18:18:47 +00001033 }
1034
1035 // Non-tree printing, also the fall-back when tree printing fails.
1036 // The fall-back is triggered when the types compared are not templates.
Richard Trieuc6058442012-06-29 21:12:16 +00001037 const char *FirstDollar = ScanFormat(Argument, ArgumentEnd, '$');
1038 const char *SecondDollar = ScanFormat(FirstDollar + 1, ArgumentEnd, '$');
Richard Trieu91844232012-06-26 18:18:47 +00001039
1040 // Append before text
Richard Trieuc6058442012-06-29 21:12:16 +00001041 FormatDiagnostic(Argument, FirstDollar, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +00001042
1043 // Append first type
1044 TDT.PrintTree = false;
1045 TDT.PrintFromType = true;
1046 getDiags()->ConvertArgToString(Kind, val,
Craig Topper3aa4fb32014-06-12 05:32:35 +00001047 StringRef(Modifier, ModifierLen),
1048 StringRef(Argument, ArgumentLen),
Craig Toppere4753502014-06-12 05:32:27 +00001049 FormattedArgs,
Richard Trieu91844232012-06-26 18:18:47 +00001050 OutStr, QualTypeVals);
Richard Trieu50f5f462012-07-10 01:46:04 +00001051 if (!TDT.TemplateDiffUsed)
1052 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
1053 TDT.FromType));
1054
Richard Trieu91844232012-06-26 18:18:47 +00001055 // Append middle text
Richard Trieuc6058442012-06-29 21:12:16 +00001056 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +00001057
1058 // Append second type
1059 TDT.PrintFromType = false;
1060 getDiags()->ConvertArgToString(Kind, val,
Craig Topper3aa4fb32014-06-12 05:32:35 +00001061 StringRef(Modifier, ModifierLen),
1062 StringRef(Argument, ArgumentLen),
Craig Toppere4753502014-06-12 05:32:27 +00001063 FormattedArgs,
Richard Trieu91844232012-06-26 18:18:47 +00001064 OutStr, QualTypeVals);
Richard Trieu50f5f462012-07-10 01:46:04 +00001065 if (!TDT.TemplateDiffUsed)
1066 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
1067 TDT.ToType));
1068
Richard Trieu91844232012-06-26 18:18:47 +00001069 // Append end text
Richard Trieuc6058442012-06-29 21:12:16 +00001070 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +00001071 break;
Nico Weber4c311642008-08-10 19:59:06 +00001072 }
Eugene Zelenko25cae5a22018-02-16 23:40:07 +00001073 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001074
Chris Lattnerc243f292009-10-20 05:25:22 +00001075 // Remember this argument info for subsequent formatting operations. Turn
1076 // std::strings into a null terminated string to make it be the same case as
1077 // all the other ones.
Richard Trieu91844232012-06-26 18:18:47 +00001078 if (Kind == DiagnosticsEngine::ak_qualtype_pair)
1079 continue;
1080 else if (Kind != DiagnosticsEngine::ak_std_string)
Chris Lattnerc243f292009-10-20 05:25:22 +00001081 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
1082 else
David Blaikie9c902b52011-09-25 23:23:43 +00001083 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_c_string,
Chris Lattnerc243f292009-10-20 05:25:22 +00001084 (intptr_t)getArgStdStr(ArgNo).c_str()));
Nico Weber4c311642008-08-10 19:59:06 +00001085 }
Richard Trieu91844232012-06-26 18:18:47 +00001086
1087 // Append the type tree to the end of the diagnostics.
1088 OutStr.append(Tree.begin(), Tree.end());
Nico Weber4c311642008-08-10 19:59:06 +00001089}
Ted Kremenekea06ec12009-01-23 20:28:53 +00001090
David Blaikie9c902b52011-09-25 23:23:43 +00001091StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001092 StringRef Message)
Eugene Zelenko25cae5a22018-02-16 23:40:07 +00001093 : ID(ID), Level(Level), Message(Message) {}
Douglas Gregor33cdd812010-02-18 18:08:43 +00001094
Fangrui Song6907ce22018-07-30 19:24:48 +00001095StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level,
David Blaikieb5784322011-09-26 01:18:08 +00001096 const Diagnostic &Info)
Eugene Zelenko25cae5a22018-02-16 23:40:07 +00001097 : ID(Info.getID()), Level(Level) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00001098 assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
1099 "Valid source location without setting a source manager for diagnostic");
1100 if (Info.getLocation().isValid())
1101 Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001102 SmallString<64> Message;
Douglas Gregor33cdd812010-02-18 18:08:43 +00001103 Info.FormatDiagnostic(Message);
1104 this->Message.assign(Message.begin(), Message.end());
Benjamin Kramerf9890422015-02-17 16:48:30 +00001105 this->Ranges.assign(Info.getRanges().begin(), Info.getRanges().end());
1106 this->FixIts.assign(Info.getFixItHints().begin(), Info.getFixItHints().end());
Douglas Gregor33cdd812010-02-18 18:08:43 +00001107}
1108
David Blaikie9c902b52011-09-25 23:23:43 +00001109StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001110 StringRef Message, FullSourceLoc Loc,
Chris Lattner54b16772011-07-23 17:14:25 +00001111 ArrayRef<CharSourceRange> Ranges,
Aaron Ballman234ebd72013-02-24 19:08:10 +00001112 ArrayRef<FixItHint> FixIts)
Eugene Zelenko25cae5a22018-02-16 23:40:07 +00001113 : ID(ID), Level(Level), Loc(Loc), Message(Message),
1114 Ranges(Ranges.begin(), Ranges.end()), FixIts(FixIts.begin(), FixIts.end())
Douglas Gregor925296b2011-07-19 16:10:42 +00001115{
Douglas Gregor925296b2011-07-19 16:10:42 +00001116}
1117
Ted Kremenekea06ec12009-01-23 20:28:53 +00001118/// IncludeInDiagnosticCounts - This method (whose default implementation
1119/// returns true) indicates whether the diagnostics handled by this
David Blaikiee2eefae2011-09-25 23:39:51 +00001120/// DiagnosticConsumer should be included in the number of diagnostics
David Blaikie9c902b52011-09-25 23:23:43 +00001121/// reported by DiagnosticsEngine.
David Blaikiee2eefae2011-09-25 23:39:51 +00001122bool DiagnosticConsumer::IncludeInDiagnosticCounts() const { return true; }
Douglas Gregor89336232010-03-29 23:34:08 +00001123
Eugene Zelenko25cae5a22018-02-16 23:40:07 +00001124void IgnoringDiagConsumer::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00001125
Eugene Zelenko25cae5a22018-02-16 23:40:07 +00001126ForwardingDiagnosticConsumer::~ForwardingDiagnosticConsumer() = default;
Douglas Gregor6b930962013-05-03 22:58:43 +00001127
1128void ForwardingDiagnosticConsumer::HandleDiagnostic(
1129 DiagnosticsEngine::Level DiagLevel,
1130 const Diagnostic &Info) {
1131 Target.HandleDiagnostic(DiagLevel, Info);
1132}
1133
1134void ForwardingDiagnosticConsumer::clear() {
1135 DiagnosticConsumer::clear();
1136 Target.clear();
1137}
1138
1139bool ForwardingDiagnosticConsumer::IncludeInDiagnosticCounts() const {
1140 return Target.IncludeInDiagnosticCounts();
1141}
1142
Benjamin Kramer7ec12c92012-02-07 22:29:24 +00001143PartialDiagnostic::StorageAllocator::StorageAllocator() {
Douglas Gregor89336232010-03-29 23:34:08 +00001144 for (unsigned I = 0; I != NumCached; ++I)
1145 FreeList[I] = Cached + I;
1146 NumFreeListEntries = NumCached;
1147}
1148
Benjamin Kramer7ec12c92012-02-07 22:29:24 +00001149PartialDiagnostic::StorageAllocator::~StorageAllocator() {
Chad Rosier849a67b2012-02-07 23:24:49 +00001150 // Don't assert if we are in a CrashRecovery context, as this invariant may
1151 // be invalidated during a crash.
Justin Lebarbf16db12016-08-10 01:09:07 +00001152 assert((NumFreeListEntries == NumCached ||
1153 llvm::CrashRecoveryContext::isRecoveringFromCrash()) &&
1154 "A partial is on the lam");
Douglas Gregor89336232010-03-29 23:34:08 +00001155}
Alex Lorenzd0e27262017-08-25 15:48:00 +00001156
1157char DiagnosticError::ID;