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