blob: c95681496a8c05d57ab12eef1cc62cea8460cdcb [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 {
208 auto OnePastIt = std::upper_bound(
209 StateTransitions.begin(), StateTransitions.end(), Offset,
210 [](unsigned Offset, const DiagStatePoint &P) {
211 return Offset < P.Offset;
212 });
213 assert(OnePastIt != StateTransitions.begin() && "missing initial state");
214 return OnePastIt[-1].State;
215}
216
217DiagnosticsEngine::DiagStateMap::File *
218DiagnosticsEngine::DiagStateMap::getFile(SourceManager &SrcMgr,
219 FileID ID) const {
220 // Get or insert the File for this ID.
221 auto Range = Files.equal_range(ID);
222 if (Range.first != Range.second)
223 return &Range.first->second;
224 auto &F = Files.insert(Range.first, std::make_pair(ID, File()))->second;
225
226 // We created a new File; look up the diagnostic state at the start of it and
227 // initialize it.
228 if (ID.isValid()) {
229 std::pair<FileID, unsigned> Decomp = SrcMgr.getDecomposedIncludedLoc(ID);
230 F.Parent = getFile(SrcMgr, Decomp.first);
231 F.ParentOffset = Decomp.second;
232 F.StateTransitions.push_back({F.Parent->lookup(Decomp.second), 0});
233 } else {
234 // This is the (imaginary) root file into which we pretend all top-level
235 // files are included; it descends from the initial state.
236 //
237 // FIXME: This doesn't guarantee that we use the same ordering as
238 // isBeforeInTranslationUnit in the cases where someone invented another
239 // top-level file and added diagnostic pragmas to it. See the code at the
240 // end of isBeforeInTranslationUnit for the quirks it deals with.
241 F.StateTransitions.push_back({FirstDiagState, 0});
242 }
243 return &F;
244}
245
Richard Smith6c2b5a82018-02-09 01:15:13 +0000246void DiagnosticsEngine::DiagStateMap::dump(SourceManager &SrcMgr,
247 StringRef DiagName) const {
248 llvm::errs() << "diagnostic state at ";
Stephen Kelly3124ce72018-08-15 20:32:06 +0000249 CurDiagStateLoc.print(llvm::errs(), SrcMgr);
Richard Smith6c2b5a82018-02-09 01:15:13 +0000250 llvm::errs() << ": " << CurDiagState << "\n";
251
252 for (auto &F : Files) {
253 FileID ID = F.first;
254 File &File = F.second;
255
256 bool PrintedOuterHeading = false;
257 auto PrintOuterHeading = [&] {
258 if (PrintedOuterHeading) return;
259 PrintedOuterHeading = true;
260
261 llvm::errs() << "File " << &File << " <FileID " << ID.getHashValue()
262 << ">: " << SrcMgr.getBuffer(ID)->getBufferIdentifier();
263 if (F.second.Parent) {
264 std::pair<FileID, unsigned> Decomp =
265 SrcMgr.getDecomposedIncludedLoc(ID);
266 assert(File.ParentOffset == Decomp.second);
267 llvm::errs() << " parent " << File.Parent << " <FileID "
268 << Decomp.first.getHashValue() << "> ";
269 SrcMgr.getLocForStartOfFile(Decomp.first)
270 .getLocWithOffset(Decomp.second)
Stephen Kelly3124ce72018-08-15 20:32:06 +0000271 .print(llvm::errs(), SrcMgr);
Richard Smith6c2b5a82018-02-09 01:15:13 +0000272 }
273 if (File.HasLocalTransitions)
274 llvm::errs() << " has_local_transitions";
275 llvm::errs() << "\n";
276 };
277
278 if (DiagName.empty())
279 PrintOuterHeading();
280
281 for (DiagStatePoint &Transition : File.StateTransitions) {
282 bool PrintedInnerHeading = false;
283 auto PrintInnerHeading = [&] {
284 if (PrintedInnerHeading) return;
285 PrintedInnerHeading = true;
286
287 PrintOuterHeading();
288 llvm::errs() << " ";
289 SrcMgr.getLocForStartOfFile(ID)
290 .getLocWithOffset(Transition.Offset)
Stephen Kelly3124ce72018-08-15 20:32:06 +0000291 .print(llvm::errs(), SrcMgr);
Richard Smith6c2b5a82018-02-09 01:15:13 +0000292 llvm::errs() << ": state " << Transition.State << ":\n";
293 };
294
295 if (DiagName.empty())
296 PrintInnerHeading();
297
298 for (auto &Mapping : *Transition.State) {
299 StringRef Option =
300 DiagnosticIDs::getWarningOptionForDiag(Mapping.first);
301 if (!DiagName.empty() && DiagName != Option)
302 continue;
303
304 PrintInnerHeading();
305 llvm::errs() << " ";
306 if (Option.empty())
307 llvm::errs() << "<unknown " << Mapping.first << ">";
308 else
309 llvm::errs() << Option;
310 llvm::errs() << ": ";
311
312 switch (Mapping.second.getSeverity()) {
313 case diag::Severity::Ignored: llvm::errs() << "ignored"; break;
314 case diag::Severity::Remark: llvm::errs() << "remark"; break;
315 case diag::Severity::Warning: llvm::errs() << "warning"; break;
316 case diag::Severity::Error: llvm::errs() << "error"; break;
317 case diag::Severity::Fatal: llvm::errs() << "fatal"; break;
318 }
319
320 if (!Mapping.second.isUser())
321 llvm::errs() << " default";
322 if (Mapping.second.isPragma())
323 llvm::errs() << " pragma";
324 if (Mapping.second.hasNoWarningAsError())
325 llvm::errs() << " no-error";
326 if (Mapping.second.hasNoErrorAsFatal())
327 llvm::errs() << " no-fatal";
328 if (Mapping.second.wasUpgradedFromWarning())
329 llvm::errs() << " overruled";
330 llvm::errs() << "\n";
331 }
332 }
333 }
334}
335
Richard Smithd230de22017-01-26 01:01:01 +0000336void DiagnosticsEngine::PushDiagStatePoint(DiagState *State,
337 SourceLocation Loc) {
338 assert(Loc.isValid() && "Adding invalid loc point");
339 DiagStatesByLoc.append(*SourceMgr, Loc, State);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000340}
341
Alp Tokerd576e002014-06-12 11:13:52 +0000342void DiagnosticsEngine::setSeverity(diag::kind Diag, diag::Severity Map,
343 SourceLocation L) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000344 assert(Diag < diag::DIAG_UPPER_LIMIT &&
345 "Can only map builtin diagnostics");
346 assert((Diags->isBuiltinWarningOrExtension(Diag) ||
Alp Toker46df1c02014-06-12 10:15:20 +0000347 (Map == diag::Severity::Fatal || Map == diag::Severity::Error)) &&
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000348 "Cannot map errors into warnings!");
Richard Smith8a0527d2012-08-14 22:37:22 +0000349 assert((L.isInvalid() || SourceMgr) && "No SourceMgr for valid location");
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000350
Chad Rosierd1956e42012-02-03 01:49:51 +0000351 // Don't allow a mapping to a warning override an error/fatal mapping.
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +0000352 bool WasUpgradedFromWarning = false;
Alp Toker46df1c02014-06-12 10:15:20 +0000353 if (Map == diag::Severity::Warning) {
Alp Tokerc726c362014-06-10 09:31:37 +0000354 DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag);
Alp Toker46df1c02014-06-12 10:15:20 +0000355 if (Info.getSeverity() == diag::Severity::Error ||
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +0000356 Info.getSeverity() == diag::Severity::Fatal) {
Alp Tokerc726c362014-06-10 09:31:37 +0000357 Map = Info.getSeverity();
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +0000358 WasUpgradedFromWarning = true;
359 }
Chad Rosierd1956e42012-02-03 01:49:51 +0000360 }
Alp Tokerc726c362014-06-10 09:31:37 +0000361 DiagnosticMapping Mapping = makeUserMapping(Map, L);
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +0000362 Mapping.setUpgradedFromWarning(WasUpgradedFromWarning);
Daniel Dunbar2fba0972011-10-04 21:17:24 +0000363
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000364 // Common case; setting all the diagnostics of a group in one place.
Richard Smithd230de22017-01-26 01:01:01 +0000365 if ((L.isInvalid() || L == DiagStatesByLoc.getCurDiagStateLoc()) &&
366 DiagStatesByLoc.getCurDiagState()) {
367 // FIXME: This is theoretically wrong: if the current state is shared with
368 // some other location (via push/pop) we will change the state for that
369 // other location as well. This cannot currently happen, as we can't update
370 // the diagnostic state at the same location at which we pop.
371 DiagStatesByLoc.getCurDiagState()->setMapping(Diag, Mapping);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000372 return;
373 }
374
Richard Smithd230de22017-01-26 01:01:01 +0000375 // A diagnostic pragma occurred, create a new DiagState initialized with
376 // the current one and a new DiagStatePoint to record at which location
377 // the new state became active.
378 DiagStates.push_back(*GetCurDiagState());
379 DiagStates.back().setMapping(Diag, Mapping);
380 PushDiagStatePoint(&DiagStates.back(), L);
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +0000381}
382
Richard Smith3be1cb22014-08-07 00:24:21 +0000383bool DiagnosticsEngine::setSeverityForGroup(diag::Flavor Flavor,
384 StringRef Group, diag::Severity Map,
Alp Tokerd576e002014-06-12 11:13:52 +0000385 SourceLocation Loc) {
Daniel Dunbard908c122011-09-29 01:47:16 +0000386 // Get the diagnostics in this group.
Hans Wennborgeb7cd662014-08-11 16:05:54 +0000387 SmallVector<diag::kind, 256> GroupDiags;
Richard Smith3be1cb22014-08-07 00:24:21 +0000388 if (Diags->getDiagnosticsInGroup(Flavor, Group, GroupDiags))
Daniel Dunbard908c122011-09-29 01:47:16 +0000389 return true;
390
391 // Set the mapping.
Hans Wennborgeb7cd662014-08-11 16:05:54 +0000392 for (diag::kind Diag : GroupDiags)
393 setSeverity(Diag, Map, Loc);
Daniel Dunbard908c122011-09-29 01:47:16 +0000394
395 return false;
396}
397
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000398bool DiagnosticsEngine::setDiagnosticGroupWarningAsError(StringRef Group,
399 bool Enabled) {
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000400 // If we are enabling this feature, just set the diagnostic mappings to map to
401 // errors.
402 if (Enabled)
Richard Smith3be1cb22014-08-07 00:24:21 +0000403 return setSeverityForGroup(diag::Flavor::WarningOrError, Group,
404 diag::Severity::Error);
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000405
406 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
407 // potentially downgrade anything already mapped to be a warning.
408
409 // Get the diagnostics in this group.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000410 SmallVector<diag::kind, 8> GroupDiags;
Richard Smith3be1cb22014-08-07 00:24:21 +0000411 if (Diags->getDiagnosticsInGroup(diag::Flavor::WarningOrError, Group,
412 GroupDiags))
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000413 return true;
414
415 // Perform the mapping change.
Craig Toppera52e2b22015-11-26 05:10:07 +0000416 for (diag::kind Diag : GroupDiags) {
417 DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag);
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000418
Alp Toker46df1c02014-06-12 10:15:20 +0000419 if (Info.getSeverity() == diag::Severity::Error ||
420 Info.getSeverity() == diag::Severity::Fatal)
421 Info.setSeverity(diag::Severity::Warning);
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000422
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000423 Info.setNoWarningAsError(true);
424 }
425
426 return false;
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000427}
428
429bool DiagnosticsEngine::setDiagnosticGroupErrorAsFatal(StringRef Group,
430 bool Enabled) {
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000431 // If we are enabling this feature, just set the diagnostic mappings to map to
432 // fatal errors.
433 if (Enabled)
Richard Smith3be1cb22014-08-07 00:24:21 +0000434 return setSeverityForGroup(diag::Flavor::WarningOrError, Group,
435 diag::Severity::Fatal);
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000436
Richard Smithe37391c2017-05-03 00:28:49 +0000437 // Otherwise, we want to set the diagnostic mapping's "no Wfatal-errors" bit,
438 // and potentially downgrade anything already mapped to be a fatal error.
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000439
440 // Get the diagnostics in this group.
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000441 SmallVector<diag::kind, 8> GroupDiags;
Richard Smith3be1cb22014-08-07 00:24:21 +0000442 if (Diags->getDiagnosticsInGroup(diag::Flavor::WarningOrError, Group,
443 GroupDiags))
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000444 return true;
445
446 // Perform the mapping change.
Craig Toppera52e2b22015-11-26 05:10:07 +0000447 for (diag::kind Diag : GroupDiags) {
448 DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag);
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000449
Alp Toker46df1c02014-06-12 10:15:20 +0000450 if (Info.getSeverity() == diag::Severity::Fatal)
451 Info.setSeverity(diag::Severity::Error);
Daniel Dunbar58d0af62011-09-29 01:58:05 +0000452
Daniel Dunbarfffcf212011-09-29 01:52:06 +0000453 Info.setNoErrorAsFatal(true);
454 }
455
456 return false;
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000457}
458
Richard Smith3be1cb22014-08-07 00:24:21 +0000459void DiagnosticsEngine::setSeverityForAll(diag::Flavor Flavor,
460 diag::Severity Map,
Alp Tokerd576e002014-06-12 11:13:52 +0000461 SourceLocation Loc) {
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000462 // Get all the diagnostics.
Gabor Horvath53b5c132017-12-20 16:55:41 +0000463 std::vector<diag::kind> AllDiags;
Gabor Horvath328d3af2017-11-14 12:14:49 +0000464 DiagnosticIDs::getAllDiagnostics(Flavor, AllDiags);
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000465
466 // Set the mapping.
Craig Toppera52e2b22015-11-26 05:10:07 +0000467 for (diag::kind Diag : AllDiags)
468 if (Diags->isBuiltinWarningOrExtension(Diag))
469 setSeverity(Diag, Map, Loc);
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000470}
471
David Blaikie9c902b52011-09-25 23:23:43 +0000472void DiagnosticsEngine::Report(const StoredDiagnostic &storedDiag) {
Eugene Zelenko25cae5a22018-02-16 23:40:07 +0000473 assert(CurDiagID == std::numeric_limits<unsigned>::max() &&
474 "Multiple diagnostics in flight at once!");
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000475
476 CurDiagLoc = storedDiag.getLocation();
477 CurDiagID = storedDiag.getID();
478 NumDiagArgs = 0;
479
Alexander Kornienkod3b4e082014-05-22 19:56:11 +0000480 DiagRanges.clear();
Benjamin Kramerf367dd92015-06-12 15:31:50 +0000481 DiagRanges.append(storedDiag.range_begin(), storedDiag.range_end());
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000482
Alexander Kornienkod3b4e082014-05-22 19:56:11 +0000483 DiagFixItHints.clear();
Benjamin Kramerf367dd92015-06-12 15:31:50 +0000484 DiagFixItHints.append(storedDiag.fixit_begin(), storedDiag.fixit_end());
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000485
David Blaikiee2eefae2011-09-25 23:39:51 +0000486 assert(Client && "DiagnosticConsumer not set!");
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000487 Level DiagLevel = storedDiag.getLevel();
David Blaikieb5784322011-09-26 01:18:08 +0000488 Diagnostic Info(this, storedDiag.getMessage());
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000489 Client->HandleDiagnostic(DiagLevel, Info);
490 if (Client->IncludeInDiagnosticCounts()) {
David Blaikie9c902b52011-09-25 23:23:43 +0000491 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000492 ++NumWarnings;
493 }
494
Eugene Zelenko25cae5a22018-02-16 23:40:07 +0000495 CurDiagID = std::numeric_limits<unsigned>::max();
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000496}
497
Jordan Rose6f524ac2012-07-11 16:50:36 +0000498bool DiagnosticsEngine::EmitCurrentDiagnostic(bool Force) {
499 assert(getClient() && "DiagnosticClient not set!");
500
501 bool Emitted;
502 if (Force) {
503 Diagnostic Info(this);
504
505 // Figure out the diagnostic level of this message.
506 DiagnosticIDs::Level DiagLevel
507 = Diags->getDiagnosticLevel(Info.getID(), Info.getLocation(), *this);
508
509 Emitted = (DiagLevel != DiagnosticIDs::Ignored);
510 if (Emitted) {
511 // Emit the diagnostic regardless of suppression level.
512 Diags->EmitDiag(*this, DiagLevel);
513 }
514 } else {
515 // Process the diagnostic, sending the accumulated information to the
516 // DiagnosticConsumer.
517 Emitted = ProcessDiag();
518 }
Douglas Gregor85795312010-03-22 15:10:57 +0000519
520 // Clear out the current diagnostic object.
Daniel Dunbarc7c00892012-03-13 21:02:14 +0000521 Clear();
Douglas Gregor85795312010-03-22 15:10:57 +0000522
523 // If there was a delayed diagnostic, emit it now.
Alex Lorenzce4518f2017-05-04 13:56:51 +0000524 if (!Force && DelayedDiagID)
Daniel Dunbarc7c00892012-03-13 21:02:14 +0000525 ReportDelayed();
Douglas Gregor85795312010-03-22 15:10:57 +0000526
527 return Emitted;
528}
529
Eugene Zelenko25cae5a22018-02-16 23:40:07 +0000530DiagnosticConsumer::~DiagnosticConsumer() = default;
Nico Weber4c311642008-08-10 19:59:06 +0000531
David Blaikiee2eefae2011-09-25 23:39:51 +0000532void DiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
David Blaikieb5784322011-09-26 01:18:08 +0000533 const Diagnostic &Info) {
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000534 if (!IncludeInDiagnosticCounts())
535 return;
536
David Blaikie9c902b52011-09-25 23:23:43 +0000537 if (DiagLevel == DiagnosticsEngine::Warning)
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000538 ++NumWarnings;
David Blaikie9c902b52011-09-25 23:23:43 +0000539 else if (DiagLevel >= DiagnosticsEngine::Error)
Argyrios Kyrtzidisc79346a2010-11-18 20:06:46 +0000540 ++NumErrors;
541}
Chris Lattner23be0672008-11-19 06:51:40 +0000542
Chris Lattner2b786902008-11-21 07:50:02 +0000543/// ModifierIs - Return true if the specified modifier matches specified string.
544template <std::size_t StrLen>
545static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
546 const char (&Str)[StrLen]) {
Eugene Zelenko25cae5a22018-02-16 23:40:07 +0000547 return StrLen-1 == ModifierLen && memcmp(Modifier, Str, StrLen-1) == 0;
Chris Lattner2b786902008-11-21 07:50:02 +0000548}
549
John McCall8cb7a8a32010-01-14 20:11:39 +0000550/// ScanForward - Scans forward, looking for the given character, skipping
551/// nested clauses and escaped characters.
552static const char *ScanFormat(const char *I, const char *E, char Target) {
553 unsigned Depth = 0;
554
555 for ( ; I != E; ++I) {
556 if (Depth == 0 && *I == Target) return I;
557 if (Depth != 0 && *I == '}') Depth--;
558
559 if (*I == '%') {
560 I++;
561 if (I == E) break;
562
563 // Escaped characters get implicitly skipped here.
564
565 // Format specifier.
Jordan Rosea7d03842013-02-08 22:30:41 +0000566 if (!isDigit(*I) && !isPunctuation(*I)) {
567 for (I++; I != E && !isDigit(*I) && *I != '{'; I++) ;
John McCall8cb7a8a32010-01-14 20:11:39 +0000568 if (I == E) break;
569 if (*I == '{')
570 Depth++;
571 }
572 }
573 }
574 return E;
575}
576
Chris Lattner2b786902008-11-21 07:50:02 +0000577/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
578/// like this: %select{foo|bar|baz}2. This means that the integer argument
579/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
580/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
581/// This is very useful for certain classes of variant diagnostics.
David Blaikieb5784322011-09-26 01:18:08 +0000582static void HandleSelectModifier(const Diagnostic &DInfo, unsigned ValNo,
Chris Lattner2b786902008-11-21 07:50:02 +0000583 const char *Argument, unsigned ArgumentLen,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000584 SmallVectorImpl<char> &OutStr) {
Chris Lattner2b786902008-11-21 07:50:02 +0000585 const char *ArgumentEnd = Argument+ArgumentLen;
Mike Stump11289f42009-09-09 15:08:12 +0000586
Chris Lattner2b786902008-11-21 07:50:02 +0000587 // Skip over 'ValNo' |'s.
588 while (ValNo) {
John McCall8cb7a8a32010-01-14 20:11:39 +0000589 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
Chris Lattner2b786902008-11-21 07:50:02 +0000590 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
591 " larger than the number of options in the diagnostic string!");
592 Argument = NextVal+1; // Skip this string.
593 --ValNo;
594 }
Mike Stump11289f42009-09-09 15:08:12 +0000595
Chris Lattner2b786902008-11-21 07:50:02 +0000596 // Get the end of the value. This is either the } or the |.
John McCall8cb7a8a32010-01-14 20:11:39 +0000597 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
John McCalle4d54322010-01-13 23:58:20 +0000598
599 // Recursively format the result of the select clause into the output string.
600 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000601}
602
603/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
604/// letter 's' to the string if the value is not 1. This is used in cases like
605/// this: "you idiot, you have %4 parameter%s4!".
606static void HandleIntegerSModifier(unsigned ValNo,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000607 SmallVectorImpl<char> &OutStr) {
Chris Lattner2b786902008-11-21 07:50:02 +0000608 if (ValNo != 1)
609 OutStr.push_back('s');
610}
611
John McCall9015cde2010-01-14 00:50:32 +0000612/// HandleOrdinalModifier - Handle the integer 'ord' modifier. This
613/// prints the ordinal form of the given integer, with 1 corresponding
614/// to the first ordinal. Currently this is hard-coded to use the
615/// English form.
616static void HandleOrdinalModifier(unsigned ValNo,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000617 SmallVectorImpl<char> &OutStr) {
John McCall9015cde2010-01-14 00:50:32 +0000618 assert(ValNo != 0 && "ValNo must be strictly positive!");
619
620 llvm::raw_svector_ostream Out(OutStr);
621
622 // We could use text forms for the first N ordinals, but the numeric
623 // forms are actually nicer in diagnostics because they stand out.
Jordan Rosec102b352012-09-22 01:24:42 +0000624 Out << ValNo << llvm::getOrdinalSuffix(ValNo);
John McCall9015cde2010-01-14 00:50:32 +0000625}
626
Sebastian Redl15b02d22008-11-22 13:44:36 +0000627/// PluralNumber - Parse an unsigned integer and advance Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000628static unsigned PluralNumber(const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000629 // Programming 101: Parse a decimal number :-)
630 unsigned Val = 0;
631 while (Start != End && *Start >= '0' && *Start <= '9') {
632 Val *= 10;
633 Val += *Start - '0';
634 ++Start;
635 }
636 return Val;
637}
638
639/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Chris Lattner2fe29202009-04-15 17:13:42 +0000640static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000641 if (*Start != '[') {
642 unsigned Ref = PluralNumber(Start, End);
643 return Ref == Val;
644 }
645
646 ++Start;
647 unsigned Low = PluralNumber(Start, End);
648 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
649 ++Start;
650 unsigned High = PluralNumber(Start, End);
651 assert(*Start == ']' && "Bad plural expression syntax: expected )");
652 ++Start;
653 return Low <= Val && Val <= High;
654}
655
656/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Chris Lattner2fe29202009-04-15 17:13:42 +0000657static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000658 // Empty condition?
659 if (*Start == ':')
660 return true;
661
Eugene Zelenko25cae5a22018-02-16 23:40:07 +0000662 while (true) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000663 char C = *Start;
664 if (C == '%') {
665 // Modulo expression
666 ++Start;
667 unsigned Arg = PluralNumber(Start, End);
668 assert(*Start == '=' && "Bad plural expression syntax: expected =");
669 ++Start;
670 unsigned ValMod = ValNo % Arg;
671 if (TestPluralRange(ValMod, Start, End))
672 return true;
673 } else {
Sebastian Redl3ceaf622008-11-27 07:28:14 +0000674 assert((C == '[' || (C >= '0' && C <= '9')) &&
Sebastian Redl15b02d22008-11-22 13:44:36 +0000675 "Bad plural expression syntax: unexpected character");
676 // Range expression
677 if (TestPluralRange(ValNo, Start, End))
678 return true;
679 }
680
681 // Scan for next or-expr part.
682 Start = std::find(Start, End, ',');
Mike Stump11289f42009-09-09 15:08:12 +0000683 if (Start == End)
Sebastian Redl15b02d22008-11-22 13:44:36 +0000684 break;
685 ++Start;
686 }
687 return false;
688}
689
690/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
691/// for complex plural forms, or in languages where all plurals are complex.
692/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
693/// conditions that are tested in order, the form corresponding to the first
694/// that applies being emitted. The empty condition is always true, making the
695/// last form a default case.
696/// Conditions are simple boolean expressions, where n is the number argument.
697/// Here are the rules.
698/// condition := expression | empty
699/// empty := -> always true
700/// expression := numeric [',' expression] -> logical or
701/// numeric := range -> true if n in range
702/// | '%' number '=' range -> true if n % number in range
703/// range := number
704/// | '[' number ',' number ']' -> ranges are inclusive both ends
705///
706/// Here are some examples from the GNU gettext manual written in this form:
707/// English:
708/// {1:form0|:form1}
709/// Latvian:
710/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
711/// Gaeilge:
712/// {1:form0|2:form1|:form2}
713/// Romanian:
714/// {1:form0|0,%100=[1,19]:form1|:form2}
715/// Lithuanian:
716/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
717/// Russian (requires repeated form):
718/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
719/// Slovak
720/// {1:form0|[2,4]:form1|:form2}
721/// Polish (requires repeated form):
722/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
David Blaikieb5784322011-09-26 01:18:08 +0000723static void HandlePluralModifier(const Diagnostic &DInfo, unsigned ValNo,
Sebastian Redl15b02d22008-11-22 13:44:36 +0000724 const char *Argument, unsigned ArgumentLen,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000725 SmallVectorImpl<char> &OutStr) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000726 const char *ArgumentEnd = Argument + ArgumentLen;
Eugene Zelenko25cae5a22018-02-16 23:40:07 +0000727 while (true) {
Sebastian Redl15b02d22008-11-22 13:44:36 +0000728 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
729 const char *ExprEnd = Argument;
730 while (*ExprEnd != ':') {
731 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
732 ++ExprEnd;
733 }
734 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
735 Argument = ExprEnd + 1;
John McCall8cb7a8a32010-01-14 20:11:39 +0000736 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
John McCall43b61682010-10-14 01:55:31 +0000737
738 // Recursively format the result of the plural clause into the
739 // output string.
740 DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000741 return;
742 }
John McCall8cb7a8a32010-01-14 20:11:39 +0000743 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
Sebastian Redl15b02d22008-11-22 13:44:36 +0000744 }
745}
746
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000747/// Returns the friendly description for a token kind that will appear
Alp Tokera231ad22014-01-06 12:54:18 +0000748/// without quotes in diagnostic messages. These strings may be translatable in
749/// future.
750static const char *getTokenDescForDiagnostic(tok::TokenKind Kind) {
Alp Tokerec543272013-12-24 09:48:30 +0000751 switch (Kind) {
752 case tok::identifier:
753 return "identifier";
754 default:
Craig Topperf1186c52014-05-08 06:41:40 +0000755 return nullptr;
Alp Tokerec543272013-12-24 09:48:30 +0000756 }
757}
Sebastian Redl15b02d22008-11-22 13:44:36 +0000758
Chris Lattner23be0672008-11-19 06:51:40 +0000759/// FormatDiagnostic - Format this diagnostic into a string, substituting the
760/// formal arguments into the %0 slots. The result is appended onto the Str
761/// array.
David Blaikieb5784322011-09-26 01:18:08 +0000762void Diagnostic::
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000763FormatDiagnostic(SmallVectorImpl<char> &OutStr) const {
Argyrios Kyrtzidise9af37d2011-05-05 07:54:59 +0000764 if (!StoredDiagMessage.empty()) {
765 OutStr.append(StoredDiagMessage.begin(), StoredDiagMessage.end());
766 return;
767 }
768
Fangrui Song6907ce22018-07-30 19:24:48 +0000769 StringRef Diag =
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000770 getDiags()->getDiagnosticIDs()->getDescription(getID());
Mike Stump11289f42009-09-09 15:08:12 +0000771
Argyrios Kyrtzidis0e37afa2011-05-25 05:05:01 +0000772 FormatDiagnostic(Diag.begin(), Diag.end(), OutStr);
John McCalle4d54322010-01-13 23:58:20 +0000773}
774
David Blaikieb5784322011-09-26 01:18:08 +0000775void Diagnostic::
John McCalle4d54322010-01-13 23:58:20 +0000776FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000777 SmallVectorImpl<char> &OutStr) const {
Richard Trieub3b8bb02015-01-08 01:27:03 +0000778 // When the diagnostic string is only "%0", the entire string is being given
779 // by an outside source. Remove unprintable characters from this string
780 // and skip all the other string processing.
Richard Trieudcd7bb02015-01-17 00:56:10 +0000781 if (DiagEnd - DiagStr == 2 &&
782 StringRef(DiagStr, DiagEnd - DiagStr).equals("%0") &&
Richard Trieub3b8bb02015-01-08 01:27:03 +0000783 getArgKind(0) == DiagnosticsEngine::ak_std_string) {
784 const std::string &S = getArgStdStr(0);
785 for (char c : S) {
786 if (llvm::sys::locale::isPrint(c) || c == '\t') {
787 OutStr.push_back(c);
788 }
789 }
790 return;
791 }
792
Chris Lattnerc243f292009-10-20 05:25:22 +0000793 /// FormattedArgs - Keep track of all of the arguments formatted by
794 /// ConvertArgToString and pass them into subsequent calls to
795 /// ConvertArgToString, allowing the implementation to avoid redundancies in
796 /// obvious cases.
David Blaikie9c902b52011-09-25 23:23:43 +0000797 SmallVector<DiagnosticsEngine::ArgumentValue, 8> FormattedArgs;
Chandler Carruthd5173952011-07-11 17:49:21 +0000798
799 /// QualTypeVals - Pass a vector of arrays so that QualType names can be
800 /// compared to see if more information is needed to be printed.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000801 SmallVector<intptr_t, 2> QualTypeVals;
Richard Trieu91844232012-06-26 18:18:47 +0000802 SmallVector<char, 64> Tree;
803
Chandler Carruthd5173952011-07-11 17:49:21 +0000804 for (unsigned i = 0, e = getNumArgs(); i < e; ++i)
David Blaikie9c902b52011-09-25 23:23:43 +0000805 if (getArgKind(i) == DiagnosticsEngine::ak_qualtype)
Chandler Carruthd5173952011-07-11 17:49:21 +0000806 QualTypeVals.push_back(getRawArg(i));
807
Chris Lattner23be0672008-11-19 06:51:40 +0000808 while (DiagStr != DiagEnd) {
809 if (DiagStr[0] != '%') {
810 // Append non-%0 substrings to Str if we have one.
811 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
812 OutStr.append(DiagStr, StrEnd);
813 DiagStr = StrEnd;
Chris Lattner2b786902008-11-21 07:50:02 +0000814 continue;
Jordan Rosea7d03842013-02-08 22:30:41 +0000815 } else if (isPunctuation(DiagStr[1])) {
John McCall8cb7a8a32010-01-14 20:11:39 +0000816 OutStr.push_back(DiagStr[1]); // %% -> %.
Chris Lattner23be0672008-11-19 06:51:40 +0000817 DiagStr += 2;
Chris Lattner2b786902008-11-21 07:50:02 +0000818 continue;
819 }
Mike Stump11289f42009-09-09 15:08:12 +0000820
Chris Lattner2b786902008-11-21 07:50:02 +0000821 // Skip the %.
822 ++DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000823
Chris Lattner2b786902008-11-21 07:50:02 +0000824 // This must be a placeholder for a diagnostic argument. The format for a
825 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
826 // The digit is a number from 0-9 indicating which argument this comes from.
827 // The modifier is a string of digits from the set [-a-z]+, arguments is a
828 // brace enclosed string.
Craig Topperf1186c52014-05-08 06:41:40 +0000829 const char *Modifier = nullptr, *Argument = nullptr;
Chris Lattner2b786902008-11-21 07:50:02 +0000830 unsigned ModifierLen = 0, ArgumentLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000831
Chris Lattner2b786902008-11-21 07:50:02 +0000832 // Check to see if we have a modifier. If so eat it.
Jordan Rosea7d03842013-02-08 22:30:41 +0000833 if (!isDigit(DiagStr[0])) {
Chris Lattner2b786902008-11-21 07:50:02 +0000834 Modifier = DiagStr;
835 while (DiagStr[0] == '-' ||
836 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
837 ++DiagStr;
838 ModifierLen = DiagStr-Modifier;
Chris Lattner23be0672008-11-19 06:51:40 +0000839
Chris Lattner2b786902008-11-21 07:50:02 +0000840 // If we have an argument, get it next.
841 if (DiagStr[0] == '{') {
842 ++DiagStr; // Skip {.
843 Argument = DiagStr;
Mike Stump11289f42009-09-09 15:08:12 +0000844
John McCall8cb7a8a32010-01-14 20:11:39 +0000845 DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
846 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
Chris Lattner2b786902008-11-21 07:50:02 +0000847 ArgumentLen = DiagStr-Argument;
848 ++DiagStr; // Skip }.
Chris Lattner23be0672008-11-19 06:51:40 +0000849 }
Chris Lattner2b786902008-11-21 07:50:02 +0000850 }
Mike Stump11289f42009-09-09 15:08:12 +0000851
Jordan Rosea7d03842013-02-08 22:30:41 +0000852 assert(isDigit(*DiagStr) && "Invalid format for argument in diagnostic");
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000853 unsigned ArgNo = *DiagStr++ - '0';
Chris Lattner2b786902008-11-21 07:50:02 +0000854
Richard Trieu91844232012-06-26 18:18:47 +0000855 // Only used for type diffing.
856 unsigned ArgNo2 = ArgNo;
857
David Blaikie9c902b52011-09-25 23:23:43 +0000858 DiagnosticsEngine::ArgumentKind Kind = getArgKind(ArgNo);
Richard Trieu90c31f52013-01-30 20:04:31 +0000859 if (ModifierIs(Modifier, ModifierLen, "diff")) {
Jordan Rosea7d03842013-02-08 22:30:41 +0000860 assert(*DiagStr == ',' && isDigit(*(DiagStr + 1)) &&
Richard Trieu91844232012-06-26 18:18:47 +0000861 "Invalid format for diff modifier");
862 ++DiagStr; // Comma.
863 ArgNo2 = *DiagStr++ - '0';
Richard Trieu90c31f52013-01-30 20:04:31 +0000864 DiagnosticsEngine::ArgumentKind Kind2 = getArgKind(ArgNo2);
865 if (Kind == DiagnosticsEngine::ak_qualtype &&
866 Kind2 == DiagnosticsEngine::ak_qualtype)
867 Kind = DiagnosticsEngine::ak_qualtype_pair;
868 else {
869 // %diff only supports QualTypes. For other kinds of arguments,
870 // use the default printing. For example, if the modifier is:
871 // "%diff{compare $ to $|other text}1,2"
872 // treat it as:
873 // "compare %1 to %2"
Chandler Carruth8df65e42016-12-23 05:19:47 +0000874 const char *ArgumentEnd = Argument + ArgumentLen;
875 const char *Pipe = ScanFormat(Argument, ArgumentEnd, '|');
876 assert(ScanFormat(Pipe + 1, ArgumentEnd, '|') == ArgumentEnd &&
877 "Found too many '|'s in a %diff modifier!");
Richard Trieu90c31f52013-01-30 20:04:31 +0000878 const char *FirstDollar = ScanFormat(Argument, Pipe, '$');
879 const char *SecondDollar = ScanFormat(FirstDollar + 1, Pipe, '$');
Filipe Cabecinhased4a00c2013-01-30 22:03:24 +0000880 const char ArgStr1[] = { '%', static_cast<char>('0' + ArgNo) };
881 const char ArgStr2[] = { '%', static_cast<char>('0' + ArgNo2) };
Richard Trieu90c31f52013-01-30 20:04:31 +0000882 FormatDiagnostic(Argument, FirstDollar, OutStr);
883 FormatDiagnostic(ArgStr1, ArgStr1 + 2, OutStr);
884 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
885 FormatDiagnostic(ArgStr2, ArgStr2 + 2, OutStr);
886 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
887 continue;
888 }
Richard Trieu91844232012-06-26 18:18:47 +0000889 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000890
Chris Lattnerc243f292009-10-20 05:25:22 +0000891 switch (Kind) {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000892 // ---- STRINGS ----
David Blaikie9c902b52011-09-25 23:23:43 +0000893 case DiagnosticsEngine::ak_std_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000894 const std::string &S = getArgStdStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000895 assert(ModifierLen == 0 && "No modifiers for strings yet");
896 OutStr.append(S.begin(), S.end());
897 break;
898 }
David Blaikie9c902b52011-09-25 23:23:43 +0000899 case DiagnosticsEngine::ak_c_string: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000900 const char *S = getArgCStr(ArgNo);
Chris Lattner2b786902008-11-21 07:50:02 +0000901 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000902
903 // Don't crash if get passed a null pointer by accident.
904 if (!S)
905 S = "(null)";
Mike Stump11289f42009-09-09 15:08:12 +0000906
Chris Lattner2b786902008-11-21 07:50:02 +0000907 OutStr.append(S, S + strlen(S));
908 break;
909 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000910 // ---- INTEGERS ----
David Blaikie9c902b52011-09-25 23:23:43 +0000911 case DiagnosticsEngine::ak_sint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000912 int Val = getArgSInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000913
Chris Lattner2b786902008-11-21 07:50:02 +0000914 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCall43b61682010-10-14 01:55:31 +0000915 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen,
916 OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000917 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
918 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000919 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCall43b61682010-10-14 01:55:31 +0000920 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
921 OutStr);
John McCall9015cde2010-01-14 00:50:32 +0000922 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
923 HandleOrdinalModifier((unsigned)Val, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000924 } else {
925 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000926 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000927 }
Chris Lattner2b786902008-11-21 07:50:02 +0000928 break;
929 }
David Blaikie9c902b52011-09-25 23:23:43 +0000930 case DiagnosticsEngine::ak_uint: {
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000931 unsigned Val = getArgUInt(ArgNo);
Mike Stump11289f42009-09-09 15:08:12 +0000932
Chris Lattner2b786902008-11-21 07:50:02 +0000933 if (ModifierIs(Modifier, ModifierLen, "select")) {
John McCalle4d54322010-01-13 23:58:20 +0000934 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000935 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
936 HandleIntegerSModifier(Val, OutStr);
Sebastian Redl15b02d22008-11-22 13:44:36 +0000937 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
John McCall43b61682010-10-14 01:55:31 +0000938 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
939 OutStr);
John McCall9015cde2010-01-14 00:50:32 +0000940 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
941 HandleOrdinalModifier(Val, OutStr);
Chris Lattner2b786902008-11-21 07:50:02 +0000942 } else {
943 assert(ModifierLen == 0 && "Unknown integer modifier");
Daniel Dunbare3633792009-10-17 18:12:14 +0000944 llvm::raw_svector_ostream(OutStr) << Val;
Chris Lattner91aea712008-11-19 07:22:31 +0000945 }
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000946 break;
Chris Lattner2b786902008-11-21 07:50:02 +0000947 }
Alp Tokerec543272013-12-24 09:48:30 +0000948 // ---- TOKEN SPELLINGS ----
949 case DiagnosticsEngine::ak_tokenkind: {
950 tok::TokenKind Kind = static_cast<tok::TokenKind>(getRawArg(ArgNo));
951 assert(ModifierLen == 0 && "No modifiers for token kinds yet");
952
953 llvm::raw_svector_ostream Out(OutStr);
Alp Tokera231ad22014-01-06 12:54:18 +0000954 if (const char *S = tok::getPunctuatorSpelling(Kind))
955 // Quoted token spelling for punctuators.
956 Out << '\'' << S << '\'';
957 else if (const char *S = tok::getKeywordSpelling(Kind))
958 // Unquoted token spelling for keywords.
959 Out << S;
960 else if (const char *S = getTokenDescForDiagnostic(Kind))
Alp Tokerec543272013-12-24 09:48:30 +0000961 // Unquoted translatable token name.
962 Out << S;
Alp Tokerec543272013-12-24 09:48:30 +0000963 else if (const char *S = tok::getTokenName(Kind))
964 // Debug name, shouldn't appear in user-facing diagnostics.
965 Out << '<' << S << '>';
966 else
967 Out << "(null)";
968 break;
969 }
Chris Lattnere3d20d92008-11-23 21:45:46 +0000970 // ---- NAMES and TYPES ----
David Blaikie9c902b52011-09-25 23:23:43 +0000971 case DiagnosticsEngine::ak_identifierinfo: {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000972 const IdentifierInfo *II = getArgIdentifier(ArgNo);
973 assert(ModifierLen == 0 && "No modifiers for strings yet");
Daniel Dunbar69a79b12009-04-20 06:13:16 +0000974
975 // Don't crash if get passed a null pointer by accident.
976 if (!II) {
977 const char *S = "(null)";
978 OutStr.append(S, S + strlen(S));
979 continue;
980 }
981
Daniel Dunbar07d07852009-10-18 21:17:35 +0000982 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
Chris Lattnere3d20d92008-11-23 21:45:46 +0000983 break;
984 }
Anastasia Stulova4cebc9d2019-01-04 11:50:36 +0000985 case DiagnosticsEngine::ak_qual:
David Blaikie9c902b52011-09-25 23:23:43 +0000986 case DiagnosticsEngine::ak_qualtype:
987 case DiagnosticsEngine::ak_declarationname:
988 case DiagnosticsEngine::ak_nameddecl:
989 case DiagnosticsEngine::ak_nestednamespec:
990 case DiagnosticsEngine::ak_declcontext:
Aaron Ballman3e424b52013-12-26 18:30:57 +0000991 case DiagnosticsEngine::ak_attr:
Chris Lattnerc243f292009-10-20 05:25:22 +0000992 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
Craig Topper3aa4fb32014-06-12 05:32:35 +0000993 StringRef(Modifier, ModifierLen),
994 StringRef(Argument, ArgumentLen),
Craig Toppere4753502014-06-12 05:32:27 +0000995 FormattedArgs,
Chandler Carruthd5173952011-07-11 17:49:21 +0000996 OutStr, QualTypeVals);
Chris Lattner6a2ed6f2008-11-23 09:13:29 +0000997 break;
Eugene Zelenko25cae5a22018-02-16 23:40:07 +0000998 case DiagnosticsEngine::ak_qualtype_pair: {
Richard Trieu91844232012-06-26 18:18:47 +0000999 // Create a struct with all the info needed for printing.
1000 TemplateDiffTypes TDT;
1001 TDT.FromType = getRawArg(ArgNo);
1002 TDT.ToType = getRawArg(ArgNo2);
1003 TDT.ElideType = getDiags()->ElideType;
1004 TDT.ShowColors = getDiags()->ShowColors;
Richard Trieu50f5f462012-07-10 01:46:04 +00001005 TDT.TemplateDiffUsed = false;
Richard Trieu91844232012-06-26 18:18:47 +00001006 intptr_t val = reinterpret_cast<intptr_t>(&TDT);
1007
Richard Trieuc6058442012-06-29 21:12:16 +00001008 const char *ArgumentEnd = Argument + ArgumentLen;
1009 const char *Pipe = ScanFormat(Argument, ArgumentEnd, '|');
1010
Richard Trieua4056002012-07-13 21:18:32 +00001011 // Print the tree. If this diagnostic already has a tree, skip the
1012 // second tree.
1013 if (getDiags()->PrintTemplateTree && Tree.empty()) {
Richard Trieu91844232012-06-26 18:18:47 +00001014 TDT.PrintFromType = true;
1015 TDT.PrintTree = true;
1016 getDiags()->ConvertArgToString(Kind, val,
Craig Topper3aa4fb32014-06-12 05:32:35 +00001017 StringRef(Modifier, ModifierLen),
1018 StringRef(Argument, ArgumentLen),
Craig Toppere4753502014-06-12 05:32:27 +00001019 FormattedArgs,
Richard Trieu91844232012-06-26 18:18:47 +00001020 Tree, QualTypeVals);
1021 // If there is no tree information, fall back to regular printing.
Richard Trieuc6058442012-06-29 21:12:16 +00001022 if (!Tree.empty()) {
1023 FormatDiagnostic(Pipe + 1, ArgumentEnd, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +00001024 break;
Richard Trieuc6058442012-06-29 21:12:16 +00001025 }
Richard Trieu91844232012-06-26 18:18:47 +00001026 }
1027
1028 // Non-tree printing, also the fall-back when tree printing fails.
1029 // The fall-back is triggered when the types compared are not templates.
Richard Trieuc6058442012-06-29 21:12:16 +00001030 const char *FirstDollar = ScanFormat(Argument, ArgumentEnd, '$');
1031 const char *SecondDollar = ScanFormat(FirstDollar + 1, ArgumentEnd, '$');
Richard Trieu91844232012-06-26 18:18:47 +00001032
1033 // Append before text
Richard Trieuc6058442012-06-29 21:12:16 +00001034 FormatDiagnostic(Argument, FirstDollar, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +00001035
1036 // Append first type
1037 TDT.PrintTree = false;
1038 TDT.PrintFromType = true;
1039 getDiags()->ConvertArgToString(Kind, val,
Craig Topper3aa4fb32014-06-12 05:32:35 +00001040 StringRef(Modifier, ModifierLen),
1041 StringRef(Argument, ArgumentLen),
Craig Toppere4753502014-06-12 05:32:27 +00001042 FormattedArgs,
Richard Trieu91844232012-06-26 18:18:47 +00001043 OutStr, QualTypeVals);
Richard Trieu50f5f462012-07-10 01:46:04 +00001044 if (!TDT.TemplateDiffUsed)
1045 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
1046 TDT.FromType));
1047
Richard Trieu91844232012-06-26 18:18:47 +00001048 // Append middle text
Richard Trieuc6058442012-06-29 21:12:16 +00001049 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +00001050
1051 // Append second type
1052 TDT.PrintFromType = false;
1053 getDiags()->ConvertArgToString(Kind, val,
Craig Topper3aa4fb32014-06-12 05:32:35 +00001054 StringRef(Modifier, ModifierLen),
1055 StringRef(Argument, ArgumentLen),
Craig Toppere4753502014-06-12 05:32:27 +00001056 FormattedArgs,
Richard Trieu91844232012-06-26 18:18:47 +00001057 OutStr, QualTypeVals);
Richard Trieu50f5f462012-07-10 01:46:04 +00001058 if (!TDT.TemplateDiffUsed)
1059 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
1060 TDT.ToType));
1061
Richard Trieu91844232012-06-26 18:18:47 +00001062 // Append end text
Richard Trieuc6058442012-06-29 21:12:16 +00001063 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
Richard Trieu91844232012-06-26 18:18:47 +00001064 break;
Nico Weber4c311642008-08-10 19:59:06 +00001065 }
Eugene Zelenko25cae5a22018-02-16 23:40:07 +00001066 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001067
Chris Lattnerc243f292009-10-20 05:25:22 +00001068 // Remember this argument info for subsequent formatting operations. Turn
1069 // std::strings into a null terminated string to make it be the same case as
1070 // all the other ones.
Richard Trieu91844232012-06-26 18:18:47 +00001071 if (Kind == DiagnosticsEngine::ak_qualtype_pair)
1072 continue;
1073 else if (Kind != DiagnosticsEngine::ak_std_string)
Chris Lattnerc243f292009-10-20 05:25:22 +00001074 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
1075 else
David Blaikie9c902b52011-09-25 23:23:43 +00001076 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_c_string,
Chris Lattnerc243f292009-10-20 05:25:22 +00001077 (intptr_t)getArgStdStr(ArgNo).c_str()));
Nico Weber4c311642008-08-10 19:59:06 +00001078 }
Richard Trieu91844232012-06-26 18:18:47 +00001079
1080 // Append the type tree to the end of the diagnostics.
1081 OutStr.append(Tree.begin(), Tree.end());
Nico Weber4c311642008-08-10 19:59:06 +00001082}
Ted Kremenekea06ec12009-01-23 20:28:53 +00001083
David Blaikie9c902b52011-09-25 23:23:43 +00001084StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001085 StringRef Message)
Eugene Zelenko25cae5a22018-02-16 23:40:07 +00001086 : ID(ID), Level(Level), Message(Message) {}
Douglas Gregor33cdd812010-02-18 18:08:43 +00001087
Fangrui Song6907ce22018-07-30 19:24:48 +00001088StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level,
David Blaikieb5784322011-09-26 01:18:08 +00001089 const Diagnostic &Info)
Eugene Zelenko25cae5a22018-02-16 23:40:07 +00001090 : ID(Info.getID()), Level(Level) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00001091 assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
1092 "Valid source location without setting a source manager for diagnostic");
1093 if (Info.getLocation().isValid())
1094 Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001095 SmallString<64> Message;
Douglas Gregor33cdd812010-02-18 18:08:43 +00001096 Info.FormatDiagnostic(Message);
1097 this->Message.assign(Message.begin(), Message.end());
Benjamin Kramerf9890422015-02-17 16:48:30 +00001098 this->Ranges.assign(Info.getRanges().begin(), Info.getRanges().end());
1099 this->FixIts.assign(Info.getFixItHints().begin(), Info.getFixItHints().end());
Douglas Gregor33cdd812010-02-18 18:08:43 +00001100}
1101
David Blaikie9c902b52011-09-25 23:23:43 +00001102StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001103 StringRef Message, FullSourceLoc Loc,
Chris Lattner54b16772011-07-23 17:14:25 +00001104 ArrayRef<CharSourceRange> Ranges,
Aaron Ballman234ebd72013-02-24 19:08:10 +00001105 ArrayRef<FixItHint> FixIts)
Eugene Zelenko25cae5a22018-02-16 23:40:07 +00001106 : ID(ID), Level(Level), Loc(Loc), Message(Message),
1107 Ranges(Ranges.begin(), Ranges.end()), FixIts(FixIts.begin(), FixIts.end())
Douglas Gregor925296b2011-07-19 16:10:42 +00001108{
Douglas Gregor925296b2011-07-19 16:10:42 +00001109}
1110
Ted Kremenekea06ec12009-01-23 20:28:53 +00001111/// IncludeInDiagnosticCounts - This method (whose default implementation
1112/// returns true) indicates whether the diagnostics handled by this
David Blaikiee2eefae2011-09-25 23:39:51 +00001113/// DiagnosticConsumer should be included in the number of diagnostics
David Blaikie9c902b52011-09-25 23:23:43 +00001114/// reported by DiagnosticsEngine.
David Blaikiee2eefae2011-09-25 23:39:51 +00001115bool DiagnosticConsumer::IncludeInDiagnosticCounts() const { return true; }
Douglas Gregor89336232010-03-29 23:34:08 +00001116
Eugene Zelenko25cae5a22018-02-16 23:40:07 +00001117void IgnoringDiagConsumer::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00001118
Eugene Zelenko25cae5a22018-02-16 23:40:07 +00001119ForwardingDiagnosticConsumer::~ForwardingDiagnosticConsumer() = default;
Douglas Gregor6b930962013-05-03 22:58:43 +00001120
1121void ForwardingDiagnosticConsumer::HandleDiagnostic(
1122 DiagnosticsEngine::Level DiagLevel,
1123 const Diagnostic &Info) {
1124 Target.HandleDiagnostic(DiagLevel, Info);
1125}
1126
1127void ForwardingDiagnosticConsumer::clear() {
1128 DiagnosticConsumer::clear();
1129 Target.clear();
1130}
1131
1132bool ForwardingDiagnosticConsumer::IncludeInDiagnosticCounts() const {
1133 return Target.IncludeInDiagnosticCounts();
1134}
1135
Benjamin Kramer7ec12c92012-02-07 22:29:24 +00001136PartialDiagnostic::StorageAllocator::StorageAllocator() {
Douglas Gregor89336232010-03-29 23:34:08 +00001137 for (unsigned I = 0; I != NumCached; ++I)
1138 FreeList[I] = Cached + I;
1139 NumFreeListEntries = NumCached;
1140}
1141
Benjamin Kramer7ec12c92012-02-07 22:29:24 +00001142PartialDiagnostic::StorageAllocator::~StorageAllocator() {
Chad Rosier849a67b2012-02-07 23:24:49 +00001143 // Don't assert if we are in a CrashRecovery context, as this invariant may
1144 // be invalidated during a crash.
Justin Lebarbf16db12016-08-10 01:09:07 +00001145 assert((NumFreeListEntries == NumCached ||
1146 llvm::CrashRecoveryContext::isRecoveringFromCrash()) &&
1147 "A partial is on the lam");
Douglas Gregor89336232010-03-29 23:34:08 +00001148}
Alex Lorenzd0e27262017-08-25 15:48:00 +00001149
1150char DiagnosticError::ID;