Diagnostic.cpp revision 3efd52cf8f4e57c5571bd8cc3168264c3bc46a1e
1//===--- Diagnostic.cpp - C Language Family Diagnostic Handling -----------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file implements the Diagnostic-related interfaces. 11// 12//===----------------------------------------------------------------------===// 13 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" 19using namespace clang; 20 21static void DummyArgToStringFn(Diagnostic::ArgumentKind AK, intptr_t QT, 22 const char *Modifier, unsigned ML, 23 const char *Argument, unsigned ArgLen, 24 const Diagnostic::ArgumentValue *PrevArgs, 25 unsigned NumPrevArgs, 26 llvm::SmallVectorImpl<char> &Output, 27 void *Cookie) { 28 const char *Str = "<can't format argument>"; 29 Output.append(Str, Str+strlen(Str)); 30} 31 32 33Diagnostic::Diagnostic(const llvm::IntrusiveRefCntPtr<DiagnosticIDs> &diags, 34 DiagnosticClient *client, bool ShouldOwnClient) 35 : Diags(diags), Client(client), OwnsDiagClient(ShouldOwnClient), 36 SourceMgr(0) { 37 ArgToStringFn = DummyArgToStringFn; 38 ArgToStringCookie = 0; 39 40 AllExtensionsSilenced = 0; 41 IgnoreAllWarnings = false; 42 WarningsAsErrors = false; 43 ErrorsAsFatal = false; 44 SuppressSystemWarnings = false; 45 SuppressAllDiagnostics = false; 46 ShowOverloads = Ovl_All; 47 ExtBehavior = Ext_Ignore; 48 49 ErrorLimit = 0; 50 TemplateBacktraceLimit = 0; 51 52 // Create a DiagState and DiagStatePoint representing diagnostic changes 53 // through command-line. 54 DiagStates.push_back(DiagState()); 55 PushDiagStatePoint(&DiagStates.back(), SourceLocation()); 56 57 Reset(); 58} 59 60Diagnostic::~Diagnostic() { 61 if (OwnsDiagClient) 62 delete Client; 63} 64 65 66void Diagnostic::pushMappings(SourceLocation Loc) { 67 DiagStateOnPushStack.push_back(GetCurDiagState()); 68} 69 70bool Diagnostic::popMappings(SourceLocation Loc) { 71 if (DiagStateOnPushStack.empty()) 72 return false; 73 74 if (DiagStateOnPushStack.back() != GetCurDiagState()) { 75 // State changed at some point between push/pop. 76 PushDiagStatePoint(DiagStateOnPushStack.back(), Loc); 77 } 78 DiagStateOnPushStack.pop_back(); 79 return true; 80} 81 82void Diagnostic::Reset() { 83 ErrorOccurred = false; 84 FatalErrorOccurred = false; 85 86 NumWarnings = 0; 87 NumErrors = 0; 88 NumErrorsSuppressed = 0; 89 CurDiagID = ~0U; 90 // Set LastDiagLevel to an "unset" state. If we set it to 'Ignored', notes 91 // using a Diagnostic associated to a translation unit that follow 92 // diagnostics from a Diagnostic associated to anoter t.u. will not be 93 // displayed. 94 LastDiagLevel = (DiagnosticIDs::Level)-1; 95 DelayedDiagID = 0; 96} 97 98void Diagnostic::SetDelayedDiagnostic(unsigned DiagID, llvm::StringRef Arg1, 99 llvm::StringRef Arg2) { 100 if (DelayedDiagID) 101 return; 102 103 DelayedDiagID = DiagID; 104 DelayedDiagArg1 = Arg1.str(); 105 DelayedDiagArg2 = Arg2.str(); 106} 107 108void Diagnostic::ReportDelayed() { 109 Report(DelayedDiagID) << DelayedDiagArg1 << DelayedDiagArg2; 110 DelayedDiagID = 0; 111 DelayedDiagArg1.clear(); 112 DelayedDiagArg2.clear(); 113} 114 115Diagnostic::DiagStatePointsTy::iterator 116Diagnostic::GetDiagStatePointForLoc(SourceLocation L) const { 117 assert(!DiagStatePoints.empty()); 118 assert(DiagStatePoints.front().Loc.isInvalid() && 119 "Should have created a DiagStatePoint for command-line"); 120 121 FullSourceLoc Loc(L, *SourceMgr); 122 if (Loc.isInvalid()) 123 return DiagStatePoints.end() - 1; 124 125 DiagStatePointsTy::iterator Pos = DiagStatePoints.end(); 126 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc; 127 if (LastStateChangePos.isValid() && 128 Loc.isBeforeInTranslationUnitThan(LastStateChangePos)) 129 Pos = std::upper_bound(DiagStatePoints.begin(), DiagStatePoints.end(), 130 DiagStatePoint(0, Loc)); 131 --Pos; 132 return Pos; 133} 134 135/// \brief This allows the client to specify that certain 136/// warnings are ignored. Notes can never be mapped, errors can only be 137/// mapped to fatal, and WARNINGs and EXTENSIONs can be mapped arbitrarily. 138/// 139/// \param The source location that this change of diagnostic state should 140/// take affect. It can be null if we are setting the latest state. 141void Diagnostic::setDiagnosticMapping(diag::kind Diag, diag::Mapping Map, 142 SourceLocation L) { 143 assert(Diag < diag::DIAG_UPPER_LIMIT && 144 "Can only map builtin diagnostics"); 145 assert((Diags->isBuiltinWarningOrExtension(Diag) || 146 (Map == diag::MAP_FATAL || Map == diag::MAP_ERROR)) && 147 "Cannot map errors into warnings!"); 148 assert(!DiagStatePoints.empty()); 149 150 bool isPragma = L.isValid(); 151 FullSourceLoc Loc(L, *SourceMgr); 152 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc; 153 154 // Common case; setting all the diagnostics of a group in one place. 155 if (Loc.isInvalid() || Loc == LastStateChangePos) { 156 setDiagnosticMappingInternal(Diag, Map, GetCurDiagState(), true, isPragma); 157 return; 158 } 159 160 // Another common case; modifying diagnostic state in a source location 161 // after the previous one. 162 if ((Loc.isValid() && LastStateChangePos.isInvalid()) || 163 LastStateChangePos.isBeforeInTranslationUnitThan(Loc)) { 164 // A diagnostic pragma occured, create a new DiagState initialized with 165 // the current one and a new DiagStatePoint to record at which location 166 // the new state became active. 167 DiagStates.push_back(*GetCurDiagState()); 168 PushDiagStatePoint(&DiagStates.back(), Loc); 169 setDiagnosticMappingInternal(Diag, Map, GetCurDiagState(), true, isPragma); 170 return; 171 } 172 173 // We allow setting the diagnostic state in random source order for 174 // completeness but it should not be actually happening in normal practice. 175 176 DiagStatePointsTy::iterator Pos = GetDiagStatePointForLoc(Loc); 177 assert(Pos != DiagStatePoints.end()); 178 179 // Update all diagnostic states that are active after the given location. 180 for (DiagStatePointsTy::iterator 181 I = Pos+1, E = DiagStatePoints.end(); I != E; ++I) { 182 setDiagnosticMappingInternal(Diag, Map, I->State, true, isPragma); 183 } 184 185 // If the location corresponds to an existing point, just update its state. 186 if (Pos->Loc == Loc) { 187 setDiagnosticMappingInternal(Diag, Map, Pos->State, true, isPragma); 188 return; 189 } 190 191 // Create a new state/point and fit it into the vector of DiagStatePoints 192 // so that the vector is always ordered according to location. 193 Pos->Loc.isBeforeInTranslationUnitThan(Loc); 194 DiagStates.push_back(*Pos->State); 195 DiagState *NewState = &DiagStates.back(); 196 setDiagnosticMappingInternal(Diag, Map, NewState, true, isPragma); 197 DiagStatePoints.insert(Pos+1, DiagStatePoint(NewState, 198 FullSourceLoc(Loc, *SourceMgr))); 199} 200 201void DiagnosticBuilder::FlushCounts() { 202 DiagObj->NumDiagArgs = NumArgs; 203 DiagObj->NumDiagRanges = NumRanges; 204 DiagObj->NumFixItHints = NumFixItHints; 205} 206 207bool DiagnosticBuilder::Emit() { 208 // If DiagObj is null, then its soul was stolen by the copy ctor 209 // or the user called Emit(). 210 if (DiagObj == 0) return false; 211 212 // When emitting diagnostics, we set the final argument count into 213 // the Diagnostic object. 214 FlushCounts(); 215 216 // Process the diagnostic, sending the accumulated information to the 217 // DiagnosticClient. 218 bool Emitted = DiagObj->ProcessDiag(); 219 220 // Clear out the current diagnostic object. 221 unsigned DiagID = DiagObj->CurDiagID; 222 DiagObj->Clear(); 223 224 // If there was a delayed diagnostic, emit it now. 225 if (DiagObj->DelayedDiagID && DiagObj->DelayedDiagID != DiagID) 226 DiagObj->ReportDelayed(); 227 228 // This diagnostic is dead. 229 DiagObj = 0; 230 231 return Emitted; 232} 233 234 235DiagnosticClient::~DiagnosticClient() {} 236 237void DiagnosticClient::HandleDiagnostic(Diagnostic::Level DiagLevel, 238 const DiagnosticInfo &Info) { 239 if (!IncludeInDiagnosticCounts()) 240 return; 241 242 if (DiagLevel == Diagnostic::Warning) 243 ++NumWarnings; 244 else if (DiagLevel >= Diagnostic::Error) 245 ++NumErrors; 246} 247 248/// ModifierIs - Return true if the specified modifier matches specified string. 249template <std::size_t StrLen> 250static bool ModifierIs(const char *Modifier, unsigned ModifierLen, 251 const char (&Str)[StrLen]) { 252 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1); 253} 254 255/// ScanForward - Scans forward, looking for the given character, skipping 256/// nested clauses and escaped characters. 257static const char *ScanFormat(const char *I, const char *E, char Target) { 258 unsigned Depth = 0; 259 260 for ( ; I != E; ++I) { 261 if (Depth == 0 && *I == Target) return I; 262 if (Depth != 0 && *I == '}') Depth--; 263 264 if (*I == '%') { 265 I++; 266 if (I == E) break; 267 268 // Escaped characters get implicitly skipped here. 269 270 // Format specifier. 271 if (!isdigit(*I) && !ispunct(*I)) { 272 for (I++; I != E && !isdigit(*I) && *I != '{'; I++) ; 273 if (I == E) break; 274 if (*I == '{') 275 Depth++; 276 } 277 } 278 } 279 return E; 280} 281 282/// HandleSelectModifier - Handle the integer 'select' modifier. This is used 283/// like this: %select{foo|bar|baz}2. This means that the integer argument 284/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'. 285/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'. 286/// This is very useful for certain classes of variant diagnostics. 287static void HandleSelectModifier(const DiagnosticInfo &DInfo, unsigned ValNo, 288 const char *Argument, unsigned ArgumentLen, 289 llvm::SmallVectorImpl<char> &OutStr) { 290 const char *ArgumentEnd = Argument+ArgumentLen; 291 292 // Skip over 'ValNo' |'s. 293 while (ValNo) { 294 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|'); 295 assert(NextVal != ArgumentEnd && "Value for integer select modifier was" 296 " larger than the number of options in the diagnostic string!"); 297 Argument = NextVal+1; // Skip this string. 298 --ValNo; 299 } 300 301 // Get the end of the value. This is either the } or the |. 302 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|'); 303 304 // Recursively format the result of the select clause into the output string. 305 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr); 306} 307 308/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the 309/// letter 's' to the string if the value is not 1. This is used in cases like 310/// this: "you idiot, you have %4 parameter%s4!". 311static void HandleIntegerSModifier(unsigned ValNo, 312 llvm::SmallVectorImpl<char> &OutStr) { 313 if (ValNo != 1) 314 OutStr.push_back('s'); 315} 316 317/// HandleOrdinalModifier - Handle the integer 'ord' modifier. This 318/// prints the ordinal form of the given integer, with 1 corresponding 319/// to the first ordinal. Currently this is hard-coded to use the 320/// English form. 321static void HandleOrdinalModifier(unsigned ValNo, 322 llvm::SmallVectorImpl<char> &OutStr) { 323 assert(ValNo != 0 && "ValNo must be strictly positive!"); 324 325 llvm::raw_svector_ostream Out(OutStr); 326 327 // We could use text forms for the first N ordinals, but the numeric 328 // forms are actually nicer in diagnostics because they stand out. 329 Out << ValNo; 330 331 // It is critically important that we do this perfectly for 332 // user-written sequences with over 100 elements. 333 switch (ValNo % 100) { 334 case 11: 335 case 12: 336 case 13: 337 Out << "th"; return; 338 default: 339 switch (ValNo % 10) { 340 case 1: Out << "st"; return; 341 case 2: Out << "nd"; return; 342 case 3: Out << "rd"; return; 343 default: Out << "th"; return; 344 } 345 } 346} 347 348 349/// PluralNumber - Parse an unsigned integer and advance Start. 350static unsigned PluralNumber(const char *&Start, const char *End) { 351 // Programming 101: Parse a decimal number :-) 352 unsigned Val = 0; 353 while (Start != End && *Start >= '0' && *Start <= '9') { 354 Val *= 10; 355 Val += *Start - '0'; 356 ++Start; 357 } 358 return Val; 359} 360 361/// TestPluralRange - Test if Val is in the parsed range. Modifies Start. 362static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) { 363 if (*Start != '[') { 364 unsigned Ref = PluralNumber(Start, End); 365 return Ref == Val; 366 } 367 368 ++Start; 369 unsigned Low = PluralNumber(Start, End); 370 assert(*Start == ',' && "Bad plural expression syntax: expected ,"); 371 ++Start; 372 unsigned High = PluralNumber(Start, End); 373 assert(*Start == ']' && "Bad plural expression syntax: expected )"); 374 ++Start; 375 return Low <= Val && Val <= High; 376} 377 378/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier. 379static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) { 380 // Empty condition? 381 if (*Start == ':') 382 return true; 383 384 while (1) { 385 char C = *Start; 386 if (C == '%') { 387 // Modulo expression 388 ++Start; 389 unsigned Arg = PluralNumber(Start, End); 390 assert(*Start == '=' && "Bad plural expression syntax: expected ="); 391 ++Start; 392 unsigned ValMod = ValNo % Arg; 393 if (TestPluralRange(ValMod, Start, End)) 394 return true; 395 } else { 396 assert((C == '[' || (C >= '0' && C <= '9')) && 397 "Bad plural expression syntax: unexpected character"); 398 // Range expression 399 if (TestPluralRange(ValNo, Start, End)) 400 return true; 401 } 402 403 // Scan for next or-expr part. 404 Start = std::find(Start, End, ','); 405 if (Start == End) 406 break; 407 ++Start; 408 } 409 return false; 410} 411 412/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used 413/// for complex plural forms, or in languages where all plurals are complex. 414/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are 415/// conditions that are tested in order, the form corresponding to the first 416/// that applies being emitted. The empty condition is always true, making the 417/// last form a default case. 418/// Conditions are simple boolean expressions, where n is the number argument. 419/// Here are the rules. 420/// condition := expression | empty 421/// empty := -> always true 422/// expression := numeric [',' expression] -> logical or 423/// numeric := range -> true if n in range 424/// | '%' number '=' range -> true if n % number in range 425/// range := number 426/// | '[' number ',' number ']' -> ranges are inclusive both ends 427/// 428/// Here are some examples from the GNU gettext manual written in this form: 429/// English: 430/// {1:form0|:form1} 431/// Latvian: 432/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0} 433/// Gaeilge: 434/// {1:form0|2:form1|:form2} 435/// Romanian: 436/// {1:form0|0,%100=[1,19]:form1|:form2} 437/// Lithuanian: 438/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1} 439/// Russian (requires repeated form): 440/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2} 441/// Slovak 442/// {1:form0|[2,4]:form1|:form2} 443/// Polish (requires repeated form): 444/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2} 445static void HandlePluralModifier(const DiagnosticInfo &DInfo, unsigned ValNo, 446 const char *Argument, unsigned ArgumentLen, 447 llvm::SmallVectorImpl<char> &OutStr) { 448 const char *ArgumentEnd = Argument + ArgumentLen; 449 while (1) { 450 assert(Argument < ArgumentEnd && "Plural expression didn't match."); 451 const char *ExprEnd = Argument; 452 while (*ExprEnd != ':') { 453 assert(ExprEnd != ArgumentEnd && "Plural missing expression end"); 454 ++ExprEnd; 455 } 456 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) { 457 Argument = ExprEnd + 1; 458 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|'); 459 460 // Recursively format the result of the plural clause into the 461 // output string. 462 DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr); 463 return; 464 } 465 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1; 466 } 467} 468 469 470/// FormatDiagnostic - Format this diagnostic into a string, substituting the 471/// formal arguments into the %0 slots. The result is appended onto the Str 472/// array. 473void DiagnosticInfo:: 474FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const { 475 const char *DiagStr = getDiags()->getDiagnosticIDs()->getDescription(getID()); 476 const char *DiagEnd = DiagStr+strlen(DiagStr); 477 478 FormatDiagnostic(DiagStr, DiagEnd, OutStr); 479} 480 481void DiagnosticInfo:: 482FormatDiagnostic(const char *DiagStr, const char *DiagEnd, 483 llvm::SmallVectorImpl<char> &OutStr) const { 484 485 /// FormattedArgs - Keep track of all of the arguments formatted by 486 /// ConvertArgToString and pass them into subsequent calls to 487 /// ConvertArgToString, allowing the implementation to avoid redundancies in 488 /// obvious cases. 489 llvm::SmallVector<Diagnostic::ArgumentValue, 8> FormattedArgs; 490 491 while (DiagStr != DiagEnd) { 492 if (DiagStr[0] != '%') { 493 // Append non-%0 substrings to Str if we have one. 494 const char *StrEnd = std::find(DiagStr, DiagEnd, '%'); 495 OutStr.append(DiagStr, StrEnd); 496 DiagStr = StrEnd; 497 continue; 498 } else if (ispunct(DiagStr[1])) { 499 OutStr.push_back(DiagStr[1]); // %% -> %. 500 DiagStr += 2; 501 continue; 502 } 503 504 // Skip the %. 505 ++DiagStr; 506 507 // This must be a placeholder for a diagnostic argument. The format for a 508 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0". 509 // The digit is a number from 0-9 indicating which argument this comes from. 510 // The modifier is a string of digits from the set [-a-z]+, arguments is a 511 // brace enclosed string. 512 const char *Modifier = 0, *Argument = 0; 513 unsigned ModifierLen = 0, ArgumentLen = 0; 514 515 // Check to see if we have a modifier. If so eat it. 516 if (!isdigit(DiagStr[0])) { 517 Modifier = DiagStr; 518 while (DiagStr[0] == '-' || 519 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z')) 520 ++DiagStr; 521 ModifierLen = DiagStr-Modifier; 522 523 // If we have an argument, get it next. 524 if (DiagStr[0] == '{') { 525 ++DiagStr; // Skip {. 526 Argument = DiagStr; 527 528 DiagStr = ScanFormat(DiagStr, DiagEnd, '}'); 529 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!"); 530 ArgumentLen = DiagStr-Argument; 531 ++DiagStr; // Skip }. 532 } 533 } 534 535 assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic"); 536 unsigned ArgNo = *DiagStr++ - '0'; 537 538 Diagnostic::ArgumentKind Kind = getArgKind(ArgNo); 539 540 switch (Kind) { 541 // ---- STRINGS ---- 542 case Diagnostic::ak_std_string: { 543 const std::string &S = getArgStdStr(ArgNo); 544 assert(ModifierLen == 0 && "No modifiers for strings yet"); 545 OutStr.append(S.begin(), S.end()); 546 break; 547 } 548 case Diagnostic::ak_c_string: { 549 const char *S = getArgCStr(ArgNo); 550 assert(ModifierLen == 0 && "No modifiers for strings yet"); 551 552 // Don't crash if get passed a null pointer by accident. 553 if (!S) 554 S = "(null)"; 555 556 OutStr.append(S, S + strlen(S)); 557 break; 558 } 559 // ---- INTEGERS ---- 560 case Diagnostic::ak_sint: { 561 int Val = getArgSInt(ArgNo); 562 563 if (ModifierIs(Modifier, ModifierLen, "select")) { 564 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen, 565 OutStr); 566 } else if (ModifierIs(Modifier, ModifierLen, "s")) { 567 HandleIntegerSModifier(Val, OutStr); 568 } else if (ModifierIs(Modifier, ModifierLen, "plural")) { 569 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen, 570 OutStr); 571 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) { 572 HandleOrdinalModifier((unsigned)Val, OutStr); 573 } else { 574 assert(ModifierLen == 0 && "Unknown integer modifier"); 575 llvm::raw_svector_ostream(OutStr) << Val; 576 } 577 break; 578 } 579 case Diagnostic::ak_uint: { 580 unsigned Val = getArgUInt(ArgNo); 581 582 if (ModifierIs(Modifier, ModifierLen, "select")) { 583 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr); 584 } else if (ModifierIs(Modifier, ModifierLen, "s")) { 585 HandleIntegerSModifier(Val, OutStr); 586 } else if (ModifierIs(Modifier, ModifierLen, "plural")) { 587 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen, 588 OutStr); 589 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) { 590 HandleOrdinalModifier(Val, OutStr); 591 } else { 592 assert(ModifierLen == 0 && "Unknown integer modifier"); 593 llvm::raw_svector_ostream(OutStr) << Val; 594 } 595 break; 596 } 597 // ---- NAMES and TYPES ---- 598 case Diagnostic::ak_identifierinfo: { 599 const IdentifierInfo *II = getArgIdentifier(ArgNo); 600 assert(ModifierLen == 0 && "No modifiers for strings yet"); 601 602 // Don't crash if get passed a null pointer by accident. 603 if (!II) { 604 const char *S = "(null)"; 605 OutStr.append(S, S + strlen(S)); 606 continue; 607 } 608 609 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\''; 610 break; 611 } 612 case Diagnostic::ak_qualtype: 613 case Diagnostic::ak_declarationname: 614 case Diagnostic::ak_nameddecl: 615 case Diagnostic::ak_nestednamespec: 616 case Diagnostic::ak_declcontext: 617 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo), 618 Modifier, ModifierLen, 619 Argument, ArgumentLen, 620 FormattedArgs.data(), FormattedArgs.size(), 621 OutStr); 622 break; 623 } 624 625 // Remember this argument info for subsequent formatting operations. Turn 626 // std::strings into a null terminated string to make it be the same case as 627 // all the other ones. 628 if (Kind != Diagnostic::ak_std_string) 629 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo))); 630 else 631 FormattedArgs.push_back(std::make_pair(Diagnostic::ak_c_string, 632 (intptr_t)getArgStdStr(ArgNo).c_str())); 633 634 } 635} 636 637StoredDiagnostic::StoredDiagnostic() { } 638 639StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level, unsigned ID, 640 llvm::StringRef Message) 641 : ID(ID), Level(Level), Loc(), Message(Message) { } 642 643StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level, 644 const DiagnosticInfo &Info) 645 : ID(Info.getID()), Level(Level) 646{ 647 assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) && 648 "Valid source location without setting a source manager for diagnostic"); 649 if (Info.getLocation().isValid()) 650 Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager()); 651 llvm::SmallString<64> Message; 652 Info.FormatDiagnostic(Message); 653 this->Message.assign(Message.begin(), Message.end()); 654 655 Ranges.reserve(Info.getNumRanges()); 656 for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I) 657 Ranges.push_back(Info.getRange(I)); 658 659 FixIts.reserve(Info.getNumFixItHints()); 660 for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I) 661 FixIts.push_back(Info.getFixItHint(I)); 662} 663 664StoredDiagnostic::~StoredDiagnostic() { } 665 666/// IncludeInDiagnosticCounts - This method (whose default implementation 667/// returns true) indicates whether the diagnostics handled by this 668/// DiagnosticClient should be included in the number of diagnostics 669/// reported by Diagnostic. 670bool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; } 671 672PartialDiagnostic::StorageAllocator::StorageAllocator() { 673 for (unsigned I = 0; I != NumCached; ++I) 674 FreeList[I] = Cached + I; 675 NumFreeListEntries = NumCached; 676} 677 678PartialDiagnostic::StorageAllocator::~StorageAllocator() { 679 assert(NumFreeListEntries == NumCached && "A partial is on the lamb"); 680} 681