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/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/Locale.h" 23 #include "llvm/Support/raw_ostream.h" 24 25 using namespace clang; 26 27 const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB, 28 DiagNullabilityKind nullability) { 29 StringRef string; 30 switch (nullability.first) { 31 case NullabilityKind::NonNull: 32 string = nullability.second ? "'nonnull'" : "'_Nonnull'"; 33 break; 34 35 case NullabilityKind::Nullable: 36 string = nullability.second ? "'nullable'" : "'_Nullable'"; 37 break; 38 39 case NullabilityKind::Unspecified: 40 string = nullability.second ? "'null_unspecified'" : "'_Null_unspecified'"; 41 break; 42 } 43 44 DB.AddString(string); 45 return DB; 46 } 47 48 static void DummyArgToStringFn(DiagnosticsEngine::ArgumentKind AK, intptr_t QT, 49 StringRef Modifier, StringRef Argument, 50 ArrayRef<DiagnosticsEngine::ArgumentValue> PrevArgs, 51 SmallVectorImpl<char> &Output, 52 void *Cookie, 53 ArrayRef<intptr_t> QualTypeVals) { 54 StringRef Str = "<can't format argument>"; 55 Output.append(Str.begin(), Str.end()); 56 } 57 58 DiagnosticsEngine::DiagnosticsEngine(IntrusiveRefCntPtr<DiagnosticIDs> diags, 59 DiagnosticOptions *DiagOpts, 60 DiagnosticConsumer *client, 61 bool ShouldOwnClient) 62 : Diags(std::move(diags)), DiagOpts(DiagOpts), Client(nullptr), 63 SourceMgr(nullptr) { 64 setClient(client, ShouldOwnClient); 65 ArgToStringFn = DummyArgToStringFn; 66 ArgToStringCookie = nullptr; 67 68 AllExtensionsSilenced = 0; 69 IgnoreAllWarnings = false; 70 WarningsAsErrors = false; 71 EnableAllWarnings = false; 72 ErrorsAsFatal = false; 73 FatalsAsError = false; 74 SuppressSystemWarnings = false; 75 SuppressAllDiagnostics = false; 76 ElideType = true; 77 PrintTemplateTree = false; 78 ShowColors = false; 79 ShowOverloads = Ovl_All; 80 ExtBehavior = diag::Severity::Ignored; 81 82 ErrorLimit = 0; 83 TemplateBacktraceLimit = 0; 84 ConstexprBacktraceLimit = 0; 85 86 Reset(); 87 } 88 89 DiagnosticsEngine::~DiagnosticsEngine() { 90 // If we own the diagnostic client, destroy it first so that it can access the 91 // engine from its destructor. 92 setClient(nullptr); 93 } 94 95 void DiagnosticsEngine::setClient(DiagnosticConsumer *client, 96 bool ShouldOwnClient) { 97 Owner.reset(ShouldOwnClient ? client : nullptr); 98 Client = client; 99 } 100 101 void DiagnosticsEngine::pushMappings(SourceLocation Loc) { 102 DiagStateOnPushStack.push_back(GetCurDiagState()); 103 } 104 105 bool DiagnosticsEngine::popMappings(SourceLocation Loc) { 106 if (DiagStateOnPushStack.empty()) 107 return false; 108 109 if (DiagStateOnPushStack.back() != GetCurDiagState()) { 110 // State changed at some point between push/pop. 111 PushDiagStatePoint(DiagStateOnPushStack.back(), Loc); 112 } 113 DiagStateOnPushStack.pop_back(); 114 return true; 115 } 116 117 void DiagnosticsEngine::Reset() { 118 ErrorOccurred = false; 119 UncompilableErrorOccurred = false; 120 FatalErrorOccurred = false; 121 UnrecoverableErrorOccurred = false; 122 123 NumWarnings = 0; 124 NumErrors = 0; 125 TrapNumErrorsOccurred = 0; 126 TrapNumUnrecoverableErrorsOccurred = 0; 127 128 CurDiagID = ~0U; 129 LastDiagLevel = DiagnosticIDs::Ignored; 130 DelayedDiagID = 0; 131 132 // Clear state related to #pragma diagnostic. 133 DiagStates.clear(); 134 DiagStatePoints.clear(); 135 DiagStateOnPushStack.clear(); 136 137 // Create a DiagState and DiagStatePoint representing diagnostic changes 138 // through command-line. 139 DiagStates.emplace_back(); 140 DiagStatePoints.push_back(DiagStatePoint(&DiagStates.back(), FullSourceLoc())); 141 } 142 143 void DiagnosticsEngine::SetDelayedDiagnostic(unsigned DiagID, StringRef Arg1, 144 StringRef Arg2) { 145 if (DelayedDiagID) 146 return; 147 148 DelayedDiagID = DiagID; 149 DelayedDiagArg1 = Arg1.str(); 150 DelayedDiagArg2 = Arg2.str(); 151 } 152 153 void DiagnosticsEngine::ReportDelayed() { 154 Report(DelayedDiagID) << DelayedDiagArg1 << DelayedDiagArg2; 155 DelayedDiagID = 0; 156 DelayedDiagArg1.clear(); 157 DelayedDiagArg2.clear(); 158 } 159 160 DiagnosticsEngine::DiagStatePointsTy::iterator 161 DiagnosticsEngine::GetDiagStatePointForLoc(SourceLocation L) const { 162 assert(!DiagStatePoints.empty()); 163 assert(DiagStatePoints.front().Loc.isInvalid() && 164 "Should have created a DiagStatePoint for command-line"); 165 166 if (!SourceMgr) 167 return DiagStatePoints.end() - 1; 168 169 FullSourceLoc Loc(L, *SourceMgr); 170 if (Loc.isInvalid()) 171 return DiagStatePoints.end() - 1; 172 173 DiagStatePointsTy::iterator Pos = DiagStatePoints.end(); 174 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc; 175 if (LastStateChangePos.isValid() && 176 Loc.isBeforeInTranslationUnitThan(LastStateChangePos)) 177 Pos = std::upper_bound(DiagStatePoints.begin(), DiagStatePoints.end(), 178 DiagStatePoint(nullptr, Loc)); 179 --Pos; 180 return Pos; 181 } 182 183 void DiagnosticsEngine::setSeverity(diag::kind Diag, diag::Severity Map, 184 SourceLocation L) { 185 assert(Diag < diag::DIAG_UPPER_LIMIT && 186 "Can only map builtin diagnostics"); 187 assert((Diags->isBuiltinWarningOrExtension(Diag) || 188 (Map == diag::Severity::Fatal || Map == diag::Severity::Error)) && 189 "Cannot map errors into warnings!"); 190 assert(!DiagStatePoints.empty()); 191 assert((L.isInvalid() || SourceMgr) && "No SourceMgr for valid location"); 192 193 FullSourceLoc Loc = SourceMgr? FullSourceLoc(L, *SourceMgr) : FullSourceLoc(); 194 FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc; 195 // Don't allow a mapping to a warning override an error/fatal mapping. 196 if (Map == diag::Severity::Warning) { 197 DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag); 198 if (Info.getSeverity() == diag::Severity::Error || 199 Info.getSeverity() == diag::Severity::Fatal) 200 Map = Info.getSeverity(); 201 } 202 DiagnosticMapping Mapping = makeUserMapping(Map, L); 203 204 // Common case; setting all the diagnostics of a group in one place. 205 if (Loc.isInvalid() || Loc == LastStateChangePos) { 206 GetCurDiagState()->setMapping(Diag, Mapping); 207 return; 208 } 209 210 // Another common case; modifying diagnostic state in a source location 211 // after the previous one. 212 if ((Loc.isValid() && LastStateChangePos.isInvalid()) || 213 LastStateChangePos.isBeforeInTranslationUnitThan(Loc)) { 214 // A diagnostic pragma occurred, create a new DiagState initialized with 215 // the current one and a new DiagStatePoint to record at which location 216 // the new state became active. 217 DiagStates.push_back(*GetCurDiagState()); 218 PushDiagStatePoint(&DiagStates.back(), Loc); 219 GetCurDiagState()->setMapping(Diag, Mapping); 220 return; 221 } 222 223 // We allow setting the diagnostic state in random source order for 224 // completeness but it should not be actually happening in normal practice. 225 226 DiagStatePointsTy::iterator Pos = GetDiagStatePointForLoc(Loc); 227 assert(Pos != DiagStatePoints.end()); 228 229 // Update all diagnostic states that are active after the given location. 230 for (DiagStatePointsTy::iterator 231 I = Pos+1, E = DiagStatePoints.end(); I != E; ++I) { 232 I->State->setMapping(Diag, Mapping); 233 } 234 235 // If the location corresponds to an existing point, just update its state. 236 if (Pos->Loc == Loc) { 237 Pos->State->setMapping(Diag, Mapping); 238 return; 239 } 240 241 // Create a new state/point and fit it into the vector of DiagStatePoints 242 // so that the vector is always ordered according to location. 243 assert(Pos->Loc.isBeforeInTranslationUnitThan(Loc)); 244 DiagStates.push_back(*Pos->State); 245 DiagState *NewState = &DiagStates.back(); 246 NewState->setMapping(Diag, Mapping); 247 DiagStatePoints.insert(Pos+1, DiagStatePoint(NewState, 248 FullSourceLoc(Loc, *SourceMgr))); 249 } 250 251 bool DiagnosticsEngine::setSeverityForGroup(diag::Flavor Flavor, 252 StringRef Group, diag::Severity Map, 253 SourceLocation Loc) { 254 // Get the diagnostics in this group. 255 SmallVector<diag::kind, 256> GroupDiags; 256 if (Diags->getDiagnosticsInGroup(Flavor, Group, GroupDiags)) 257 return true; 258 259 // Set the mapping. 260 for (diag::kind Diag : GroupDiags) 261 setSeverity(Diag, Map, Loc); 262 263 return false; 264 } 265 266 bool DiagnosticsEngine::setDiagnosticGroupWarningAsError(StringRef Group, 267 bool Enabled) { 268 // If we are enabling this feature, just set the diagnostic mappings to map to 269 // errors. 270 if (Enabled) 271 return setSeverityForGroup(diag::Flavor::WarningOrError, Group, 272 diag::Severity::Error); 273 274 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and 275 // potentially downgrade anything already mapped to be a warning. 276 277 // Get the diagnostics in this group. 278 SmallVector<diag::kind, 8> GroupDiags; 279 if (Diags->getDiagnosticsInGroup(diag::Flavor::WarningOrError, Group, 280 GroupDiags)) 281 return true; 282 283 // Perform the mapping change. 284 for (diag::kind Diag : GroupDiags) { 285 DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag); 286 287 if (Info.getSeverity() == diag::Severity::Error || 288 Info.getSeverity() == diag::Severity::Fatal) 289 Info.setSeverity(diag::Severity::Warning); 290 291 Info.setNoWarningAsError(true); 292 } 293 294 return false; 295 } 296 297 bool DiagnosticsEngine::setDiagnosticGroupErrorAsFatal(StringRef Group, 298 bool Enabled) { 299 // If we are enabling this feature, just set the diagnostic mappings to map to 300 // fatal errors. 301 if (Enabled) 302 return setSeverityForGroup(diag::Flavor::WarningOrError, Group, 303 diag::Severity::Fatal); 304 305 // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and 306 // potentially downgrade anything already mapped to be an error. 307 308 // Get the diagnostics in this group. 309 SmallVector<diag::kind, 8> GroupDiags; 310 if (Diags->getDiagnosticsInGroup(diag::Flavor::WarningOrError, Group, 311 GroupDiags)) 312 return true; 313 314 // Perform the mapping change. 315 for (diag::kind Diag : GroupDiags) { 316 DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag); 317 318 if (Info.getSeverity() == diag::Severity::Fatal) 319 Info.setSeverity(diag::Severity::Error); 320 321 Info.setNoErrorAsFatal(true); 322 } 323 324 return false; 325 } 326 327 void DiagnosticsEngine::setSeverityForAll(diag::Flavor Flavor, 328 diag::Severity Map, 329 SourceLocation Loc) { 330 // Get all the diagnostics. 331 SmallVector<diag::kind, 64> AllDiags; 332 Diags->getAllDiagnostics(Flavor, AllDiags); 333 334 // Set the mapping. 335 for (diag::kind Diag : AllDiags) 336 if (Diags->isBuiltinWarningOrExtension(Diag)) 337 setSeverity(Diag, Map, Loc); 338 } 339 340 void DiagnosticsEngine::Report(const StoredDiagnostic &storedDiag) { 341 assert(CurDiagID == ~0U && "Multiple diagnostics in flight at once!"); 342 343 CurDiagLoc = storedDiag.getLocation(); 344 CurDiagID = storedDiag.getID(); 345 NumDiagArgs = 0; 346 347 DiagRanges.clear(); 348 DiagRanges.append(storedDiag.range_begin(), storedDiag.range_end()); 349 350 DiagFixItHints.clear(); 351 DiagFixItHints.append(storedDiag.fixit_begin(), storedDiag.fixit_end()); 352 353 assert(Client && "DiagnosticConsumer not set!"); 354 Level DiagLevel = storedDiag.getLevel(); 355 Diagnostic Info(this, storedDiag.getMessage()); 356 Client->HandleDiagnostic(DiagLevel, Info); 357 if (Client->IncludeInDiagnosticCounts()) { 358 if (DiagLevel == DiagnosticsEngine::Warning) 359 ++NumWarnings; 360 } 361 362 CurDiagID = ~0U; 363 } 364 365 bool DiagnosticsEngine::EmitCurrentDiagnostic(bool Force) { 366 assert(getClient() && "DiagnosticClient not set!"); 367 368 bool Emitted; 369 if (Force) { 370 Diagnostic Info(this); 371 372 // Figure out the diagnostic level of this message. 373 DiagnosticIDs::Level DiagLevel 374 = Diags->getDiagnosticLevel(Info.getID(), Info.getLocation(), *this); 375 376 Emitted = (DiagLevel != DiagnosticIDs::Ignored); 377 if (Emitted) { 378 // Emit the diagnostic regardless of suppression level. 379 Diags->EmitDiag(*this, DiagLevel); 380 } 381 } else { 382 // Process the diagnostic, sending the accumulated information to the 383 // DiagnosticConsumer. 384 Emitted = ProcessDiag(); 385 } 386 387 // Clear out the current diagnostic object. 388 unsigned DiagID = CurDiagID; 389 Clear(); 390 391 // If there was a delayed diagnostic, emit it now. 392 if (!Force && DelayedDiagID && DelayedDiagID != DiagID) 393 ReportDelayed(); 394 395 return Emitted; 396 } 397 398 399 DiagnosticConsumer::~DiagnosticConsumer() {} 400 401 void DiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, 402 const Diagnostic &Info) { 403 if (!IncludeInDiagnosticCounts()) 404 return; 405 406 if (DiagLevel == DiagnosticsEngine::Warning) 407 ++NumWarnings; 408 else if (DiagLevel >= DiagnosticsEngine::Error) 409 ++NumErrors; 410 } 411 412 /// ModifierIs - Return true if the specified modifier matches specified string. 413 template <std::size_t StrLen> 414 static bool ModifierIs(const char *Modifier, unsigned ModifierLen, 415 const char (&Str)[StrLen]) { 416 return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1); 417 } 418 419 /// ScanForward - Scans forward, looking for the given character, skipping 420 /// nested clauses and escaped characters. 421 static const char *ScanFormat(const char *I, const char *E, char Target) { 422 unsigned Depth = 0; 423 424 for ( ; I != E; ++I) { 425 if (Depth == 0 && *I == Target) return I; 426 if (Depth != 0 && *I == '}') Depth--; 427 428 if (*I == '%') { 429 I++; 430 if (I == E) break; 431 432 // Escaped characters get implicitly skipped here. 433 434 // Format specifier. 435 if (!isDigit(*I) && !isPunctuation(*I)) { 436 for (I++; I != E && !isDigit(*I) && *I != '{'; I++) ; 437 if (I == E) break; 438 if (*I == '{') 439 Depth++; 440 } 441 } 442 } 443 return E; 444 } 445 446 /// HandleSelectModifier - Handle the integer 'select' modifier. This is used 447 /// like this: %select{foo|bar|baz}2. This means that the integer argument 448 /// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'. 449 /// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'. 450 /// This is very useful for certain classes of variant diagnostics. 451 static void HandleSelectModifier(const Diagnostic &DInfo, unsigned ValNo, 452 const char *Argument, unsigned ArgumentLen, 453 SmallVectorImpl<char> &OutStr) { 454 const char *ArgumentEnd = Argument+ArgumentLen; 455 456 // Skip over 'ValNo' |'s. 457 while (ValNo) { 458 const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|'); 459 assert(NextVal != ArgumentEnd && "Value for integer select modifier was" 460 " larger than the number of options in the diagnostic string!"); 461 Argument = NextVal+1; // Skip this string. 462 --ValNo; 463 } 464 465 // Get the end of the value. This is either the } or the |. 466 const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|'); 467 468 // Recursively format the result of the select clause into the output string. 469 DInfo.FormatDiagnostic(Argument, EndPtr, OutStr); 470 } 471 472 /// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the 473 /// letter 's' to the string if the value is not 1. This is used in cases like 474 /// this: "you idiot, you have %4 parameter%s4!". 475 static void HandleIntegerSModifier(unsigned ValNo, 476 SmallVectorImpl<char> &OutStr) { 477 if (ValNo != 1) 478 OutStr.push_back('s'); 479 } 480 481 /// HandleOrdinalModifier - Handle the integer 'ord' modifier. This 482 /// prints the ordinal form of the given integer, with 1 corresponding 483 /// to the first ordinal. Currently this is hard-coded to use the 484 /// English form. 485 static void HandleOrdinalModifier(unsigned ValNo, 486 SmallVectorImpl<char> &OutStr) { 487 assert(ValNo != 0 && "ValNo must be strictly positive!"); 488 489 llvm::raw_svector_ostream Out(OutStr); 490 491 // We could use text forms for the first N ordinals, but the numeric 492 // forms are actually nicer in diagnostics because they stand out. 493 Out << ValNo << llvm::getOrdinalSuffix(ValNo); 494 } 495 496 497 /// PluralNumber - Parse an unsigned integer and advance Start. 498 static unsigned PluralNumber(const char *&Start, const char *End) { 499 // Programming 101: Parse a decimal number :-) 500 unsigned Val = 0; 501 while (Start != End && *Start >= '0' && *Start <= '9') { 502 Val *= 10; 503 Val += *Start - '0'; 504 ++Start; 505 } 506 return Val; 507 } 508 509 /// TestPluralRange - Test if Val is in the parsed range. Modifies Start. 510 static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) { 511 if (*Start != '[') { 512 unsigned Ref = PluralNumber(Start, End); 513 return Ref == Val; 514 } 515 516 ++Start; 517 unsigned Low = PluralNumber(Start, End); 518 assert(*Start == ',' && "Bad plural expression syntax: expected ,"); 519 ++Start; 520 unsigned High = PluralNumber(Start, End); 521 assert(*Start == ']' && "Bad plural expression syntax: expected )"); 522 ++Start; 523 return Low <= Val && Val <= High; 524 } 525 526 /// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier. 527 static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) { 528 // Empty condition? 529 if (*Start == ':') 530 return true; 531 532 while (1) { 533 char C = *Start; 534 if (C == '%') { 535 // Modulo expression 536 ++Start; 537 unsigned Arg = PluralNumber(Start, End); 538 assert(*Start == '=' && "Bad plural expression syntax: expected ="); 539 ++Start; 540 unsigned ValMod = ValNo % Arg; 541 if (TestPluralRange(ValMod, Start, End)) 542 return true; 543 } else { 544 assert((C == '[' || (C >= '0' && C <= '9')) && 545 "Bad plural expression syntax: unexpected character"); 546 // Range expression 547 if (TestPluralRange(ValNo, Start, End)) 548 return true; 549 } 550 551 // Scan for next or-expr part. 552 Start = std::find(Start, End, ','); 553 if (Start == End) 554 break; 555 ++Start; 556 } 557 return false; 558 } 559 560 /// HandlePluralModifier - Handle the integer 'plural' modifier. This is used 561 /// for complex plural forms, or in languages where all plurals are complex. 562 /// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are 563 /// conditions that are tested in order, the form corresponding to the first 564 /// that applies being emitted. The empty condition is always true, making the 565 /// last form a default case. 566 /// Conditions are simple boolean expressions, where n is the number argument. 567 /// Here are the rules. 568 /// condition := expression | empty 569 /// empty := -> always true 570 /// expression := numeric [',' expression] -> logical or 571 /// numeric := range -> true if n in range 572 /// | '%' number '=' range -> true if n % number in range 573 /// range := number 574 /// | '[' number ',' number ']' -> ranges are inclusive both ends 575 /// 576 /// Here are some examples from the GNU gettext manual written in this form: 577 /// English: 578 /// {1:form0|:form1} 579 /// Latvian: 580 /// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0} 581 /// Gaeilge: 582 /// {1:form0|2:form1|:form2} 583 /// Romanian: 584 /// {1:form0|0,%100=[1,19]:form1|:form2} 585 /// Lithuanian: 586 /// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1} 587 /// Russian (requires repeated form): 588 /// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2} 589 /// Slovak 590 /// {1:form0|[2,4]:form1|:form2} 591 /// Polish (requires repeated form): 592 /// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2} 593 static void HandlePluralModifier(const Diagnostic &DInfo, unsigned ValNo, 594 const char *Argument, unsigned ArgumentLen, 595 SmallVectorImpl<char> &OutStr) { 596 const char *ArgumentEnd = Argument + ArgumentLen; 597 while (1) { 598 assert(Argument < ArgumentEnd && "Plural expression didn't match."); 599 const char *ExprEnd = Argument; 600 while (*ExprEnd != ':') { 601 assert(ExprEnd != ArgumentEnd && "Plural missing expression end"); 602 ++ExprEnd; 603 } 604 if (EvalPluralExpr(ValNo, Argument, ExprEnd)) { 605 Argument = ExprEnd + 1; 606 ExprEnd = ScanFormat(Argument, ArgumentEnd, '|'); 607 608 // Recursively format the result of the plural clause into the 609 // output string. 610 DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr); 611 return; 612 } 613 Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1; 614 } 615 } 616 617 /// \brief Returns the friendly description for a token kind that will appear 618 /// without quotes in diagnostic messages. These strings may be translatable in 619 /// future. 620 static const char *getTokenDescForDiagnostic(tok::TokenKind Kind) { 621 switch (Kind) { 622 case tok::identifier: 623 return "identifier"; 624 default: 625 return nullptr; 626 } 627 } 628 629 /// FormatDiagnostic - Format this diagnostic into a string, substituting the 630 /// formal arguments into the %0 slots. The result is appended onto the Str 631 /// array. 632 void Diagnostic:: 633 FormatDiagnostic(SmallVectorImpl<char> &OutStr) const { 634 if (!StoredDiagMessage.empty()) { 635 OutStr.append(StoredDiagMessage.begin(), StoredDiagMessage.end()); 636 return; 637 } 638 639 StringRef Diag = 640 getDiags()->getDiagnosticIDs()->getDescription(getID()); 641 642 FormatDiagnostic(Diag.begin(), Diag.end(), OutStr); 643 } 644 645 void Diagnostic:: 646 FormatDiagnostic(const char *DiagStr, const char *DiagEnd, 647 SmallVectorImpl<char> &OutStr) const { 648 649 // When the diagnostic string is only "%0", the entire string is being given 650 // by an outside source. Remove unprintable characters from this string 651 // and skip all the other string processing. 652 if (DiagEnd - DiagStr == 2 && 653 StringRef(DiagStr, DiagEnd - DiagStr).equals("%0") && 654 getArgKind(0) == DiagnosticsEngine::ak_std_string) { 655 const std::string &S = getArgStdStr(0); 656 for (char c : S) { 657 if (llvm::sys::locale::isPrint(c) || c == '\t') { 658 OutStr.push_back(c); 659 } 660 } 661 return; 662 } 663 664 /// FormattedArgs - Keep track of all of the arguments formatted by 665 /// ConvertArgToString and pass them into subsequent calls to 666 /// ConvertArgToString, allowing the implementation to avoid redundancies in 667 /// obvious cases. 668 SmallVector<DiagnosticsEngine::ArgumentValue, 8> FormattedArgs; 669 670 /// QualTypeVals - Pass a vector of arrays so that QualType names can be 671 /// compared to see if more information is needed to be printed. 672 SmallVector<intptr_t, 2> QualTypeVals; 673 SmallVector<char, 64> Tree; 674 675 for (unsigned i = 0, e = getNumArgs(); i < e; ++i) 676 if (getArgKind(i) == DiagnosticsEngine::ak_qualtype) 677 QualTypeVals.push_back(getRawArg(i)); 678 679 while (DiagStr != DiagEnd) { 680 if (DiagStr[0] != '%') { 681 // Append non-%0 substrings to Str if we have one. 682 const char *StrEnd = std::find(DiagStr, DiagEnd, '%'); 683 OutStr.append(DiagStr, StrEnd); 684 DiagStr = StrEnd; 685 continue; 686 } else if (isPunctuation(DiagStr[1])) { 687 OutStr.push_back(DiagStr[1]); // %% -> %. 688 DiagStr += 2; 689 continue; 690 } 691 692 // Skip the %. 693 ++DiagStr; 694 695 // This must be a placeholder for a diagnostic argument. The format for a 696 // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0". 697 // The digit is a number from 0-9 indicating which argument this comes from. 698 // The modifier is a string of digits from the set [-a-z]+, arguments is a 699 // brace enclosed string. 700 const char *Modifier = nullptr, *Argument = nullptr; 701 unsigned ModifierLen = 0, ArgumentLen = 0; 702 703 // Check to see if we have a modifier. If so eat it. 704 if (!isDigit(DiagStr[0])) { 705 Modifier = DiagStr; 706 while (DiagStr[0] == '-' || 707 (DiagStr[0] >= 'a' && DiagStr[0] <= 'z')) 708 ++DiagStr; 709 ModifierLen = DiagStr-Modifier; 710 711 // If we have an argument, get it next. 712 if (DiagStr[0] == '{') { 713 ++DiagStr; // Skip {. 714 Argument = DiagStr; 715 716 DiagStr = ScanFormat(DiagStr, DiagEnd, '}'); 717 assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!"); 718 ArgumentLen = DiagStr-Argument; 719 ++DiagStr; // Skip }. 720 } 721 } 722 723 assert(isDigit(*DiagStr) && "Invalid format for argument in diagnostic"); 724 unsigned ArgNo = *DiagStr++ - '0'; 725 726 // Only used for type diffing. 727 unsigned ArgNo2 = ArgNo; 728 729 DiagnosticsEngine::ArgumentKind Kind = getArgKind(ArgNo); 730 if (ModifierIs(Modifier, ModifierLen, "diff")) { 731 assert(*DiagStr == ',' && isDigit(*(DiagStr + 1)) && 732 "Invalid format for diff modifier"); 733 ++DiagStr; // Comma. 734 ArgNo2 = *DiagStr++ - '0'; 735 DiagnosticsEngine::ArgumentKind Kind2 = getArgKind(ArgNo2); 736 if (Kind == DiagnosticsEngine::ak_qualtype && 737 Kind2 == DiagnosticsEngine::ak_qualtype) 738 Kind = DiagnosticsEngine::ak_qualtype_pair; 739 else { 740 // %diff only supports QualTypes. For other kinds of arguments, 741 // use the default printing. For example, if the modifier is: 742 // "%diff{compare $ to $|other text}1,2" 743 // treat it as: 744 // "compare %1 to %2" 745 const char *Pipe = ScanFormat(Argument, Argument + ArgumentLen, '|'); 746 const char *FirstDollar = ScanFormat(Argument, Pipe, '$'); 747 const char *SecondDollar = ScanFormat(FirstDollar + 1, Pipe, '$'); 748 const char ArgStr1[] = { '%', static_cast<char>('0' + ArgNo) }; 749 const char ArgStr2[] = { '%', static_cast<char>('0' + ArgNo2) }; 750 FormatDiagnostic(Argument, FirstDollar, OutStr); 751 FormatDiagnostic(ArgStr1, ArgStr1 + 2, OutStr); 752 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr); 753 FormatDiagnostic(ArgStr2, ArgStr2 + 2, OutStr); 754 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr); 755 continue; 756 } 757 } 758 759 switch (Kind) { 760 // ---- STRINGS ---- 761 case DiagnosticsEngine::ak_std_string: { 762 const std::string &S = getArgStdStr(ArgNo); 763 assert(ModifierLen == 0 && "No modifiers for strings yet"); 764 OutStr.append(S.begin(), S.end()); 765 break; 766 } 767 case DiagnosticsEngine::ak_c_string: { 768 const char *S = getArgCStr(ArgNo); 769 assert(ModifierLen == 0 && "No modifiers for strings yet"); 770 771 // Don't crash if get passed a null pointer by accident. 772 if (!S) 773 S = "(null)"; 774 775 OutStr.append(S, S + strlen(S)); 776 break; 777 } 778 // ---- INTEGERS ---- 779 case DiagnosticsEngine::ak_sint: { 780 int Val = getArgSInt(ArgNo); 781 782 if (ModifierIs(Modifier, ModifierLen, "select")) { 783 HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen, 784 OutStr); 785 } else if (ModifierIs(Modifier, ModifierLen, "s")) { 786 HandleIntegerSModifier(Val, OutStr); 787 } else if (ModifierIs(Modifier, ModifierLen, "plural")) { 788 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen, 789 OutStr); 790 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) { 791 HandleOrdinalModifier((unsigned)Val, OutStr); 792 } else { 793 assert(ModifierLen == 0 && "Unknown integer modifier"); 794 llvm::raw_svector_ostream(OutStr) << Val; 795 } 796 break; 797 } 798 case DiagnosticsEngine::ak_uint: { 799 unsigned Val = getArgUInt(ArgNo); 800 801 if (ModifierIs(Modifier, ModifierLen, "select")) { 802 HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr); 803 } else if (ModifierIs(Modifier, ModifierLen, "s")) { 804 HandleIntegerSModifier(Val, OutStr); 805 } else if (ModifierIs(Modifier, ModifierLen, "plural")) { 806 HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen, 807 OutStr); 808 } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) { 809 HandleOrdinalModifier(Val, OutStr); 810 } else { 811 assert(ModifierLen == 0 && "Unknown integer modifier"); 812 llvm::raw_svector_ostream(OutStr) << Val; 813 } 814 break; 815 } 816 // ---- TOKEN SPELLINGS ---- 817 case DiagnosticsEngine::ak_tokenkind: { 818 tok::TokenKind Kind = static_cast<tok::TokenKind>(getRawArg(ArgNo)); 819 assert(ModifierLen == 0 && "No modifiers for token kinds yet"); 820 821 llvm::raw_svector_ostream Out(OutStr); 822 if (const char *S = tok::getPunctuatorSpelling(Kind)) 823 // Quoted token spelling for punctuators. 824 Out << '\'' << S << '\''; 825 else if (const char *S = tok::getKeywordSpelling(Kind)) 826 // Unquoted token spelling for keywords. 827 Out << S; 828 else if (const char *S = getTokenDescForDiagnostic(Kind)) 829 // Unquoted translatable token name. 830 Out << S; 831 else if (const char *S = tok::getTokenName(Kind)) 832 // Debug name, shouldn't appear in user-facing diagnostics. 833 Out << '<' << S << '>'; 834 else 835 Out << "(null)"; 836 break; 837 } 838 // ---- NAMES and TYPES ---- 839 case DiagnosticsEngine::ak_identifierinfo: { 840 const IdentifierInfo *II = getArgIdentifier(ArgNo); 841 assert(ModifierLen == 0 && "No modifiers for strings yet"); 842 843 // Don't crash if get passed a null pointer by accident. 844 if (!II) { 845 const char *S = "(null)"; 846 OutStr.append(S, S + strlen(S)); 847 continue; 848 } 849 850 llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\''; 851 break; 852 } 853 case DiagnosticsEngine::ak_qualtype: 854 case DiagnosticsEngine::ak_declarationname: 855 case DiagnosticsEngine::ak_nameddecl: 856 case DiagnosticsEngine::ak_nestednamespec: 857 case DiagnosticsEngine::ak_declcontext: 858 case DiagnosticsEngine::ak_attr: 859 getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo), 860 StringRef(Modifier, ModifierLen), 861 StringRef(Argument, ArgumentLen), 862 FormattedArgs, 863 OutStr, QualTypeVals); 864 break; 865 case DiagnosticsEngine::ak_qualtype_pair: 866 // Create a struct with all the info needed for printing. 867 TemplateDiffTypes TDT; 868 TDT.FromType = getRawArg(ArgNo); 869 TDT.ToType = getRawArg(ArgNo2); 870 TDT.ElideType = getDiags()->ElideType; 871 TDT.ShowColors = getDiags()->ShowColors; 872 TDT.TemplateDiffUsed = false; 873 intptr_t val = reinterpret_cast<intptr_t>(&TDT); 874 875 const char *ArgumentEnd = Argument + ArgumentLen; 876 const char *Pipe = ScanFormat(Argument, ArgumentEnd, '|'); 877 878 // Print the tree. If this diagnostic already has a tree, skip the 879 // second tree. 880 if (getDiags()->PrintTemplateTree && Tree.empty()) { 881 TDT.PrintFromType = true; 882 TDT.PrintTree = true; 883 getDiags()->ConvertArgToString(Kind, val, 884 StringRef(Modifier, ModifierLen), 885 StringRef(Argument, ArgumentLen), 886 FormattedArgs, 887 Tree, QualTypeVals); 888 // If there is no tree information, fall back to regular printing. 889 if (!Tree.empty()) { 890 FormatDiagnostic(Pipe + 1, ArgumentEnd, OutStr); 891 break; 892 } 893 } 894 895 // Non-tree printing, also the fall-back when tree printing fails. 896 // The fall-back is triggered when the types compared are not templates. 897 const char *FirstDollar = ScanFormat(Argument, ArgumentEnd, '$'); 898 const char *SecondDollar = ScanFormat(FirstDollar + 1, ArgumentEnd, '$'); 899 900 // Append before text 901 FormatDiagnostic(Argument, FirstDollar, OutStr); 902 903 // Append first type 904 TDT.PrintTree = false; 905 TDT.PrintFromType = true; 906 getDiags()->ConvertArgToString(Kind, val, 907 StringRef(Modifier, ModifierLen), 908 StringRef(Argument, ArgumentLen), 909 FormattedArgs, 910 OutStr, QualTypeVals); 911 if (!TDT.TemplateDiffUsed) 912 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype, 913 TDT.FromType)); 914 915 // Append middle text 916 FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr); 917 918 // Append second type 919 TDT.PrintFromType = false; 920 getDiags()->ConvertArgToString(Kind, val, 921 StringRef(Modifier, ModifierLen), 922 StringRef(Argument, ArgumentLen), 923 FormattedArgs, 924 OutStr, QualTypeVals); 925 if (!TDT.TemplateDiffUsed) 926 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype, 927 TDT.ToType)); 928 929 // Append end text 930 FormatDiagnostic(SecondDollar + 1, Pipe, OutStr); 931 break; 932 } 933 934 // Remember this argument info for subsequent formatting operations. Turn 935 // std::strings into a null terminated string to make it be the same case as 936 // all the other ones. 937 if (Kind == DiagnosticsEngine::ak_qualtype_pair) 938 continue; 939 else if (Kind != DiagnosticsEngine::ak_std_string) 940 FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo))); 941 else 942 FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_c_string, 943 (intptr_t)getArgStdStr(ArgNo).c_str())); 944 945 } 946 947 // Append the type tree to the end of the diagnostics. 948 OutStr.append(Tree.begin(), Tree.end()); 949 } 950 951 StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID, 952 StringRef Message) 953 : ID(ID), Level(Level), Loc(), Message(Message) { } 954 955 StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, 956 const Diagnostic &Info) 957 : ID(Info.getID()), Level(Level) 958 { 959 assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) && 960 "Valid source location without setting a source manager for diagnostic"); 961 if (Info.getLocation().isValid()) 962 Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager()); 963 SmallString<64> Message; 964 Info.FormatDiagnostic(Message); 965 this->Message.assign(Message.begin(), Message.end()); 966 this->Ranges.assign(Info.getRanges().begin(), Info.getRanges().end()); 967 this->FixIts.assign(Info.getFixItHints().begin(), Info.getFixItHints().end()); 968 } 969 970 StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID, 971 StringRef Message, FullSourceLoc Loc, 972 ArrayRef<CharSourceRange> Ranges, 973 ArrayRef<FixItHint> FixIts) 974 : ID(ID), Level(Level), Loc(Loc), Message(Message), 975 Ranges(Ranges.begin(), Ranges.end()), FixIts(FixIts.begin(), FixIts.end()) 976 { 977 } 978 979 /// IncludeInDiagnosticCounts - This method (whose default implementation 980 /// returns true) indicates whether the diagnostics handled by this 981 /// DiagnosticConsumer should be included in the number of diagnostics 982 /// reported by DiagnosticsEngine. 983 bool DiagnosticConsumer::IncludeInDiagnosticCounts() const { return true; } 984 985 void IgnoringDiagConsumer::anchor() { } 986 987 ForwardingDiagnosticConsumer::~ForwardingDiagnosticConsumer() {} 988 989 void ForwardingDiagnosticConsumer::HandleDiagnostic( 990 DiagnosticsEngine::Level DiagLevel, 991 const Diagnostic &Info) { 992 Target.HandleDiagnostic(DiagLevel, Info); 993 } 994 995 void ForwardingDiagnosticConsumer::clear() { 996 DiagnosticConsumer::clear(); 997 Target.clear(); 998 } 999 1000 bool ForwardingDiagnosticConsumer::IncludeInDiagnosticCounts() const { 1001 return Target.IncludeInDiagnosticCounts(); 1002 } 1003 1004 PartialDiagnostic::StorageAllocator::StorageAllocator() { 1005 for (unsigned I = 0; I != NumCached; ++I) 1006 FreeList[I] = Cached + I; 1007 NumFreeListEntries = NumCached; 1008 } 1009 1010 PartialDiagnostic::StorageAllocator::~StorageAllocator() { 1011 // Don't assert if we are in a CrashRecovery context, as this invariant may 1012 // be invalidated during a crash. 1013 assert((NumFreeListEntries == NumCached || 1014 llvm::CrashRecoveryContext::isRecoveringFromCrash()) && 1015 "A partial is on the lam"); 1016 } 1017