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