blob: b92fcbd2a0d9f4ddcd025c1c4d94ac1283ef9a87 [file] [log] [blame]
Sebastian Redl4915e632009-10-11 09:03:14 +00001//===--- SemaExceptionSpec.cpp - C++ Exception Specifications ---*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file provides Sema routines for C++ exception specification testing.
11//
12//===----------------------------------------------------------------------===//
13
John McCall83024632010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Richard Smith564417a2014-03-20 21:47:22 +000015#include "clang/AST/ASTMutationListener.h"
Sebastian Redl4915e632009-10-11 09:03:14 +000016#include "clang/AST/CXXInheritance.h"
17#include "clang/AST/Expr.h"
18#include "clang/AST/ExprCXX.h"
Douglas Gregord6bc5e62010-03-24 07:14:45 +000019#include "clang/AST/TypeLoc.h"
Douglas Gregorf40863c2010-02-12 07:32:17 +000020#include "clang/Basic/Diagnostic.h"
21#include "clang/Basic/SourceManager.h"
Sebastian Redl4915e632009-10-11 09:03:14 +000022#include "llvm/ADT/SmallPtrSet.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000023#include "llvm/ADT/SmallString.h"
Sebastian Redl4915e632009-10-11 09:03:14 +000024
25namespace clang {
26
27static const FunctionProtoType *GetUnderlyingFunction(QualType T)
28{
29 if (const PointerType *PtrTy = T->getAs<PointerType>())
30 T = PtrTy->getPointeeType();
31 else if (const ReferenceType *RefTy = T->getAs<ReferenceType>())
32 T = RefTy->getPointeeType();
Sebastian Redl075b21d2009-10-14 14:38:54 +000033 else if (const MemberPointerType *MPTy = T->getAs<MemberPointerType>())
34 T = MPTy->getPointeeType();
Sebastian Redl4915e632009-10-11 09:03:14 +000035 return T->getAs<FunctionProtoType>();
36}
37
38/// CheckSpecifiedExceptionType - Check if the given type is valid in an
39/// exception specification. Incomplete types, or pointers to incomplete types
40/// other than void are not allowed.
Richard Smith8606d752012-11-28 22:33:28 +000041///
42/// \param[in,out] T The exception type. This will be decayed to a pointer type
43/// when the input is an array or a function type.
44bool Sema::CheckSpecifiedExceptionType(QualType &T, const SourceRange &Range) {
Richard Smitha118c6a2012-11-28 22:52:42 +000045 // C++11 [except.spec]p2:
46 // A type cv T, "array of T", or "function returning T" denoted
Richard Smith8606d752012-11-28 22:33:28 +000047 // in an exception-specification is adjusted to type T, "pointer to T", or
48 // "pointer to function returning T", respectively.
Richard Smitha118c6a2012-11-28 22:52:42 +000049 //
50 // We also apply this rule in C++98.
Richard Smith8606d752012-11-28 22:33:28 +000051 if (T->isArrayType())
52 T = Context.getArrayDecayedType(T);
53 else if (T->isFunctionType())
54 T = Context.getPointerType(T);
Sebastian Redl4915e632009-10-11 09:03:14 +000055
Richard Smitha118c6a2012-11-28 22:52:42 +000056 int Kind = 0;
Richard Smith8606d752012-11-28 22:33:28 +000057 QualType PointeeT = T;
Richard Smitha118c6a2012-11-28 22:52:42 +000058 if (const PointerType *PT = T->getAs<PointerType>()) {
59 PointeeT = PT->getPointeeType();
60 Kind = 1;
Sebastian Redl4915e632009-10-11 09:03:14 +000061
Richard Smitha118c6a2012-11-28 22:52:42 +000062 // cv void* is explicitly permitted, despite being a pointer to an
63 // incomplete type.
64 if (PointeeT->isVoidType())
65 return false;
66 } else if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
67 PointeeT = RT->getPointeeType();
68 Kind = 2;
Richard Smith8606d752012-11-28 22:33:28 +000069
Richard Smitha118c6a2012-11-28 22:52:42 +000070 if (RT->isRValueReferenceType()) {
71 // C++11 [except.spec]p2:
72 // A type denoted in an exception-specification shall not denote [...]
73 // an rvalue reference type.
74 Diag(Range.getBegin(), diag::err_rref_in_exception_spec)
75 << T << Range;
76 return true;
77 }
78 }
79
80 // C++11 [except.spec]p2:
81 // A type denoted in an exception-specification shall not denote an
82 // incomplete type other than a class currently being defined [...].
83 // A type denoted in an exception-specification shall not denote a
84 // pointer or reference to an incomplete type, other than (cv) void* or a
85 // pointer or reference to a class currently being defined.
86 if (!(PointeeT->isRecordType() &&
87 PointeeT->getAs<RecordType>()->isBeingDefined()) &&
Richard Smith8606d752012-11-28 22:33:28 +000088 RequireCompleteType(Range.getBegin(), PointeeT,
Richard Smitha118c6a2012-11-28 22:52:42 +000089 diag::err_incomplete_in_exception_spec, Kind, Range))
Sebastian Redl7eb5d372009-10-14 14:59:48 +000090 return true;
Sebastian Redl4915e632009-10-11 09:03:14 +000091
92 return false;
93}
94
95/// CheckDistantExceptionSpec - Check if the given type is a pointer or pointer
96/// to member to a function with an exception specification. This means that
97/// it is invalid to add another level of indirection.
98bool Sema::CheckDistantExceptionSpec(QualType T) {
99 if (const PointerType *PT = T->getAs<PointerType>())
100 T = PT->getPointeeType();
101 else if (const MemberPointerType *PT = T->getAs<MemberPointerType>())
102 T = PT->getPointeeType();
103 else
104 return false;
105
106 const FunctionProtoType *FnT = T->getAs<FunctionProtoType>();
107 if (!FnT)
108 return false;
109
110 return FnT->hasExceptionSpec();
111}
112
Richard Smithf623c962012-04-17 00:58:00 +0000113const FunctionProtoType *
114Sema::ResolveExceptionSpec(SourceLocation Loc, const FunctionProtoType *FPT) {
Richard Smithd3b5c9082012-07-27 04:22:15 +0000115 if (!isUnresolvedExceptionSpec(FPT->getExceptionSpecType()))
Richard Smithf623c962012-04-17 00:58:00 +0000116 return FPT;
117
118 FunctionDecl *SourceDecl = FPT->getExceptionSpecDecl();
119 const FunctionProtoType *SourceFPT =
120 SourceDecl->getType()->castAs<FunctionProtoType>();
121
Richard Smithd3b5c9082012-07-27 04:22:15 +0000122 // If the exception specification has already been resolved, just return it.
123 if (!isUnresolvedExceptionSpec(SourceFPT->getExceptionSpecType()))
Richard Smithf623c962012-04-17 00:58:00 +0000124 return SourceFPT;
125
Richard Smithd3b5c9082012-07-27 04:22:15 +0000126 // Compute or instantiate the exception specification now.
Richard Smith3901dfe2013-03-27 00:22:47 +0000127 if (SourceFPT->getExceptionSpecType() == EST_Unevaluated)
Richard Smithd3b5c9082012-07-27 04:22:15 +0000128 EvaluateImplicitExceptionSpec(Loc, cast<CXXMethodDecl>(SourceDecl));
129 else
130 InstantiateExceptionSpec(Loc, SourceDecl);
Richard Smithf623c962012-04-17 00:58:00 +0000131
132 return SourceDecl->getType()->castAs<FunctionProtoType>();
133}
134
Richard Smith564417a2014-03-20 21:47:22 +0000135void Sema::UpdateExceptionSpec(FunctionDecl *FD,
136 const FunctionProtoType::ExtProtoInfo &EPI) {
137 const FunctionProtoType *Proto = FD->getType()->castAs<FunctionProtoType>();
138
139 // Overwrite the exception spec and rebuild the function type.
140 FunctionProtoType::ExtProtoInfo NewEPI = Proto->getExtProtoInfo();
141 NewEPI.ExceptionSpecType = EPI.ExceptionSpecType;
142 NewEPI.NumExceptions = EPI.NumExceptions;
143 NewEPI.Exceptions = EPI.Exceptions;
144 NewEPI.NoexceptExpr = EPI.NoexceptExpr;
145 FD->setType(Context.getFunctionType(Proto->getReturnType(),
146 Proto->getParamTypes(), NewEPI));
147
148 // If we've fully resolved the exception specification, notify listeners.
149 if (!isUnresolvedExceptionSpec(EPI.ExceptionSpecType))
150 if (auto *Listener = getASTMutationListener())
151 Listener->ResolvedExceptionSpec(FD);
152}
153
Richard Smith66f3ac92012-10-20 08:26:51 +0000154/// Determine whether a function has an implicitly-generated exception
Richard Smith1ee63522012-10-16 23:30:16 +0000155/// specification.
Richard Smith66f3ac92012-10-20 08:26:51 +0000156static bool hasImplicitExceptionSpec(FunctionDecl *Decl) {
157 if (!isa<CXXDestructorDecl>(Decl) &&
158 Decl->getDeclName().getCXXOverloadedOperator() != OO_Delete &&
159 Decl->getDeclName().getCXXOverloadedOperator() != OO_Array_Delete)
160 return false;
Richard Smith1ee63522012-10-16 23:30:16 +0000161
Richard Smithc7fb2252014-02-07 22:51:16 +0000162 // For a function that the user didn't declare:
163 // - if this is a destructor, its exception specification is implicit.
164 // - if this is 'operator delete' or 'operator delete[]', the exception
165 // specification is as-if an explicit exception specification was given
166 // (per [basic.stc.dynamic]p2).
Richard Smith66f3ac92012-10-20 08:26:51 +0000167 if (!Decl->getTypeSourceInfo())
Richard Smithc7fb2252014-02-07 22:51:16 +0000168 return isa<CXXDestructorDecl>(Decl);
Richard Smith66f3ac92012-10-20 08:26:51 +0000169
170 const FunctionProtoType *Ty =
171 Decl->getTypeSourceInfo()->getType()->getAs<FunctionProtoType>();
172 return !Ty->hasExceptionSpec();
Richard Smith1ee63522012-10-16 23:30:16 +0000173}
174
Douglas Gregorf40863c2010-02-12 07:32:17 +0000175bool Sema::CheckEquivalentExceptionSpec(FunctionDecl *Old, FunctionDecl *New) {
Sebastian Redlcb5dd002011-03-15 19:52:30 +0000176 OverloadedOperatorKind OO = New->getDeclName().getCXXOverloadedOperator();
177 bool IsOperatorNew = OO == OO_New || OO == OO_Array_New;
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000178 bool MissingExceptionSpecification = false;
Douglas Gregorf40863c2010-02-12 07:32:17 +0000179 bool MissingEmptyExceptionSpecification = false;
Hans Wennborg39a509a2014-02-05 02:37:58 +0000180
Francois Pichet13b4e682011-03-19 23:05:18 +0000181 unsigned DiagID = diag::err_mismatched_exception_spec;
Hans Wennborg39a509a2014-02-05 02:37:58 +0000182 bool ReturnValueOnError = true;
183 if (getLangOpts().MicrosoftExt) {
Richard Smith1b98ccc2014-07-19 01:39:17 +0000184 DiagID = diag::ext_mismatched_exception_spec;
Hans Wennborg39a509a2014-02-05 02:37:58 +0000185 ReturnValueOnError = false;
186 }
Richard Smithf623c962012-04-17 00:58:00 +0000187
Richard Smith1ee63522012-10-16 23:30:16 +0000188 // Check the types as written: they must match before any exception
189 // specification adjustment is applied.
190 if (!CheckEquivalentExceptionSpec(
191 PDiag(DiagID), PDiag(diag::note_previous_declaration),
Richard Smith66f3ac92012-10-20 08:26:51 +0000192 Old->getType()->getAs<FunctionProtoType>(), Old->getLocation(),
193 New->getType()->getAs<FunctionProtoType>(), New->getLocation(),
Richard Smith1ee63522012-10-16 23:30:16 +0000194 &MissingExceptionSpecification, &MissingEmptyExceptionSpecification,
Richard Smith66f3ac92012-10-20 08:26:51 +0000195 /*AllowNoexceptAllMatchWithNoSpec=*/true, IsOperatorNew)) {
196 // C++11 [except.spec]p4 [DR1492]:
197 // If a declaration of a function has an implicit
198 // exception-specification, other declarations of the function shall
199 // not specify an exception-specification.
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000200 if (getLangOpts().CPlusPlus11 &&
Richard Smith66f3ac92012-10-20 08:26:51 +0000201 hasImplicitExceptionSpec(Old) != hasImplicitExceptionSpec(New)) {
202 Diag(New->getLocation(), diag::ext_implicit_exception_spec_mismatch)
203 << hasImplicitExceptionSpec(Old);
204 if (!Old->getLocation().isInvalid())
205 Diag(Old->getLocation(), diag::note_previous_declaration);
206 }
Douglas Gregorf40863c2010-02-12 07:32:17 +0000207 return false;
Richard Smith66f3ac92012-10-20 08:26:51 +0000208 }
Douglas Gregorf40863c2010-02-12 07:32:17 +0000209
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000210 // The failure was something other than an missing exception
Hans Wennborg39a509a2014-02-05 02:37:58 +0000211 // specification; return an error, except in MS mode where this is a warning.
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000212 if (!MissingExceptionSpecification)
Hans Wennborg39a509a2014-02-05 02:37:58 +0000213 return ReturnValueOnError;
Douglas Gregorf40863c2010-02-12 07:32:17 +0000214
Richard Smith66f3ac92012-10-20 08:26:51 +0000215 const FunctionProtoType *NewProto =
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000216 New->getType()->castAs<FunctionProtoType>();
John McCalldb40c7f2010-12-14 08:05:40 +0000217
Douglas Gregorf40863c2010-02-12 07:32:17 +0000218 // The new function declaration is only missing an empty exception
219 // specification "throw()". If the throw() specification came from a
220 // function in a system header that has C linkage, just add an empty
221 // exception specification to the "new" declaration. This is an
222 // egregious workaround for glibc, which adds throw() specifications
223 // to many libc functions as an optimization. Unfortunately, that
224 // optimization isn't permitted by the C++ standard, so we're forced
225 // to work around it here.
John McCalldb40c7f2010-12-14 08:05:40 +0000226 if (MissingEmptyExceptionSpecification && NewProto &&
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000227 (Old->getLocation().isInvalid() ||
228 Context.getSourceManager().isInSystemHeader(Old->getLocation())) &&
Douglas Gregorf40863c2010-02-12 07:32:17 +0000229 Old->isExternC()) {
John McCalldb40c7f2010-12-14 08:05:40 +0000230 FunctionProtoType::ExtProtoInfo EPI = NewProto->getExtProtoInfo();
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000231 EPI.ExceptionSpecType = EST_DynamicNone;
Alp Toker314cc812014-01-25 16:55:45 +0000232 QualType NewType = Context.getFunctionType(NewProto->getReturnType(),
Alp Toker9cacbab2014-01-20 20:26:09 +0000233 NewProto->getParamTypes(), EPI);
Douglas Gregorf40863c2010-02-12 07:32:17 +0000234 New->setType(NewType);
235 return false;
236 }
237
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000238 const FunctionProtoType *OldProto =
239 Old->getType()->castAs<FunctionProtoType>();
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000240
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000241 FunctionProtoType::ExtProtoInfo EPI = NewProto->getExtProtoInfo();
242 EPI.ExceptionSpecType = OldProto->getExceptionSpecType();
243 if (EPI.ExceptionSpecType == EST_Dynamic) {
244 EPI.NumExceptions = OldProto->getNumExceptions();
245 EPI.Exceptions = OldProto->exception_begin();
246 } else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) {
247 // FIXME: We can't just take the expression from the old prototype. It
248 // likely contains references to the old prototype's parameters.
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000249 }
250
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000251 // Update the type of the function with the appropriate exception
252 // specification.
Alp Toker314cc812014-01-25 16:55:45 +0000253 QualType NewType = Context.getFunctionType(NewProto->getReturnType(),
Alp Toker9cacbab2014-01-20 20:26:09 +0000254 NewProto->getParamTypes(), EPI);
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000255 New->setType(NewType);
256
257 // Warn about the lack of exception specification.
258 SmallString<128> ExceptionSpecString;
259 llvm::raw_svector_ostream OS(ExceptionSpecString);
260 switch (OldProto->getExceptionSpecType()) {
261 case EST_DynamicNone:
262 OS << "throw()";
263 break;
264
265 case EST_Dynamic: {
266 OS << "throw(";
267 bool OnFirstException = true;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000268 for (const auto &E : OldProto->exceptions()) {
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000269 if (OnFirstException)
270 OnFirstException = false;
271 else
272 OS << ", ";
273
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000274 OS << E.getAsString(getPrintingPolicy());
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000275 }
276 OS << ")";
277 break;
278 }
279
280 case EST_BasicNoexcept:
281 OS << "noexcept";
282 break;
283
284 case EST_ComputedNoexcept:
285 OS << "noexcept(";
Richard Trieuddd01ce2014-06-09 22:53:25 +0000286 assert(OldProto->getNoexceptExpr() != nullptr && "Expected non-null Expr");
Craig Topperc3ec1492014-05-26 06:22:03 +0000287 OldProto->getNoexceptExpr()->printPretty(OS, nullptr, getPrintingPolicy());
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000288 OS << ")";
289 break;
290
291 default:
292 llvm_unreachable("This spec type is compatible with none.");
293 }
294 OS.flush();
295
296 SourceLocation FixItLoc;
297 if (TypeSourceInfo *TSInfo = New->getTypeSourceInfo()) {
298 TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens();
299 if (FunctionTypeLoc FTLoc = TL.getAs<FunctionTypeLoc>())
Alp Tokerb6cc5922014-05-03 03:45:55 +0000300 FixItLoc = getLocForEndOfToken(FTLoc.getLocalRangeEnd());
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000301 }
302
303 if (FixItLoc.isInvalid())
304 Diag(New->getLocation(), diag::warn_missing_exception_specification)
305 << New << OS.str();
306 else {
307 // FIXME: This will get more complicated with C++0x
308 // late-specified return types.
309 Diag(New->getLocation(), diag::warn_missing_exception_specification)
310 << New << OS.str()
311 << FixItHint::CreateInsertion(FixItLoc, " " + OS.str().str());
312 }
313
314 if (!Old->getLocation().isInvalid())
315 Diag(Old->getLocation(), diag::note_previous_declaration);
316
317 return false;
Douglas Gregorf40863c2010-02-12 07:32:17 +0000318}
319
Sebastian Redl4915e632009-10-11 09:03:14 +0000320/// CheckEquivalentExceptionSpec - Check if the two types have equivalent
321/// exception specifications. Exception specifications are equivalent if
322/// they allow exactly the same set of exception types. It does not matter how
323/// that is achieved. See C++ [except.spec]p2.
324bool Sema::CheckEquivalentExceptionSpec(
325 const FunctionProtoType *Old, SourceLocation OldLoc,
326 const FunctionProtoType *New, SourceLocation NewLoc) {
Francois Pichet13b4e682011-03-19 23:05:18 +0000327 unsigned DiagID = diag::err_mismatched_exception_spec;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000328 if (getLangOpts().MicrosoftExt)
Richard Smith1b98ccc2014-07-19 01:39:17 +0000329 DiagID = diag::ext_mismatched_exception_spec;
Hans Wennborg39a509a2014-02-05 02:37:58 +0000330 bool Result = CheckEquivalentExceptionSpec(PDiag(DiagID),
331 PDiag(diag::note_previous_declaration), Old, OldLoc, New, NewLoc);
332
333 // In Microsoft mode, mismatching exception specifications just cause a warning.
334 if (getLangOpts().MicrosoftExt)
335 return false;
336 return Result;
Sebastian Redl4915e632009-10-11 09:03:14 +0000337}
338
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000339/// CheckEquivalentExceptionSpec - Check if the two types have compatible
340/// exception specifications. See C++ [except.spec]p3.
Richard Smith66f3ac92012-10-20 08:26:51 +0000341///
342/// \return \c false if the exception specifications match, \c true if there is
343/// a problem. If \c true is returned, either a diagnostic has already been
344/// produced or \c *MissingExceptionSpecification is set to \c true.
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000345bool Sema::CheckEquivalentExceptionSpec(const PartialDiagnostic &DiagID,
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000346 const PartialDiagnostic & NoteID,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000347 const FunctionProtoType *Old,
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000348 SourceLocation OldLoc,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000349 const FunctionProtoType *New,
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000350 SourceLocation NewLoc,
351 bool *MissingExceptionSpecification,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000352 bool*MissingEmptyExceptionSpecification,
Sebastian Redlcb5dd002011-03-15 19:52:30 +0000353 bool AllowNoexceptAllMatchWithNoSpec,
354 bool IsOperatorNew) {
John McCallf9c94092010-05-28 08:37:35 +0000355 // Just completely ignore this under -fno-exceptions.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000356 if (!getLangOpts().CXXExceptions)
John McCallf9c94092010-05-28 08:37:35 +0000357 return false;
358
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000359 if (MissingExceptionSpecification)
360 *MissingExceptionSpecification = false;
361
Douglas Gregorf40863c2010-02-12 07:32:17 +0000362 if (MissingEmptyExceptionSpecification)
363 *MissingEmptyExceptionSpecification = false;
364
Richard Smithf623c962012-04-17 00:58:00 +0000365 Old = ResolveExceptionSpec(NewLoc, Old);
366 if (!Old)
367 return false;
368 New = ResolveExceptionSpec(NewLoc, New);
369 if (!New)
370 return false;
371
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000372 // C++0x [except.spec]p3: Two exception-specifications are compatible if:
373 // - both are non-throwing, regardless of their form,
374 // - both have the form noexcept(constant-expression) and the constant-
375 // expressions are equivalent,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000376 // - both are dynamic-exception-specifications that have the same set of
377 // adjusted types.
378 //
379 // C++0x [except.spec]p12: An exception-specifcation is non-throwing if it is
380 // of the form throw(), noexcept, or noexcept(constant-expression) where the
381 // constant-expression yields true.
382 //
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000383 // C++0x [except.spec]p4: If any declaration of a function has an exception-
384 // specifier that is not a noexcept-specification allowing all exceptions,
385 // all declarations [...] of that function shall have a compatible
386 // exception-specification.
387 //
388 // That last point basically means that noexcept(false) matches no spec.
389 // It's considered when AllowNoexceptAllMatchWithNoSpec is true.
390
391 ExceptionSpecificationType OldEST = Old->getExceptionSpecType();
392 ExceptionSpecificationType NewEST = New->getExceptionSpecType();
393
Richard Smithd3b5c9082012-07-27 04:22:15 +0000394 assert(!isUnresolvedExceptionSpec(OldEST) &&
395 !isUnresolvedExceptionSpec(NewEST) &&
Richard Smith938f40b2011-06-11 17:19:42 +0000396 "Shouldn't see unknown exception specifications here");
397
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000398 // Shortcut the case where both have no spec.
399 if (OldEST == EST_None && NewEST == EST_None)
400 return false;
401
Sebastian Redl31ad7542011-03-13 17:09:40 +0000402 FunctionProtoType::NoexceptResult OldNR = Old->getNoexceptSpec(Context);
403 FunctionProtoType::NoexceptResult NewNR = New->getNoexceptSpec(Context);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000404 if (OldNR == FunctionProtoType::NR_BadNoexcept ||
405 NewNR == FunctionProtoType::NR_BadNoexcept)
406 return false;
407
408 // Dependent noexcept specifiers are compatible with each other, but nothing
409 // else.
410 // One noexcept is compatible with another if the argument is the same
411 if (OldNR == NewNR &&
412 OldNR != FunctionProtoType::NR_NoNoexcept &&
413 NewNR != FunctionProtoType::NR_NoNoexcept)
414 return false;
415 if (OldNR != NewNR &&
416 OldNR != FunctionProtoType::NR_NoNoexcept &&
417 NewNR != FunctionProtoType::NR_NoNoexcept) {
418 Diag(NewLoc, DiagID);
419 if (NoteID.getDiagID() != 0)
420 Diag(OldLoc, NoteID);
421 return true;
Douglas Gregorf62c5292010-08-30 15:04:51 +0000422 }
423
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000424 // The MS extension throw(...) is compatible with itself.
425 if (OldEST == EST_MSAny && NewEST == EST_MSAny)
Sebastian Redl4915e632009-10-11 09:03:14 +0000426 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000427
428 // It's also compatible with no spec.
429 if ((OldEST == EST_None && NewEST == EST_MSAny) ||
430 (OldEST == EST_MSAny && NewEST == EST_None))
431 return false;
432
433 // It's also compatible with noexcept(false).
434 if (OldEST == EST_MSAny && NewNR == FunctionProtoType::NR_Throw)
435 return false;
436 if (NewEST == EST_MSAny && OldNR == FunctionProtoType::NR_Throw)
437 return false;
438
439 // As described above, noexcept(false) matches no spec only for functions.
440 if (AllowNoexceptAllMatchWithNoSpec) {
441 if (OldEST == EST_None && NewNR == FunctionProtoType::NR_Throw)
442 return false;
443 if (NewEST == EST_None && OldNR == FunctionProtoType::NR_Throw)
444 return false;
445 }
446
447 // Any non-throwing specifications are compatible.
448 bool OldNonThrowing = OldNR == FunctionProtoType::NR_Nothrow ||
449 OldEST == EST_DynamicNone;
450 bool NewNonThrowing = NewNR == FunctionProtoType::NR_Nothrow ||
451 NewEST == EST_DynamicNone;
452 if (OldNonThrowing && NewNonThrowing)
453 return false;
454
Sebastian Redlcb5dd002011-03-15 19:52:30 +0000455 // As a special compatibility feature, under C++0x we accept no spec and
456 // throw(std::bad_alloc) as equivalent for operator new and operator new[].
457 // This is because the implicit declaration changed, but old code would break.
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000458 if (getLangOpts().CPlusPlus11 && IsOperatorNew) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000459 const FunctionProtoType *WithExceptions = nullptr;
Sebastian Redlcb5dd002011-03-15 19:52:30 +0000460 if (OldEST == EST_None && NewEST == EST_Dynamic)
461 WithExceptions = New;
462 else if (OldEST == EST_Dynamic && NewEST == EST_None)
463 WithExceptions = Old;
464 if (WithExceptions && WithExceptions->getNumExceptions() == 1) {
465 // One has no spec, the other throw(something). If that something is
466 // std::bad_alloc, all conditions are met.
467 QualType Exception = *WithExceptions->exception_begin();
468 if (CXXRecordDecl *ExRecord = Exception->getAsCXXRecordDecl()) {
469 IdentifierInfo* Name = ExRecord->getIdentifier();
470 if (Name && Name->getName() == "bad_alloc") {
471 // It's called bad_alloc, but is it in std?
Richard Trieuc771d5d2014-05-28 02:16:01 +0000472 if (ExRecord->isInStdNamespace()) {
473 return false;
Sebastian Redlcb5dd002011-03-15 19:52:30 +0000474 }
475 }
476 }
477 }
478 }
479
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000480 // At this point, the only remaining valid case is two matching dynamic
481 // specifications. We return here unless both specifications are dynamic.
482 if (OldEST != EST_Dynamic || NewEST != EST_Dynamic) {
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000483 if (MissingExceptionSpecification && Old->hasExceptionSpec() &&
Douglas Gregorf40863c2010-02-12 07:32:17 +0000484 !New->hasExceptionSpec()) {
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000485 // The old type has an exception specification of some sort, but
486 // the new type does not.
487 *MissingExceptionSpecification = true;
488
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000489 if (MissingEmptyExceptionSpecification && OldNonThrowing) {
490 // The old type has a throw() or noexcept(true) exception specification
491 // and the new type has no exception specification, and the caller asked
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000492 // to handle this itself.
493 *MissingEmptyExceptionSpecification = true;
494 }
495
Douglas Gregorf40863c2010-02-12 07:32:17 +0000496 return true;
497 }
498
Sebastian Redl4915e632009-10-11 09:03:14 +0000499 Diag(NewLoc, DiagID);
Sebastian Redla44822f2009-10-14 16:09:29 +0000500 if (NoteID.getDiagID() != 0)
Sebastian Redl4915e632009-10-11 09:03:14 +0000501 Diag(OldLoc, NoteID);
502 return true;
503 }
504
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000505 assert(OldEST == EST_Dynamic && NewEST == EST_Dynamic &&
506 "Exception compatibility logic error: non-dynamic spec slipped through.");
507
Sebastian Redl4915e632009-10-11 09:03:14 +0000508 bool Success = true;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000509 // Both have a dynamic exception spec. Collect the first set, then compare
Sebastian Redl4915e632009-10-11 09:03:14 +0000510 // to the second.
Sebastian Redl184edca2009-10-14 15:06:25 +0000511 llvm::SmallPtrSet<CanQualType, 8> OldTypes, NewTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000512 for (const auto &I : Old->exceptions())
513 OldTypes.insert(Context.getCanonicalType(I).getUnqualifiedType());
Sebastian Redl4915e632009-10-11 09:03:14 +0000514
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000515 for (const auto &I : New->exceptions()) {
516 CanQualType TypePtr = Context.getCanonicalType(I).getUnqualifiedType();
Sebastian Redl6e4c8712009-10-11 09:11:23 +0000517 if(OldTypes.count(TypePtr))
518 NewTypes.insert(TypePtr);
519 else
520 Success = false;
521 }
Sebastian Redl4915e632009-10-11 09:03:14 +0000522
Sebastian Redl6e4c8712009-10-11 09:11:23 +0000523 Success = Success && OldTypes.size() == NewTypes.size();
Sebastian Redl4915e632009-10-11 09:03:14 +0000524
525 if (Success) {
526 return false;
527 }
528 Diag(NewLoc, DiagID);
Sebastian Redla44822f2009-10-14 16:09:29 +0000529 if (NoteID.getDiagID() != 0)
Sebastian Redl4915e632009-10-11 09:03:14 +0000530 Diag(OldLoc, NoteID);
531 return true;
532}
533
534/// CheckExceptionSpecSubset - Check whether the second function type's
535/// exception specification is a subset (or equivalent) of the first function
536/// type. This is used by override and pointer assignment checks.
Sebastian Redla44822f2009-10-14 16:09:29 +0000537bool Sema::CheckExceptionSpecSubset(
538 const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID,
Sebastian Redl4915e632009-10-11 09:03:14 +0000539 const FunctionProtoType *Superset, SourceLocation SuperLoc,
540 const FunctionProtoType *Subset, SourceLocation SubLoc) {
John McCallf9c94092010-05-28 08:37:35 +0000541
542 // Just auto-succeed under -fno-exceptions.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000543 if (!getLangOpts().CXXExceptions)
John McCallf9c94092010-05-28 08:37:35 +0000544 return false;
545
Sebastian Redl4915e632009-10-11 09:03:14 +0000546 // FIXME: As usual, we could be more specific in our error messages, but
547 // that better waits until we've got types with source locations.
548
549 if (!SubLoc.isValid())
550 SubLoc = SuperLoc;
551
Richard Smithf623c962012-04-17 00:58:00 +0000552 // Resolve the exception specifications, if needed.
553 Superset = ResolveExceptionSpec(SuperLoc, Superset);
554 if (!Superset)
555 return false;
556 Subset = ResolveExceptionSpec(SubLoc, Subset);
557 if (!Subset)
558 return false;
559
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000560 ExceptionSpecificationType SuperEST = Superset->getExceptionSpecType();
561
Sebastian Redl4915e632009-10-11 09:03:14 +0000562 // If superset contains everything, we're done.
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000563 if (SuperEST == EST_None || SuperEST == EST_MSAny)
Sebastian Redl4915e632009-10-11 09:03:14 +0000564 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
565
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000566 // If there are dependent noexcept specs, assume everything is fine. Unlike
567 // with the equivalency check, this is safe in this case, because we don't
568 // want to merge declarations. Checks after instantiation will catch any
569 // omissions we make here.
570 // We also shortcut checking if a noexcept expression was bad.
571
Sebastian Redl31ad7542011-03-13 17:09:40 +0000572 FunctionProtoType::NoexceptResult SuperNR =Superset->getNoexceptSpec(Context);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000573 if (SuperNR == FunctionProtoType::NR_BadNoexcept ||
574 SuperNR == FunctionProtoType::NR_Dependent)
575 return false;
576
577 // Another case of the superset containing everything.
578 if (SuperNR == FunctionProtoType::NR_Throw)
579 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
580
581 ExceptionSpecificationType SubEST = Subset->getExceptionSpecType();
582
Richard Smithd3b5c9082012-07-27 04:22:15 +0000583 assert(!isUnresolvedExceptionSpec(SuperEST) &&
584 !isUnresolvedExceptionSpec(SubEST) &&
Richard Smith938f40b2011-06-11 17:19:42 +0000585 "Shouldn't see unknown exception specifications here");
586
Sebastian Redl4915e632009-10-11 09:03:14 +0000587 // It does not. If the subset contains everything, we've failed.
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000588 if (SubEST == EST_None || SubEST == EST_MSAny) {
Sebastian Redl4915e632009-10-11 09:03:14 +0000589 Diag(SubLoc, DiagID);
Sebastian Redla44822f2009-10-14 16:09:29 +0000590 if (NoteID.getDiagID() != 0)
Sebastian Redl4915e632009-10-11 09:03:14 +0000591 Diag(SuperLoc, NoteID);
592 return true;
593 }
594
Sebastian Redl31ad7542011-03-13 17:09:40 +0000595 FunctionProtoType::NoexceptResult SubNR = Subset->getNoexceptSpec(Context);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000596 if (SubNR == FunctionProtoType::NR_BadNoexcept ||
597 SubNR == FunctionProtoType::NR_Dependent)
598 return false;
599
600 // Another case of the subset containing everything.
601 if (SubNR == FunctionProtoType::NR_Throw) {
602 Diag(SubLoc, DiagID);
603 if (NoteID.getDiagID() != 0)
604 Diag(SuperLoc, NoteID);
605 return true;
606 }
607
608 // If the subset contains nothing, we're done.
609 if (SubEST == EST_DynamicNone || SubNR == FunctionProtoType::NR_Nothrow)
610 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
611
612 // Otherwise, if the superset contains nothing, we've failed.
613 if (SuperEST == EST_DynamicNone || SuperNR == FunctionProtoType::NR_Nothrow) {
614 Diag(SubLoc, DiagID);
615 if (NoteID.getDiagID() != 0)
616 Diag(SuperLoc, NoteID);
617 return true;
618 }
619
620 assert(SuperEST == EST_Dynamic && SubEST == EST_Dynamic &&
621 "Exception spec subset: non-dynamic case slipped through.");
622
623 // Neither contains everything or nothing. Do a proper comparison.
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000624 for (const auto &SubI : Subset->exceptions()) {
Sebastian Redl4915e632009-10-11 09:03:14 +0000625 // Take one type from the subset.
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000626 QualType CanonicalSubT = Context.getCanonicalType(SubI);
Sebastian Redl075b21d2009-10-14 14:38:54 +0000627 // Unwrap pointers and references so that we can do checks within a class
628 // hierarchy. Don't unwrap member pointers; they don't have hierarchy
629 // conversions on the pointee.
Sebastian Redl4915e632009-10-11 09:03:14 +0000630 bool SubIsPointer = false;
631 if (const ReferenceType *RefTy = CanonicalSubT->getAs<ReferenceType>())
632 CanonicalSubT = RefTy->getPointeeType();
633 if (const PointerType *PtrTy = CanonicalSubT->getAs<PointerType>()) {
634 CanonicalSubT = PtrTy->getPointeeType();
635 SubIsPointer = true;
636 }
637 bool SubIsClass = CanonicalSubT->isRecordType();
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +0000638 CanonicalSubT = CanonicalSubT.getLocalUnqualifiedType();
Sebastian Redl4915e632009-10-11 09:03:14 +0000639
640 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
641 /*DetectVirtual=*/false);
642
643 bool Contained = false;
644 // Make sure it's in the superset.
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000645 for (const auto &SuperI : Superset->exceptions()) {
646 QualType CanonicalSuperT = Context.getCanonicalType(SuperI);
Sebastian Redl4915e632009-10-11 09:03:14 +0000647 // SubT must be SuperT or derived from it, or pointer or reference to
648 // such types.
649 if (const ReferenceType *RefTy = CanonicalSuperT->getAs<ReferenceType>())
650 CanonicalSuperT = RefTy->getPointeeType();
651 if (SubIsPointer) {
652 if (const PointerType *PtrTy = CanonicalSuperT->getAs<PointerType>())
653 CanonicalSuperT = PtrTy->getPointeeType();
654 else {
655 continue;
656 }
657 }
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +0000658 CanonicalSuperT = CanonicalSuperT.getLocalUnqualifiedType();
Sebastian Redl4915e632009-10-11 09:03:14 +0000659 // If the types are the same, move on to the next type in the subset.
660 if (CanonicalSubT == CanonicalSuperT) {
661 Contained = true;
662 break;
663 }
664
665 // Otherwise we need to check the inheritance.
666 if (!SubIsClass || !CanonicalSuperT->isRecordType())
667 continue;
668
669 Paths.clear();
670 if (!IsDerivedFrom(CanonicalSubT, CanonicalSuperT, Paths))
671 continue;
672
Douglas Gregor27ac4292010-05-21 20:29:55 +0000673 if (Paths.isAmbiguous(Context.getCanonicalType(CanonicalSuperT)))
Sebastian Redl4915e632009-10-11 09:03:14 +0000674 continue;
675
John McCall5b0829a2010-02-10 09:31:12 +0000676 // Do this check from a context without privileges.
John McCall1064d7e2010-03-16 05:22:47 +0000677 switch (CheckBaseClassAccess(SourceLocation(),
John McCall5b0829a2010-02-10 09:31:12 +0000678 CanonicalSuperT, CanonicalSubT,
679 Paths.front(),
John McCall1064d7e2010-03-16 05:22:47 +0000680 /*Diagnostic*/ 0,
John McCall5b0829a2010-02-10 09:31:12 +0000681 /*ForceCheck*/ true,
John McCall1064d7e2010-03-16 05:22:47 +0000682 /*ForceUnprivileged*/ true)) {
John McCall5b0829a2010-02-10 09:31:12 +0000683 case AR_accessible: break;
684 case AR_inaccessible: continue;
685 case AR_dependent:
686 llvm_unreachable("access check dependent for unprivileged context");
John McCall5b0829a2010-02-10 09:31:12 +0000687 case AR_delayed:
688 llvm_unreachable("access check delayed in non-declaration");
John McCall5b0829a2010-02-10 09:31:12 +0000689 }
Sebastian Redl4915e632009-10-11 09:03:14 +0000690
691 Contained = true;
692 break;
693 }
694 if (!Contained) {
695 Diag(SubLoc, DiagID);
Sebastian Redla44822f2009-10-14 16:09:29 +0000696 if (NoteID.getDiagID() != 0)
Sebastian Redl4915e632009-10-11 09:03:14 +0000697 Diag(SuperLoc, NoteID);
698 return true;
699 }
700 }
701 // We've run half the gauntlet.
702 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
703}
704
705static bool CheckSpecForTypesEquivalent(Sema &S,
Sebastian Redla44822f2009-10-14 16:09:29 +0000706 const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID,
Sebastian Redl4915e632009-10-11 09:03:14 +0000707 QualType Target, SourceLocation TargetLoc,
708 QualType Source, SourceLocation SourceLoc)
709{
710 const FunctionProtoType *TFunc = GetUnderlyingFunction(Target);
711 if (!TFunc)
712 return false;
713 const FunctionProtoType *SFunc = GetUnderlyingFunction(Source);
714 if (!SFunc)
715 return false;
716
717 return S.CheckEquivalentExceptionSpec(DiagID, NoteID, TFunc, TargetLoc,
718 SFunc, SourceLoc);
719}
720
721/// CheckParamExceptionSpec - Check if the parameter and return types of the
722/// two functions have equivalent exception specs. This is part of the
723/// assignment and override compatibility check. We do not check the parameters
724/// of parameter function pointers recursively, as no sane programmer would
725/// even be able to write such a function type.
Sebastian Redla44822f2009-10-14 16:09:29 +0000726bool Sema::CheckParamExceptionSpec(const PartialDiagnostic & NoteID,
Sebastian Redl4915e632009-10-11 09:03:14 +0000727 const FunctionProtoType *Target, SourceLocation TargetLoc,
728 const FunctionProtoType *Source, SourceLocation SourceLoc)
729{
Alp Toker314cc812014-01-25 16:55:45 +0000730 if (CheckSpecForTypesEquivalent(
731 *this, PDiag(diag::err_deep_exception_specs_differ) << 0, PDiag(),
732 Target->getReturnType(), TargetLoc, Source->getReturnType(),
733 SourceLoc))
Sebastian Redl4915e632009-10-11 09:03:14 +0000734 return true;
735
Sebastian Redla44822f2009-10-14 16:09:29 +0000736 // We shouldn't even be testing this unless the arguments are otherwise
Sebastian Redl4915e632009-10-11 09:03:14 +0000737 // compatible.
Alp Toker9cacbab2014-01-20 20:26:09 +0000738 assert(Target->getNumParams() == Source->getNumParams() &&
Sebastian Redl4915e632009-10-11 09:03:14 +0000739 "Functions have different argument counts.");
Alp Toker9cacbab2014-01-20 20:26:09 +0000740 for (unsigned i = 0, E = Target->getNumParams(); i != E; ++i) {
741 if (CheckSpecForTypesEquivalent(
742 *this, PDiag(diag::err_deep_exception_specs_differ) << 1, PDiag(),
743 Target->getParamType(i), TargetLoc, Source->getParamType(i),
744 SourceLoc))
Sebastian Redl4915e632009-10-11 09:03:14 +0000745 return true;
746 }
747 return false;
748}
749
750bool Sema::CheckExceptionSpecCompatibility(Expr *From, QualType ToType)
751{
752 // First we check for applicability.
753 // Target type must be a function, function pointer or function reference.
754 const FunctionProtoType *ToFunc = GetUnderlyingFunction(ToType);
755 if (!ToFunc)
756 return false;
757
758 // SourceType must be a function or function pointer.
759 const FunctionProtoType *FromFunc = GetUnderlyingFunction(From->getType());
760 if (!FromFunc)
761 return false;
762
763 // Now we've got the correct types on both sides, check their compatibility.
764 // This means that the source of the conversion can only throw a subset of
765 // the exceptions of the target, and any exception specs on arguments or
766 // return types must be equivalent.
Douglas Gregor89336232010-03-29 23:34:08 +0000767 return CheckExceptionSpecSubset(PDiag(diag::err_incompatible_exception_specs),
768 PDiag(), ToFunc,
769 From->getSourceRange().getBegin(),
Sebastian Redl4915e632009-10-11 09:03:14 +0000770 FromFunc, SourceLocation());
771}
772
773bool Sema::CheckOverridingFunctionExceptionSpec(const CXXMethodDecl *New,
774 const CXXMethodDecl *Old) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000775 if (getLangOpts().CPlusPlus11 && isa<CXXDestructorDecl>(New)) {
Sebastian Redl645d9582011-05-20 05:57:18 +0000776 // Don't check uninstantiated template destructors at all. We can only
777 // synthesize correct specs after the template is instantiated.
778 if (New->getParent()->isDependentType())
779 return false;
780 if (New->getParent()->isBeingDefined()) {
781 // The destructor might be updated once the definition is finished. So
782 // remember it and check later.
783 DelayedDestructorExceptionSpecChecks.push_back(std::make_pair(
784 cast<CXXDestructorDecl>(New), cast<CXXDestructorDecl>(Old)));
785 return false;
786 }
Sebastian Redl623ea822011-05-19 05:13:44 +0000787 }
Francois Picheta8032e92011-05-24 02:11:43 +0000788 unsigned DiagID = diag::err_override_exception_spec;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000789 if (getLangOpts().MicrosoftExt)
Richard Smith1b98ccc2014-07-19 01:39:17 +0000790 DiagID = diag::ext_override_exception_spec;
Francois Picheta8032e92011-05-24 02:11:43 +0000791 return CheckExceptionSpecSubset(PDiag(DiagID),
Douglas Gregor89336232010-03-29 23:34:08 +0000792 PDiag(diag::note_overridden_virtual_function),
Sebastian Redl4915e632009-10-11 09:03:14 +0000793 Old->getType()->getAs<FunctionProtoType>(),
794 Old->getLocation(),
795 New->getType()->getAs<FunctionProtoType>(),
796 New->getLocation());
797}
798
Richard Smithf623c962012-04-17 00:58:00 +0000799static CanThrowResult canSubExprsThrow(Sema &S, const Expr *CE) {
800 Expr *E = const_cast<Expr*>(CE);
801 CanThrowResult R = CT_Cannot;
802 for (Expr::child_range I = E->children(); I && R != CT_Can; ++I)
803 R = mergeCanThrow(R, S.canThrow(cast<Expr>(*I)));
804 return R;
805}
806
Eli Friedman0423b762013-06-25 01:24:22 +0000807static CanThrowResult canCalleeThrow(Sema &S, const Expr *E, const Decl *D) {
808 assert(D && "Expected decl");
Richard Smithf623c962012-04-17 00:58:00 +0000809
810 // See if we can get a function type from the decl somehow.
811 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
812 if (!VD) // If we have no clue what we're calling, assume the worst.
813 return CT_Can;
814
815 // As an extension, we assume that __attribute__((nothrow)) functions don't
816 // throw.
817 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
818 return CT_Cannot;
819
820 QualType T = VD->getType();
821 const FunctionProtoType *FT;
822 if ((FT = T->getAs<FunctionProtoType>())) {
823 } else if (const PointerType *PT = T->getAs<PointerType>())
824 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
825 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
826 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
827 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
828 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
829 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
830 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
831
832 if (!FT)
833 return CT_Can;
834
835 FT = S.ResolveExceptionSpec(E->getLocStart(), FT);
836 if (!FT)
837 return CT_Can;
838
Richard Smithf623c962012-04-17 00:58:00 +0000839 return FT->isNothrow(S.Context) ? CT_Cannot : CT_Can;
840}
841
842static CanThrowResult canDynamicCastThrow(const CXXDynamicCastExpr *DC) {
843 if (DC->isTypeDependent())
844 return CT_Dependent;
845
846 if (!DC->getTypeAsWritten()->isReferenceType())
847 return CT_Cannot;
848
849 if (DC->getSubExpr()->isTypeDependent())
850 return CT_Dependent;
851
852 return DC->getCastKind() == clang::CK_Dynamic? CT_Can : CT_Cannot;
853}
854
855static CanThrowResult canTypeidThrow(Sema &S, const CXXTypeidExpr *DC) {
856 if (DC->isTypeOperand())
857 return CT_Cannot;
858
859 Expr *Op = DC->getExprOperand();
860 if (Op->isTypeDependent())
861 return CT_Dependent;
862
863 const RecordType *RT = Op->getType()->getAs<RecordType>();
864 if (!RT)
865 return CT_Cannot;
866
867 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
868 return CT_Cannot;
869
870 if (Op->Classify(S.Context).isPRValue())
871 return CT_Cannot;
872
873 return CT_Can;
874}
875
876CanThrowResult Sema::canThrow(const Expr *E) {
877 // C++ [expr.unary.noexcept]p3:
878 // [Can throw] if in a potentially-evaluated context the expression would
879 // contain:
880 switch (E->getStmtClass()) {
881 case Expr::CXXThrowExprClass:
882 // - a potentially evaluated throw-expression
883 return CT_Can;
884
885 case Expr::CXXDynamicCastExprClass: {
886 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
887 // where T is a reference type, that requires a run-time check
888 CanThrowResult CT = canDynamicCastThrow(cast<CXXDynamicCastExpr>(E));
889 if (CT == CT_Can)
890 return CT;
891 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
892 }
893
894 case Expr::CXXTypeidExprClass:
895 // - a potentially evaluated typeid expression applied to a glvalue
896 // expression whose type is a polymorphic class type
897 return canTypeidThrow(*this, cast<CXXTypeidExpr>(E));
898
899 // - a potentially evaluated call to a function, member function, function
900 // pointer, or member function pointer that does not have a non-throwing
901 // exception-specification
902 case Expr::CallExprClass:
903 case Expr::CXXMemberCallExprClass:
904 case Expr::CXXOperatorCallExprClass:
905 case Expr::UserDefinedLiteralClass: {
906 const CallExpr *CE = cast<CallExpr>(E);
907 CanThrowResult CT;
908 if (E->isTypeDependent())
909 CT = CT_Dependent;
910 else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
911 CT = CT_Cannot;
Eli Friedman5a8738f2013-06-25 01:55:41 +0000912 else if (CE->getCalleeDecl())
Richard Smithf623c962012-04-17 00:58:00 +0000913 CT = canCalleeThrow(*this, E, CE->getCalleeDecl());
Eli Friedman5a8738f2013-06-25 01:55:41 +0000914 else
915 CT = CT_Can;
Richard Smithf623c962012-04-17 00:58:00 +0000916 if (CT == CT_Can)
917 return CT;
918 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
919 }
920
921 case Expr::CXXConstructExprClass:
922 case Expr::CXXTemporaryObjectExprClass: {
923 CanThrowResult CT = canCalleeThrow(*this, E,
924 cast<CXXConstructExpr>(E)->getConstructor());
925 if (CT == CT_Can)
926 return CT;
927 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
928 }
929
930 case Expr::LambdaExprClass: {
931 const LambdaExpr *Lambda = cast<LambdaExpr>(E);
932 CanThrowResult CT = CT_Cannot;
933 for (LambdaExpr::capture_init_iterator Cap = Lambda->capture_init_begin(),
934 CapEnd = Lambda->capture_init_end();
935 Cap != CapEnd; ++Cap)
936 CT = mergeCanThrow(CT, canThrow(*Cap));
937 return CT;
938 }
939
940 case Expr::CXXNewExprClass: {
941 CanThrowResult CT;
942 if (E->isTypeDependent())
943 CT = CT_Dependent;
944 else
945 CT = canCalleeThrow(*this, E, cast<CXXNewExpr>(E)->getOperatorNew());
946 if (CT == CT_Can)
947 return CT;
948 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
949 }
950
951 case Expr::CXXDeleteExprClass: {
952 CanThrowResult CT;
953 QualType DTy = cast<CXXDeleteExpr>(E)->getDestroyedType();
954 if (DTy.isNull() || DTy->isDependentType()) {
955 CT = CT_Dependent;
956 } else {
957 CT = canCalleeThrow(*this, E,
958 cast<CXXDeleteExpr>(E)->getOperatorDelete());
959 if (const RecordType *RT = DTy->getAs<RecordType>()) {
960 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Eli Friedman0423b762013-06-25 01:24:22 +0000961 const CXXDestructorDecl *DD = RD->getDestructor();
962 if (DD)
963 CT = mergeCanThrow(CT, canCalleeThrow(*this, E, DD));
Richard Smithf623c962012-04-17 00:58:00 +0000964 }
965 if (CT == CT_Can)
966 return CT;
967 }
968 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
969 }
970
971 case Expr::CXXBindTemporaryExprClass: {
972 // The bound temporary has to be destroyed again, which might throw.
973 CanThrowResult CT = canCalleeThrow(*this, E,
974 cast<CXXBindTemporaryExpr>(E)->getTemporary()->getDestructor());
975 if (CT == CT_Can)
976 return CT;
977 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
978 }
979
980 // ObjC message sends are like function calls, but never have exception
981 // specs.
982 case Expr::ObjCMessageExprClass:
983 case Expr::ObjCPropertyRefExprClass:
984 case Expr::ObjCSubscriptRefExprClass:
985 return CT_Can;
986
987 // All the ObjC literals that are implemented as calls are
988 // potentially throwing unless we decide to close off that
989 // possibility.
990 case Expr::ObjCArrayLiteralClass:
991 case Expr::ObjCDictionaryLiteralClass:
Patrick Beard0caa3942012-04-19 00:25:12 +0000992 case Expr::ObjCBoxedExprClass:
Richard Smithf623c962012-04-17 00:58:00 +0000993 return CT_Can;
994
995 // Many other things have subexpressions, so we have to test those.
996 // Some are simple:
997 case Expr::ConditionalOperatorClass:
998 case Expr::CompoundLiteralExprClass:
999 case Expr::CXXConstCastExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001000 case Expr::CXXReinterpretCastExprClass:
Richard Smithcc1b96d2013-06-12 22:31:48 +00001001 case Expr::CXXStdInitializerListExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001002 case Expr::DesignatedInitExprClass:
1003 case Expr::ExprWithCleanupsClass:
1004 case Expr::ExtVectorElementExprClass:
1005 case Expr::InitListExprClass:
1006 case Expr::MemberExprClass:
1007 case Expr::ObjCIsaExprClass:
1008 case Expr::ObjCIvarRefExprClass:
1009 case Expr::ParenExprClass:
1010 case Expr::ParenListExprClass:
1011 case Expr::ShuffleVectorExprClass:
Hal Finkelc4d7c822013-09-18 03:29:45 +00001012 case Expr::ConvertVectorExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001013 case Expr::VAArgExprClass:
1014 return canSubExprsThrow(*this, E);
1015
1016 // Some might be dependent for other reasons.
1017 case Expr::ArraySubscriptExprClass:
1018 case Expr::BinaryOperatorClass:
1019 case Expr::CompoundAssignOperatorClass:
1020 case Expr::CStyleCastExprClass:
1021 case Expr::CXXStaticCastExprClass:
1022 case Expr::CXXFunctionalCastExprClass:
1023 case Expr::ImplicitCastExprClass:
1024 case Expr::MaterializeTemporaryExprClass:
1025 case Expr::UnaryOperatorClass: {
1026 CanThrowResult CT = E->isTypeDependent() ? CT_Dependent : CT_Cannot;
1027 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1028 }
1029
1030 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1031 case Expr::StmtExprClass:
1032 return CT_Can;
1033
Richard Smith852c9db2013-04-20 22:23:05 +00001034 case Expr::CXXDefaultArgExprClass:
1035 return canThrow(cast<CXXDefaultArgExpr>(E)->getExpr());
1036
1037 case Expr::CXXDefaultInitExprClass:
1038 return canThrow(cast<CXXDefaultInitExpr>(E)->getExpr());
1039
Richard Smithf623c962012-04-17 00:58:00 +00001040 case Expr::ChooseExprClass:
1041 if (E->isTypeDependent() || E->isValueDependent())
1042 return CT_Dependent;
Eli Friedman75807f22013-07-20 00:40:58 +00001043 return canThrow(cast<ChooseExpr>(E)->getChosenSubExpr());
Richard Smithf623c962012-04-17 00:58:00 +00001044
1045 case Expr::GenericSelectionExprClass:
1046 if (cast<GenericSelectionExpr>(E)->isResultDependent())
1047 return CT_Dependent;
1048 return canThrow(cast<GenericSelectionExpr>(E)->getResultExpr());
1049
1050 // Some expressions are always dependent.
1051 case Expr::CXXDependentScopeMemberExprClass:
1052 case Expr::CXXUnresolvedConstructExprClass:
1053 case Expr::DependentScopeDeclRefExprClass:
1054 return CT_Dependent;
1055
1056 case Expr::AsTypeExprClass:
1057 case Expr::BinaryConditionalOperatorClass:
1058 case Expr::BlockExprClass:
1059 case Expr::CUDAKernelCallExprClass:
1060 case Expr::DeclRefExprClass:
1061 case Expr::ObjCBridgedCastExprClass:
1062 case Expr::ObjCIndirectCopyRestoreExprClass:
1063 case Expr::ObjCProtocolExprClass:
1064 case Expr::ObjCSelectorExprClass:
1065 case Expr::OffsetOfExprClass:
1066 case Expr::PackExpansionExprClass:
1067 case Expr::PseudoObjectExprClass:
1068 case Expr::SubstNonTypeTemplateParmExprClass:
1069 case Expr::SubstNonTypeTemplateParmPackExprClass:
Richard Smithb15fe3a2012-09-12 00:56:43 +00001070 case Expr::FunctionParmPackExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001071 case Expr::UnaryExprOrTypeTraitExprClass:
1072 case Expr::UnresolvedLookupExprClass:
1073 case Expr::UnresolvedMemberExprClass:
1074 // FIXME: Can any of the above throw? If so, when?
1075 return CT_Cannot;
1076
1077 case Expr::AddrLabelExprClass:
1078 case Expr::ArrayTypeTraitExprClass:
1079 case Expr::AtomicExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001080 case Expr::TypeTraitExprClass:
1081 case Expr::CXXBoolLiteralExprClass:
1082 case Expr::CXXNoexceptExprClass:
1083 case Expr::CXXNullPtrLiteralExprClass:
1084 case Expr::CXXPseudoDestructorExprClass:
1085 case Expr::CXXScalarValueInitExprClass:
1086 case Expr::CXXThisExprClass:
1087 case Expr::CXXUuidofExprClass:
1088 case Expr::CharacterLiteralClass:
1089 case Expr::ExpressionTraitExprClass:
1090 case Expr::FloatingLiteralClass:
1091 case Expr::GNUNullExprClass:
1092 case Expr::ImaginaryLiteralClass:
1093 case Expr::ImplicitValueInitExprClass:
1094 case Expr::IntegerLiteralClass:
1095 case Expr::ObjCEncodeExprClass:
1096 case Expr::ObjCStringLiteralClass:
1097 case Expr::ObjCBoolLiteralExprClass:
1098 case Expr::OpaqueValueExprClass:
1099 case Expr::PredefinedExprClass:
1100 case Expr::SizeOfPackExprClass:
1101 case Expr::StringLiteralClass:
Richard Smithf623c962012-04-17 00:58:00 +00001102 // These expressions can never throw.
1103 return CT_Cannot;
1104
John McCall5e77d762013-04-16 07:28:30 +00001105 case Expr::MSPropertyRefExprClass:
1106 llvm_unreachable("Invalid class for expression");
1107
Richard Smithf623c962012-04-17 00:58:00 +00001108#define STMT(CLASS, PARENT) case Expr::CLASS##Class:
1109#define STMT_RANGE(Base, First, Last)
1110#define LAST_STMT_RANGE(BASE, FIRST, LAST)
1111#define EXPR(CLASS, PARENT)
1112#define ABSTRACT_STMT(STMT)
1113#include "clang/AST/StmtNodes.inc"
1114 case Expr::NoStmtClass:
1115 llvm_unreachable("Invalid class for expression");
1116 }
1117 llvm_unreachable("Bogus StmtClass");
1118}
1119
Sebastian Redl4915e632009-10-11 09:03:14 +00001120} // end namespace clang