blob: 285f400b07b3a6a378f0402bbd4c82944e9073cc [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) {
Douglas Gregor2eef8292010-03-24 07:14:45 +000099 bool MissingExceptionSpecification = false;
Douglas Gregore13ad832010-02-12 07:32:17 +0000100 bool MissingEmptyExceptionSpecification = false;
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000101 if (!CheckEquivalentExceptionSpec(PDiag(diag::err_mismatched_exception_spec),
102 PDiag(diag::note_previous_declaration),
Douglas Gregore13ad832010-02-12 07:32:17 +0000103 Old->getType()->getAs<FunctionProtoType>(),
104 Old->getLocation(),
105 New->getType()->getAs<FunctionProtoType>(),
106 New->getLocation(),
Douglas Gregor2eef8292010-03-24 07:14:45 +0000107 &MissingExceptionSpecification,
Sebastian Redl60618fa2011-03-12 11:50:43 +0000108 &MissingEmptyExceptionSpecification,
109 /*AllowNoexceptAllMatchWithNoSpec=*/true))
Douglas Gregore13ad832010-02-12 07:32:17 +0000110 return false;
111
112 // The failure was something other than an empty exception
113 // specification; return an error.
Douglas Gregor2eef8292010-03-24 07:14:45 +0000114 if (!MissingExceptionSpecification && !MissingEmptyExceptionSpecification)
Douglas Gregore13ad832010-02-12 07:32:17 +0000115 return true;
116
John McCalle23cf432010-12-14 08:05:40 +0000117 const FunctionProtoType *NewProto
118 = New->getType()->getAs<FunctionProtoType>();
119
Douglas Gregore13ad832010-02-12 07:32:17 +0000120 // The new function declaration is only missing an empty exception
121 // specification "throw()". If the throw() specification came from a
122 // function in a system header that has C linkage, just add an empty
123 // exception specification to the "new" declaration. This is an
124 // egregious workaround for glibc, which adds throw() specifications
125 // to many libc functions as an optimization. Unfortunately, that
126 // optimization isn't permitted by the C++ standard, so we're forced
127 // to work around it here.
John McCalle23cf432010-12-14 08:05:40 +0000128 if (MissingEmptyExceptionSpecification && NewProto &&
Douglas Gregor2eef8292010-03-24 07:14:45 +0000129 (Old->getLocation().isInvalid() ||
130 Context.getSourceManager().isInSystemHeader(Old->getLocation())) &&
Douglas Gregore13ad832010-02-12 07:32:17 +0000131 Old->isExternC()) {
John McCalle23cf432010-12-14 08:05:40 +0000132 FunctionProtoType::ExtProtoInfo EPI = NewProto->getExtProtoInfo();
Sebastian Redl60618fa2011-03-12 11:50:43 +0000133 EPI.ExceptionSpecType = EST_DynamicNone;
Douglas Gregore13ad832010-02-12 07:32:17 +0000134 QualType NewType = Context.getFunctionType(NewProto->getResultType(),
135 NewProto->arg_type_begin(),
136 NewProto->getNumArgs(),
John McCalle23cf432010-12-14 08:05:40 +0000137 EPI);
Douglas Gregore13ad832010-02-12 07:32:17 +0000138 New->setType(NewType);
139 return false;
140 }
141
John McCalle23cf432010-12-14 08:05:40 +0000142 if (MissingExceptionSpecification && NewProto) {
Douglas Gregor2eef8292010-03-24 07:14:45 +0000143 const FunctionProtoType *OldProto
144 = Old->getType()->getAs<FunctionProtoType>();
145
John McCalle23cf432010-12-14 08:05:40 +0000146 FunctionProtoType::ExtProtoInfo EPI = NewProto->getExtProtoInfo();
Sebastian Redl60618fa2011-03-12 11:50:43 +0000147 EPI.ExceptionSpecType = OldProto->getExceptionSpecType();
148 if (EPI.ExceptionSpecType == EST_Dynamic) {
149 EPI.NumExceptions = OldProto->getNumExceptions();
150 EPI.Exceptions = OldProto->exception_begin();
151 } else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) {
152 // FIXME: We can't just take the expression from the old prototype. It
153 // likely contains references to the old prototype's parameters.
154 }
John McCalle23cf432010-12-14 08:05:40 +0000155
Douglas Gregor2eef8292010-03-24 07:14:45 +0000156 // Update the type of the function with the appropriate exception
157 // specification.
158 QualType NewType = Context.getFunctionType(NewProto->getResultType(),
159 NewProto->arg_type_begin(),
160 NewProto->getNumArgs(),
John McCalle23cf432010-12-14 08:05:40 +0000161 EPI);
Douglas Gregor2eef8292010-03-24 07:14:45 +0000162 New->setType(NewType);
163
164 // If exceptions are disabled, suppress the warning about missing
165 // exception specifications for new and delete operators.
John McCall018591f2011-03-02 02:04:40 +0000166 if (!getLangOptions().CXXExceptions) {
Douglas Gregor2eef8292010-03-24 07:14:45 +0000167 switch (New->getDeclName().getCXXOverloadedOperator()) {
168 case OO_New:
169 case OO_Array_New:
170 case OO_Delete:
171 case OO_Array_Delete:
172 if (New->getDeclContext()->isTranslationUnit())
173 return false;
174 break;
175
176 default:
177 break;
178 }
179 }
180
181 // Warn about the lack of exception specification.
182 llvm::SmallString<128> ExceptionSpecString;
183 llvm::raw_svector_ostream OS(ExceptionSpecString);
Sebastian Redl60618fa2011-03-12 11:50:43 +0000184 switch (OldProto->getExceptionSpecType()) {
185 case EST_DynamicNone:
186 OS << "throw()";
187 break;
188
189 case EST_Dynamic: {
190 OS << "throw(";
191 bool OnFirstException = true;
192 for (FunctionProtoType::exception_iterator E = OldProto->exception_begin(),
193 EEnd = OldProto->exception_end();
194 E != EEnd;
195 ++E) {
196 if (OnFirstException)
197 OnFirstException = false;
198 else
199 OS << ", ";
200
201 OS << E->getAsString(Context.PrintingPolicy);
202 }
203 OS << ")";
204 break;
Douglas Gregor2eef8292010-03-24 07:14:45 +0000205 }
Sebastian Redl60618fa2011-03-12 11:50:43 +0000206
207 case EST_BasicNoexcept:
208 OS << "noexcept";
209 break;
210
211 case EST_ComputedNoexcept:
212 OS << "noexcept(";
213 OldProto->getNoexceptExpr()->printPretty(OS, Context, 0,
214 Context.PrintingPolicy);
215 OS << ")";
216 break;
217
218 default:
219 assert(false && "This spec type is compatible with none.");
220 }
Douglas Gregor2eef8292010-03-24 07:14:45 +0000221 OS.flush();
222
Abramo Bagnara796aa442011-03-12 11:17:06 +0000223 SourceLocation FixItLoc;
Douglas Gregor2eef8292010-03-24 07:14:45 +0000224 if (TypeSourceInfo *TSInfo = New->getTypeSourceInfo()) {
Abramo Bagnara723df242010-12-14 22:11:44 +0000225 TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens();
Douglas Gregor2eef8292010-03-24 07:14:45 +0000226 if (const FunctionTypeLoc *FTLoc = dyn_cast<FunctionTypeLoc>(&TL))
Abramo Bagnara796aa442011-03-12 11:17:06 +0000227 FixItLoc = PP.getLocForEndOfToken(FTLoc->getLocalRangeEnd());
Douglas Gregor2eef8292010-03-24 07:14:45 +0000228 }
229
Abramo Bagnara796aa442011-03-12 11:17:06 +0000230 if (FixItLoc.isInvalid())
Douglas Gregor2eef8292010-03-24 07:14:45 +0000231 Diag(New->getLocation(), diag::warn_missing_exception_specification)
232 << New << OS.str();
233 else {
234 // FIXME: This will get more complicated with C++0x
235 // late-specified return types.
236 Diag(New->getLocation(), diag::warn_missing_exception_specification)
237 << New << OS.str()
Abramo Bagnara796aa442011-03-12 11:17:06 +0000238 << FixItHint::CreateInsertion(FixItLoc, " " + OS.str().str());
Douglas Gregor2eef8292010-03-24 07:14:45 +0000239 }
240
241 if (!Old->getLocation().isInvalid())
242 Diag(Old->getLocation(), diag::note_previous_declaration);
243
244 return false;
245 }
246
Douglas Gregore13ad832010-02-12 07:32:17 +0000247 Diag(New->getLocation(), diag::err_mismatched_exception_spec);
248 Diag(Old->getLocation(), diag::note_previous_declaration);
249 return true;
250}
251
Sebastian Redldced2262009-10-11 09:03:14 +0000252/// CheckEquivalentExceptionSpec - Check if the two types have equivalent
253/// exception specifications. Exception specifications are equivalent if
254/// they allow exactly the same set of exception types. It does not matter how
255/// that is achieved. See C++ [except.spec]p2.
256bool Sema::CheckEquivalentExceptionSpec(
257 const FunctionProtoType *Old, SourceLocation OldLoc,
258 const FunctionProtoType *New, SourceLocation NewLoc) {
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000259 return CheckEquivalentExceptionSpec(
260 PDiag(diag::err_mismatched_exception_spec),
261 PDiag(diag::note_previous_declaration),
Sebastian Redldced2262009-10-11 09:03:14 +0000262 Old, OldLoc, New, NewLoc);
263}
264
Sebastian Redl60618fa2011-03-12 11:50:43 +0000265/// CheckEquivalentExceptionSpec - Check if the two types have compatible
266/// exception specifications. See C++ [except.spec]p3.
267bool Sema::CheckEquivalentExceptionSpec(const PartialDiagnostic &DiagID,
Douglas Gregor2eef8292010-03-24 07:14:45 +0000268 const PartialDiagnostic & NoteID,
Sebastian Redl60618fa2011-03-12 11:50:43 +0000269 const FunctionProtoType *Old,
Douglas Gregor2eef8292010-03-24 07:14:45 +0000270 SourceLocation OldLoc,
Sebastian Redl60618fa2011-03-12 11:50:43 +0000271 const FunctionProtoType *New,
Douglas Gregor2eef8292010-03-24 07:14:45 +0000272 SourceLocation NewLoc,
273 bool *MissingExceptionSpecification,
Sebastian Redl60618fa2011-03-12 11:50:43 +0000274 bool*MissingEmptyExceptionSpecification,
275 bool AllowNoexceptAllMatchWithNoSpec) {
John McCall811d0be2010-05-28 08:37:35 +0000276 // Just completely ignore this under -fno-exceptions.
John McCall018591f2011-03-02 02:04:40 +0000277 if (!getLangOptions().CXXExceptions)
John McCall811d0be2010-05-28 08:37:35 +0000278 return false;
279
Douglas Gregor2eef8292010-03-24 07:14:45 +0000280 if (MissingExceptionSpecification)
281 *MissingExceptionSpecification = false;
282
Douglas Gregore13ad832010-02-12 07:32:17 +0000283 if (MissingEmptyExceptionSpecification)
284 *MissingEmptyExceptionSpecification = false;
285
Sebastian Redl60618fa2011-03-12 11:50:43 +0000286 // C++0x [except.spec]p3: Two exception-specifications are compatible if:
287 // - both are non-throwing, regardless of their form,
288 // - both have the form noexcept(constant-expression) and the constant-
289 // expressions are equivalent,
290 // - one exception-specification is a noexcept-specification allowing all
291 // exceptions and the other is of the form throw(type-id-list), or
292 // - both are dynamic-exception-specifications that have the same set of
293 // adjusted types.
294 //
295 // C++0x [except.spec]p12: An exception-specifcation is non-throwing if it is
296 // of the form throw(), noexcept, or noexcept(constant-expression) where the
297 // constant-expression yields true.
298 //
299 // CWG 1073 Proposed resolution: Strike the third bullet above.
300 //
301 // C++0x [except.spec]p4: If any declaration of a function has an exception-
302 // specifier that is not a noexcept-specification allowing all exceptions,
303 // all declarations [...] of that function shall have a compatible
304 // exception-specification.
305 //
306 // That last point basically means that noexcept(false) matches no spec.
307 // It's considered when AllowNoexceptAllMatchWithNoSpec is true.
308
309 ExceptionSpecificationType OldEST = Old->getExceptionSpecType();
310 ExceptionSpecificationType NewEST = New->getExceptionSpecType();
311
312 // Shortcut the case where both have no spec.
313 if (OldEST == EST_None && NewEST == EST_None)
314 return false;
315
Sebastian Redl8026f6d2011-03-13 17:09:40 +0000316 FunctionProtoType::NoexceptResult OldNR = Old->getNoexceptSpec(Context);
317 FunctionProtoType::NoexceptResult NewNR = New->getNoexceptSpec(Context);
Sebastian Redl60618fa2011-03-12 11:50:43 +0000318 if (OldNR == FunctionProtoType::NR_BadNoexcept ||
319 NewNR == FunctionProtoType::NR_BadNoexcept)
320 return false;
321
322 // Dependent noexcept specifiers are compatible with each other, but nothing
323 // else.
324 // One noexcept is compatible with another if the argument is the same
325 if (OldNR == NewNR &&
326 OldNR != FunctionProtoType::NR_NoNoexcept &&
327 NewNR != FunctionProtoType::NR_NoNoexcept)
328 return false;
329 if (OldNR != NewNR &&
330 OldNR != FunctionProtoType::NR_NoNoexcept &&
331 NewNR != FunctionProtoType::NR_NoNoexcept) {
332 Diag(NewLoc, DiagID);
333 if (NoteID.getDiagID() != 0)
334 Diag(OldLoc, NoteID);
335 return true;
Douglas Gregor5b6f7692010-08-30 15:04:51 +0000336 }
337
Sebastian Redl60618fa2011-03-12 11:50:43 +0000338 if (getLangOptions().Microsoft) {
339 // Treat throw(whatever) as throw(...) to be compatible with MS headers.
340 if (OldEST == EST_Dynamic)
341 OldEST = EST_MSAny;
342 if (NewEST == EST_Dynamic)
343 NewEST = EST_MSAny;
344 }
345
346 // The MS extension throw(...) is compatible with itself.
347 if (OldEST == EST_MSAny && NewEST == EST_MSAny)
Sebastian Redldced2262009-10-11 09:03:14 +0000348 return false;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000349
350 // It's also compatible with no spec.
351 if ((OldEST == EST_None && NewEST == EST_MSAny) ||
352 (OldEST == EST_MSAny && NewEST == EST_None))
353 return false;
354
355 // It's also compatible with noexcept(false).
356 if (OldEST == EST_MSAny && NewNR == FunctionProtoType::NR_Throw)
357 return false;
358 if (NewEST == EST_MSAny && OldNR == FunctionProtoType::NR_Throw)
359 return false;
360
361 // As described above, noexcept(false) matches no spec only for functions.
362 if (AllowNoexceptAllMatchWithNoSpec) {
363 if (OldEST == EST_None && NewNR == FunctionProtoType::NR_Throw)
364 return false;
365 if (NewEST == EST_None && OldNR == FunctionProtoType::NR_Throw)
366 return false;
367 }
368
369 // Any non-throwing specifications are compatible.
370 bool OldNonThrowing = OldNR == FunctionProtoType::NR_Nothrow ||
371 OldEST == EST_DynamicNone;
372 bool NewNonThrowing = NewNR == FunctionProtoType::NR_Nothrow ||
373 NewEST == EST_DynamicNone;
374 if (OldNonThrowing && NewNonThrowing)
375 return false;
376
377 // At this point, the only remaining valid case is two matching dynamic
378 // specifications. We return here unless both specifications are dynamic.
379 if (OldEST != EST_Dynamic || NewEST != EST_Dynamic) {
Douglas Gregor2eef8292010-03-24 07:14:45 +0000380 if (MissingExceptionSpecification && Old->hasExceptionSpec() &&
Douglas Gregore13ad832010-02-12 07:32:17 +0000381 !New->hasExceptionSpec()) {
Douglas Gregor2eef8292010-03-24 07:14:45 +0000382 // The old type has an exception specification of some sort, but
383 // the new type does not.
384 *MissingExceptionSpecification = true;
385
Sebastian Redl60618fa2011-03-12 11:50:43 +0000386 if (MissingEmptyExceptionSpecification && OldNonThrowing) {
387 // The old type has a throw() or noexcept(true) exception specification
388 // and the new type has no exception specification, and the caller asked
Douglas Gregor2eef8292010-03-24 07:14:45 +0000389 // to handle this itself.
390 *MissingEmptyExceptionSpecification = true;
391 }
392
Douglas Gregore13ad832010-02-12 07:32:17 +0000393 return true;
394 }
395
Sebastian Redldced2262009-10-11 09:03:14 +0000396 Diag(NewLoc, DiagID);
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000397 if (NoteID.getDiagID() != 0)
Sebastian Redldced2262009-10-11 09:03:14 +0000398 Diag(OldLoc, NoteID);
399 return true;
400 }
401
Sebastian Redl60618fa2011-03-12 11:50:43 +0000402 assert(OldEST == EST_Dynamic && NewEST == EST_Dynamic &&
403 "Exception compatibility logic error: non-dynamic spec slipped through.");
404
Sebastian Redldced2262009-10-11 09:03:14 +0000405 bool Success = true;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000406 // Both have a dynamic exception spec. Collect the first set, then compare
Sebastian Redldced2262009-10-11 09:03:14 +0000407 // to the second.
Sebastian Redl1219d152009-10-14 15:06:25 +0000408 llvm::SmallPtrSet<CanQualType, 8> OldTypes, NewTypes;
Sebastian Redldced2262009-10-11 09:03:14 +0000409 for (FunctionProtoType::exception_iterator I = Old->exception_begin(),
410 E = Old->exception_end(); I != E; ++I)
Sebastian Redl1219d152009-10-14 15:06:25 +0000411 OldTypes.insert(Context.getCanonicalType(*I).getUnqualifiedType());
Sebastian Redldced2262009-10-11 09:03:14 +0000412
413 for (FunctionProtoType::exception_iterator I = New->exception_begin(),
Sebastian Redl5db4d902009-10-11 09:11:23 +0000414 E = New->exception_end(); I != E && Success; ++I) {
Sebastian Redl1219d152009-10-14 15:06:25 +0000415 CanQualType TypePtr = Context.getCanonicalType(*I).getUnqualifiedType();
Sebastian Redl5db4d902009-10-11 09:11:23 +0000416 if(OldTypes.count(TypePtr))
417 NewTypes.insert(TypePtr);
418 else
419 Success = false;
420 }
Sebastian Redldced2262009-10-11 09:03:14 +0000421
Sebastian Redl5db4d902009-10-11 09:11:23 +0000422 Success = Success && OldTypes.size() == NewTypes.size();
Sebastian Redldced2262009-10-11 09:03:14 +0000423
424 if (Success) {
425 return false;
426 }
427 Diag(NewLoc, DiagID);
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000428 if (NoteID.getDiagID() != 0)
Sebastian Redldced2262009-10-11 09:03:14 +0000429 Diag(OldLoc, NoteID);
430 return true;
431}
432
433/// CheckExceptionSpecSubset - Check whether the second function type's
434/// exception specification is a subset (or equivalent) of the first function
435/// type. This is used by override and pointer assignment checks.
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000436bool Sema::CheckExceptionSpecSubset(
437 const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID,
Sebastian Redldced2262009-10-11 09:03:14 +0000438 const FunctionProtoType *Superset, SourceLocation SuperLoc,
439 const FunctionProtoType *Subset, SourceLocation SubLoc) {
John McCall811d0be2010-05-28 08:37:35 +0000440
441 // Just auto-succeed under -fno-exceptions.
John McCall018591f2011-03-02 02:04:40 +0000442 if (!getLangOptions().CXXExceptions)
John McCall811d0be2010-05-28 08:37:35 +0000443 return false;
444
Sebastian Redldced2262009-10-11 09:03:14 +0000445 // FIXME: As usual, we could be more specific in our error messages, but
446 // that better waits until we've got types with source locations.
447
448 if (!SubLoc.isValid())
449 SubLoc = SuperLoc;
450
Sebastian Redl60618fa2011-03-12 11:50:43 +0000451 ExceptionSpecificationType SuperEST = Superset->getExceptionSpecType();
452
Sebastian Redldced2262009-10-11 09:03:14 +0000453 // If superset contains everything, we're done.
Sebastian Redl60618fa2011-03-12 11:50:43 +0000454 if (SuperEST == EST_None || SuperEST == EST_MSAny)
Sebastian Redldced2262009-10-11 09:03:14 +0000455 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
456
Sebastian Redl60618fa2011-03-12 11:50:43 +0000457 // If there are dependent noexcept specs, assume everything is fine. Unlike
458 // with the equivalency check, this is safe in this case, because we don't
459 // want to merge declarations. Checks after instantiation will catch any
460 // omissions we make here.
461 // We also shortcut checking if a noexcept expression was bad.
462
Sebastian Redl8026f6d2011-03-13 17:09:40 +0000463 FunctionProtoType::NoexceptResult SuperNR =Superset->getNoexceptSpec(Context);
Sebastian Redl60618fa2011-03-12 11:50:43 +0000464 if (SuperNR == FunctionProtoType::NR_BadNoexcept ||
465 SuperNR == FunctionProtoType::NR_Dependent)
466 return false;
467
468 // Another case of the superset containing everything.
469 if (SuperNR == FunctionProtoType::NR_Throw)
470 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
471
472 ExceptionSpecificationType SubEST = Subset->getExceptionSpecType();
473
Sebastian Redldced2262009-10-11 09:03:14 +0000474 // It does not. If the subset contains everything, we've failed.
Sebastian Redl60618fa2011-03-12 11:50:43 +0000475 if (SubEST == EST_None || SubEST == EST_MSAny) {
Sebastian Redldced2262009-10-11 09:03:14 +0000476 Diag(SubLoc, DiagID);
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000477 if (NoteID.getDiagID() != 0)
Sebastian Redldced2262009-10-11 09:03:14 +0000478 Diag(SuperLoc, NoteID);
479 return true;
480 }
481
Sebastian Redl8026f6d2011-03-13 17:09:40 +0000482 FunctionProtoType::NoexceptResult SubNR = Subset->getNoexceptSpec(Context);
Sebastian Redl60618fa2011-03-12 11:50:43 +0000483 if (SubNR == FunctionProtoType::NR_BadNoexcept ||
484 SubNR == FunctionProtoType::NR_Dependent)
485 return false;
486
487 // Another case of the subset containing everything.
488 if (SubNR == FunctionProtoType::NR_Throw) {
489 Diag(SubLoc, DiagID);
490 if (NoteID.getDiagID() != 0)
491 Diag(SuperLoc, NoteID);
492 return true;
493 }
494
495 // If the subset contains nothing, we're done.
496 if (SubEST == EST_DynamicNone || SubNR == FunctionProtoType::NR_Nothrow)
497 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
498
499 // Otherwise, if the superset contains nothing, we've failed.
500 if (SuperEST == EST_DynamicNone || SuperNR == FunctionProtoType::NR_Nothrow) {
501 Diag(SubLoc, DiagID);
502 if (NoteID.getDiagID() != 0)
503 Diag(SuperLoc, NoteID);
504 return true;
505 }
506
507 assert(SuperEST == EST_Dynamic && SubEST == EST_Dynamic &&
508 "Exception spec subset: non-dynamic case slipped through.");
509
510 // Neither contains everything or nothing. Do a proper comparison.
Sebastian Redldced2262009-10-11 09:03:14 +0000511 for (FunctionProtoType::exception_iterator SubI = Subset->exception_begin(),
512 SubE = Subset->exception_end(); SubI != SubE; ++SubI) {
513 // Take one type from the subset.
514 QualType CanonicalSubT = Context.getCanonicalType(*SubI);
Sebastian Redlc3a3b7b2009-10-14 14:38:54 +0000515 // Unwrap pointers and references so that we can do checks within a class
516 // hierarchy. Don't unwrap member pointers; they don't have hierarchy
517 // conversions on the pointee.
Sebastian Redldced2262009-10-11 09:03:14 +0000518 bool SubIsPointer = false;
519 if (const ReferenceType *RefTy = CanonicalSubT->getAs<ReferenceType>())
520 CanonicalSubT = RefTy->getPointeeType();
521 if (const PointerType *PtrTy = CanonicalSubT->getAs<PointerType>()) {
522 CanonicalSubT = PtrTy->getPointeeType();
523 SubIsPointer = true;
524 }
525 bool SubIsClass = CanonicalSubT->isRecordType();
Douglas Gregora4923eb2009-11-16 21:35:15 +0000526 CanonicalSubT = CanonicalSubT.getLocalUnqualifiedType();
Sebastian Redldced2262009-10-11 09:03:14 +0000527
528 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
529 /*DetectVirtual=*/false);
530
531 bool Contained = false;
532 // Make sure it's in the superset.
533 for (FunctionProtoType::exception_iterator SuperI =
534 Superset->exception_begin(), SuperE = Superset->exception_end();
535 SuperI != SuperE; ++SuperI) {
536 QualType CanonicalSuperT = Context.getCanonicalType(*SuperI);
537 // SubT must be SuperT or derived from it, or pointer or reference to
538 // such types.
539 if (const ReferenceType *RefTy = CanonicalSuperT->getAs<ReferenceType>())
540 CanonicalSuperT = RefTy->getPointeeType();
541 if (SubIsPointer) {
542 if (const PointerType *PtrTy = CanonicalSuperT->getAs<PointerType>())
543 CanonicalSuperT = PtrTy->getPointeeType();
544 else {
545 continue;
546 }
547 }
Douglas Gregora4923eb2009-11-16 21:35:15 +0000548 CanonicalSuperT = CanonicalSuperT.getLocalUnqualifiedType();
Sebastian Redldced2262009-10-11 09:03:14 +0000549 // If the types are the same, move on to the next type in the subset.
550 if (CanonicalSubT == CanonicalSuperT) {
551 Contained = true;
552 break;
553 }
554
555 // Otherwise we need to check the inheritance.
556 if (!SubIsClass || !CanonicalSuperT->isRecordType())
557 continue;
558
559 Paths.clear();
560 if (!IsDerivedFrom(CanonicalSubT, CanonicalSuperT, Paths))
561 continue;
562
Douglas Gregore0d5fe22010-05-21 20:29:55 +0000563 if (Paths.isAmbiguous(Context.getCanonicalType(CanonicalSuperT)))
Sebastian Redldced2262009-10-11 09:03:14 +0000564 continue;
565
John McCall6b2accb2010-02-10 09:31:12 +0000566 // Do this check from a context without privileges.
John McCall58e6f342010-03-16 05:22:47 +0000567 switch (CheckBaseClassAccess(SourceLocation(),
John McCall6b2accb2010-02-10 09:31:12 +0000568 CanonicalSuperT, CanonicalSubT,
569 Paths.front(),
John McCall58e6f342010-03-16 05:22:47 +0000570 /*Diagnostic*/ 0,
John McCall6b2accb2010-02-10 09:31:12 +0000571 /*ForceCheck*/ true,
John McCall58e6f342010-03-16 05:22:47 +0000572 /*ForceUnprivileged*/ true)) {
John McCall6b2accb2010-02-10 09:31:12 +0000573 case AR_accessible: break;
574 case AR_inaccessible: continue;
575 case AR_dependent:
576 llvm_unreachable("access check dependent for unprivileged context");
577 break;
578 case AR_delayed:
579 llvm_unreachable("access check delayed in non-declaration");
580 break;
581 }
Sebastian Redldced2262009-10-11 09:03:14 +0000582
583 Contained = true;
584 break;
585 }
586 if (!Contained) {
587 Diag(SubLoc, DiagID);
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000588 if (NoteID.getDiagID() != 0)
Sebastian Redldced2262009-10-11 09:03:14 +0000589 Diag(SuperLoc, NoteID);
590 return true;
591 }
592 }
593 // We've run half the gauntlet.
594 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
595}
596
597static bool CheckSpecForTypesEquivalent(Sema &S,
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000598 const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID,
Sebastian Redldced2262009-10-11 09:03:14 +0000599 QualType Target, SourceLocation TargetLoc,
600 QualType Source, SourceLocation SourceLoc)
601{
602 const FunctionProtoType *TFunc = GetUnderlyingFunction(Target);
603 if (!TFunc)
604 return false;
605 const FunctionProtoType *SFunc = GetUnderlyingFunction(Source);
606 if (!SFunc)
607 return false;
608
609 return S.CheckEquivalentExceptionSpec(DiagID, NoteID, TFunc, TargetLoc,
610 SFunc, SourceLoc);
611}
612
613/// CheckParamExceptionSpec - Check if the parameter and return types of the
614/// two functions have equivalent exception specs. This is part of the
615/// assignment and override compatibility check. We do not check the parameters
616/// of parameter function pointers recursively, as no sane programmer would
617/// even be able to write such a function type.
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000618bool Sema::CheckParamExceptionSpec(const PartialDiagnostic & NoteID,
Sebastian Redldced2262009-10-11 09:03:14 +0000619 const FunctionProtoType *Target, SourceLocation TargetLoc,
620 const FunctionProtoType *Source, SourceLocation SourceLoc)
621{
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000622 if (CheckSpecForTypesEquivalent(*this,
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000623 PDiag(diag::err_deep_exception_specs_differ) << 0,
624 PDiag(),
Sebastian Redldced2262009-10-11 09:03:14 +0000625 Target->getResultType(), TargetLoc,
626 Source->getResultType(), SourceLoc))
627 return true;
628
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000629 // We shouldn't even be testing this unless the arguments are otherwise
Sebastian Redldced2262009-10-11 09:03:14 +0000630 // compatible.
631 assert(Target->getNumArgs() == Source->getNumArgs() &&
632 "Functions have different argument counts.");
633 for (unsigned i = 0, E = Target->getNumArgs(); i != E; ++i) {
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000634 if (CheckSpecForTypesEquivalent(*this,
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000635 PDiag(diag::err_deep_exception_specs_differ) << 1,
636 PDiag(),
Sebastian Redldced2262009-10-11 09:03:14 +0000637 Target->getArgType(i), TargetLoc,
638 Source->getArgType(i), SourceLoc))
639 return true;
640 }
641 return false;
642}
643
644bool Sema::CheckExceptionSpecCompatibility(Expr *From, QualType ToType)
645{
646 // First we check for applicability.
647 // Target type must be a function, function pointer or function reference.
648 const FunctionProtoType *ToFunc = GetUnderlyingFunction(ToType);
649 if (!ToFunc)
650 return false;
651
652 // SourceType must be a function or function pointer.
653 const FunctionProtoType *FromFunc = GetUnderlyingFunction(From->getType());
654 if (!FromFunc)
655 return false;
656
657 // Now we've got the correct types on both sides, check their compatibility.
658 // This means that the source of the conversion can only throw a subset of
659 // the exceptions of the target, and any exception specs on arguments or
660 // return types must be equivalent.
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000661 return CheckExceptionSpecSubset(PDiag(diag::err_incompatible_exception_specs),
662 PDiag(), ToFunc,
663 From->getSourceRange().getBegin(),
Sebastian Redldced2262009-10-11 09:03:14 +0000664 FromFunc, SourceLocation());
665}
666
667bool Sema::CheckOverridingFunctionExceptionSpec(const CXXMethodDecl *New,
668 const CXXMethodDecl *Old) {
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000669 return CheckExceptionSpecSubset(PDiag(diag::err_override_exception_spec),
670 PDiag(diag::note_overridden_virtual_function),
Sebastian Redldced2262009-10-11 09:03:14 +0000671 Old->getType()->getAs<FunctionProtoType>(),
672 Old->getLocation(),
673 New->getType()->getAs<FunctionProtoType>(),
674 New->getLocation());
675}
676
677} // end namespace clang