clang API Documentation

SemaExceptionSpec.cpp
Go to the documentation of this file.
00001 //===--- SemaExceptionSpec.cpp - C++ Exception Specifications ---*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file provides Sema routines for C++ exception specification testing.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "clang/Sema/SemaInternal.h"
00015 #include "clang/AST/ASTMutationListener.h"
00016 #include "clang/AST/CXXInheritance.h"
00017 #include "clang/AST/Expr.h"
00018 #include "clang/AST/ExprCXX.h"
00019 #include "clang/AST/TypeLoc.h"
00020 #include "clang/Basic/Diagnostic.h"
00021 #include "clang/Basic/SourceManager.h"
00022 #include "llvm/ADT/SmallPtrSet.h"
00023 #include "llvm/ADT/SmallString.h"
00024 
00025 namespace clang {
00026 
00027 static const FunctionProtoType *GetUnderlyingFunction(QualType T)
00028 {
00029   if (const PointerType *PtrTy = T->getAs<PointerType>())
00030     T = PtrTy->getPointeeType();
00031   else if (const ReferenceType *RefTy = T->getAs<ReferenceType>())
00032     T = RefTy->getPointeeType();
00033   else if (const MemberPointerType *MPTy = T->getAs<MemberPointerType>())
00034     T = MPTy->getPointeeType();
00035   return T->getAs<FunctionProtoType>();
00036 }
00037 
00038 /// HACK: libstdc++ has a bug where it shadows std::swap with a member
00039 /// swap function then tries to call std::swap unqualified from the exception
00040 /// specification of that function. This function detects whether we're in
00041 /// such a case and turns off delay-parsing of exception specifications.
00042 bool Sema::isLibstdcxxEagerExceptionSpecHack(const Declarator &D) {
00043   auto *RD = dyn_cast<CXXRecordDecl>(CurContext);
00044 
00045   // All the problem cases are member functions named "swap" within class
00046   // templates declared directly within namespace std.
00047   if (!RD || RD->getEnclosingNamespaceContext() != getStdNamespace() ||
00048       !RD->getIdentifier() || !RD->getDescribedClassTemplate() ||
00049       !D.getIdentifier() || !D.getIdentifier()->isStr("swap"))
00050     return false;
00051 
00052   // Only apply this hack within a system header.
00053   if (!Context.getSourceManager().isInSystemHeader(D.getLocStart()))
00054     return false;
00055 
00056   return llvm::StringSwitch<bool>(RD->getIdentifier()->getName())
00057       .Case("array", true)
00058       .Case("pair", true)
00059       .Case("priority_queue", true)
00060       .Case("stack", true)
00061       .Case("queue", true)
00062       .Default(false);
00063 }
00064 
00065 /// CheckSpecifiedExceptionType - Check if the given type is valid in an
00066 /// exception specification. Incomplete types, or pointers to incomplete types
00067 /// other than void are not allowed.
00068 ///
00069 /// \param[in,out] T  The exception type. This will be decayed to a pointer type
00070 ///                   when the input is an array or a function type.
00071 bool Sema::CheckSpecifiedExceptionType(QualType &T, const SourceRange &Range) {
00072   // C++11 [except.spec]p2:
00073   //   A type cv T, "array of T", or "function returning T" denoted
00074   //   in an exception-specification is adjusted to type T, "pointer to T", or
00075   //   "pointer to function returning T", respectively.
00076   //
00077   // We also apply this rule in C++98.
00078   if (T->isArrayType())
00079     T = Context.getArrayDecayedType(T);
00080   else if (T->isFunctionType())
00081     T = Context.getPointerType(T);
00082 
00083   int Kind = 0;
00084   QualType PointeeT = T;
00085   if (const PointerType *PT = T->getAs<PointerType>()) {
00086     PointeeT = PT->getPointeeType();
00087     Kind = 1;
00088 
00089     // cv void* is explicitly permitted, despite being a pointer to an
00090     // incomplete type.
00091     if (PointeeT->isVoidType())
00092       return false;
00093   } else if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
00094     PointeeT = RT->getPointeeType();
00095     Kind = 2;
00096 
00097     if (RT->isRValueReferenceType()) {
00098       // C++11 [except.spec]p2:
00099       //   A type denoted in an exception-specification shall not denote [...]
00100       //   an rvalue reference type.
00101       Diag(Range.getBegin(), diag::err_rref_in_exception_spec)
00102         << T << Range;
00103       return true;
00104     }
00105   }
00106 
00107   // C++11 [except.spec]p2:
00108   //   A type denoted in an exception-specification shall not denote an
00109   //   incomplete type other than a class currently being defined [...].
00110   //   A type denoted in an exception-specification shall not denote a
00111   //   pointer or reference to an incomplete type, other than (cv) void* or a
00112   //   pointer or reference to a class currently being defined.
00113   if (!(PointeeT->isRecordType() &&
00114         PointeeT->getAs<RecordType>()->isBeingDefined()) &&
00115       RequireCompleteType(Range.getBegin(), PointeeT,
00116                           diag::err_incomplete_in_exception_spec, Kind, Range))
00117     return true;
00118 
00119   return false;
00120 }
00121 
00122 /// CheckDistantExceptionSpec - Check if the given type is a pointer or pointer
00123 /// to member to a function with an exception specification. This means that
00124 /// it is invalid to add another level of indirection.
00125 bool Sema::CheckDistantExceptionSpec(QualType T) {
00126   if (const PointerType *PT = T->getAs<PointerType>())
00127     T = PT->getPointeeType();
00128   else if (const MemberPointerType *PT = T->getAs<MemberPointerType>())
00129     T = PT->getPointeeType();
00130   else
00131     return false;
00132 
00133   const FunctionProtoType *FnT = T->getAs<FunctionProtoType>();
00134   if (!FnT)
00135     return false;
00136 
00137   return FnT->hasExceptionSpec();
00138 }
00139 
00140 const FunctionProtoType *
00141 Sema::ResolveExceptionSpec(SourceLocation Loc, const FunctionProtoType *FPT) {
00142   if (FPT->getExceptionSpecType() == EST_Unparsed) {
00143     Diag(Loc, diag::err_exception_spec_not_parsed);
00144     return nullptr;
00145   }
00146 
00147   if (!isUnresolvedExceptionSpec(FPT->getExceptionSpecType()))
00148     return FPT;
00149 
00150   FunctionDecl *SourceDecl = FPT->getExceptionSpecDecl();
00151   const FunctionProtoType *SourceFPT =
00152       SourceDecl->getType()->castAs<FunctionProtoType>();
00153 
00154   // If the exception specification has already been resolved, just return it.
00155   if (!isUnresolvedExceptionSpec(SourceFPT->getExceptionSpecType()))
00156     return SourceFPT;
00157 
00158   // Compute or instantiate the exception specification now.
00159   if (SourceFPT->getExceptionSpecType() == EST_Unevaluated)
00160     EvaluateImplicitExceptionSpec(Loc, cast<CXXMethodDecl>(SourceDecl));
00161   else
00162     InstantiateExceptionSpec(Loc, SourceDecl);
00163 
00164   return SourceDecl->getType()->castAs<FunctionProtoType>();
00165 }
00166 
00167 void
00168 Sema::UpdateExceptionSpec(FunctionDecl *FD,
00169                           const FunctionProtoType::ExceptionSpecInfo &ESI) {
00170   for (auto *Redecl : FD->redecls())
00171     Context.adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
00172 
00173   // If we've fully resolved the exception specification, notify listeners.
00174   if (!isUnresolvedExceptionSpec(ESI.Type))
00175     if (auto *Listener = getASTMutationListener())
00176       Listener->ResolvedExceptionSpec(FD);
00177 }
00178 
00179 /// Determine whether a function has an implicitly-generated exception
00180 /// specification.
00181 static bool hasImplicitExceptionSpec(FunctionDecl *Decl) {
00182   if (!isa<CXXDestructorDecl>(Decl) &&
00183       Decl->getDeclName().getCXXOverloadedOperator() != OO_Delete &&
00184       Decl->getDeclName().getCXXOverloadedOperator() != OO_Array_Delete)
00185     return false;
00186 
00187   // For a function that the user didn't declare:
00188   //  - if this is a destructor, its exception specification is implicit.
00189   //  - if this is 'operator delete' or 'operator delete[]', the exception
00190   //    specification is as-if an explicit exception specification was given
00191   //    (per [basic.stc.dynamic]p2).
00192   if (!Decl->getTypeSourceInfo())
00193     return isa<CXXDestructorDecl>(Decl);
00194 
00195   const FunctionProtoType *Ty =
00196     Decl->getTypeSourceInfo()->getType()->getAs<FunctionProtoType>();
00197   return !Ty->hasExceptionSpec();
00198 }
00199 
00200 bool Sema::CheckEquivalentExceptionSpec(FunctionDecl *Old, FunctionDecl *New) {
00201   OverloadedOperatorKind OO = New->getDeclName().getCXXOverloadedOperator();
00202   bool IsOperatorNew = OO == OO_New || OO == OO_Array_New;
00203   bool MissingExceptionSpecification = false;
00204   bool MissingEmptyExceptionSpecification = false;
00205 
00206   unsigned DiagID = diag::err_mismatched_exception_spec;
00207   bool ReturnValueOnError = true;
00208   if (getLangOpts().MicrosoftExt) {
00209     DiagID = diag::ext_mismatched_exception_spec;
00210     ReturnValueOnError = false;
00211   }
00212 
00213   // Check the types as written: they must match before any exception
00214   // specification adjustment is applied.
00215   if (!CheckEquivalentExceptionSpec(
00216         PDiag(DiagID), PDiag(diag::note_previous_declaration),
00217         Old->getType()->getAs<FunctionProtoType>(), Old->getLocation(),
00218         New->getType()->getAs<FunctionProtoType>(), New->getLocation(),
00219         &MissingExceptionSpecification, &MissingEmptyExceptionSpecification,
00220         /*AllowNoexceptAllMatchWithNoSpec=*/true, IsOperatorNew)) {
00221     // C++11 [except.spec]p4 [DR1492]:
00222     //   If a declaration of a function has an implicit
00223     //   exception-specification, other declarations of the function shall
00224     //   not specify an exception-specification.
00225     if (getLangOpts().CPlusPlus11 &&
00226         hasImplicitExceptionSpec(Old) != hasImplicitExceptionSpec(New)) {
00227       Diag(New->getLocation(), diag::ext_implicit_exception_spec_mismatch)
00228         << hasImplicitExceptionSpec(Old);
00229       if (!Old->getLocation().isInvalid())
00230         Diag(Old->getLocation(), diag::note_previous_declaration);
00231     }
00232     return false;
00233   }
00234 
00235   // The failure was something other than an missing exception
00236   // specification; return an error, except in MS mode where this is a warning.
00237   if (!MissingExceptionSpecification)
00238     return ReturnValueOnError;
00239 
00240   const FunctionProtoType *NewProto =
00241     New->getType()->castAs<FunctionProtoType>();
00242 
00243   // The new function declaration is only missing an empty exception
00244   // specification "throw()". If the throw() specification came from a
00245   // function in a system header that has C linkage, just add an empty
00246   // exception specification to the "new" declaration. This is an
00247   // egregious workaround for glibc, which adds throw() specifications
00248   // to many libc functions as an optimization. Unfortunately, that
00249   // optimization isn't permitted by the C++ standard, so we're forced
00250   // to work around it here.
00251   if (MissingEmptyExceptionSpecification && NewProto &&
00252       (Old->getLocation().isInvalid() ||
00253        Context.getSourceManager().isInSystemHeader(Old->getLocation())) &&
00254       Old->isExternC()) {
00255     New->setType(Context.getFunctionType(
00256         NewProto->getReturnType(), NewProto->getParamTypes(),
00257         NewProto->getExtProtoInfo().withExceptionSpec(EST_DynamicNone)));
00258     return false;
00259   }
00260 
00261   const FunctionProtoType *OldProto =
00262     Old->getType()->castAs<FunctionProtoType>();
00263 
00264   FunctionProtoType::ExceptionSpecInfo ESI = OldProto->getExceptionSpecType();
00265   if (ESI.Type == EST_Dynamic) {
00266     ESI.Exceptions = OldProto->exceptions();
00267   } else if (ESI.Type == EST_ComputedNoexcept) {
00268     // FIXME: We can't just take the expression from the old prototype. It
00269     // likely contains references to the old prototype's parameters.
00270   }
00271 
00272   // Update the type of the function with the appropriate exception
00273   // specification.
00274   New->setType(Context.getFunctionType(
00275       NewProto->getReturnType(), NewProto->getParamTypes(),
00276       NewProto->getExtProtoInfo().withExceptionSpec(ESI)));
00277 
00278   // Warn about the lack of exception specification.
00279   SmallString<128> ExceptionSpecString;
00280   llvm::raw_svector_ostream OS(ExceptionSpecString);
00281   switch (OldProto->getExceptionSpecType()) {
00282   case EST_DynamicNone:
00283     OS << "throw()";
00284     break;
00285 
00286   case EST_Dynamic: {
00287     OS << "throw(";
00288     bool OnFirstException = true;
00289     for (const auto &E : OldProto->exceptions()) {
00290       if (OnFirstException)
00291         OnFirstException = false;
00292       else
00293         OS << ", ";
00294       
00295       OS << E.getAsString(getPrintingPolicy());
00296     }
00297     OS << ")";
00298     break;
00299   }
00300 
00301   case EST_BasicNoexcept:
00302     OS << "noexcept";
00303     break;
00304 
00305   case EST_ComputedNoexcept:
00306     OS << "noexcept(";
00307     assert(OldProto->getNoexceptExpr() != nullptr && "Expected non-null Expr");
00308     OldProto->getNoexceptExpr()->printPretty(OS, nullptr, getPrintingPolicy());
00309     OS << ")";
00310     break;
00311 
00312   default:
00313     llvm_unreachable("This spec type is compatible with none.");
00314   }
00315   OS.flush();
00316 
00317   SourceLocation FixItLoc;
00318   if (TypeSourceInfo *TSInfo = New->getTypeSourceInfo()) {
00319     TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens();
00320     if (FunctionTypeLoc FTLoc = TL.getAs<FunctionTypeLoc>())
00321       FixItLoc = getLocForEndOfToken(FTLoc.getLocalRangeEnd());
00322   }
00323 
00324   if (FixItLoc.isInvalid())
00325     Diag(New->getLocation(), diag::warn_missing_exception_specification)
00326       << New << OS.str();
00327   else {
00328     // FIXME: This will get more complicated with C++0x
00329     // late-specified return types.
00330     Diag(New->getLocation(), diag::warn_missing_exception_specification)
00331       << New << OS.str()
00332       << FixItHint::CreateInsertion(FixItLoc, " " + OS.str().str());
00333   }
00334 
00335   if (!Old->getLocation().isInvalid())
00336     Diag(Old->getLocation(), diag::note_previous_declaration);
00337 
00338   return false;
00339 }
00340 
00341 /// CheckEquivalentExceptionSpec - Check if the two types have equivalent
00342 /// exception specifications. Exception specifications are equivalent if
00343 /// they allow exactly the same set of exception types. It does not matter how
00344 /// that is achieved. See C++ [except.spec]p2.
00345 bool Sema::CheckEquivalentExceptionSpec(
00346     const FunctionProtoType *Old, SourceLocation OldLoc,
00347     const FunctionProtoType *New, SourceLocation NewLoc) {
00348   unsigned DiagID = diag::err_mismatched_exception_spec;
00349   if (getLangOpts().MicrosoftExt)
00350     DiagID = diag::ext_mismatched_exception_spec;
00351   bool Result = CheckEquivalentExceptionSpec(PDiag(DiagID),
00352       PDiag(diag::note_previous_declaration), Old, OldLoc, New, NewLoc);
00353 
00354   // In Microsoft mode, mismatching exception specifications just cause a warning.
00355   if (getLangOpts().MicrosoftExt)
00356     return false;
00357   return Result;
00358 }
00359 
00360 /// CheckEquivalentExceptionSpec - Check if the two types have compatible
00361 /// exception specifications. See C++ [except.spec]p3.
00362 ///
00363 /// \return \c false if the exception specifications match, \c true if there is
00364 /// a problem. If \c true is returned, either a diagnostic has already been
00365 /// produced or \c *MissingExceptionSpecification is set to \c true.
00366 bool Sema::CheckEquivalentExceptionSpec(const PartialDiagnostic &DiagID,
00367                                         const PartialDiagnostic & NoteID,
00368                                         const FunctionProtoType *Old,
00369                                         SourceLocation OldLoc,
00370                                         const FunctionProtoType *New,
00371                                         SourceLocation NewLoc,
00372                                         bool *MissingExceptionSpecification,
00373                                         bool*MissingEmptyExceptionSpecification,
00374                                         bool AllowNoexceptAllMatchWithNoSpec,
00375                                         bool IsOperatorNew) {
00376   // Just completely ignore this under -fno-exceptions.
00377   if (!getLangOpts().CXXExceptions)
00378     return false;
00379 
00380   if (MissingExceptionSpecification)
00381     *MissingExceptionSpecification = false;
00382 
00383   if (MissingEmptyExceptionSpecification)
00384     *MissingEmptyExceptionSpecification = false;
00385 
00386   Old = ResolveExceptionSpec(NewLoc, Old);
00387   if (!Old)
00388     return false;
00389   New = ResolveExceptionSpec(NewLoc, New);
00390   if (!New)
00391     return false;
00392 
00393   // C++0x [except.spec]p3: Two exception-specifications are compatible if:
00394   //   - both are non-throwing, regardless of their form,
00395   //   - both have the form noexcept(constant-expression) and the constant-
00396   //     expressions are equivalent,
00397   //   - both are dynamic-exception-specifications that have the same set of
00398   //     adjusted types.
00399   //
00400   // C++0x [except.spec]p12: An exception-specifcation is non-throwing if it is
00401   //   of the form throw(), noexcept, or noexcept(constant-expression) where the
00402   //   constant-expression yields true.
00403   //
00404   // C++0x [except.spec]p4: If any declaration of a function has an exception-
00405   //   specifier that is not a noexcept-specification allowing all exceptions,
00406   //   all declarations [...] of that function shall have a compatible
00407   //   exception-specification.
00408   //
00409   // That last point basically means that noexcept(false) matches no spec.
00410   // It's considered when AllowNoexceptAllMatchWithNoSpec is true.
00411 
00412   ExceptionSpecificationType OldEST = Old->getExceptionSpecType();
00413   ExceptionSpecificationType NewEST = New->getExceptionSpecType();
00414 
00415   assert(!isUnresolvedExceptionSpec(OldEST) &&
00416          !isUnresolvedExceptionSpec(NewEST) &&
00417          "Shouldn't see unknown exception specifications here");
00418 
00419   // Shortcut the case where both have no spec.
00420   if (OldEST == EST_None && NewEST == EST_None)
00421     return false;
00422 
00423   FunctionProtoType::NoexceptResult OldNR = Old->getNoexceptSpec(Context);
00424   FunctionProtoType::NoexceptResult NewNR = New->getNoexceptSpec(Context);
00425   if (OldNR == FunctionProtoType::NR_BadNoexcept ||
00426       NewNR == FunctionProtoType::NR_BadNoexcept)
00427     return false;
00428 
00429   // Dependent noexcept specifiers are compatible with each other, but nothing
00430   // else.
00431   // One noexcept is compatible with another if the argument is the same
00432   if (OldNR == NewNR &&
00433       OldNR != FunctionProtoType::NR_NoNoexcept &&
00434       NewNR != FunctionProtoType::NR_NoNoexcept)
00435     return false;
00436   if (OldNR != NewNR &&
00437       OldNR != FunctionProtoType::NR_NoNoexcept &&
00438       NewNR != FunctionProtoType::NR_NoNoexcept) {
00439     Diag(NewLoc, DiagID);
00440     if (NoteID.getDiagID() != 0)
00441       Diag(OldLoc, NoteID);
00442     return true;
00443   }
00444 
00445   // The MS extension throw(...) is compatible with itself.
00446   if (OldEST == EST_MSAny && NewEST == EST_MSAny)
00447     return false;
00448 
00449   // It's also compatible with no spec.
00450   if ((OldEST == EST_None && NewEST == EST_MSAny) ||
00451       (OldEST == EST_MSAny && NewEST == EST_None))
00452     return false;
00453 
00454   // It's also compatible with noexcept(false).
00455   if (OldEST == EST_MSAny && NewNR == FunctionProtoType::NR_Throw)
00456     return false;
00457   if (NewEST == EST_MSAny && OldNR == FunctionProtoType::NR_Throw)
00458     return false;
00459 
00460   // As described above, noexcept(false) matches no spec only for functions.
00461   if (AllowNoexceptAllMatchWithNoSpec) {
00462     if (OldEST == EST_None && NewNR == FunctionProtoType::NR_Throw)
00463       return false;
00464     if (NewEST == EST_None && OldNR == FunctionProtoType::NR_Throw)
00465       return false;
00466   }
00467 
00468   // Any non-throwing specifications are compatible.
00469   bool OldNonThrowing = OldNR == FunctionProtoType::NR_Nothrow ||
00470                         OldEST == EST_DynamicNone;
00471   bool NewNonThrowing = NewNR == FunctionProtoType::NR_Nothrow ||
00472                         NewEST == EST_DynamicNone;
00473   if (OldNonThrowing && NewNonThrowing)
00474     return false;
00475 
00476   // As a special compatibility feature, under C++0x we accept no spec and
00477   // throw(std::bad_alloc) as equivalent for operator new and operator new[].
00478   // This is because the implicit declaration changed, but old code would break.
00479   if (getLangOpts().CPlusPlus11 && IsOperatorNew) {
00480     const FunctionProtoType *WithExceptions = nullptr;
00481     if (OldEST == EST_None && NewEST == EST_Dynamic)
00482       WithExceptions = New;
00483     else if (OldEST == EST_Dynamic && NewEST == EST_None)
00484       WithExceptions = Old;
00485     if (WithExceptions && WithExceptions->getNumExceptions() == 1) {
00486       // One has no spec, the other throw(something). If that something is
00487       // std::bad_alloc, all conditions are met.
00488       QualType Exception = *WithExceptions->exception_begin();
00489       if (CXXRecordDecl *ExRecord = Exception->getAsCXXRecordDecl()) {
00490         IdentifierInfo* Name = ExRecord->getIdentifier();
00491         if (Name && Name->getName() == "bad_alloc") {
00492           // It's called bad_alloc, but is it in std?
00493           if (ExRecord->isInStdNamespace()) {
00494             return false;
00495           }
00496         }
00497       }
00498     }
00499   }
00500 
00501   // At this point, the only remaining valid case is two matching dynamic
00502   // specifications. We return here unless both specifications are dynamic.
00503   if (OldEST != EST_Dynamic || NewEST != EST_Dynamic) {
00504     if (MissingExceptionSpecification && Old->hasExceptionSpec() &&
00505         !New->hasExceptionSpec()) {
00506       // The old type has an exception specification of some sort, but
00507       // the new type does not.
00508       *MissingExceptionSpecification = true;
00509 
00510       if (MissingEmptyExceptionSpecification && OldNonThrowing) {
00511         // The old type has a throw() or noexcept(true) exception specification
00512         // and the new type has no exception specification, and the caller asked
00513         // to handle this itself.
00514         *MissingEmptyExceptionSpecification = true;
00515       }
00516 
00517       return true;
00518     }
00519 
00520     Diag(NewLoc, DiagID);
00521     if (NoteID.getDiagID() != 0)
00522       Diag(OldLoc, NoteID);
00523     return true;
00524   }
00525 
00526   assert(OldEST == EST_Dynamic && NewEST == EST_Dynamic &&
00527       "Exception compatibility logic error: non-dynamic spec slipped through.");
00528 
00529   bool Success = true;
00530   // Both have a dynamic exception spec. Collect the first set, then compare
00531   // to the second.
00532   llvm::SmallPtrSet<CanQualType, 8> OldTypes, NewTypes;
00533   for (const auto &I : Old->exceptions())
00534     OldTypes.insert(Context.getCanonicalType(I).getUnqualifiedType());
00535 
00536   for (const auto &I : New->exceptions()) {
00537     CanQualType TypePtr = Context.getCanonicalType(I).getUnqualifiedType();
00538     if(OldTypes.count(TypePtr))
00539       NewTypes.insert(TypePtr);
00540     else
00541       Success = false;
00542   }
00543 
00544   Success = Success && OldTypes.size() == NewTypes.size();
00545 
00546   if (Success) {
00547     return false;
00548   }
00549   Diag(NewLoc, DiagID);
00550   if (NoteID.getDiagID() != 0)
00551     Diag(OldLoc, NoteID);
00552   return true;
00553 }
00554 
00555 /// CheckExceptionSpecSubset - Check whether the second function type's
00556 /// exception specification is a subset (or equivalent) of the first function
00557 /// type. This is used by override and pointer assignment checks.
00558 bool Sema::CheckExceptionSpecSubset(
00559     const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID,
00560     const FunctionProtoType *Superset, SourceLocation SuperLoc,
00561     const FunctionProtoType *Subset, SourceLocation SubLoc) {
00562 
00563   // Just auto-succeed under -fno-exceptions.
00564   if (!getLangOpts().CXXExceptions)
00565     return false;
00566 
00567   // FIXME: As usual, we could be more specific in our error messages, but
00568   // that better waits until we've got types with source locations.
00569 
00570   if (!SubLoc.isValid())
00571     SubLoc = SuperLoc;
00572 
00573   // Resolve the exception specifications, if needed.
00574   Superset = ResolveExceptionSpec(SuperLoc, Superset);
00575   if (!Superset)
00576     return false;
00577   Subset = ResolveExceptionSpec(SubLoc, Subset);
00578   if (!Subset)
00579     return false;
00580 
00581   ExceptionSpecificationType SuperEST = Superset->getExceptionSpecType();
00582 
00583   // If superset contains everything, we're done.
00584   if (SuperEST == EST_None || SuperEST == EST_MSAny)
00585     return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
00586 
00587   // If there are dependent noexcept specs, assume everything is fine. Unlike
00588   // with the equivalency check, this is safe in this case, because we don't
00589   // want to merge declarations. Checks after instantiation will catch any
00590   // omissions we make here.
00591   // We also shortcut checking if a noexcept expression was bad.
00592 
00593   FunctionProtoType::NoexceptResult SuperNR =Superset->getNoexceptSpec(Context);
00594   if (SuperNR == FunctionProtoType::NR_BadNoexcept ||
00595       SuperNR == FunctionProtoType::NR_Dependent)
00596     return false;
00597 
00598   // Another case of the superset containing everything.
00599   if (SuperNR == FunctionProtoType::NR_Throw)
00600     return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
00601 
00602   ExceptionSpecificationType SubEST = Subset->getExceptionSpecType();
00603 
00604   assert(!isUnresolvedExceptionSpec(SuperEST) &&
00605          !isUnresolvedExceptionSpec(SubEST) &&
00606          "Shouldn't see unknown exception specifications here");
00607 
00608   // It does not. If the subset contains everything, we've failed.
00609   if (SubEST == EST_None || SubEST == EST_MSAny) {
00610     Diag(SubLoc, DiagID);
00611     if (NoteID.getDiagID() != 0)
00612       Diag(SuperLoc, NoteID);
00613     return true;
00614   }
00615 
00616   FunctionProtoType::NoexceptResult SubNR = Subset->getNoexceptSpec(Context);
00617   if (SubNR == FunctionProtoType::NR_BadNoexcept ||
00618       SubNR == FunctionProtoType::NR_Dependent)
00619     return false;
00620 
00621   // Another case of the subset containing everything.
00622   if (SubNR == FunctionProtoType::NR_Throw) {
00623     Diag(SubLoc, DiagID);
00624     if (NoteID.getDiagID() != 0)
00625       Diag(SuperLoc, NoteID);
00626     return true;
00627   }
00628 
00629   // If the subset contains nothing, we're done.
00630   if (SubEST == EST_DynamicNone || SubNR == FunctionProtoType::NR_Nothrow)
00631     return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
00632 
00633   // Otherwise, if the superset contains nothing, we've failed.
00634   if (SuperEST == EST_DynamicNone || SuperNR == FunctionProtoType::NR_Nothrow) {
00635     Diag(SubLoc, DiagID);
00636     if (NoteID.getDiagID() != 0)
00637       Diag(SuperLoc, NoteID);
00638     return true;
00639   }
00640 
00641   assert(SuperEST == EST_Dynamic && SubEST == EST_Dynamic &&
00642          "Exception spec subset: non-dynamic case slipped through.");
00643 
00644   // Neither contains everything or nothing. Do a proper comparison.
00645   for (const auto &SubI : Subset->exceptions()) {
00646     // Take one type from the subset.
00647     QualType CanonicalSubT = Context.getCanonicalType(SubI);
00648     // Unwrap pointers and references so that we can do checks within a class
00649     // hierarchy. Don't unwrap member pointers; they don't have hierarchy
00650     // conversions on the pointee.
00651     bool SubIsPointer = false;
00652     if (const ReferenceType *RefTy = CanonicalSubT->getAs<ReferenceType>())
00653       CanonicalSubT = RefTy->getPointeeType();
00654     if (const PointerType *PtrTy = CanonicalSubT->getAs<PointerType>()) {
00655       CanonicalSubT = PtrTy->getPointeeType();
00656       SubIsPointer = true;
00657     }
00658     bool SubIsClass = CanonicalSubT->isRecordType();
00659     CanonicalSubT = CanonicalSubT.getLocalUnqualifiedType();
00660 
00661     CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
00662                        /*DetectVirtual=*/false);
00663 
00664     bool Contained = false;
00665     // Make sure it's in the superset.
00666     for (const auto &SuperI : Superset->exceptions()) {
00667       QualType CanonicalSuperT = Context.getCanonicalType(SuperI);
00668       // SubT must be SuperT or derived from it, or pointer or reference to
00669       // such types.
00670       if (const ReferenceType *RefTy = CanonicalSuperT->getAs<ReferenceType>())
00671         CanonicalSuperT = RefTy->getPointeeType();
00672       if (SubIsPointer) {
00673         if (const PointerType *PtrTy = CanonicalSuperT->getAs<PointerType>())
00674           CanonicalSuperT = PtrTy->getPointeeType();
00675         else {
00676           continue;
00677         }
00678       }
00679       CanonicalSuperT = CanonicalSuperT.getLocalUnqualifiedType();
00680       // If the types are the same, move on to the next type in the subset.
00681       if (CanonicalSubT == CanonicalSuperT) {
00682         Contained = true;
00683         break;
00684       }
00685 
00686       // Otherwise we need to check the inheritance.
00687       if (!SubIsClass || !CanonicalSuperT->isRecordType())
00688         continue;
00689 
00690       Paths.clear();
00691       if (!IsDerivedFrom(CanonicalSubT, CanonicalSuperT, Paths))
00692         continue;
00693 
00694       if (Paths.isAmbiguous(Context.getCanonicalType(CanonicalSuperT)))
00695         continue;
00696 
00697       // Do this check from a context without privileges.
00698       switch (CheckBaseClassAccess(SourceLocation(),
00699                                    CanonicalSuperT, CanonicalSubT,
00700                                    Paths.front(),
00701                                    /*Diagnostic*/ 0,
00702                                    /*ForceCheck*/ true,
00703                                    /*ForceUnprivileged*/ true)) {
00704       case AR_accessible: break;
00705       case AR_inaccessible: continue;
00706       case AR_dependent:
00707         llvm_unreachable("access check dependent for unprivileged context");
00708       case AR_delayed:
00709         llvm_unreachable("access check delayed in non-declaration");
00710       }
00711 
00712       Contained = true;
00713       break;
00714     }
00715     if (!Contained) {
00716       Diag(SubLoc, DiagID);
00717       if (NoteID.getDiagID() != 0)
00718         Diag(SuperLoc, NoteID);
00719       return true;
00720     }
00721   }
00722   // We've run half the gauntlet.
00723   return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
00724 }
00725 
00726 static bool CheckSpecForTypesEquivalent(Sema &S,
00727     const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID,
00728     QualType Target, SourceLocation TargetLoc,
00729     QualType Source, SourceLocation SourceLoc)
00730 {
00731   const FunctionProtoType *TFunc = GetUnderlyingFunction(Target);
00732   if (!TFunc)
00733     return false;
00734   const FunctionProtoType *SFunc = GetUnderlyingFunction(Source);
00735   if (!SFunc)
00736     return false;
00737 
00738   return S.CheckEquivalentExceptionSpec(DiagID, NoteID, TFunc, TargetLoc,
00739                                         SFunc, SourceLoc);
00740 }
00741 
00742 /// CheckParamExceptionSpec - Check if the parameter and return types of the
00743 /// two functions have equivalent exception specs. This is part of the
00744 /// assignment and override compatibility check. We do not check the parameters
00745 /// of parameter function pointers recursively, as no sane programmer would
00746 /// even be able to write such a function type.
00747 bool Sema::CheckParamExceptionSpec(const PartialDiagnostic &NoteID,
00748                                    const FunctionProtoType *Target,
00749                                    SourceLocation TargetLoc,
00750                                    const FunctionProtoType *Source,
00751                                    SourceLocation SourceLoc) {
00752   if (CheckSpecForTypesEquivalent(
00753           *this, PDiag(diag::err_deep_exception_specs_differ) << 0, PDiag(),
00754           Target->getReturnType(), TargetLoc, Source->getReturnType(),
00755           SourceLoc))
00756     return true;
00757 
00758   // We shouldn't even be testing this unless the arguments are otherwise
00759   // compatible.
00760   assert(Target->getNumParams() == Source->getNumParams() &&
00761          "Functions have different argument counts.");
00762   for (unsigned i = 0, E = Target->getNumParams(); i != E; ++i) {
00763     if (CheckSpecForTypesEquivalent(
00764             *this, PDiag(diag::err_deep_exception_specs_differ) << 1, PDiag(),
00765             Target->getParamType(i), TargetLoc, Source->getParamType(i),
00766             SourceLoc))
00767       return true;
00768   }
00769   return false;
00770 }
00771 
00772 bool Sema::CheckExceptionSpecCompatibility(Expr *From, QualType ToType) {
00773   // First we check for applicability.
00774   // Target type must be a function, function pointer or function reference.
00775   const FunctionProtoType *ToFunc = GetUnderlyingFunction(ToType);
00776   if (!ToFunc || ToFunc->hasDependentExceptionSpec())
00777     return false;
00778 
00779   // SourceType must be a function or function pointer.
00780   const FunctionProtoType *FromFunc = GetUnderlyingFunction(From->getType());
00781   if (!FromFunc || FromFunc->hasDependentExceptionSpec())
00782     return false;
00783 
00784   // Now we've got the correct types on both sides, check their compatibility.
00785   // This means that the source of the conversion can only throw a subset of
00786   // the exceptions of the target, and any exception specs on arguments or
00787   // return types must be equivalent.
00788   //
00789   // FIXME: If there is a nested dependent exception specification, we should
00790   // not be checking it here. This is fine:
00791   //   template<typename T> void f() {
00792   //     void (*p)(void (*) throw(T));
00793   //     void (*q)(void (*) throw(int)) = p;
00794   //   }
00795   // ... because it might be instantiated with T=int.
00796   return CheckExceptionSpecSubset(PDiag(diag::err_incompatible_exception_specs),
00797                                   PDiag(), ToFunc, 
00798                                   From->getSourceRange().getBegin(),
00799                                   FromFunc, SourceLocation());
00800 }
00801 
00802 bool Sema::CheckOverridingFunctionExceptionSpec(const CXXMethodDecl *New,
00803                                                 const CXXMethodDecl *Old) {
00804   if (getLangOpts().CPlusPlus11 && isa<CXXDestructorDecl>(New)) {
00805     // Don't check uninstantiated template destructors at all. We can only
00806     // synthesize correct specs after the template is instantiated.
00807     if (New->getParent()->isDependentType())
00808       return false;
00809     if (New->getParent()->isBeingDefined()) {
00810       // The destructor might be updated once the definition is finished. So
00811       // remember it and check later.
00812       DelayedDestructorExceptionSpecChecks.push_back(std::make_pair(
00813         cast<CXXDestructorDecl>(New), cast<CXXDestructorDecl>(Old)));
00814       return false;
00815     }
00816   }
00817   // If the exception specification hasn't been parsed yet, skip the check.
00818   // We'll get called again once it's been parsed.
00819   if (New->getType()->castAs<FunctionProtoType>()->getExceptionSpecType() ==
00820       EST_Unparsed)
00821     return false;
00822   unsigned DiagID = diag::err_override_exception_spec;
00823   if (getLangOpts().MicrosoftExt)
00824     DiagID = diag::ext_override_exception_spec;
00825   return CheckExceptionSpecSubset(PDiag(DiagID),
00826                                   PDiag(diag::note_overridden_virtual_function),
00827                                   Old->getType()->getAs<FunctionProtoType>(),
00828                                   Old->getLocation(),
00829                                   New->getType()->getAs<FunctionProtoType>(),
00830                                   New->getLocation());
00831 }
00832 
00833 static CanThrowResult canSubExprsThrow(Sema &S, const Expr *CE) {
00834   Expr *E = const_cast<Expr*>(CE);
00835   CanThrowResult R = CT_Cannot;
00836   for (Expr::child_range I = E->children(); I && R != CT_Can; ++I)
00837     R = mergeCanThrow(R, S.canThrow(cast<Expr>(*I)));
00838   return R;
00839 }
00840 
00841 static CanThrowResult canCalleeThrow(Sema &S, const Expr *E, const Decl *D) {
00842   assert(D && "Expected decl");
00843 
00844   // See if we can get a function type from the decl somehow.
00845   const ValueDecl *VD = dyn_cast<ValueDecl>(D);
00846   if (!VD) // If we have no clue what we're calling, assume the worst.
00847     return CT_Can;
00848 
00849   // As an extension, we assume that __attribute__((nothrow)) functions don't
00850   // throw.
00851   if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
00852     return CT_Cannot;
00853 
00854   QualType T = VD->getType();
00855   const FunctionProtoType *FT;
00856   if ((FT = T->getAs<FunctionProtoType>())) {
00857   } else if (const PointerType *PT = T->getAs<PointerType>())
00858     FT = PT->getPointeeType()->getAs<FunctionProtoType>();
00859   else if (const ReferenceType *RT = T->getAs<ReferenceType>())
00860     FT = RT->getPointeeType()->getAs<FunctionProtoType>();
00861   else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
00862     FT = MT->getPointeeType()->getAs<FunctionProtoType>();
00863   else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
00864     FT = BT->getPointeeType()->getAs<FunctionProtoType>();
00865 
00866   if (!FT)
00867     return CT_Can;
00868 
00869   FT = S.ResolveExceptionSpec(E->getLocStart(), FT);
00870   if (!FT)
00871     return CT_Can;
00872 
00873   return FT->isNothrow(S.Context) ? CT_Cannot : CT_Can;
00874 }
00875 
00876 static CanThrowResult canDynamicCastThrow(const CXXDynamicCastExpr *DC) {
00877   if (DC->isTypeDependent())
00878     return CT_Dependent;
00879 
00880   if (!DC->getTypeAsWritten()->isReferenceType())
00881     return CT_Cannot;
00882 
00883   if (DC->getSubExpr()->isTypeDependent())
00884     return CT_Dependent;
00885 
00886   return DC->getCastKind() == clang::CK_Dynamic? CT_Can : CT_Cannot;
00887 }
00888 
00889 static CanThrowResult canTypeidThrow(Sema &S, const CXXTypeidExpr *DC) {
00890   if (DC->isTypeOperand())
00891     return CT_Cannot;
00892 
00893   Expr *Op = DC->getExprOperand();
00894   if (Op->isTypeDependent())
00895     return CT_Dependent;
00896 
00897   const RecordType *RT = Op->getType()->getAs<RecordType>();
00898   if (!RT)
00899     return CT_Cannot;
00900 
00901   if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
00902     return CT_Cannot;
00903 
00904   if (Op->Classify(S.Context).isPRValue())
00905     return CT_Cannot;
00906 
00907   return CT_Can;
00908 }
00909 
00910 CanThrowResult Sema::canThrow(const Expr *E) {
00911   // C++ [expr.unary.noexcept]p3:
00912   //   [Can throw] if in a potentially-evaluated context the expression would
00913   //   contain:
00914   switch (E->getStmtClass()) {
00915   case Expr::CXXThrowExprClass:
00916     //   - a potentially evaluated throw-expression
00917     return CT_Can;
00918 
00919   case Expr::CXXDynamicCastExprClass: {
00920     //   - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
00921     //     where T is a reference type, that requires a run-time check
00922     CanThrowResult CT = canDynamicCastThrow(cast<CXXDynamicCastExpr>(E));
00923     if (CT == CT_Can)
00924       return CT;
00925     return mergeCanThrow(CT, canSubExprsThrow(*this, E));
00926   }
00927 
00928   case Expr::CXXTypeidExprClass:
00929     //   - a potentially evaluated typeid expression applied to a glvalue
00930     //     expression whose type is a polymorphic class type
00931     return canTypeidThrow(*this, cast<CXXTypeidExpr>(E));
00932 
00933     //   - a potentially evaluated call to a function, member function, function
00934     //     pointer, or member function pointer that does not have a non-throwing
00935     //     exception-specification
00936   case Expr::CallExprClass:
00937   case Expr::CXXMemberCallExprClass:
00938   case Expr::CXXOperatorCallExprClass:
00939   case Expr::UserDefinedLiteralClass: {
00940     const CallExpr *CE = cast<CallExpr>(E);
00941     CanThrowResult CT;
00942     if (E->isTypeDependent())
00943       CT = CT_Dependent;
00944     else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
00945       CT = CT_Cannot;
00946     else if (CE->getCalleeDecl())
00947       CT = canCalleeThrow(*this, E, CE->getCalleeDecl());
00948     else
00949       CT = CT_Can;
00950     if (CT == CT_Can)
00951       return CT;
00952     return mergeCanThrow(CT, canSubExprsThrow(*this, E));
00953   }
00954 
00955   case Expr::CXXConstructExprClass:
00956   case Expr::CXXTemporaryObjectExprClass: {
00957     CanThrowResult CT = canCalleeThrow(*this, E,
00958         cast<CXXConstructExpr>(E)->getConstructor());
00959     if (CT == CT_Can)
00960       return CT;
00961     return mergeCanThrow(CT, canSubExprsThrow(*this, E));
00962   }
00963 
00964   case Expr::LambdaExprClass: {
00965     const LambdaExpr *Lambda = cast<LambdaExpr>(E);
00966     CanThrowResult CT = CT_Cannot;
00967     for (LambdaExpr::capture_init_iterator Cap = Lambda->capture_init_begin(),
00968                                         CapEnd = Lambda->capture_init_end();
00969          Cap != CapEnd; ++Cap)
00970       CT = mergeCanThrow(CT, canThrow(*Cap));
00971     return CT;
00972   }
00973 
00974   case Expr::CXXNewExprClass: {
00975     CanThrowResult CT;
00976     if (E->isTypeDependent())
00977       CT = CT_Dependent;
00978     else
00979       CT = canCalleeThrow(*this, E, cast<CXXNewExpr>(E)->getOperatorNew());
00980     if (CT == CT_Can)
00981       return CT;
00982     return mergeCanThrow(CT, canSubExprsThrow(*this, E));
00983   }
00984 
00985   case Expr::CXXDeleteExprClass: {
00986     CanThrowResult CT;
00987     QualType DTy = cast<CXXDeleteExpr>(E)->getDestroyedType();
00988     if (DTy.isNull() || DTy->isDependentType()) {
00989       CT = CT_Dependent;
00990     } else {
00991       CT = canCalleeThrow(*this, E,
00992                           cast<CXXDeleteExpr>(E)->getOperatorDelete());
00993       if (const RecordType *RT = DTy->getAs<RecordType>()) {
00994         const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
00995         const CXXDestructorDecl *DD = RD->getDestructor();
00996         if (DD)
00997           CT = mergeCanThrow(CT, canCalleeThrow(*this, E, DD));
00998       }
00999       if (CT == CT_Can)
01000         return CT;
01001     }
01002     return mergeCanThrow(CT, canSubExprsThrow(*this, E));
01003   }
01004 
01005   case Expr::CXXBindTemporaryExprClass: {
01006     // The bound temporary has to be destroyed again, which might throw.
01007     CanThrowResult CT = canCalleeThrow(*this, E,
01008       cast<CXXBindTemporaryExpr>(E)->getTemporary()->getDestructor());
01009     if (CT == CT_Can)
01010       return CT;
01011     return mergeCanThrow(CT, canSubExprsThrow(*this, E));
01012   }
01013 
01014     // ObjC message sends are like function calls, but never have exception
01015     // specs.
01016   case Expr::ObjCMessageExprClass:
01017   case Expr::ObjCPropertyRefExprClass:
01018   case Expr::ObjCSubscriptRefExprClass:
01019     return CT_Can;
01020 
01021     // All the ObjC literals that are implemented as calls are
01022     // potentially throwing unless we decide to close off that
01023     // possibility.
01024   case Expr::ObjCArrayLiteralClass:
01025   case Expr::ObjCDictionaryLiteralClass:
01026   case Expr::ObjCBoxedExprClass:
01027     return CT_Can;
01028 
01029     // Many other things have subexpressions, so we have to test those.
01030     // Some are simple:
01031   case Expr::ConditionalOperatorClass:
01032   case Expr::CompoundLiteralExprClass:
01033   case Expr::CXXConstCastExprClass:
01034   case Expr::CXXReinterpretCastExprClass:
01035   case Expr::CXXStdInitializerListExprClass:
01036   case Expr::DesignatedInitExprClass:
01037   case Expr::ExprWithCleanupsClass:
01038   case Expr::ExtVectorElementExprClass:
01039   case Expr::InitListExprClass:
01040   case Expr::MemberExprClass:
01041   case Expr::ObjCIsaExprClass:
01042   case Expr::ObjCIvarRefExprClass:
01043   case Expr::ParenExprClass:
01044   case Expr::ParenListExprClass:
01045   case Expr::ShuffleVectorExprClass:
01046   case Expr::ConvertVectorExprClass:
01047   case Expr::VAArgExprClass:
01048     return canSubExprsThrow(*this, E);
01049 
01050     // Some might be dependent for other reasons.
01051   case Expr::ArraySubscriptExprClass:
01052   case Expr::BinaryOperatorClass:
01053   case Expr::CompoundAssignOperatorClass:
01054   case Expr::CStyleCastExprClass:
01055   case Expr::CXXStaticCastExprClass:
01056   case Expr::CXXFunctionalCastExprClass:
01057   case Expr::ImplicitCastExprClass:
01058   case Expr::MaterializeTemporaryExprClass:
01059   case Expr::UnaryOperatorClass: {
01060     CanThrowResult CT = E->isTypeDependent() ? CT_Dependent : CT_Cannot;
01061     return mergeCanThrow(CT, canSubExprsThrow(*this, E));
01062   }
01063 
01064     // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
01065   case Expr::StmtExprClass:
01066     return CT_Can;
01067 
01068   case Expr::CXXDefaultArgExprClass:
01069     return canThrow(cast<CXXDefaultArgExpr>(E)->getExpr());
01070 
01071   case Expr::CXXDefaultInitExprClass:
01072     return canThrow(cast<CXXDefaultInitExpr>(E)->getExpr());
01073 
01074   case Expr::ChooseExprClass:
01075     if (E->isTypeDependent() || E->isValueDependent())
01076       return CT_Dependent;
01077     return canThrow(cast<ChooseExpr>(E)->getChosenSubExpr());
01078 
01079   case Expr::GenericSelectionExprClass:
01080     if (cast<GenericSelectionExpr>(E)->isResultDependent())
01081       return CT_Dependent;
01082     return canThrow(cast<GenericSelectionExpr>(E)->getResultExpr());
01083 
01084     // Some expressions are always dependent.
01085   case Expr::CXXDependentScopeMemberExprClass:
01086   case Expr::CXXUnresolvedConstructExprClass:
01087   case Expr::DependentScopeDeclRefExprClass:
01088   case Expr::CXXFoldExprClass:
01089     return CT_Dependent;
01090 
01091   case Expr::AsTypeExprClass:
01092   case Expr::BinaryConditionalOperatorClass:
01093   case Expr::BlockExprClass:
01094   case Expr::CUDAKernelCallExprClass:
01095   case Expr::DeclRefExprClass:
01096   case Expr::ObjCBridgedCastExprClass:
01097   case Expr::ObjCIndirectCopyRestoreExprClass:
01098   case Expr::ObjCProtocolExprClass:
01099   case Expr::ObjCSelectorExprClass:
01100   case Expr::OffsetOfExprClass:
01101   case Expr::PackExpansionExprClass:
01102   case Expr::PseudoObjectExprClass:
01103   case Expr::SubstNonTypeTemplateParmExprClass:
01104   case Expr::SubstNonTypeTemplateParmPackExprClass:
01105   case Expr::FunctionParmPackExprClass:
01106   case Expr::UnaryExprOrTypeTraitExprClass:
01107   case Expr::UnresolvedLookupExprClass:
01108   case Expr::UnresolvedMemberExprClass:
01109   case Expr::TypoExprClass:
01110     // FIXME: Can any of the above throw?  If so, when?
01111     return CT_Cannot;
01112 
01113   case Expr::AddrLabelExprClass:
01114   case Expr::ArrayTypeTraitExprClass:
01115   case Expr::AtomicExprClass:
01116   case Expr::TypeTraitExprClass:
01117   case Expr::CXXBoolLiteralExprClass:
01118   case Expr::CXXNoexceptExprClass:
01119   case Expr::CXXNullPtrLiteralExprClass:
01120   case Expr::CXXPseudoDestructorExprClass:
01121   case Expr::CXXScalarValueInitExprClass:
01122   case Expr::CXXThisExprClass:
01123   case Expr::CXXUuidofExprClass:
01124   case Expr::CharacterLiteralClass:
01125   case Expr::ExpressionTraitExprClass:
01126   case Expr::FloatingLiteralClass:
01127   case Expr::GNUNullExprClass:
01128   case Expr::ImaginaryLiteralClass:
01129   case Expr::ImplicitValueInitExprClass:
01130   case Expr::IntegerLiteralClass:
01131   case Expr::ObjCEncodeExprClass:
01132   case Expr::ObjCStringLiteralClass:
01133   case Expr::ObjCBoolLiteralExprClass:
01134   case Expr::OpaqueValueExprClass:
01135   case Expr::PredefinedExprClass:
01136   case Expr::SizeOfPackExprClass:
01137   case Expr::StringLiteralClass:
01138     // These expressions can never throw.
01139     return CT_Cannot;
01140 
01141   case Expr::MSPropertyRefExprClass:
01142     llvm_unreachable("Invalid class for expression");
01143 
01144 #define STMT(CLASS, PARENT) case Expr::CLASS##Class:
01145 #define STMT_RANGE(Base, First, Last)
01146 #define LAST_STMT_RANGE(BASE, FIRST, LAST)
01147 #define EXPR(CLASS, PARENT)
01148 #define ABSTRACT_STMT(STMT)
01149 #include "clang/AST/StmtNodes.inc"
01150   case Expr::NoStmtClass:
01151     llvm_unreachable("Invalid class for expression");
01152   }
01153   llvm_unreachable("Bogus StmtClass");
01154 }
01155 
01156 } // end namespace clang