blob: eae30dc97295a9c1f9a8e2fc097f22c6ca898a4e [file] [log] [blame]
Sebastian Redl4915e632009-10-11 09:03:14 +00001//===--- SemaExceptionSpec.cpp - C++ Exception Specifications ---*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file provides Sema routines for C++ exception specification testing.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Sebastian Redl4915e632009-10-11 09:03:14 +000015#include "clang/AST/CXXInheritance.h"
16#include "clang/AST/Expr.h"
17#include "clang/AST/ExprCXX.h"
Douglas Gregord6bc5e62010-03-24 07:14:45 +000018#include "clang/AST/TypeLoc.h"
19#include "clang/Lex/Preprocessor.h"
Douglas Gregorf40863c2010-02-12 07:32:17 +000020#include "clang/Basic/Diagnostic.h"
21#include "clang/Basic/SourceManager.h"
Sebastian Redl4915e632009-10-11 09:03:14 +000022#include "llvm/ADT/SmallPtrSet.h"
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 Redl075b21d2009-10-14 14:38:54 +000032 else if (const MemberPointerType *MPTy = T->getAs<MemberPointerType>())
33 T = MPTy->getPointeeType();
Sebastian Redl4915e632009-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 Redl4915e632009-10-11 09:03:14 +000041
Douglas Gregor5b747a12009-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 Redl4915e632009-10-11 09:03:14 +000050 // C++ 15.4p2: A type denoted in an exception-specification shall not denote
51 // an incomplete type.
Sebastian Redl7eb5d372009-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 Redl4915e632009-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 Gregor5b747a12009-12-10 18:13:52 +000069 // Again as before
70 if (T->isRecordType() && T->getAs<RecordType>()->isBeingDefined())
71 return false;
72
Sebastian Redl7eb5d372009-10-14 14:59:48 +000073 if (!T->isVoidType() && RequireCompleteType(Range.getBegin(), T,
Douglas Gregor5b747a12009-12-10 18:13:52 +000074 PDiag(diag::err_incomplete_in_exception_spec) << kind << Range))
Sebastian Redl7eb5d372009-10-14 14:59:48 +000075 return true;
Sebastian Redl4915e632009-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 Gregorf40863c2010-02-12 07:32:17 +000098bool Sema::CheckEquivalentExceptionSpec(FunctionDecl *Old, FunctionDecl *New) {
Douglas Gregord6bc5e62010-03-24 07:14:45 +000099 bool MissingExceptionSpecification = false;
Douglas Gregorf40863c2010-02-12 07:32:17 +0000100 bool MissingEmptyExceptionSpecification = false;
Douglas Gregor89336232010-03-29 23:34:08 +0000101 if (!CheckEquivalentExceptionSpec(PDiag(diag::err_mismatched_exception_spec),
102 PDiag(diag::note_previous_declaration),
Douglas Gregorf40863c2010-02-12 07:32:17 +0000103 Old->getType()->getAs<FunctionProtoType>(),
104 Old->getLocation(),
105 New->getType()->getAs<FunctionProtoType>(),
106 New->getLocation(),
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000107 &MissingExceptionSpecification,
Douglas Gregorf40863c2010-02-12 07:32:17 +0000108 &MissingEmptyExceptionSpecification))
109 return false;
110
111 // The failure was something other than an empty exception
112 // specification; return an error.
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000113 if (!MissingExceptionSpecification && !MissingEmptyExceptionSpecification)
Douglas Gregorf40863c2010-02-12 07:32:17 +0000114 return true;
115
116 // The new function declaration is only missing an empty exception
117 // specification "throw()". If the throw() specification came from a
118 // function in a system header that has C linkage, just add an empty
119 // exception specification to the "new" declaration. This is an
120 // egregious workaround for glibc, which adds throw() specifications
121 // to many libc functions as an optimization. Unfortunately, that
122 // optimization isn't permitted by the C++ standard, so we're forced
123 // to work around it here.
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000124 if (MissingEmptyExceptionSpecification &&
125 isa<FunctionProtoType>(New->getType()) &&
126 (Old->getLocation().isInvalid() ||
127 Context.getSourceManager().isInSystemHeader(Old->getLocation())) &&
Douglas Gregorf40863c2010-02-12 07:32:17 +0000128 Old->isExternC()) {
129 const FunctionProtoType *NewProto
130 = cast<FunctionProtoType>(New->getType());
131 QualType NewType = Context.getFunctionType(NewProto->getResultType(),
132 NewProto->arg_type_begin(),
133 NewProto->getNumArgs(),
134 NewProto->isVariadic(),
135 NewProto->getTypeQuals(),
136 true, false, 0, 0,
137 NewProto->getNoReturnAttr(),
138 NewProto->getCallConv());
139 New->setType(NewType);
140 return false;
141 }
142
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000143 if (MissingExceptionSpecification && isa<FunctionProtoType>(New->getType())) {
144 const FunctionProtoType *NewProto
145 = cast<FunctionProtoType>(New->getType());
146 const FunctionProtoType *OldProto
147 = Old->getType()->getAs<FunctionProtoType>();
148
149 // Update the type of the function with the appropriate exception
150 // specification.
151 QualType NewType = Context.getFunctionType(NewProto->getResultType(),
152 NewProto->arg_type_begin(),
153 NewProto->getNumArgs(),
154 NewProto->isVariadic(),
155 NewProto->getTypeQuals(),
156 OldProto->hasExceptionSpec(),
157 OldProto->hasAnyExceptionSpec(),
158 OldProto->getNumExceptions(),
159 OldProto->exception_begin(),
160 NewProto->getNoReturnAttr(),
161 NewProto->getCallConv());
162 New->setType(NewType);
163
164 // If exceptions are disabled, suppress the warning about missing
165 // exception specifications for new and delete operators.
166 if (!getLangOptions().Exceptions) {
167 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);
184 OS << "throw(";
185 bool OnFirstException = true;
186 for (FunctionProtoType::exception_iterator E = OldProto->exception_begin(),
187 EEnd = OldProto->exception_end();
188 E != EEnd;
189 ++E) {
190 if (OnFirstException)
191 OnFirstException = false;
192 else
193 OS << ", ";
194
195 OS << E->getAsString(Context.PrintingPolicy);
196 }
197 OS << ")";
198 OS.flush();
199
200 SourceLocation AfterParenLoc;
201 if (TypeSourceInfo *TSInfo = New->getTypeSourceInfo()) {
202 TypeLoc TL = TSInfo->getTypeLoc();
203 if (const FunctionTypeLoc *FTLoc = dyn_cast<FunctionTypeLoc>(&TL))
204 AfterParenLoc = PP.getLocForEndOfToken(FTLoc->getRParenLoc());
205 }
206
207 if (AfterParenLoc.isInvalid())
208 Diag(New->getLocation(), diag::warn_missing_exception_specification)
209 << New << OS.str();
210 else {
211 // FIXME: This will get more complicated with C++0x
212 // late-specified return types.
213 Diag(New->getLocation(), diag::warn_missing_exception_specification)
214 << New << OS.str()
215 << CodeModificationHint::CreateInsertion(AfterParenLoc,
216 " " + OS.str().str());
217 }
218
219 if (!Old->getLocation().isInvalid())
220 Diag(Old->getLocation(), diag::note_previous_declaration);
221
222 return false;
223 }
224
Douglas Gregorf40863c2010-02-12 07:32:17 +0000225 Diag(New->getLocation(), diag::err_mismatched_exception_spec);
226 Diag(Old->getLocation(), diag::note_previous_declaration);
227 return true;
228}
229
Sebastian Redl4915e632009-10-11 09:03:14 +0000230/// CheckEquivalentExceptionSpec - Check if the two types have equivalent
231/// exception specifications. Exception specifications are equivalent if
232/// they allow exactly the same set of exception types. It does not matter how
233/// that is achieved. See C++ [except.spec]p2.
234bool Sema::CheckEquivalentExceptionSpec(
235 const FunctionProtoType *Old, SourceLocation OldLoc,
236 const FunctionProtoType *New, SourceLocation NewLoc) {
Douglas Gregor89336232010-03-29 23:34:08 +0000237 return CheckEquivalentExceptionSpec(
238 PDiag(diag::err_mismatched_exception_spec),
239 PDiag(diag::note_previous_declaration),
Sebastian Redl4915e632009-10-11 09:03:14 +0000240 Old, OldLoc, New, NewLoc);
241}
242
243/// CheckEquivalentExceptionSpec - Check if the two types have equivalent
244/// exception specifications. Exception specifications are equivalent if
245/// they allow exactly the same set of exception types. It does not matter how
246/// that is achieved. See C++ [except.spec]p2.
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000247bool Sema::CheckEquivalentExceptionSpec(const PartialDiagnostic &DiagID,
248 const PartialDiagnostic & NoteID,
249 const FunctionProtoType *Old,
250 SourceLocation OldLoc,
251 const FunctionProtoType *New,
252 SourceLocation NewLoc,
253 bool *MissingExceptionSpecification,
254 bool *MissingEmptyExceptionSpecification) {
255 if (MissingExceptionSpecification)
256 *MissingExceptionSpecification = false;
257
Douglas Gregorf40863c2010-02-12 07:32:17 +0000258 if (MissingEmptyExceptionSpecification)
259 *MissingEmptyExceptionSpecification = false;
260
Sebastian Redl4915e632009-10-11 09:03:14 +0000261 bool OldAny = !Old->hasExceptionSpec() || Old->hasAnyExceptionSpec();
262 bool NewAny = !New->hasExceptionSpec() || New->hasAnyExceptionSpec();
263 if (OldAny && NewAny)
264 return false;
265 if (OldAny || NewAny) {
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000266 if (MissingExceptionSpecification && Old->hasExceptionSpec() &&
Douglas Gregorf40863c2010-02-12 07:32:17 +0000267 !New->hasExceptionSpec()) {
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000268 // The old type has an exception specification of some sort, but
269 // the new type does not.
270 *MissingExceptionSpecification = true;
271
272 if (MissingEmptyExceptionSpecification &&
273 !Old->hasAnyExceptionSpec() && Old->getNumExceptions() == 0) {
274 // The old type has a throw() exception specification and the
275 // new type has no exception specification, and the caller asked
276 // to handle this itself.
277 *MissingEmptyExceptionSpecification = true;
278 }
279
Douglas Gregorf40863c2010-02-12 07:32:17 +0000280 return true;
281 }
282
Sebastian Redl4915e632009-10-11 09:03:14 +0000283 Diag(NewLoc, DiagID);
Sebastian Redla44822f2009-10-14 16:09:29 +0000284 if (NoteID.getDiagID() != 0)
Sebastian Redl4915e632009-10-11 09:03:14 +0000285 Diag(OldLoc, NoteID);
286 return true;
287 }
288
289 bool Success = true;
290 // Both have a definite exception spec. Collect the first set, then compare
291 // to the second.
Sebastian Redl184edca2009-10-14 15:06:25 +0000292 llvm::SmallPtrSet<CanQualType, 8> OldTypes, NewTypes;
Sebastian Redl4915e632009-10-11 09:03:14 +0000293 for (FunctionProtoType::exception_iterator I = Old->exception_begin(),
294 E = Old->exception_end(); I != E; ++I)
Sebastian Redl184edca2009-10-14 15:06:25 +0000295 OldTypes.insert(Context.getCanonicalType(*I).getUnqualifiedType());
Sebastian Redl4915e632009-10-11 09:03:14 +0000296
297 for (FunctionProtoType::exception_iterator I = New->exception_begin(),
Sebastian Redl6e4c8712009-10-11 09:11:23 +0000298 E = New->exception_end(); I != E && Success; ++I) {
Sebastian Redl184edca2009-10-14 15:06:25 +0000299 CanQualType TypePtr = Context.getCanonicalType(*I).getUnqualifiedType();
Sebastian Redl6e4c8712009-10-11 09:11:23 +0000300 if(OldTypes.count(TypePtr))
301 NewTypes.insert(TypePtr);
302 else
303 Success = false;
304 }
Sebastian Redl4915e632009-10-11 09:03:14 +0000305
Sebastian Redl6e4c8712009-10-11 09:11:23 +0000306 Success = Success && OldTypes.size() == NewTypes.size();
Sebastian Redl4915e632009-10-11 09:03:14 +0000307
308 if (Success) {
309 return false;
310 }
311 Diag(NewLoc, DiagID);
Sebastian Redla44822f2009-10-14 16:09:29 +0000312 if (NoteID.getDiagID() != 0)
Sebastian Redl4915e632009-10-11 09:03:14 +0000313 Diag(OldLoc, NoteID);
314 return true;
315}
316
317/// CheckExceptionSpecSubset - Check whether the second function type's
318/// exception specification is a subset (or equivalent) of the first function
319/// type. This is used by override and pointer assignment checks.
Sebastian Redla44822f2009-10-14 16:09:29 +0000320bool Sema::CheckExceptionSpecSubset(
321 const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID,
Sebastian Redl4915e632009-10-11 09:03:14 +0000322 const FunctionProtoType *Superset, SourceLocation SuperLoc,
323 const FunctionProtoType *Subset, SourceLocation SubLoc) {
324 // FIXME: As usual, we could be more specific in our error messages, but
325 // that better waits until we've got types with source locations.
326
327 if (!SubLoc.isValid())
328 SubLoc = SuperLoc;
329
330 // If superset contains everything, we're done.
331 if (!Superset->hasExceptionSpec() || Superset->hasAnyExceptionSpec())
332 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
333
334 // It does not. If the subset contains everything, we've failed.
335 if (!Subset->hasExceptionSpec() || Subset->hasAnyExceptionSpec()) {
336 Diag(SubLoc, DiagID);
Sebastian Redla44822f2009-10-14 16:09:29 +0000337 if (NoteID.getDiagID() != 0)
Sebastian Redl4915e632009-10-11 09:03:14 +0000338 Diag(SuperLoc, NoteID);
339 return true;
340 }
341
342 // Neither contains everything. Do a proper comparison.
343 for (FunctionProtoType::exception_iterator SubI = Subset->exception_begin(),
344 SubE = Subset->exception_end(); SubI != SubE; ++SubI) {
345 // Take one type from the subset.
346 QualType CanonicalSubT = Context.getCanonicalType(*SubI);
Sebastian Redl075b21d2009-10-14 14:38:54 +0000347 // Unwrap pointers and references so that we can do checks within a class
348 // hierarchy. Don't unwrap member pointers; they don't have hierarchy
349 // conversions on the pointee.
Sebastian Redl4915e632009-10-11 09:03:14 +0000350 bool SubIsPointer = false;
351 if (const ReferenceType *RefTy = CanonicalSubT->getAs<ReferenceType>())
352 CanonicalSubT = RefTy->getPointeeType();
353 if (const PointerType *PtrTy = CanonicalSubT->getAs<PointerType>()) {
354 CanonicalSubT = PtrTy->getPointeeType();
355 SubIsPointer = true;
356 }
357 bool SubIsClass = CanonicalSubT->isRecordType();
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +0000358 CanonicalSubT = CanonicalSubT.getLocalUnqualifiedType();
Sebastian Redl4915e632009-10-11 09:03:14 +0000359
360 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
361 /*DetectVirtual=*/false);
362
363 bool Contained = false;
364 // Make sure it's in the superset.
365 for (FunctionProtoType::exception_iterator SuperI =
366 Superset->exception_begin(), SuperE = Superset->exception_end();
367 SuperI != SuperE; ++SuperI) {
368 QualType CanonicalSuperT = Context.getCanonicalType(*SuperI);
369 // SubT must be SuperT or derived from it, or pointer or reference to
370 // such types.
371 if (const ReferenceType *RefTy = CanonicalSuperT->getAs<ReferenceType>())
372 CanonicalSuperT = RefTy->getPointeeType();
373 if (SubIsPointer) {
374 if (const PointerType *PtrTy = CanonicalSuperT->getAs<PointerType>())
375 CanonicalSuperT = PtrTy->getPointeeType();
376 else {
377 continue;
378 }
379 }
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +0000380 CanonicalSuperT = CanonicalSuperT.getLocalUnqualifiedType();
Sebastian Redl4915e632009-10-11 09:03:14 +0000381 // If the types are the same, move on to the next type in the subset.
382 if (CanonicalSubT == CanonicalSuperT) {
383 Contained = true;
384 break;
385 }
386
387 // Otherwise we need to check the inheritance.
388 if (!SubIsClass || !CanonicalSuperT->isRecordType())
389 continue;
390
391 Paths.clear();
392 if (!IsDerivedFrom(CanonicalSubT, CanonicalSuperT, Paths))
393 continue;
394
395 if (Paths.isAmbiguous(CanonicalSuperT))
396 continue;
397
John McCall5b0829a2010-02-10 09:31:12 +0000398 // Do this check from a context without privileges.
John McCall1064d7e2010-03-16 05:22:47 +0000399 switch (CheckBaseClassAccess(SourceLocation(),
John McCall5b0829a2010-02-10 09:31:12 +0000400 CanonicalSuperT, CanonicalSubT,
401 Paths.front(),
John McCall1064d7e2010-03-16 05:22:47 +0000402 /*Diagnostic*/ 0,
John McCall5b0829a2010-02-10 09:31:12 +0000403 /*ForceCheck*/ true,
John McCall1064d7e2010-03-16 05:22:47 +0000404 /*ForceUnprivileged*/ true)) {
John McCall5b0829a2010-02-10 09:31:12 +0000405 case AR_accessible: break;
406 case AR_inaccessible: continue;
407 case AR_dependent:
408 llvm_unreachable("access check dependent for unprivileged context");
409 break;
410 case AR_delayed:
411 llvm_unreachable("access check delayed in non-declaration");
412 break;
413 }
Sebastian Redl4915e632009-10-11 09:03:14 +0000414
415 Contained = true;
416 break;
417 }
418 if (!Contained) {
419 Diag(SubLoc, DiagID);
Sebastian Redla44822f2009-10-14 16:09:29 +0000420 if (NoteID.getDiagID() != 0)
Sebastian Redl4915e632009-10-11 09:03:14 +0000421 Diag(SuperLoc, NoteID);
422 return true;
423 }
424 }
425 // We've run half the gauntlet.
426 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
427}
428
429static bool CheckSpecForTypesEquivalent(Sema &S,
Sebastian Redla44822f2009-10-14 16:09:29 +0000430 const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID,
Sebastian Redl4915e632009-10-11 09:03:14 +0000431 QualType Target, SourceLocation TargetLoc,
432 QualType Source, SourceLocation SourceLoc)
433{
434 const FunctionProtoType *TFunc = GetUnderlyingFunction(Target);
435 if (!TFunc)
436 return false;
437 const FunctionProtoType *SFunc = GetUnderlyingFunction(Source);
438 if (!SFunc)
439 return false;
440
441 return S.CheckEquivalentExceptionSpec(DiagID, NoteID, TFunc, TargetLoc,
442 SFunc, SourceLoc);
443}
444
445/// CheckParamExceptionSpec - Check if the parameter and return types of the
446/// two functions have equivalent exception specs. This is part of the
447/// assignment and override compatibility check. We do not check the parameters
448/// of parameter function pointers recursively, as no sane programmer would
449/// even be able to write such a function type.
Sebastian Redla44822f2009-10-14 16:09:29 +0000450bool Sema::CheckParamExceptionSpec(const PartialDiagnostic & NoteID,
Sebastian Redl4915e632009-10-11 09:03:14 +0000451 const FunctionProtoType *Target, SourceLocation TargetLoc,
452 const FunctionProtoType *Source, SourceLocation SourceLoc)
453{
Sebastian Redla44822f2009-10-14 16:09:29 +0000454 if (CheckSpecForTypesEquivalent(*this,
Douglas Gregor89336232010-03-29 23:34:08 +0000455 PDiag(diag::err_deep_exception_specs_differ) << 0,
456 PDiag(),
Sebastian Redl4915e632009-10-11 09:03:14 +0000457 Target->getResultType(), TargetLoc,
458 Source->getResultType(), SourceLoc))
459 return true;
460
Sebastian Redla44822f2009-10-14 16:09:29 +0000461 // We shouldn't even be testing this unless the arguments are otherwise
Sebastian Redl4915e632009-10-11 09:03:14 +0000462 // compatible.
463 assert(Target->getNumArgs() == Source->getNumArgs() &&
464 "Functions have different argument counts.");
465 for (unsigned i = 0, E = Target->getNumArgs(); i != E; ++i) {
Sebastian Redla44822f2009-10-14 16:09:29 +0000466 if (CheckSpecForTypesEquivalent(*this,
Douglas Gregor89336232010-03-29 23:34:08 +0000467 PDiag(diag::err_deep_exception_specs_differ) << 1,
468 PDiag(),
Sebastian Redl4915e632009-10-11 09:03:14 +0000469 Target->getArgType(i), TargetLoc,
470 Source->getArgType(i), SourceLoc))
471 return true;
472 }
473 return false;
474}
475
476bool Sema::CheckExceptionSpecCompatibility(Expr *From, QualType ToType)
477{
478 // First we check for applicability.
479 // Target type must be a function, function pointer or function reference.
480 const FunctionProtoType *ToFunc = GetUnderlyingFunction(ToType);
481 if (!ToFunc)
482 return false;
483
484 // SourceType must be a function or function pointer.
485 const FunctionProtoType *FromFunc = GetUnderlyingFunction(From->getType());
486 if (!FromFunc)
487 return false;
488
489 // Now we've got the correct types on both sides, check their compatibility.
490 // This means that the source of the conversion can only throw a subset of
491 // the exceptions of the target, and any exception specs on arguments or
492 // return types must be equivalent.
Douglas Gregor89336232010-03-29 23:34:08 +0000493 return CheckExceptionSpecSubset(PDiag(diag::err_incompatible_exception_specs),
494 PDiag(), ToFunc,
495 From->getSourceRange().getBegin(),
Sebastian Redl4915e632009-10-11 09:03:14 +0000496 FromFunc, SourceLocation());
497}
498
499bool Sema::CheckOverridingFunctionExceptionSpec(const CXXMethodDecl *New,
500 const CXXMethodDecl *Old) {
Douglas Gregor89336232010-03-29 23:34:08 +0000501 return CheckExceptionSpecSubset(PDiag(diag::err_override_exception_spec),
502 PDiag(diag::note_overridden_virtual_function),
Sebastian Redl4915e632009-10-11 09:03:14 +0000503 Old->getType()->getAs<FunctionProtoType>(),
504 Old->getLocation(),
505 New->getType()->getAs<FunctionProtoType>(),
506 New->getLocation());
507}
508
509} // end namespace clang