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/Diagnostic.h"
15 #include "clang/Basic/IdentifierTable.h"
16 #include "clang/Basic/PartialDiagnostic.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include "llvm/Support/CrashRecoveryContext.h"
21 using namespace clang;
23 static void DummyArgToStringFn(Diagnostic::ArgumentKind AK, intptr_t QT,
24 const char *Modifier, unsigned ML,
25 const char *Argument, unsigned ArgLen,
26 const Diagnostic::ArgumentValue *PrevArgs,
28 llvm::SmallVectorImpl<char> &Output,
30 llvm::SmallVectorImpl<intptr_t> &QualTypeVals) {
31 const char *Str = "<can't format argument>";
32 Output.append(Str, Str+strlen(Str));
36 Diagnostic::Diagnostic(const llvm::IntrusiveRefCntPtr<DiagnosticIDs> &diags,
37 DiagnosticClient *client, bool ShouldOwnClient)
38 : Diags(diags), Client(client), OwnsDiagClient(ShouldOwnClient),
40 ArgToStringFn = DummyArgToStringFn;
41 ArgToStringCookie = 0;
43 AllExtensionsSilenced = 0;
44 IgnoreAllWarnings = false;
45 WarningsAsErrors = false;
46 ErrorsAsFatal = false;
47 SuppressSystemWarnings = false;
48 SuppressAllDiagnostics = false;
49 ShowOverloads = Ovl_All;
50 ExtBehavior = Ext_Ignore;
53 TemplateBacktraceLimit = 0;
58 Diagnostic::~Diagnostic() {
63 void Diagnostic::setClient(DiagnosticClient *client, bool ShouldOwnClient) {
64 if (OwnsDiagClient && Client)
68 OwnsDiagClient = ShouldOwnClient;
71 void Diagnostic::pushMappings(SourceLocation Loc) {
72 DiagStateOnPushStack.push_back(GetCurDiagState());
75 bool Diagnostic::popMappings(SourceLocation Loc) {
76 if (DiagStateOnPushStack.empty())
79 if (DiagStateOnPushStack.back() != GetCurDiagState()) {
80 // State changed at some point between push/pop.
81 PushDiagStatePoint(DiagStateOnPushStack.back(), Loc);
83 DiagStateOnPushStack.pop_back();
87 void Diagnostic::Reset() {
88 ErrorOccurred = false;
89 FatalErrorOccurred = false;
90 UnrecoverableErrorOccurred = false;
94 NumErrorsSuppressed = 0;
97 // Set LastDiagLevel to an "unset" state. If we set it to 'Ignored', notes
98 // using a Diagnostic associated to a translation unit that follow
99 // diagnostics from a Diagnostic associated to anoter t.u. will not be
101 LastDiagLevel = (DiagnosticIDs::Level)-1;
104 // Clear state related to #pragma diagnostic.
106 DiagStatePoints.clear();
107 DiagStateOnPushStack.clear();
109 // Create a DiagState and DiagStatePoint representing diagnostic changes
110 // through command-line.
111 DiagStates.push_back(DiagState());
112 PushDiagStatePoint(&DiagStates.back(), SourceLocation());
115 void Diagnostic::SetDelayedDiagnostic(unsigned DiagID, llvm::StringRef Arg1,
116 llvm::StringRef Arg2) {
120 DelayedDiagID = DiagID;
121 DelayedDiagArg1 = Arg1.str();
122 DelayedDiagArg2 = Arg2.str();
125 void Diagnostic::ReportDelayed() {
126 Report(DelayedDiagID) << DelayedDiagArg1 << DelayedDiagArg2;
128 DelayedDiagArg1.clear();
129 DelayedDiagArg2.clear();
132 Diagnostic::DiagStatePointsTy::iterator
133 Diagnostic::GetDiagStatePointForLoc(SourceLocation L) const {
134 assert(!DiagStatePoints.empty());
135 assert(DiagStatePoints.front().Loc.isInvalid() &&
136 "Should have created a DiagStatePoint for command-line");
138 FullSourceLoc Loc(L, *SourceMgr);
140 return DiagStatePoints.end() - 1;
142 DiagStatePointsTy::iterator Pos = DiagStatePoints.end();
143 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
144 if (LastStateChangePos.isValid() &&
145 Loc.isBeforeInTranslationUnitThan(LastStateChangePos))
146 Pos = std::upper_bound(DiagStatePoints.begin(), DiagStatePoints.end(),
147 DiagStatePoint(0, Loc));
152 /// \brief This allows the client to specify that certain
153 /// warnings are ignored. Notes can never be mapped, errors can only be
154 /// mapped to fatal, and WARNINGs and EXTENSIONs can be mapped arbitrarily.
156 /// \param The source location that this change of diagnostic state should
157 /// take affect. It can be null if we are setting the latest state.
158 void Diagnostic::setDiagnosticMapping(diag::kind Diag, diag::Mapping Map,
160 assert(Diag < diag::DIAG_UPPER_LIMIT &&
161 "Can only map builtin diagnostics");
162 assert((Diags->isBuiltinWarningOrExtension(Diag) ||
163 (Map == diag::MAP_FATAL || Map == diag::MAP_ERROR)) &&
164 "Cannot map errors into warnings!");
165 assert(!DiagStatePoints.empty());
167 bool isPragma = L.isValid();
168 FullSourceLoc Loc(L, *SourceMgr);
169 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
171 // Common case; setting all the diagnostics of a group in one place.
172 if (Loc.isInvalid() || Loc == LastStateChangePos) {
173 setDiagnosticMappingInternal(Diag, Map, GetCurDiagState(), true, isPragma);
177 // Another common case; modifying diagnostic state in a source location
178 // after the previous one.
179 if ((Loc.isValid() && LastStateChangePos.isInvalid()) ||
180 LastStateChangePos.isBeforeInTranslationUnitThan(Loc)) {
181 // A diagnostic pragma occurred, create a new DiagState initialized with
182 // the current one and a new DiagStatePoint to record at which location
183 // the new state became active.
184 DiagStates.push_back(*GetCurDiagState());
185 PushDiagStatePoint(&DiagStates.back(), Loc);
186 setDiagnosticMappingInternal(Diag, Map, GetCurDiagState(), true, isPragma);
190 // We allow setting the diagnostic state in random source order for
191 // completeness but it should not be actually happening in normal practice.
193 DiagStatePointsTy::iterator Pos = GetDiagStatePointForLoc(Loc);
194 assert(Pos != DiagStatePoints.end());
196 // Update all diagnostic states that are active after the given location.
197 for (DiagStatePointsTy::iterator
198 I = Pos+1, E = DiagStatePoints.end(); I != E; ++I) {
199 setDiagnosticMappingInternal(Diag, Map, I->State, true, isPragma);
202 // If the location corresponds to an existing point, just update its state.
203 if (Pos->Loc == Loc) {
204 setDiagnosticMappingInternal(Diag, Map, Pos->State, true, isPragma);
208 // Create a new state/point and fit it into the vector of DiagStatePoints
209 // so that the vector is always ordered according to location.
210 Pos->Loc.isBeforeInTranslationUnitThan(Loc);
211 DiagStates.push_back(*Pos->State);
212 DiagState *NewState = &DiagStates.back();
213 setDiagnosticMappingInternal(Diag, Map, NewState, true, isPragma);
214 DiagStatePoints.insert(Pos+1, DiagStatePoint(NewState,
215 FullSourceLoc(Loc, *SourceMgr)));
218 void Diagnostic::Report(const StoredDiagnostic &storedDiag) {
219 assert(CurDiagID == ~0U && "Multiple diagnostics in flight at once!");
221 CurDiagLoc = storedDiag.getLocation();
222 CurDiagID = storedDiag.getID();
225 NumDiagRanges = storedDiag.range_size();
226 assert(NumDiagRanges < sizeof(DiagRanges)/sizeof(DiagRanges[0]) &&
227 "Too many arguments to diagnostic!");
229 for (StoredDiagnostic::range_iterator
230 RI = storedDiag.range_begin(),
231 RE = storedDiag.range_end(); RI != RE; ++RI)
232 DiagRanges[i++] = *RI;
234 NumFixItHints = storedDiag.fixit_size();
235 assert(NumFixItHints < Diagnostic::MaxFixItHints && "Too many fix-it hints!");
237 for (StoredDiagnostic::fixit_iterator
238 FI = storedDiag.fixit_begin(),
239 FE = storedDiag.fixit_end(); FI != FE; ++FI)
240 FixItHints[i++] = *FI;
242 assert(Client && "DiagnosticClient not set!");
243 Level DiagLevel = storedDiag.getLevel();
244 DiagnosticInfo Info(this, storedDiag.getMessage());
245 Client->HandleDiagnostic(DiagLevel, Info);
246 if (Client->IncludeInDiagnosticCounts()) {
247 if (DiagLevel == Diagnostic::Warning)
254 void DiagnosticBuilder::FlushCounts() {
255 DiagObj->NumDiagArgs = NumArgs;
256 DiagObj->NumDiagRanges = NumRanges;
257 DiagObj->NumFixItHints = NumFixItHints;
260 bool DiagnosticBuilder::Emit() {
261 // If DiagObj is null, then its soul was stolen by the copy ctor
262 // or the user called Emit().
263 if (DiagObj == 0) return false;
265 // When emitting diagnostics, we set the final argument count into
266 // the Diagnostic object.
269 // Process the diagnostic, sending the accumulated information to the
271 bool Emitted = DiagObj->ProcessDiag();
273 // Clear out the current diagnostic object.
274 unsigned DiagID = DiagObj->CurDiagID;
277 // If there was a delayed diagnostic, emit it now.
278 if (DiagObj->DelayedDiagID && DiagObj->DelayedDiagID != DiagID)
279 DiagObj->ReportDelayed();
281 // This diagnostic is dead.
288 DiagnosticClient::~DiagnosticClient() {}
290 void DiagnosticClient::HandleDiagnostic(Diagnostic::Level DiagLevel,
291 const DiagnosticInfo &Info) {
292 if (!IncludeInDiagnosticCounts())
295 if (DiagLevel == Diagnostic::Warning)
297 else if (DiagLevel >= Diagnostic::Error)
301 /// ModifierIs - Return true if the specified modifier matches specified string.
302 template <std::size_t StrLen>
303 static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
304 const char (&Str)[StrLen]) {
305 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
308 /// ScanForward - Scans forward, looking for the given character, skipping
309 /// nested clauses and escaped characters.
310 static const char *ScanFormat(const char *I, const char *E, char Target) {
313 for ( ; I != E; ++I) {
314 if (Depth == 0 && *I == Target) return I;
315 if (Depth != 0 && *I == '}') Depth--;
321 // Escaped characters get implicitly skipped here.
324 if (!isdigit(*I) && !ispunct(*I)) {
325 for (I++; I != E && !isdigit(*I) && *I != '{'; I++) ;
335 /// HandleSelectModifier - Handle the integer 'select' modifier. This is used
336 /// like this: %select{foo|bar|baz}2. This means that the integer argument
337 /// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'.
338 /// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
339 /// This is very useful for certain classes of variant diagnostics.
340 static void HandleSelectModifier(const DiagnosticInfo &DInfo, unsigned ValNo,
341 const char *Argument, unsigned ArgumentLen,
342 llvm::SmallVectorImpl<char> &OutStr) {
343 const char *ArgumentEnd = Argument+ArgumentLen;
345 // Skip over 'ValNo' |'s.
347 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
348 assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
349 " larger than the number of options in the diagnostic string!");
350 Argument = NextVal+1; // Skip this string.
354 // Get the end of the value. This is either the } or the |.
355 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
357 // Recursively format the result of the select clause into the output string.
358 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
361 /// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the
362 /// letter 's' to the string if the value is not 1. This is used in cases like
363 /// this: "you idiot, you have %4 parameter%s4!".
364 static void HandleIntegerSModifier(unsigned ValNo,
365 llvm::SmallVectorImpl<char> &OutStr) {
367 OutStr.push_back('s');
370 /// HandleOrdinalModifier - Handle the integer 'ord' modifier. This
371 /// prints the ordinal form of the given integer, with 1 corresponding
372 /// to the first ordinal. Currently this is hard-coded to use the
374 static void HandleOrdinalModifier(unsigned ValNo,
375 llvm::SmallVectorImpl<char> &OutStr) {
376 assert(ValNo != 0 && "ValNo must be strictly positive!");
378 llvm::raw_svector_ostream Out(OutStr);
380 // We could use text forms for the first N ordinals, but the numeric
381 // forms are actually nicer in diagnostics because they stand out.
384 // It is critically important that we do this perfectly for
385 // user-written sequences with over 100 elements.
386 switch (ValNo % 100) {
392 switch (ValNo % 10) {
393 case 1: Out << "st"; return;
394 case 2: Out << "nd"; return;
395 case 3: Out << "rd"; return;
396 default: Out << "th"; return;
402 /// PluralNumber - Parse an unsigned integer and advance Start.
403 static unsigned PluralNumber(const char *&Start, const char *End) {
404 // Programming 101: Parse a decimal number :-)
406 while (Start != End && *Start >= '0' && *Start <= '9') {
414 /// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
415 static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
417 unsigned Ref = PluralNumber(Start, End);
422 unsigned Low = PluralNumber(Start, End);
423 assert(*Start == ',' && "Bad plural expression syntax: expected ,");
425 unsigned High = PluralNumber(Start, End);
426 assert(*Start == ']' && "Bad plural expression syntax: expected )");
428 return Low <= Val && Val <= High;
431 /// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
432 static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
442 unsigned Arg = PluralNumber(Start, End);
443 assert(*Start == '=' && "Bad plural expression syntax: expected =");
445 unsigned ValMod = ValNo % Arg;
446 if (TestPluralRange(ValMod, Start, End))
449 assert((C == '[' || (C >= '0' && C <= '9')) &&
450 "Bad plural expression syntax: unexpected character");
452 if (TestPluralRange(ValNo, Start, End))
456 // Scan for next or-expr part.
457 Start = std::find(Start, End, ',');
465 /// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
466 /// for complex plural forms, or in languages where all plurals are complex.
467 /// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
468 /// conditions that are tested in order, the form corresponding to the first
469 /// that applies being emitted. The empty condition is always true, making the
470 /// last form a default case.
471 /// Conditions are simple boolean expressions, where n is the number argument.
472 /// Here are the rules.
473 /// condition := expression | empty
474 /// empty := -> always true
475 /// expression := numeric [',' expression] -> logical or
476 /// numeric := range -> true if n in range
477 /// | '%' number '=' range -> true if n % number in range
479 /// | '[' number ',' number ']' -> ranges are inclusive both ends
481 /// Here are some examples from the GNU gettext manual written in this form:
485 /// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
487 /// {1:form0|2:form1|:form2}
489 /// {1:form0|0,%100=[1,19]:form1|:form2}
491 /// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
492 /// Russian (requires repeated form):
493 /// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
495 /// {1:form0|[2,4]:form1|:form2}
496 /// Polish (requires repeated form):
497 /// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
498 static void HandlePluralModifier(const DiagnosticInfo &DInfo, unsigned ValNo,
499 const char *Argument, unsigned ArgumentLen,
500 llvm::SmallVectorImpl<char> &OutStr) {
501 const char *ArgumentEnd = Argument + ArgumentLen;
503 assert(Argument < ArgumentEnd && "Plural expression didn't match.");
504 const char *ExprEnd = Argument;
505 while (*ExprEnd != ':') {
506 assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
509 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
510 Argument = ExprEnd + 1;
511 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
513 // Recursively format the result of the plural clause into the
515 DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr);
518 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
523 /// FormatDiagnostic - Format this diagnostic into a string, substituting the
524 /// formal arguments into the %0 slots. The result is appended onto the Str
526 void DiagnosticInfo::
527 FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const {
528 if (!StoredDiagMessage.empty()) {
529 OutStr.append(StoredDiagMessage.begin(), StoredDiagMessage.end());
533 llvm::StringRef Diag =
534 getDiags()->getDiagnosticIDs()->getDescription(getID());
536 FormatDiagnostic(Diag.begin(), Diag.end(), OutStr);
539 void DiagnosticInfo::
540 FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
541 llvm::SmallVectorImpl<char> &OutStr) const {
543 /// FormattedArgs - Keep track of all of the arguments formatted by
544 /// ConvertArgToString and pass them into subsequent calls to
545 /// ConvertArgToString, allowing the implementation to avoid redundancies in
547 llvm::SmallVector<Diagnostic::ArgumentValue, 8> FormattedArgs;
549 /// QualTypeVals - Pass a vector of arrays so that QualType names can be
550 /// compared to see if more information is needed to be printed.
551 llvm::SmallVector<intptr_t, 2> QualTypeVals;
552 for (unsigned i = 0, e = getNumArgs(); i < e; ++i)
553 if (getArgKind(i) == Diagnostic::ak_qualtype)
554 QualTypeVals.push_back(getRawArg(i));
556 while (DiagStr != DiagEnd) {
557 if (DiagStr[0] != '%') {
558 // Append non-%0 substrings to Str if we have one.
559 const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
560 OutStr.append(DiagStr, StrEnd);
563 } else if (ispunct(DiagStr[1])) {
564 OutStr.push_back(DiagStr[1]); // %% -> %.
572 // This must be a placeholder for a diagnostic argument. The format for a
573 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
574 // The digit is a number from 0-9 indicating which argument this comes from.
575 // The modifier is a string of digits from the set [-a-z]+, arguments is a
576 // brace enclosed string.
577 const char *Modifier = 0, *Argument = 0;
578 unsigned ModifierLen = 0, ArgumentLen = 0;
580 // Check to see if we have a modifier. If so eat it.
581 if (!isdigit(DiagStr[0])) {
583 while (DiagStr[0] == '-' ||
584 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
586 ModifierLen = DiagStr-Modifier;
588 // If we have an argument, get it next.
589 if (DiagStr[0] == '{') {
590 ++DiagStr; // Skip {.
593 DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
594 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
595 ArgumentLen = DiagStr-Argument;
596 ++DiagStr; // Skip }.
600 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
601 unsigned ArgNo = *DiagStr++ - '0';
603 Diagnostic::ArgumentKind Kind = getArgKind(ArgNo);
607 case Diagnostic::ak_std_string: {
608 const std::string &S = getArgStdStr(ArgNo);
609 assert(ModifierLen == 0 && "No modifiers for strings yet");
610 OutStr.append(S.begin(), S.end());
613 case Diagnostic::ak_c_string: {
614 const char *S = getArgCStr(ArgNo);
615 assert(ModifierLen == 0 && "No modifiers for strings yet");
617 // Don't crash if get passed a null pointer by accident.
621 OutStr.append(S, S + strlen(S));
624 // ---- INTEGERS ----
625 case Diagnostic::ak_sint: {
626 int Val = getArgSInt(ArgNo);
628 if (ModifierIs(Modifier, ModifierLen, "select")) {
629 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen,
631 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
632 HandleIntegerSModifier(Val, OutStr);
633 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
634 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
636 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
637 HandleOrdinalModifier((unsigned)Val, OutStr);
639 assert(ModifierLen == 0 && "Unknown integer modifier");
640 llvm::raw_svector_ostream(OutStr) << Val;
644 case Diagnostic::ak_uint: {
645 unsigned Val = getArgUInt(ArgNo);
647 if (ModifierIs(Modifier, ModifierLen, "select")) {
648 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
649 } else if (ModifierIs(Modifier, ModifierLen, "s")) {
650 HandleIntegerSModifier(Val, OutStr);
651 } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
652 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
654 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
655 HandleOrdinalModifier(Val, OutStr);
657 assert(ModifierLen == 0 && "Unknown integer modifier");
658 llvm::raw_svector_ostream(OutStr) << Val;
662 // ---- NAMES and TYPES ----
663 case Diagnostic::ak_identifierinfo: {
664 const IdentifierInfo *II = getArgIdentifier(ArgNo);
665 assert(ModifierLen == 0 && "No modifiers for strings yet");
667 // Don't crash if get passed a null pointer by accident.
669 const char *S = "(null)";
670 OutStr.append(S, S + strlen(S));
674 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
677 case Diagnostic::ak_qualtype:
678 case Diagnostic::ak_declarationname:
679 case Diagnostic::ak_nameddecl:
680 case Diagnostic::ak_nestednamespec:
681 case Diagnostic::ak_declcontext:
682 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
683 Modifier, ModifierLen,
684 Argument, ArgumentLen,
685 FormattedArgs.data(), FormattedArgs.size(),
686 OutStr, QualTypeVals);
690 // Remember this argument info for subsequent formatting operations. Turn
691 // std::strings into a null terminated string to make it be the same case as
692 // all the other ones.
693 if (Kind != Diagnostic::ak_std_string)
694 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
696 FormattedArgs.push_back(std::make_pair(Diagnostic::ak_c_string,
697 (intptr_t)getArgStdStr(ArgNo).c_str()));
702 StoredDiagnostic::StoredDiagnostic() { }
704 StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level, unsigned ID,
705 llvm::StringRef Message)
706 : ID(ID), Level(Level), Loc(), Message(Message) { }
708 StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level,
709 const DiagnosticInfo &Info)
710 : ID(Info.getID()), Level(Level)
712 assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
713 "Valid source location without setting a source manager for diagnostic");
714 if (Info.getLocation().isValid())
715 Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
716 llvm::SmallString<64> Message;
717 Info.FormatDiagnostic(Message);
718 this->Message.assign(Message.begin(), Message.end());
720 Ranges.reserve(Info.getNumRanges());
721 for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I)
722 Ranges.push_back(Info.getRange(I));
724 FixIts.reserve(Info.getNumFixItHints());
725 for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I)
726 FixIts.push_back(Info.getFixItHint(I));
729 StoredDiagnostic::~StoredDiagnostic() { }
731 /// IncludeInDiagnosticCounts - This method (whose default implementation
732 /// returns true) indicates whether the diagnostics handled by this
733 /// DiagnosticClient should be included in the number of diagnostics
734 /// reported by Diagnostic.
735 bool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; }
737 PartialDiagnostic::StorageAllocator::StorageAllocator() {
738 for (unsigned I = 0; I != NumCached; ++I)
739 FreeList[I] = Cached + I;
740 NumFreeListEntries = NumCached;
743 PartialDiagnostic::StorageAllocator::~StorageAllocator() {
744 // Don't assert if we are in a CrashRecovery context, as this
745 // invariant may be invalidated during a crash.
746 assert((NumFreeListEntries == NumCached || llvm::CrashRecoveryContext::isRecoveringFromCrash()) && "A partial is on the lamb");