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