blob: ab8dcd1ad4cfbb3bddb2c169564da3d187712ed5 [file] [log] [blame]
Sebastian Redldced2262009-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 McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Sebastian Redldced2262009-10-11 09:03:14 +000015#include "clang/AST/CXXInheritance.h"
16#include "clang/AST/Expr.h"
17#include "clang/AST/ExprCXX.h"
Douglas Gregor2eef8292010-03-24 07:14:45 +000018#include "clang/AST/TypeLoc.h"
Douglas Gregore13ad832010-02-12 07:32:17 +000019#include "clang/Basic/Diagnostic.h"
20#include "clang/Basic/SourceManager.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include "clang/Lex/Preprocessor.h"
Sebastian Redldced2262009-10-11 09:03:14 +000022#include "llvm/ADT/SmallPtrSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000023#include "llvm/ADT/SmallString.h"
Sebastian Redldced2262009-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 Redlc3a3b7b2009-10-14 14:38:54 +000033 else if (const MemberPointerType *MPTy = T->getAs<MemberPointerType>())
34 T = MPTy->getPointeeType();
Sebastian Redldced2262009-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 Smith21173b12012-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 Smitha70c3f82012-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 Smith21173b12012-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 Smitha70c3f82012-11-28 22:52:42 +000049 //
50 // We also apply this rule in C++98.
Richard Smith21173b12012-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 Redldced2262009-10-11 09:03:14 +000055
Richard Smitha70c3f82012-11-28 22:52:42 +000056 int Kind = 0;
Richard Smith21173b12012-11-28 22:33:28 +000057 QualType PointeeT = T;
Richard Smitha70c3f82012-11-28 22:52:42 +000058 if (const PointerType *PT = T->getAs<PointerType>()) {
59 PointeeT = PT->getPointeeType();
60 Kind = 1;
Sebastian Redldced2262009-10-11 09:03:14 +000061
Richard Smitha70c3f82012-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 Smith21173b12012-11-28 22:33:28 +000069
Richard Smitha70c3f82012-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 Smith21173b12012-11-28 22:33:28 +000088 RequireCompleteType(Range.getBegin(), PointeeT,
Richard Smitha70c3f82012-11-28 22:52:42 +000089 diag::err_incomplete_in_exception_spec, Kind, Range))
Sebastian Redl491b84c2009-10-14 14:59:48 +000090 return true;
Sebastian Redldced2262009-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 Smithe6975e92012-04-17 00:58:00 +0000113const FunctionProtoType *
114Sema::ResolveExceptionSpec(SourceLocation Loc, const FunctionProtoType *FPT) {
Richard Smithb9d0b762012-07-27 04:22:15 +0000115 if (!isUnresolvedExceptionSpec(FPT->getExceptionSpecType()))
Richard Smithe6975e92012-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 Smithb9d0b762012-07-27 04:22:15 +0000122 // If the exception specification has already been resolved, just return it.
123 if (!isUnresolvedExceptionSpec(SourceFPT->getExceptionSpecType()))
Richard Smithe6975e92012-04-17 00:58:00 +0000124 return SourceFPT;
125
Richard Smithb9d0b762012-07-27 04:22:15 +0000126 // Compute or instantiate the exception specification now.
127 if (FPT->getExceptionSpecType() == EST_Unevaluated)
128 EvaluateImplicitExceptionSpec(Loc, cast<CXXMethodDecl>(SourceDecl));
129 else
130 InstantiateExceptionSpec(Loc, SourceDecl);
Richard Smithe6975e92012-04-17 00:58:00 +0000131
132 return SourceDecl->getType()->castAs<FunctionProtoType>();
133}
134
Richard Smith444d3842012-10-20 08:26:51 +0000135/// Determine whether a function has an implicitly-generated exception
Richard Smith708f69b2012-10-16 23:30:16 +0000136/// specification.
Richard Smith444d3842012-10-20 08:26:51 +0000137static bool hasImplicitExceptionSpec(FunctionDecl *Decl) {
138 if (!isa<CXXDestructorDecl>(Decl) &&
139 Decl->getDeclName().getCXXOverloadedOperator() != OO_Delete &&
140 Decl->getDeclName().getCXXOverloadedOperator() != OO_Array_Delete)
141 return false;
Richard Smith708f69b2012-10-16 23:30:16 +0000142
Richard Smith444d3842012-10-20 08:26:51 +0000143 // If the user didn't declare the function, its exception specification must
144 // be implicit.
145 if (!Decl->getTypeSourceInfo())
146 return true;
147
148 const FunctionProtoType *Ty =
149 Decl->getTypeSourceInfo()->getType()->getAs<FunctionProtoType>();
150 return !Ty->hasExceptionSpec();
Richard Smith708f69b2012-10-16 23:30:16 +0000151}
152
Douglas Gregore13ad832010-02-12 07:32:17 +0000153bool Sema::CheckEquivalentExceptionSpec(FunctionDecl *Old, FunctionDecl *New) {
Sebastian Redl99439d42011-03-15 19:52:30 +0000154 OverloadedOperatorKind OO = New->getDeclName().getCXXOverloadedOperator();
155 bool IsOperatorNew = OO == OO_New || OO == OO_Array_New;
Douglas Gregor2eef8292010-03-24 07:14:45 +0000156 bool MissingExceptionSpecification = false;
Douglas Gregore13ad832010-02-12 07:32:17 +0000157 bool MissingEmptyExceptionSpecification = false;
Francois Picheteedd4672011-03-19 23:05:18 +0000158 unsigned DiagID = diag::err_mismatched_exception_spec;
David Blaikie4e4d0842012-03-11 07:00:24 +0000159 if (getLangOpts().MicrosoftExt)
Francois Pichetcf320c62011-04-22 08:25:24 +0000160 DiagID = diag::warn_mismatched_exception_spec;
Richard Smithe6975e92012-04-17 00:58:00 +0000161
Richard Smith708f69b2012-10-16 23:30:16 +0000162 // Check the types as written: they must match before any exception
163 // specification adjustment is applied.
164 if (!CheckEquivalentExceptionSpec(
165 PDiag(DiagID), PDiag(diag::note_previous_declaration),
Richard Smith444d3842012-10-20 08:26:51 +0000166 Old->getType()->getAs<FunctionProtoType>(), Old->getLocation(),
167 New->getType()->getAs<FunctionProtoType>(), New->getLocation(),
Richard Smith708f69b2012-10-16 23:30:16 +0000168 &MissingExceptionSpecification, &MissingEmptyExceptionSpecification,
Richard Smith444d3842012-10-20 08:26:51 +0000169 /*AllowNoexceptAllMatchWithNoSpec=*/true, IsOperatorNew)) {
170 // C++11 [except.spec]p4 [DR1492]:
171 // If a declaration of a function has an implicit
172 // exception-specification, other declarations of the function shall
173 // not specify an exception-specification.
Richard Smith80ad52f2013-01-02 11:42:31 +0000174 if (getLangOpts().CPlusPlus11 &&
Richard Smith444d3842012-10-20 08:26:51 +0000175 hasImplicitExceptionSpec(Old) != hasImplicitExceptionSpec(New)) {
176 Diag(New->getLocation(), diag::ext_implicit_exception_spec_mismatch)
177 << hasImplicitExceptionSpec(Old);
178 if (!Old->getLocation().isInvalid())
179 Diag(Old->getLocation(), diag::note_previous_declaration);
180 }
Douglas Gregore13ad832010-02-12 07:32:17 +0000181 return false;
Richard Smith444d3842012-10-20 08:26:51 +0000182 }
Douglas Gregore13ad832010-02-12 07:32:17 +0000183
184 // The failure was something other than an empty exception
185 // specification; return an error.
Douglas Gregor2eef8292010-03-24 07:14:45 +0000186 if (!MissingExceptionSpecification && !MissingEmptyExceptionSpecification)
Douglas Gregore13ad832010-02-12 07:32:17 +0000187 return true;
188
Richard Smith444d3842012-10-20 08:26:51 +0000189 const FunctionProtoType *NewProto =
190 New->getType()->getAs<FunctionProtoType>();
John McCalle23cf432010-12-14 08:05:40 +0000191
Douglas Gregore13ad832010-02-12 07:32:17 +0000192 // The new function declaration is only missing an empty exception
193 // specification "throw()". If the throw() specification came from a
194 // function in a system header that has C linkage, just add an empty
195 // exception specification to the "new" declaration. This is an
196 // egregious workaround for glibc, which adds throw() specifications
197 // to many libc functions as an optimization. Unfortunately, that
198 // optimization isn't permitted by the C++ standard, so we're forced
199 // to work around it here.
John McCalle23cf432010-12-14 08:05:40 +0000200 if (MissingEmptyExceptionSpecification && NewProto &&
Douglas Gregor2eef8292010-03-24 07:14:45 +0000201 (Old->getLocation().isInvalid() ||
202 Context.getSourceManager().isInSystemHeader(Old->getLocation())) &&
Douglas Gregore13ad832010-02-12 07:32:17 +0000203 Old->isExternC()) {
John McCalle23cf432010-12-14 08:05:40 +0000204 FunctionProtoType::ExtProtoInfo EPI = NewProto->getExtProtoInfo();
Sebastian Redl60618fa2011-03-12 11:50:43 +0000205 EPI.ExceptionSpecType = EST_DynamicNone;
Jordan Rosebea522f2013-03-08 21:51:21 +0000206 QualType NewType =
207 Context.getFunctionType(NewProto->getResultType(),
208 ArrayRef<QualType>(NewProto->arg_type_begin(),
209 NewProto->getNumArgs()),
210 EPI);
Douglas Gregore13ad832010-02-12 07:32:17 +0000211 New->setType(NewType);
212 return false;
213 }
214
John McCalle23cf432010-12-14 08:05:40 +0000215 if (MissingExceptionSpecification && NewProto) {
Richard Smith444d3842012-10-20 08:26:51 +0000216 const FunctionProtoType *OldProto =
217 Old->getType()->getAs<FunctionProtoType>();
Douglas Gregor2eef8292010-03-24 07:14:45 +0000218
John McCalle23cf432010-12-14 08:05:40 +0000219 FunctionProtoType::ExtProtoInfo EPI = NewProto->getExtProtoInfo();
Sebastian Redl60618fa2011-03-12 11:50:43 +0000220 EPI.ExceptionSpecType = OldProto->getExceptionSpecType();
221 if (EPI.ExceptionSpecType == EST_Dynamic) {
222 EPI.NumExceptions = OldProto->getNumExceptions();
223 EPI.Exceptions = OldProto->exception_begin();
224 } else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) {
225 // FIXME: We can't just take the expression from the old prototype. It
226 // likely contains references to the old prototype's parameters.
227 }
John McCalle23cf432010-12-14 08:05:40 +0000228
Douglas Gregor2eef8292010-03-24 07:14:45 +0000229 // Update the type of the function with the appropriate exception
230 // specification.
Jordan Rosebea522f2013-03-08 21:51:21 +0000231 QualType NewType =
232 Context.getFunctionType(NewProto->getResultType(),
233 ArrayRef<QualType>(NewProto->arg_type_begin(),
234 NewProto->getNumArgs()),
235 EPI);
Douglas Gregor2eef8292010-03-24 07:14:45 +0000236 New->setType(NewType);
237
238 // If exceptions are disabled, suppress the warning about missing
239 // exception specifications for new and delete operators.
David Blaikie4e4d0842012-03-11 07:00:24 +0000240 if (!getLangOpts().CXXExceptions) {
Douglas Gregor2eef8292010-03-24 07:14:45 +0000241 switch (New->getDeclName().getCXXOverloadedOperator()) {
242 case OO_New:
243 case OO_Array_New:
244 case OO_Delete:
245 case OO_Array_Delete:
246 if (New->getDeclContext()->isTranslationUnit())
247 return false;
248 break;
249
250 default:
251 break;
252 }
253 }
254
255 // Warn about the lack of exception specification.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000256 SmallString<128> ExceptionSpecString;
Douglas Gregor2eef8292010-03-24 07:14:45 +0000257 llvm::raw_svector_ostream OS(ExceptionSpecString);
Sebastian Redl60618fa2011-03-12 11:50:43 +0000258 switch (OldProto->getExceptionSpecType()) {
259 case EST_DynamicNone:
260 OS << "throw()";
261 break;
262
263 case EST_Dynamic: {
264 OS << "throw(";
265 bool OnFirstException = true;
266 for (FunctionProtoType::exception_iterator E = OldProto->exception_begin(),
267 EEnd = OldProto->exception_end();
268 E != EEnd;
269 ++E) {
270 if (OnFirstException)
271 OnFirstException = false;
272 else
273 OS << ", ";
274
Douglas Gregor8987b232011-09-27 23:30:47 +0000275 OS << E->getAsString(getPrintingPolicy());
Sebastian Redl60618fa2011-03-12 11:50:43 +0000276 }
277 OS << ")";
278 break;
Douglas Gregor2eef8292010-03-24 07:14:45 +0000279 }
Sebastian Redl60618fa2011-03-12 11:50:43 +0000280
281 case EST_BasicNoexcept:
282 OS << "noexcept";
283 break;
284
285 case EST_ComputedNoexcept:
286 OS << "noexcept(";
Richard Smithd1420c62012-08-16 03:56:14 +0000287 OldProto->getNoexceptExpr()->printPretty(OS, 0, getPrintingPolicy());
Sebastian Redl60618fa2011-03-12 11:50:43 +0000288 OS << ")";
289 break;
290
291 default:
David Blaikieb219cfc2011-09-23 05:06:16 +0000292 llvm_unreachable("This spec type is compatible with none.");
Sebastian Redl60618fa2011-03-12 11:50:43 +0000293 }
Douglas Gregor2eef8292010-03-24 07:14:45 +0000294 OS.flush();
295
Abramo Bagnara796aa442011-03-12 11:17:06 +0000296 SourceLocation FixItLoc;
Douglas Gregor2eef8292010-03-24 07:14:45 +0000297 if (TypeSourceInfo *TSInfo = New->getTypeSourceInfo()) {
Abramo Bagnara723df242010-12-14 22:11:44 +0000298 TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens();
David Blaikie39e6ab42013-02-18 22:06:02 +0000299 if (FunctionTypeLoc FTLoc = TL.getAs<FunctionTypeLoc>())
300 FixItLoc = PP.getLocForEndOfToken(FTLoc.getLocalRangeEnd());
Douglas Gregor2eef8292010-03-24 07:14:45 +0000301 }
302
Abramo Bagnara796aa442011-03-12 11:17:06 +0000303 if (FixItLoc.isInvalid())
Douglas Gregor2eef8292010-03-24 07:14:45 +0000304 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()
Abramo Bagnara796aa442011-03-12 11:17:06 +0000311 << FixItHint::CreateInsertion(FixItLoc, " " + OS.str().str());
Douglas Gregor2eef8292010-03-24 07:14:45 +0000312 }
313
314 if (!Old->getLocation().isInvalid())
315 Diag(Old->getLocation(), diag::note_previous_declaration);
316
317 return false;
318 }
319
Francois Picheteedd4672011-03-19 23:05:18 +0000320 Diag(New->getLocation(), DiagID);
Douglas Gregore13ad832010-02-12 07:32:17 +0000321 Diag(Old->getLocation(), diag::note_previous_declaration);
322 return true;
323}
324
Sebastian Redldced2262009-10-11 09:03:14 +0000325/// CheckEquivalentExceptionSpec - Check if the two types have equivalent
326/// exception specifications. Exception specifications are equivalent if
327/// they allow exactly the same set of exception types. It does not matter how
328/// that is achieved. See C++ [except.spec]p2.
329bool Sema::CheckEquivalentExceptionSpec(
330 const FunctionProtoType *Old, SourceLocation OldLoc,
331 const FunctionProtoType *New, SourceLocation NewLoc) {
Francois Picheteedd4672011-03-19 23:05:18 +0000332 unsigned DiagID = diag::err_mismatched_exception_spec;
David Blaikie4e4d0842012-03-11 07:00:24 +0000333 if (getLangOpts().MicrosoftExt)
Francois Pichetcf320c62011-04-22 08:25:24 +0000334 DiagID = diag::warn_mismatched_exception_spec;
Richard Smith708f69b2012-10-16 23:30:16 +0000335 return CheckEquivalentExceptionSpec(PDiag(DiagID),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000336 PDiag(diag::note_previous_declaration),
Sebastian Redldced2262009-10-11 09:03:14 +0000337 Old, OldLoc, New, NewLoc);
338}
339
Sebastian Redl60618fa2011-03-12 11:50:43 +0000340/// CheckEquivalentExceptionSpec - Check if the two types have compatible
341/// exception specifications. See C++ [except.spec]p3.
Richard Smith444d3842012-10-20 08:26:51 +0000342///
343/// \return \c false if the exception specifications match, \c true if there is
344/// a problem. If \c true is returned, either a diagnostic has already been
345/// produced or \c *MissingExceptionSpecification is set to \c true.
Sebastian Redl60618fa2011-03-12 11:50:43 +0000346bool Sema::CheckEquivalentExceptionSpec(const PartialDiagnostic &DiagID,
Douglas Gregor2eef8292010-03-24 07:14:45 +0000347 const PartialDiagnostic & NoteID,
Sebastian Redl60618fa2011-03-12 11:50:43 +0000348 const FunctionProtoType *Old,
Douglas Gregor2eef8292010-03-24 07:14:45 +0000349 SourceLocation OldLoc,
Sebastian Redl60618fa2011-03-12 11:50:43 +0000350 const FunctionProtoType *New,
Douglas Gregor2eef8292010-03-24 07:14:45 +0000351 SourceLocation NewLoc,
352 bool *MissingExceptionSpecification,
Sebastian Redl60618fa2011-03-12 11:50:43 +0000353 bool*MissingEmptyExceptionSpecification,
Sebastian Redl99439d42011-03-15 19:52:30 +0000354 bool AllowNoexceptAllMatchWithNoSpec,
355 bool IsOperatorNew) {
John McCall811d0be2010-05-28 08:37:35 +0000356 // Just completely ignore this under -fno-exceptions.
David Blaikie4e4d0842012-03-11 07:00:24 +0000357 if (!getLangOpts().CXXExceptions)
John McCall811d0be2010-05-28 08:37:35 +0000358 return false;
359
Douglas Gregor2eef8292010-03-24 07:14:45 +0000360 if (MissingExceptionSpecification)
361 *MissingExceptionSpecification = false;
362
Douglas Gregore13ad832010-02-12 07:32:17 +0000363 if (MissingEmptyExceptionSpecification)
364 *MissingEmptyExceptionSpecification = false;
365
Richard Smithe6975e92012-04-17 00:58:00 +0000366 Old = ResolveExceptionSpec(NewLoc, Old);
367 if (!Old)
368 return false;
369 New = ResolveExceptionSpec(NewLoc, New);
370 if (!New)
371 return false;
372
Sebastian Redl60618fa2011-03-12 11:50:43 +0000373 // C++0x [except.spec]p3: Two exception-specifications are compatible if:
374 // - both are non-throwing, regardless of their form,
375 // - both have the form noexcept(constant-expression) and the constant-
376 // expressions are equivalent,
Sebastian Redl60618fa2011-03-12 11:50:43 +0000377 // - both are dynamic-exception-specifications that have the same set of
378 // adjusted types.
379 //
380 // C++0x [except.spec]p12: An exception-specifcation is non-throwing if it is
381 // of the form throw(), noexcept, or noexcept(constant-expression) where the
382 // constant-expression yields true.
383 //
Sebastian Redl60618fa2011-03-12 11:50:43 +0000384 // C++0x [except.spec]p4: If any declaration of a function has an exception-
385 // specifier that is not a noexcept-specification allowing all exceptions,
386 // all declarations [...] of that function shall have a compatible
387 // exception-specification.
388 //
389 // That last point basically means that noexcept(false) matches no spec.
390 // It's considered when AllowNoexceptAllMatchWithNoSpec is true.
391
392 ExceptionSpecificationType OldEST = Old->getExceptionSpecType();
393 ExceptionSpecificationType NewEST = New->getExceptionSpecType();
394
Richard Smithb9d0b762012-07-27 04:22:15 +0000395 assert(!isUnresolvedExceptionSpec(OldEST) &&
396 !isUnresolvedExceptionSpec(NewEST) &&
Richard Smith7a614d82011-06-11 17:19:42 +0000397 "Shouldn't see unknown exception specifications here");
398
Sebastian Redl60618fa2011-03-12 11:50:43 +0000399 // Shortcut the case where both have no spec.
400 if (OldEST == EST_None && NewEST == EST_None)
401 return false;
402
Sebastian Redl8026f6d2011-03-13 17:09:40 +0000403 FunctionProtoType::NoexceptResult OldNR = Old->getNoexceptSpec(Context);
404 FunctionProtoType::NoexceptResult NewNR = New->getNoexceptSpec(Context);
Sebastian Redl60618fa2011-03-12 11:50:43 +0000405 if (OldNR == FunctionProtoType::NR_BadNoexcept ||
406 NewNR == FunctionProtoType::NR_BadNoexcept)
407 return false;
408
409 // Dependent noexcept specifiers are compatible with each other, but nothing
410 // else.
411 // One noexcept is compatible with another if the argument is the same
412 if (OldNR == NewNR &&
413 OldNR != FunctionProtoType::NR_NoNoexcept &&
414 NewNR != FunctionProtoType::NR_NoNoexcept)
415 return false;
416 if (OldNR != NewNR &&
417 OldNR != FunctionProtoType::NR_NoNoexcept &&
418 NewNR != FunctionProtoType::NR_NoNoexcept) {
419 Diag(NewLoc, DiagID);
420 if (NoteID.getDiagID() != 0)
421 Diag(OldLoc, NoteID);
422 return true;
Douglas Gregor5b6f7692010-08-30 15:04:51 +0000423 }
424
Sebastian Redl60618fa2011-03-12 11:50:43 +0000425 // The MS extension throw(...) is compatible with itself.
426 if (OldEST == EST_MSAny && NewEST == EST_MSAny)
Sebastian Redldced2262009-10-11 09:03:14 +0000427 return false;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000428
429 // It's also compatible with no spec.
430 if ((OldEST == EST_None && NewEST == EST_MSAny) ||
431 (OldEST == EST_MSAny && NewEST == EST_None))
432 return false;
433
434 // It's also compatible with noexcept(false).
435 if (OldEST == EST_MSAny && NewNR == FunctionProtoType::NR_Throw)
436 return false;
437 if (NewEST == EST_MSAny && OldNR == FunctionProtoType::NR_Throw)
438 return false;
439
440 // As described above, noexcept(false) matches no spec only for functions.
441 if (AllowNoexceptAllMatchWithNoSpec) {
442 if (OldEST == EST_None && NewNR == FunctionProtoType::NR_Throw)
443 return false;
444 if (NewEST == EST_None && OldNR == FunctionProtoType::NR_Throw)
445 return false;
446 }
447
448 // Any non-throwing specifications are compatible.
449 bool OldNonThrowing = OldNR == FunctionProtoType::NR_Nothrow ||
450 OldEST == EST_DynamicNone;
451 bool NewNonThrowing = NewNR == FunctionProtoType::NR_Nothrow ||
452 NewEST == EST_DynamicNone;
453 if (OldNonThrowing && NewNonThrowing)
454 return false;
455
Sebastian Redl99439d42011-03-15 19:52:30 +0000456 // As a special compatibility feature, under C++0x we accept no spec and
457 // throw(std::bad_alloc) as equivalent for operator new and operator new[].
458 // This is because the implicit declaration changed, but old code would break.
Richard Smith80ad52f2013-01-02 11:42:31 +0000459 if (getLangOpts().CPlusPlus11 && IsOperatorNew) {
Sebastian Redl99439d42011-03-15 19:52:30 +0000460 const FunctionProtoType *WithExceptions = 0;
461 if (OldEST == EST_None && NewEST == EST_Dynamic)
462 WithExceptions = New;
463 else if (OldEST == EST_Dynamic && NewEST == EST_None)
464 WithExceptions = Old;
465 if (WithExceptions && WithExceptions->getNumExceptions() == 1) {
466 // One has no spec, the other throw(something). If that something is
467 // std::bad_alloc, all conditions are met.
468 QualType Exception = *WithExceptions->exception_begin();
469 if (CXXRecordDecl *ExRecord = Exception->getAsCXXRecordDecl()) {
470 IdentifierInfo* Name = ExRecord->getIdentifier();
471 if (Name && Name->getName() == "bad_alloc") {
472 // It's called bad_alloc, but is it in std?
473 DeclContext* DC = ExRecord->getDeclContext();
Sebastian Redld8f2e8e2011-03-15 20:41:09 +0000474 DC = DC->getEnclosingNamespaceContext();
475 if (NamespaceDecl* NS = dyn_cast<NamespaceDecl>(DC)) {
Sebastian Redl99439d42011-03-15 19:52:30 +0000476 IdentifierInfo* NSName = NS->getIdentifier();
Sebastian Redld8f2e8e2011-03-15 20:41:09 +0000477 DC = DC->getParent();
Sebastian Redl99439d42011-03-15 19:52:30 +0000478 if (NSName && NSName->getName() == "std" &&
Sebastian Redld8f2e8e2011-03-15 20:41:09 +0000479 DC->getEnclosingNamespaceContext()->isTranslationUnit()) {
Sebastian Redl99439d42011-03-15 19:52:30 +0000480 return false;
Sebastian Redld8f2e8e2011-03-15 20:41:09 +0000481 }
Sebastian Redl99439d42011-03-15 19:52:30 +0000482 }
483 }
484 }
485 }
486 }
487
Sebastian Redl60618fa2011-03-12 11:50:43 +0000488 // At this point, the only remaining valid case is two matching dynamic
489 // specifications. We return here unless both specifications are dynamic.
490 if (OldEST != EST_Dynamic || NewEST != EST_Dynamic) {
Douglas Gregor2eef8292010-03-24 07:14:45 +0000491 if (MissingExceptionSpecification && Old->hasExceptionSpec() &&
Douglas Gregore13ad832010-02-12 07:32:17 +0000492 !New->hasExceptionSpec()) {
Douglas Gregor2eef8292010-03-24 07:14:45 +0000493 // The old type has an exception specification of some sort, but
494 // the new type does not.
495 *MissingExceptionSpecification = true;
496
Sebastian Redl60618fa2011-03-12 11:50:43 +0000497 if (MissingEmptyExceptionSpecification && OldNonThrowing) {
498 // The old type has a throw() or noexcept(true) exception specification
499 // and the new type has no exception specification, and the caller asked
Douglas Gregor2eef8292010-03-24 07:14:45 +0000500 // to handle this itself.
501 *MissingEmptyExceptionSpecification = true;
502 }
503
Douglas Gregore13ad832010-02-12 07:32:17 +0000504 return true;
505 }
506
Sebastian Redldced2262009-10-11 09:03:14 +0000507 Diag(NewLoc, DiagID);
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000508 if (NoteID.getDiagID() != 0)
Sebastian Redldced2262009-10-11 09:03:14 +0000509 Diag(OldLoc, NoteID);
510 return true;
511 }
512
Sebastian Redl60618fa2011-03-12 11:50:43 +0000513 assert(OldEST == EST_Dynamic && NewEST == EST_Dynamic &&
514 "Exception compatibility logic error: non-dynamic spec slipped through.");
515
Sebastian Redldced2262009-10-11 09:03:14 +0000516 bool Success = true;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000517 // Both have a dynamic exception spec. Collect the first set, then compare
Sebastian Redldced2262009-10-11 09:03:14 +0000518 // to the second.
Sebastian Redl1219d152009-10-14 15:06:25 +0000519 llvm::SmallPtrSet<CanQualType, 8> OldTypes, NewTypes;
Sebastian Redldced2262009-10-11 09:03:14 +0000520 for (FunctionProtoType::exception_iterator I = Old->exception_begin(),
521 E = Old->exception_end(); I != E; ++I)
Sebastian Redl1219d152009-10-14 15:06:25 +0000522 OldTypes.insert(Context.getCanonicalType(*I).getUnqualifiedType());
Sebastian Redldced2262009-10-11 09:03:14 +0000523
524 for (FunctionProtoType::exception_iterator I = New->exception_begin(),
Sebastian Redl5db4d902009-10-11 09:11:23 +0000525 E = New->exception_end(); I != E && Success; ++I) {
Sebastian Redl1219d152009-10-14 15:06:25 +0000526 CanQualType TypePtr = Context.getCanonicalType(*I).getUnqualifiedType();
Sebastian Redl5db4d902009-10-11 09:11:23 +0000527 if(OldTypes.count(TypePtr))
528 NewTypes.insert(TypePtr);
529 else
530 Success = false;
531 }
Sebastian Redldced2262009-10-11 09:03:14 +0000532
Sebastian Redl5db4d902009-10-11 09:11:23 +0000533 Success = Success && OldTypes.size() == NewTypes.size();
Sebastian Redldced2262009-10-11 09:03:14 +0000534
535 if (Success) {
536 return false;
537 }
538 Diag(NewLoc, DiagID);
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000539 if (NoteID.getDiagID() != 0)
Sebastian Redldced2262009-10-11 09:03:14 +0000540 Diag(OldLoc, NoteID);
541 return true;
542}
543
544/// CheckExceptionSpecSubset - Check whether the second function type's
545/// exception specification is a subset (or equivalent) of the first function
546/// type. This is used by override and pointer assignment checks.
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000547bool Sema::CheckExceptionSpecSubset(
548 const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID,
Sebastian Redldced2262009-10-11 09:03:14 +0000549 const FunctionProtoType *Superset, SourceLocation SuperLoc,
550 const FunctionProtoType *Subset, SourceLocation SubLoc) {
John McCall811d0be2010-05-28 08:37:35 +0000551
552 // Just auto-succeed under -fno-exceptions.
David Blaikie4e4d0842012-03-11 07:00:24 +0000553 if (!getLangOpts().CXXExceptions)
John McCall811d0be2010-05-28 08:37:35 +0000554 return false;
555
Sebastian Redldced2262009-10-11 09:03:14 +0000556 // FIXME: As usual, we could be more specific in our error messages, but
557 // that better waits until we've got types with source locations.
558
559 if (!SubLoc.isValid())
560 SubLoc = SuperLoc;
561
Richard Smithe6975e92012-04-17 00:58:00 +0000562 // Resolve the exception specifications, if needed.
563 Superset = ResolveExceptionSpec(SuperLoc, Superset);
564 if (!Superset)
565 return false;
566 Subset = ResolveExceptionSpec(SubLoc, Subset);
567 if (!Subset)
568 return false;
569
Sebastian Redl60618fa2011-03-12 11:50:43 +0000570 ExceptionSpecificationType SuperEST = Superset->getExceptionSpecType();
571
Sebastian Redldced2262009-10-11 09:03:14 +0000572 // If superset contains everything, we're done.
Sebastian Redl60618fa2011-03-12 11:50:43 +0000573 if (SuperEST == EST_None || SuperEST == EST_MSAny)
Sebastian Redldced2262009-10-11 09:03:14 +0000574 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
575
Sebastian Redl60618fa2011-03-12 11:50:43 +0000576 // If there are dependent noexcept specs, assume everything is fine. Unlike
577 // with the equivalency check, this is safe in this case, because we don't
578 // want to merge declarations. Checks after instantiation will catch any
579 // omissions we make here.
580 // We also shortcut checking if a noexcept expression was bad.
581
Sebastian Redl8026f6d2011-03-13 17:09:40 +0000582 FunctionProtoType::NoexceptResult SuperNR =Superset->getNoexceptSpec(Context);
Sebastian Redl60618fa2011-03-12 11:50:43 +0000583 if (SuperNR == FunctionProtoType::NR_BadNoexcept ||
584 SuperNR == FunctionProtoType::NR_Dependent)
585 return false;
586
587 // Another case of the superset containing everything.
588 if (SuperNR == FunctionProtoType::NR_Throw)
589 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
590
591 ExceptionSpecificationType SubEST = Subset->getExceptionSpecType();
592
Richard Smithb9d0b762012-07-27 04:22:15 +0000593 assert(!isUnresolvedExceptionSpec(SuperEST) &&
594 !isUnresolvedExceptionSpec(SubEST) &&
Richard Smith7a614d82011-06-11 17:19:42 +0000595 "Shouldn't see unknown exception specifications here");
596
Sebastian Redldced2262009-10-11 09:03:14 +0000597 // It does not. If the subset contains everything, we've failed.
Sebastian Redl60618fa2011-03-12 11:50:43 +0000598 if (SubEST == EST_None || SubEST == EST_MSAny) {
Sebastian Redldced2262009-10-11 09:03:14 +0000599 Diag(SubLoc, DiagID);
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000600 if (NoteID.getDiagID() != 0)
Sebastian Redldced2262009-10-11 09:03:14 +0000601 Diag(SuperLoc, NoteID);
602 return true;
603 }
604
Sebastian Redl8026f6d2011-03-13 17:09:40 +0000605 FunctionProtoType::NoexceptResult SubNR = Subset->getNoexceptSpec(Context);
Sebastian Redl60618fa2011-03-12 11:50:43 +0000606 if (SubNR == FunctionProtoType::NR_BadNoexcept ||
607 SubNR == FunctionProtoType::NR_Dependent)
608 return false;
609
610 // Another case of the subset containing everything.
611 if (SubNR == FunctionProtoType::NR_Throw) {
612 Diag(SubLoc, DiagID);
613 if (NoteID.getDiagID() != 0)
614 Diag(SuperLoc, NoteID);
615 return true;
616 }
617
618 // If the subset contains nothing, we're done.
619 if (SubEST == EST_DynamicNone || SubNR == FunctionProtoType::NR_Nothrow)
620 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
621
622 // Otherwise, if the superset contains nothing, we've failed.
623 if (SuperEST == EST_DynamicNone || SuperNR == FunctionProtoType::NR_Nothrow) {
624 Diag(SubLoc, DiagID);
625 if (NoteID.getDiagID() != 0)
626 Diag(SuperLoc, NoteID);
627 return true;
628 }
629
630 assert(SuperEST == EST_Dynamic && SubEST == EST_Dynamic &&
631 "Exception spec subset: non-dynamic case slipped through.");
632
633 // Neither contains everything or nothing. Do a proper comparison.
Sebastian Redldced2262009-10-11 09:03:14 +0000634 for (FunctionProtoType::exception_iterator SubI = Subset->exception_begin(),
635 SubE = Subset->exception_end(); SubI != SubE; ++SubI) {
636 // Take one type from the subset.
637 QualType CanonicalSubT = Context.getCanonicalType(*SubI);
Sebastian Redlc3a3b7b2009-10-14 14:38:54 +0000638 // Unwrap pointers and references so that we can do checks within a class
639 // hierarchy. Don't unwrap member pointers; they don't have hierarchy
640 // conversions on the pointee.
Sebastian Redldced2262009-10-11 09:03:14 +0000641 bool SubIsPointer = false;
642 if (const ReferenceType *RefTy = CanonicalSubT->getAs<ReferenceType>())
643 CanonicalSubT = RefTy->getPointeeType();
644 if (const PointerType *PtrTy = CanonicalSubT->getAs<PointerType>()) {
645 CanonicalSubT = PtrTy->getPointeeType();
646 SubIsPointer = true;
647 }
648 bool SubIsClass = CanonicalSubT->isRecordType();
Douglas Gregora4923eb2009-11-16 21:35:15 +0000649 CanonicalSubT = CanonicalSubT.getLocalUnqualifiedType();
Sebastian Redldced2262009-10-11 09:03:14 +0000650
651 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
652 /*DetectVirtual=*/false);
653
654 bool Contained = false;
655 // Make sure it's in the superset.
656 for (FunctionProtoType::exception_iterator SuperI =
657 Superset->exception_begin(), SuperE = Superset->exception_end();
658 SuperI != SuperE; ++SuperI) {
659 QualType CanonicalSuperT = Context.getCanonicalType(*SuperI);
660 // SubT must be SuperT or derived from it, or pointer or reference to
661 // such types.
662 if (const ReferenceType *RefTy = CanonicalSuperT->getAs<ReferenceType>())
663 CanonicalSuperT = RefTy->getPointeeType();
664 if (SubIsPointer) {
665 if (const PointerType *PtrTy = CanonicalSuperT->getAs<PointerType>())
666 CanonicalSuperT = PtrTy->getPointeeType();
667 else {
668 continue;
669 }
670 }
Douglas Gregora4923eb2009-11-16 21:35:15 +0000671 CanonicalSuperT = CanonicalSuperT.getLocalUnqualifiedType();
Sebastian Redldced2262009-10-11 09:03:14 +0000672 // If the types are the same, move on to the next type in the subset.
673 if (CanonicalSubT == CanonicalSuperT) {
674 Contained = true;
675 break;
676 }
677
678 // Otherwise we need to check the inheritance.
679 if (!SubIsClass || !CanonicalSuperT->isRecordType())
680 continue;
681
682 Paths.clear();
683 if (!IsDerivedFrom(CanonicalSubT, CanonicalSuperT, Paths))
684 continue;
685
Douglas Gregore0d5fe22010-05-21 20:29:55 +0000686 if (Paths.isAmbiguous(Context.getCanonicalType(CanonicalSuperT)))
Sebastian Redldced2262009-10-11 09:03:14 +0000687 continue;
688
John McCall6b2accb2010-02-10 09:31:12 +0000689 // Do this check from a context without privileges.
John McCall58e6f342010-03-16 05:22:47 +0000690 switch (CheckBaseClassAccess(SourceLocation(),
John McCall6b2accb2010-02-10 09:31:12 +0000691 CanonicalSuperT, CanonicalSubT,
692 Paths.front(),
John McCall58e6f342010-03-16 05:22:47 +0000693 /*Diagnostic*/ 0,
John McCall6b2accb2010-02-10 09:31:12 +0000694 /*ForceCheck*/ true,
John McCall58e6f342010-03-16 05:22:47 +0000695 /*ForceUnprivileged*/ true)) {
John McCall6b2accb2010-02-10 09:31:12 +0000696 case AR_accessible: break;
697 case AR_inaccessible: continue;
698 case AR_dependent:
699 llvm_unreachable("access check dependent for unprivileged context");
John McCall6b2accb2010-02-10 09:31:12 +0000700 case AR_delayed:
701 llvm_unreachable("access check delayed in non-declaration");
John McCall6b2accb2010-02-10 09:31:12 +0000702 }
Sebastian Redldced2262009-10-11 09:03:14 +0000703
704 Contained = true;
705 break;
706 }
707 if (!Contained) {
708 Diag(SubLoc, DiagID);
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000709 if (NoteID.getDiagID() != 0)
Sebastian Redldced2262009-10-11 09:03:14 +0000710 Diag(SuperLoc, NoteID);
711 return true;
712 }
713 }
714 // We've run half the gauntlet.
715 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
716}
717
718static bool CheckSpecForTypesEquivalent(Sema &S,
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000719 const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID,
Sebastian Redldced2262009-10-11 09:03:14 +0000720 QualType Target, SourceLocation TargetLoc,
721 QualType Source, SourceLocation SourceLoc)
722{
723 const FunctionProtoType *TFunc = GetUnderlyingFunction(Target);
724 if (!TFunc)
725 return false;
726 const FunctionProtoType *SFunc = GetUnderlyingFunction(Source);
727 if (!SFunc)
728 return false;
729
730 return S.CheckEquivalentExceptionSpec(DiagID, NoteID, TFunc, TargetLoc,
731 SFunc, SourceLoc);
732}
733
734/// CheckParamExceptionSpec - Check if the parameter and return types of the
735/// two functions have equivalent exception specs. This is part of the
736/// assignment and override compatibility check. We do not check the parameters
737/// of parameter function pointers recursively, as no sane programmer would
738/// even be able to write such a function type.
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000739bool Sema::CheckParamExceptionSpec(const PartialDiagnostic & NoteID,
Sebastian Redldced2262009-10-11 09:03:14 +0000740 const FunctionProtoType *Target, SourceLocation TargetLoc,
741 const FunctionProtoType *Source, SourceLocation SourceLoc)
742{
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000743 if (CheckSpecForTypesEquivalent(*this,
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000744 PDiag(diag::err_deep_exception_specs_differ) << 0,
745 PDiag(),
Sebastian Redldced2262009-10-11 09:03:14 +0000746 Target->getResultType(), TargetLoc,
747 Source->getResultType(), SourceLoc))
748 return true;
749
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000750 // We shouldn't even be testing this unless the arguments are otherwise
Sebastian Redldced2262009-10-11 09:03:14 +0000751 // compatible.
752 assert(Target->getNumArgs() == Source->getNumArgs() &&
753 "Functions have different argument counts.");
754 for (unsigned i = 0, E = Target->getNumArgs(); i != E; ++i) {
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000755 if (CheckSpecForTypesEquivalent(*this,
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000756 PDiag(diag::err_deep_exception_specs_differ) << 1,
757 PDiag(),
Sebastian Redldced2262009-10-11 09:03:14 +0000758 Target->getArgType(i), TargetLoc,
759 Source->getArgType(i), SourceLoc))
760 return true;
761 }
762 return false;
763}
764
765bool Sema::CheckExceptionSpecCompatibility(Expr *From, QualType ToType)
766{
767 // First we check for applicability.
768 // Target type must be a function, function pointer or function reference.
769 const FunctionProtoType *ToFunc = GetUnderlyingFunction(ToType);
770 if (!ToFunc)
771 return false;
772
773 // SourceType must be a function or function pointer.
774 const FunctionProtoType *FromFunc = GetUnderlyingFunction(From->getType());
775 if (!FromFunc)
776 return false;
777
778 // Now we've got the correct types on both sides, check their compatibility.
779 // This means that the source of the conversion can only throw a subset of
780 // the exceptions of the target, and any exception specs on arguments or
781 // return types must be equivalent.
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000782 return CheckExceptionSpecSubset(PDiag(diag::err_incompatible_exception_specs),
783 PDiag(), ToFunc,
784 From->getSourceRange().getBegin(),
Sebastian Redldced2262009-10-11 09:03:14 +0000785 FromFunc, SourceLocation());
786}
787
788bool Sema::CheckOverridingFunctionExceptionSpec(const CXXMethodDecl *New,
789 const CXXMethodDecl *Old) {
Richard Smith80ad52f2013-01-02 11:42:31 +0000790 if (getLangOpts().CPlusPlus11 && isa<CXXDestructorDecl>(New)) {
Sebastian Redla0448262011-05-20 05:57:18 +0000791 // Don't check uninstantiated template destructors at all. We can only
792 // synthesize correct specs after the template is instantiated.
793 if (New->getParent()->isDependentType())
794 return false;
795 if (New->getParent()->isBeingDefined()) {
796 // The destructor might be updated once the definition is finished. So
797 // remember it and check later.
798 DelayedDestructorExceptionSpecChecks.push_back(std::make_pair(
799 cast<CXXDestructorDecl>(New), cast<CXXDestructorDecl>(Old)));
800 return false;
801 }
Sebastian Redl0ee33912011-05-19 05:13:44 +0000802 }
Francois Pichet0f161592011-05-24 02:11:43 +0000803 unsigned DiagID = diag::err_override_exception_spec;
David Blaikie4e4d0842012-03-11 07:00:24 +0000804 if (getLangOpts().MicrosoftExt)
Francois Pichet0f161592011-05-24 02:11:43 +0000805 DiagID = diag::warn_override_exception_spec;
806 return CheckExceptionSpecSubset(PDiag(DiagID),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000807 PDiag(diag::note_overridden_virtual_function),
Sebastian Redldced2262009-10-11 09:03:14 +0000808 Old->getType()->getAs<FunctionProtoType>(),
809 Old->getLocation(),
810 New->getType()->getAs<FunctionProtoType>(),
811 New->getLocation());
812}
813
Richard Smithe6975e92012-04-17 00:58:00 +0000814static CanThrowResult canSubExprsThrow(Sema &S, const Expr *CE) {
815 Expr *E = const_cast<Expr*>(CE);
816 CanThrowResult R = CT_Cannot;
817 for (Expr::child_range I = E->children(); I && R != CT_Can; ++I)
818 R = mergeCanThrow(R, S.canThrow(cast<Expr>(*I)));
819 return R;
820}
821
822static CanThrowResult canCalleeThrow(Sema &S, const Expr *E,
823 const Decl *D,
824 bool NullThrows = true) {
825 if (!D)
826 return NullThrows ? CT_Can : CT_Cannot;
827
828 // See if we can get a function type from the decl somehow.
829 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
830 if (!VD) // If we have no clue what we're calling, assume the worst.
831 return CT_Can;
832
833 // As an extension, we assume that __attribute__((nothrow)) functions don't
834 // throw.
835 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
836 return CT_Cannot;
837
838 QualType T = VD->getType();
839 const FunctionProtoType *FT;
840 if ((FT = T->getAs<FunctionProtoType>())) {
841 } else if (const PointerType *PT = T->getAs<PointerType>())
842 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
843 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
844 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
845 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
846 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
847 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
848 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
849
850 if (!FT)
851 return CT_Can;
852
853 FT = S.ResolveExceptionSpec(E->getLocStart(), FT);
854 if (!FT)
855 return CT_Can;
856
Richard Smithe6975e92012-04-17 00:58:00 +0000857 return FT->isNothrow(S.Context) ? CT_Cannot : CT_Can;
858}
859
860static CanThrowResult canDynamicCastThrow(const CXXDynamicCastExpr *DC) {
861 if (DC->isTypeDependent())
862 return CT_Dependent;
863
864 if (!DC->getTypeAsWritten()->isReferenceType())
865 return CT_Cannot;
866
867 if (DC->getSubExpr()->isTypeDependent())
868 return CT_Dependent;
869
870 return DC->getCastKind() == clang::CK_Dynamic? CT_Can : CT_Cannot;
871}
872
873static CanThrowResult canTypeidThrow(Sema &S, const CXXTypeidExpr *DC) {
874 if (DC->isTypeOperand())
875 return CT_Cannot;
876
877 Expr *Op = DC->getExprOperand();
878 if (Op->isTypeDependent())
879 return CT_Dependent;
880
881 const RecordType *RT = Op->getType()->getAs<RecordType>();
882 if (!RT)
883 return CT_Cannot;
884
885 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
886 return CT_Cannot;
887
888 if (Op->Classify(S.Context).isPRValue())
889 return CT_Cannot;
890
891 return CT_Can;
892}
893
894CanThrowResult Sema::canThrow(const Expr *E) {
895 // C++ [expr.unary.noexcept]p3:
896 // [Can throw] if in a potentially-evaluated context the expression would
897 // contain:
898 switch (E->getStmtClass()) {
899 case Expr::CXXThrowExprClass:
900 // - a potentially evaluated throw-expression
901 return CT_Can;
902
903 case Expr::CXXDynamicCastExprClass: {
904 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
905 // where T is a reference type, that requires a run-time check
906 CanThrowResult CT = canDynamicCastThrow(cast<CXXDynamicCastExpr>(E));
907 if (CT == CT_Can)
908 return CT;
909 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
910 }
911
912 case Expr::CXXTypeidExprClass:
913 // - a potentially evaluated typeid expression applied to a glvalue
914 // expression whose type is a polymorphic class type
915 return canTypeidThrow(*this, cast<CXXTypeidExpr>(E));
916
917 // - a potentially evaluated call to a function, member function, function
918 // pointer, or member function pointer that does not have a non-throwing
919 // exception-specification
920 case Expr::CallExprClass:
921 case Expr::CXXMemberCallExprClass:
922 case Expr::CXXOperatorCallExprClass:
923 case Expr::UserDefinedLiteralClass: {
924 const CallExpr *CE = cast<CallExpr>(E);
925 CanThrowResult CT;
926 if (E->isTypeDependent())
927 CT = CT_Dependent;
928 else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
929 CT = CT_Cannot;
930 else
931 CT = canCalleeThrow(*this, E, CE->getCalleeDecl());
932 if (CT == CT_Can)
933 return CT;
934 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
935 }
936
937 case Expr::CXXConstructExprClass:
938 case Expr::CXXTemporaryObjectExprClass: {
939 CanThrowResult CT = canCalleeThrow(*this, E,
940 cast<CXXConstructExpr>(E)->getConstructor());
941 if (CT == CT_Can)
942 return CT;
943 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
944 }
945
946 case Expr::LambdaExprClass: {
947 const LambdaExpr *Lambda = cast<LambdaExpr>(E);
948 CanThrowResult CT = CT_Cannot;
949 for (LambdaExpr::capture_init_iterator Cap = Lambda->capture_init_begin(),
950 CapEnd = Lambda->capture_init_end();
951 Cap != CapEnd; ++Cap)
952 CT = mergeCanThrow(CT, canThrow(*Cap));
953 return CT;
954 }
955
956 case Expr::CXXNewExprClass: {
957 CanThrowResult CT;
958 if (E->isTypeDependent())
959 CT = CT_Dependent;
960 else
961 CT = canCalleeThrow(*this, E, cast<CXXNewExpr>(E)->getOperatorNew());
962 if (CT == CT_Can)
963 return CT;
964 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
965 }
966
967 case Expr::CXXDeleteExprClass: {
968 CanThrowResult CT;
969 QualType DTy = cast<CXXDeleteExpr>(E)->getDestroyedType();
970 if (DTy.isNull() || DTy->isDependentType()) {
971 CT = CT_Dependent;
972 } else {
973 CT = canCalleeThrow(*this, E,
974 cast<CXXDeleteExpr>(E)->getOperatorDelete());
975 if (const RecordType *RT = DTy->getAs<RecordType>()) {
976 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
977 CT = mergeCanThrow(CT, canCalleeThrow(*this, E, RD->getDestructor()));
978 }
979 if (CT == CT_Can)
980 return CT;
981 }
982 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
983 }
984
985 case Expr::CXXBindTemporaryExprClass: {
986 // The bound temporary has to be destroyed again, which might throw.
987 CanThrowResult CT = canCalleeThrow(*this, E,
988 cast<CXXBindTemporaryExpr>(E)->getTemporary()->getDestructor());
989 if (CT == CT_Can)
990 return CT;
991 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
992 }
993
994 // ObjC message sends are like function calls, but never have exception
995 // specs.
996 case Expr::ObjCMessageExprClass:
997 case Expr::ObjCPropertyRefExprClass:
998 case Expr::ObjCSubscriptRefExprClass:
999 return CT_Can;
1000
1001 // All the ObjC literals that are implemented as calls are
1002 // potentially throwing unless we decide to close off that
1003 // possibility.
1004 case Expr::ObjCArrayLiteralClass:
1005 case Expr::ObjCDictionaryLiteralClass:
Patrick Beardeb382ec2012-04-19 00:25:12 +00001006 case Expr::ObjCBoxedExprClass:
Richard Smithe6975e92012-04-17 00:58:00 +00001007 return CT_Can;
1008
1009 // Many other things have subexpressions, so we have to test those.
1010 // Some are simple:
1011 case Expr::ConditionalOperatorClass:
1012 case Expr::CompoundLiteralExprClass:
1013 case Expr::CXXConstCastExprClass:
1014 case Expr::CXXDefaultArgExprClass:
1015 case Expr::CXXReinterpretCastExprClass:
1016 case Expr::DesignatedInitExprClass:
1017 case Expr::ExprWithCleanupsClass:
1018 case Expr::ExtVectorElementExprClass:
1019 case Expr::InitListExprClass:
1020 case Expr::MemberExprClass:
1021 case Expr::ObjCIsaExprClass:
1022 case Expr::ObjCIvarRefExprClass:
1023 case Expr::ParenExprClass:
1024 case Expr::ParenListExprClass:
1025 case Expr::ShuffleVectorExprClass:
1026 case Expr::VAArgExprClass:
1027 return canSubExprsThrow(*this, E);
1028
1029 // Some might be dependent for other reasons.
1030 case Expr::ArraySubscriptExprClass:
1031 case Expr::BinaryOperatorClass:
1032 case Expr::CompoundAssignOperatorClass:
1033 case Expr::CStyleCastExprClass:
1034 case Expr::CXXStaticCastExprClass:
1035 case Expr::CXXFunctionalCastExprClass:
1036 case Expr::ImplicitCastExprClass:
1037 case Expr::MaterializeTemporaryExprClass:
1038 case Expr::UnaryOperatorClass: {
1039 CanThrowResult CT = E->isTypeDependent() ? CT_Dependent : CT_Cannot;
1040 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1041 }
1042
1043 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1044 case Expr::StmtExprClass:
1045 return CT_Can;
1046
1047 case Expr::ChooseExprClass:
1048 if (E->isTypeDependent() || E->isValueDependent())
1049 return CT_Dependent;
1050 return canThrow(cast<ChooseExpr>(E)->getChosenSubExpr(Context));
1051
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 Smith9a4db032012-09-12 00:56:43 +00001077 case Expr::FunctionParmPackExprClass:
Richard Smithe6975e92012-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:
1087 case Expr::BinaryTypeTraitExprClass:
1088 case Expr::TypeTraitExprClass:
1089 case Expr::CXXBoolLiteralExprClass:
1090 case Expr::CXXNoexceptExprClass:
1091 case Expr::CXXNullPtrLiteralExprClass:
1092 case Expr::CXXPseudoDestructorExprClass:
1093 case Expr::CXXScalarValueInitExprClass:
1094 case Expr::CXXThisExprClass:
1095 case Expr::CXXUuidofExprClass:
1096 case Expr::CharacterLiteralClass:
1097 case Expr::ExpressionTraitExprClass:
1098 case Expr::FloatingLiteralClass:
1099 case Expr::GNUNullExprClass:
1100 case Expr::ImaginaryLiteralClass:
1101 case Expr::ImplicitValueInitExprClass:
1102 case Expr::IntegerLiteralClass:
1103 case Expr::ObjCEncodeExprClass:
1104 case Expr::ObjCStringLiteralClass:
1105 case Expr::ObjCBoolLiteralExprClass:
1106 case Expr::OpaqueValueExprClass:
1107 case Expr::PredefinedExprClass:
1108 case Expr::SizeOfPackExprClass:
1109 case Expr::StringLiteralClass:
1110 case Expr::UnaryTypeTraitExprClass:
1111 // These expressions can never throw.
1112 return CT_Cannot;
1113
1114#define STMT(CLASS, PARENT) case Expr::CLASS##Class:
1115#define STMT_RANGE(Base, First, Last)
1116#define LAST_STMT_RANGE(BASE, FIRST, LAST)
1117#define EXPR(CLASS, PARENT)
1118#define ABSTRACT_STMT(STMT)
1119#include "clang/AST/StmtNodes.inc"
1120 case Expr::NoStmtClass:
1121 llvm_unreachable("Invalid class for expression");
1122 }
1123 llvm_unreachable("Bogus StmtClass");
1124}
1125
Sebastian Redldced2262009-10-11 09:03:14 +00001126} // end namespace clang