blob: c82f74413ec147f175e07f6d3f99d23d40de6fc8 [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
David Blaikie9c902b52011-09-25 23:23:43 +000064static void DummyArgToStringFn(DiagnosticsEngine::ArgumentKind AK, intptr_t QT,
Craig Topper3aa4fb32014-06-12 05:32:35 +000065 StringRef Modifier, StringRef Argument,
Craig Toppere4753502014-06-12 05:32:27 +000066 ArrayRef<DiagnosticsEngine::ArgumentValue> PrevArgs,
67 SmallVectorImpl<char> &Output,
68 void *Cookie,
69 ArrayRef<intptr_t> QualTypeVals) {
70 StringRef Str = "<can't format argument>";
71 Output.append(Str.begin(), Str.end());
Chris Lattner6a2ed6f2008-11-23 09:13:29 +000072}
73
Nico Weber8321ad92018-01-17 02:55:27 +000074DiagnosticsEngine::DiagnosticsEngine(
75 IntrusiveRefCntPtr<DiagnosticIDs> diags,
76 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, DiagnosticConsumer *client,
77 bool ShouldOwnClient)
Eugene Zelenko25cae5a22018-02-16 23:40:07 +000078 : Diags(std::move(diags)), DiagOpts(std::move(DiagOpts)) {
Alexander Kornienko41c247a2014-11-17 23:46:02 +000079 setClient(client, ShouldOwnClient);
Chris Lattner63ecc502008-11-23 09:21:17 +000080 ArgToStringFn = DummyArgToStringFn;
Douglas Gregor0e119552010-07-31 00:40:00 +000081
Douglas Gregoraa21cc42010-07-19 21:46:24 +000082 Reset();
Chris Lattnerae411572006-07-05 00:55:08 +000083}
84
Reid Klecknerdccbabf2014-12-17 20:23:11 +000085DiagnosticsEngine::~DiagnosticsEngine() {
86 // If we own the diagnostic client, destroy it first so that it can access the
87 // engine from its destructor.
88 setClient(nullptr);
89}
90
Fangrui Song2f553202018-12-01 01:43:05 +000091void DiagnosticsEngine::dump() const {
92 DiagStatesByLoc.dump(*SourceMgr);
93}
94
95void DiagnosticsEngine::dump(StringRef DiagName) const {
96 DiagStatesByLoc.dump(*SourceMgr, DiagName);
97}
98
David Blaikiee2eefae2011-09-25 23:39:51 +000099void DiagnosticsEngine::setClient(DiagnosticConsumer *client,
David Blaikie9c902b52011-09-25 23:23:43 +0000100 bool ShouldOwnClient) {
Alexander Kornienko41c247a2014-11-17 23:46:02 +0000101 Owner.reset(ShouldOwnClient ? client : nullptr);
Douglas Gregor7a964ad2011-01-31 22:04:05 +0000102 Client = client;
Douglas Gregor7a964ad2011-01-31 22:04:05 +0000103}
Chris Lattnerfb42a182009-07-12 21:18:45 +0000104
David Blaikie9c902b52011-09-25 23:23:43 +0000105void DiagnosticsEngine::pushMappings(SourceLocation Loc) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000106 DiagStateOnPushStack.push_back(GetCurDiagState());
Chris Lattnerfb42a182009-07-12 21:18:45 +0000107}
108
David Blaikie9c902b52011-09-25 23:23:43 +0000109bool DiagnosticsEngine::popMappings(SourceLocation Loc) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000110 if (DiagStateOnPushStack.empty())
Chris Lattnerfb42a182009-07-12 21:18:45 +0000111 return false;
112
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000113 if (DiagStateOnPushStack.back() != GetCurDiagState()) {
114 // State changed at some point between push/pop.
115 PushDiagStatePoint(DiagStateOnPushStack.back(), Loc);
116 }
117 DiagStateOnPushStack.pop_back();
Chris Lattnerfb42a182009-07-12 21:18:45 +0000118 return true;
119}
120
David Blaikie9c902b52011-09-25 23:23:43 +0000121void DiagnosticsEngine::Reset() {
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000122 ErrorOccurred = false;
DeLesley Hutchins8ecd4912012-12-07 22:53:48 +0000123 UncompilableErrorOccurred = false;
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000124 FatalErrorOccurred = false;
Douglas Gregor8a60bbe2011-07-06 17:40:26 +0000125 UnrecoverableErrorOccurred = false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000126
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000127 NumWarnings = 0;
128 NumErrors = 0;
Argyrios Kyrtzidis1fa8b4b2011-07-29 01:25:44 +0000129 TrapNumErrorsOccurred = 0;
130 TrapNumUnrecoverableErrorsOccurred = 0;
Fangrui Song6907ce22018-07-30 19:24:48 +0000131
Eugene Zelenko25cae5a22018-02-16 23:40:07 +0000132 CurDiagID = std::numeric_limits<unsigned>::max();
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000133 LastDiagLevel = DiagnosticIDs::Ignored;
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000134 DelayedDiagID = 0;
Argyrios Kyrtzidisbbbeea12011-03-26 18:58:17 +0000135
136 // Clear state related to #pragma diagnostic.
137 DiagStates.clear();
Richard Smithd230de22017-01-26 01:01:01 +0000138 DiagStatesByLoc.clear();
Argyrios Kyrtzidisbbbeea12011-03-26 18:58:17 +0000139 DiagStateOnPushStack.clear();
140
141 // Create a DiagState and DiagStatePoint representing diagnostic changes
142 // through command-line.
Benjamin Kramer3204b152015-05-29 19:42:19 +0000143 DiagStates.emplace_back();
Richard Smithd230de22017-01-26 01:01:01 +0000144 DiagStatesByLoc.appendFirst(&DiagStates.back());
Douglas Gregoraa21cc42010-07-19 21:46:24 +0000145}
Chris Lattner22eb9722006-06-18 05:43:12 +0000146
David Blaikie9c902b52011-09-25 23:23:43 +0000147void DiagnosticsEngine::SetDelayedDiagnostic(unsigned DiagID, StringRef Arg1,
Chad Rosier849a67b2012-02-07 23:24:49 +0000148 StringRef Arg2) {
Douglas Gregor85795312010-03-22 15:10:57 +0000149 if (DelayedDiagID)
150 return;
151
152 DelayedDiagID = DiagID;
Douglas Gregor96380982010-03-22 15:47:45 +0000153 DelayedDiagArg1 = Arg1.str();
154 DelayedDiagArg2 = Arg2.str();
Douglas Gregor85795312010-03-22 15:10:57 +0000155}
156
David Blaikie9c902b52011-09-25 23:23:43 +0000157void DiagnosticsEngine::ReportDelayed() {
Alex Lorenzce4518f2017-05-04 13:56:51 +0000158 unsigned ID = DelayedDiagID;
Douglas Gregor85795312010-03-22 15:10:57 +0000159 DelayedDiagID = 0;
Alex Lorenzce4518f2017-05-04 13:56:51 +0000160 Report(ID) << DelayedDiagArg1 << DelayedDiagArg2;
Douglas Gregor85795312010-03-22 15:10:57 +0000161}
162
Eugene Zelenko25cae5a22018-02-16 23:40:07 +0000163void DiagnosticsEngine::DiagStateMap::appendFirst(DiagState *State) {
Richard Smithd230de22017-01-26 01:01:01 +0000164 assert(Files.empty() && "not first");
165 FirstDiagState = CurDiagState = State;
166 CurDiagStateLoc = SourceLocation();
Benjamin Kramerbc9ef592017-01-18 15:50:26 +0000167}
168
Richard Smithd230de22017-01-26 01:01:01 +0000169void DiagnosticsEngine::DiagStateMap::append(SourceManager &SrcMgr,
170 SourceLocation Loc,
171 DiagState *State) {
172 CurDiagState = State;
173 CurDiagStateLoc = Loc;
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000174
Richard Smithd230de22017-01-26 01:01:01 +0000175 std::pair<FileID, unsigned> Decomp = SrcMgr.getDecomposedLoc(Loc);
176 unsigned Offset = Decomp.second;
177 for (File *F = getFile(SrcMgr, Decomp.first); F;
178 Offset = F->ParentOffset, F = F->Parent) {
179 F->HasLocalTransitions = true;
180 auto &Last = F->StateTransitions.back();
181 assert(Last.Offset <= Offset && "state transitions added out of order");
Richard Smith99eff012012-08-17 00:55:32 +0000182
Richard Smithd230de22017-01-26 01:01:01 +0000183 if (Last.Offset == Offset) {
184 if (Last.State == State)
185 break;
186 Last.State = State;
187 continue;
188 }
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000189
Richard Smithd230de22017-01-26 01:01:01 +0000190 F->StateTransitions.push_back({State, Offset});
191 }
192}
193
194DiagnosticsEngine::DiagState *
195DiagnosticsEngine::DiagStateMap::lookup(SourceManager &SrcMgr,
196 SourceLocation Loc) const {
197 // Common case: we have not seen any diagnostic pragmas.
198 if (Files.empty())
199 return FirstDiagState;
200
201 std::pair<FileID, unsigned> Decomp = SrcMgr.getDecomposedLoc(Loc);
202 const File *F = getFile(SrcMgr, Decomp.first);
203 return F->lookup(Decomp.second);
204}
205
206DiagnosticsEngine::DiagState *
207DiagnosticsEngine::DiagStateMap::File::lookup(unsigned Offset) const {
Fangrui Song7264a472019-07-03 08:13:17 +0000208 auto OnePastIt =
209 llvm::partition_point(StateTransitions, [=](const DiagStatePoint &P) {
210 return P.Offset <= Offset;
Richard Smithd230de22017-01-26 01:01:01 +0000211 });
212 assert(OnePastIt != StateTransitions.begin() && "missing initial state");
213 return OnePastIt[-1].State;
214}
215
216DiagnosticsEngine::DiagStateMap::File *
217DiagnosticsEngine::DiagStateMap::getFile(SourceManager &SrcMgr,
218 FileID ID) const {
219 // Get or insert the File for this ID.
220 auto Range = Files.equal_range(ID);
221 if (Range.first != Range.second)
222 return &Range.first->second;
223 auto &F = Files.insert(Range.first, std::make_pair(ID, File()))->second;
224
225 // We created a new File; look up the diagnostic state at the start of it and
226 // initialize it.
227 if (ID.isValid()) {
228 std::pair<FileID, unsigned> Decomp = SrcMgr.getDecomposedIncludedLoc(ID);
229 F.Parent = getFile(SrcMgr, Decomp.first);
230 F.ParentOffset = Decomp.second;
231 F.StateTransitions.push_back({F.Parent->lookup(Decomp.second), 0});
232 } else {
233 // This is the (imaginary) root file into which we pretend all top-level
234 // files are included; it descends from the initial state.
235 //
236 // FIXME: This doesn't guarantee that we use the same ordering as
237 // isBeforeInTranslationUnit in the cases where someone invented another
238 // top-level file and added diagnostic pragmas to it. See the code at the
239 // end of isBeforeInTranslationUnit for the quirks it deals with.
240 F.StateTransitions.push_back({FirstDiagState, 0});
241 }
242 return &F;
243}
244
Richard Smith6c2b5a82018-02-09 01:15:13 +0000245void DiagnosticsEngine::DiagStateMap::dump(SourceManager &SrcMgr,
246 StringRef DiagName) const {
247 llvm::errs() << "diagnostic state at ";
Stephen Kelly3124ce72018-08-15 20:32:06 +0000248 CurDiagStateLoc.print(llvm::errs(), SrcMgr);
Richard Smith6c2b5a82018-02-09 01:15:13 +0000249 llvm::errs() << ": " << CurDiagState << "\n";
250
251 for (auto &F : Files) {
252 FileID ID = F.first;
253 File &File = F.second;
254
255 bool PrintedOuterHeading = false;
256 auto PrintOuterHeading = [&] {
257 if (PrintedOuterHeading) return;
258 PrintedOuterHeading = true;
259
260 llvm::errs() << "File " << &File << " <FileID " << ID.getHashValue()
261 << ">: " << SrcMgr.getBuffer(ID)->getBufferIdentifier();
262 if (F.second.Parent) {
263 std::pair<FileID, unsigned> Decomp =
264 SrcMgr.getDecomposedIncludedLoc(ID);
265 assert(File.ParentOffset == Decomp.second);
266 llvm::errs() << " parent " << File.Parent << " <FileID "
267 << Decomp.first.getHashValue() << "> ";
268 SrcMgr.getLocForStartOfFile(Decomp.first)
269 .getLocWithOffset(Decomp.second)
Stephen Kelly3124ce72018-08-15 20:32:06 +0000270 .print(llvm::errs(), SrcMgr);
Richard Smith6c2b5a82018-02-09 01:15:13 +0000271 }
272 if (File.HasLocalTransitions)
273 llvm::errs() << " has_local_transitions";
274 llvm::errs() << "\n";
275 };
276
277 if (DiagName.empty())
278 PrintOuterHeading();
279
280 for (DiagStatePoint &Transition : File.StateTransitions) {
281 bool PrintedInnerHeading = false;
282 auto PrintInnerHeading = [&] {
283 if (PrintedInnerHeading) return;
284 PrintedInnerHeading = true;
285
286 PrintOuterHeading();
287 llvm::errs() << " ";
288 SrcMgr.getLocForStartOfFile(ID)
289 .getLocWithOffset(Transition.Offset)
Stephen Kelly3124ce72018-08-15 20:32:06 +0000290 .print(llvm::errs(), SrcMgr);
Richard Smith6c2b5a82018-02-09 01:15:13 +0000291 llvm::errs() << ": state " << Transition.State << ":\n";
292 };
293
294 if (DiagName.empty())
295 PrintInnerHeading();
296
297 for (auto &Mapping : *Transition.State) {
298 StringRef Option =
299 DiagnosticIDs::getWarningOptionForDiag(Mapping.first);
300 if (!DiagName.empty() && DiagName != Option)
301 continue;
302
303 PrintInnerHeading();
304 llvm::errs() << " ";
305 if (Option.empty())
306 llvm::errs() << "<unknown " << Mapping.first << ">";
307 else
308 llvm::errs() << Option;
309 llvm::errs() << ": ";
310
311 switch (Mapping.second.getSeverity()) {
312 case diag::Severity::Ignored: llvm::errs() << "ignored"; break;
313 case diag::Severity::Remark: llvm::errs() << "remark"; break;
314 case diag::Severity::Warning: llvm::errs() << "warning"; break;
315 case diag::Severity::Error: llvm::errs() << "error"; break;
316 case diag::Severity::Fatal: llvm::errs() << "fatal"; break;
317 }
318
319 if (!Mapping.second.isUser())
320 llvm::errs() << " default";
321 if (Mapping.second.isPragma())
322 llvm::errs() << " pragma";
323 if (Mapping.second.hasNoWarningAsError())
324 llvm::errs() << " no-error";
325 if (Mapping.second.hasNoErrorAsFatal())
326 llvm::errs() << " no-fatal";
327 if (Mapping.second.wasUpgradedFromWarning())
328 llvm::errs() << " overruled";
329 llvm::errs() << "\n";
330 }
331 }
332 }
333}
334
Richard Smithd230de22017-01-26 01:01:01 +0000335void DiagnosticsEngine::PushDiagStatePoint(DiagState *State,
336 SourceLocation Loc) {
337 assert(Loc.isValid() && "Adding invalid loc point");
338 DiagStatesByLoc.append(*SourceMgr, Loc, State);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000339}
340
Alp Tokerd576e002014-06-12 11:13:52 +0000341void DiagnosticsEngine::setSeverity(diag::kind Diag, diag::Severity Map,
342 SourceLocation L) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000343 assert(Diag < diag::DIAG_UPPER_LIMIT &&
344 "Can only map builtin diagnostics");
345 assert((Diags->isBuiltinWarningOrExtension(Diag) ||
Alp Toker46df1c02014-06-12 10:15:20 +0000346 (Map == diag::Severity::Fatal || Map == diag::Severity::Error)) &&
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000347 "Cannot map errors into warnings!");
Richard Smith8a0527d2012-08-14 22:37:22 +0000348 assert((L.isInvalid() || SourceMgr) && "No SourceMgr for valid location");
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000349
Chad Rosierd1956e42012-02-03 01:49:51 +0000350 // Don't allow a mapping to a warning override an error/fatal mapping.
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +0000351 bool WasUpgradedFromWarning = false;
Alp Toker46df1c02014-06-12 10:15:20 +0000352 if (Map == diag::Severity::Warning) {
Alp Tokerc726c362014-06-10 09:31:37 +0000353 DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag);
Alp Toker46df1c02014-06-12 10:15:20 +0000354 if (Info.getSeverity() == diag::Severity::Error ||
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +0000355 Info.getSeverity() == diag::Severity::Fatal) {
Alp Tokerc726c362014-06-10 09:31:37 +0000356 Map = Info.getSeverity();
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +0000357 WasUpgradedFromWarning = true;
358 }
Chad Rosierd1956e42012-02-03 01:49:51 +0000359 }
Alp Tokerc726c362014-06-10 09:31:37 +0000360 DiagnosticMapping Mapping = makeUserMapping(Map, L);
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +0000361 Mapping.setUpgradedFromWarning(WasUpgradedFromWarning);
Daniel Dunbar2fba0972011-10-04 21:17:24 +0000362
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000363 // Common case; setting all the diagnostics of a group in one place.
Richard Smithd230de22017-01-26 01:01:01 +0000364 if ((L.isInvalid() || L == DiagStatesByLoc.getCurDiagStateLoc()) &&
365 DiagStatesByLoc.getCurDiagState()) {
366 // FIXME: This is theoretically wrong: if the current state is shared with
367 // some other location (via push/pop) we will change the state for that
368 // other location as well. This cannot currently happen, as we can't update
369 // the diagnostic state at the same location at which we pop.
370 DiagStatesByLoc.getCurDiagState()->setMapping(Diag, Mapping);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000371 return;
372 }
373
Richard Smithd230de22017-01-26 01:01:01 +0000374 // A diagnostic pragma occurred, create a new DiagState initialized with
375 // the current one and a new DiagStatePoint to record at which location
376 // the new state became active.
377 DiagStates.push_back(*GetCurDiagState());
378 DiagStates.back().setMapping(Diag, Mapping);
379 PushDiagStatePoint(&DiagStates.back(), L);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000380}
381
Richard Smith3be1cb22014-08-07 00:24:21 +0000382bool DiagnosticsEngine::setSeverityForGroup(diag::Flavor Flavor,
383 StringRef Group, diag::Severity Map,
Alp Tokerd576e002014-06-12 11:13:52 +0000384 SourceLocation Loc) {
Daniel Dunbard908c122011-09-29 01:47:16 +0000385 // Get the diagnostics in this group.
Hans Wennborgeb7cd662014-08-11 16:05:54 +0000386 SmallVector<diag::kind, 256> GroupDiags;
Richard Smith3be1cb22014-08-07 00:24:21 +0000387 if (Diags->getDiagnosticsInGroup(Flavor, Group, GroupDiags))
Daniel Dunbard908c122011-09-29 01:47:16 +0000388 return true;
389
390 // Set the mapping.
Hans Wennborgeb7cd662014-08-11 16:05:54 +0000391 for (diag::kind Diag : GroupDiags)
392 setSeverity(Diag, Map, Loc);
Daniel Dunbard908c122011-09-29 01:47:16 +0000393
394 return false;
395}
396
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000397bool DiagnosticsEngine::setDiagnosticGroupWarningAsError(StringRef Group,
398 bool Enabled) {
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000399 // If we are enabling this feature, just set the diagnostic mappings to map to
400 // errors.
401 if (Enabled)
Richard Smith3be1cb22014-08-07 00:24:21 +0000402 return setSeverityForGroup(diag::Flavor::WarningOrError, Group,
403 diag::Severity::Error);
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000404
405 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
406 // potentially downgrade anything already mapped to be a warning.
407
408 // Get the diagnostics in this group.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000409 SmallVector<diag::kind, 8> GroupDiags;
Richard Smith3be1cb22014-08-07 00:24:21 +0000410 if (Diags->getDiagnosticsInGroup(diag::Flavor::WarningOrError, Group,
411 GroupDiags))
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000412 return true;
413
414 // Perform the mapping change.
Craig Toppera52e2b22015-11-26 05:10:07 +0000415 for (diag::kind Diag : GroupDiags) {
416 DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag);
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000417
Alp Toker46df1c02014-06-12 10:15:20 +0000418 if (Info.getSeverity() == diag::Severity::Error ||
419 Info.getSeverity() == diag::Severity::Fatal)
420 Info.setSeverity(diag::Severity::Warning);
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000421
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000422 Info.setNoWarningAsError(true);
423 }
424
425 return false;
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000426}
427
428bool DiagnosticsEngine::setDiagnosticGroupErrorAsFatal(StringRef Group,
429 bool Enabled) {
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000430 // If we are enabling this feature, just set the diagnostic mappings to map to
431 // fatal errors.
432 if (Enabled)
Richard Smith3be1cb22014-08-07 00:24:21 +0000433 return setSeverityForGroup(diag::Flavor::WarningOrError, Group,
434 diag::Severity::Fatal);
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000435
Richard Smithe37391c2017-05-03 00:28:49 +0000436 // Otherwise, we want to set the diagnostic mapping's "no Wfatal-errors" bit,
437 // and potentially downgrade anything already mapped to be a fatal error.
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000438
439 // Get the diagnostics in this group.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000440 SmallVector<diag::kind, 8> GroupDiags;
Richard Smith3be1cb22014-08-07 00:24:21 +0000441 if (Diags->getDiagnosticsInGroup(diag::Flavor::WarningOrError, Group,
442 GroupDiags))
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000443 return true;
444
445 // Perform the mapping change.
Craig Toppera52e2b22015-11-26 05:10:07 +0000446 for (diag::kind Diag : GroupDiags) {
447 DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag);
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000448
Alp Toker46df1c02014-06-12 10:15:20 +0000449 if (Info.getSeverity() == diag::Severity::Fatal)
450 Info.setSeverity(diag::Severity::Error);
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000451
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000452 Info.setNoErrorAsFatal(true);
453 }
454
455 return false;
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000456}
457
Richard Smith3be1cb22014-08-07 00:24:21 +0000458void DiagnosticsEngine::setSeverityForAll(diag::Flavor Flavor,
459 diag::Severity Map,
Alp Tokerd576e002014-06-12 11:13:52 +0000460 SourceLocation Loc) {
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000461 // Get all the diagnostics.
Gabor Horvath53b5c132017-12-20 16:55:41 +0000462 std::vector<diag::kind> AllDiags;
Gabor Horvath328d3af2017-11-14 12:14:49 +0000463 DiagnosticIDs::getAllDiagnostics(Flavor, AllDiags);
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000464
465 // Set the mapping.
Craig Toppera52e2b22015-11-26 05:10:07 +0000466 for (diag::kind Diag : AllDiags)
467 if (Diags->isBuiltinWarningOrExtension(Diag))
468 setSeverity(Diag, Map, Loc);
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000469}
470
David Blaikie9c902b52011-09-25 23:23:43 +0000471void DiagnosticsEngine::Report(const StoredDiagnostic &storedDiag) {
Eugene Zelenko25cae5a22018-02-16 23:40:07 +0000472 assert(CurDiagID == std::numeric_limits<unsigned>::max() &&
473 "Multiple diagnostics in flight at once!");
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000474
475 CurDiagLoc = storedDiag.getLocation();
476 CurDiagID = storedDiag.getID();
477 NumDiagArgs = 0;
478
Alexander Kornienkod3b4e082014-05-22 19:56:11 +0000479 DiagRanges.clear();
Benjamin Kramerf367dd92015-06-12 15:31:50 +0000480 DiagRanges.append(storedDiag.range_begin(), storedDiag.range_end());
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000481
Alexander Kornienkod3b4e082014-05-22 19:56:11 +0000482 DiagFixItHints.clear();
Benjamin Kramerf367dd92015-06-12 15:31:50 +0000483 DiagFixItHints.append(storedDiag.fixit_begin(), storedDiag.fixit_end());
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000484
David Blaikiee2eefae2011-09-25 23:39:51 +0000485 assert(Client && "DiagnosticConsumer not set!");
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000486 Level DiagLevel = storedDiag.getLevel();
David Blaikieb5784322011-09-26 01:18:08 +0000487 Diagnostic Info(this, storedDiag.getMessage());
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000488 Client->HandleDiagnostic(DiagLevel, Info);
489 if (Client->IncludeInDiagnosticCounts()) {
David Blaikie9c902b52011-09-25 23:23:43 +0000490 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000491 ++NumWarnings;
492 }
493
Eugene Zelenko25cae5a22018-02-16 23:40:07 +0000494 CurDiagID = std::numeric_limits<unsigned>::max();
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000495}
496
Jordan Rose6f524ac2012-07-11 16:50:36 +0000497bool DiagnosticsEngine::EmitCurrentDiagnostic(bool Force) {
498 assert(getClient() && "DiagnosticClient not set!");
499
500 bool Emitted;
501 if (Force) {
502 Diagnostic Info(this);
503
504 // Figure out the diagnostic level of this message.
505 DiagnosticIDs::Level DiagLevel
506 = Diags->getDiagnosticLevel(Info.getID(), Info.getLocation(), *this);
507
508 Emitted = (DiagLevel != DiagnosticIDs::Ignored);
509 if (Emitted) {
510 // Emit the diagnostic regardless of suppression level.
511 Diags->EmitDiag(*this, DiagLevel);
512 }
513 } else {
514 // Process the diagnostic, sending the accumulated information to the
515 // DiagnosticConsumer.
516 Emitted = ProcessDiag();
517 }
Douglas Gregor85795312010-03-22 15:10:57 +0000518
519 // Clear out the current diagnostic object.
Daniel Dunbarc7c00892012-03-13 21:02:14 +0000520 Clear();
Douglas Gregor85795312010-03-22 15:10:57 +0000521
522 // If there was a delayed diagnostic, emit it now.
Alex Lorenzce4518f2017-05-04 13:56:51 +0000523 if (!Force && DelayedDiagID)
Daniel Dunbarc7c00892012-03-13 21:02:14 +0000524 ReportDelayed();
Douglas Gregor85795312010-03-22 15:10:57 +0000525
526 return Emitted;
527}
528
Eugene Zelenko25cae5a22018-02-16 23:40:07 +0000529DiagnosticConsumer::~DiagnosticConsumer() = default;
Nico Weber4c311642008-08-10 19:59:06 +0000530
David Blaikiee2eefae2011-09-25 23:39:51 +0000531void DiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
David Blaikieb5784322011-09-26 01:18:08 +0000532 const Diagnostic &Info) {
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000533 if (!IncludeInDiagnosticCounts())
534 return;
535
David Blaikie9c902b52011-09-25 23:23:43 +0000536 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000537 ++NumWarnings;
David Blaikie9c902b52011-09-25 23:23:43 +0000538 else if (DiagLevel >= DiagnosticsEngine::Error)
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000539 ++NumErrors;
540}
Chris Lattner23be0672008-11-19 06:51:40 +0000541
Chris Lattner2b786902008-11-21 07:50:02 +0000542/// ModifierIs - Return true if the specified modifier matches specified string.
543template <std::size_t StrLen>
544static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
545 const char (&Str)[StrLen]) {
Eugene Zelenko25cae5a22018-02-16 23:40:07 +0000546 return StrLen-1 == ModifierLen && memcmp(Modifier, Str, StrLen-1) == 0;
Chris Lattner2b786902008-11-21 07:50:02 +0000547}
548
John McCall8cb7a8a32010-01-14 20:11:39 +0000549/// ScanForward - Scans forward, looking for the given character, skipping
550/// nested clauses and escaped characters.
551static const char *ScanFormat(const char *I, const char *E, char Target) {
552 unsigned Depth = 0;
553
554 for ( ; I != E; ++I) {
555 if (Depth == 0 && *I == Target) return I;
556 if (Depth != 0 && *I == '}') Depth--;
557
558 if (*I == '%') {
559 I++;
560 if (I == E) break;
561
562 // Escaped characters get implicitly skipped here.
563
564 // Format specifier.
Jordan Rosea7d03842013-02-08 22:30:41 +0000565 if (!isDigit(*I) && !isPunctuation(*I)) {
566 for (I++; I != E && !isDigit(*I) && *I != '{'; I++) ;
John McCall8cb7a8a32010-01-14 20:11:39 +0000567 if (I == E) break;
568 if (*I == '{')
569 Depth++;
570 }
571 }
572 }
573 return E;
574}
575
Chris Lattner2b786902008-11-21 07:50:02 +0000576/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
577/// like this: %select{foo|bar|baz}2. This means that the integer argument
578/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
579/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
580/// This is very useful for certain classes of variant diagnostics.
David Blaikieb5784322011-09-26 01:18:08 +0000581static void HandleSelectModifier(const Diagnostic &DInfo, unsigned ValNo,
Chris Lattner2b786902008-11-21 07:50:02 +0000582 const char *Argument, unsigned ArgumentLen,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000583 SmallVectorImpl<char> &OutStr) {
Chris Lattner2b786902008-11-21 07:50:02 +0000584 const char *ArgumentEnd = Argument+ArgumentLen;
Mike Stump11289f42009-09-09 15:08:12 +0000585
Chris Lattner2b786902008-11-21 07:50:02 +0000586 // Skip over 'ValNo' |'s.
587 while (ValNo) {
John McCall8cb7a8a32010-01-14 20:11:39 +0000588 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
Chris Lattner2b786902008-11-21 07:50:02 +0000589 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
590 " larger than the number of options in the diagnostic string!");
591 Argument = NextVal+1; // Skip this string.
592 --ValNo;
593 }
Mike Stump11289f42009-09-09 15:08:12 +0000594
Chris Lattner2b786902008-11-21 07:50:02 +0000595 // Get the end of the value. This is either the } or the |.
John McCall8cb7a8a32010-01-14 20:11:39 +0000596 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
John McCalle4d54322010-01-13 23:58:20 +0000597
598 // Recursively format the result of the select clause into the output string.
599 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000600}
601
602/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
603/// letter 's' to the string if the value is not 1. This is used in cases like
604/// this: "you idiot, you have %4 parameter%s4!".
605static void HandleIntegerSModifier(unsigned ValNo,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000606 SmallVectorImpl<char> &OutStr) {
Chris Lattner2b786902008-11-21 07:50:02 +0000607 if (ValNo != 1)
608 OutStr.push_back('s');
609}
610
John McCall9015cde2010-01-14 00:50:32 +0000611/// HandleOrdinalModifier - Handle the integer 'ord' modifier. This
612/// prints the ordinal form of the given integer, with 1 corresponding
613/// to the first ordinal. Currently this is hard-coded to use the
614/// English form.
615static void HandleOrdinalModifier(unsigned ValNo,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000616 SmallVectorImpl<char> &OutStr) {
John McCall9015cde2010-01-14 00:50:32 +0000617 assert(ValNo != 0 && "ValNo must be strictly positive!");
618
619 llvm::raw_svector_ostream Out(OutStr);
620
621 // We could use text forms for the first N ordinals, but the numeric
622 // forms are actually nicer in diagnostics because they stand out.
Jordan Rosec102b352012-09-22 01:24:42 +0000623 Out << ValNo << llvm::getOrdinalSuffix(ValNo);
John McCall9015cde2010-01-14 00:50:32 +0000624}
625
Sebastian Redl15b02d22008-11-22 13:44:36 +0000626/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000627static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000628 // Programming 101: Parse a decimal number :-)
629 unsigned Val = 0;
630 while (Start != End && *Start >= '0' && *Start <= '9') {
631 Val *= 10;
632 Val += *Start - '0';
633 ++Start;
634 }
635 return Val;
636}
637
638/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000639static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000640 if (*Start != '[') {
641 unsigned Ref = PluralNumber(Start, End);
642 return Ref == Val;
643 }
644
645 ++Start;
646 unsigned Low = PluralNumber(Start, End);
647 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
648 ++Start;
649 unsigned High = PluralNumber(Start, End);
650 assert(*Start == ']' && "Bad plural expression syntax: expected )");
651 ++Start;
652 return Low <= Val && Val <= High;
653}
654
655/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattner2fe29202009-04-15 17:13:42 +0000656static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000657 // Empty condition?
658 if (*Start == ':')
659 return true;
660
Eugene Zelenko25cae5a22018-02-16 23:40:07 +0000661 while (true) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000662 char C = *Start;
663 if (C == '%') {
664 // Modulo expression
665 ++Start;
666 unsigned Arg = PluralNumber(Start, End);
667 assert(*Start == '=' && "Bad plural expression syntax: expected =");
668 ++Start;
669 unsigned ValMod = ValNo % Arg;
670 if (TestPluralRange(ValMod, Start, End))
671 return true;
672 } else {
Sebastian Redl3ceaf622008-11-27 07:28:14 +0000673 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redl15b02d22008-11-22 13:44:36 +0000674 "Bad plural expression syntax: unexpected character");
675 // Range expression
676 if (TestPluralRange(ValNo, Start, End))
677 return true;
678 }
679
680 // Scan for next or-expr part.
681 Start = std::find(Start, End, ',');
Mike Stump11289f42009-09-09 15:08:12 +0000682 if (Start == End)
Sebastian Redl15b02d22008-11-22 13:44:36 +0000683 break;
684 ++Start;
685 }
686 return false;
687}
688
689/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
690/// for complex plural forms, or in languages where all plurals are complex.
691/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
692/// conditions that are tested in order, the form corresponding to the first
693/// that applies being emitted. The empty condition is always true, making the
694/// last form a default case.
695/// Conditions are simple boolean expressions, where n is the number argument.
696/// Here are the rules.
697/// condition := expression | empty
698/// empty := -> always true
699/// expression := numeric [',' expression] -> logical or
700/// numeric := range -> true if n in range
701/// | '%' number '=' range -> true if n % number in range
702/// range := number
703/// | '[' number ',' number ']' -> ranges are inclusive both ends
704///
705/// Here are some examples from the GNU gettext manual written in this form:
706/// English:
707/// {1:form0|:form1}
708/// Latvian:
709/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
710/// Gaeilge:
711/// {1:form0|2:form1|:form2}
712/// Romanian:
713/// {1:form0|0,%100=[1,19]:form1|:form2}
714/// Lithuanian:
715/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
716/// Russian (requires repeated form):
717/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
718/// Slovak
719/// {1:form0|[2,4]:form1|:form2}
720/// Polish (requires repeated form):
721/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
David Blaikieb5784322011-09-26 01:18:08 +0000722static void HandlePluralModifier(const Diagnostic &DInfo, unsigned ValNo,
Sebastian Redl15b02d22008-11-22 13:44:36 +0000723 const char *Argument, unsigned ArgumentLen,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000724 SmallVectorImpl<char> &OutStr) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000725 const char *ArgumentEnd = Argument + ArgumentLen;
Eugene Zelenko25cae5a22018-02-16 23:40:07 +0000726 while (true) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000727 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
728 const char *ExprEnd = Argument;
729 while (*ExprEnd != ':') {
730 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
731 ++ExprEnd;
732 }
733 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
734 Argument = ExprEnd + 1;
John McCall8cb7a8a32010-01-14 20:11:39 +0000735 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
John McCall43b61682010-10-14 01:55:31 +0000736
737 // Recursively format the result of the plural clause into the
738 // output string.
739 DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000740 return;
741 }
John McCall8cb7a8a32010-01-14 20:11:39 +0000742 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
Sebastian Redl15b02d22008-11-22 13:44:36 +0000743 }
744}
745
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000746/// Returns the friendly description for a token kind that will appear
Alp Tokera231ad22014-01-06 12:54:18 +0000747/// without quotes in diagnostic messages. These strings may be translatable in
748/// future.
749static const char *getTokenDescForDiagnostic(tok::TokenKind Kind) {
Alp Tokerec543272013-12-24 09:48:30 +0000750 switch (Kind) {
751 case tok::identifier:
752 return "identifier";
753 default:
Craig Topperf1186c52014-05-08 06:41:40 +0000754 return nullptr;
Alp Tokerec543272013-12-24 09:48:30 +0000755 }
756}
Sebastian Redl15b02d22008-11-22 13:44:36 +0000757
Chris Lattner23be0672008-11-19 06:51:40 +0000758/// FormatDiagnostic - Format this diagnostic into a string, substituting the
759/// formal arguments into the %0 slots. The result is appended onto the Str
760/// array.
David Blaikieb5784322011-09-26 01:18:08 +0000761void Diagnostic::
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000762FormatDiagnostic(SmallVectorImpl<char> &OutStr) const {
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000763 if (!StoredDiagMessage.empty()) {
764 OutStr.append(StoredDiagMessage.begin(), StoredDiagMessage.end());
765 return;
766 }
767
Fangrui Song6907ce22018-07-30 19:24:48 +0000768 StringRef Diag =
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000769 getDiags()->getDiagnosticIDs()->getDescription(getID());
Mike Stump11289f42009-09-09 15:08:12 +0000770
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000771 FormatDiagnostic(Diag.begin(), Diag.end(), OutStr);
John McCalle4d54322010-01-13 23:58:20 +0000772}
773
David Blaikieb5784322011-09-26 01:18:08 +0000774void Diagnostic::
John McCalle4d54322010-01-13 23:58:20 +0000775FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000776 SmallVectorImpl<char> &OutStr) const {
Richard Trieub3b8bb02015-01-08 01:27:03 +0000777 // When the diagnostic string is only "%0", the entire string is being given
778 // by an outside source. Remove unprintable characters from this string
779 // and skip all the other string processing.
Richard Trieudcd7bb02015-01-17 00:56:10 +0000780 if (DiagEnd - DiagStr == 2 &&
781 StringRef(DiagStr, DiagEnd - DiagStr).equals("%0") &&
Richard Trieub3b8bb02015-01-08 01:27:03 +0000782 getArgKind(0) == DiagnosticsEngine::ak_std_string) {
783 const std::string &S = getArgStdStr(0);
784 for (char c : S) {
785 if (llvm::sys::locale::isPrint(c) || c == '\t') {
786 OutStr.push_back(c);
787 }
788 }
789 return;
790 }
791
Chris Lattnerc243f292009-10-20 05:25:22 +0000792 /// FormattedArgs - Keep track of all of the arguments formatted by
793 /// ConvertArgToString and pass them into subsequent calls to
794 /// ConvertArgToString, allowing the implementation to avoid redundancies in
795 /// obvious cases.
David Blaikie9c902b52011-09-25 23:23:43 +0000796 SmallVector<DiagnosticsEngine::ArgumentValue, 8> FormattedArgs;
Chandler Carruthd5173952011-07-11 17:49:21 +0000797
798 /// QualTypeVals - Pass a vector of arrays so that QualType names can be
799 /// compared to see if more information is needed to be printed.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000800 SmallVector<intptr_t, 2> QualTypeVals;
Richard Trieu91844232012-06-26 18:18:47 +0000801 SmallVector<char, 64> Tree;
802
Chandler Carruthd5173952011-07-11 17:49:21 +0000803 for (unsigned i = 0, e = getNumArgs(); i < e; ++i)
David Blaikie9c902b52011-09-25 23:23:43 +0000804 if (getArgKind(i) == DiagnosticsEngine::ak_qualtype)
Chandler Carruthd5173952011-07-11 17:49:21 +0000805 QualTypeVals.push_back(getRawArg(i));
806
Chris Lattner23be0672008-11-19 06:51:40 +0000807 while (DiagStr != DiagEnd) {
808 if (DiagStr[0] != '%') {
809 // Append non-%0 substrings to Str if we have one.
810 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
811 OutStr.append(DiagStr, StrEnd);
812 DiagStr = StrEnd;
Chris Lattner2b786902008-11-21 07:50:02 +0000813 continue;
Jordan Rosea7d03842013-02-08 22:30:41 +0000814 } else if (isPunctuation(DiagStr[1])) {
John McCall8cb7a8a32010-01-14 20:11:39 +0000815 OutStr.push_back(DiagStr[1]); // %% -> %.
Chris Lattner23be0672008-11-19 06:51:40 +0000816 DiagStr += 2;
Chris Lattner2b786902008-11-21 07:50:02 +0000817 continue;
818 }
Mike Stump11289f42009-09-09 15:08:12 +0000819
Chris Lattner2b786902008-11-21 07:50:02 +0000820 // Skip the %.
821 ++DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000822
Chris Lattner2b786902008-11-21 07:50:02 +0000823 // This must be a placeholder for a diagnostic argument. The format for a
824 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
825 // The digit is a number from 0-9 indicating which argument this comes from.
826 // The modifier is a string of digits from the set [-a-z]+, arguments is a
827 // brace enclosed string.
Craig Topperf1186c52014-05-08 06:41:40 +0000828 const char *Modifier = nullptr, *Argument = nullptr;
Chris Lattner2b786902008-11-21 07:50:02 +0000829 unsigned ModifierLen = 0, ArgumentLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000830
Chris Lattner2b786902008-11-21 07:50:02 +0000831 // Check to see if we have a modifier. If so eat it.
Jordan Rosea7d03842013-02-08 22:30:41 +0000832 if (!isDigit(DiagStr[0])) {
Chris Lattner2b786902008-11-21 07:50:02 +0000833 Modifier = DiagStr;
834 while (DiagStr[0] == '-' ||
835 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
836 ++DiagStr;
837 ModifierLen = DiagStr-Modifier;
Chris Lattner23be0672008-11-19 06:51:40 +0000838
Chris Lattner2b786902008-11-21 07:50:02 +0000839 // If we have an argument, get it next.
840 if (DiagStr[0] == '{') {
841 ++DiagStr; // Skip {.
842 Argument = DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000843
John McCall8cb7a8a32010-01-14 20:11:39 +0000844 DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
845 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
Chris Lattner2b786902008-11-21 07:50:02 +0000846 ArgumentLen = DiagStr-Argument;
847 ++DiagStr; // Skip }.
Chris Lattner23be0672008-11-19 06:51:40 +0000848 }
Chris Lattner2b786902008-11-21 07:50:02 +0000849 }
Mike Stump11289f42009-09-09 15:08:12 +0000850
Jordan Rosea7d03842013-02-08 22:30:41 +0000851 assert(isDigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000852 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattner2b786902008-11-21 07:50:02 +0000853
Richard Trieu91844232012-06-26 18:18:47 +0000854 // Only used for type diffing.
855 unsigned ArgNo2 = ArgNo;
856
David Blaikie9c902b52011-09-25 23:23:43 +0000857 DiagnosticsEngine::ArgumentKind Kind = getArgKind(ArgNo);
Richard Trieu90c31f52013-01-30 20:04:31 +0000858 if (ModifierIs(Modifier, ModifierLen, "diff")) {
Jordan Rosea7d03842013-02-08 22:30:41 +0000859 assert(*DiagStr == ',' && isDigit(*(DiagStr + 1)) &&
Richard Trieu91844232012-06-26 18:18:47 +0000860 "Invalid format for diff modifier");
861 ++DiagStr; // Comma.
862 ArgNo2 = *DiagStr++ - '0';
Richard Trieu90c31f52013-01-30 20:04:31 +0000863 DiagnosticsEngine::ArgumentKind Kind2 = getArgKind(ArgNo2);
864 if (Kind == DiagnosticsEngine::ak_qualtype &&
865 Kind2 == DiagnosticsEngine::ak_qualtype)
866 Kind = DiagnosticsEngine::ak_qualtype_pair;
867 else {
868 // %diff only supports QualTypes. For other kinds of arguments,
869 // use the default printing. For example, if the modifier is:
870 // "%diff{compare $ to $|other text}1,2"
871 // treat it as:
872 // "compare %1 to %2"
Chandler Carruth8df65e42016-12-23 05:19:47 +0000873 const char *ArgumentEnd = Argument + ArgumentLen;
874 const char *Pipe = ScanFormat(Argument, ArgumentEnd, '|');
875 assert(ScanFormat(Pipe + 1, ArgumentEnd, '|') == ArgumentEnd &&
876 "Found too many '|'s in a %diff modifier!");
Richard Trieu90c31f52013-01-30 20:04:31 +0000877 const char *FirstDollar = ScanFormat(Argument, Pipe, '$');
878 const char *SecondDollar = ScanFormat(FirstDollar + 1, Pipe, '$');
Filipe Cabecinhased4a00c2013-01-30 22:03:24 +0000879 const char ArgStr1[] = { '%', static_cast<char>('0' + ArgNo) };
880 const char ArgStr2[] = { '%', static_cast<char>('0' + ArgNo2) };
Richard Trieu90c31f52013-01-30 20:04:31 +0000881 FormatDiagnostic(Argument, FirstDollar, OutStr);
882 FormatDiagnostic(ArgStr1, ArgStr1 + 2, OutStr);
883 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
884 FormatDiagnostic(ArgStr2, ArgStr2 + 2, OutStr);
885 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
886 continue;
887 }
Richard Trieu91844232012-06-26 18:18:47 +0000888 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000889
Chris Lattnerc243f292009-10-20 05:25:22 +0000890 switch (Kind) {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000891 // ---- STRINGS ----
David Blaikie9c902b52011-09-25 23:23:43 +0000892 case DiagnosticsEngine::ak_std_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000893 const std::string &S = getArgStdStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000894 assert(ModifierLen == 0 && "No modifiers for strings yet");
895 OutStr.append(S.begin(), S.end());
896 break;
897 }
David Blaikie9c902b52011-09-25 23:23:43 +0000898 case DiagnosticsEngine::ak_c_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000899 const char *S = getArgCStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000900 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000901
902 // Don't crash if get passed a null pointer by accident.
903 if (!S)
904 S = "(null)";
Mike Stump11289f42009-09-09 15:08:12 +0000905
Chris Lattner2b786902008-11-21 07:50:02 +0000906 OutStr.append(S, S + strlen(S));
907 break;
908 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000909 // ---- INTEGERS ----
David Blaikie9c902b52011-09-25 23:23:43 +0000910 case DiagnosticsEngine::ak_sint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000911 int Val = getArgSInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000912
Chris Lattner2b786902008-11-21 07:50:02 +0000913 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCall43b61682010-10-14 01:55:31 +0000914 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen,
915 OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000916 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
917 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000918 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCall43b61682010-10-14 01:55:31 +0000919 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
920 OutStr);
John McCall9015cde2010-01-14 00:50:32 +0000921 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
922 HandleOrdinalModifier((unsigned)Val, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000923 } else {
924 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000925 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000926 }
Chris Lattner2b786902008-11-21 07:50:02 +0000927 break;
928 }
David Blaikie9c902b52011-09-25 23:23:43 +0000929 case DiagnosticsEngine::ak_uint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000930 unsigned Val = getArgUInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000931
Chris Lattner2b786902008-11-21 07:50:02 +0000932 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCalle4d54322010-01-13 23:58:20 +0000933 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000934 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
935 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000936 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCall43b61682010-10-14 01:55:31 +0000937 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
938 OutStr);
John McCall9015cde2010-01-14 00:50:32 +0000939 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
940 HandleOrdinalModifier(Val, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000941 } else {
942 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000943 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000944 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000945 break;
Chris Lattner2b786902008-11-21 07:50:02 +0000946 }
Alp Tokerec543272013-12-24 09:48:30 +0000947 // ---- TOKEN SPELLINGS ----
948 case DiagnosticsEngine::ak_tokenkind: {
949 tok::TokenKind Kind = static_cast<tok::TokenKind>(getRawArg(ArgNo));
950 assert(ModifierLen == 0 && "No modifiers for token kinds yet");
951
952 llvm::raw_svector_ostream Out(OutStr);
Alp Tokera231ad22014-01-06 12:54:18 +0000953 if (const char *S = tok::getPunctuatorSpelling(Kind))
954 // Quoted token spelling for punctuators.
955 Out << '\'' << S << '\'';
956 else if (const char *S = tok::getKeywordSpelling(Kind))
957 // Unquoted token spelling for keywords.
958 Out << S;
959 else if (const char *S = getTokenDescForDiagnostic(Kind))
Alp Tokerec543272013-12-24 09:48:30 +0000960 // Unquoted translatable token name.
961 Out << S;
Alp Tokerec543272013-12-24 09:48:30 +0000962 else if (const char *S = tok::getTokenName(Kind))
963 // Debug name, shouldn't appear in user-facing diagnostics.
964 Out << '<' << S << '>';
965 else
966 Out << "(null)";
967 break;
968 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000969 // ---- NAMES and TYPES ----
David Blaikie9c902b52011-09-25 23:23:43 +0000970 case DiagnosticsEngine::ak_identifierinfo: {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000971 const IdentifierInfo *II = getArgIdentifier(ArgNo);
972 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000973
974 // Don't crash if get passed a null pointer by accident.
975 if (!II) {
976 const char *S = "(null)";
977 OutStr.append(S, S + strlen(S));
978 continue;
979 }
980
Daniel Dunbar07d07852009-10-18 21:17:35 +0000981 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
Chris Lattnere3d20d92008-11-23 21:45:46 +0000982 break;
983 }
Anastasia Stulova4cebc9d2019-01-04 11:50:36 +0000984 case DiagnosticsEngine::ak_qual:
David Blaikie9c902b52011-09-25 23:23:43 +0000985 case DiagnosticsEngine::ak_qualtype:
986 case DiagnosticsEngine::ak_declarationname:
987 case DiagnosticsEngine::ak_nameddecl:
988 case DiagnosticsEngine::ak_nestednamespec:
989 case DiagnosticsEngine::ak_declcontext:
Aaron Ballman3e424b52013-12-26 18:30:57 +0000990 case DiagnosticsEngine::ak_attr:
Chris Lattnerc243f292009-10-20 05:25:22 +0000991 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
Craig Topper3aa4fb32014-06-12 05:32:35 +0000992 StringRef(Modifier, ModifierLen),
993 StringRef(Argument, ArgumentLen),
Craig Toppere4753502014-06-12 05:32:27 +0000994 FormattedArgs,
Chandler Carruthd5173952011-07-11 17:49:21 +0000995 OutStr, QualTypeVals);
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000996 break;
Eugene Zelenko25cae5a22018-02-16 23:40:07 +0000997 case DiagnosticsEngine::ak_qualtype_pair: {
Richard Trieu91844232012-06-26 18:18:47 +0000998 // Create a struct with all the info needed for printing.
999 TemplateDiffTypes TDT;
1000 TDT.FromType = getRawArg(ArgNo);
1001 TDT.ToType = getRawArg(ArgNo2);
1002 TDT.ElideType = getDiags()->ElideType;
1003 TDT.ShowColors = getDiags()->ShowColors;
Richard Trieu50f5f462012-07-10 01:46:04 +00001004 TDT.TemplateDiffUsed = false;
Richard Trieu91844232012-06-26 18:18:47 +00001005 intptr_t val = reinterpret_cast<intptr_t>(&TDT);
1006
Richard Trieuc6058442012-06-29 21:12:16 +00001007 const char *ArgumentEnd = Argument + ArgumentLen;
1008 const char *Pipe = ScanFormat(Argument, ArgumentEnd, '|');
1009
Richard Trieua4056002012-07-13 21:18:32 +00001010 // Print the tree. If this diagnostic already has a tree, skip the
1011 // second tree.
1012 if (getDiags()->PrintTemplateTree && Tree.empty()) {
Richard Trieu91844232012-06-26 18:18:47 +00001013 TDT.PrintFromType = true;
1014 TDT.PrintTree = true;
1015 getDiags()->ConvertArgToString(Kind, val,
Craig Topper3aa4fb32014-06-12 05:32:35 +00001016 StringRef(Modifier, ModifierLen),
1017 StringRef(Argument, ArgumentLen),
Craig Toppere4753502014-06-12 05:32:27 +00001018 FormattedArgs,
Richard Trieu91844232012-06-26 18:18:47 +00001019 Tree, QualTypeVals);
1020 // If there is no tree information, fall back to regular printing.
Richard Trieuc6058442012-06-29 21:12:16 +00001021 if (!Tree.empty()) {
1022 FormatDiagnostic(Pipe + 1, ArgumentEnd, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +00001023 break;
Richard Trieuc6058442012-06-29 21:12:16 +00001024 }
Richard Trieu91844232012-06-26 18:18:47 +00001025 }
1026
1027 // Non-tree printing, also the fall-back when tree printing fails.
1028 // The fall-back is triggered when the types compared are not templates.
Richard Trieuc6058442012-06-29 21:12:16 +00001029 const char *FirstDollar = ScanFormat(Argument, ArgumentEnd, '$');
1030 const char *SecondDollar = ScanFormat(FirstDollar + 1, ArgumentEnd, '$');
Richard Trieu91844232012-06-26 18:18:47 +00001031
1032 // Append before text
Richard Trieuc6058442012-06-29 21:12:16 +00001033 FormatDiagnostic(Argument, FirstDollar, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +00001034
1035 // Append first type
1036 TDT.PrintTree = false;
1037 TDT.PrintFromType = true;
1038 getDiags()->ConvertArgToString(Kind, val,
Craig Topper3aa4fb32014-06-12 05:32:35 +00001039 StringRef(Modifier, ModifierLen),
1040 StringRef(Argument, ArgumentLen),
Craig Toppere4753502014-06-12 05:32:27 +00001041 FormattedArgs,
Richard Trieu91844232012-06-26 18:18:47 +00001042 OutStr, QualTypeVals);
Richard Trieu50f5f462012-07-10 01:46:04 +00001043 if (!TDT.TemplateDiffUsed)
1044 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
1045 TDT.FromType));
1046
Richard Trieu91844232012-06-26 18:18:47 +00001047 // Append middle text
Richard Trieuc6058442012-06-29 21:12:16 +00001048 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +00001049
1050 // Append second type
1051 TDT.PrintFromType = false;
1052 getDiags()->ConvertArgToString(Kind, val,
Craig Topper3aa4fb32014-06-12 05:32:35 +00001053 StringRef(Modifier, ModifierLen),
1054 StringRef(Argument, ArgumentLen),
Craig Toppere4753502014-06-12 05:32:27 +00001055 FormattedArgs,
Richard Trieu91844232012-06-26 18:18:47 +00001056 OutStr, QualTypeVals);
Richard Trieu50f5f462012-07-10 01:46:04 +00001057 if (!TDT.TemplateDiffUsed)
1058 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
1059 TDT.ToType));
1060
Richard Trieu91844232012-06-26 18:18:47 +00001061 // Append end text
Richard Trieuc6058442012-06-29 21:12:16 +00001062 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +00001063 break;
Nico Weber4c311642008-08-10 19:59:06 +00001064 }
Eugene Zelenko25cae5a22018-02-16 23:40:07 +00001065 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001066
Chris Lattnerc243f292009-10-20 05:25:22 +00001067 // Remember this argument info for subsequent formatting operations. Turn
1068 // std::strings into a null terminated string to make it be the same case as
1069 // all the other ones.
Richard Trieu91844232012-06-26 18:18:47 +00001070 if (Kind == DiagnosticsEngine::ak_qualtype_pair)
1071 continue;
1072 else if (Kind != DiagnosticsEngine::ak_std_string)
Chris Lattnerc243f292009-10-20 05:25:22 +00001073 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
1074 else
David Blaikie9c902b52011-09-25 23:23:43 +00001075 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_c_string,
Chris Lattnerc243f292009-10-20 05:25:22 +00001076 (intptr_t)getArgStdStr(ArgNo).c_str()));
Nico Weber4c311642008-08-10 19:59:06 +00001077 }
Richard Trieu91844232012-06-26 18:18:47 +00001078
1079 // Append the type tree to the end of the diagnostics.
1080 OutStr.append(Tree.begin(), Tree.end());
Nico Weber4c311642008-08-10 19:59:06 +00001081}
Ted Kremenekea06ec12009-01-23 20:28:53 +00001082
David Blaikie9c902b52011-09-25 23:23:43 +00001083StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001084 StringRef Message)
Eugene Zelenko25cae5a22018-02-16 23:40:07 +00001085 : ID(ID), Level(Level), Message(Message) {}
Douglas Gregor33cdd812010-02-18 18:08:43 +00001086
Fangrui Song6907ce22018-07-30 19:24:48 +00001087StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level,
David Blaikieb5784322011-09-26 01:18:08 +00001088 const Diagnostic &Info)
Eugene Zelenko25cae5a22018-02-16 23:40:07 +00001089 : ID(Info.getID()), Level(Level) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00001090 assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
1091 "Valid source location without setting a source manager for diagnostic");
1092 if (Info.getLocation().isValid())
1093 Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001094 SmallString<64> Message;
Douglas Gregor33cdd812010-02-18 18:08:43 +00001095 Info.FormatDiagnostic(Message);
1096 this->Message.assign(Message.begin(), Message.end());
Benjamin Kramerf9890422015-02-17 16:48:30 +00001097 this->Ranges.assign(Info.getRanges().begin(), Info.getRanges().end());
1098 this->FixIts.assign(Info.getFixItHints().begin(), Info.getFixItHints().end());
Douglas Gregor33cdd812010-02-18 18:08:43 +00001099}
1100
David Blaikie9c902b52011-09-25 23:23:43 +00001101StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001102 StringRef Message, FullSourceLoc Loc,
Chris Lattner54b16772011-07-23 17:14:25 +00001103 ArrayRef<CharSourceRange> Ranges,
Aaron Ballman234ebd72013-02-24 19:08:10 +00001104 ArrayRef<FixItHint> FixIts)
Eugene Zelenko25cae5a22018-02-16 23:40:07 +00001105 : ID(ID), Level(Level), Loc(Loc), Message(Message),
1106 Ranges(Ranges.begin(), Ranges.end()), FixIts(FixIts.begin(), FixIts.end())
Douglas Gregor925296b2011-07-19 16:10:42 +00001107{
Douglas Gregor925296b2011-07-19 16:10:42 +00001108}
1109
Ted Kremenekea06ec12009-01-23 20:28:53 +00001110/// IncludeInDiagnosticCounts - This method (whose default implementation
1111/// returns true) indicates whether the diagnostics handled by this
David Blaikiee2eefae2011-09-25 23:39:51 +00001112/// DiagnosticConsumer should be included in the number of diagnostics
David Blaikie9c902b52011-09-25 23:23:43 +00001113/// reported by DiagnosticsEngine.
David Blaikiee2eefae2011-09-25 23:39:51 +00001114bool DiagnosticConsumer::IncludeInDiagnosticCounts() const { return true; }
Douglas Gregor89336232010-03-29 23:34:08 +00001115
Eugene Zelenko25cae5a22018-02-16 23:40:07 +00001116void IgnoringDiagConsumer::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00001117
Eugene Zelenko25cae5a22018-02-16 23:40:07 +00001118ForwardingDiagnosticConsumer::~ForwardingDiagnosticConsumer() = default;
Douglas Gregor6b930962013-05-03 22:58:43 +00001119
1120void ForwardingDiagnosticConsumer::HandleDiagnostic(
1121 DiagnosticsEngine::Level DiagLevel,
1122 const Diagnostic &Info) {
1123 Target.HandleDiagnostic(DiagLevel, Info);
1124}
1125
1126void ForwardingDiagnosticConsumer::clear() {
1127 DiagnosticConsumer::clear();
1128 Target.clear();
1129}
1130
1131bool ForwardingDiagnosticConsumer::IncludeInDiagnosticCounts() const {
1132 return Target.IncludeInDiagnosticCounts();
1133}
1134
Benjamin Kramer7ec12c92012-02-07 22:29:24 +00001135PartialDiagnostic::StorageAllocator::StorageAllocator() {
Douglas Gregor89336232010-03-29 23:34:08 +00001136 for (unsigned I = 0; I != NumCached; ++I)
1137 FreeList[I] = Cached + I;
1138 NumFreeListEntries = NumCached;
1139}
1140
Benjamin Kramer7ec12c92012-02-07 22:29:24 +00001141PartialDiagnostic::StorageAllocator::~StorageAllocator() {
Chad Rosier849a67b2012-02-07 23:24:49 +00001142 // Don't assert if we are in a CrashRecovery context, as this invariant may
1143 // be invalidated during a crash.
Justin Lebarbf16db12016-08-10 01:09:07 +00001144 assert((NumFreeListEntries == NumCached ||
1145 llvm::CrashRecoveryContext::isRecoveringFromCrash()) &&
1146 "A partial is on the lam");
Douglas Gregor89336232010-03-29 23:34:08 +00001147}
Alex Lorenzd0e27262017-08-25 15:48:00 +00001148
1149char DiagnosticError::ID;