blob: 59fd85ef710b43ffcd3462dd3a0a26718d223cdf [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"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/Lex/Preprocessor.h"
Sebastian Redl4915e632009-10-11 09:03:14 +000023#include "llvm/ADT/SmallPtrSet.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000024#include "llvm/ADT/SmallString.h"
Sebastian Redl4915e632009-10-11 09:03:14 +000025
26namespace clang {
27
28static const FunctionProtoType *GetUnderlyingFunction(QualType T)
29{
30 if (const PointerType *PtrTy = T->getAs<PointerType>())
31 T = PtrTy->getPointeeType();
32 else if (const ReferenceType *RefTy = T->getAs<ReferenceType>())
33 T = RefTy->getPointeeType();
Sebastian Redl075b21d2009-10-14 14:38:54 +000034 else if (const MemberPointerType *MPTy = T->getAs<MemberPointerType>())
35 T = MPTy->getPointeeType();
Sebastian Redl4915e632009-10-11 09:03:14 +000036 return T->getAs<FunctionProtoType>();
37}
38
39/// CheckSpecifiedExceptionType - Check if the given type is valid in an
40/// exception specification. Incomplete types, or pointers to incomplete types
41/// other than void are not allowed.
Richard Smith8606d752012-11-28 22:33:28 +000042///
43/// \param[in,out] T The exception type. This will be decayed to a pointer type
44/// when the input is an array or a function type.
45bool Sema::CheckSpecifiedExceptionType(QualType &T, const SourceRange &Range) {
Richard Smitha118c6a2012-11-28 22:52:42 +000046 // C++11 [except.spec]p2:
47 // A type cv T, "array of T", or "function returning T" denoted
Richard Smith8606d752012-11-28 22:33:28 +000048 // in an exception-specification is adjusted to type T, "pointer to T", or
49 // "pointer to function returning T", respectively.
Richard Smitha118c6a2012-11-28 22:52:42 +000050 //
51 // We also apply this rule in C++98.
Richard Smith8606d752012-11-28 22:33:28 +000052 if (T->isArrayType())
53 T = Context.getArrayDecayedType(T);
54 else if (T->isFunctionType())
55 T = Context.getPointerType(T);
Sebastian Redl4915e632009-10-11 09:03:14 +000056
Richard Smitha118c6a2012-11-28 22:52:42 +000057 int Kind = 0;
Richard Smith8606d752012-11-28 22:33:28 +000058 QualType PointeeT = T;
Richard Smitha118c6a2012-11-28 22:52:42 +000059 if (const PointerType *PT = T->getAs<PointerType>()) {
60 PointeeT = PT->getPointeeType();
61 Kind = 1;
Sebastian Redl4915e632009-10-11 09:03:14 +000062
Richard Smitha118c6a2012-11-28 22:52:42 +000063 // cv void* is explicitly permitted, despite being a pointer to an
64 // incomplete type.
65 if (PointeeT->isVoidType())
66 return false;
67 } else if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
68 PointeeT = RT->getPointeeType();
69 Kind = 2;
Richard Smith8606d752012-11-28 22:33:28 +000070
Richard Smitha118c6a2012-11-28 22:52:42 +000071 if (RT->isRValueReferenceType()) {
72 // C++11 [except.spec]p2:
73 // A type denoted in an exception-specification shall not denote [...]
74 // an rvalue reference type.
75 Diag(Range.getBegin(), diag::err_rref_in_exception_spec)
76 << T << Range;
77 return true;
78 }
79 }
80
81 // C++11 [except.spec]p2:
82 // A type denoted in an exception-specification shall not denote an
83 // incomplete type other than a class currently being defined [...].
84 // A type denoted in an exception-specification shall not denote a
85 // pointer or reference to an incomplete type, other than (cv) void* or a
86 // pointer or reference to a class currently being defined.
87 if (!(PointeeT->isRecordType() &&
88 PointeeT->getAs<RecordType>()->isBeingDefined()) &&
Richard Smith8606d752012-11-28 22:33:28 +000089 RequireCompleteType(Range.getBegin(), PointeeT,
Richard Smitha118c6a2012-11-28 22:52:42 +000090 diag::err_incomplete_in_exception_spec, Kind, Range))
Sebastian Redl7eb5d372009-10-14 14:59:48 +000091 return true;
Sebastian Redl4915e632009-10-11 09:03:14 +000092
93 return false;
94}
95
96/// CheckDistantExceptionSpec - Check if the given type is a pointer or pointer
97/// to member to a function with an exception specification. This means that
98/// it is invalid to add another level of indirection.
99bool Sema::CheckDistantExceptionSpec(QualType T) {
100 if (const PointerType *PT = T->getAs<PointerType>())
101 T = PT->getPointeeType();
102 else if (const MemberPointerType *PT = T->getAs<MemberPointerType>())
103 T = PT->getPointeeType();
104 else
105 return false;
106
107 const FunctionProtoType *FnT = T->getAs<FunctionProtoType>();
108 if (!FnT)
109 return false;
110
111 return FnT->hasExceptionSpec();
112}
113
Richard Smithf623c962012-04-17 00:58:00 +0000114const FunctionProtoType *
115Sema::ResolveExceptionSpec(SourceLocation Loc, const FunctionProtoType *FPT) {
Richard Smithd3b5c9082012-07-27 04:22:15 +0000116 if (!isUnresolvedExceptionSpec(FPT->getExceptionSpecType()))
Richard Smithf623c962012-04-17 00:58:00 +0000117 return FPT;
118
119 FunctionDecl *SourceDecl = FPT->getExceptionSpecDecl();
120 const FunctionProtoType *SourceFPT =
121 SourceDecl->getType()->castAs<FunctionProtoType>();
122
Richard Smithd3b5c9082012-07-27 04:22:15 +0000123 // If the exception specification has already been resolved, just return it.
124 if (!isUnresolvedExceptionSpec(SourceFPT->getExceptionSpecType()))
Richard Smithf623c962012-04-17 00:58:00 +0000125 return SourceFPT;
126
Richard Smithd3b5c9082012-07-27 04:22:15 +0000127 // Compute or instantiate the exception specification now.
Richard Smith3901dfe2013-03-27 00:22:47 +0000128 if (SourceFPT->getExceptionSpecType() == EST_Unevaluated)
Richard Smithd3b5c9082012-07-27 04:22:15 +0000129 EvaluateImplicitExceptionSpec(Loc, cast<CXXMethodDecl>(SourceDecl));
130 else
131 InstantiateExceptionSpec(Loc, SourceDecl);
Richard Smithf623c962012-04-17 00:58:00 +0000132
133 return SourceDecl->getType()->castAs<FunctionProtoType>();
134}
135
Richard Smith564417a2014-03-20 21:47:22 +0000136void Sema::UpdateExceptionSpec(FunctionDecl *FD,
137 const FunctionProtoType::ExtProtoInfo &EPI) {
138 const FunctionProtoType *Proto = FD->getType()->castAs<FunctionProtoType>();
139
140 // Overwrite the exception spec and rebuild the function type.
141 FunctionProtoType::ExtProtoInfo NewEPI = Proto->getExtProtoInfo();
142 NewEPI.ExceptionSpecType = EPI.ExceptionSpecType;
143 NewEPI.NumExceptions = EPI.NumExceptions;
144 NewEPI.Exceptions = EPI.Exceptions;
145 NewEPI.NoexceptExpr = EPI.NoexceptExpr;
146 FD->setType(Context.getFunctionType(Proto->getReturnType(),
147 Proto->getParamTypes(), NewEPI));
148
149 // If we've fully resolved the exception specification, notify listeners.
150 if (!isUnresolvedExceptionSpec(EPI.ExceptionSpecType))
151 if (auto *Listener = getASTMutationListener())
152 Listener->ResolvedExceptionSpec(FD);
153}
154
Richard Smith66f3ac92012-10-20 08:26:51 +0000155/// Determine whether a function has an implicitly-generated exception
Richard Smith1ee63522012-10-16 23:30:16 +0000156/// specification.
Richard Smith66f3ac92012-10-20 08:26:51 +0000157static bool hasImplicitExceptionSpec(FunctionDecl *Decl) {
158 if (!isa<CXXDestructorDecl>(Decl) &&
159 Decl->getDeclName().getCXXOverloadedOperator() != OO_Delete &&
160 Decl->getDeclName().getCXXOverloadedOperator() != OO_Array_Delete)
161 return false;
Richard Smith1ee63522012-10-16 23:30:16 +0000162
Richard Smithc7fb2252014-02-07 22:51:16 +0000163 // For a function that the user didn't declare:
164 // - if this is a destructor, its exception specification is implicit.
165 // - if this is 'operator delete' or 'operator delete[]', the exception
166 // specification is as-if an explicit exception specification was given
167 // (per [basic.stc.dynamic]p2).
Richard Smith66f3ac92012-10-20 08:26:51 +0000168 if (!Decl->getTypeSourceInfo())
Richard Smithc7fb2252014-02-07 22:51:16 +0000169 return isa<CXXDestructorDecl>(Decl);
Richard Smith66f3ac92012-10-20 08:26:51 +0000170
171 const FunctionProtoType *Ty =
172 Decl->getTypeSourceInfo()->getType()->getAs<FunctionProtoType>();
173 return !Ty->hasExceptionSpec();
Richard Smith1ee63522012-10-16 23:30:16 +0000174}
175
Douglas Gregorf40863c2010-02-12 07:32:17 +0000176bool Sema::CheckEquivalentExceptionSpec(FunctionDecl *Old, FunctionDecl *New) {
Sebastian Redlcb5dd002011-03-15 19:52:30 +0000177 OverloadedOperatorKind OO = New->getDeclName().getCXXOverloadedOperator();
178 bool IsOperatorNew = OO == OO_New || OO == OO_Array_New;
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000179 bool MissingExceptionSpecification = false;
Douglas Gregorf40863c2010-02-12 07:32:17 +0000180 bool MissingEmptyExceptionSpecification = false;
Hans Wennborg39a509a2014-02-05 02:37:58 +0000181
Francois Pichet13b4e682011-03-19 23:05:18 +0000182 unsigned DiagID = diag::err_mismatched_exception_spec;
Hans Wennborg39a509a2014-02-05 02:37:58 +0000183 bool ReturnValueOnError = true;
184 if (getLangOpts().MicrosoftExt) {
Francois Pichet93921652011-04-22 08:25:24 +0000185 DiagID = diag::warn_mismatched_exception_spec;
Hans Wennborg39a509a2014-02-05 02:37:58 +0000186 ReturnValueOnError = false;
187 }
Richard Smithf623c962012-04-17 00:58:00 +0000188
Richard Smith1ee63522012-10-16 23:30:16 +0000189 // Check the types as written: they must match before any exception
190 // specification adjustment is applied.
191 if (!CheckEquivalentExceptionSpec(
192 PDiag(DiagID), PDiag(diag::note_previous_declaration),
Richard Smith66f3ac92012-10-20 08:26:51 +0000193 Old->getType()->getAs<FunctionProtoType>(), Old->getLocation(),
194 New->getType()->getAs<FunctionProtoType>(), New->getLocation(),
Richard Smith1ee63522012-10-16 23:30:16 +0000195 &MissingExceptionSpecification, &MissingEmptyExceptionSpecification,
Richard Smith66f3ac92012-10-20 08:26:51 +0000196 /*AllowNoexceptAllMatchWithNoSpec=*/true, IsOperatorNew)) {
197 // C++11 [except.spec]p4 [DR1492]:
198 // If a declaration of a function has an implicit
199 // exception-specification, other declarations of the function shall
200 // not specify an exception-specification.
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000201 if (getLangOpts().CPlusPlus11 &&
Richard Smith66f3ac92012-10-20 08:26:51 +0000202 hasImplicitExceptionSpec(Old) != hasImplicitExceptionSpec(New)) {
203 Diag(New->getLocation(), diag::ext_implicit_exception_spec_mismatch)
204 << hasImplicitExceptionSpec(Old);
205 if (!Old->getLocation().isInvalid())
206 Diag(Old->getLocation(), diag::note_previous_declaration);
207 }
Douglas Gregorf40863c2010-02-12 07:32:17 +0000208 return false;
Richard Smith66f3ac92012-10-20 08:26:51 +0000209 }
Douglas Gregorf40863c2010-02-12 07:32:17 +0000210
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000211 // The failure was something other than an missing exception
Hans Wennborg39a509a2014-02-05 02:37:58 +0000212 // specification; return an error, except in MS mode where this is a warning.
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000213 if (!MissingExceptionSpecification)
Hans Wennborg39a509a2014-02-05 02:37:58 +0000214 return ReturnValueOnError;
Douglas Gregorf40863c2010-02-12 07:32:17 +0000215
Richard Smith66f3ac92012-10-20 08:26:51 +0000216 const FunctionProtoType *NewProto =
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000217 New->getType()->castAs<FunctionProtoType>();
John McCalldb40c7f2010-12-14 08:05:40 +0000218
Douglas Gregorf40863c2010-02-12 07:32:17 +0000219 // The new function declaration is only missing an empty exception
220 // specification "throw()". If the throw() specification came from a
221 // function in a system header that has C linkage, just add an empty
222 // exception specification to the "new" declaration. This is an
223 // egregious workaround for glibc, which adds throw() specifications
224 // to many libc functions as an optimization. Unfortunately, that
225 // optimization isn't permitted by the C++ standard, so we're forced
226 // to work around it here.
John McCalldb40c7f2010-12-14 08:05:40 +0000227 if (MissingEmptyExceptionSpecification && NewProto &&
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000228 (Old->getLocation().isInvalid() ||
229 Context.getSourceManager().isInSystemHeader(Old->getLocation())) &&
Douglas Gregorf40863c2010-02-12 07:32:17 +0000230 Old->isExternC()) {
John McCalldb40c7f2010-12-14 08:05:40 +0000231 FunctionProtoType::ExtProtoInfo EPI = NewProto->getExtProtoInfo();
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000232 EPI.ExceptionSpecType = EST_DynamicNone;
Alp Toker314cc812014-01-25 16:55:45 +0000233 QualType NewType = Context.getFunctionType(NewProto->getReturnType(),
Alp Toker9cacbab2014-01-20 20:26:09 +0000234 NewProto->getParamTypes(), EPI);
Douglas Gregorf40863c2010-02-12 07:32:17 +0000235 New->setType(NewType);
236 return false;
237 }
238
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000239 const FunctionProtoType *OldProto =
240 Old->getType()->castAs<FunctionProtoType>();
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000241
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000242 FunctionProtoType::ExtProtoInfo EPI = NewProto->getExtProtoInfo();
243 EPI.ExceptionSpecType = OldProto->getExceptionSpecType();
244 if (EPI.ExceptionSpecType == EST_Dynamic) {
245 EPI.NumExceptions = OldProto->getNumExceptions();
246 EPI.Exceptions = OldProto->exception_begin();
247 } else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) {
248 // FIXME: We can't just take the expression from the old prototype. It
249 // likely contains references to the old prototype's parameters.
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000250 }
251
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000252 // Update the type of the function with the appropriate exception
253 // specification.
Alp Toker314cc812014-01-25 16:55:45 +0000254 QualType NewType = Context.getFunctionType(NewProto->getReturnType(),
Alp Toker9cacbab2014-01-20 20:26:09 +0000255 NewProto->getParamTypes(), EPI);
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000256 New->setType(NewType);
257
258 // Warn about the lack of exception specification.
259 SmallString<128> ExceptionSpecString;
260 llvm::raw_svector_ostream OS(ExceptionSpecString);
261 switch (OldProto->getExceptionSpecType()) {
262 case EST_DynamicNone:
263 OS << "throw()";
264 break;
265
266 case EST_Dynamic: {
267 OS << "throw(";
268 bool OnFirstException = true;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000269 for (const auto &E : OldProto->exceptions()) {
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000270 if (OnFirstException)
271 OnFirstException = false;
272 else
273 OS << ", ";
274
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000275 OS << E.getAsString(getPrintingPolicy());
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000276 }
277 OS << ")";
278 break;
279 }
280
281 case EST_BasicNoexcept:
282 OS << "noexcept";
283 break;
284
285 case EST_ComputedNoexcept:
286 OS << "noexcept(";
287 OldProto->getNoexceptExpr()->printPretty(OS, 0, getPrintingPolicy());
288 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>())
300 FixItLoc = PP.getLocForEndOfToken(FTLoc.getLocalRangeEnd());
301 }
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)
Hans Wennborg39a509a2014-02-05 02:37:58 +0000329 DiagID = diag::warn_mismatched_exception_spec;
330 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) {
Sebastian Redlcb5dd002011-03-15 19:52:30 +0000459 const FunctionProtoType *WithExceptions = 0;
460 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?
472 DeclContext* DC = ExRecord->getDeclContext();
Sebastian Redlc34c29f2011-03-15 20:41:09 +0000473 DC = DC->getEnclosingNamespaceContext();
474 if (NamespaceDecl* NS = dyn_cast<NamespaceDecl>(DC)) {
Sebastian Redlcb5dd002011-03-15 19:52:30 +0000475 IdentifierInfo* NSName = NS->getIdentifier();
Sebastian Redlc34c29f2011-03-15 20:41:09 +0000476 DC = DC->getParent();
Sebastian Redlcb5dd002011-03-15 19:52:30 +0000477 if (NSName && NSName->getName() == "std" &&
Sebastian Redlc34c29f2011-03-15 20:41:09 +0000478 DC->getEnclosingNamespaceContext()->isTranslationUnit()) {
Sebastian Redlcb5dd002011-03-15 19:52:30 +0000479 return false;
Sebastian Redlc34c29f2011-03-15 20:41:09 +0000480 }
Sebastian Redlcb5dd002011-03-15 19:52:30 +0000481 }
482 }
483 }
484 }
485 }
486
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000487 // At this point, the only remaining valid case is two matching dynamic
488 // specifications. We return here unless both specifications are dynamic.
489 if (OldEST != EST_Dynamic || NewEST != EST_Dynamic) {
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000490 if (MissingExceptionSpecification && Old->hasExceptionSpec() &&
Douglas Gregorf40863c2010-02-12 07:32:17 +0000491 !New->hasExceptionSpec()) {
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000492 // The old type has an exception specification of some sort, but
493 // the new type does not.
494 *MissingExceptionSpecification = true;
495
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000496 if (MissingEmptyExceptionSpecification && OldNonThrowing) {
497 // The old type has a throw() or noexcept(true) exception specification
498 // and the new type has no exception specification, and the caller asked
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000499 // to handle this itself.
500 *MissingEmptyExceptionSpecification = true;
501 }
502
Douglas Gregorf40863c2010-02-12 07:32:17 +0000503 return true;
504 }
505
Sebastian Redl4915e632009-10-11 09:03:14 +0000506 Diag(NewLoc, DiagID);
Sebastian Redla44822f2009-10-14 16:09:29 +0000507 if (NoteID.getDiagID() != 0)
Sebastian Redl4915e632009-10-11 09:03:14 +0000508 Diag(OldLoc, NoteID);
509 return true;
510 }
511
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000512 assert(OldEST == EST_Dynamic && NewEST == EST_Dynamic &&
513 "Exception compatibility logic error: non-dynamic spec slipped through.");
514
Sebastian Redl4915e632009-10-11 09:03:14 +0000515 bool Success = true;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000516 // Both have a dynamic exception spec. Collect the first set, then compare
Sebastian Redl4915e632009-10-11 09:03:14 +0000517 // to the second.
Sebastian Redl184edca2009-10-14 15:06:25 +0000518 llvm::SmallPtrSet<CanQualType, 8> OldTypes, NewTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000519 for (const auto &I : Old->exceptions())
520 OldTypes.insert(Context.getCanonicalType(I).getUnqualifiedType());
Sebastian Redl4915e632009-10-11 09:03:14 +0000521
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000522 for (const auto &I : New->exceptions()) {
523 CanQualType TypePtr = Context.getCanonicalType(I).getUnqualifiedType();
Sebastian Redl6e4c8712009-10-11 09:11:23 +0000524 if(OldTypes.count(TypePtr))
525 NewTypes.insert(TypePtr);
526 else
527 Success = false;
528 }
Sebastian Redl4915e632009-10-11 09:03:14 +0000529
Sebastian Redl6e4c8712009-10-11 09:11:23 +0000530 Success = Success && OldTypes.size() == NewTypes.size();
Sebastian Redl4915e632009-10-11 09:03:14 +0000531
532 if (Success) {
533 return false;
534 }
535 Diag(NewLoc, DiagID);
Sebastian Redla44822f2009-10-14 16:09:29 +0000536 if (NoteID.getDiagID() != 0)
Sebastian Redl4915e632009-10-11 09:03:14 +0000537 Diag(OldLoc, NoteID);
538 return true;
539}
540
541/// CheckExceptionSpecSubset - Check whether the second function type's
542/// exception specification is a subset (or equivalent) of the first function
543/// type. This is used by override and pointer assignment checks.
Sebastian Redla44822f2009-10-14 16:09:29 +0000544bool Sema::CheckExceptionSpecSubset(
545 const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID,
Sebastian Redl4915e632009-10-11 09:03:14 +0000546 const FunctionProtoType *Superset, SourceLocation SuperLoc,
547 const FunctionProtoType *Subset, SourceLocation SubLoc) {
John McCallf9c94092010-05-28 08:37:35 +0000548
549 // Just auto-succeed under -fno-exceptions.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000550 if (!getLangOpts().CXXExceptions)
John McCallf9c94092010-05-28 08:37:35 +0000551 return false;
552
Sebastian Redl4915e632009-10-11 09:03:14 +0000553 // FIXME: As usual, we could be more specific in our error messages, but
554 // that better waits until we've got types with source locations.
555
556 if (!SubLoc.isValid())
557 SubLoc = SuperLoc;
558
Richard Smithf623c962012-04-17 00:58:00 +0000559 // Resolve the exception specifications, if needed.
560 Superset = ResolveExceptionSpec(SuperLoc, Superset);
561 if (!Superset)
562 return false;
563 Subset = ResolveExceptionSpec(SubLoc, Subset);
564 if (!Subset)
565 return false;
566
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000567 ExceptionSpecificationType SuperEST = Superset->getExceptionSpecType();
568
Sebastian Redl4915e632009-10-11 09:03:14 +0000569 // If superset contains everything, we're done.
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000570 if (SuperEST == EST_None || SuperEST == EST_MSAny)
Sebastian Redl4915e632009-10-11 09:03:14 +0000571 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
572
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000573 // If there are dependent noexcept specs, assume everything is fine. Unlike
574 // with the equivalency check, this is safe in this case, because we don't
575 // want to merge declarations. Checks after instantiation will catch any
576 // omissions we make here.
577 // We also shortcut checking if a noexcept expression was bad.
578
Sebastian Redl31ad7542011-03-13 17:09:40 +0000579 FunctionProtoType::NoexceptResult SuperNR =Superset->getNoexceptSpec(Context);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000580 if (SuperNR == FunctionProtoType::NR_BadNoexcept ||
581 SuperNR == FunctionProtoType::NR_Dependent)
582 return false;
583
584 // Another case of the superset containing everything.
585 if (SuperNR == FunctionProtoType::NR_Throw)
586 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
587
588 ExceptionSpecificationType SubEST = Subset->getExceptionSpecType();
589
Richard Smithd3b5c9082012-07-27 04:22:15 +0000590 assert(!isUnresolvedExceptionSpec(SuperEST) &&
591 !isUnresolvedExceptionSpec(SubEST) &&
Richard Smith938f40b2011-06-11 17:19:42 +0000592 "Shouldn't see unknown exception specifications here");
593
Sebastian Redl4915e632009-10-11 09:03:14 +0000594 // It does not. If the subset contains everything, we've failed.
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000595 if (SubEST == EST_None || SubEST == EST_MSAny) {
Sebastian Redl4915e632009-10-11 09:03:14 +0000596 Diag(SubLoc, DiagID);
Sebastian Redla44822f2009-10-14 16:09:29 +0000597 if (NoteID.getDiagID() != 0)
Sebastian Redl4915e632009-10-11 09:03:14 +0000598 Diag(SuperLoc, NoteID);
599 return true;
600 }
601
Sebastian Redl31ad7542011-03-13 17:09:40 +0000602 FunctionProtoType::NoexceptResult SubNR = Subset->getNoexceptSpec(Context);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000603 if (SubNR == FunctionProtoType::NR_BadNoexcept ||
604 SubNR == FunctionProtoType::NR_Dependent)
605 return false;
606
607 // Another case of the subset containing everything.
608 if (SubNR == FunctionProtoType::NR_Throw) {
609 Diag(SubLoc, DiagID);
610 if (NoteID.getDiagID() != 0)
611 Diag(SuperLoc, NoteID);
612 return true;
613 }
614
615 // If the subset contains nothing, we're done.
616 if (SubEST == EST_DynamicNone || SubNR == FunctionProtoType::NR_Nothrow)
617 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
618
619 // Otherwise, if the superset contains nothing, we've failed.
620 if (SuperEST == EST_DynamicNone || SuperNR == FunctionProtoType::NR_Nothrow) {
621 Diag(SubLoc, DiagID);
622 if (NoteID.getDiagID() != 0)
623 Diag(SuperLoc, NoteID);
624 return true;
625 }
626
627 assert(SuperEST == EST_Dynamic && SubEST == EST_Dynamic &&
628 "Exception spec subset: non-dynamic case slipped through.");
629
630 // Neither contains everything or nothing. Do a proper comparison.
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000631 for (const auto &SubI : Subset->exceptions()) {
Sebastian Redl4915e632009-10-11 09:03:14 +0000632 // Take one type from the subset.
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000633 QualType CanonicalSubT = Context.getCanonicalType(SubI);
Sebastian Redl075b21d2009-10-14 14:38:54 +0000634 // Unwrap pointers and references so that we can do checks within a class
635 // hierarchy. Don't unwrap member pointers; they don't have hierarchy
636 // conversions on the pointee.
Sebastian Redl4915e632009-10-11 09:03:14 +0000637 bool SubIsPointer = false;
638 if (const ReferenceType *RefTy = CanonicalSubT->getAs<ReferenceType>())
639 CanonicalSubT = RefTy->getPointeeType();
640 if (const PointerType *PtrTy = CanonicalSubT->getAs<PointerType>()) {
641 CanonicalSubT = PtrTy->getPointeeType();
642 SubIsPointer = true;
643 }
644 bool SubIsClass = CanonicalSubT->isRecordType();
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +0000645 CanonicalSubT = CanonicalSubT.getLocalUnqualifiedType();
Sebastian Redl4915e632009-10-11 09:03:14 +0000646
647 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
648 /*DetectVirtual=*/false);
649
650 bool Contained = false;
651 // Make sure it's in the superset.
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000652 for (const auto &SuperI : Superset->exceptions()) {
653 QualType CanonicalSuperT = Context.getCanonicalType(SuperI);
Sebastian Redl4915e632009-10-11 09:03:14 +0000654 // SubT must be SuperT or derived from it, or pointer or reference to
655 // such types.
656 if (const ReferenceType *RefTy = CanonicalSuperT->getAs<ReferenceType>())
657 CanonicalSuperT = RefTy->getPointeeType();
658 if (SubIsPointer) {
659 if (const PointerType *PtrTy = CanonicalSuperT->getAs<PointerType>())
660 CanonicalSuperT = PtrTy->getPointeeType();
661 else {
662 continue;
663 }
664 }
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +0000665 CanonicalSuperT = CanonicalSuperT.getLocalUnqualifiedType();
Sebastian Redl4915e632009-10-11 09:03:14 +0000666 // If the types are the same, move on to the next type in the subset.
667 if (CanonicalSubT == CanonicalSuperT) {
668 Contained = true;
669 break;
670 }
671
672 // Otherwise we need to check the inheritance.
673 if (!SubIsClass || !CanonicalSuperT->isRecordType())
674 continue;
675
676 Paths.clear();
677 if (!IsDerivedFrom(CanonicalSubT, CanonicalSuperT, Paths))
678 continue;
679
Douglas Gregor27ac4292010-05-21 20:29:55 +0000680 if (Paths.isAmbiguous(Context.getCanonicalType(CanonicalSuperT)))
Sebastian Redl4915e632009-10-11 09:03:14 +0000681 continue;
682
John McCall5b0829a2010-02-10 09:31:12 +0000683 // Do this check from a context without privileges.
John McCall1064d7e2010-03-16 05:22:47 +0000684 switch (CheckBaseClassAccess(SourceLocation(),
John McCall5b0829a2010-02-10 09:31:12 +0000685 CanonicalSuperT, CanonicalSubT,
686 Paths.front(),
John McCall1064d7e2010-03-16 05:22:47 +0000687 /*Diagnostic*/ 0,
John McCall5b0829a2010-02-10 09:31:12 +0000688 /*ForceCheck*/ true,
John McCall1064d7e2010-03-16 05:22:47 +0000689 /*ForceUnprivileged*/ true)) {
John McCall5b0829a2010-02-10 09:31:12 +0000690 case AR_accessible: break;
691 case AR_inaccessible: continue;
692 case AR_dependent:
693 llvm_unreachable("access check dependent for unprivileged context");
John McCall5b0829a2010-02-10 09:31:12 +0000694 case AR_delayed:
695 llvm_unreachable("access check delayed in non-declaration");
John McCall5b0829a2010-02-10 09:31:12 +0000696 }
Sebastian Redl4915e632009-10-11 09:03:14 +0000697
698 Contained = true;
699 break;
700 }
701 if (!Contained) {
702 Diag(SubLoc, DiagID);
Sebastian Redla44822f2009-10-14 16:09:29 +0000703 if (NoteID.getDiagID() != 0)
Sebastian Redl4915e632009-10-11 09:03:14 +0000704 Diag(SuperLoc, NoteID);
705 return true;
706 }
707 }
708 // We've run half the gauntlet.
709 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
710}
711
712static bool CheckSpecForTypesEquivalent(Sema &S,
Sebastian Redla44822f2009-10-14 16:09:29 +0000713 const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID,
Sebastian Redl4915e632009-10-11 09:03:14 +0000714 QualType Target, SourceLocation TargetLoc,
715 QualType Source, SourceLocation SourceLoc)
716{
717 const FunctionProtoType *TFunc = GetUnderlyingFunction(Target);
718 if (!TFunc)
719 return false;
720 const FunctionProtoType *SFunc = GetUnderlyingFunction(Source);
721 if (!SFunc)
722 return false;
723
724 return S.CheckEquivalentExceptionSpec(DiagID, NoteID, TFunc, TargetLoc,
725 SFunc, SourceLoc);
726}
727
728/// CheckParamExceptionSpec - Check if the parameter and return types of the
729/// two functions have equivalent exception specs. This is part of the
730/// assignment and override compatibility check. We do not check the parameters
731/// of parameter function pointers recursively, as no sane programmer would
732/// even be able to write such a function type.
Sebastian Redla44822f2009-10-14 16:09:29 +0000733bool Sema::CheckParamExceptionSpec(const PartialDiagnostic & NoteID,
Sebastian Redl4915e632009-10-11 09:03:14 +0000734 const FunctionProtoType *Target, SourceLocation TargetLoc,
735 const FunctionProtoType *Source, SourceLocation SourceLoc)
736{
Alp Toker314cc812014-01-25 16:55:45 +0000737 if (CheckSpecForTypesEquivalent(
738 *this, PDiag(diag::err_deep_exception_specs_differ) << 0, PDiag(),
739 Target->getReturnType(), TargetLoc, Source->getReturnType(),
740 SourceLoc))
Sebastian Redl4915e632009-10-11 09:03:14 +0000741 return true;
742
Sebastian Redla44822f2009-10-14 16:09:29 +0000743 // We shouldn't even be testing this unless the arguments are otherwise
Sebastian Redl4915e632009-10-11 09:03:14 +0000744 // compatible.
Alp Toker9cacbab2014-01-20 20:26:09 +0000745 assert(Target->getNumParams() == Source->getNumParams() &&
Sebastian Redl4915e632009-10-11 09:03:14 +0000746 "Functions have different argument counts.");
Alp Toker9cacbab2014-01-20 20:26:09 +0000747 for (unsigned i = 0, E = Target->getNumParams(); i != E; ++i) {
748 if (CheckSpecForTypesEquivalent(
749 *this, PDiag(diag::err_deep_exception_specs_differ) << 1, PDiag(),
750 Target->getParamType(i), TargetLoc, Source->getParamType(i),
751 SourceLoc))
Sebastian Redl4915e632009-10-11 09:03:14 +0000752 return true;
753 }
754 return false;
755}
756
757bool Sema::CheckExceptionSpecCompatibility(Expr *From, QualType ToType)
758{
759 // First we check for applicability.
760 // Target type must be a function, function pointer or function reference.
761 const FunctionProtoType *ToFunc = GetUnderlyingFunction(ToType);
762 if (!ToFunc)
763 return false;
764
765 // SourceType must be a function or function pointer.
766 const FunctionProtoType *FromFunc = GetUnderlyingFunction(From->getType());
767 if (!FromFunc)
768 return false;
769
770 // Now we've got the correct types on both sides, check their compatibility.
771 // This means that the source of the conversion can only throw a subset of
772 // the exceptions of the target, and any exception specs on arguments or
773 // return types must be equivalent.
Douglas Gregor89336232010-03-29 23:34:08 +0000774 return CheckExceptionSpecSubset(PDiag(diag::err_incompatible_exception_specs),
775 PDiag(), ToFunc,
776 From->getSourceRange().getBegin(),
Sebastian Redl4915e632009-10-11 09:03:14 +0000777 FromFunc, SourceLocation());
778}
779
780bool Sema::CheckOverridingFunctionExceptionSpec(const CXXMethodDecl *New,
781 const CXXMethodDecl *Old) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000782 if (getLangOpts().CPlusPlus11 && isa<CXXDestructorDecl>(New)) {
Sebastian Redl645d9582011-05-20 05:57:18 +0000783 // Don't check uninstantiated template destructors at all. We can only
784 // synthesize correct specs after the template is instantiated.
785 if (New->getParent()->isDependentType())
786 return false;
787 if (New->getParent()->isBeingDefined()) {
788 // The destructor might be updated once the definition is finished. So
789 // remember it and check later.
790 DelayedDestructorExceptionSpecChecks.push_back(std::make_pair(
791 cast<CXXDestructorDecl>(New), cast<CXXDestructorDecl>(Old)));
792 return false;
793 }
Sebastian Redl623ea822011-05-19 05:13:44 +0000794 }
Francois Picheta8032e92011-05-24 02:11:43 +0000795 unsigned DiagID = diag::err_override_exception_spec;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000796 if (getLangOpts().MicrosoftExt)
Francois Picheta8032e92011-05-24 02:11:43 +0000797 DiagID = diag::warn_override_exception_spec;
798 return CheckExceptionSpecSubset(PDiag(DiagID),
Douglas Gregor89336232010-03-29 23:34:08 +0000799 PDiag(diag::note_overridden_virtual_function),
Sebastian Redl4915e632009-10-11 09:03:14 +0000800 Old->getType()->getAs<FunctionProtoType>(),
801 Old->getLocation(),
802 New->getType()->getAs<FunctionProtoType>(),
803 New->getLocation());
804}
805
Richard Smithf623c962012-04-17 00:58:00 +0000806static CanThrowResult canSubExprsThrow(Sema &S, const Expr *CE) {
807 Expr *E = const_cast<Expr*>(CE);
808 CanThrowResult R = CT_Cannot;
809 for (Expr::child_range I = E->children(); I && R != CT_Can; ++I)
810 R = mergeCanThrow(R, S.canThrow(cast<Expr>(*I)));
811 return R;
812}
813
Eli Friedman0423b762013-06-25 01:24:22 +0000814static CanThrowResult canCalleeThrow(Sema &S, const Expr *E, const Decl *D) {
815 assert(D && "Expected decl");
Richard Smithf623c962012-04-17 00:58:00 +0000816
817 // See if we can get a function type from the decl somehow.
818 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
819 if (!VD) // If we have no clue what we're calling, assume the worst.
820 return CT_Can;
821
822 // As an extension, we assume that __attribute__((nothrow)) functions don't
823 // throw.
824 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
825 return CT_Cannot;
826
827 QualType T = VD->getType();
828 const FunctionProtoType *FT;
829 if ((FT = T->getAs<FunctionProtoType>())) {
830 } else if (const PointerType *PT = T->getAs<PointerType>())
831 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
832 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
833 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
834 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
835 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
836 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
837 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
838
839 if (!FT)
840 return CT_Can;
841
842 FT = S.ResolveExceptionSpec(E->getLocStart(), FT);
843 if (!FT)
844 return CT_Can;
845
Richard Smithf623c962012-04-17 00:58:00 +0000846 return FT->isNothrow(S.Context) ? CT_Cannot : CT_Can;
847}
848
849static CanThrowResult canDynamicCastThrow(const CXXDynamicCastExpr *DC) {
850 if (DC->isTypeDependent())
851 return CT_Dependent;
852
853 if (!DC->getTypeAsWritten()->isReferenceType())
854 return CT_Cannot;
855
856 if (DC->getSubExpr()->isTypeDependent())
857 return CT_Dependent;
858
859 return DC->getCastKind() == clang::CK_Dynamic? CT_Can : CT_Cannot;
860}
861
862static CanThrowResult canTypeidThrow(Sema &S, const CXXTypeidExpr *DC) {
863 if (DC->isTypeOperand())
864 return CT_Cannot;
865
866 Expr *Op = DC->getExprOperand();
867 if (Op->isTypeDependent())
868 return CT_Dependent;
869
870 const RecordType *RT = Op->getType()->getAs<RecordType>();
871 if (!RT)
872 return CT_Cannot;
873
874 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
875 return CT_Cannot;
876
877 if (Op->Classify(S.Context).isPRValue())
878 return CT_Cannot;
879
880 return CT_Can;
881}
882
883CanThrowResult Sema::canThrow(const Expr *E) {
884 // C++ [expr.unary.noexcept]p3:
885 // [Can throw] if in a potentially-evaluated context the expression would
886 // contain:
887 switch (E->getStmtClass()) {
888 case Expr::CXXThrowExprClass:
889 // - a potentially evaluated throw-expression
890 return CT_Can;
891
892 case Expr::CXXDynamicCastExprClass: {
893 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
894 // where T is a reference type, that requires a run-time check
895 CanThrowResult CT = canDynamicCastThrow(cast<CXXDynamicCastExpr>(E));
896 if (CT == CT_Can)
897 return CT;
898 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
899 }
900
901 case Expr::CXXTypeidExprClass:
902 // - a potentially evaluated typeid expression applied to a glvalue
903 // expression whose type is a polymorphic class type
904 return canTypeidThrow(*this, cast<CXXTypeidExpr>(E));
905
906 // - a potentially evaluated call to a function, member function, function
907 // pointer, or member function pointer that does not have a non-throwing
908 // exception-specification
909 case Expr::CallExprClass:
910 case Expr::CXXMemberCallExprClass:
911 case Expr::CXXOperatorCallExprClass:
912 case Expr::UserDefinedLiteralClass: {
913 const CallExpr *CE = cast<CallExpr>(E);
914 CanThrowResult CT;
915 if (E->isTypeDependent())
916 CT = CT_Dependent;
917 else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
918 CT = CT_Cannot;
Eli Friedman5a8738f2013-06-25 01:55:41 +0000919 else if (CE->getCalleeDecl())
Richard Smithf623c962012-04-17 00:58:00 +0000920 CT = canCalleeThrow(*this, E, CE->getCalleeDecl());
Eli Friedman5a8738f2013-06-25 01:55:41 +0000921 else
922 CT = CT_Can;
Richard Smithf623c962012-04-17 00:58:00 +0000923 if (CT == CT_Can)
924 return CT;
925 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
926 }
927
928 case Expr::CXXConstructExprClass:
929 case Expr::CXXTemporaryObjectExprClass: {
930 CanThrowResult CT = canCalleeThrow(*this, E,
931 cast<CXXConstructExpr>(E)->getConstructor());
932 if (CT == CT_Can)
933 return CT;
934 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
935 }
936
937 case Expr::LambdaExprClass: {
938 const LambdaExpr *Lambda = cast<LambdaExpr>(E);
939 CanThrowResult CT = CT_Cannot;
940 for (LambdaExpr::capture_init_iterator Cap = Lambda->capture_init_begin(),
941 CapEnd = Lambda->capture_init_end();
942 Cap != CapEnd; ++Cap)
943 CT = mergeCanThrow(CT, canThrow(*Cap));
944 return CT;
945 }
946
947 case Expr::CXXNewExprClass: {
948 CanThrowResult CT;
949 if (E->isTypeDependent())
950 CT = CT_Dependent;
951 else
952 CT = canCalleeThrow(*this, E, cast<CXXNewExpr>(E)->getOperatorNew());
953 if (CT == CT_Can)
954 return CT;
955 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
956 }
957
958 case Expr::CXXDeleteExprClass: {
959 CanThrowResult CT;
960 QualType DTy = cast<CXXDeleteExpr>(E)->getDestroyedType();
961 if (DTy.isNull() || DTy->isDependentType()) {
962 CT = CT_Dependent;
963 } else {
964 CT = canCalleeThrow(*this, E,
965 cast<CXXDeleteExpr>(E)->getOperatorDelete());
966 if (const RecordType *RT = DTy->getAs<RecordType>()) {
967 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Eli Friedman0423b762013-06-25 01:24:22 +0000968 const CXXDestructorDecl *DD = RD->getDestructor();
969 if (DD)
970 CT = mergeCanThrow(CT, canCalleeThrow(*this, E, DD));
Richard Smithf623c962012-04-17 00:58:00 +0000971 }
972 if (CT == CT_Can)
973 return CT;
974 }
975 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
976 }
977
978 case Expr::CXXBindTemporaryExprClass: {
979 // The bound temporary has to be destroyed again, which might throw.
980 CanThrowResult CT = canCalleeThrow(*this, E,
981 cast<CXXBindTemporaryExpr>(E)->getTemporary()->getDestructor());
982 if (CT == CT_Can)
983 return CT;
984 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
985 }
986
987 // ObjC message sends are like function calls, but never have exception
988 // specs.
989 case Expr::ObjCMessageExprClass:
990 case Expr::ObjCPropertyRefExprClass:
991 case Expr::ObjCSubscriptRefExprClass:
992 return CT_Can;
993
994 // All the ObjC literals that are implemented as calls are
995 // potentially throwing unless we decide to close off that
996 // possibility.
997 case Expr::ObjCArrayLiteralClass:
998 case Expr::ObjCDictionaryLiteralClass:
Patrick Beard0caa3942012-04-19 00:25:12 +0000999 case Expr::ObjCBoxedExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001000 return CT_Can;
1001
1002 // Many other things have subexpressions, so we have to test those.
1003 // Some are simple:
1004 case Expr::ConditionalOperatorClass:
1005 case Expr::CompoundLiteralExprClass:
1006 case Expr::CXXConstCastExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001007 case Expr::CXXReinterpretCastExprClass:
Richard Smithcc1b96d2013-06-12 22:31:48 +00001008 case Expr::CXXStdInitializerListExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001009 case Expr::DesignatedInitExprClass:
1010 case Expr::ExprWithCleanupsClass:
1011 case Expr::ExtVectorElementExprClass:
1012 case Expr::InitListExprClass:
1013 case Expr::MemberExprClass:
1014 case Expr::ObjCIsaExprClass:
1015 case Expr::ObjCIvarRefExprClass:
1016 case Expr::ParenExprClass:
1017 case Expr::ParenListExprClass:
1018 case Expr::ShuffleVectorExprClass:
Hal Finkelc4d7c822013-09-18 03:29:45 +00001019 case Expr::ConvertVectorExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001020 case Expr::VAArgExprClass:
1021 return canSubExprsThrow(*this, E);
1022
1023 // Some might be dependent for other reasons.
1024 case Expr::ArraySubscriptExprClass:
1025 case Expr::BinaryOperatorClass:
1026 case Expr::CompoundAssignOperatorClass:
1027 case Expr::CStyleCastExprClass:
1028 case Expr::CXXStaticCastExprClass:
1029 case Expr::CXXFunctionalCastExprClass:
1030 case Expr::ImplicitCastExprClass:
1031 case Expr::MaterializeTemporaryExprClass:
1032 case Expr::UnaryOperatorClass: {
1033 CanThrowResult CT = E->isTypeDependent() ? CT_Dependent : CT_Cannot;
1034 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1035 }
1036
1037 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1038 case Expr::StmtExprClass:
1039 return CT_Can;
1040
Richard Smith852c9db2013-04-20 22:23:05 +00001041 case Expr::CXXDefaultArgExprClass:
1042 return canThrow(cast<CXXDefaultArgExpr>(E)->getExpr());
1043
1044 case Expr::CXXDefaultInitExprClass:
1045 return canThrow(cast<CXXDefaultInitExpr>(E)->getExpr());
1046
Richard Smithf623c962012-04-17 00:58:00 +00001047 case Expr::ChooseExprClass:
1048 if (E->isTypeDependent() || E->isValueDependent())
1049 return CT_Dependent;
Eli Friedman75807f22013-07-20 00:40:58 +00001050 return canThrow(cast<ChooseExpr>(E)->getChosenSubExpr());
Richard Smithf623c962012-04-17 00:58:00 +00001051
1052 case Expr::GenericSelectionExprClass:
1053 if (cast<GenericSelectionExpr>(E)->isResultDependent())
1054 return CT_Dependent;
1055 return canThrow(cast<GenericSelectionExpr>(E)->getResultExpr());
1056
1057 // Some expressions are always dependent.
1058 case Expr::CXXDependentScopeMemberExprClass:
1059 case Expr::CXXUnresolvedConstructExprClass:
1060 case Expr::DependentScopeDeclRefExprClass:
1061 return CT_Dependent;
1062
1063 case Expr::AsTypeExprClass:
1064 case Expr::BinaryConditionalOperatorClass:
1065 case Expr::BlockExprClass:
1066 case Expr::CUDAKernelCallExprClass:
1067 case Expr::DeclRefExprClass:
1068 case Expr::ObjCBridgedCastExprClass:
1069 case Expr::ObjCIndirectCopyRestoreExprClass:
1070 case Expr::ObjCProtocolExprClass:
1071 case Expr::ObjCSelectorExprClass:
1072 case Expr::OffsetOfExprClass:
1073 case Expr::PackExpansionExprClass:
1074 case Expr::PseudoObjectExprClass:
1075 case Expr::SubstNonTypeTemplateParmExprClass:
1076 case Expr::SubstNonTypeTemplateParmPackExprClass:
Richard Smithb15fe3a2012-09-12 00:56:43 +00001077 case Expr::FunctionParmPackExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001078 case Expr::UnaryExprOrTypeTraitExprClass:
1079 case Expr::UnresolvedLookupExprClass:
1080 case Expr::UnresolvedMemberExprClass:
1081 // FIXME: Can any of the above throw? If so, when?
1082 return CT_Cannot;
1083
1084 case Expr::AddrLabelExprClass:
1085 case Expr::ArrayTypeTraitExprClass:
1086 case Expr::AtomicExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001087 case Expr::TypeTraitExprClass:
1088 case Expr::CXXBoolLiteralExprClass:
1089 case Expr::CXXNoexceptExprClass:
1090 case Expr::CXXNullPtrLiteralExprClass:
1091 case Expr::CXXPseudoDestructorExprClass:
1092 case Expr::CXXScalarValueInitExprClass:
1093 case Expr::CXXThisExprClass:
1094 case Expr::CXXUuidofExprClass:
1095 case Expr::CharacterLiteralClass:
1096 case Expr::ExpressionTraitExprClass:
1097 case Expr::FloatingLiteralClass:
1098 case Expr::GNUNullExprClass:
1099 case Expr::ImaginaryLiteralClass:
1100 case Expr::ImplicitValueInitExprClass:
1101 case Expr::IntegerLiteralClass:
1102 case Expr::ObjCEncodeExprClass:
1103 case Expr::ObjCStringLiteralClass:
1104 case Expr::ObjCBoolLiteralExprClass:
1105 case Expr::OpaqueValueExprClass:
1106 case Expr::PredefinedExprClass:
1107 case Expr::SizeOfPackExprClass:
1108 case Expr::StringLiteralClass:
Richard Smithf623c962012-04-17 00:58:00 +00001109 // These expressions can never throw.
1110 return CT_Cannot;
1111
John McCall5e77d762013-04-16 07:28:30 +00001112 case Expr::MSPropertyRefExprClass:
1113 llvm_unreachable("Invalid class for expression");
1114
Richard Smithf623c962012-04-17 00:58:00 +00001115#define STMT(CLASS, PARENT) case Expr::CLASS##Class:
1116#define STMT_RANGE(Base, First, Last)
1117#define LAST_STMT_RANGE(BASE, FIRST, LAST)
1118#define EXPR(CLASS, PARENT)
1119#define ABSTRACT_STMT(STMT)
1120#include "clang/AST/StmtNodes.inc"
1121 case Expr::NoStmtClass:
1122 llvm_unreachable("Invalid class for expression");
1123 }
1124 llvm_unreachable("Bogus StmtClass");
1125}
1126
Sebastian Redl4915e632009-10-11 09:03:14 +00001127} // end namespace clang