1 //===- Diagnostic.cpp - C Language Family Diagnostic Handling -------------===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the Diagnostic-related interfaces.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "clang/Basic/Diagnostic.h"
14 #include "clang/Basic/CharInfo.h"
15 #include "clang/Basic/DiagnosticError.h"
16 #include "clang/Basic/DiagnosticIDs.h"
17 #include "clang/Basic/DiagnosticOptions.h"
18 #include "clang/Basic/IdentifierTable.h"
19 #include "clang/Basic/PartialDiagnostic.h"
20 #include "clang/Basic/SourceLocation.h"
21 #include "clang/Basic/SourceManager.h"
22 #include "clang/Basic/Specifiers.h"
23 #include "clang/Basic/TokenKinds.h"
24 #include "llvm/ADT/SmallString.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/ADT/StringExtras.h"
27 #include "llvm/ADT/StringRef.h"
28 #include "llvm/Support/CrashRecoveryContext.h"
29 #include "llvm/Support/Locale.h"
30 #include "llvm/Support/raw_ostream.h"
31 #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>
40
41 using namespace clang;
42
operator <<(const StreamingDiagnostic & DB,DiagNullabilityKind nullability)43 const StreamingDiagnostic &clang::operator<<(const StreamingDiagnostic &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
operator <<(const StreamingDiagnostic & DB,llvm::Error && E)64 const StreamingDiagnostic &clang::operator<<(const StreamingDiagnostic &DB,
65 llvm::Error &&E) {
66 DB.AddString(toString(std::move(E)));
67 return DB;
68 }
69
DummyArgToStringFn(DiagnosticsEngine::ArgumentKind AK,intptr_t QT,StringRef Modifier,StringRef Argument,ArrayRef<DiagnosticsEngine::ArgumentValue> PrevArgs,SmallVectorImpl<char> & Output,void * Cookie,ArrayRef<intptr_t> QualTypeVals)70 static void DummyArgToStringFn(DiagnosticsEngine::ArgumentKind AK, intptr_t QT,
71 StringRef Modifier, StringRef Argument,
72 ArrayRef<DiagnosticsEngine::ArgumentValue> PrevArgs,
73 SmallVectorImpl<char> &Output,
74 void *Cookie,
75 ArrayRef<intptr_t> QualTypeVals) {
76 StringRef Str = "<can't format argument>";
77 Output.append(Str.begin(), Str.end());
78 }
79
DiagnosticsEngine(IntrusiveRefCntPtr<DiagnosticIDs> diags,IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,DiagnosticConsumer * client,bool ShouldOwnClient)80 DiagnosticsEngine::DiagnosticsEngine(
81 IntrusiveRefCntPtr<DiagnosticIDs> diags,
82 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, DiagnosticConsumer *client,
83 bool ShouldOwnClient)
84 : Diags(std::move(diags)), DiagOpts(std::move(DiagOpts)) {
85 setClient(client, ShouldOwnClient);
86 ArgToStringFn = DummyArgToStringFn;
87
88 Reset();
89 }
90
~DiagnosticsEngine()91 DiagnosticsEngine::~DiagnosticsEngine() {
92 // If we own the diagnostic client, destroy it first so that it can access the
93 // engine from its destructor.
94 setClient(nullptr);
95 }
96
dump() const97 void DiagnosticsEngine::dump() const {
98 DiagStatesByLoc.dump(*SourceMgr);
99 }
100
dump(StringRef DiagName) const101 void DiagnosticsEngine::dump(StringRef DiagName) const {
102 DiagStatesByLoc.dump(*SourceMgr, DiagName);
103 }
104
setClient(DiagnosticConsumer * client,bool ShouldOwnClient)105 void DiagnosticsEngine::setClient(DiagnosticConsumer *client,
106 bool ShouldOwnClient) {
107 Owner.reset(ShouldOwnClient ? client : nullptr);
108 Client = client;
109 }
110
pushMappings(SourceLocation Loc)111 void DiagnosticsEngine::pushMappings(SourceLocation Loc) {
112 DiagStateOnPushStack.push_back(GetCurDiagState());
113 }
114
popMappings(SourceLocation Loc)115 bool DiagnosticsEngine::popMappings(SourceLocation Loc) {
116 if (DiagStateOnPushStack.empty())
117 return false;
118
119 if (DiagStateOnPushStack.back() != GetCurDiagState()) {
120 // State changed at some point between push/pop.
121 PushDiagStatePoint(DiagStateOnPushStack.back(), Loc);
122 }
123 DiagStateOnPushStack.pop_back();
124 return true;
125 }
126
Reset()127 void DiagnosticsEngine::Reset() {
128 ErrorOccurred = false;
129 UncompilableErrorOccurred = false;
130 FatalErrorOccurred = false;
131 UnrecoverableErrorOccurred = false;
132
133 NumWarnings = 0;
134 NumErrors = 0;
135 TrapNumErrorsOccurred = 0;
136 TrapNumUnrecoverableErrorsOccurred = 0;
137
138 CurDiagID = std::numeric_limits<unsigned>::max();
139 LastDiagLevel = DiagnosticIDs::Ignored;
140 DelayedDiagID = 0;
141
142 // Clear state related to #pragma diagnostic.
143 DiagStates.clear();
144 DiagStatesByLoc.clear();
145 DiagStateOnPushStack.clear();
146
147 // Create a DiagState and DiagStatePoint representing diagnostic changes
148 // through command-line.
149 DiagStates.emplace_back();
150 DiagStatesByLoc.appendFirst(&DiagStates.back());
151 }
152
SetDelayedDiagnostic(unsigned DiagID,StringRef Arg1,StringRef Arg2,StringRef Arg3)153 void DiagnosticsEngine::SetDelayedDiagnostic(unsigned DiagID, StringRef Arg1,
154 StringRef Arg2, StringRef Arg3) {
155 if (DelayedDiagID)
156 return;
157
158 DelayedDiagID = DiagID;
159 DelayedDiagArg1 = Arg1.str();
160 DelayedDiagArg2 = Arg2.str();
161 DelayedDiagArg3 = Arg3.str();
162 }
163
ReportDelayed()164 void DiagnosticsEngine::ReportDelayed() {
165 unsigned ID = DelayedDiagID;
166 DelayedDiagID = 0;
167 Report(ID) << DelayedDiagArg1 << DelayedDiagArg2 << DelayedDiagArg3;
168 }
169
appendFirst(DiagState * State)170 void DiagnosticsEngine::DiagStateMap::appendFirst(DiagState *State) {
171 assert(Files.empty() && "not first");
172 FirstDiagState = CurDiagState = State;
173 CurDiagStateLoc = SourceLocation();
174 }
175
append(SourceManager & SrcMgr,SourceLocation Loc,DiagState * State)176 void DiagnosticsEngine::DiagStateMap::append(SourceManager &SrcMgr,
177 SourceLocation Loc,
178 DiagState *State) {
179 CurDiagState = State;
180 CurDiagStateLoc = Loc;
181
182 std::pair<FileID, unsigned> Decomp = SrcMgr.getDecomposedLoc(Loc);
183 unsigned Offset = Decomp.second;
184 for (File *F = getFile(SrcMgr, Decomp.first); F;
185 Offset = F->ParentOffset, F = F->Parent) {
186 F->HasLocalTransitions = true;
187 auto &Last = F->StateTransitions.back();
188 assert(Last.Offset <= Offset && "state transitions added out of order");
189
190 if (Last.Offset == Offset) {
191 if (Last.State == State)
192 break;
193 Last.State = State;
194 continue;
195 }
196
197 F->StateTransitions.push_back({State, Offset});
198 }
199 }
200
201 DiagnosticsEngine::DiagState *
lookup(SourceManager & SrcMgr,SourceLocation Loc) const202 DiagnosticsEngine::DiagStateMap::lookup(SourceManager &SrcMgr,
203 SourceLocation Loc) const {
204 // Common case: we have not seen any diagnostic pragmas.
205 if (Files.empty())
206 return FirstDiagState;
207
208 std::pair<FileID, unsigned> Decomp = SrcMgr.getDecomposedLoc(Loc);
209 const File *F = getFile(SrcMgr, Decomp.first);
210 return F->lookup(Decomp.second);
211 }
212
213 DiagnosticsEngine::DiagState *
lookup(unsigned Offset) const214 DiagnosticsEngine::DiagStateMap::File::lookup(unsigned Offset) const {
215 auto OnePastIt =
216 llvm::partition_point(StateTransitions, [=](const DiagStatePoint &P) {
217 return P.Offset <= Offset;
218 });
219 assert(OnePastIt != StateTransitions.begin() && "missing initial state");
220 return OnePastIt[-1].State;
221 }
222
223 DiagnosticsEngine::DiagStateMap::File *
getFile(SourceManager & SrcMgr,FileID ID) const224 DiagnosticsEngine::DiagStateMap::getFile(SourceManager &SrcMgr,
225 FileID ID) const {
226 // Get or insert the File for this ID.
227 auto Range = Files.equal_range(ID);
228 if (Range.first != Range.second)
229 return &Range.first->second;
230 auto &F = Files.insert(Range.first, std::make_pair(ID, File()))->second;
231
232 // We created a new File; look up the diagnostic state at the start of it and
233 // initialize it.
234 if (ID.isValid()) {
235 std::pair<FileID, unsigned> Decomp = SrcMgr.getDecomposedIncludedLoc(ID);
236 F.Parent = getFile(SrcMgr, Decomp.first);
237 F.ParentOffset = Decomp.second;
238 F.StateTransitions.push_back({F.Parent->lookup(Decomp.second), 0});
239 } else {
240 // This is the (imaginary) root file into which we pretend all top-level
241 // files are included; it descends from the initial state.
242 //
243 // FIXME: This doesn't guarantee that we use the same ordering as
244 // isBeforeInTranslationUnit in the cases where someone invented another
245 // top-level file and added diagnostic pragmas to it. See the code at the
246 // end of isBeforeInTranslationUnit for the quirks it deals with.
247 F.StateTransitions.push_back({FirstDiagState, 0});
248 }
249 return &F;
250 }
251
dump(SourceManager & SrcMgr,StringRef DiagName) const252 void DiagnosticsEngine::DiagStateMap::dump(SourceManager &SrcMgr,
253 StringRef DiagName) const {
254 llvm::errs() << "diagnostic state at ";
255 CurDiagStateLoc.print(llvm::errs(), SrcMgr);
256 llvm::errs() << ": " << CurDiagState << "\n";
257
258 for (auto &F : Files) {
259 FileID ID = F.first;
260 File &File = F.second;
261
262 bool PrintedOuterHeading = false;
263 auto PrintOuterHeading = [&] {
264 if (PrintedOuterHeading) return;
265 PrintedOuterHeading = true;
266
267 llvm::errs() << "File " << &File << " <FileID " << ID.getHashValue()
268 << ">: " << SrcMgr.getBufferOrFake(ID).getBufferIdentifier();
269
270 if (F.second.Parent) {
271 std::pair<FileID, unsigned> Decomp =
272 SrcMgr.getDecomposedIncludedLoc(ID);
273 assert(File.ParentOffset == Decomp.second);
274 llvm::errs() << " parent " << File.Parent << " <FileID "
275 << Decomp.first.getHashValue() << "> ";
276 SrcMgr.getLocForStartOfFile(Decomp.first)
277 .getLocWithOffset(Decomp.second)
278 .print(llvm::errs(), SrcMgr);
279 }
280 if (File.HasLocalTransitions)
281 llvm::errs() << " has_local_transitions";
282 llvm::errs() << "\n";
283 };
284
285 if (DiagName.empty())
286 PrintOuterHeading();
287
288 for (DiagStatePoint &Transition : File.StateTransitions) {
289 bool PrintedInnerHeading = false;
290 auto PrintInnerHeading = [&] {
291 if (PrintedInnerHeading) return;
292 PrintedInnerHeading = true;
293
294 PrintOuterHeading();
295 llvm::errs() << " ";
296 SrcMgr.getLocForStartOfFile(ID)
297 .getLocWithOffset(Transition.Offset)
298 .print(llvm::errs(), SrcMgr);
299 llvm::errs() << ": state " << Transition.State << ":\n";
300 };
301
302 if (DiagName.empty())
303 PrintInnerHeading();
304
305 for (auto &Mapping : *Transition.State) {
306 StringRef Option =
307 DiagnosticIDs::getWarningOptionForDiag(Mapping.first);
308 if (!DiagName.empty() && DiagName != Option)
309 continue;
310
311 PrintInnerHeading();
312 llvm::errs() << " ";
313 if (Option.empty())
314 llvm::errs() << "<unknown " << Mapping.first << ">";
315 else
316 llvm::errs() << Option;
317 llvm::errs() << ": ";
318
319 switch (Mapping.second.getSeverity()) {
320 case diag::Severity::Ignored: llvm::errs() << "ignored"; break;
321 case diag::Severity::Remark: llvm::errs() << "remark"; break;
322 case diag::Severity::Warning: llvm::errs() << "warning"; break;
323 case diag::Severity::Error: llvm::errs() << "error"; break;
324 case diag::Severity::Fatal: llvm::errs() << "fatal"; break;
325 }
326
327 if (!Mapping.second.isUser())
328 llvm::errs() << " default";
329 if (Mapping.second.isPragma())
330 llvm::errs() << " pragma";
331 if (Mapping.second.hasNoWarningAsError())
332 llvm::errs() << " no-error";
333 if (Mapping.second.hasNoErrorAsFatal())
334 llvm::errs() << " no-fatal";
335 if (Mapping.second.wasUpgradedFromWarning())
336 llvm::errs() << " overruled";
337 llvm::errs() << "\n";
338 }
339 }
340 }
341 }
342
PushDiagStatePoint(DiagState * State,SourceLocation Loc)343 void DiagnosticsEngine::PushDiagStatePoint(DiagState *State,
344 SourceLocation Loc) {
345 assert(Loc.isValid() && "Adding invalid loc point");
346 DiagStatesByLoc.append(*SourceMgr, Loc, State);
347 }
348
setSeverity(diag::kind Diag,diag::Severity Map,SourceLocation L)349 void DiagnosticsEngine::setSeverity(diag::kind Diag, diag::Severity Map,
350 SourceLocation L) {
351 assert(Diag < diag::DIAG_UPPER_LIMIT &&
352 "Can only map builtin diagnostics");
353 assert((Diags->isBuiltinWarningOrExtension(Diag) ||
354 (Map == diag::Severity::Fatal || Map == diag::Severity::Error)) &&
355 "Cannot map errors into warnings!");
356 assert((L.isInvalid() || SourceMgr) && "No SourceMgr for valid location");
357
358 // Don't allow a mapping to a warning override an error/fatal mapping.
359 bool WasUpgradedFromWarning = false;
360 if (Map == diag::Severity::Warning) {
361 DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag);
362 if (Info.getSeverity() == diag::Severity::Error ||
363 Info.getSeverity() == diag::Severity::Fatal) {
364 Map = Info.getSeverity();
365 WasUpgradedFromWarning = true;
366 }
367 }
368 DiagnosticMapping Mapping = makeUserMapping(Map, L);
369 Mapping.setUpgradedFromWarning(WasUpgradedFromWarning);
370
371 // Common case; setting all the diagnostics of a group in one place.
372 if ((L.isInvalid() || L == DiagStatesByLoc.getCurDiagStateLoc()) &&
373 DiagStatesByLoc.getCurDiagState()) {
374 // FIXME: This is theoretically wrong: if the current state is shared with
375 // some other location (via push/pop) we will change the state for that
376 // other location as well. This cannot currently happen, as we can't update
377 // the diagnostic state at the same location at which we pop.
378 DiagStatesByLoc.getCurDiagState()->setMapping(Diag, Mapping);
379 return;
380 }
381
382 // A diagnostic pragma occurred, create a new DiagState initialized with
383 // the current one and a new DiagStatePoint to record at which location
384 // the new state became active.
385 DiagStates.push_back(*GetCurDiagState());
386 DiagStates.back().setMapping(Diag, Mapping);
387 PushDiagStatePoint(&DiagStates.back(), L);
388 }
389
setSeverityForGroup(diag::Flavor Flavor,StringRef Group,diag::Severity Map,SourceLocation Loc)390 bool DiagnosticsEngine::setSeverityForGroup(diag::Flavor Flavor,
391 StringRef Group, diag::Severity Map,
392 SourceLocation Loc) {
393 // Get the diagnostics in this group.
394 SmallVector<diag::kind, 256> GroupDiags;
395 if (Diags->getDiagnosticsInGroup(Flavor, Group, GroupDiags))
396 return true;
397
398 // Set the mapping.
399 for (diag::kind Diag : GroupDiags)
400 setSeverity(Diag, Map, Loc);
401
402 return false;
403 }
404
setDiagnosticGroupWarningAsError(StringRef Group,bool Enabled)405 bool DiagnosticsEngine::setDiagnosticGroupWarningAsError(StringRef Group,
406 bool Enabled) {
407 // If we are enabling this feature, just set the diagnostic mappings to map to
408 // errors.
409 if (Enabled)
410 return setSeverityForGroup(diag::Flavor::WarningOrError, Group,
411 diag::Severity::Error);
412
413 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
414 // potentially downgrade anything already mapped to be a warning.
415
416 // Get the diagnostics in this group.
417 SmallVector<diag::kind, 8> GroupDiags;
418 if (Diags->getDiagnosticsInGroup(diag::Flavor::WarningOrError, Group,
419 GroupDiags))
420 return true;
421
422 // Perform the mapping change.
423 for (diag::kind Diag : GroupDiags) {
424 DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag);
425
426 if (Info.getSeverity() == diag::Severity::Error ||
427 Info.getSeverity() == diag::Severity::Fatal)
428 Info.setSeverity(diag::Severity::Warning);
429
430 Info.setNoWarningAsError(true);
431 }
432
433 return false;
434 }
435
setDiagnosticGroupErrorAsFatal(StringRef Group,bool Enabled)436 bool DiagnosticsEngine::setDiagnosticGroupErrorAsFatal(StringRef Group,
437 bool Enabled) {
438 // If we are enabling this feature, just set the diagnostic mappings to map to
439 // fatal errors.
440 if (Enabled)
441 return setSeverityForGroup(diag::Flavor::WarningOrError, Group,
442 diag::Severity::Fatal);
443
444 // Otherwise, we want to set the diagnostic mapping's "no Wfatal-errors" bit,
445 // and potentially downgrade anything already mapped to be a fatal error.
446
447 // Get the diagnostics in this group.
448 SmallVector<diag::kind, 8> GroupDiags;
449 if (Diags->getDiagnosticsInGroup(diag::Flavor::WarningOrError, Group,
450 GroupDiags))
451 return true;
452
453 // Perform the mapping change.
454 for (diag::kind Diag : GroupDiags) {
455 DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag);
456
457 if (Info.getSeverity() == diag::Severity::Fatal)
458 Info.setSeverity(diag::Severity::Error);
459
460 Info.setNoErrorAsFatal(true);
461 }
462
463 return false;
464 }
465
setSeverityForAll(diag::Flavor Flavor,diag::Severity Map,SourceLocation Loc)466 void DiagnosticsEngine::setSeverityForAll(diag::Flavor Flavor,
467 diag::Severity Map,
468 SourceLocation Loc) {
469 // Get all the diagnostics.
470 std::vector<diag::kind> AllDiags;
471 DiagnosticIDs::getAllDiagnostics(Flavor, AllDiags);
472
473 // Set the mapping.
474 for (diag::kind Diag : AllDiags)
475 if (Diags->isBuiltinWarningOrExtension(Diag))
476 setSeverity(Diag, Map, Loc);
477 }
478
Report(const StoredDiagnostic & storedDiag)479 void DiagnosticsEngine::Report(const StoredDiagnostic &storedDiag) {
480 assert(CurDiagID == std::numeric_limits<unsigned>::max() &&
481 "Multiple diagnostics in flight at once!");
482
483 CurDiagLoc = storedDiag.getLocation();
484 CurDiagID = storedDiag.getID();
485 DiagStorage.NumDiagArgs = 0;
486
487 DiagStorage.DiagRanges.clear();
488 DiagStorage.DiagRanges.append(storedDiag.range_begin(),
489 storedDiag.range_end());
490
491 DiagStorage.FixItHints.clear();
492 DiagStorage.FixItHints.append(storedDiag.fixit_begin(),
493 storedDiag.fixit_end());
494
495 assert(Client && "DiagnosticConsumer not set!");
496 Level DiagLevel = storedDiag.getLevel();
497 Diagnostic Info(this, storedDiag.getMessage());
498 Client->HandleDiagnostic(DiagLevel, Info);
499 if (Client->IncludeInDiagnosticCounts()) {
500 if (DiagLevel == DiagnosticsEngine::Warning)
501 ++NumWarnings;
502 }
503
504 CurDiagID = std::numeric_limits<unsigned>::max();
505 }
506
EmitCurrentDiagnostic(bool Force)507 bool DiagnosticsEngine::EmitCurrentDiagnostic(bool Force) {
508 assert(getClient() && "DiagnosticClient not set!");
509
510 bool Emitted;
511 if (Force) {
512 Diagnostic Info(this);
513
514 // Figure out the diagnostic level of this message.
515 DiagnosticIDs::Level DiagLevel
516 = Diags->getDiagnosticLevel(Info.getID(), Info.getLocation(), *this);
517
518 Emitted = (DiagLevel != DiagnosticIDs::Ignored);
519 if (Emitted) {
520 // Emit the diagnostic regardless of suppression level.
521 Diags->EmitDiag(*this, DiagLevel);
522 }
523 } else {
524 // Process the diagnostic, sending the accumulated information to the
525 // DiagnosticConsumer.
526 Emitted = ProcessDiag();
527 }
528
529 // Clear out the current diagnostic object.
530 Clear();
531
532 // If there was a delayed diagnostic, emit it now.
533 if (!Force && DelayedDiagID)
534 ReportDelayed();
535
536 return Emitted;
537 }
538
539 DiagnosticConsumer::~DiagnosticConsumer() = default;
540
HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,const Diagnostic & Info)541 void DiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
542 const Diagnostic &Info) {
543 if (!IncludeInDiagnosticCounts())
544 return;
545
546 if (DiagLevel == DiagnosticsEngine::Warning)
547 ++NumWarnings;
548 else if (DiagLevel >= DiagnosticsEngine::Error)
549 ++NumErrors;
550 }
551
552 /// ModifierIs - Return true if the specified modifier matches specified string.
553 template <std::size_t StrLen>
ModifierIs(const char * Modifier,unsigned ModifierLen,const char (& Str)[StrLen])554 static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
555 const char (&Str)[StrLen]) {
556 return StrLen-1 == ModifierLen && memcmp(Modifier, Str, StrLen-1) == 0;
557 }
558
559 /// ScanForward - Scans forward, looking for the given character, skipping
560 /// nested clauses and escaped characters.
ScanFormat(const char * I,const char * E,char Target)561 static const char *ScanFormat(const char *I, const char *E, char Target) {
562 unsigned Depth = 0;
563
564 for ( ; I != E; ++I) {
565 if (Depth == 0 && *I == Target) return I;
566 if (Depth != 0 && *I == '}') Depth--;
567
568 if (*I == '%') {
569 I++;
570 if (I == E) break;
571
572 // Escaped characters get implicitly skipped here.
573
574 // Format specifier.
575 if (!isDigit(*I) && !isPunctuation(*I)) {
576 for (I++; I != E && !isDigit(*I) && *I != '{'; I++) ;
577 if (I == E) break;
578 if (*I == '{')
579 Depth++;
580 }
581 }
582 }
583 return E;
584 }
585
586 /// HandleSelectModifier - Handle the integer 'select' modifier. This is used
587 /// like this: %select{foo|bar|baz}2. This means that the integer argument
588 /// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
589 /// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
590 /// This is very useful for certain classes of variant diagnostics.
HandleSelectModifier(const Diagnostic & DInfo,unsigned ValNo,const char * Argument,unsigned ArgumentLen,SmallVectorImpl<char> & OutStr)591 static void HandleSelectModifier(const Diagnostic &DInfo, unsigned ValNo,
592 const char *Argument, unsigned ArgumentLen,
593 SmallVectorImpl<char> &OutStr) {
594 const char *ArgumentEnd = Argument+ArgumentLen;
595
596 // Skip over 'ValNo' |'s.
597 while (ValNo) {
598 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
599 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
600 " larger than the number of options in the diagnostic string!");
601 Argument = NextVal+1; // Skip this string.
602 --ValNo;
603 }
604
605 // Get the end of the value. This is either the } or the |.
606 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
607
608 // Recursively format the result of the select clause into the output string.
609 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
610 }
611
612 /// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
613 /// letter 's' to the string if the value is not 1. This is used in cases like
614 /// this: "you idiot, you have %4 parameter%s4!".
HandleIntegerSModifier(unsigned ValNo,SmallVectorImpl<char> & OutStr)615 static void HandleIntegerSModifier(unsigned ValNo,
616 SmallVectorImpl<char> &OutStr) {
617 if (ValNo != 1)
618 OutStr.push_back('s');
619 }
620
621 /// HandleOrdinalModifier - Handle the integer 'ord' modifier. This
622 /// prints the ordinal form of the given integer, with 1 corresponding
623 /// to the first ordinal. Currently this is hard-coded to use the
624 /// English form.
HandleOrdinalModifier(unsigned ValNo,SmallVectorImpl<char> & OutStr)625 static void HandleOrdinalModifier(unsigned ValNo,
626 SmallVectorImpl<char> &OutStr) {
627 assert(ValNo != 0 && "ValNo must be strictly positive!");
628
629 llvm::raw_svector_ostream Out(OutStr);
630
631 // We could use text forms for the first N ordinals, but the numeric
632 // forms are actually nicer in diagnostics because they stand out.
633 Out << ValNo << llvm::getOrdinalSuffix(ValNo);
634 }
635
636 /// PluralNumber - Parse an unsigned integer and advance Start.
PluralNumber(const char * & Start,const char * End)637 static unsigned PluralNumber(const char *&Start, const char *End) {
638 // Programming 101: Parse a decimal number :-)
639 unsigned Val = 0;
640 while (Start != End && *Start >= '0' && *Start <= '9') {
641 Val *= 10;
642 Val += *Start - '0';
643 ++Start;
644 }
645 return Val;
646 }
647
648 /// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
TestPluralRange(unsigned Val,const char * & Start,const char * End)649 static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
650 if (*Start != '[') {
651 unsigned Ref = PluralNumber(Start, End);
652 return Ref == Val;
653 }
654
655 ++Start;
656 unsigned Low = PluralNumber(Start, End);
657 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
658 ++Start;
659 unsigned High = PluralNumber(Start, End);
660 assert(*Start == ']' && "Bad plural expression syntax: expected )");
661 ++Start;
662 return Low <= Val && Val <= High;
663 }
664
665 /// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
EvalPluralExpr(unsigned ValNo,const char * Start,const char * End)666 static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
667 // Empty condition?
668 if (*Start == ':')
669 return true;
670
671 while (true) {
672 char C = *Start;
673 if (C == '%') {
674 // Modulo expression
675 ++Start;
676 unsigned Arg = PluralNumber(Start, End);
677 assert(*Start == '=' && "Bad plural expression syntax: expected =");
678 ++Start;
679 unsigned ValMod = ValNo % Arg;
680 if (TestPluralRange(ValMod, Start, End))
681 return true;
682 } else {
683 assert((C == '[' || (C >= '0' && C <= '9')) &&
684 "Bad plural expression syntax: unexpected character");
685 // Range expression
686 if (TestPluralRange(ValNo, Start, End))
687 return true;
688 }
689
690 // Scan for next or-expr part.
691 Start = std::find(Start, End, ',');
692 if (Start == End)
693 break;
694 ++Start;
695 }
696 return false;
697 }
698
699 /// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
700 /// for complex plural forms, or in languages where all plurals are complex.
701 /// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
702 /// conditions that are tested in order, the form corresponding to the first
703 /// that applies being emitted. The empty condition is always true, making the
704 /// last form a default case.
705 /// Conditions are simple boolean expressions, where n is the number argument.
706 /// Here are the rules.
707 /// condition := expression | empty
708 /// empty := -> always true
709 /// expression := numeric [',' expression] -> logical or
710 /// numeric := range -> true if n in range
711 /// | '%' number '=' range -> true if n % number in range
712 /// range := number
713 /// | '[' number ',' number ']' -> ranges are inclusive both ends
714 ///
715 /// Here are some examples from the GNU gettext manual written in this form:
716 /// English:
717 /// {1:form0|:form1}
718 /// Latvian:
719 /// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
720 /// Gaeilge:
721 /// {1:form0|2:form1|:form2}
722 /// Romanian:
723 /// {1:form0|0,%100=[1,19]:form1|:form2}
724 /// Lithuanian:
725 /// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
726 /// Russian (requires repeated form):
727 /// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
728 /// Slovak
729 /// {1:form0|[2,4]:form1|:form2}
730 /// Polish (requires repeated form):
731 /// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
HandlePluralModifier(const Diagnostic & DInfo,unsigned ValNo,const char * Argument,unsigned ArgumentLen,SmallVectorImpl<char> & OutStr)732 static void HandlePluralModifier(const Diagnostic &DInfo, unsigned ValNo,
733 const char *Argument, unsigned ArgumentLen,
734 SmallVectorImpl<char> &OutStr) {
735 const char *ArgumentEnd = Argument + ArgumentLen;
736 while (true) {
737 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
738 const char *ExprEnd = Argument;
739 while (*ExprEnd != ':') {
740 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
741 ++ExprEnd;
742 }
743 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
744 Argument = ExprEnd + 1;
745 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
746
747 // Recursively format the result of the plural clause into the
748 // output string.
749 DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr);
750 return;
751 }
752 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
753 }
754 }
755
756 /// Returns the friendly description for a token kind that will appear
757 /// without quotes in diagnostic messages. These strings may be translatable in
758 /// future.
getTokenDescForDiagnostic(tok::TokenKind Kind)759 static const char *getTokenDescForDiagnostic(tok::TokenKind Kind) {
760 switch (Kind) {
761 case tok::identifier:
762 return "identifier";
763 default:
764 return nullptr;
765 }
766 }
767
768 /// FormatDiagnostic - Format this diagnostic into a string, substituting the
769 /// formal arguments into the %0 slots. The result is appended onto the Str
770 /// array.
771 void Diagnostic::
FormatDiagnostic(SmallVectorImpl<char> & OutStr) const772 FormatDiagnostic(SmallVectorImpl<char> &OutStr) const {
773 if (!StoredDiagMessage.empty()) {
774 OutStr.append(StoredDiagMessage.begin(), StoredDiagMessage.end());
775 return;
776 }
777
778 StringRef Diag =
779 getDiags()->getDiagnosticIDs()->getDescription(getID());
780
781 FormatDiagnostic(Diag.begin(), Diag.end(), OutStr);
782 }
783
784 void Diagnostic::
FormatDiagnostic(const char * DiagStr,const char * DiagEnd,SmallVectorImpl<char> & OutStr) const785 FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
786 SmallVectorImpl<char> &OutStr) const {
787 // When the diagnostic string is only "%0", the entire string is being given
788 // by an outside source. Remove unprintable characters from this string
789 // and skip all the other string processing.
790 if (DiagEnd - DiagStr == 2 &&
791 StringRef(DiagStr, DiagEnd - DiagStr).equals("%0") &&
792 getArgKind(0) == DiagnosticsEngine::ak_std_string) {
793 const std::string &S = getArgStdStr(0);
794 for (char c : S) {
795 if (llvm::sys::locale::isPrint(c) || c == '\t') {
796 OutStr.push_back(c);
797 }
798 }
799 return;
800 }
801
802 /// FormattedArgs - Keep track of all of the arguments formatted by
803 /// ConvertArgToString and pass them into subsequent calls to
804 /// ConvertArgToString, allowing the implementation to avoid redundancies in
805 /// obvious cases.
806 SmallVector<DiagnosticsEngine::ArgumentValue, 8> FormattedArgs;
807
808 /// QualTypeVals - Pass a vector of arrays so that QualType names can be
809 /// compared to see if more information is needed to be printed.
810 SmallVector<intptr_t, 2> QualTypeVals;
811 SmallVector<char, 64> Tree;
812
813 for (unsigned i = 0, e = getNumArgs(); i < e; ++i)
814 if (getArgKind(i) == DiagnosticsEngine::ak_qualtype)
815 QualTypeVals.push_back(getRawArg(i));
816
817 while (DiagStr != DiagEnd) {
818 if (DiagStr[0] != '%') {
819 // Append non-%0 substrings to Str if we have one.
820 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
821 OutStr.append(DiagStr, StrEnd);
822 DiagStr = StrEnd;
823 continue;
824 } else if (isPunctuation(DiagStr[1])) {
825 OutStr.push_back(DiagStr[1]); // %% -> %.
826 DiagStr += 2;
827 continue;
828 }
829
830 // Skip the %.
831 ++DiagStr;
832
833 // This must be a placeholder for a diagnostic argument. The format for a
834 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
835 // The digit is a number from 0-9 indicating which argument this comes from.
836 // The modifier is a string of digits from the set [-a-z]+, arguments is a
837 // brace enclosed string.
838 const char *Modifier = nullptr, *Argument = nullptr;
839 unsigned ModifierLen = 0, ArgumentLen = 0;
840
841 // Check to see if we have a modifier. If so eat it.
842 if (!isDigit(DiagStr[0])) {
843 Modifier = DiagStr;
844 while (DiagStr[0] == '-' ||
845 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
846 ++DiagStr;
847 ModifierLen = DiagStr-Modifier;
848
849 // If we have an argument, get it next.
850 if (DiagStr[0] == '{') {
851 ++DiagStr; // Skip {.
852 Argument = DiagStr;
853
854 DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
855 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
856 ArgumentLen = DiagStr-Argument;
857 ++DiagStr; // Skip }.
858 }
859 }
860
861 assert(isDigit(*DiagStr) && "Invalid format for argument in diagnostic");
862 unsigned ArgNo = *DiagStr++ - '0';
863
864 // Only used for type diffing.
865 unsigned ArgNo2 = ArgNo;
866
867 DiagnosticsEngine::ArgumentKind Kind = getArgKind(ArgNo);
868 if (ModifierIs(Modifier, ModifierLen, "diff")) {
869 assert(*DiagStr == ',' && isDigit(*(DiagStr + 1)) &&
870 "Invalid format for diff modifier");
871 ++DiagStr; // Comma.
872 ArgNo2 = *DiagStr++ - '0';
873 DiagnosticsEngine::ArgumentKind Kind2 = getArgKind(ArgNo2);
874 if (Kind == DiagnosticsEngine::ak_qualtype &&
875 Kind2 == DiagnosticsEngine::ak_qualtype)
876 Kind = DiagnosticsEngine::ak_qualtype_pair;
877 else {
878 // %diff only supports QualTypes. For other kinds of arguments,
879 // use the default printing. For example, if the modifier is:
880 // "%diff{compare $ to $|other text}1,2"
881 // treat it as:
882 // "compare %1 to %2"
883 const char *ArgumentEnd = Argument + ArgumentLen;
884 const char *Pipe = ScanFormat(Argument, ArgumentEnd, '|');
885 assert(ScanFormat(Pipe + 1, ArgumentEnd, '|') == ArgumentEnd &&
886 "Found too many '|'s in a %diff modifier!");
887 const char *FirstDollar = ScanFormat(Argument, Pipe, '$');
888 const char *SecondDollar = ScanFormat(FirstDollar + 1, Pipe, '$');
889 const char ArgStr1[] = { '%', static_cast<char>('0' + ArgNo) };
890 const char ArgStr2[] = { '%', static_cast<char>('0' + ArgNo2) };
891 FormatDiagnostic(Argument, FirstDollar, OutStr);
892 FormatDiagnostic(ArgStr1, ArgStr1 + 2, OutStr);
893 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
894 FormatDiagnostic(ArgStr2, ArgStr2 + 2, OutStr);
895 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
896 continue;
897 }
898 }
899
900 switch (Kind) {
901 // ---- STRINGS ----
902 case DiagnosticsEngine::ak_std_string: {
903 const std::string &S = getArgStdStr(ArgNo);
904 assert(ModifierLen == 0 && "No modifiers for strings yet");
905 OutStr.append(S.begin(), S.end());
906 break;
907 }
908 case DiagnosticsEngine::ak_c_string: {
909 const char *S = getArgCStr(ArgNo);
910 assert(ModifierLen == 0 && "No modifiers for strings yet");
911
912 // Don't crash if get passed a null pointer by accident.
913 if (!S)
914 S = "(null)";
915
916 OutStr.append(S, S + strlen(S));
917 break;
918 }
919 // ---- INTEGERS ----
920 case DiagnosticsEngine::ak_sint: {
921 int Val = getArgSInt(ArgNo);
922
923 if (ModifierIs(Modifier, ModifierLen, "select")) {
924 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen,
925 OutStr);
926 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
927 HandleIntegerSModifier(Val, OutStr);
928 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
929 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
930 OutStr);
931 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
932 HandleOrdinalModifier((unsigned)Val, OutStr);
933 } else {
934 assert(ModifierLen == 0 && "Unknown integer modifier");
935 llvm::raw_svector_ostream(OutStr) << Val;
936 }
937 break;
938 }
939 case DiagnosticsEngine::ak_uint: {
940 unsigned Val = getArgUInt(ArgNo);
941
942 if (ModifierIs(Modifier, ModifierLen, "select")) {
943 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
944 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
945 HandleIntegerSModifier(Val, OutStr);
946 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
947 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
948 OutStr);
949 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
950 HandleOrdinalModifier(Val, OutStr);
951 } else {
952 assert(ModifierLen == 0 && "Unknown integer modifier");
953 llvm::raw_svector_ostream(OutStr) << Val;
954 }
955 break;
956 }
957 // ---- TOKEN SPELLINGS ----
958 case DiagnosticsEngine::ak_tokenkind: {
959 tok::TokenKind Kind = static_cast<tok::TokenKind>(getRawArg(ArgNo));
960 assert(ModifierLen == 0 && "No modifiers for token kinds yet");
961
962 llvm::raw_svector_ostream Out(OutStr);
963 if (const char *S = tok::getPunctuatorSpelling(Kind))
964 // Quoted token spelling for punctuators.
965 Out << '\'' << S << '\'';
966 else if (const char *S = tok::getKeywordSpelling(Kind))
967 // Unquoted token spelling for keywords.
968 Out << S;
969 else if (const char *S = getTokenDescForDiagnostic(Kind))
970 // Unquoted translatable token name.
971 Out << S;
972 else if (const char *S = tok::getTokenName(Kind))
973 // Debug name, shouldn't appear in user-facing diagnostics.
974 Out << '<' << S << '>';
975 else
976 Out << "(null)";
977 break;
978 }
979 // ---- NAMES and TYPES ----
980 case DiagnosticsEngine::ak_identifierinfo: {
981 const IdentifierInfo *II = getArgIdentifier(ArgNo);
982 assert(ModifierLen == 0 && "No modifiers for strings yet");
983
984 // Don't crash if get passed a null pointer by accident.
985 if (!II) {
986 const char *S = "(null)";
987 OutStr.append(S, S + strlen(S));
988 continue;
989 }
990
991 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
992 break;
993 }
994 case DiagnosticsEngine::ak_addrspace:
995 case DiagnosticsEngine::ak_qual:
996 case DiagnosticsEngine::ak_qualtype:
997 case DiagnosticsEngine::ak_declarationname:
998 case DiagnosticsEngine::ak_nameddecl:
999 case DiagnosticsEngine::ak_nestednamespec:
1000 case DiagnosticsEngine::ak_declcontext:
1001 case DiagnosticsEngine::ak_attr:
1002 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
1003 StringRef(Modifier, ModifierLen),
1004 StringRef(Argument, ArgumentLen),
1005 FormattedArgs,
1006 OutStr, QualTypeVals);
1007 break;
1008 case DiagnosticsEngine::ak_qualtype_pair: {
1009 // Create a struct with all the info needed for printing.
1010 TemplateDiffTypes TDT;
1011 TDT.FromType = getRawArg(ArgNo);
1012 TDT.ToType = getRawArg(ArgNo2);
1013 TDT.ElideType = getDiags()->ElideType;
1014 TDT.ShowColors = getDiags()->ShowColors;
1015 TDT.TemplateDiffUsed = false;
1016 intptr_t val = reinterpret_cast<intptr_t>(&TDT);
1017
1018 const char *ArgumentEnd = Argument + ArgumentLen;
1019 const char *Pipe = ScanFormat(Argument, ArgumentEnd, '|');
1020
1021 // Print the tree. If this diagnostic already has a tree, skip the
1022 // second tree.
1023 if (getDiags()->PrintTemplateTree && Tree.empty()) {
1024 TDT.PrintFromType = true;
1025 TDT.PrintTree = true;
1026 getDiags()->ConvertArgToString(Kind, val,
1027 StringRef(Modifier, ModifierLen),
1028 StringRef(Argument, ArgumentLen),
1029 FormattedArgs,
1030 Tree, QualTypeVals);
1031 // If there is no tree information, fall back to regular printing.
1032 if (!Tree.empty()) {
1033 FormatDiagnostic(Pipe + 1, ArgumentEnd, OutStr);
1034 break;
1035 }
1036 }
1037
1038 // Non-tree printing, also the fall-back when tree printing fails.
1039 // The fall-back is triggered when the types compared are not templates.
1040 const char *FirstDollar = ScanFormat(Argument, ArgumentEnd, '$');
1041 const char *SecondDollar = ScanFormat(FirstDollar + 1, ArgumentEnd, '$');
1042
1043 // Append before text
1044 FormatDiagnostic(Argument, FirstDollar, OutStr);
1045
1046 // Append first type
1047 TDT.PrintTree = false;
1048 TDT.PrintFromType = true;
1049 getDiags()->ConvertArgToString(Kind, val,
1050 StringRef(Modifier, ModifierLen),
1051 StringRef(Argument, ArgumentLen),
1052 FormattedArgs,
1053 OutStr, QualTypeVals);
1054 if (!TDT.TemplateDiffUsed)
1055 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
1056 TDT.FromType));
1057
1058 // Append middle text
1059 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
1060
1061 // Append second type
1062 TDT.PrintFromType = false;
1063 getDiags()->ConvertArgToString(Kind, val,
1064 StringRef(Modifier, ModifierLen),
1065 StringRef(Argument, ArgumentLen),
1066 FormattedArgs,
1067 OutStr, QualTypeVals);
1068 if (!TDT.TemplateDiffUsed)
1069 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
1070 TDT.ToType));
1071
1072 // Append end text
1073 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
1074 break;
1075 }
1076 }
1077
1078 // Remember this argument info for subsequent formatting operations. Turn
1079 // std::strings into a null terminated string to make it be the same case as
1080 // all the other ones.
1081 if (Kind == DiagnosticsEngine::ak_qualtype_pair)
1082 continue;
1083 else if (Kind != DiagnosticsEngine::ak_std_string)
1084 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
1085 else
1086 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_c_string,
1087 (intptr_t)getArgStdStr(ArgNo).c_str()));
1088 }
1089
1090 // Append the type tree to the end of the diagnostics.
1091 OutStr.append(Tree.begin(), Tree.end());
1092 }
1093
StoredDiagnostic(DiagnosticsEngine::Level Level,unsigned ID,StringRef Message)1094 StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
1095 StringRef Message)
1096 : ID(ID), Level(Level), Message(Message) {}
1097
StoredDiagnostic(DiagnosticsEngine::Level Level,const Diagnostic & Info)1098 StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level,
1099 const Diagnostic &Info)
1100 : ID(Info.getID()), Level(Level) {
1101 assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
1102 "Valid source location without setting a source manager for diagnostic");
1103 if (Info.getLocation().isValid())
1104 Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
1105 SmallString<64> Message;
1106 Info.FormatDiagnostic(Message);
1107 this->Message.assign(Message.begin(), Message.end());
1108 this->Ranges.assign(Info.getRanges().begin(), Info.getRanges().end());
1109 this->FixIts.assign(Info.getFixItHints().begin(), Info.getFixItHints().end());
1110 }
1111
StoredDiagnostic(DiagnosticsEngine::Level Level,unsigned ID,StringRef Message,FullSourceLoc Loc,ArrayRef<CharSourceRange> Ranges,ArrayRef<FixItHint> FixIts)1112 StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
1113 StringRef Message, FullSourceLoc Loc,
1114 ArrayRef<CharSourceRange> Ranges,
1115 ArrayRef<FixItHint> FixIts)
1116 : ID(ID), Level(Level), Loc(Loc), Message(Message),
1117 Ranges(Ranges.begin(), Ranges.end()), FixIts(FixIts.begin(), FixIts.end())
1118 {
1119 }
1120
1121 /// IncludeInDiagnosticCounts - This method (whose default implementation
1122 /// returns true) indicates whether the diagnostics handled by this
1123 /// DiagnosticConsumer should be included in the number of diagnostics
1124 /// reported by DiagnosticsEngine.
IncludeInDiagnosticCounts() const1125 bool DiagnosticConsumer::IncludeInDiagnosticCounts() const { return true; }
1126
anchor()1127 void IgnoringDiagConsumer::anchor() {}
1128
1129 ForwardingDiagnosticConsumer::~ForwardingDiagnosticConsumer() = default;
1130
HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,const Diagnostic & Info)1131 void ForwardingDiagnosticConsumer::HandleDiagnostic(
1132 DiagnosticsEngine::Level DiagLevel,
1133 const Diagnostic &Info) {
1134 Target.HandleDiagnostic(DiagLevel, Info);
1135 }
1136
clear()1137 void ForwardingDiagnosticConsumer::clear() {
1138 DiagnosticConsumer::clear();
1139 Target.clear();
1140 }
1141
IncludeInDiagnosticCounts() const1142 bool ForwardingDiagnosticConsumer::IncludeInDiagnosticCounts() const {
1143 return Target.IncludeInDiagnosticCounts();
1144 }
1145
DiagStorageAllocator()1146 PartialDiagnostic::DiagStorageAllocator::DiagStorageAllocator() {
1147 for (unsigned I = 0; I != NumCached; ++I)
1148 FreeList[I] = Cached + I;
1149 NumFreeListEntries = NumCached;
1150 }
1151
~DiagStorageAllocator()1152 PartialDiagnostic::DiagStorageAllocator::~DiagStorageAllocator() {
1153 // Don't assert if we are in a CrashRecovery context, as this invariant may
1154 // be invalidated during a crash.
1155 assert((NumFreeListEntries == NumCached ||
1156 llvm::CrashRecoveryContext::isRecoveringFromCrash()) &&
1157 "A partial is on the lam");
1158 }
1159
1160 char DiagnosticError::ID;
1161