clang 17.0.0git
Diagnostic.cpp
Go to the documentation of this file.
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
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/ConvertUTF.h"
29#include "llvm/Support/CrashRecoveryContext.h"
30#include "llvm/Support/Unicode.h"
31#include "llvm/Support/raw_ostream.h"
32#include <algorithm>
33#include <cassert>
34#include <cstddef>
35#include <cstdint>
36#include <cstring>
37#include <limits>
38#include <string>
39#include <utility>
40#include <vector>
41
42using namespace clang;
43
45 DiagNullabilityKind nullability) {
46 DB.AddString(
47 ("'" +
48 getNullabilitySpelling(nullability.first,
49 /*isContextSensitive=*/nullability.second) +
50 "'")
51 .str());
52 return DB;
53}
54
56 llvm::Error &&E) {
57 DB.AddString(toString(std::move(E)));
58 return DB;
59}
60
62 StringRef Modifier, StringRef Argument,
65 void *Cookie,
66 ArrayRef<intptr_t> QualTypeVals) {
67 StringRef Str = "<can't format argument>";
68 Output.append(Str.begin(), Str.end());
69}
70
74 bool ShouldOwnClient)
75 : Diags(std::move(diags)), DiagOpts(std::move(DiagOpts)) {
76 setClient(client, ShouldOwnClient);
77 ArgToStringFn = DummyArgToStringFn;
78
79 Reset();
80}
81
83 // If we own the diagnostic client, destroy it first so that it can access the
84 // engine from its destructor.
85 setClient(nullptr);
86}
87
89 DiagStatesByLoc.dump(*SourceMgr);
90}
91
92void DiagnosticsEngine::dump(StringRef DiagName) const {
93 DiagStatesByLoc.dump(*SourceMgr, DiagName);
94}
95
97 bool ShouldOwnClient) {
98 Owner.reset(ShouldOwnClient ? client : nullptr);
99 Client = client;
100}
101
103 DiagStateOnPushStack.push_back(GetCurDiagState());
104}
105
107 if (DiagStateOnPushStack.empty())
108 return false;
109
110 if (DiagStateOnPushStack.back() != GetCurDiagState()) {
111 // State changed at some point between push/pop.
112 PushDiagStatePoint(DiagStateOnPushStack.back(), Loc);
113 }
114 DiagStateOnPushStack.pop_back();
115 return true;
116}
117
118void DiagnosticsEngine::Reset(bool soft /*=false*/) {
119 ErrorOccurred = false;
120 UncompilableErrorOccurred = false;
121 FatalErrorOccurred = false;
122 UnrecoverableErrorOccurred = false;
123
124 NumWarnings = 0;
125 NumErrors = 0;
126 TrapNumErrorsOccurred = 0;
127 TrapNumUnrecoverableErrorsOccurred = 0;
128
129 CurDiagID = std::numeric_limits<unsigned>::max();
130 LastDiagLevel = DiagnosticIDs::Ignored;
131 DelayedDiagID = 0;
132
133 if (!soft) {
134 // Clear state related to #pragma diagnostic.
135 DiagStates.clear();
136 DiagStatesByLoc.clear();
137 DiagStateOnPushStack.clear();
138
139 // Create a DiagState and DiagStatePoint representing diagnostic changes
140 // through command-line.
141 DiagStates.emplace_back();
142 DiagStatesByLoc.appendFirst(&DiagStates.back());
143 }
144}
145
146void DiagnosticsEngine::SetDelayedDiagnostic(unsigned DiagID, StringRef Arg1,
147 StringRef Arg2, StringRef Arg3) {
148 if (DelayedDiagID)
149 return;
150
151 DelayedDiagID = DiagID;
152 DelayedDiagArg1 = Arg1.str();
153 DelayedDiagArg2 = Arg2.str();
154 DelayedDiagArg3 = Arg3.str();
155}
156
157void DiagnosticsEngine::ReportDelayed() {
158 unsigned ID = DelayedDiagID;
159 DelayedDiagID = 0;
160 Report(ID) << DelayedDiagArg1 << DelayedDiagArg2 << DelayedDiagArg3;
161}
162
163void DiagnosticsEngine::DiagStateMap::appendFirst(DiagState *State) {
164 assert(Files.empty() && "not first");
165 FirstDiagState = CurDiagState = State;
166 CurDiagStateLoc = SourceLocation();
167}
168
169void DiagnosticsEngine::DiagStateMap::append(SourceManager &SrcMgr,
170 SourceLocation Loc,
171 DiagState *State) {
172 CurDiagState = State;
173 CurDiagStateLoc = Loc;
174
175 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");
182
183 if (Last.Offset == Offset) {
184 if (Last.State == State)
185 break;
186 Last.State = State;
187 continue;
188 }
189
190 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 =
209 llvm::partition_point(StateTransitions, [=](const DiagStatePoint &P) {
210 return P.Offset <= Offset;
211 });
212 assert(OnePastIt != StateTransitions.begin() && "missing initial state");
213 return OnePastIt[-1].State;
214}
215
216DiagnosticsEngine::DiagStateMap::File *
217DiagnosticsEngine::DiagStateMap::getFile(SourceManager &SrcMgr,
218 FileID ID) const {
219 // Get or insert the File for this ID.
220 auto Range = Files.equal_range(ID);
221 if (Range.first != Range.second)
222 return &Range.first->second;
223 auto &F = Files.insert(Range.first, std::make_pair(ID, File()))->second;
224
225 // We created a new File; look up the diagnostic state at the start of it and
226 // initialize it.
227 if (ID.isValid()) {
228 std::pair<FileID, unsigned> Decomp = SrcMgr.getDecomposedIncludedLoc(ID);
229 F.Parent = getFile(SrcMgr, Decomp.first);
230 F.ParentOffset = Decomp.second;
231 F.StateTransitions.push_back({F.Parent->lookup(Decomp.second), 0});
232 } else {
233 // This is the (imaginary) root file into which we pretend all top-level
234 // files are included; it descends from the initial state.
235 //
236 // FIXME: This doesn't guarantee that we use the same ordering as
237 // isBeforeInTranslationUnit in the cases where someone invented another
238 // top-level file and added diagnostic pragmas to it. See the code at the
239 // end of isBeforeInTranslationUnit for the quirks it deals with.
240 F.StateTransitions.push_back({FirstDiagState, 0});
241 }
242 return &F;
243}
244
245void DiagnosticsEngine::DiagStateMap::dump(SourceManager &SrcMgr,
246 StringRef DiagName) const {
247 llvm::errs() << "diagnostic state at ";
248 CurDiagStateLoc.print(llvm::errs(), SrcMgr);
249 llvm::errs() << ": " << CurDiagState << "\n";
250
251 for (auto &F : Files) {
252 FileID ID = F.first;
253 File &File = F.second;
254
255 bool PrintedOuterHeading = false;
256 auto PrintOuterHeading = [&] {
257 if (PrintedOuterHeading) return;
258 PrintedOuterHeading = true;
259
260 llvm::errs() << "File " << &File << " <FileID " << ID.getHashValue()
261 << ">: " << SrcMgr.getBufferOrFake(ID).getBufferIdentifier();
262
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)
271 .print(llvm::errs(), SrcMgr);
272 }
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)
291 .print(llvm::errs(), SrcMgr);
292 llvm::errs() << ": state " << Transition.State << ":\n";
293 };
294
295 if (DiagName.empty())
296 PrintInnerHeading();
297
298 for (auto &Mapping : *Transition.State) {
299 StringRef Option =
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
336void DiagnosticsEngine::PushDiagStatePoint(DiagState *State,
337 SourceLocation Loc) {
338 assert(Loc.isValid() && "Adding invalid loc point");
339 DiagStatesByLoc.append(*SourceMgr, Loc, State);
340}
341
343 SourceLocation L) {
344 assert(Diag < diag::DIAG_UPPER_LIMIT &&
345 "Can only map builtin diagnostics");
346 assert((Diags->isBuiltinWarningOrExtension(Diag) ||
347 (Map == diag::Severity::Fatal || Map == diag::Severity::Error)) &&
348 "Cannot map errors into warnings!");
349 assert((L.isInvalid() || SourceMgr) && "No SourceMgr for valid location");
350
351 // Don't allow a mapping to a warning override an error/fatal mapping.
352 bool WasUpgradedFromWarning = false;
353 if (Map == diag::Severity::Warning) {
354 DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag);
355 if (Info.getSeverity() == diag::Severity::Error ||
357 Map = Info.getSeverity();
358 WasUpgradedFromWarning = true;
359 }
360 }
361 DiagnosticMapping Mapping = makeUserMapping(Map, L);
362 Mapping.setUpgradedFromWarning(WasUpgradedFromWarning);
363
364 // Make sure we propagate the NoWarningAsError flag from an existing
365 // mapping (which may be the default mapping).
366 DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag);
368 Mapping.hasNoWarningAsError());
369
370 // Common case; setting all the diagnostics of a group in one place.
371 if ((L.isInvalid() || L == DiagStatesByLoc.getCurDiagStateLoc()) &&
372 DiagStatesByLoc.getCurDiagState()) {
373 // FIXME: This is theoretically wrong: if the current state is shared with
374 // some other location (via push/pop) we will change the state for that
375 // other location as well. This cannot currently happen, as we can't update
376 // the diagnostic state at the same location at which we pop.
377 DiagStatesByLoc.getCurDiagState()->setMapping(Diag, Mapping);
378 return;
379 }
380
381 // A diagnostic pragma occurred, create a new DiagState initialized with
382 // the current one and a new DiagStatePoint to record at which location
383 // the new state became active.
384 DiagStates.push_back(*GetCurDiagState());
385 DiagStates.back().setMapping(Diag, Mapping);
386 PushDiagStatePoint(&DiagStates.back(), L);
387}
388
390 StringRef Group, diag::Severity Map,
391 SourceLocation Loc) {
392 // Get the diagnostics in this group.
394 if (Diags->getDiagnosticsInGroup(Flavor, Group, GroupDiags))
395 return true;
396
397 // Set the mapping.
398 for (diag::kind Diag : GroupDiags)
399 setSeverity(Diag, Map, Loc);
400
401 return false;
402}
403
405 diag::Group Group,
406 diag::Severity Map,
407 SourceLocation Loc) {
408 return setSeverityForGroup(Flavor, Diags->getWarningOptionForGroup(Group),
409 Map, Loc);
410}
411
413 bool Enabled) {
414 // If we are enabling this feature, just set the diagnostic mappings to map to
415 // errors.
416 if (Enabled)
419
420 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
421 // potentially downgrade anything already mapped to be a warning.
422
423 // Get the diagnostics in this group.
425 if (Diags->getDiagnosticsInGroup(diag::Flavor::WarningOrError, Group,
426 GroupDiags))
427 return true;
428
429 // Perform the mapping change.
430 for (diag::kind Diag : GroupDiags) {
431 DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag);
432
433 if (Info.getSeverity() == diag::Severity::Error ||
436
437 Info.setNoWarningAsError(true);
438 }
439
440 return false;
441}
442
444 bool Enabled) {
445 // If we are enabling this feature, just set the diagnostic mappings to map to
446 // fatal errors.
447 if (Enabled)
450
451 // Otherwise, we want to set the diagnostic mapping's "no Wfatal-errors" bit,
452 // and potentially downgrade anything already mapped to be a fatal error.
453
454 // Get the diagnostics in this group.
456 if (Diags->getDiagnosticsInGroup(diag::Flavor::WarningOrError, Group,
457 GroupDiags))
458 return true;
459
460 // Perform the mapping change.
461 for (diag::kind Diag : GroupDiags) {
462 DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag);
463
466
467 Info.setNoErrorAsFatal(true);
468 }
469
470 return false;
471}
472
474 diag::Severity Map,
475 SourceLocation Loc) {
476 // Get all the diagnostics.
477 std::vector<diag::kind> AllDiags;
478 DiagnosticIDs::getAllDiagnostics(Flavor, AllDiags);
479
480 // Set the mapping.
481 for (diag::kind Diag : AllDiags)
482 if (Diags->isBuiltinWarningOrExtension(Diag))
483 setSeverity(Diag, Map, Loc);
484}
485
487 assert(CurDiagID == std::numeric_limits<unsigned>::max() &&
488 "Multiple diagnostics in flight at once!");
489
490 CurDiagLoc = storedDiag.getLocation();
491 CurDiagID = storedDiag.getID();
492 DiagStorage.NumDiagArgs = 0;
493
494 DiagStorage.DiagRanges.clear();
495 DiagStorage.DiagRanges.append(storedDiag.range_begin(),
496 storedDiag.range_end());
497
498 DiagStorage.FixItHints.clear();
499 DiagStorage.FixItHints.append(storedDiag.fixit_begin(),
500 storedDiag.fixit_end());
501
502 assert(Client && "DiagnosticConsumer not set!");
503 Level DiagLevel = storedDiag.getLevel();
504 Diagnostic Info(this, storedDiag.getMessage());
505 Client->HandleDiagnostic(DiagLevel, Info);
506 if (Client->IncludeInDiagnosticCounts()) {
507 if (DiagLevel == DiagnosticsEngine::Warning)
508 ++NumWarnings;
509 }
510
511 CurDiagID = std::numeric_limits<unsigned>::max();
512}
513
515 assert(getClient() && "DiagnosticClient not set!");
516
517 bool Emitted;
518 if (Force) {
519 Diagnostic Info(this);
520
521 // Figure out the diagnostic level of this message.
522 DiagnosticIDs::Level DiagLevel
523 = Diags->getDiagnosticLevel(Info.getID(), Info.getLocation(), *this);
524
525 Emitted = (DiagLevel != DiagnosticIDs::Ignored);
526 if (Emitted) {
527 // Emit the diagnostic regardless of suppression level.
528 Diags->EmitDiag(*this, DiagLevel);
529 }
530 } else {
531 // Process the diagnostic, sending the accumulated information to the
532 // DiagnosticConsumer.
533 Emitted = ProcessDiag();
534 }
535
536 // Clear out the current diagnostic object.
537 Clear();
538
539 // If there was a delayed diagnostic, emit it now.
540 if (!Force && DelayedDiagID)
541 ReportDelayed();
542
543 return Emitted;
544}
545
547
549 const Diagnostic &Info) {
551 return;
552
553 if (DiagLevel == DiagnosticsEngine::Warning)
554 ++NumWarnings;
555 else if (DiagLevel >= DiagnosticsEngine::Error)
556 ++NumErrors;
557}
558
559/// ModifierIs - Return true if the specified modifier matches specified string.
560template <std::size_t StrLen>
561static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
562 const char (&Str)[StrLen]) {
563 return StrLen-1 == ModifierLen && memcmp(Modifier, Str, StrLen-1) == 0;
564}
565
566/// ScanForward - Scans forward, looking for the given character, skipping
567/// nested clauses and escaped characters.
568static const char *ScanFormat(const char *I, const char *E, char Target) {
569 unsigned Depth = 0;
570
571 for ( ; I != E; ++I) {
572 if (Depth == 0 && *I == Target) return I;
573 if (Depth != 0 && *I == '}') Depth--;
574
575 if (*I == '%') {
576 I++;
577 if (I == E) break;
578
579 // Escaped characters get implicitly skipped here.
580
581 // Format specifier.
582 if (!isDigit(*I) && !isPunctuation(*I)) {
583 for (I++; I != E && !isDigit(*I) && *I != '{'; I++) ;
584 if (I == E) break;
585 if (*I == '{')
586 Depth++;
587 }
588 }
589 }
590 return E;
591}
592
593/// HandleSelectModifier - Handle the integer 'select' modifier. This is used
594/// like this: %select{foo|bar|baz}2. This means that the integer argument
595/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
596/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
597/// This is very useful for certain classes of variant diagnostics.
598static void HandleSelectModifier(const Diagnostic &DInfo, unsigned ValNo,
599 const char *Argument, unsigned ArgumentLen,
600 SmallVectorImpl<char> &OutStr) {
601 const char *ArgumentEnd = Argument+ArgumentLen;
602
603 // Skip over 'ValNo' |'s.
604 while (ValNo) {
605 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
606 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
607 " larger than the number of options in the diagnostic string!");
608 Argument = NextVal+1; // Skip this string.
609 --ValNo;
610 }
611
612 // Get the end of the value. This is either the } or the |.
613 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
614
615 // Recursively format the result of the select clause into the output string.
616 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
617}
618
619/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
620/// letter 's' to the string if the value is not 1. This is used in cases like
621/// this: "you idiot, you have %4 parameter%s4!".
622static void HandleIntegerSModifier(unsigned ValNo,
623 SmallVectorImpl<char> &OutStr) {
624 if (ValNo != 1)
625 OutStr.push_back('s');
626}
627
628/// HandleOrdinalModifier - Handle the integer 'ord' modifier. This
629/// prints the ordinal form of the given integer, with 1 corresponding
630/// to the first ordinal. Currently this is hard-coded to use the
631/// English form.
632static void HandleOrdinalModifier(unsigned ValNo,
633 SmallVectorImpl<char> &OutStr) {
634 assert(ValNo != 0 && "ValNo must be strictly positive!");
635
636 llvm::raw_svector_ostream Out(OutStr);
637
638 // We could use text forms for the first N ordinals, but the numeric
639 // forms are actually nicer in diagnostics because they stand out.
640 Out << ValNo << llvm::getOrdinalSuffix(ValNo);
641}
642
643/// PluralNumber - Parse an unsigned integer and advance Start.
644static unsigned PluralNumber(const char *&Start, const char *End) {
645 // Programming 101: Parse a decimal number :-)
646 unsigned Val = 0;
647 while (Start != End && *Start >= '0' && *Start <= '9') {
648 Val *= 10;
649 Val += *Start - '0';
650 ++Start;
651 }
652 return Val;
653}
654
655/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
656static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
657 if (*Start != '[') {
658 unsigned Ref = PluralNumber(Start, End);
659 return Ref == Val;
660 }
661
662 ++Start;
663 unsigned Low = PluralNumber(Start, End);
664 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
665 ++Start;
666 unsigned High = PluralNumber(Start, End);
667 assert(*Start == ']' && "Bad plural expression syntax: expected )");
668 ++Start;
669 return Low <= Val && Val <= High;
670}
671
672/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
673static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
674 // Empty condition?
675 if (*Start == ':')
676 return true;
677
678 while (true) {
679 char C = *Start;
680 if (C == '%') {
681 // Modulo expression
682 ++Start;
683 unsigned Arg = PluralNumber(Start, End);
684 assert(*Start == '=' && "Bad plural expression syntax: expected =");
685 ++Start;
686 unsigned ValMod = ValNo % Arg;
687 if (TestPluralRange(ValMod, Start, End))
688 return true;
689 } else {
690 assert((C == '[' || (C >= '0' && C <= '9')) &&
691 "Bad plural expression syntax: unexpected character");
692 // Range expression
693 if (TestPluralRange(ValNo, Start, End))
694 return true;
695 }
696
697 // Scan for next or-expr part.
698 Start = std::find(Start, End, ',');
699 if (Start == End)
700 break;
701 ++Start;
702 }
703 return false;
704}
705
706/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
707/// for complex plural forms, or in languages where all plurals are complex.
708/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
709/// conditions that are tested in order, the form corresponding to the first
710/// that applies being emitted. The empty condition is always true, making the
711/// last form a default case.
712/// Conditions are simple boolean expressions, where n is the number argument.
713/// Here are the rules.
714/// condition := expression | empty
715/// empty := -> always true
716/// expression := numeric [',' expression] -> logical or
717/// numeric := range -> true if n in range
718/// | '%' number '=' range -> true if n % number in range
719/// range := number
720/// | '[' number ',' number ']' -> ranges are inclusive both ends
721///
722/// Here are some examples from the GNU gettext manual written in this form:
723/// English:
724/// {1:form0|:form1}
725/// Latvian:
726/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
727/// Gaeilge:
728/// {1:form0|2:form1|:form2}
729/// Romanian:
730/// {1:form0|0,%100=[1,19]:form1|:form2}
731/// Lithuanian:
732/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
733/// Russian (requires repeated form):
734/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
735/// Slovak
736/// {1:form0|[2,4]:form1|:form2}
737/// Polish (requires repeated form):
738/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
739static void HandlePluralModifier(const Diagnostic &DInfo, unsigned ValNo,
740 const char *Argument, unsigned ArgumentLen,
741 SmallVectorImpl<char> &OutStr) {
742 const char *ArgumentEnd = Argument + ArgumentLen;
743 while (true) {
744 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
745 const char *ExprEnd = Argument;
746 while (*ExprEnd != ':') {
747 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
748 ++ExprEnd;
749 }
750 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
751 Argument = ExprEnd + 1;
752 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
753
754 // Recursively format the result of the plural clause into the
755 // output string.
756 DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr);
757 return;
758 }
759 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
760 }
761}
762
763/// Returns the friendly description for a token kind that will appear
764/// without quotes in diagnostic messages. These strings may be translatable in
765/// future.
767 switch (Kind) {
768 case tok::identifier:
769 return "identifier";
770 default:
771 return nullptr;
772 }
773}
774
775/// FormatDiagnostic - Format this diagnostic into a string, substituting the
776/// formal arguments into the %0 slots. The result is appended onto the Str
777/// array.
780 if (StoredDiagMessage.has_value()) {
781 OutStr.append(StoredDiagMessage->begin(), StoredDiagMessage->end());
782 return;
783 }
784
785 StringRef Diag =
786 getDiags()->getDiagnosticIDs()->getDescription(getID());
787
788 FormatDiagnostic(Diag.begin(), Diag.end(), OutStr);
789}
790
791/// pushEscapedString - Append Str to the diagnostic buffer,
792/// escaping non-printable characters and ill-formed code unit sequences.
793static void pushEscapedString(StringRef Str, SmallVectorImpl<char> &OutStr) {
794 OutStr.reserve(OutStr.size() + Str.size());
795 auto *Begin = reinterpret_cast<const unsigned char *>(Str.data());
796 llvm::raw_svector_ostream OutStream(OutStr);
797 const unsigned char *End = Begin + Str.size();
798 while (Begin != End) {
799 // ASCII case
800 if (isPrintable(*Begin) || isWhitespace(*Begin)) {
801 OutStream << *Begin;
802 ++Begin;
803 continue;
804 }
805 if (llvm::isLegalUTF8Sequence(Begin, End)) {
806 llvm::UTF32 CodepointValue;
807 llvm::UTF32 *CpPtr = &CodepointValue;
808 const unsigned char *CodepointBegin = Begin;
809 const unsigned char *CodepointEnd =
810 Begin + llvm::getNumBytesForUTF8(*Begin);
811 llvm::ConversionResult Res = llvm::ConvertUTF8toUTF32(
812 &Begin, CodepointEnd, &CpPtr, CpPtr + 1, llvm::strictConversion);
813 (void)Res;
814 assert(
815 llvm::conversionOK == Res &&
816 "the sequence is legal UTF-8 but we couldn't convert it to UTF-32");
817 assert(Begin == CodepointEnd &&
818 "we must be further along in the string now");
819 if (llvm::sys::unicode::isPrintable(CodepointValue) ||
820 llvm::sys::unicode::isFormatting(CodepointValue)) {
821 OutStr.append(CodepointBegin, CodepointEnd);
822 continue;
823 }
824 // Unprintable code point.
825 OutStream << "<U+" << llvm::format_hex_no_prefix(CodepointValue, 4, true)
826 << ">";
827 continue;
828 }
829 // Invalid code unit.
830 OutStream << "<" << llvm::format_hex_no_prefix(*Begin, 2, true) << ">";
831 ++Begin;
832 }
833}
834
836FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
837 SmallVectorImpl<char> &OutStr) const {
838 // When the diagnostic string is only "%0", the entire string is being given
839 // by an outside source. Remove unprintable characters from this string
840 // and skip all the other string processing.
841 if (DiagEnd - DiagStr == 2 &&
842 StringRef(DiagStr, DiagEnd - DiagStr).equals("%0") &&
844 const std::string &S = getArgStdStr(0);
845 pushEscapedString(S, OutStr);
846 return;
847 }
848
849 /// FormattedArgs - Keep track of all of the arguments formatted by
850 /// ConvertArgToString and pass them into subsequent calls to
851 /// ConvertArgToString, allowing the implementation to avoid redundancies in
852 /// obvious cases.
854
855 /// QualTypeVals - Pass a vector of arrays so that QualType names can be
856 /// compared to see if more information is needed to be printed.
857 SmallVector<intptr_t, 2> QualTypeVals;
859
860 for (unsigned i = 0, e = getNumArgs(); i < e; ++i)
862 QualTypeVals.push_back(getRawArg(i));
863
864 while (DiagStr != DiagEnd) {
865 if (DiagStr[0] != '%') {
866 // Append non-%0 substrings to Str if we have one.
867 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
868 OutStr.append(DiagStr, StrEnd);
869 DiagStr = StrEnd;
870 continue;
871 } else if (isPunctuation(DiagStr[1])) {
872 OutStr.push_back(DiagStr[1]); // %% -> %.
873 DiagStr += 2;
874 continue;
875 }
876
877 // Skip the %.
878 ++DiagStr;
879
880 // This must be a placeholder for a diagnostic argument. The format for a
881 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
882 // The digit is a number from 0-9 indicating which argument this comes from.
883 // The modifier is a string of digits from the set [-a-z]+, arguments is a
884 // brace enclosed string.
885 const char *Modifier = nullptr, *Argument = nullptr;
886 unsigned ModifierLen = 0, ArgumentLen = 0;
887
888 // Check to see if we have a modifier. If so eat it.
889 if (!isDigit(DiagStr[0])) {
890 Modifier = DiagStr;
891 while (DiagStr[0] == '-' ||
892 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
893 ++DiagStr;
894 ModifierLen = DiagStr-Modifier;
895
896 // If we have an argument, get it next.
897 if (DiagStr[0] == '{') {
898 ++DiagStr; // Skip {.
899 Argument = DiagStr;
900
901 DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
902 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
903 ArgumentLen = DiagStr-Argument;
904 ++DiagStr; // Skip }.
905 }
906 }
907
908 assert(isDigit(*DiagStr) && "Invalid format for argument in diagnostic");
909 unsigned ArgNo = *DiagStr++ - '0';
910
911 // Only used for type diffing.
912 unsigned ArgNo2 = ArgNo;
913
915 if (ModifierIs(Modifier, ModifierLen, "diff")) {
916 assert(*DiagStr == ',' && isDigit(*(DiagStr + 1)) &&
917 "Invalid format for diff modifier");
918 ++DiagStr; // Comma.
919 ArgNo2 = *DiagStr++ - '0';
921 if (Kind == DiagnosticsEngine::ak_qualtype &&
924 else {
925 // %diff only supports QualTypes. For other kinds of arguments,
926 // use the default printing. For example, if the modifier is:
927 // "%diff{compare $ to $|other text}1,2"
928 // treat it as:
929 // "compare %1 to %2"
930 const char *ArgumentEnd = Argument + ArgumentLen;
931 const char *Pipe = ScanFormat(Argument, ArgumentEnd, '|');
932 assert(ScanFormat(Pipe + 1, ArgumentEnd, '|') == ArgumentEnd &&
933 "Found too many '|'s in a %diff modifier!");
934 const char *FirstDollar = ScanFormat(Argument, Pipe, '$');
935 const char *SecondDollar = ScanFormat(FirstDollar + 1, Pipe, '$');
936 const char ArgStr1[] = { '%', static_cast<char>('0' + ArgNo) };
937 const char ArgStr2[] = { '%', static_cast<char>('0' + ArgNo2) };
938 FormatDiagnostic(Argument, FirstDollar, OutStr);
939 FormatDiagnostic(ArgStr1, ArgStr1 + 2, OutStr);
940 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
941 FormatDiagnostic(ArgStr2, ArgStr2 + 2, OutStr);
942 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
943 continue;
944 }
945 }
946
947 switch (Kind) {
948 // ---- STRINGS ----
950 const std::string &S = getArgStdStr(ArgNo);
951 assert(ModifierLen == 0 && "No modifiers for strings yet");
952 pushEscapedString(S, OutStr);
953 break;
954 }
956 const char *S = getArgCStr(ArgNo);
957 assert(ModifierLen == 0 && "No modifiers for strings yet");
958
959 // Don't crash if get passed a null pointer by accident.
960 if (!S)
961 S = "(null)";
962 pushEscapedString(S, OutStr);
963 break;
964 }
965 // ---- INTEGERS ----
967 int64_t Val = getArgSInt(ArgNo);
968
969 if (ModifierIs(Modifier, ModifierLen, "select")) {
970 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen,
971 OutStr);
972 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
973 HandleIntegerSModifier(Val, OutStr);
974 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
975 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
976 OutStr);
977 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
978 HandleOrdinalModifier((unsigned)Val, OutStr);
979 } else {
980 assert(ModifierLen == 0 && "Unknown integer modifier");
981 llvm::raw_svector_ostream(OutStr) << Val;
982 }
983 break;
984 }
986 uint64_t Val = getArgUInt(ArgNo);
987
988 if (ModifierIs(Modifier, ModifierLen, "select")) {
989 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
990 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
991 HandleIntegerSModifier(Val, OutStr);
992 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
993 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
994 OutStr);
995 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
996 HandleOrdinalModifier(Val, OutStr);
997 } else {
998 assert(ModifierLen == 0 && "Unknown integer modifier");
999 llvm::raw_svector_ostream(OutStr) << Val;
1000 }
1001 break;
1002 }
1003 // ---- TOKEN SPELLINGS ----
1005 tok::TokenKind Kind = static_cast<tok::TokenKind>(getRawArg(ArgNo));
1006 assert(ModifierLen == 0 && "No modifiers for token kinds yet");
1007
1008 llvm::raw_svector_ostream Out(OutStr);
1009 if (const char *S = tok::getPunctuatorSpelling(Kind))
1010 // Quoted token spelling for punctuators.
1011 Out << '\'' << S << '\'';
1012 else if ((S = tok::getKeywordSpelling(Kind)))
1013 // Unquoted token spelling for keywords.
1014 Out << S;
1015 else if ((S = getTokenDescForDiagnostic(Kind)))
1016 // Unquoted translatable token name.
1017 Out << S;
1018 else if ((S = tok::getTokenName(Kind)))
1019 // Debug name, shouldn't appear in user-facing diagnostics.
1020 Out << '<' << S << '>';
1021 else
1022 Out << "(null)";
1023 break;
1024 }
1025 // ---- NAMES and TYPES ----
1027 const IdentifierInfo *II = getArgIdentifier(ArgNo);
1028 assert(ModifierLen == 0 && "No modifiers for strings yet");
1029
1030 // Don't crash if get passed a null pointer by accident.
1031 if (!II) {
1032 const char *S = "(null)";
1033 OutStr.append(S, S + strlen(S));
1034 continue;
1035 }
1036
1037 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
1038 break;
1039 }
1048 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
1049 StringRef(Modifier, ModifierLen),
1050 StringRef(Argument, ArgumentLen),
1051 FormattedArgs,
1052 OutStr, QualTypeVals);
1053 break;
1055 // Create a struct with all the info needed for printing.
1057 TDT.FromType = getRawArg(ArgNo);
1058 TDT.ToType = getRawArg(ArgNo2);
1059 TDT.ElideType = getDiags()->ElideType;
1060 TDT.ShowColors = getDiags()->ShowColors;
1061 TDT.TemplateDiffUsed = false;
1062 intptr_t val = reinterpret_cast<intptr_t>(&TDT);
1063
1064 const char *ArgumentEnd = Argument + ArgumentLen;
1065 const char *Pipe = ScanFormat(Argument, ArgumentEnd, '|');
1066
1067 // Print the tree. If this diagnostic already has a tree, skip the
1068 // second tree.
1069 if (getDiags()->PrintTemplateTree && Tree.empty()) {
1070 TDT.PrintFromType = true;
1071 TDT.PrintTree = true;
1072 getDiags()->ConvertArgToString(Kind, val,
1073 StringRef(Modifier, ModifierLen),
1074 StringRef(Argument, ArgumentLen),
1075 FormattedArgs,
1076 Tree, QualTypeVals);
1077 // If there is no tree information, fall back to regular printing.
1078 if (!Tree.empty()) {
1079 FormatDiagnostic(Pipe + 1, ArgumentEnd, OutStr);
1080 break;
1081 }
1082 }
1083
1084 // Non-tree printing, also the fall-back when tree printing fails.
1085 // The fall-back is triggered when the types compared are not templates.
1086 const char *FirstDollar = ScanFormat(Argument, ArgumentEnd, '$');
1087 const char *SecondDollar = ScanFormat(FirstDollar + 1, ArgumentEnd, '$');
1088
1089 // Append before text
1090 FormatDiagnostic(Argument, FirstDollar, OutStr);
1091
1092 // Append first type
1093 TDT.PrintTree = false;
1094 TDT.PrintFromType = true;
1095 getDiags()->ConvertArgToString(Kind, val,
1096 StringRef(Modifier, ModifierLen),
1097 StringRef(Argument, ArgumentLen),
1098 FormattedArgs,
1099 OutStr, QualTypeVals);
1100 if (!TDT.TemplateDiffUsed)
1101 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
1102 TDT.FromType));
1103
1104 // Append middle text
1105 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
1106
1107 // Append second type
1108 TDT.PrintFromType = false;
1109 getDiags()->ConvertArgToString(Kind, val,
1110 StringRef(Modifier, ModifierLen),
1111 StringRef(Argument, ArgumentLen),
1112 FormattedArgs,
1113 OutStr, QualTypeVals);
1114 if (!TDT.TemplateDiffUsed)
1115 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
1116 TDT.ToType));
1117
1118 // Append end text
1119 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
1120 break;
1121 }
1122 }
1123
1124 // Remember this argument info for subsequent formatting operations. Turn
1125 // std::strings into a null terminated string to make it be the same case as
1126 // all the other ones.
1128 continue;
1129 else if (Kind != DiagnosticsEngine::ak_std_string)
1130 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
1131 else
1132 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_c_string,
1133 (intptr_t)getArgStdStr(ArgNo).c_str()));
1134 }
1135
1136 // Append the type tree to the end of the diagnostics.
1137 OutStr.append(Tree.begin(), Tree.end());
1138}
1139
1141 StringRef Message)
1142 : ID(ID), Level(Level), Message(Message) {}
1143
1145 const Diagnostic &Info)
1146 : ID(Info.getID()), Level(Level) {
1147 assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
1148 "Valid source location without setting a source manager for diagnostic");
1149 if (Info.getLocation().isValid())
1150 Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
1151 SmallString<64> Message;
1152 Info.FormatDiagnostic(Message);
1153 this->Message.assign(Message.begin(), Message.end());
1154 this->Ranges.assign(Info.getRanges().begin(), Info.getRanges().end());
1155 this->FixIts.assign(Info.getFixItHints().begin(), Info.getFixItHints().end());
1156}
1157
1159 StringRef Message, FullSourceLoc Loc,
1161 ArrayRef<FixItHint> FixIts)
1162 : ID(ID), Level(Level), Loc(Loc), Message(Message),
1163 Ranges(Ranges.begin(), Ranges.end()), FixIts(FixIts.begin(), FixIts.end())
1164{
1165}
1166
1167llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS,
1168 const StoredDiagnostic &SD) {
1169 if (SD.getLocation().hasManager())
1170 OS << SD.getLocation().printToString(SD.getLocation().getManager()) << ": ";
1171 OS << SD.getMessage();
1172 return OS;
1173}
1174
1175/// IncludeInDiagnosticCounts - This method (whose default implementation
1176/// returns true) indicates whether the diagnostics handled by this
1177/// DiagnosticConsumer should be included in the number of diagnostics
1178/// reported by DiagnosticsEngine.
1180
1181void IgnoringDiagConsumer::anchor() {}
1182
1184
1186 DiagnosticsEngine::Level DiagLevel,
1187 const Diagnostic &Info) {
1188 Target.HandleDiagnostic(DiagLevel, Info);
1189}
1190
1193 Target.clear();
1194}
1195
1197 return Target.IncludeInDiagnosticCounts();
1198}
1199
1200PartialDiagnostic::DiagStorageAllocator::DiagStorageAllocator() {
1201 for (unsigned I = 0; I != NumCached; ++I)
1202 FreeList[I] = Cached + I;
1203 NumFreeListEntries = NumCached;
1204}
1205
1206PartialDiagnostic::DiagStorageAllocator::~DiagStorageAllocator() {
1207 // Don't assert if we are in a CrashRecovery context, as this invariant may
1208 // be invalidated during a crash.
1209 assert((NumFreeListEntries == NumCached ||
1210 llvm::CrashRecoveryContext::isRecoveringFromCrash()) &&
1211 "A partial is on the lam");
1212}
1213
SyntaxTree::Impl & Tree
Definition: ASTDiff.cpp:192
StringRef P
static const char * ScanFormat(const char *I, const char *E, char Target)
ScanForward - Scans forward, looking for the given character, skipping nested clauses and escaped cha...
Definition: Diagnostic.cpp:568
static void HandlePluralModifier(const Diagnostic &DInfo, unsigned ValNo, const char *Argument, unsigned ArgumentLen, SmallVectorImpl< char > &OutStr)
HandlePluralModifier - Handle the integer 'plural' modifier.
Definition: Diagnostic.cpp:739
static void pushEscapedString(StringRef Str, SmallVectorImpl< char > &OutStr)
pushEscapedString - Append Str to the diagnostic buffer, escaping non-printable characters and ill-fo...
Definition: Diagnostic.cpp:793
static void HandleIntegerSModifier(unsigned ValNo, SmallVectorImpl< char > &OutStr)
HandleIntegerSModifier - Handle the integer 's' modifier.
Definition: Diagnostic.cpp:622
static void DummyArgToStringFn(DiagnosticsEngine::ArgumentKind AK, intptr_t QT, StringRef Modifier, StringRef Argument, ArrayRef< DiagnosticsEngine::ArgumentValue > PrevArgs, SmallVectorImpl< char > &Output, void *Cookie, ArrayRef< intptr_t > QualTypeVals)
Definition: Diagnostic.cpp:61
static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End)
EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
Definition: Diagnostic.cpp:673
static unsigned PluralNumber(const char *&Start, const char *End)
PluralNumber - Parse an unsigned integer and advance Start.
Definition: Diagnostic.cpp:644
static void HandleSelectModifier(const Diagnostic &DInfo, unsigned ValNo, const char *Argument, unsigned ArgumentLen, SmallVectorImpl< char > &OutStr)
HandleSelectModifier - Handle the integer 'select' modifier.
Definition: Diagnostic.cpp:598
static bool ModifierIs(const char *Modifier, unsigned ModifierLen, const char(&Str)[StrLen])
ModifierIs - Return true if the specified modifier matches specified string.
Definition: Diagnostic.cpp:561
static bool TestPluralRange(unsigned Val, const char *&Start, const char *End)
TestPluralRange - Test if Val is in the parsed range. Modifies Start.
Definition: Diagnostic.cpp:656
static void HandleOrdinalModifier(unsigned ValNo, SmallVectorImpl< char > &OutStr)
HandleOrdinalModifier - Handle the integer 'ord' modifier.
Definition: Diagnostic.cpp:632
static const char * getTokenDescForDiagnostic(tok::TokenKind Kind)
Returns the friendly description for a token kind that will appear without quotes in diagnostic messa...
Definition: Diagnostic.cpp:766
Defines the Diagnostic-related interfaces.
Defines the Diagnostic IDs-related interfaces.
unsigned Offset
Definition: Format.cpp:2800
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
Implements a partial diagnostic that can be emitted anwyhere in a DiagnosticBuilder stream.
static std::string toString(const clang::SanitizerSet &Sanitizers)
Produce a string containing comma-separated names of sanitizers in Sanitizers set.
Defines the clang::SourceLocation class and associated facilities.
Defines the SourceManager interface.
Defines various enumerations that describe declaration and type specifiers.
Defines the clang::TokenKind enum and support functions.
SourceLocation Begin
Abstract interface, implemented by clients of the front-end, which formats and prints fully processed...
Definition: Diagnostic.h:1740
virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info)
Handle this diagnostic, reporting it to the user or capturing it to a log as needed.
Definition: Diagnostic.cpp:548
unsigned NumErrors
Number of errors reported.
Definition: Diagnostic.h:1743
unsigned NumWarnings
Number of warnings reported.
Definition: Diagnostic.h:1742
virtual bool IncludeInDiagnosticCounts() const
Indicates whether the diagnostics handled by this DiagnosticConsumer should be included in the number...
static StringRef getWarningOptionForDiag(unsigned DiagID)
Return the lowest-level warning option that enables the specified diagnostic.
Level
The level of the diagnostic, after it has been through mapping.
static void getAllDiagnostics(diag::Flavor Flavor, std::vector< diag::kind > &Diags)
Get the set of all diagnostic IDs.
void setNoWarningAsError(bool Value)
void setSeverity(diag::Severity Value)
diag::Severity getSeverity() const
void setUpgradedFromWarning(bool Value)
void setNoErrorAsFatal(bool Value)
bool hasNoWarningAsError() const
A little helper class (which is basically a smart pointer that forwards info from DiagnosticsEngine) ...
Definition: Diagnostic.h:1566
const SourceLocation & getLocation() const
Definition: Diagnostic.h:1577
const std::string & getArgStdStr(unsigned Idx) const
Return the provided argument string specified by Idx.
Definition: Diagnostic.h:1597
void FormatDiagnostic(SmallVectorImpl< char > &OutStr) const
Format this diagnostic into a string, substituting the formal arguments into the %0 slots.
Definition: Diagnostic.cpp:779
uint64_t getRawArg(unsigned Idx) const
Return the specified non-string argument in an opaque form.
Definition: Diagnostic.h:1639
const char * getArgCStr(unsigned Idx) const
Return the specified C string argument.
Definition: Diagnostic.h:1605
const IdentifierInfo * getArgIdentifier(unsigned Idx) const
Return the specified IdentifierInfo argument.
Definition: Diagnostic.h:1630
SourceManager & getSourceManager() const
Definition: Diagnostic.h:1579
ArrayRef< FixItHint > getFixItHints() const
Definition: Diagnostic.h:1670
unsigned getNumArgs() const
Definition: Diagnostic.h:1581
bool hasSourceManager() const
Definition: Diagnostic.h:1578
unsigned getID() const
Definition: Diagnostic.h:1576
DiagnosticsEngine::ArgumentKind getArgKind(unsigned Idx) const
Return the kind of the specified index.
Definition: Diagnostic.h:1589
int64_t getArgSInt(unsigned Idx) const
Return the specified signed integer argument.
Definition: Diagnostic.h:1614
uint64_t getArgUInt(unsigned Idx) const
Return the specified unsigned integer argument.
Definition: Diagnostic.h:1622
ArrayRef< CharSourceRange > getRanges() const
Return an array reference for this diagnostic's ranges.
Definition: Diagnostic.h:1657
const DiagnosticsEngine * getDiags() const
Definition: Diagnostic.h:1575
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1542
void setSeverityForAll(diag::Flavor Flavor, diag::Severity Map, SourceLocation Loc=SourceLocation())
Add the specified mapping to all diagnostics of the specified flavor.
Definition: Diagnostic.cpp:473
LLVM_DUMP_METHOD void dump() const
Definition: Diagnostic.cpp:88
void setClient(DiagnosticConsumer *client, bool ShouldOwnClient=true)
Set the diagnostic client associated with this diagnostic object.
Definition: Diagnostic.cpp:96
void Clear()
Clear out the current diagnostic.
Definition: Diagnostic.h:977
void pushMappings(SourceLocation Loc)
Copies the current DiagMappings and pushes the new copy onto the top of the stack.
Definition: Diagnostic.cpp:102
void setSeverity(diag::kind Diag, diag::Severity Map, SourceLocation Loc)
This allows the client to specify that certain warnings are ignored.
Definition: Diagnostic.cpp:342
Level
The level of the diagnostic, after it has been through mapping.
Definition: Diagnostic.h:195
DiagnosticConsumer * getClient()
Definition: Diagnostic.h:567
@ ak_nameddecl
NamedDecl *.
Definition: Diagnostic.h:236
@ ak_declcontext
DeclContext *.
Definition: Diagnostic.h:242
@ ak_addrspace
address space
Definition: Diagnostic.h:224
@ ak_identifierinfo
IdentifierInfo.
Definition: Diagnostic.h:221
@ ak_qualtype_pair
pair<QualType, QualType>
Definition: Diagnostic.h:245
@ ak_c_string
const char *
Definition: Diagnostic.h:209
@ ak_declarationname
DeclarationName.
Definition: Diagnostic.h:233
@ ak_tokenkind
enum TokenKind : unsigned
Definition: Diagnostic.h:218
@ ak_std_string
std::string
Definition: Diagnostic.h:206
@ ak_nestednamespec
NestedNameSpecifier *.
Definition: Diagnostic.h:239
DiagnosticsEngine(IntrusiveRefCntPtr< DiagnosticIDs > Diags, IntrusiveRefCntPtr< DiagnosticOptions > DiagOpts, DiagnosticConsumer *client=nullptr, bool ShouldOwnClient=true)
Definition: Diagnostic.cpp:71
bool EmitCurrentDiagnostic(bool Force=false)
Emit the current diagnostic and clear the diagnostic state.
Definition: Diagnostic.cpp:514
bool setDiagnosticGroupErrorAsFatal(StringRef Group, bool Enabled)
Set the error-as-fatal flag for the given diagnostic group.
Definition: Diagnostic.cpp:443
bool setDiagnosticGroupWarningAsError(StringRef Group, bool Enabled)
Set the warning-as-error flag for the given diagnostic group.
Definition: Diagnostic.cpp:412
void ConvertArgToString(ArgumentKind Kind, intptr_t Val, StringRef Modifier, StringRef Argument, ArrayRef< ArgumentValue > PrevArgs, SmallVectorImpl< char > &Output, ArrayRef< intptr_t > QualTypeVals) const
Converts a diagnostic argument (as an intptr_t) into the string that represents it.
Definition: Diagnostic.h:875
void SetDelayedDiagnostic(unsigned DiagID, StringRef Arg1="", StringRef Arg2="", StringRef Arg3="")
Set the "delayed" diagnostic that will be emitted once the current diagnostic completes.
Definition: Diagnostic.cpp:146
bool setSeverityForGroup(diag::Flavor Flavor, StringRef Group, diag::Severity Map, SourceLocation Loc=SourceLocation())
Change an entire diagnostic group (e.g.
Definition: Diagnostic.cpp:389
bool popMappings(SourceLocation Loc)
Pops the current DiagMappings off the top of the stack, causing the new top of the stack to be the ac...
Definition: Diagnostic.cpp:106
const IntrusiveRefCntPtr< DiagnosticIDs > & getDiagnosticIDs() const
Definition: Diagnostic.h:552
void Reset(bool soft=false)
Reset the state of the diagnostic object to its initial configuration.
Definition: Diagnostic.cpp:118
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
bool IncludeInDiagnosticCounts() const override
Indicates whether the diagnostics handled by this DiagnosticConsumer should be included in the number...
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) override
Handle this diagnostic, reporting it to the user or capturing it to a log as needed.
A SourceLocation and its associated SourceManager.
bool hasManager() const
Checks whether the SourceManager is present.
const SourceManager & getManager() const
One of these records is kept for each identifier that is lexed.
StringRef getName() const
Return the actual identifier string.
Encodes a location in the source.
std::string printToString(const SourceManager &SM) const
bool isValid() const
Return true if this is a valid SourceLocation object.
void print(raw_ostream &OS, const SourceManager &SM) const
SourceLocation getLocWithOffset(IntTy Offset) const
Return a source location with the specified offset from this SourceLocation.
This class handles loading and caching of source files into memory.
llvm::MemoryBufferRef getBufferOrFake(FileID FID, SourceLocation Loc=SourceLocation()) const
Return the buffer for the specified FileID.
std::pair< FileID, unsigned > getDecomposedLoc(SourceLocation Loc) const
Decompose the specified location into a raw FileID + Offset pair.
SourceLocation getLocForStartOfFile(FileID FID) const
Return the source location corresponding to the first byte of the specified file.
std::pair< FileID, unsigned > getDecomposedIncludedLoc(FileID FID) const
Returns the "included/expanded in" decomposed location of the given FileID.
Represents a diagnostic in a form that can be retained until its corresponding source manager is dest...
Definition: Diagnostic.h:1690
unsigned getID() const
Definition: Diagnostic.h:1711
range_iterator range_begin() const
Definition: Diagnostic.h:1720
DiagnosticsEngine::Level getLevel() const
Definition: Diagnostic.h:1712
fixit_iterator fixit_begin() const
Definition: Diagnostic.h:1728
const FullSourceLoc & getLocation() const
Definition: Diagnostic.h:1713
range_iterator range_end() const
Definition: Diagnostic.h:1721
StringRef getMessage() const
Definition: Diagnostic.h:1714
fixit_iterator fixit_end() const
Definition: Diagnostic.h:1729
The streaming interface shared between DiagnosticBuilder and PartialDiagnostic.
Definition: Diagnostic.h:1110
void AddString(StringRef V) const
Definition: Diagnostic.h:1194
internal::PolymorphicMatcher< internal::ValueEqualsMatcher, void(internal::AllNodeBaseTypes), ValueT > equals(const ValueT &Value)
Matches literals that are equal to the given value of type ValueT.
Definition: ASTMatchers.h:5597
Severity
Enum values that allow the client to map NOTEs, WARNINGs, and EXTENSIONs to either Ignore (nothing),...
Definition: DiagnosticIDs.h:83
@ Warning
Present this diagnostic as a warning.
@ Fatal
Present this diagnostic as a fatal error.
@ Error
Present this diagnostic as an error.
@ Remark
Present this diagnostic as a remark.
@ Ignored
Do not present this diagnostic, ignore it.
Flavor
Flavors of diagnostics we can emit.
Definition: DiagnosticIDs.h:94
@ WarningOrError
A diagnostic that indicates a problem or potential problem.
const char * getTokenName(TokenKind Kind) LLVM_READNONE
Determines the name of a token as used within the front end.
Definition: TokenKinds.cpp:24
const char * getKeywordSpelling(TokenKind Kind) LLVM_READNONE
Determines the spelling of simple keyword and contextual keyword tokens like 'int' and 'dynamic_cast'...
Definition: TokenKinds.cpp:40
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
Definition: TokenKinds.h:25
const char * getPunctuatorSpelling(TokenKind Kind) LLVM_READNONE
Determines the spelling of simple punctuation tokens like '!' or '', and returns NULL for literal and...
Definition: TokenKinds.cpp:31
LLVM_READONLY bool isPrintable(unsigned char c)
Return true if this character is an ASCII printable character; that is, a character that should take ...
Definition: CharInfo.h:145
const StreamingDiagnostic & operator<<(const StreamingDiagnostic &DB, const ASTContext::SectionInfo &Section)
Insertion operator for diagnostics.
@ C
Languages that the frontend can parse and compile.
llvm::StringRef getNullabilitySpelling(NullabilityKind kind, bool isContextSensitive=false)
Retrieve the spelling of the given nullability kind.
LLVM_READONLY bool isDigit(unsigned char c)
Return true if this character is an ASCII digit: [0-9].
Definition: CharInfo.h:99
LLVM_READONLY bool isWhitespace(unsigned char c)
Return true if this character is horizontal or vertical ASCII whitespace: ' ', '\t',...
Definition: CharInfo.h:93
std::pair< NullabilityKind, bool > DiagNullabilityKind
A nullability kind paired with a bit indicating whether it used a context-sensitive keyword.
Definition: Diagnostic.h:1537
LLVM_READONLY bool isPunctuation(unsigned char c)
Return true if this character is an ASCII punctuation character.
Definition: CharInfo.h:137
Definition: Format.h:4761
__INTPTR_TYPE__ intptr_t
A signed integer type with the property that any valid pointer to void can be converted to this type,...
SmallVector< CharSourceRange, 8 > DiagRanges
The list of ranges added to this diagnostic.
Definition: Diagnostic.h:177
unsigned char NumDiagArgs
The number of entries in Arguments.
Definition: Diagnostic.h:159
SmallVector< FixItHint, 6 > FixItHints
If valid, provides a hint with some code to insert, remove, or modify at a particular position.
Definition: Diagnostic.h:181