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