blob: 1512a2578977b5cedae97c756eedfa4eded57f72 [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"
19#include "clang/Lex/Preprocessor.h"
Douglas Gregore13ad832010-02-12 07:32:17 +000020#include "clang/Basic/Diagnostic.h"
21#include "clang/Basic/SourceManager.h"
Sebastian Redldced2262009-10-11 09:03:14 +000022#include "llvm/ADT/SmallPtrSet.h"
23
24namespace clang {
25
26static const FunctionProtoType *GetUnderlyingFunction(QualType T)
27{
28 if (const PointerType *PtrTy = T->getAs<PointerType>())
29 T = PtrTy->getPointeeType();
30 else if (const ReferenceType *RefTy = T->getAs<ReferenceType>())
31 T = RefTy->getPointeeType();
Sebastian Redlc3a3b7b2009-10-14 14:38:54 +000032 else if (const MemberPointerType *MPTy = T->getAs<MemberPointerType>())
33 T = MPTy->getPointeeType();
Sebastian Redldced2262009-10-11 09:03:14 +000034 return T->getAs<FunctionProtoType>();
35}
36
37/// CheckSpecifiedExceptionType - Check if the given type is valid in an
38/// exception specification. Incomplete types, or pointers to incomplete types
39/// other than void are not allowed.
40bool Sema::CheckSpecifiedExceptionType(QualType T, const SourceRange &Range) {
Sebastian Redldced2262009-10-11 09:03:14 +000041
Douglas Gregor0966f352009-12-10 18:13:52 +000042 // This check (and the similar one below) deals with issue 437, that changes
43 // C++ 9.2p2 this way:
44 // Within the class member-specification, the class is regarded as complete
45 // within function bodies, default arguments, exception-specifications, and
46 // constructor ctor-initializers (including such things in nested classes).
47 if (T->isRecordType() && T->getAs<RecordType>()->isBeingDefined())
48 return false;
49
Sebastian Redldced2262009-10-11 09:03:14 +000050 // C++ 15.4p2: A type denoted in an exception-specification shall not denote
51 // an incomplete type.
Sebastian Redl491b84c2009-10-14 14:59:48 +000052 if (RequireCompleteType(Range.getBegin(), T,
53 PDiag(diag::err_incomplete_in_exception_spec) << /*direct*/0 << Range))
54 return true;
Sebastian Redldced2262009-10-11 09:03:14 +000055
56 // C++ 15.4p2: A type denoted in an exception-specification shall not denote
57 // an incomplete type a pointer or reference to an incomplete type, other
58 // than (cv) void*.
59 int kind;
60 if (const PointerType* IT = T->getAs<PointerType>()) {
61 T = IT->getPointeeType();
62 kind = 1;
63 } else if (const ReferenceType* IT = T->getAs<ReferenceType>()) {
64 T = IT->getPointeeType();
65 kind = 2;
66 } else
67 return false;
68
Douglas Gregor0966f352009-12-10 18:13:52 +000069 // Again as before
70 if (T->isRecordType() && T->getAs<RecordType>()->isBeingDefined())
71 return false;
72
Sebastian Redl491b84c2009-10-14 14:59:48 +000073 if (!T->isVoidType() && RequireCompleteType(Range.getBegin(), T,
Douglas Gregor0966f352009-12-10 18:13:52 +000074 PDiag(diag::err_incomplete_in_exception_spec) << kind << Range))
Sebastian Redl491b84c2009-10-14 14:59:48 +000075 return true;
Sebastian Redldced2262009-10-11 09:03:14 +000076
77 return false;
78}
79
80/// CheckDistantExceptionSpec - Check if the given type is a pointer or pointer
81/// to member to a function with an exception specification. This means that
82/// it is invalid to add another level of indirection.
83bool Sema::CheckDistantExceptionSpec(QualType T) {
84 if (const PointerType *PT = T->getAs<PointerType>())
85 T = PT->getPointeeType();
86 else if (const MemberPointerType *PT = T->getAs<MemberPointerType>())
87 T = PT->getPointeeType();
88 else
89 return false;
90
91 const FunctionProtoType *FnT = T->getAs<FunctionProtoType>();
92 if (!FnT)
93 return false;
94
95 return FnT->hasExceptionSpec();
96}
97
Douglas Gregore13ad832010-02-12 07:32:17 +000098bool Sema::CheckEquivalentExceptionSpec(FunctionDecl *Old, FunctionDecl *New) {
Sebastian Redl99439d42011-03-15 19:52:30 +000099 OverloadedOperatorKind OO = New->getDeclName().getCXXOverloadedOperator();
100 bool IsOperatorNew = OO == OO_New || OO == OO_Array_New;
Douglas Gregor2eef8292010-03-24 07:14:45 +0000101 bool MissingExceptionSpecification = false;
Douglas Gregore13ad832010-02-12 07:32:17 +0000102 bool MissingEmptyExceptionSpecification = false;
Francois Picheteedd4672011-03-19 23:05:18 +0000103 unsigned DiagID = diag::err_mismatched_exception_spec;
104 if (getLangOptions().Microsoft)
105 DiagID = diag::war_mismatched_exception_spec;
106
107 if (!CheckEquivalentExceptionSpec(PDiag(DiagID),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000108 PDiag(diag::note_previous_declaration),
Douglas Gregore13ad832010-02-12 07:32:17 +0000109 Old->getType()->getAs<FunctionProtoType>(),
110 Old->getLocation(),
111 New->getType()->getAs<FunctionProtoType>(),
112 New->getLocation(),
Douglas Gregor2eef8292010-03-24 07:14:45 +0000113 &MissingExceptionSpecification,
Sebastian Redl60618fa2011-03-12 11:50:43 +0000114 &MissingEmptyExceptionSpecification,
Sebastian Redl99439d42011-03-15 19:52:30 +0000115 /*AllowNoexceptAllMatchWithNoSpec=*/true,
116 IsOperatorNew))
Douglas Gregore13ad832010-02-12 07:32:17 +0000117 return false;
118
119 // The failure was something other than an empty exception
120 // specification; return an error.
Douglas Gregor2eef8292010-03-24 07:14:45 +0000121 if (!MissingExceptionSpecification && !MissingEmptyExceptionSpecification)
Douglas Gregore13ad832010-02-12 07:32:17 +0000122 return true;
123
John McCalle23cf432010-12-14 08:05:40 +0000124 const FunctionProtoType *NewProto
125 = New->getType()->getAs<FunctionProtoType>();
126
Douglas Gregore13ad832010-02-12 07:32:17 +0000127 // The new function declaration is only missing an empty exception
128 // specification "throw()". If the throw() specification came from a
129 // function in a system header that has C linkage, just add an empty
130 // exception specification to the "new" declaration. This is an
131 // egregious workaround for glibc, which adds throw() specifications
132 // to many libc functions as an optimization. Unfortunately, that
133 // optimization isn't permitted by the C++ standard, so we're forced
134 // to work around it here.
John McCalle23cf432010-12-14 08:05:40 +0000135 if (MissingEmptyExceptionSpecification && NewProto &&
Douglas Gregor2eef8292010-03-24 07:14:45 +0000136 (Old->getLocation().isInvalid() ||
137 Context.getSourceManager().isInSystemHeader(Old->getLocation())) &&
Douglas Gregore13ad832010-02-12 07:32:17 +0000138 Old->isExternC()) {
John McCalle23cf432010-12-14 08:05:40 +0000139 FunctionProtoType::ExtProtoInfo EPI = NewProto->getExtProtoInfo();
Sebastian Redl60618fa2011-03-12 11:50:43 +0000140 EPI.ExceptionSpecType = EST_DynamicNone;
Douglas Gregore13ad832010-02-12 07:32:17 +0000141 QualType NewType = Context.getFunctionType(NewProto->getResultType(),
142 NewProto->arg_type_begin(),
143 NewProto->getNumArgs(),
John McCalle23cf432010-12-14 08:05:40 +0000144 EPI);
Douglas Gregore13ad832010-02-12 07:32:17 +0000145 New->setType(NewType);
146 return false;
147 }
148
John McCalle23cf432010-12-14 08:05:40 +0000149 if (MissingExceptionSpecification && NewProto) {
Douglas Gregor2eef8292010-03-24 07:14:45 +0000150 const FunctionProtoType *OldProto
151 = Old->getType()->getAs<FunctionProtoType>();
152
John McCalle23cf432010-12-14 08:05:40 +0000153 FunctionProtoType::ExtProtoInfo EPI = NewProto->getExtProtoInfo();
Sebastian Redl60618fa2011-03-12 11:50:43 +0000154 EPI.ExceptionSpecType = OldProto->getExceptionSpecType();
155 if (EPI.ExceptionSpecType == EST_Dynamic) {
156 EPI.NumExceptions = OldProto->getNumExceptions();
157 EPI.Exceptions = OldProto->exception_begin();
158 } else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) {
159 // FIXME: We can't just take the expression from the old prototype. It
160 // likely contains references to the old prototype's parameters.
161 }
John McCalle23cf432010-12-14 08:05:40 +0000162
Douglas Gregor2eef8292010-03-24 07:14:45 +0000163 // Update the type of the function with the appropriate exception
164 // specification.
165 QualType NewType = Context.getFunctionType(NewProto->getResultType(),
166 NewProto->arg_type_begin(),
167 NewProto->getNumArgs(),
John McCalle23cf432010-12-14 08:05:40 +0000168 EPI);
Douglas Gregor2eef8292010-03-24 07:14:45 +0000169 New->setType(NewType);
170
171 // If exceptions are disabled, suppress the warning about missing
172 // exception specifications for new and delete operators.
John McCall018591f2011-03-02 02:04:40 +0000173 if (!getLangOptions().CXXExceptions) {
Douglas Gregor2eef8292010-03-24 07:14:45 +0000174 switch (New->getDeclName().getCXXOverloadedOperator()) {
175 case OO_New:
176 case OO_Array_New:
177 case OO_Delete:
178 case OO_Array_Delete:
179 if (New->getDeclContext()->isTranslationUnit())
180 return false;
181 break;
182
183 default:
184 break;
185 }
186 }
187
188 // Warn about the lack of exception specification.
189 llvm::SmallString<128> ExceptionSpecString;
190 llvm::raw_svector_ostream OS(ExceptionSpecString);
Sebastian Redl60618fa2011-03-12 11:50:43 +0000191 switch (OldProto->getExceptionSpecType()) {
192 case EST_DynamicNone:
193 OS << "throw()";
194 break;
195
196 case EST_Dynamic: {
197 OS << "throw(";
198 bool OnFirstException = true;
199 for (FunctionProtoType::exception_iterator E = OldProto->exception_begin(),
200 EEnd = OldProto->exception_end();
201 E != EEnd;
202 ++E) {
203 if (OnFirstException)
204 OnFirstException = false;
205 else
206 OS << ", ";
207
208 OS << E->getAsString(Context.PrintingPolicy);
209 }
210 OS << ")";
211 break;
Douglas Gregor2eef8292010-03-24 07:14:45 +0000212 }
Sebastian Redl60618fa2011-03-12 11:50:43 +0000213
214 case EST_BasicNoexcept:
215 OS << "noexcept";
216 break;
217
218 case EST_ComputedNoexcept:
219 OS << "noexcept(";
220 OldProto->getNoexceptExpr()->printPretty(OS, Context, 0,
221 Context.PrintingPolicy);
222 OS << ")";
223 break;
224
225 default:
226 assert(false && "This spec type is compatible with none.");
227 }
Douglas Gregor2eef8292010-03-24 07:14:45 +0000228 OS.flush();
229
Abramo Bagnara796aa442011-03-12 11:17:06 +0000230 SourceLocation FixItLoc;
Douglas Gregor2eef8292010-03-24 07:14:45 +0000231 if (TypeSourceInfo *TSInfo = New->getTypeSourceInfo()) {
Abramo Bagnara723df242010-12-14 22:11:44 +0000232 TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens();
Douglas Gregor2eef8292010-03-24 07:14:45 +0000233 if (const FunctionTypeLoc *FTLoc = dyn_cast<FunctionTypeLoc>(&TL))
Abramo Bagnara796aa442011-03-12 11:17:06 +0000234 FixItLoc = PP.getLocForEndOfToken(FTLoc->getLocalRangeEnd());
Douglas Gregor2eef8292010-03-24 07:14:45 +0000235 }
236
Abramo Bagnara796aa442011-03-12 11:17:06 +0000237 if (FixItLoc.isInvalid())
Douglas Gregor2eef8292010-03-24 07:14:45 +0000238 Diag(New->getLocation(), diag::warn_missing_exception_specification)
239 << New << OS.str();
240 else {
241 // FIXME: This will get more complicated with C++0x
242 // late-specified return types.
243 Diag(New->getLocation(), diag::warn_missing_exception_specification)
244 << New << OS.str()
Abramo Bagnara796aa442011-03-12 11:17:06 +0000245 << FixItHint::CreateInsertion(FixItLoc, " " + OS.str().str());
Douglas Gregor2eef8292010-03-24 07:14:45 +0000246 }
247
248 if (!Old->getLocation().isInvalid())
249 Diag(Old->getLocation(), diag::note_previous_declaration);
250
251 return false;
252 }
253
Francois Picheteedd4672011-03-19 23:05:18 +0000254 Diag(New->getLocation(), DiagID);
Douglas Gregore13ad832010-02-12 07:32:17 +0000255 Diag(Old->getLocation(), diag::note_previous_declaration);
256 return true;
257}
258
Sebastian Redldced2262009-10-11 09:03:14 +0000259/// CheckEquivalentExceptionSpec - Check if the two types have equivalent
260/// exception specifications. Exception specifications are equivalent if
261/// they allow exactly the same set of exception types. It does not matter how
262/// that is achieved. See C++ [except.spec]p2.
263bool Sema::CheckEquivalentExceptionSpec(
264 const FunctionProtoType *Old, SourceLocation OldLoc,
265 const FunctionProtoType *New, SourceLocation NewLoc) {
Francois Picheteedd4672011-03-19 23:05:18 +0000266 unsigned DiagID = diag::err_mismatched_exception_spec;
267 if (getLangOptions().Microsoft)
268 DiagID = diag::war_mismatched_exception_spec;
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000269 return CheckEquivalentExceptionSpec(
Francois Picheteedd4672011-03-19 23:05:18 +0000270 PDiag(DiagID),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000271 PDiag(diag::note_previous_declaration),
Sebastian Redldced2262009-10-11 09:03:14 +0000272 Old, OldLoc, New, NewLoc);
273}
274
Sebastian Redl60618fa2011-03-12 11:50:43 +0000275/// CheckEquivalentExceptionSpec - Check if the two types have compatible
276/// exception specifications. See C++ [except.spec]p3.
277bool Sema::CheckEquivalentExceptionSpec(const PartialDiagnostic &DiagID,
Douglas Gregor2eef8292010-03-24 07:14:45 +0000278 const PartialDiagnostic & NoteID,
Sebastian Redl60618fa2011-03-12 11:50:43 +0000279 const FunctionProtoType *Old,
Douglas Gregor2eef8292010-03-24 07:14:45 +0000280 SourceLocation OldLoc,
Sebastian Redl60618fa2011-03-12 11:50:43 +0000281 const FunctionProtoType *New,
Douglas Gregor2eef8292010-03-24 07:14:45 +0000282 SourceLocation NewLoc,
283 bool *MissingExceptionSpecification,
Sebastian Redl60618fa2011-03-12 11:50:43 +0000284 bool*MissingEmptyExceptionSpecification,
Sebastian Redl99439d42011-03-15 19:52:30 +0000285 bool AllowNoexceptAllMatchWithNoSpec,
286 bool IsOperatorNew) {
John McCall811d0be2010-05-28 08:37:35 +0000287 // Just completely ignore this under -fno-exceptions.
John McCall018591f2011-03-02 02:04:40 +0000288 if (!getLangOptions().CXXExceptions)
John McCall811d0be2010-05-28 08:37:35 +0000289 return false;
290
Douglas Gregor2eef8292010-03-24 07:14:45 +0000291 if (MissingExceptionSpecification)
292 *MissingExceptionSpecification = false;
293
Douglas Gregore13ad832010-02-12 07:32:17 +0000294 if (MissingEmptyExceptionSpecification)
295 *MissingEmptyExceptionSpecification = false;
296
Sebastian Redl60618fa2011-03-12 11:50:43 +0000297 // C++0x [except.spec]p3: Two exception-specifications are compatible if:
298 // - both are non-throwing, regardless of their form,
299 // - both have the form noexcept(constant-expression) and the constant-
300 // expressions are equivalent,
301 // - one exception-specification is a noexcept-specification allowing all
302 // exceptions and the other is of the form throw(type-id-list), or
303 // - both are dynamic-exception-specifications that have the same set of
304 // adjusted types.
305 //
306 // C++0x [except.spec]p12: An exception-specifcation is non-throwing if it is
307 // of the form throw(), noexcept, or noexcept(constant-expression) where the
308 // constant-expression yields true.
309 //
310 // CWG 1073 Proposed resolution: Strike the third bullet above.
311 //
312 // C++0x [except.spec]p4: If any declaration of a function has an exception-
313 // specifier that is not a noexcept-specification allowing all exceptions,
314 // all declarations [...] of that function shall have a compatible
315 // exception-specification.
316 //
317 // That last point basically means that noexcept(false) matches no spec.
318 // It's considered when AllowNoexceptAllMatchWithNoSpec is true.
319
320 ExceptionSpecificationType OldEST = Old->getExceptionSpecType();
321 ExceptionSpecificationType NewEST = New->getExceptionSpecType();
322
323 // Shortcut the case where both have no spec.
324 if (OldEST == EST_None && NewEST == EST_None)
325 return false;
326
Sebastian Redl8026f6d2011-03-13 17:09:40 +0000327 FunctionProtoType::NoexceptResult OldNR = Old->getNoexceptSpec(Context);
328 FunctionProtoType::NoexceptResult NewNR = New->getNoexceptSpec(Context);
Sebastian Redl60618fa2011-03-12 11:50:43 +0000329 if (OldNR == FunctionProtoType::NR_BadNoexcept ||
330 NewNR == FunctionProtoType::NR_BadNoexcept)
331 return false;
332
333 // Dependent noexcept specifiers are compatible with each other, but nothing
334 // else.
335 // One noexcept is compatible with another if the argument is the same
336 if (OldNR == NewNR &&
337 OldNR != FunctionProtoType::NR_NoNoexcept &&
338 NewNR != FunctionProtoType::NR_NoNoexcept)
339 return false;
340 if (OldNR != NewNR &&
341 OldNR != FunctionProtoType::NR_NoNoexcept &&
342 NewNR != FunctionProtoType::NR_NoNoexcept) {
343 Diag(NewLoc, DiagID);
344 if (NoteID.getDiagID() != 0)
345 Diag(OldLoc, NoteID);
346 return true;
Douglas Gregor5b6f7692010-08-30 15:04:51 +0000347 }
348
Sebastian Redl60618fa2011-03-12 11:50:43 +0000349 // The MS extension throw(...) is compatible with itself.
350 if (OldEST == EST_MSAny && NewEST == EST_MSAny)
Sebastian Redldced2262009-10-11 09:03:14 +0000351 return false;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000352
353 // It's also compatible with no spec.
354 if ((OldEST == EST_None && NewEST == EST_MSAny) ||
355 (OldEST == EST_MSAny && NewEST == EST_None))
356 return false;
357
358 // It's also compatible with noexcept(false).
359 if (OldEST == EST_MSAny && NewNR == FunctionProtoType::NR_Throw)
360 return false;
361 if (NewEST == EST_MSAny && OldNR == FunctionProtoType::NR_Throw)
362 return false;
363
364 // As described above, noexcept(false) matches no spec only for functions.
365 if (AllowNoexceptAllMatchWithNoSpec) {
366 if (OldEST == EST_None && NewNR == FunctionProtoType::NR_Throw)
367 return false;
368 if (NewEST == EST_None && OldNR == FunctionProtoType::NR_Throw)
369 return false;
370 }
371
372 // Any non-throwing specifications are compatible.
373 bool OldNonThrowing = OldNR == FunctionProtoType::NR_Nothrow ||
374 OldEST == EST_DynamicNone;
375 bool NewNonThrowing = NewNR == FunctionProtoType::NR_Nothrow ||
376 NewEST == EST_DynamicNone;
377 if (OldNonThrowing && NewNonThrowing)
378 return false;
379
Sebastian Redl99439d42011-03-15 19:52:30 +0000380 // As a special compatibility feature, under C++0x we accept no spec and
381 // throw(std::bad_alloc) as equivalent for operator new and operator new[].
382 // This is because the implicit declaration changed, but old code would break.
383 if (getLangOptions().CPlusPlus0x && IsOperatorNew) {
384 const FunctionProtoType *WithExceptions = 0;
385 if (OldEST == EST_None && NewEST == EST_Dynamic)
386 WithExceptions = New;
387 else if (OldEST == EST_Dynamic && NewEST == EST_None)
388 WithExceptions = Old;
389 if (WithExceptions && WithExceptions->getNumExceptions() == 1) {
390 // One has no spec, the other throw(something). If that something is
391 // std::bad_alloc, all conditions are met.
392 QualType Exception = *WithExceptions->exception_begin();
393 if (CXXRecordDecl *ExRecord = Exception->getAsCXXRecordDecl()) {
394 IdentifierInfo* Name = ExRecord->getIdentifier();
395 if (Name && Name->getName() == "bad_alloc") {
396 // It's called bad_alloc, but is it in std?
397 DeclContext* DC = ExRecord->getDeclContext();
Sebastian Redld8f2e8e2011-03-15 20:41:09 +0000398 DC = DC->getEnclosingNamespaceContext();
399 if (NamespaceDecl* NS = dyn_cast<NamespaceDecl>(DC)) {
Sebastian Redl99439d42011-03-15 19:52:30 +0000400 IdentifierInfo* NSName = NS->getIdentifier();
Sebastian Redld8f2e8e2011-03-15 20:41:09 +0000401 DC = DC->getParent();
Sebastian Redl99439d42011-03-15 19:52:30 +0000402 if (NSName && NSName->getName() == "std" &&
Sebastian Redld8f2e8e2011-03-15 20:41:09 +0000403 DC->getEnclosingNamespaceContext()->isTranslationUnit()) {
Sebastian Redl99439d42011-03-15 19:52:30 +0000404 return false;
Sebastian Redld8f2e8e2011-03-15 20:41:09 +0000405 }
Sebastian Redl99439d42011-03-15 19:52:30 +0000406 }
407 }
408 }
409 }
410 }
411
Sebastian Redl60618fa2011-03-12 11:50:43 +0000412 // At this point, the only remaining valid case is two matching dynamic
413 // specifications. We return here unless both specifications are dynamic.
414 if (OldEST != EST_Dynamic || NewEST != EST_Dynamic) {
Douglas Gregor2eef8292010-03-24 07:14:45 +0000415 if (MissingExceptionSpecification && Old->hasExceptionSpec() &&
Douglas Gregore13ad832010-02-12 07:32:17 +0000416 !New->hasExceptionSpec()) {
Douglas Gregor2eef8292010-03-24 07:14:45 +0000417 // The old type has an exception specification of some sort, but
418 // the new type does not.
419 *MissingExceptionSpecification = true;
420
Sebastian Redl60618fa2011-03-12 11:50:43 +0000421 if (MissingEmptyExceptionSpecification && OldNonThrowing) {
422 // The old type has a throw() or noexcept(true) exception specification
423 // and the new type has no exception specification, and the caller asked
Douglas Gregor2eef8292010-03-24 07:14:45 +0000424 // to handle this itself.
425 *MissingEmptyExceptionSpecification = true;
426 }
427
Douglas Gregore13ad832010-02-12 07:32:17 +0000428 return true;
429 }
430
Sebastian Redldced2262009-10-11 09:03:14 +0000431 Diag(NewLoc, DiagID);
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000432 if (NoteID.getDiagID() != 0)
Sebastian Redldced2262009-10-11 09:03:14 +0000433 Diag(OldLoc, NoteID);
434 return true;
435 }
436
Sebastian Redl60618fa2011-03-12 11:50:43 +0000437 assert(OldEST == EST_Dynamic && NewEST == EST_Dynamic &&
438 "Exception compatibility logic error: non-dynamic spec slipped through.");
439
Sebastian Redldced2262009-10-11 09:03:14 +0000440 bool Success = true;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000441 // Both have a dynamic exception spec. Collect the first set, then compare
Sebastian Redldced2262009-10-11 09:03:14 +0000442 // to the second.
Sebastian Redl1219d152009-10-14 15:06:25 +0000443 llvm::SmallPtrSet<CanQualType, 8> OldTypes, NewTypes;
Sebastian Redldced2262009-10-11 09:03:14 +0000444 for (FunctionProtoType::exception_iterator I = Old->exception_begin(),
445 E = Old->exception_end(); I != E; ++I)
Sebastian Redl1219d152009-10-14 15:06:25 +0000446 OldTypes.insert(Context.getCanonicalType(*I).getUnqualifiedType());
Sebastian Redldced2262009-10-11 09:03:14 +0000447
448 for (FunctionProtoType::exception_iterator I = New->exception_begin(),
Sebastian Redl5db4d902009-10-11 09:11:23 +0000449 E = New->exception_end(); I != E && Success; ++I) {
Sebastian Redl1219d152009-10-14 15:06:25 +0000450 CanQualType TypePtr = Context.getCanonicalType(*I).getUnqualifiedType();
Sebastian Redl5db4d902009-10-11 09:11:23 +0000451 if(OldTypes.count(TypePtr))
452 NewTypes.insert(TypePtr);
453 else
454 Success = false;
455 }
Sebastian Redldced2262009-10-11 09:03:14 +0000456
Sebastian Redl5db4d902009-10-11 09:11:23 +0000457 Success = Success && OldTypes.size() == NewTypes.size();
Sebastian Redldced2262009-10-11 09:03:14 +0000458
459 if (Success) {
460 return false;
461 }
462 Diag(NewLoc, DiagID);
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000463 if (NoteID.getDiagID() != 0)
Sebastian Redldced2262009-10-11 09:03:14 +0000464 Diag(OldLoc, NoteID);
465 return true;
466}
467
468/// CheckExceptionSpecSubset - Check whether the second function type's
469/// exception specification is a subset (or equivalent) of the first function
470/// type. This is used by override and pointer assignment checks.
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000471bool Sema::CheckExceptionSpecSubset(
472 const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID,
Sebastian Redldced2262009-10-11 09:03:14 +0000473 const FunctionProtoType *Superset, SourceLocation SuperLoc,
474 const FunctionProtoType *Subset, SourceLocation SubLoc) {
John McCall811d0be2010-05-28 08:37:35 +0000475
476 // Just auto-succeed under -fno-exceptions.
John McCall018591f2011-03-02 02:04:40 +0000477 if (!getLangOptions().CXXExceptions)
John McCall811d0be2010-05-28 08:37:35 +0000478 return false;
479
Sebastian Redldced2262009-10-11 09:03:14 +0000480 // FIXME: As usual, we could be more specific in our error messages, but
481 // that better waits until we've got types with source locations.
482
483 if (!SubLoc.isValid())
484 SubLoc = SuperLoc;
485
Sebastian Redl60618fa2011-03-12 11:50:43 +0000486 ExceptionSpecificationType SuperEST = Superset->getExceptionSpecType();
487
Sebastian Redldced2262009-10-11 09:03:14 +0000488 // If superset contains everything, we're done.
Sebastian Redl60618fa2011-03-12 11:50:43 +0000489 if (SuperEST == EST_None || SuperEST == EST_MSAny)
Sebastian Redldced2262009-10-11 09:03:14 +0000490 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
491
Sebastian Redl60618fa2011-03-12 11:50:43 +0000492 // If there are dependent noexcept specs, assume everything is fine. Unlike
493 // with the equivalency check, this is safe in this case, because we don't
494 // want to merge declarations. Checks after instantiation will catch any
495 // omissions we make here.
496 // We also shortcut checking if a noexcept expression was bad.
497
Sebastian Redl8026f6d2011-03-13 17:09:40 +0000498 FunctionProtoType::NoexceptResult SuperNR =Superset->getNoexceptSpec(Context);
Sebastian Redl60618fa2011-03-12 11:50:43 +0000499 if (SuperNR == FunctionProtoType::NR_BadNoexcept ||
500 SuperNR == FunctionProtoType::NR_Dependent)
501 return false;
502
503 // Another case of the superset containing everything.
504 if (SuperNR == FunctionProtoType::NR_Throw)
505 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
506
507 ExceptionSpecificationType SubEST = Subset->getExceptionSpecType();
508
Sebastian Redldced2262009-10-11 09:03:14 +0000509 // It does not. If the subset contains everything, we've failed.
Sebastian Redl60618fa2011-03-12 11:50:43 +0000510 if (SubEST == EST_None || SubEST == EST_MSAny) {
Sebastian Redldced2262009-10-11 09:03:14 +0000511 Diag(SubLoc, DiagID);
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000512 if (NoteID.getDiagID() != 0)
Sebastian Redldced2262009-10-11 09:03:14 +0000513 Diag(SuperLoc, NoteID);
514 return true;
515 }
516
Sebastian Redl8026f6d2011-03-13 17:09:40 +0000517 FunctionProtoType::NoexceptResult SubNR = Subset->getNoexceptSpec(Context);
Sebastian Redl60618fa2011-03-12 11:50:43 +0000518 if (SubNR == FunctionProtoType::NR_BadNoexcept ||
519 SubNR == FunctionProtoType::NR_Dependent)
520 return false;
521
522 // Another case of the subset containing everything.
523 if (SubNR == FunctionProtoType::NR_Throw) {
524 Diag(SubLoc, DiagID);
525 if (NoteID.getDiagID() != 0)
526 Diag(SuperLoc, NoteID);
527 return true;
528 }
529
530 // If the subset contains nothing, we're done.
531 if (SubEST == EST_DynamicNone || SubNR == FunctionProtoType::NR_Nothrow)
532 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
533
534 // Otherwise, if the superset contains nothing, we've failed.
535 if (SuperEST == EST_DynamicNone || SuperNR == FunctionProtoType::NR_Nothrow) {
536 Diag(SubLoc, DiagID);
537 if (NoteID.getDiagID() != 0)
538 Diag(SuperLoc, NoteID);
539 return true;
540 }
541
542 assert(SuperEST == EST_Dynamic && SubEST == EST_Dynamic &&
543 "Exception spec subset: non-dynamic case slipped through.");
544
545 // Neither contains everything or nothing. Do a proper comparison.
Sebastian Redldced2262009-10-11 09:03:14 +0000546 for (FunctionProtoType::exception_iterator SubI = Subset->exception_begin(),
547 SubE = Subset->exception_end(); SubI != SubE; ++SubI) {
548 // Take one type from the subset.
549 QualType CanonicalSubT = Context.getCanonicalType(*SubI);
Sebastian Redlc3a3b7b2009-10-14 14:38:54 +0000550 // Unwrap pointers and references so that we can do checks within a class
551 // hierarchy. Don't unwrap member pointers; they don't have hierarchy
552 // conversions on the pointee.
Sebastian Redldced2262009-10-11 09:03:14 +0000553 bool SubIsPointer = false;
554 if (const ReferenceType *RefTy = CanonicalSubT->getAs<ReferenceType>())
555 CanonicalSubT = RefTy->getPointeeType();
556 if (const PointerType *PtrTy = CanonicalSubT->getAs<PointerType>()) {
557 CanonicalSubT = PtrTy->getPointeeType();
558 SubIsPointer = true;
559 }
560 bool SubIsClass = CanonicalSubT->isRecordType();
Douglas Gregora4923eb2009-11-16 21:35:15 +0000561 CanonicalSubT = CanonicalSubT.getLocalUnqualifiedType();
Sebastian Redldced2262009-10-11 09:03:14 +0000562
563 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
564 /*DetectVirtual=*/false);
565
566 bool Contained = false;
567 // Make sure it's in the superset.
568 for (FunctionProtoType::exception_iterator SuperI =
569 Superset->exception_begin(), SuperE = Superset->exception_end();
570 SuperI != SuperE; ++SuperI) {
571 QualType CanonicalSuperT = Context.getCanonicalType(*SuperI);
572 // SubT must be SuperT or derived from it, or pointer or reference to
573 // such types.
574 if (const ReferenceType *RefTy = CanonicalSuperT->getAs<ReferenceType>())
575 CanonicalSuperT = RefTy->getPointeeType();
576 if (SubIsPointer) {
577 if (const PointerType *PtrTy = CanonicalSuperT->getAs<PointerType>())
578 CanonicalSuperT = PtrTy->getPointeeType();
579 else {
580 continue;
581 }
582 }
Douglas Gregora4923eb2009-11-16 21:35:15 +0000583 CanonicalSuperT = CanonicalSuperT.getLocalUnqualifiedType();
Sebastian Redldced2262009-10-11 09:03:14 +0000584 // If the types are the same, move on to the next type in the subset.
585 if (CanonicalSubT == CanonicalSuperT) {
586 Contained = true;
587 break;
588 }
589
590 // Otherwise we need to check the inheritance.
591 if (!SubIsClass || !CanonicalSuperT->isRecordType())
592 continue;
593
594 Paths.clear();
595 if (!IsDerivedFrom(CanonicalSubT, CanonicalSuperT, Paths))
596 continue;
597
Douglas Gregore0d5fe22010-05-21 20:29:55 +0000598 if (Paths.isAmbiguous(Context.getCanonicalType(CanonicalSuperT)))
Sebastian Redldced2262009-10-11 09:03:14 +0000599 continue;
600
John McCall6b2accb2010-02-10 09:31:12 +0000601 // Do this check from a context without privileges.
John McCall58e6f342010-03-16 05:22:47 +0000602 switch (CheckBaseClassAccess(SourceLocation(),
John McCall6b2accb2010-02-10 09:31:12 +0000603 CanonicalSuperT, CanonicalSubT,
604 Paths.front(),
John McCall58e6f342010-03-16 05:22:47 +0000605 /*Diagnostic*/ 0,
John McCall6b2accb2010-02-10 09:31:12 +0000606 /*ForceCheck*/ true,
John McCall58e6f342010-03-16 05:22:47 +0000607 /*ForceUnprivileged*/ true)) {
John McCall6b2accb2010-02-10 09:31:12 +0000608 case AR_accessible: break;
609 case AR_inaccessible: continue;
610 case AR_dependent:
611 llvm_unreachable("access check dependent for unprivileged context");
612 break;
613 case AR_delayed:
614 llvm_unreachable("access check delayed in non-declaration");
615 break;
616 }
Sebastian Redldced2262009-10-11 09:03:14 +0000617
618 Contained = true;
619 break;
620 }
621 if (!Contained) {
622 Diag(SubLoc, DiagID);
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000623 if (NoteID.getDiagID() != 0)
Sebastian Redldced2262009-10-11 09:03:14 +0000624 Diag(SuperLoc, NoteID);
625 return true;
626 }
627 }
628 // We've run half the gauntlet.
629 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
630}
631
632static bool CheckSpecForTypesEquivalent(Sema &S,
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000633 const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID,
Sebastian Redldced2262009-10-11 09:03:14 +0000634 QualType Target, SourceLocation TargetLoc,
635 QualType Source, SourceLocation SourceLoc)
636{
637 const FunctionProtoType *TFunc = GetUnderlyingFunction(Target);
638 if (!TFunc)
639 return false;
640 const FunctionProtoType *SFunc = GetUnderlyingFunction(Source);
641 if (!SFunc)
642 return false;
643
644 return S.CheckEquivalentExceptionSpec(DiagID, NoteID, TFunc, TargetLoc,
645 SFunc, SourceLoc);
646}
647
648/// CheckParamExceptionSpec - Check if the parameter and return types of the
649/// two functions have equivalent exception specs. This is part of the
650/// assignment and override compatibility check. We do not check the parameters
651/// of parameter function pointers recursively, as no sane programmer would
652/// even be able to write such a function type.
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000653bool Sema::CheckParamExceptionSpec(const PartialDiagnostic & NoteID,
Sebastian Redldced2262009-10-11 09:03:14 +0000654 const FunctionProtoType *Target, SourceLocation TargetLoc,
655 const FunctionProtoType *Source, SourceLocation SourceLoc)
656{
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000657 if (CheckSpecForTypesEquivalent(*this,
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000658 PDiag(diag::err_deep_exception_specs_differ) << 0,
659 PDiag(),
Sebastian Redldced2262009-10-11 09:03:14 +0000660 Target->getResultType(), TargetLoc,
661 Source->getResultType(), SourceLoc))
662 return true;
663
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000664 // We shouldn't even be testing this unless the arguments are otherwise
Sebastian Redldced2262009-10-11 09:03:14 +0000665 // compatible.
666 assert(Target->getNumArgs() == Source->getNumArgs() &&
667 "Functions have different argument counts.");
668 for (unsigned i = 0, E = Target->getNumArgs(); i != E; ++i) {
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000669 if (CheckSpecForTypesEquivalent(*this,
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000670 PDiag(diag::err_deep_exception_specs_differ) << 1,
671 PDiag(),
Sebastian Redldced2262009-10-11 09:03:14 +0000672 Target->getArgType(i), TargetLoc,
673 Source->getArgType(i), SourceLoc))
674 return true;
675 }
676 return false;
677}
678
679bool Sema::CheckExceptionSpecCompatibility(Expr *From, QualType ToType)
680{
681 // First we check for applicability.
682 // Target type must be a function, function pointer or function reference.
683 const FunctionProtoType *ToFunc = GetUnderlyingFunction(ToType);
684 if (!ToFunc)
685 return false;
686
687 // SourceType must be a function or function pointer.
688 const FunctionProtoType *FromFunc = GetUnderlyingFunction(From->getType());
689 if (!FromFunc)
690 return false;
691
692 // Now we've got the correct types on both sides, check their compatibility.
693 // This means that the source of the conversion can only throw a subset of
694 // the exceptions of the target, and any exception specs on arguments or
695 // return types must be equivalent.
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000696 return CheckExceptionSpecSubset(PDiag(diag::err_incompatible_exception_specs),
697 PDiag(), ToFunc,
698 From->getSourceRange().getBegin(),
Sebastian Redldced2262009-10-11 09:03:14 +0000699 FromFunc, SourceLocation());
700}
701
702bool Sema::CheckOverridingFunctionExceptionSpec(const CXXMethodDecl *New,
703 const CXXMethodDecl *Old) {
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000704 return CheckExceptionSpecSubset(PDiag(diag::err_override_exception_spec),
705 PDiag(diag::note_overridden_virtual_function),
Sebastian Redldced2262009-10-11 09:03:14 +0000706 Old->getType()->getAs<FunctionProtoType>(),
707 Old->getLocation(),
708 New->getType()->getAs<FunctionProtoType>(),
709 New->getLocation());
710}
711
712} // end namespace clang