1 //===--- SemaExceptionSpec.cpp - C++ Exception Specifications ---*- C++ -*-===// 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 provides Sema routines for C++ exception specification testing. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/Sema/SemaInternal.h" 15 #include "clang/AST/ASTMutationListener.h" 16 #include "clang/AST/CXXInheritance.h" 17 #include "clang/AST/Expr.h" 18 #include "clang/AST/ExprCXX.h" 19 #include "clang/AST/TypeLoc.h" 20 #include "clang/Basic/Diagnostic.h" 21 #include "clang/Basic/SourceManager.h" 22 #include "llvm/ADT/SmallPtrSet.h" 23 #include "llvm/ADT/SmallString.h" 24 25 namespace clang { 26 27 static const FunctionProtoType *GetUnderlyingFunction(QualType T) 28 { 29 if (const PointerType *PtrTy = T->getAs<PointerType>()) 30 T = PtrTy->getPointeeType(); 31 else if (const ReferenceType *RefTy = T->getAs<ReferenceType>()) 32 T = RefTy->getPointeeType(); 33 else if (const MemberPointerType *MPTy = T->getAs<MemberPointerType>()) 34 T = MPTy->getPointeeType(); 35 return T->getAs<FunctionProtoType>(); 36 } 37 38 /// HACK: libstdc++ has a bug where it shadows std::swap with a member 39 /// swap function then tries to call std::swap unqualified from the exception 40 /// specification of that function. This function detects whether we're in 41 /// such a case and turns off delay-parsing of exception specifications. 42 bool Sema::isLibstdcxxEagerExceptionSpecHack(const Declarator &D) { 43 auto *RD = dyn_cast<CXXRecordDecl>(CurContext); 44 45 // All the problem cases are member functions named "swap" within class 46 // templates declared directly within namespace std or std::__debug or 47 // std::__profile. 48 if (!RD || !RD->getIdentifier() || !RD->getDescribedClassTemplate() || 49 !D.getIdentifier() || !D.getIdentifier()->isStr("swap")) 50 return false; 51 52 auto *ND = dyn_cast<NamespaceDecl>(RD->getDeclContext()); 53 if (!ND) 54 return false; 55 56 bool IsInStd = ND->isStdNamespace(); 57 if (!IsInStd) { 58 // This isn't a direct member of namespace std, but it might still be 59 // libstdc++'s std::__debug::array or std::__profile::array. 60 IdentifierInfo *II = ND->getIdentifier(); 61 if (!II || !(II->isStr("__debug") || II->isStr("__profile")) || 62 !ND->isInStdNamespace()) 63 return false; 64 } 65 66 // Only apply this hack within a system header. 67 if (!Context.getSourceManager().isInSystemHeader(D.getLocStart())) 68 return false; 69 70 return llvm::StringSwitch<bool>(RD->getIdentifier()->getName()) 71 .Case("array", true) 72 .Case("pair", IsInStd) 73 .Case("priority_queue", IsInStd) 74 .Case("stack", IsInStd) 75 .Case("queue", IsInStd) 76 .Default(false); 77 } 78 79 /// CheckSpecifiedExceptionType - Check if the given type is valid in an 80 /// exception specification. Incomplete types, or pointers to incomplete types 81 /// other than void are not allowed. 82 /// 83 /// \param[in,out] T The exception type. This will be decayed to a pointer type 84 /// when the input is an array or a function type. 85 bool Sema::CheckSpecifiedExceptionType(QualType &T, SourceRange Range) { 86 // C++11 [except.spec]p2: 87 // A type cv T, "array of T", or "function returning T" denoted 88 // in an exception-specification is adjusted to type T, "pointer to T", or 89 // "pointer to function returning T", respectively. 90 // 91 // We also apply this rule in C++98. 92 if (T->isArrayType()) 93 T = Context.getArrayDecayedType(T); 94 else if (T->isFunctionType()) 95 T = Context.getPointerType(T); 96 97 int Kind = 0; 98 QualType PointeeT = T; 99 if (const PointerType *PT = T->getAs<PointerType>()) { 100 PointeeT = PT->getPointeeType(); 101 Kind = 1; 102 103 // cv void* is explicitly permitted, despite being a pointer to an 104 // incomplete type. 105 if (PointeeT->isVoidType()) 106 return false; 107 } else if (const ReferenceType *RT = T->getAs<ReferenceType>()) { 108 PointeeT = RT->getPointeeType(); 109 Kind = 2; 110 111 if (RT->isRValueReferenceType()) { 112 // C++11 [except.spec]p2: 113 // A type denoted in an exception-specification shall not denote [...] 114 // an rvalue reference type. 115 Diag(Range.getBegin(), diag::err_rref_in_exception_spec) 116 << T << Range; 117 return true; 118 } 119 } 120 121 // C++11 [except.spec]p2: 122 // A type denoted in an exception-specification shall not denote an 123 // incomplete type other than a class currently being defined [...]. 124 // A type denoted in an exception-specification shall not denote a 125 // pointer or reference to an incomplete type, other than (cv) void* or a 126 // pointer or reference to a class currently being defined. 127 // In Microsoft mode, downgrade this to a warning. 128 unsigned DiagID = diag::err_incomplete_in_exception_spec; 129 bool ReturnValueOnError = true; 130 if (getLangOpts().MicrosoftExt) { 131 DiagID = diag::ext_incomplete_in_exception_spec; 132 ReturnValueOnError = false; 133 } 134 if (!(PointeeT->isRecordType() && 135 PointeeT->getAs<RecordType>()->isBeingDefined()) && 136 RequireCompleteType(Range.getBegin(), PointeeT, DiagID, Kind, Range)) 137 return ReturnValueOnError; 138 139 return false; 140 } 141 142 /// CheckDistantExceptionSpec - Check if the given type is a pointer or pointer 143 /// to member to a function with an exception specification. This means that 144 /// it is invalid to add another level of indirection. 145 bool Sema::CheckDistantExceptionSpec(QualType T) { 146 // C++17 removes this rule in favor of putting exception specifications into 147 // the type system. 148 if (getLangOpts().CPlusPlus1z) 149 return false; 150 151 if (const PointerType *PT = T->getAs<PointerType>()) 152 T = PT->getPointeeType(); 153 else if (const MemberPointerType *PT = T->getAs<MemberPointerType>()) 154 T = PT->getPointeeType(); 155 else 156 return false; 157 158 const FunctionProtoType *FnT = T->getAs<FunctionProtoType>(); 159 if (!FnT) 160 return false; 161 162 return FnT->hasExceptionSpec(); 163 } 164 165 const FunctionProtoType * 166 Sema::ResolveExceptionSpec(SourceLocation Loc, const FunctionProtoType *FPT) { 167 if (FPT->getExceptionSpecType() == EST_Unparsed) { 168 Diag(Loc, diag::err_exception_spec_not_parsed); 169 return nullptr; 170 } 171 172 if (!isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) 173 return FPT; 174 175 FunctionDecl *SourceDecl = FPT->getExceptionSpecDecl(); 176 const FunctionProtoType *SourceFPT = 177 SourceDecl->getType()->castAs<FunctionProtoType>(); 178 179 // If the exception specification has already been resolved, just return it. 180 if (!isUnresolvedExceptionSpec(SourceFPT->getExceptionSpecType())) 181 return SourceFPT; 182 183 // Compute or instantiate the exception specification now. 184 if (SourceFPT->getExceptionSpecType() == EST_Unevaluated) 185 EvaluateImplicitExceptionSpec(Loc, cast<CXXMethodDecl>(SourceDecl)); 186 else 187 InstantiateExceptionSpec(Loc, SourceDecl); 188 189 const FunctionProtoType *Proto = 190 SourceDecl->getType()->castAs<FunctionProtoType>(); 191 if (Proto->getExceptionSpecType() == clang::EST_Unparsed) { 192 Diag(Loc, diag::err_exception_spec_not_parsed); 193 Proto = nullptr; 194 } 195 return Proto; 196 } 197 198 void 199 Sema::UpdateExceptionSpec(FunctionDecl *FD, 200 const FunctionProtoType::ExceptionSpecInfo &ESI) { 201 // If we've fully resolved the exception specification, notify listeners. 202 if (!isUnresolvedExceptionSpec(ESI.Type)) 203 if (auto *Listener = getASTMutationListener()) 204 Listener->ResolvedExceptionSpec(FD); 205 206 for (auto *Redecl : FD->redecls()) 207 Context.adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI); 208 } 209 210 /// Determine whether a function has an implicitly-generated exception 211 /// specification. 212 static bool hasImplicitExceptionSpec(FunctionDecl *Decl) { 213 if (!isa<CXXDestructorDecl>(Decl) && 214 Decl->getDeclName().getCXXOverloadedOperator() != OO_Delete && 215 Decl->getDeclName().getCXXOverloadedOperator() != OO_Array_Delete) 216 return false; 217 218 // For a function that the user didn't declare: 219 // - if this is a destructor, its exception specification is implicit. 220 // - if this is 'operator delete' or 'operator delete[]', the exception 221 // specification is as-if an explicit exception specification was given 222 // (per [basic.stc.dynamic]p2). 223 if (!Decl->getTypeSourceInfo()) 224 return isa<CXXDestructorDecl>(Decl); 225 226 const FunctionProtoType *Ty = 227 Decl->getTypeSourceInfo()->getType()->getAs<FunctionProtoType>(); 228 return !Ty->hasExceptionSpec(); 229 } 230 231 bool Sema::CheckEquivalentExceptionSpec(FunctionDecl *Old, FunctionDecl *New) { 232 OverloadedOperatorKind OO = New->getDeclName().getCXXOverloadedOperator(); 233 bool IsOperatorNew = OO == OO_New || OO == OO_Array_New; 234 bool MissingExceptionSpecification = false; 235 bool MissingEmptyExceptionSpecification = false; 236 237 unsigned DiagID = diag::err_mismatched_exception_spec; 238 bool ReturnValueOnError = true; 239 if (getLangOpts().MicrosoftExt) { 240 DiagID = diag::ext_mismatched_exception_spec; 241 ReturnValueOnError = false; 242 } 243 244 // Check the types as written: they must match before any exception 245 // specification adjustment is applied. 246 if (!CheckEquivalentExceptionSpec( 247 PDiag(DiagID), PDiag(diag::note_previous_declaration), 248 Old->getType()->getAs<FunctionProtoType>(), Old->getLocation(), 249 New->getType()->getAs<FunctionProtoType>(), New->getLocation(), 250 &MissingExceptionSpecification, &MissingEmptyExceptionSpecification, 251 /*AllowNoexceptAllMatchWithNoSpec=*/true, IsOperatorNew)) { 252 // C++11 [except.spec]p4 [DR1492]: 253 // If a declaration of a function has an implicit 254 // exception-specification, other declarations of the function shall 255 // not specify an exception-specification. 256 if (getLangOpts().CPlusPlus11 && getLangOpts().CXXExceptions && 257 hasImplicitExceptionSpec(Old) != hasImplicitExceptionSpec(New)) { 258 Diag(New->getLocation(), diag::ext_implicit_exception_spec_mismatch) 259 << hasImplicitExceptionSpec(Old); 260 if (Old->getLocation().isValid()) 261 Diag(Old->getLocation(), diag::note_previous_declaration); 262 } 263 return false; 264 } 265 266 // The failure was something other than an missing exception 267 // specification; return an error, except in MS mode where this is a warning. 268 if (!MissingExceptionSpecification) 269 return ReturnValueOnError; 270 271 const FunctionProtoType *NewProto = 272 New->getType()->castAs<FunctionProtoType>(); 273 274 // The new function declaration is only missing an empty exception 275 // specification "throw()". If the throw() specification came from a 276 // function in a system header that has C linkage, just add an empty 277 // exception specification to the "new" declaration. This is an 278 // egregious workaround for glibc, which adds throw() specifications 279 // to many libc functions as an optimization. Unfortunately, that 280 // optimization isn't permitted by the C++ standard, so we're forced 281 // to work around it here. 282 if (MissingEmptyExceptionSpecification && NewProto && 283 (Old->getLocation().isInvalid() || 284 Context.getSourceManager().isInSystemHeader(Old->getLocation())) && 285 Old->isExternC()) { 286 New->setType(Context.getFunctionType( 287 NewProto->getReturnType(), NewProto->getParamTypes(), 288 NewProto->getExtProtoInfo().withExceptionSpec(EST_DynamicNone))); 289 return false; 290 } 291 292 const FunctionProtoType *OldProto = 293 Old->getType()->castAs<FunctionProtoType>(); 294 295 FunctionProtoType::ExceptionSpecInfo ESI = OldProto->getExceptionSpecType(); 296 if (ESI.Type == EST_Dynamic) { 297 ESI.Exceptions = OldProto->exceptions(); 298 } 299 300 if (ESI.Type == EST_ComputedNoexcept) { 301 // For computed noexcept, we can't just take the expression from the old 302 // prototype. It likely contains references to the old prototype's 303 // parameters. 304 New->setInvalidDecl(); 305 } else { 306 // Update the type of the function with the appropriate exception 307 // specification. 308 New->setType(Context.getFunctionType( 309 NewProto->getReturnType(), NewProto->getParamTypes(), 310 NewProto->getExtProtoInfo().withExceptionSpec(ESI))); 311 } 312 313 if (getLangOpts().MicrosoftExt && ESI.Type != EST_ComputedNoexcept) { 314 // Allow missing exception specifications in redeclarations as an extension. 315 DiagID = diag::ext_ms_missing_exception_specification; 316 ReturnValueOnError = false; 317 } else if (New->isReplaceableGlobalAllocationFunction() && 318 ESI.Type != EST_ComputedNoexcept) { 319 // Allow missing exception specifications in redeclarations as an extension, 320 // when declaring a replaceable global allocation function. 321 DiagID = diag::ext_missing_exception_specification; 322 ReturnValueOnError = false; 323 } else { 324 DiagID = diag::err_missing_exception_specification; 325 ReturnValueOnError = true; 326 } 327 328 // Warn about the lack of exception specification. 329 SmallString<128> ExceptionSpecString; 330 llvm::raw_svector_ostream OS(ExceptionSpecString); 331 switch (OldProto->getExceptionSpecType()) { 332 case EST_DynamicNone: 333 OS << "throw()"; 334 break; 335 336 case EST_Dynamic: { 337 OS << "throw("; 338 bool OnFirstException = true; 339 for (const auto &E : OldProto->exceptions()) { 340 if (OnFirstException) 341 OnFirstException = false; 342 else 343 OS << ", "; 344 345 OS << E.getAsString(getPrintingPolicy()); 346 } 347 OS << ")"; 348 break; 349 } 350 351 case EST_BasicNoexcept: 352 OS << "noexcept"; 353 break; 354 355 case EST_ComputedNoexcept: 356 OS << "noexcept("; 357 assert(OldProto->getNoexceptExpr() != nullptr && "Expected non-null Expr"); 358 OldProto->getNoexceptExpr()->printPretty(OS, nullptr, getPrintingPolicy()); 359 OS << ")"; 360 break; 361 362 default: 363 llvm_unreachable("This spec type is compatible with none."); 364 } 365 366 SourceLocation FixItLoc; 367 if (TypeSourceInfo *TSInfo = New->getTypeSourceInfo()) { 368 TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens(); 369 // FIXME: Preserve enough information so that we can produce a correct fixit 370 // location when there is a trailing return type. 371 if (auto FTLoc = TL.getAs<FunctionProtoTypeLoc>()) 372 if (!FTLoc.getTypePtr()->hasTrailingReturn()) 373 FixItLoc = getLocForEndOfToken(FTLoc.getLocalRangeEnd()); 374 } 375 376 if (FixItLoc.isInvalid()) 377 Diag(New->getLocation(), DiagID) 378 << New << OS.str(); 379 else { 380 Diag(New->getLocation(), DiagID) 381 << New << OS.str() 382 << FixItHint::CreateInsertion(FixItLoc, " " + OS.str().str()); 383 } 384 385 if (Old->getLocation().isValid()) 386 Diag(Old->getLocation(), diag::note_previous_declaration); 387 388 return ReturnValueOnError; 389 } 390 391 /// CheckEquivalentExceptionSpec - Check if the two types have equivalent 392 /// exception specifications. Exception specifications are equivalent if 393 /// they allow exactly the same set of exception types. It does not matter how 394 /// that is achieved. See C++ [except.spec]p2. 395 bool Sema::CheckEquivalentExceptionSpec( 396 const FunctionProtoType *Old, SourceLocation OldLoc, 397 const FunctionProtoType *New, SourceLocation NewLoc) { 398 unsigned DiagID = diag::err_mismatched_exception_spec; 399 if (getLangOpts().MicrosoftExt) 400 DiagID = diag::ext_mismatched_exception_spec; 401 bool Result = CheckEquivalentExceptionSpec(PDiag(DiagID), 402 PDiag(diag::note_previous_declaration), Old, OldLoc, New, NewLoc); 403 404 // In Microsoft mode, mismatching exception specifications just cause a warning. 405 if (getLangOpts().MicrosoftExt) 406 return false; 407 return Result; 408 } 409 410 /// CheckEquivalentExceptionSpec - Check if the two types have compatible 411 /// exception specifications. See C++ [except.spec]p3. 412 /// 413 /// \return \c false if the exception specifications match, \c true if there is 414 /// a problem. If \c true is returned, either a diagnostic has already been 415 /// produced or \c *MissingExceptionSpecification is set to \c true. 416 bool Sema::CheckEquivalentExceptionSpec(const PartialDiagnostic &DiagID, 417 const PartialDiagnostic & NoteID, 418 const FunctionProtoType *Old, 419 SourceLocation OldLoc, 420 const FunctionProtoType *New, 421 SourceLocation NewLoc, 422 bool *MissingExceptionSpecification, 423 bool*MissingEmptyExceptionSpecification, 424 bool AllowNoexceptAllMatchWithNoSpec, 425 bool IsOperatorNew) { 426 // Just completely ignore this under -fno-exceptions. 427 if (!getLangOpts().CXXExceptions) 428 return false; 429 430 if (MissingExceptionSpecification) 431 *MissingExceptionSpecification = false; 432 433 if (MissingEmptyExceptionSpecification) 434 *MissingEmptyExceptionSpecification = false; 435 436 Old = ResolveExceptionSpec(NewLoc, Old); 437 if (!Old) 438 return false; 439 New = ResolveExceptionSpec(NewLoc, New); 440 if (!New) 441 return false; 442 443 // C++0x [except.spec]p3: Two exception-specifications are compatible if: 444 // - both are non-throwing, regardless of their form, 445 // - both have the form noexcept(constant-expression) and the constant- 446 // expressions are equivalent, 447 // - both are dynamic-exception-specifications that have the same set of 448 // adjusted types. 449 // 450 // C++0x [except.spec]p12: An exception-specification is non-throwing if it is 451 // of the form throw(), noexcept, or noexcept(constant-expression) where the 452 // constant-expression yields true. 453 // 454 // C++0x [except.spec]p4: If any declaration of a function has an exception- 455 // specifier that is not a noexcept-specification allowing all exceptions, 456 // all declarations [...] of that function shall have a compatible 457 // exception-specification. 458 // 459 // That last point basically means that noexcept(false) matches no spec. 460 // It's considered when AllowNoexceptAllMatchWithNoSpec is true. 461 462 ExceptionSpecificationType OldEST = Old->getExceptionSpecType(); 463 ExceptionSpecificationType NewEST = New->getExceptionSpecType(); 464 465 assert(!isUnresolvedExceptionSpec(OldEST) && 466 !isUnresolvedExceptionSpec(NewEST) && 467 "Shouldn't see unknown exception specifications here"); 468 469 // Shortcut the case where both have no spec. 470 if (OldEST == EST_None && NewEST == EST_None) 471 return false; 472 473 FunctionProtoType::NoexceptResult OldNR = Old->getNoexceptSpec(Context); 474 FunctionProtoType::NoexceptResult NewNR = New->getNoexceptSpec(Context); 475 if (OldNR == FunctionProtoType::NR_BadNoexcept || 476 NewNR == FunctionProtoType::NR_BadNoexcept) 477 return false; 478 479 // Dependent noexcept specifiers are compatible with each other, but nothing 480 // else. 481 // One noexcept is compatible with another if the argument is the same 482 if (OldNR == NewNR && 483 OldNR != FunctionProtoType::NR_NoNoexcept && 484 NewNR != FunctionProtoType::NR_NoNoexcept) 485 return false; 486 if (OldNR != NewNR && 487 OldNR != FunctionProtoType::NR_NoNoexcept && 488 NewNR != FunctionProtoType::NR_NoNoexcept) { 489 Diag(NewLoc, DiagID); 490 if (NoteID.getDiagID() != 0 && OldLoc.isValid()) 491 Diag(OldLoc, NoteID); 492 return true; 493 } 494 495 // The MS extension throw(...) is compatible with itself. 496 if (OldEST == EST_MSAny && NewEST == EST_MSAny) 497 return false; 498 499 // It's also compatible with no spec. 500 if ((OldEST == EST_None && NewEST == EST_MSAny) || 501 (OldEST == EST_MSAny && NewEST == EST_None)) 502 return false; 503 504 // It's also compatible with noexcept(false). 505 if (OldEST == EST_MSAny && NewNR == FunctionProtoType::NR_Throw) 506 return false; 507 if (NewEST == EST_MSAny && OldNR == FunctionProtoType::NR_Throw) 508 return false; 509 510 // As described above, noexcept(false) matches no spec only for functions. 511 if (AllowNoexceptAllMatchWithNoSpec) { 512 if (OldEST == EST_None && NewNR == FunctionProtoType::NR_Throw) 513 return false; 514 if (NewEST == EST_None && OldNR == FunctionProtoType::NR_Throw) 515 return false; 516 } 517 518 // Any non-throwing specifications are compatible. 519 bool OldNonThrowing = OldNR == FunctionProtoType::NR_Nothrow || 520 OldEST == EST_DynamicNone; 521 bool NewNonThrowing = NewNR == FunctionProtoType::NR_Nothrow || 522 NewEST == EST_DynamicNone; 523 if (OldNonThrowing && NewNonThrowing) 524 return false; 525 526 // As a special compatibility feature, under C++0x we accept no spec and 527 // throw(std::bad_alloc) as equivalent for operator new and operator new[]. 528 // This is because the implicit declaration changed, but old code would break. 529 if (getLangOpts().CPlusPlus11 && IsOperatorNew) { 530 const FunctionProtoType *WithExceptions = nullptr; 531 if (OldEST == EST_None && NewEST == EST_Dynamic) 532 WithExceptions = New; 533 else if (OldEST == EST_Dynamic && NewEST == EST_None) 534 WithExceptions = Old; 535 if (WithExceptions && WithExceptions->getNumExceptions() == 1) { 536 // One has no spec, the other throw(something). If that something is 537 // std::bad_alloc, all conditions are met. 538 QualType Exception = *WithExceptions->exception_begin(); 539 if (CXXRecordDecl *ExRecord = Exception->getAsCXXRecordDecl()) { 540 IdentifierInfo* Name = ExRecord->getIdentifier(); 541 if (Name && Name->getName() == "bad_alloc") { 542 // It's called bad_alloc, but is it in std? 543 if (ExRecord->isInStdNamespace()) { 544 return false; 545 } 546 } 547 } 548 } 549 } 550 551 // At this point, the only remaining valid case is two matching dynamic 552 // specifications. We return here unless both specifications are dynamic. 553 if (OldEST != EST_Dynamic || NewEST != EST_Dynamic) { 554 if (MissingExceptionSpecification && Old->hasExceptionSpec() && 555 !New->hasExceptionSpec()) { 556 // The old type has an exception specification of some sort, but 557 // the new type does not. 558 *MissingExceptionSpecification = true; 559 560 if (MissingEmptyExceptionSpecification && OldNonThrowing) { 561 // The old type has a throw() or noexcept(true) exception specification 562 // and the new type has no exception specification, and the caller asked 563 // to handle this itself. 564 *MissingEmptyExceptionSpecification = true; 565 } 566 567 return true; 568 } 569 570 Diag(NewLoc, DiagID); 571 if (NoteID.getDiagID() != 0 && OldLoc.isValid()) 572 Diag(OldLoc, NoteID); 573 return true; 574 } 575 576 assert(OldEST == EST_Dynamic && NewEST == EST_Dynamic && 577 "Exception compatibility logic error: non-dynamic spec slipped through."); 578 579 bool Success = true; 580 // Both have a dynamic exception spec. Collect the first set, then compare 581 // to the second. 582 llvm::SmallPtrSet<CanQualType, 8> OldTypes, NewTypes; 583 for (const auto &I : Old->exceptions()) 584 OldTypes.insert(Context.getCanonicalType(I).getUnqualifiedType()); 585 586 for (const auto &I : New->exceptions()) { 587 CanQualType TypePtr = Context.getCanonicalType(I).getUnqualifiedType(); 588 if(OldTypes.count(TypePtr)) 589 NewTypes.insert(TypePtr); 590 else 591 Success = false; 592 } 593 594 Success = Success && OldTypes.size() == NewTypes.size(); 595 596 if (Success) { 597 return false; 598 } 599 Diag(NewLoc, DiagID); 600 if (NoteID.getDiagID() != 0 && OldLoc.isValid()) 601 Diag(OldLoc, NoteID); 602 return true; 603 } 604 605 /// CheckExceptionSpecSubset - Check whether the second function type's 606 /// exception specification is a subset (or equivalent) of the first function 607 /// type. This is used by override and pointer assignment checks. 608 bool Sema::CheckExceptionSpecSubset( 609 const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID, 610 const FunctionProtoType *Superset, SourceLocation SuperLoc, 611 const FunctionProtoType *Subset, SourceLocation SubLoc) { 612 613 // Just auto-succeed under -fno-exceptions. 614 if (!getLangOpts().CXXExceptions) 615 return false; 616 617 // FIXME: As usual, we could be more specific in our error messages, but 618 // that better waits until we've got types with source locations. 619 620 if (!SubLoc.isValid()) 621 SubLoc = SuperLoc; 622 623 // Resolve the exception specifications, if needed. 624 Superset = ResolveExceptionSpec(SuperLoc, Superset); 625 if (!Superset) 626 return false; 627 Subset = ResolveExceptionSpec(SubLoc, Subset); 628 if (!Subset) 629 return false; 630 631 ExceptionSpecificationType SuperEST = Superset->getExceptionSpecType(); 632 633 // If superset contains everything, we're done. 634 if (SuperEST == EST_None || SuperEST == EST_MSAny) 635 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc); 636 637 // If there are dependent noexcept specs, assume everything is fine. Unlike 638 // with the equivalency check, this is safe in this case, because we don't 639 // want to merge declarations. Checks after instantiation will catch any 640 // omissions we make here. 641 // We also shortcut checking if a noexcept expression was bad. 642 643 FunctionProtoType::NoexceptResult SuperNR =Superset->getNoexceptSpec(Context); 644 if (SuperNR == FunctionProtoType::NR_BadNoexcept || 645 SuperNR == FunctionProtoType::NR_Dependent) 646 return false; 647 648 // Another case of the superset containing everything. 649 if (SuperNR == FunctionProtoType::NR_Throw) 650 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc); 651 652 ExceptionSpecificationType SubEST = Subset->getExceptionSpecType(); 653 654 assert(!isUnresolvedExceptionSpec(SuperEST) && 655 !isUnresolvedExceptionSpec(SubEST) && 656 "Shouldn't see unknown exception specifications here"); 657 658 // It does not. If the subset contains everything, we've failed. 659 if (SubEST == EST_None || SubEST == EST_MSAny) { 660 Diag(SubLoc, DiagID); 661 if (NoteID.getDiagID() != 0) 662 Diag(SuperLoc, NoteID); 663 return true; 664 } 665 666 FunctionProtoType::NoexceptResult SubNR = Subset->getNoexceptSpec(Context); 667 if (SubNR == FunctionProtoType::NR_BadNoexcept || 668 SubNR == FunctionProtoType::NR_Dependent) 669 return false; 670 671 // Another case of the subset containing everything. 672 if (SubNR == FunctionProtoType::NR_Throw) { 673 Diag(SubLoc, DiagID); 674 if (NoteID.getDiagID() != 0) 675 Diag(SuperLoc, NoteID); 676 return true; 677 } 678 679 // If the subset contains nothing, we're done. 680 if (SubEST == EST_DynamicNone || SubNR == FunctionProtoType::NR_Nothrow) 681 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc); 682 683 // Otherwise, if the superset contains nothing, we've failed. 684 if (SuperEST == EST_DynamicNone || SuperNR == FunctionProtoType::NR_Nothrow) { 685 Diag(SubLoc, DiagID); 686 if (NoteID.getDiagID() != 0) 687 Diag(SuperLoc, NoteID); 688 return true; 689 } 690 691 assert(SuperEST == EST_Dynamic && SubEST == EST_Dynamic && 692 "Exception spec subset: non-dynamic case slipped through."); 693 694 // Neither contains everything or nothing. Do a proper comparison. 695 for (const auto &SubI : Subset->exceptions()) { 696 // Take one type from the subset. 697 QualType CanonicalSubT = Context.getCanonicalType(SubI); 698 // Unwrap pointers and references so that we can do checks within a class 699 // hierarchy. Don't unwrap member pointers; they don't have hierarchy 700 // conversions on the pointee. 701 bool SubIsPointer = false; 702 if (const ReferenceType *RefTy = CanonicalSubT->getAs<ReferenceType>()) 703 CanonicalSubT = RefTy->getPointeeType(); 704 if (const PointerType *PtrTy = CanonicalSubT->getAs<PointerType>()) { 705 CanonicalSubT = PtrTy->getPointeeType(); 706 SubIsPointer = true; 707 } 708 bool SubIsClass = CanonicalSubT->isRecordType(); 709 CanonicalSubT = CanonicalSubT.getLocalUnqualifiedType(); 710 711 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 712 /*DetectVirtual=*/false); 713 714 bool Contained = false; 715 // Make sure it's in the superset. 716 for (const auto &SuperI : Superset->exceptions()) { 717 QualType CanonicalSuperT = Context.getCanonicalType(SuperI); 718 // SubT must be SuperT or derived from it, or pointer or reference to 719 // such types. 720 if (const ReferenceType *RefTy = CanonicalSuperT->getAs<ReferenceType>()) 721 CanonicalSuperT = RefTy->getPointeeType(); 722 if (SubIsPointer) { 723 if (const PointerType *PtrTy = CanonicalSuperT->getAs<PointerType>()) 724 CanonicalSuperT = PtrTy->getPointeeType(); 725 else { 726 continue; 727 } 728 } 729 CanonicalSuperT = CanonicalSuperT.getLocalUnqualifiedType(); 730 // If the types are the same, move on to the next type in the subset. 731 if (CanonicalSubT == CanonicalSuperT) { 732 Contained = true; 733 break; 734 } 735 736 // Otherwise we need to check the inheritance. 737 if (!SubIsClass || !CanonicalSuperT->isRecordType()) 738 continue; 739 740 Paths.clear(); 741 if (!IsDerivedFrom(SubLoc, CanonicalSubT, CanonicalSuperT, Paths)) 742 continue; 743 744 if (Paths.isAmbiguous(Context.getCanonicalType(CanonicalSuperT))) 745 continue; 746 747 // Do this check from a context without privileges. 748 switch (CheckBaseClassAccess(SourceLocation(), 749 CanonicalSuperT, CanonicalSubT, 750 Paths.front(), 751 /*Diagnostic*/ 0, 752 /*ForceCheck*/ true, 753 /*ForceUnprivileged*/ true)) { 754 case AR_accessible: break; 755 case AR_inaccessible: continue; 756 case AR_dependent: 757 llvm_unreachable("access check dependent for unprivileged context"); 758 case AR_delayed: 759 llvm_unreachable("access check delayed in non-declaration"); 760 } 761 762 Contained = true; 763 break; 764 } 765 if (!Contained) { 766 Diag(SubLoc, DiagID); 767 if (NoteID.getDiagID() != 0) 768 Diag(SuperLoc, NoteID); 769 return true; 770 } 771 } 772 // We've run half the gauntlet. 773 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc); 774 } 775 776 static bool CheckSpecForTypesEquivalent(Sema &S, 777 const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID, 778 QualType Target, SourceLocation TargetLoc, 779 QualType Source, SourceLocation SourceLoc) 780 { 781 const FunctionProtoType *TFunc = GetUnderlyingFunction(Target); 782 if (!TFunc) 783 return false; 784 const FunctionProtoType *SFunc = GetUnderlyingFunction(Source); 785 if (!SFunc) 786 return false; 787 788 return S.CheckEquivalentExceptionSpec(DiagID, NoteID, TFunc, TargetLoc, 789 SFunc, SourceLoc); 790 } 791 792 /// CheckParamExceptionSpec - Check if the parameter and return types of the 793 /// two functions have equivalent exception specs. This is part of the 794 /// assignment and override compatibility check. We do not check the parameters 795 /// of parameter function pointers recursively, as no sane programmer would 796 /// even be able to write such a function type. 797 bool Sema::CheckParamExceptionSpec(const PartialDiagnostic &NoteID, 798 const FunctionProtoType *Target, 799 SourceLocation TargetLoc, 800 const FunctionProtoType *Source, 801 SourceLocation SourceLoc) { 802 if (CheckSpecForTypesEquivalent( 803 *this, PDiag(diag::err_deep_exception_specs_differ) << 0, PDiag(), 804 Target->getReturnType(), TargetLoc, Source->getReturnType(), 805 SourceLoc)) 806 return true; 807 808 // We shouldn't even be testing this unless the arguments are otherwise 809 // compatible. 810 assert(Target->getNumParams() == Source->getNumParams() && 811 "Functions have different argument counts."); 812 for (unsigned i = 0, E = Target->getNumParams(); i != E; ++i) { 813 if (CheckSpecForTypesEquivalent( 814 *this, PDiag(diag::err_deep_exception_specs_differ) << 1, PDiag(), 815 Target->getParamType(i), TargetLoc, Source->getParamType(i), 816 SourceLoc)) 817 return true; 818 } 819 return false; 820 } 821 822 bool Sema::CheckExceptionSpecCompatibility(Expr *From, QualType ToType) { 823 // First we check for applicability. 824 // Target type must be a function, function pointer or function reference. 825 const FunctionProtoType *ToFunc = GetUnderlyingFunction(ToType); 826 if (!ToFunc || ToFunc->hasDependentExceptionSpec()) 827 return false; 828 829 // SourceType must be a function or function pointer. 830 const FunctionProtoType *FromFunc = GetUnderlyingFunction(From->getType()); 831 if (!FromFunc || FromFunc->hasDependentExceptionSpec()) 832 return false; 833 834 // Now we've got the correct types on both sides, check their compatibility. 835 // This means that the source of the conversion can only throw a subset of 836 // the exceptions of the target, and any exception specs on arguments or 837 // return types must be equivalent. 838 // 839 // FIXME: If there is a nested dependent exception specification, we should 840 // not be checking it here. This is fine: 841 // template<typename T> void f() { 842 // void (*p)(void (*) throw(T)); 843 // void (*q)(void (*) throw(int)) = p; 844 // } 845 // ... because it might be instantiated with T=int. 846 return CheckExceptionSpecSubset(PDiag(diag::err_incompatible_exception_specs), 847 PDiag(), ToFunc, 848 From->getSourceRange().getBegin(), 849 FromFunc, SourceLocation()); 850 } 851 852 bool Sema::CheckOverridingFunctionExceptionSpec(const CXXMethodDecl *New, 853 const CXXMethodDecl *Old) { 854 // If the new exception specification hasn't been parsed yet, skip the check. 855 // We'll get called again once it's been parsed. 856 if (New->getType()->castAs<FunctionProtoType>()->getExceptionSpecType() == 857 EST_Unparsed) 858 return false; 859 if (getLangOpts().CPlusPlus11 && isa<CXXDestructorDecl>(New)) { 860 // Don't check uninstantiated template destructors at all. We can only 861 // synthesize correct specs after the template is instantiated. 862 if (New->getParent()->isDependentType()) 863 return false; 864 if (New->getParent()->isBeingDefined()) { 865 // The destructor might be updated once the definition is finished. So 866 // remember it and check later. 867 DelayedExceptionSpecChecks.push_back(std::make_pair(New, Old)); 868 return false; 869 } 870 } 871 // If the old exception specification hasn't been parsed yet, remember that 872 // we need to perform this check when we get to the end of the outermost 873 // lexically-surrounding class. 874 if (Old->getType()->castAs<FunctionProtoType>()->getExceptionSpecType() == 875 EST_Unparsed) { 876 DelayedExceptionSpecChecks.push_back(std::make_pair(New, Old)); 877 return false; 878 } 879 unsigned DiagID = diag::err_override_exception_spec; 880 if (getLangOpts().MicrosoftExt) 881 DiagID = diag::ext_override_exception_spec; 882 return CheckExceptionSpecSubset(PDiag(DiagID), 883 PDiag(diag::note_overridden_virtual_function), 884 Old->getType()->getAs<FunctionProtoType>(), 885 Old->getLocation(), 886 New->getType()->getAs<FunctionProtoType>(), 887 New->getLocation()); 888 } 889 890 static CanThrowResult canSubExprsThrow(Sema &S, const Expr *E) { 891 CanThrowResult R = CT_Cannot; 892 for (const Stmt *SubStmt : E->children()) { 893 R = mergeCanThrow(R, S.canThrow(cast<Expr>(SubStmt))); 894 if (R == CT_Can) 895 break; 896 } 897 return R; 898 } 899 900 static CanThrowResult canCalleeThrow(Sema &S, const Expr *E, const Decl *D) { 901 assert(D && "Expected decl"); 902 903 // See if we can get a function type from the decl somehow. 904 const ValueDecl *VD = dyn_cast<ValueDecl>(D); 905 if (!VD) // If we have no clue what we're calling, assume the worst. 906 return CT_Can; 907 908 // As an extension, we assume that __attribute__((nothrow)) functions don't 909 // throw. 910 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>()) 911 return CT_Cannot; 912 913 QualType T = VD->getType(); 914 const FunctionProtoType *FT; 915 if ((FT = T->getAs<FunctionProtoType>())) { 916 } else if (const PointerType *PT = T->getAs<PointerType>()) 917 FT = PT->getPointeeType()->getAs<FunctionProtoType>(); 918 else if (const ReferenceType *RT = T->getAs<ReferenceType>()) 919 FT = RT->getPointeeType()->getAs<FunctionProtoType>(); 920 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>()) 921 FT = MT->getPointeeType()->getAs<FunctionProtoType>(); 922 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) 923 FT = BT->getPointeeType()->getAs<FunctionProtoType>(); 924 925 if (!FT) 926 return CT_Can; 927 928 FT = S.ResolveExceptionSpec(E->getLocStart(), FT); 929 if (!FT) 930 return CT_Can; 931 932 return FT->isNothrow(S.Context) ? CT_Cannot : CT_Can; 933 } 934 935 static CanThrowResult canDynamicCastThrow(const CXXDynamicCastExpr *DC) { 936 if (DC->isTypeDependent()) 937 return CT_Dependent; 938 939 if (!DC->getTypeAsWritten()->isReferenceType()) 940 return CT_Cannot; 941 942 if (DC->getSubExpr()->isTypeDependent()) 943 return CT_Dependent; 944 945 return DC->getCastKind() == clang::CK_Dynamic? CT_Can : CT_Cannot; 946 } 947 948 static CanThrowResult canTypeidThrow(Sema &S, const CXXTypeidExpr *DC) { 949 if (DC->isTypeOperand()) 950 return CT_Cannot; 951 952 Expr *Op = DC->getExprOperand(); 953 if (Op->isTypeDependent()) 954 return CT_Dependent; 955 956 const RecordType *RT = Op->getType()->getAs<RecordType>(); 957 if (!RT) 958 return CT_Cannot; 959 960 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic()) 961 return CT_Cannot; 962 963 if (Op->Classify(S.Context).isPRValue()) 964 return CT_Cannot; 965 966 return CT_Can; 967 } 968 969 CanThrowResult Sema::canThrow(const Expr *E) { 970 // C++ [expr.unary.noexcept]p3: 971 // [Can throw] if in a potentially-evaluated context the expression would 972 // contain: 973 switch (E->getStmtClass()) { 974 case Expr::CXXThrowExprClass: 975 // - a potentially evaluated throw-expression 976 return CT_Can; 977 978 case Expr::CXXDynamicCastExprClass: { 979 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v), 980 // where T is a reference type, that requires a run-time check 981 CanThrowResult CT = canDynamicCastThrow(cast<CXXDynamicCastExpr>(E)); 982 if (CT == CT_Can) 983 return CT; 984 return mergeCanThrow(CT, canSubExprsThrow(*this, E)); 985 } 986 987 case Expr::CXXTypeidExprClass: 988 // - a potentially evaluated typeid expression applied to a glvalue 989 // expression whose type is a polymorphic class type 990 return canTypeidThrow(*this, cast<CXXTypeidExpr>(E)); 991 992 // - a potentially evaluated call to a function, member function, function 993 // pointer, or member function pointer that does not have a non-throwing 994 // exception-specification 995 case Expr::CallExprClass: 996 case Expr::CXXMemberCallExprClass: 997 case Expr::CXXOperatorCallExprClass: 998 case Expr::UserDefinedLiteralClass: { 999 const CallExpr *CE = cast<CallExpr>(E); 1000 CanThrowResult CT; 1001 if (E->isTypeDependent()) 1002 CT = CT_Dependent; 1003 else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens())) 1004 CT = CT_Cannot; 1005 else if (CE->getCalleeDecl()) 1006 CT = canCalleeThrow(*this, E, CE->getCalleeDecl()); 1007 else 1008 CT = CT_Can; 1009 if (CT == CT_Can) 1010 return CT; 1011 return mergeCanThrow(CT, canSubExprsThrow(*this, E)); 1012 } 1013 1014 case Expr::CXXConstructExprClass: 1015 case Expr::CXXTemporaryObjectExprClass: { 1016 CanThrowResult CT = canCalleeThrow(*this, E, 1017 cast<CXXConstructExpr>(E)->getConstructor()); 1018 if (CT == CT_Can) 1019 return CT; 1020 return mergeCanThrow(CT, canSubExprsThrow(*this, E)); 1021 } 1022 1023 case Expr::CXXInheritedCtorInitExprClass: 1024 return canCalleeThrow(*this, E, 1025 cast<CXXInheritedCtorInitExpr>(E)->getConstructor()); 1026 1027 case Expr::LambdaExprClass: { 1028 const LambdaExpr *Lambda = cast<LambdaExpr>(E); 1029 CanThrowResult CT = CT_Cannot; 1030 for (LambdaExpr::const_capture_init_iterator 1031 Cap = Lambda->capture_init_begin(), 1032 CapEnd = Lambda->capture_init_end(); 1033 Cap != CapEnd; ++Cap) 1034 CT = mergeCanThrow(CT, canThrow(*Cap)); 1035 return CT; 1036 } 1037 1038 case Expr::CXXNewExprClass: { 1039 CanThrowResult CT; 1040 if (E->isTypeDependent()) 1041 CT = CT_Dependent; 1042 else 1043 CT = canCalleeThrow(*this, E, cast<CXXNewExpr>(E)->getOperatorNew()); 1044 if (CT == CT_Can) 1045 return CT; 1046 return mergeCanThrow(CT, canSubExprsThrow(*this, E)); 1047 } 1048 1049 case Expr::CXXDeleteExprClass: { 1050 CanThrowResult CT; 1051 QualType DTy = cast<CXXDeleteExpr>(E)->getDestroyedType(); 1052 if (DTy.isNull() || DTy->isDependentType()) { 1053 CT = CT_Dependent; 1054 } else { 1055 CT = canCalleeThrow(*this, E, 1056 cast<CXXDeleteExpr>(E)->getOperatorDelete()); 1057 if (const RecordType *RT = DTy->getAs<RecordType>()) { 1058 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); 1059 const CXXDestructorDecl *DD = RD->getDestructor(); 1060 if (DD) 1061 CT = mergeCanThrow(CT, canCalleeThrow(*this, E, DD)); 1062 } 1063 if (CT == CT_Can) 1064 return CT; 1065 } 1066 return mergeCanThrow(CT, canSubExprsThrow(*this, E)); 1067 } 1068 1069 case Expr::CXXBindTemporaryExprClass: { 1070 // The bound temporary has to be destroyed again, which might throw. 1071 CanThrowResult CT = canCalleeThrow(*this, E, 1072 cast<CXXBindTemporaryExpr>(E)->getTemporary()->getDestructor()); 1073 if (CT == CT_Can) 1074 return CT; 1075 return mergeCanThrow(CT, canSubExprsThrow(*this, E)); 1076 } 1077 1078 // ObjC message sends are like function calls, but never have exception 1079 // specs. 1080 case Expr::ObjCMessageExprClass: 1081 case Expr::ObjCPropertyRefExprClass: 1082 case Expr::ObjCSubscriptRefExprClass: 1083 return CT_Can; 1084 1085 // All the ObjC literals that are implemented as calls are 1086 // potentially throwing unless we decide to close off that 1087 // possibility. 1088 case Expr::ObjCArrayLiteralClass: 1089 case Expr::ObjCDictionaryLiteralClass: 1090 case Expr::ObjCBoxedExprClass: 1091 return CT_Can; 1092 1093 // Many other things have subexpressions, so we have to test those. 1094 // Some are simple: 1095 case Expr::CoawaitExprClass: 1096 case Expr::ConditionalOperatorClass: 1097 case Expr::CompoundLiteralExprClass: 1098 case Expr::CoyieldExprClass: 1099 case Expr::CXXConstCastExprClass: 1100 case Expr::CXXReinterpretCastExprClass: 1101 case Expr::CXXStdInitializerListExprClass: 1102 case Expr::DesignatedInitExprClass: 1103 case Expr::DesignatedInitUpdateExprClass: 1104 case Expr::ExprWithCleanupsClass: 1105 case Expr::ExtVectorElementExprClass: 1106 case Expr::InitListExprClass: 1107 case Expr::MemberExprClass: 1108 case Expr::ObjCIsaExprClass: 1109 case Expr::ObjCIvarRefExprClass: 1110 case Expr::ParenExprClass: 1111 case Expr::ParenListExprClass: 1112 case Expr::ShuffleVectorExprClass: 1113 case Expr::ConvertVectorExprClass: 1114 case Expr::VAArgExprClass: 1115 return canSubExprsThrow(*this, E); 1116 1117 // Some might be dependent for other reasons. 1118 case Expr::ArraySubscriptExprClass: 1119 case Expr::OMPArraySectionExprClass: 1120 case Expr::BinaryOperatorClass: 1121 case Expr::CompoundAssignOperatorClass: 1122 case Expr::CStyleCastExprClass: 1123 case Expr::CXXStaticCastExprClass: 1124 case Expr::CXXFunctionalCastExprClass: 1125 case Expr::ImplicitCastExprClass: 1126 case Expr::MaterializeTemporaryExprClass: 1127 case Expr::UnaryOperatorClass: { 1128 CanThrowResult CT = E->isTypeDependent() ? CT_Dependent : CT_Cannot; 1129 return mergeCanThrow(CT, canSubExprsThrow(*this, E)); 1130 } 1131 1132 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms. 1133 case Expr::StmtExprClass: 1134 return CT_Can; 1135 1136 case Expr::CXXDefaultArgExprClass: 1137 return canThrow(cast<CXXDefaultArgExpr>(E)->getExpr()); 1138 1139 case Expr::CXXDefaultInitExprClass: 1140 return canThrow(cast<CXXDefaultInitExpr>(E)->getExpr()); 1141 1142 case Expr::ChooseExprClass: 1143 if (E->isTypeDependent() || E->isValueDependent()) 1144 return CT_Dependent; 1145 return canThrow(cast<ChooseExpr>(E)->getChosenSubExpr()); 1146 1147 case Expr::GenericSelectionExprClass: 1148 if (cast<GenericSelectionExpr>(E)->isResultDependent()) 1149 return CT_Dependent; 1150 return canThrow(cast<GenericSelectionExpr>(E)->getResultExpr()); 1151 1152 // Some expressions are always dependent. 1153 case Expr::CXXDependentScopeMemberExprClass: 1154 case Expr::CXXUnresolvedConstructExprClass: 1155 case Expr::DependentScopeDeclRefExprClass: 1156 case Expr::CXXFoldExprClass: 1157 return CT_Dependent; 1158 1159 case Expr::AsTypeExprClass: 1160 case Expr::BinaryConditionalOperatorClass: 1161 case Expr::BlockExprClass: 1162 case Expr::CUDAKernelCallExprClass: 1163 case Expr::DeclRefExprClass: 1164 case Expr::ObjCBridgedCastExprClass: 1165 case Expr::ObjCIndirectCopyRestoreExprClass: 1166 case Expr::ObjCProtocolExprClass: 1167 case Expr::ObjCSelectorExprClass: 1168 case Expr::ObjCAvailabilityCheckExprClass: 1169 case Expr::OffsetOfExprClass: 1170 case Expr::PackExpansionExprClass: 1171 case Expr::PseudoObjectExprClass: 1172 case Expr::SubstNonTypeTemplateParmExprClass: 1173 case Expr::SubstNonTypeTemplateParmPackExprClass: 1174 case Expr::FunctionParmPackExprClass: 1175 case Expr::UnaryExprOrTypeTraitExprClass: 1176 case Expr::UnresolvedLookupExprClass: 1177 case Expr::UnresolvedMemberExprClass: 1178 case Expr::TypoExprClass: 1179 // FIXME: Can any of the above throw? If so, when? 1180 return CT_Cannot; 1181 1182 case Expr::AddrLabelExprClass: 1183 case Expr::ArrayTypeTraitExprClass: 1184 case Expr::AtomicExprClass: 1185 case Expr::TypeTraitExprClass: 1186 case Expr::CXXBoolLiteralExprClass: 1187 case Expr::CXXNoexceptExprClass: 1188 case Expr::CXXNullPtrLiteralExprClass: 1189 case Expr::CXXPseudoDestructorExprClass: 1190 case Expr::CXXScalarValueInitExprClass: 1191 case Expr::CXXThisExprClass: 1192 case Expr::CXXUuidofExprClass: 1193 case Expr::CharacterLiteralClass: 1194 case Expr::ExpressionTraitExprClass: 1195 case Expr::FloatingLiteralClass: 1196 case Expr::GNUNullExprClass: 1197 case Expr::ImaginaryLiteralClass: 1198 case Expr::ImplicitValueInitExprClass: 1199 case Expr::IntegerLiteralClass: 1200 case Expr::NoInitExprClass: 1201 case Expr::ObjCEncodeExprClass: 1202 case Expr::ObjCStringLiteralClass: 1203 case Expr::ObjCBoolLiteralExprClass: 1204 case Expr::OpaqueValueExprClass: 1205 case Expr::PredefinedExprClass: 1206 case Expr::SizeOfPackExprClass: 1207 case Expr::StringLiteralClass: 1208 // These expressions can never throw. 1209 return CT_Cannot; 1210 1211 case Expr::MSPropertyRefExprClass: 1212 case Expr::MSPropertySubscriptExprClass: 1213 llvm_unreachable("Invalid class for expression"); 1214 1215 #define STMT(CLASS, PARENT) case Expr::CLASS##Class: 1216 #define STMT_RANGE(Base, First, Last) 1217 #define LAST_STMT_RANGE(BASE, FIRST, LAST) 1218 #define EXPR(CLASS, PARENT) 1219 #define ABSTRACT_STMT(STMT) 1220 #include "clang/AST/StmtNodes.inc" 1221 case Expr::NoStmtClass: 1222 llvm_unreachable("Invalid class for expression"); 1223 } 1224 llvm_unreachable("Bogus StmtClass"); 1225 } 1226 1227 } // end namespace clang 1228