1 //===--- Diagnostic.cpp - C Language Family Diagnostic Handling -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Diagnostic-related interfaces.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Basic/CharInfo.h"
15 #include "clang/Basic/Diagnostic.h"
16 #include "clang/Basic/DiagnosticOptions.h"
17 #include "clang/Basic/IdentifierTable.h"
18 #include "clang/Basic/PartialDiagnostic.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/Support/CrashRecoveryContext.h"
22 #include "llvm/Support/raw_ostream.h"
24 using namespace clang;
26 static void DummyArgToStringFn(DiagnosticsEngine::ArgumentKind AK, intptr_t QT,
27 const char *Modifier, unsigned ML,
28 const char *Argument, unsigned ArgLen,
29 const DiagnosticsEngine::ArgumentValue *PrevArgs,
31 SmallVectorImpl<char> &Output,
33 ArrayRef<intptr_t> QualTypeVals) {
34 const char *Str = "<can't format argument>";
35 Output.append(Str, Str+strlen(Str));
39 DiagnosticsEngine::DiagnosticsEngine(
40 const IntrusiveRefCntPtr<DiagnosticIDs> &diags,
41 DiagnosticOptions *DiagOpts,
42 DiagnosticConsumer *client, bool ShouldOwnClient)
43 : Diags(diags), DiagOpts(DiagOpts), Client(client),
44 OwnsDiagClient(ShouldOwnClient), SourceMgr(0) {
45 ArgToStringFn = DummyArgToStringFn;
46 ArgToStringCookie = 0;
48 AllExtensionsSilenced = 0;
49 IgnoreAllWarnings = false;
50 WarningsAsErrors = false;
51 EnableAllWarnings = false;
52 ErrorsAsFatal = false;
53 SuppressSystemWarnings = false;
54 SuppressAllDiagnostics = false;
56 PrintTemplateTree = false;
58 ShowOverloads = Ovl_All;
59 ExtBehavior = Ext_Ignore;
62 TemplateBacktraceLimit = 0;
63 ConstexprBacktraceLimit = 0;
68 DiagnosticsEngine::~DiagnosticsEngine() {
73 void DiagnosticsEngine::setClient(DiagnosticConsumer *client,
74 bool ShouldOwnClient) {
75 if (OwnsDiagClient && Client)
79 OwnsDiagClient = ShouldOwnClient;
82 void DiagnosticsEngine::pushMappings(SourceLocation Loc) {
83 DiagStateOnPushStack.push_back(GetCurDiagState());
86 bool DiagnosticsEngine::popMappings(SourceLocation Loc) {
87 if (DiagStateOnPushStack.empty())
90 if (DiagStateOnPushStack.back() != GetCurDiagState()) {
91 // State changed at some point between push/pop.
92 PushDiagStatePoint(DiagStateOnPushStack.back(), Loc);
94 DiagStateOnPushStack.pop_back();
98 void DiagnosticsEngine::Reset() {
99 ErrorOccurred = false;
100 UncompilableErrorOccurred = false;
101 FatalErrorOccurred = false;
102 UnrecoverableErrorOccurred = false;
106 NumErrorsSuppressed = 0;
107 TrapNumErrorsOccurred = 0;
108 TrapNumUnrecoverableErrorsOccurred = 0;
111 LastDiagLevel = DiagnosticIDs::Ignored;
114 // Clear state related to #pragma diagnostic.
116 DiagStatePoints.clear();
117 DiagStateOnPushStack.clear();
119 // Create a DiagState and DiagStatePoint representing diagnostic changes
120 // through command-line.
121 DiagStates.push_back(DiagState());
122 DiagStatePoints.push_back(DiagStatePoint(&DiagStates.back(), FullSourceLoc()));
125 void DiagnosticsEngine::SetDelayedDiagnostic(unsigned DiagID, StringRef Arg1,
130 DelayedDiagID = DiagID;
131 DelayedDiagArg1 = Arg1.str();
132 DelayedDiagArg2 = Arg2.str();
135 void DiagnosticsEngine::ReportDelayed() {
136 Report(DelayedDiagID) << DelayedDiagArg1 << DelayedDiagArg2;
138 DelayedDiagArg1.clear();
139 DelayedDiagArg2.clear();
142 DiagnosticsEngine::DiagStatePointsTy::iterator
143 DiagnosticsEngine::GetDiagStatePointForLoc(SourceLocation L) const {
144 assert(!DiagStatePoints.empty());
145 assert(DiagStatePoints.front().Loc.isInvalid() &&
146 "Should have created a DiagStatePoint for command-line");
149 return DiagStatePoints.end() - 1;
151 FullSourceLoc Loc(L, *SourceMgr);
153 return DiagStatePoints.end() - 1;
155 DiagStatePointsTy::iterator Pos = DiagStatePoints.end();
156 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
157 if (LastStateChangePos.isValid() &&
158 Loc.isBeforeInTranslationUnitThan(LastStateChangePos))
159 Pos = std::upper_bound(DiagStatePoints.begin(), DiagStatePoints.end(),
160 DiagStatePoint(0, Loc));
165 void DiagnosticsEngine::setDiagnosticMapping(diag::kind Diag, diag::Mapping Map,
167 assert(Diag < diag::DIAG_UPPER_LIMIT &&
168 "Can only map builtin diagnostics");
169 assert((Diags->isBuiltinWarningOrExtension(Diag) ||
170 (Map == diag::MAP_FATAL || Map == diag::MAP_ERROR)) &&
171 "Cannot map errors into warnings!");
172 assert(!DiagStatePoints.empty());
173 assert((L.isInvalid() || SourceMgr) && "No SourceMgr for valid location");
175 FullSourceLoc Loc = SourceMgr? FullSourceLoc(L, *SourceMgr) : FullSourceLoc();
176 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
177 // Don't allow a mapping to a warning override an error/fatal mapping.
178 if (Map == diag::MAP_WARNING) {
179 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(Diag);
180 if (Info.getMapping() == diag::MAP_ERROR ||
181 Info.getMapping() == diag::MAP_FATAL)
182 Map = Info.getMapping();
184 DiagnosticMappingInfo MappingInfo = makeMappingInfo(Map, L);
186 // Common case; setting all the diagnostics of a group in one place.
187 if (Loc.isInvalid() || Loc == LastStateChangePos) {
188 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
192 // Another common case; modifying diagnostic state in a source location
193 // after the previous one.
194 if ((Loc.isValid() && LastStateChangePos.isInvalid()) ||
195 LastStateChangePos.isBeforeInTranslationUnitThan(Loc)) {
196 // A diagnostic pragma occurred, create a new DiagState initialized with
197 // the current one and a new DiagStatePoint to record at which location
198 // the new state became active.
199 DiagStates.push_back(*GetCurDiagState());
200 PushDiagStatePoint(&DiagStates.back(), Loc);
201 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
205 // We allow setting the diagnostic state in random source order for
206 // completeness but it should not be actually happening in normal practice.
208 DiagStatePointsTy::iterator Pos = GetDiagStatePointForLoc(Loc);
209 assert(Pos != DiagStatePoints.end());
211 // Update all diagnostic states that are active after the given location.
212 for (DiagStatePointsTy::iterator
213 I = Pos+1, E = DiagStatePoints.end(); I != E; ++I) {
214 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
217 // If the location corresponds to an existing point, just update its state.
218 if (Pos->Loc == Loc) {
219 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
223 // Create a new state/point and fit it into the vector of DiagStatePoints
224 // so that the vector is always ordered according to location.
225 Pos->Loc.isBeforeInTranslationUnitThan(Loc);
226 DiagStates.push_back(*Pos->State);
227 DiagState *NewState = &DiagStates.back();
228 GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
229 DiagStatePoints.insert(Pos+1, DiagStatePoint(NewState,
230 FullSourceLoc(Loc, *SourceMgr)));
233 bool DiagnosticsEngine::setDiagnosticGroupMapping(
234 StringRef Group, diag::Mapping Map, SourceLocation Loc)
236 // Get the diagnostics in this group.
237 SmallVector<diag::kind, 8> GroupDiags;
238 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
242 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i)
243 setDiagnosticMapping(GroupDiags[i], Map, Loc);
248 void DiagnosticsEngine::setDiagnosticWarningAsError(diag::kind Diag,
250 // If we are enabling this feature, just set the diagnostic mappings to map to
253 setDiagnosticMapping(Diag, diag::MAP_ERROR, SourceLocation());
255 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
256 // potentially downgrade anything already mapped to be a warning.
257 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(Diag);
259 if (Info.getMapping() == diag::MAP_ERROR ||
260 Info.getMapping() == diag::MAP_FATAL)
261 Info.setMapping(diag::MAP_WARNING);
263 Info.setNoWarningAsError(true);
266 bool DiagnosticsEngine::setDiagnosticGroupWarningAsError(StringRef Group,
268 // If we are enabling this feature, just set the diagnostic mappings to map to
271 return setDiagnosticGroupMapping(Group, diag::MAP_ERROR);
273 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
274 // potentially downgrade anything already mapped to be a warning.
276 // Get the diagnostics in this group.
277 SmallVector<diag::kind, 8> GroupDiags;
278 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
281 // Perform the mapping change.
282 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i) {
283 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(
286 if (Info.getMapping() == diag::MAP_ERROR ||
287 Info.getMapping() == diag::MAP_FATAL)
288 Info.setMapping(diag::MAP_WARNING);
290 Info.setNoWarningAsError(true);
296 void DiagnosticsEngine::setDiagnosticErrorAsFatal(diag::kind Diag,
298 // If we are enabling this feature, just set the diagnostic mappings to map to
301 setDiagnosticMapping(Diag, diag::MAP_FATAL, SourceLocation());
303 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
304 // potentially downgrade anything already mapped to be a warning.
305 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(Diag);
307 if (Info.getMapping() == diag::MAP_FATAL)
308 Info.setMapping(diag::MAP_ERROR);
310 Info.setNoErrorAsFatal(true);
313 bool DiagnosticsEngine::setDiagnosticGroupErrorAsFatal(StringRef Group,
315 // If we are enabling this feature, just set the diagnostic mappings to map to
318 return setDiagnosticGroupMapping(Group, diag::MAP_FATAL);
320 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
321 // potentially downgrade anything already mapped to be an error.
323 // Get the diagnostics in this group.
324 SmallVector<diag::kind, 8> GroupDiags;
325 if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
328 // Perform the mapping change.
329 for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i) {
330 DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(
333 if (Info.getMapping() == diag::MAP_FATAL)
334 Info.setMapping(diag::MAP_ERROR);
336 Info.setNoErrorAsFatal(true);
342 void DiagnosticsEngine::setMappingToAllDiagnostics(diag::Mapping Map,
343 SourceLocation Loc) {
344 // Get all the diagnostics.
345 SmallVector<diag::kind, 64> AllDiags;
346 Diags->getAllDiagnostics(AllDiags);
349 for (unsigned i = 0, e = AllDiags.size(); i != e; ++i)
350 if (Diags->isBuiltinWarningOrExtension(AllDiags[i]))
351 setDiagnosticMapping(AllDiags[i], Map, Loc);
354 void DiagnosticsEngine::Report(const StoredDiagnostic &storedDiag) {
355 assert(CurDiagID == ~0U && "Multiple diagnostics in flight at once!");
357 CurDiagLoc = storedDiag.getLocation();
358 CurDiagID = storedDiag.getID();
361 NumDiagRanges = storedDiag.range_size();
362 assert(NumDiagRanges < DiagnosticsEngine::MaxRanges &&
363 "Too many arguments to diagnostic!");
365 for (StoredDiagnostic::range_iterator
366 RI = storedDiag.range_begin(),
367 RE = storedDiag.range_end(); RI != RE; ++RI)
368 DiagRanges[i++] = *RI;
370 assert(NumDiagRanges < DiagnosticsEngine::MaxFixItHints &&
371 "Too many arguments to diagnostic!");
372 NumDiagFixItHints = 0;
373 for (StoredDiagnostic::fixit_iterator
374 FI = storedDiag.fixit_begin(),
375 FE = storedDiag.fixit_end(); FI != FE; ++FI)
376 DiagFixItHints[NumDiagFixItHints++] = *FI;
378 assert(Client && "DiagnosticConsumer not set!");
379 Level DiagLevel = storedDiag.getLevel();
380 Diagnostic Info(this, storedDiag.getMessage());
381 Client->HandleDiagnostic(DiagLevel, Info);
382 if (Client->IncludeInDiagnosticCounts()) {
383 if (DiagLevel == DiagnosticsEngine::Warning)
390 bool DiagnosticsEngine::EmitCurrentDiagnostic(bool Force) {
391 assert(getClient() && "DiagnosticClient not set!");
395 Diagnostic Info(this);
397 // Figure out the diagnostic level of this message.
398 DiagnosticIDs::Level DiagLevel
399 = Diags->getDiagnosticLevel(Info.getID(), Info.getLocation(), *this);
401 Emitted = (DiagLevel != DiagnosticIDs::Ignored);
403 // Emit the diagnostic regardless of suppression level.
404 Diags->EmitDiag(*this, DiagLevel);
407 // Process the diagnostic, sending the accumulated information to the
408 // DiagnosticConsumer.
409 Emitted = ProcessDiag();
412 // Clear out the current diagnostic object.
413 unsigned DiagID = CurDiagID;
416 // If there was a delayed diagnostic, emit it now.
417 if (!Force && DelayedDiagID && DelayedDiagID != DiagID)
424 DiagnosticConsumer::~DiagnosticConsumer() {}
426 void DiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
427 const Diagnostic &Info) {
428 if (!IncludeInDiagnosticCounts())
431 if (DiagLevel == DiagnosticsEngine::Warning)
433 else if (DiagLevel >= DiagnosticsEngine::Error)
437 /// ModifierIs - Return true if the specified modifier matches specified string.
438 template <std::size_t StrLen>
439 static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
440 const char (&Str)[StrLen]) {
441 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
444 /// ScanForward - Scans forward, looking for the given character, skipping
445 /// nested clauses and escaped characters.
446 static const char *ScanFormat(const char *I, const char *E, char Target) {
449 for ( ; I != E; ++I) {
450 if (Depth == 0 && *I == Target) return I;
451 if (Depth != 0 && *I == '}') Depth--;
457 // Escaped characters get implicitly skipped here.
460 if (!isDigit(*I) && !isPunctuation(*I)) {
461 for (I++; I != E && !isDigit(*I) && *I != '{'; I++) ;
471 /// HandleSelectModifier - Handle the integer 'select' modifier. This is used
472 /// like this: %select{foo|bar|baz}2. This means that the integer argument
473 /// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
474 /// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
475 /// This is very useful for certain classes of variant diagnostics.
476 static void HandleSelectModifier(const Diagnostic &DInfo, unsigned ValNo,
477 const char *Argument, unsigned ArgumentLen,
478 SmallVectorImpl<char> &OutStr) {
479 const char *ArgumentEnd = Argument+ArgumentLen;
481 // Skip over 'ValNo' |'s.
483 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
484 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
485 " larger than the number of options in the diagnostic string!");
486 Argument = NextVal+1; // Skip this string.
490 // Get the end of the value. This is either the } or the |.
491 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
493 // Recursively format the result of the select clause into the output string.
494 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
497 /// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
498 /// letter 's' to the string if the value is not 1. This is used in cases like
499 /// this: "you idiot, you have %4 parameter%s4!".
500 static void HandleIntegerSModifier(unsigned ValNo,
501 SmallVectorImpl<char> &OutStr) {
503 OutStr.push_back('s');
506 /// HandleOrdinalModifier - Handle the integer 'ord' modifier. This
507 /// prints the ordinal form of the given integer, with 1 corresponding
508 /// to the first ordinal. Currently this is hard-coded to use the
510 static void HandleOrdinalModifier(unsigned ValNo,
511 SmallVectorImpl<char> &OutStr) {
512 assert(ValNo != 0 && "ValNo must be strictly positive!");
514 llvm::raw_svector_ostream Out(OutStr);
516 // We could use text forms for the first N ordinals, but the numeric
517 // forms are actually nicer in diagnostics because they stand out.
518 Out << ValNo << llvm::getOrdinalSuffix(ValNo);
522 /// PluralNumber - Parse an unsigned integer and advance Start.
523 static unsigned PluralNumber(const char *&Start, const char *End) {
524 // Programming 101: Parse a decimal number :-)
526 while (Start != End && *Start >= '0' && *Start <= '9') {
534 /// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
535 static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
537 unsigned Ref = PluralNumber(Start, End);
542 unsigned Low = PluralNumber(Start, End);
543 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
545 unsigned High = PluralNumber(Start, End);
546 assert(*Start == ']' && "Bad plural expression syntax: expected )");
548 return Low <= Val && Val <= High;
551 /// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
552 static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
562 unsigned Arg = PluralNumber(Start, End);
563 assert(*Start == '=' && "Bad plural expression syntax: expected =");
565 unsigned ValMod = ValNo % Arg;
566 if (TestPluralRange(ValMod, Start, End))
569 assert((C == '[' || (C >= '0' && C <= '9')) &&
570 "Bad plural expression syntax: unexpected character");
572 if (TestPluralRange(ValNo, Start, End))
576 // Scan for next or-expr part.
577 Start = std::find(Start, End, ',');
585 /// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
586 /// for complex plural forms, or in languages where all plurals are complex.
587 /// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
588 /// conditions that are tested in order, the form corresponding to the first
589 /// that applies being emitted. The empty condition is always true, making the
590 /// last form a default case.
591 /// Conditions are simple boolean expressions, where n is the number argument.
592 /// Here are the rules.
593 /// condition := expression | empty
594 /// empty := -> always true
595 /// expression := numeric [',' expression] -> logical or
596 /// numeric := range -> true if n in range
597 /// | '%' number '=' range -> true if n % number in range
599 /// | '[' number ',' number ']' -> ranges are inclusive both ends
601 /// Here are some examples from the GNU gettext manual written in this form:
605 /// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
607 /// {1:form0|2:form1|:form2}
609 /// {1:form0|0,%100=[1,19]:form1|:form2}
611 /// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
612 /// Russian (requires repeated form):
613 /// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
615 /// {1:form0|[2,4]:form1|:form2}
616 /// Polish (requires repeated form):
617 /// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
618 static void HandlePluralModifier(const Diagnostic &DInfo, unsigned ValNo,
619 const char *Argument, unsigned ArgumentLen,
620 SmallVectorImpl<char> &OutStr) {
621 const char *ArgumentEnd = Argument + ArgumentLen;
623 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
624 const char *ExprEnd = Argument;
625 while (*ExprEnd != ':') {
626 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
629 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
630 Argument = ExprEnd + 1;
631 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
633 // Recursively format the result of the plural clause into the
635 DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr);
638 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
643 /// FormatDiagnostic - Format this diagnostic into a string, substituting the
644 /// formal arguments into the %0 slots. The result is appended onto the Str
647 FormatDiagnostic(SmallVectorImpl<char> &OutStr) const {
648 if (!StoredDiagMessage.empty()) {
649 OutStr.append(StoredDiagMessage.begin(), StoredDiagMessage.end());
654 getDiags()->getDiagnosticIDs()->getDescription(getID());
656 FormatDiagnostic(Diag.begin(), Diag.end(), OutStr);
660 FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
661 SmallVectorImpl<char> &OutStr) const {
663 /// FormattedArgs - Keep track of all of the arguments formatted by
664 /// ConvertArgToString and pass them into subsequent calls to
665 /// ConvertArgToString, allowing the implementation to avoid redundancies in
667 SmallVector<DiagnosticsEngine::ArgumentValue, 8> FormattedArgs;
669 /// QualTypeVals - Pass a vector of arrays so that QualType names can be
670 /// compared to see if more information is needed to be printed.
671 SmallVector<intptr_t, 2> QualTypeVals;
672 SmallVector<char, 64> Tree;
674 for (unsigned i = 0, e = getNumArgs(); i < e; ++i)
675 if (getArgKind(i) == DiagnosticsEngine::ak_qualtype)
676 QualTypeVals.push_back(getRawArg(i));
678 while (DiagStr != DiagEnd) {
679 if (DiagStr[0] != '%') {
680 // Append non-%0 substrings to Str if we have one.
681 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
682 OutStr.append(DiagStr, StrEnd);
685 } else if (isPunctuation(DiagStr[1])) {
686 OutStr.push_back(DiagStr[1]); // %% -> %.
694 // This must be a placeholder for a diagnostic argument. The format for a
695 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
696 // The digit is a number from 0-9 indicating which argument this comes from.
697 // The modifier is a string of digits from the set [-a-z]+, arguments is a
698 // brace enclosed string.
699 const char *Modifier = 0, *Argument = 0;
700 unsigned ModifierLen = 0, ArgumentLen = 0;
702 // Check to see if we have a modifier. If so eat it.
703 if (!isDigit(DiagStr[0])) {
705 while (DiagStr[0] == '-' ||
706 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
708 ModifierLen = DiagStr-Modifier;
710 // If we have an argument, get it next.
711 if (DiagStr[0] == '{') {
712 ++DiagStr; // Skip {.
715 DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
716 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
717 ArgumentLen = DiagStr-Argument;
718 ++DiagStr; // Skip }.
722 assert(isDigit(*DiagStr) && "Invalid format for argument in diagnostic");
723 unsigned ArgNo = *DiagStr++ - '0';
725 // Only used for type diffing.
726 unsigned ArgNo2 = ArgNo;
728 DiagnosticsEngine::ArgumentKind Kind = getArgKind(ArgNo);
729 if (ModifierIs(Modifier, ModifierLen, "diff")) {
730 assert(*DiagStr == ',' && isDigit(*(DiagStr + 1)) &&
731 "Invalid format for diff modifier");
733 ArgNo2 = *DiagStr++ - '0';
734 DiagnosticsEngine::ArgumentKind Kind2 = getArgKind(ArgNo2);
735 if (Kind == DiagnosticsEngine::ak_qualtype &&
736 Kind2 == DiagnosticsEngine::ak_qualtype)
737 Kind = DiagnosticsEngine::ak_qualtype_pair;
739 // %diff only supports QualTypes. For other kinds of arguments,
740 // use the default printing. For example, if the modifier is:
741 // "%diff{compare $ to $|other text}1,2"
743 // "compare %1 to %2"
744 const char *Pipe = ScanFormat(Argument, Argument + ArgumentLen, '|');
745 const char *FirstDollar = ScanFormat(Argument, Pipe, '$');
746 const char *SecondDollar = ScanFormat(FirstDollar + 1, Pipe, '$');
747 const char ArgStr1[] = { '%', static_cast<char>('0' + ArgNo) };
748 const char ArgStr2[] = { '%', static_cast<char>('0' + ArgNo2) };
749 FormatDiagnostic(Argument, FirstDollar, OutStr);
750 FormatDiagnostic(ArgStr1, ArgStr1 + 2, OutStr);
751 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
752 FormatDiagnostic(ArgStr2, ArgStr2 + 2, OutStr);
753 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
760 case DiagnosticsEngine::ak_std_string: {
761 const std::string &S = getArgStdStr(ArgNo);
762 assert(ModifierLen == 0 && "No modifiers for strings yet");
763 OutStr.append(S.begin(), S.end());
766 case DiagnosticsEngine::ak_c_string: {
767 const char *S = getArgCStr(ArgNo);
768 assert(ModifierLen == 0 && "No modifiers for strings yet");
770 // Don't crash if get passed a null pointer by accident.
774 OutStr.append(S, S + strlen(S));
777 // ---- INTEGERS ----
778 case DiagnosticsEngine::ak_sint: {
779 int Val = getArgSInt(ArgNo);
781 if (ModifierIs(Modifier, ModifierLen, "select")) {
782 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen,
784 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
785 HandleIntegerSModifier(Val, OutStr);
786 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
787 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
789 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
790 HandleOrdinalModifier((unsigned)Val, OutStr);
792 assert(ModifierLen == 0 && "Unknown integer modifier");
793 llvm::raw_svector_ostream(OutStr) << Val;
797 case DiagnosticsEngine::ak_uint: {
798 unsigned Val = getArgUInt(ArgNo);
800 if (ModifierIs(Modifier, ModifierLen, "select")) {
801 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
802 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
803 HandleIntegerSModifier(Val, OutStr);
804 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
805 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
807 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
808 HandleOrdinalModifier(Val, OutStr);
810 assert(ModifierLen == 0 && "Unknown integer modifier");
811 llvm::raw_svector_ostream(OutStr) << Val;
815 // ---- NAMES and TYPES ----
816 case DiagnosticsEngine::ak_identifierinfo: {
817 const IdentifierInfo *II = getArgIdentifier(ArgNo);
818 assert(ModifierLen == 0 && "No modifiers for strings yet");
820 // Don't crash if get passed a null pointer by accident.
822 const char *S = "(null)";
823 OutStr.append(S, S + strlen(S));
827 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
830 case DiagnosticsEngine::ak_qualtype:
831 case DiagnosticsEngine::ak_declarationname:
832 case DiagnosticsEngine::ak_nameddecl:
833 case DiagnosticsEngine::ak_nestednamespec:
834 case DiagnosticsEngine::ak_declcontext:
835 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
836 Modifier, ModifierLen,
837 Argument, ArgumentLen,
838 FormattedArgs.data(), FormattedArgs.size(),
839 OutStr, QualTypeVals);
841 case DiagnosticsEngine::ak_qualtype_pair:
842 // Create a struct with all the info needed for printing.
843 TemplateDiffTypes TDT;
844 TDT.FromType = getRawArg(ArgNo);
845 TDT.ToType = getRawArg(ArgNo2);
846 TDT.ElideType = getDiags()->ElideType;
847 TDT.ShowColors = getDiags()->ShowColors;
848 TDT.TemplateDiffUsed = false;
849 intptr_t val = reinterpret_cast<intptr_t>(&TDT);
851 const char *ArgumentEnd = Argument + ArgumentLen;
852 const char *Pipe = ScanFormat(Argument, ArgumentEnd, '|');
854 // Print the tree. If this diagnostic already has a tree, skip the
856 if (getDiags()->PrintTemplateTree && Tree.empty()) {
857 TDT.PrintFromType = true;
858 TDT.PrintTree = true;
859 getDiags()->ConvertArgToString(Kind, val,
860 Modifier, ModifierLen,
861 Argument, ArgumentLen,
862 FormattedArgs.data(),
863 FormattedArgs.size(),
865 // If there is no tree information, fall back to regular printing.
867 FormatDiagnostic(Pipe + 1, ArgumentEnd, OutStr);
872 // Non-tree printing, also the fall-back when tree printing fails.
873 // The fall-back is triggered when the types compared are not templates.
874 const char *FirstDollar = ScanFormat(Argument, ArgumentEnd, '$');
875 const char *SecondDollar = ScanFormat(FirstDollar + 1, ArgumentEnd, '$');
877 // Append before text
878 FormatDiagnostic(Argument, FirstDollar, OutStr);
881 TDT.PrintTree = false;
882 TDT.PrintFromType = true;
883 getDiags()->ConvertArgToString(Kind, val,
884 Modifier, ModifierLen,
885 Argument, ArgumentLen,
886 FormattedArgs.data(), FormattedArgs.size(),
887 OutStr, QualTypeVals);
888 if (!TDT.TemplateDiffUsed)
889 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
892 // Append middle text
893 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
895 // Append second type
896 TDT.PrintFromType = false;
897 getDiags()->ConvertArgToString(Kind, val,
898 Modifier, ModifierLen,
899 Argument, ArgumentLen,
900 FormattedArgs.data(), FormattedArgs.size(),
901 OutStr, QualTypeVals);
902 if (!TDT.TemplateDiffUsed)
903 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
907 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
911 // Remember this argument info for subsequent formatting operations. Turn
912 // std::strings into a null terminated string to make it be the same case as
913 // all the other ones.
914 if (Kind == DiagnosticsEngine::ak_qualtype_pair)
916 else if (Kind != DiagnosticsEngine::ak_std_string)
917 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
919 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_c_string,
920 (intptr_t)getArgStdStr(ArgNo).c_str()));
924 // Append the type tree to the end of the diagnostics.
925 OutStr.append(Tree.begin(), Tree.end());
928 StoredDiagnostic::StoredDiagnostic() { }
930 StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
932 : ID(ID), Level(Level), Loc(), Message(Message) { }
934 StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level,
935 const Diagnostic &Info)
936 : ID(Info.getID()), Level(Level)
938 assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
939 "Valid source location without setting a source manager for diagnostic");
940 if (Info.getLocation().isValid())
941 Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
942 SmallString<64> Message;
943 Info.FormatDiagnostic(Message);
944 this->Message.assign(Message.begin(), Message.end());
946 Ranges.reserve(Info.getNumRanges());
947 for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I)
948 Ranges.push_back(Info.getRange(I));
950 FixIts.reserve(Info.getNumFixItHints());
951 for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I)
952 FixIts.push_back(Info.getFixItHint(I));
955 StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
956 StringRef Message, FullSourceLoc Loc,
957 ArrayRef<CharSourceRange> Ranges,
958 ArrayRef<FixItHint> FixIts)
959 : ID(ID), Level(Level), Loc(Loc), Message(Message),
960 Ranges(Ranges.begin(), Ranges.end()), FixIts(FixIts.begin(), FixIts.end())
964 StoredDiagnostic::~StoredDiagnostic() { }
966 /// IncludeInDiagnosticCounts - This method (whose default implementation
967 /// returns true) indicates whether the diagnostics handled by this
968 /// DiagnosticConsumer should be included in the number of diagnostics
969 /// reported by DiagnosticsEngine.
970 bool DiagnosticConsumer::IncludeInDiagnosticCounts() const { return true; }
972 void IgnoringDiagConsumer::anchor() { }
974 ForwardingDiagnosticConsumer::~ForwardingDiagnosticConsumer() {}
976 void ForwardingDiagnosticConsumer::HandleDiagnostic(
977 DiagnosticsEngine::Level DiagLevel,
978 const Diagnostic &Info) {
979 Target.HandleDiagnostic(DiagLevel, Info);
982 void ForwardingDiagnosticConsumer::clear() {
983 DiagnosticConsumer::clear();
987 bool ForwardingDiagnosticConsumer::IncludeInDiagnosticCounts() const {
988 return Target.IncludeInDiagnosticCounts();
991 PartialDiagnostic::StorageAllocator::StorageAllocator() {
992 for (unsigned I = 0; I != NumCached; ++I)
993 FreeList[I] = Cached + I;
994 NumFreeListEntries = NumCached;
997 PartialDiagnostic::StorageAllocator::~StorageAllocator() {
998 // Don't assert if we are in a CrashRecovery context, as this invariant may
999 // be invalidated during a crash.
1000 assert((NumFreeListEntries == NumCached ||
1001 llvm::CrashRecoveryContext::isRecoveringFromCrash()) &&
1002 "A partial is on the lamb");