blob: c902e7787096d488a5805243ae93736f2a32a393 [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,
Douglas Gregore13ad832010-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 Gregor2eef8292010-03-24 07:14:45 +0000113 if (!MissingExceptionSpecification && !MissingEmptyExceptionSpecification)
Douglas Gregore13ad832010-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 Gregor2eef8292010-03-24 07:14:45 +0000124 if (MissingEmptyExceptionSpecification &&
125 isa<FunctionProtoType>(New->getType()) &&
126 (Old->getLocation().isInvalid() ||
127 Context.getSourceManager().isInSystemHeader(Old->getLocation())) &&
Douglas Gregore13ad832010-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,
Rafael Espindola264ba482010-03-30 20:24:48 +0000137 NewProto->getExtInfo());
Douglas Gregore13ad832010-02-12 07:32:17 +0000138 New->setType(NewType);
139 return false;
140 }
141
Douglas Gregor2eef8292010-03-24 07:14:45 +0000142 if (MissingExceptionSpecification && isa<FunctionProtoType>(New->getType())) {
143 const FunctionProtoType *NewProto
144 = cast<FunctionProtoType>(New->getType());
145 const FunctionProtoType *OldProto
146 = Old->getType()->getAs<FunctionProtoType>();
147
148 // Update the type of the function with the appropriate exception
149 // specification.
150 QualType NewType = Context.getFunctionType(NewProto->getResultType(),
151 NewProto->arg_type_begin(),
152 NewProto->getNumArgs(),
153 NewProto->isVariadic(),
154 NewProto->getTypeQuals(),
155 OldProto->hasExceptionSpec(),
156 OldProto->hasAnyExceptionSpec(),
157 OldProto->getNumExceptions(),
158 OldProto->exception_begin(),
Rafael Espindola264ba482010-03-30 20:24:48 +0000159 NewProto->getExtInfo());
Douglas Gregor2eef8292010-03-24 07:14:45 +0000160 New->setType(NewType);
161
162 // If exceptions are disabled, suppress the warning about missing
163 // exception specifications for new and delete operators.
164 if (!getLangOptions().Exceptions) {
165 switch (New->getDeclName().getCXXOverloadedOperator()) {
166 case OO_New:
167 case OO_Array_New:
168 case OO_Delete:
169 case OO_Array_Delete:
170 if (New->getDeclContext()->isTranslationUnit())
171 return false;
172 break;
173
174 default:
175 break;
176 }
177 }
178
179 // Warn about the lack of exception specification.
180 llvm::SmallString<128> ExceptionSpecString;
181 llvm::raw_svector_ostream OS(ExceptionSpecString);
182 OS << "throw(";
183 bool OnFirstException = true;
184 for (FunctionProtoType::exception_iterator E = OldProto->exception_begin(),
185 EEnd = OldProto->exception_end();
186 E != EEnd;
187 ++E) {
188 if (OnFirstException)
189 OnFirstException = false;
190 else
191 OS << ", ";
192
193 OS << E->getAsString(Context.PrintingPolicy);
194 }
195 OS << ")";
196 OS.flush();
197
198 SourceLocation AfterParenLoc;
199 if (TypeSourceInfo *TSInfo = New->getTypeSourceInfo()) {
200 TypeLoc TL = TSInfo->getTypeLoc();
201 if (const FunctionTypeLoc *FTLoc = dyn_cast<FunctionTypeLoc>(&TL))
202 AfterParenLoc = PP.getLocForEndOfToken(FTLoc->getRParenLoc());
203 }
204
205 if (AfterParenLoc.isInvalid())
206 Diag(New->getLocation(), diag::warn_missing_exception_specification)
207 << New << OS.str();
208 else {
209 // FIXME: This will get more complicated with C++0x
210 // late-specified return types.
211 Diag(New->getLocation(), diag::warn_missing_exception_specification)
212 << New << OS.str()
Douglas Gregor849b2432010-03-31 17:46:05 +0000213 << FixItHint::CreateInsertion(AfterParenLoc, " " + OS.str().str());
Douglas Gregor2eef8292010-03-24 07:14:45 +0000214 }
215
216 if (!Old->getLocation().isInvalid())
217 Diag(Old->getLocation(), diag::note_previous_declaration);
218
219 return false;
220 }
221
Douglas Gregore13ad832010-02-12 07:32:17 +0000222 Diag(New->getLocation(), diag::err_mismatched_exception_spec);
223 Diag(Old->getLocation(), diag::note_previous_declaration);
224 return true;
225}
226
Sebastian Redldced2262009-10-11 09:03:14 +0000227/// CheckEquivalentExceptionSpec - Check if the two types have equivalent
228/// exception specifications. Exception specifications are equivalent if
229/// they allow exactly the same set of exception types. It does not matter how
230/// that is achieved. See C++ [except.spec]p2.
231bool Sema::CheckEquivalentExceptionSpec(
232 const FunctionProtoType *Old, SourceLocation OldLoc,
233 const FunctionProtoType *New, SourceLocation NewLoc) {
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000234 return CheckEquivalentExceptionSpec(
235 PDiag(diag::err_mismatched_exception_spec),
236 PDiag(diag::note_previous_declaration),
Sebastian Redldced2262009-10-11 09:03:14 +0000237 Old, OldLoc, New, NewLoc);
238}
239
240/// CheckEquivalentExceptionSpec - Check if the two types have equivalent
241/// exception specifications. Exception specifications are equivalent if
242/// they allow exactly the same set of exception types. It does not matter how
243/// that is achieved. See C++ [except.spec]p2.
Douglas Gregor2eef8292010-03-24 07:14:45 +0000244bool Sema::CheckEquivalentExceptionSpec(const PartialDiagnostic &DiagID,
245 const PartialDiagnostic & NoteID,
246 const FunctionProtoType *Old,
247 SourceLocation OldLoc,
248 const FunctionProtoType *New,
249 SourceLocation NewLoc,
250 bool *MissingExceptionSpecification,
251 bool *MissingEmptyExceptionSpecification) {
John McCall811d0be2010-05-28 08:37:35 +0000252 // Just completely ignore this under -fno-exceptions.
253 if (!getLangOptions().Exceptions)
254 return false;
255
Douglas Gregor2eef8292010-03-24 07:14:45 +0000256 if (MissingExceptionSpecification)
257 *MissingExceptionSpecification = false;
258
Douglas Gregore13ad832010-02-12 07:32:17 +0000259 if (MissingEmptyExceptionSpecification)
260 *MissingEmptyExceptionSpecification = false;
261
Sebastian Redldced2262009-10-11 09:03:14 +0000262 bool OldAny = !Old->hasExceptionSpec() || Old->hasAnyExceptionSpec();
263 bool NewAny = !New->hasExceptionSpec() || New->hasAnyExceptionSpec();
Douglas Gregor5b6f7692010-08-30 15:04:51 +0000264 if (getLangOptions().Microsoft) {
265 // Treat throw(whatever) as throw(...) to be compatible with MS headers.
266 if (New->hasExceptionSpec() && New->getNumExceptions() > 0)
267 NewAny = true;
268 if (Old->hasExceptionSpec() && Old->getNumExceptions() > 0)
269 OldAny = true;
270 }
271
Sebastian Redldced2262009-10-11 09:03:14 +0000272 if (OldAny && NewAny)
273 return false;
274 if (OldAny || NewAny) {
Douglas Gregor2eef8292010-03-24 07:14:45 +0000275 if (MissingExceptionSpecification && Old->hasExceptionSpec() &&
Douglas Gregore13ad832010-02-12 07:32:17 +0000276 !New->hasExceptionSpec()) {
Douglas Gregor2eef8292010-03-24 07:14:45 +0000277 // The old type has an exception specification of some sort, but
278 // the new type does not.
279 *MissingExceptionSpecification = true;
280
281 if (MissingEmptyExceptionSpecification &&
282 !Old->hasAnyExceptionSpec() && Old->getNumExceptions() == 0) {
283 // The old type has a throw() exception specification and the
284 // new type has no exception specification, and the caller asked
285 // to handle this itself.
286 *MissingEmptyExceptionSpecification = true;
287 }
288
Douglas Gregore13ad832010-02-12 07:32:17 +0000289 return true;
290 }
291
Sebastian Redldced2262009-10-11 09:03:14 +0000292 Diag(NewLoc, DiagID);
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000293 if (NoteID.getDiagID() != 0)
Sebastian Redldced2262009-10-11 09:03:14 +0000294 Diag(OldLoc, NoteID);
295 return true;
296 }
297
298 bool Success = true;
299 // Both have a definite exception spec. Collect the first set, then compare
300 // to the second.
Sebastian Redl1219d152009-10-14 15:06:25 +0000301 llvm::SmallPtrSet<CanQualType, 8> OldTypes, NewTypes;
Sebastian Redldced2262009-10-11 09:03:14 +0000302 for (FunctionProtoType::exception_iterator I = Old->exception_begin(),
303 E = Old->exception_end(); I != E; ++I)
Sebastian Redl1219d152009-10-14 15:06:25 +0000304 OldTypes.insert(Context.getCanonicalType(*I).getUnqualifiedType());
Sebastian Redldced2262009-10-11 09:03:14 +0000305
306 for (FunctionProtoType::exception_iterator I = New->exception_begin(),
Sebastian Redl5db4d902009-10-11 09:11:23 +0000307 E = New->exception_end(); I != E && Success; ++I) {
Sebastian Redl1219d152009-10-14 15:06:25 +0000308 CanQualType TypePtr = Context.getCanonicalType(*I).getUnqualifiedType();
Sebastian Redl5db4d902009-10-11 09:11:23 +0000309 if(OldTypes.count(TypePtr))
310 NewTypes.insert(TypePtr);
311 else
312 Success = false;
313 }
Sebastian Redldced2262009-10-11 09:03:14 +0000314
Sebastian Redl5db4d902009-10-11 09:11:23 +0000315 Success = Success && OldTypes.size() == NewTypes.size();
Sebastian Redldced2262009-10-11 09:03:14 +0000316
317 if (Success) {
318 return false;
319 }
320 Diag(NewLoc, DiagID);
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000321 if (NoteID.getDiagID() != 0)
Sebastian Redldced2262009-10-11 09:03:14 +0000322 Diag(OldLoc, NoteID);
323 return true;
324}
325
326/// CheckExceptionSpecSubset - Check whether the second function type's
327/// exception specification is a subset (or equivalent) of the first function
328/// type. This is used by override and pointer assignment checks.
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000329bool Sema::CheckExceptionSpecSubset(
330 const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID,
Sebastian Redldced2262009-10-11 09:03:14 +0000331 const FunctionProtoType *Superset, SourceLocation SuperLoc,
332 const FunctionProtoType *Subset, SourceLocation SubLoc) {
John McCall811d0be2010-05-28 08:37:35 +0000333
334 // Just auto-succeed under -fno-exceptions.
335 if (!getLangOptions().Exceptions)
336 return false;
337
Sebastian Redldced2262009-10-11 09:03:14 +0000338 // FIXME: As usual, we could be more specific in our error messages, but
339 // that better waits until we've got types with source locations.
340
341 if (!SubLoc.isValid())
342 SubLoc = SuperLoc;
343
344 // If superset contains everything, we're done.
345 if (!Superset->hasExceptionSpec() || Superset->hasAnyExceptionSpec())
346 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
347
348 // It does not. If the subset contains everything, we've failed.
349 if (!Subset->hasExceptionSpec() || Subset->hasAnyExceptionSpec()) {
350 Diag(SubLoc, DiagID);
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000351 if (NoteID.getDiagID() != 0)
Sebastian Redldced2262009-10-11 09:03:14 +0000352 Diag(SuperLoc, NoteID);
353 return true;
354 }
355
356 // Neither contains everything. Do a proper comparison.
357 for (FunctionProtoType::exception_iterator SubI = Subset->exception_begin(),
358 SubE = Subset->exception_end(); SubI != SubE; ++SubI) {
359 // Take one type from the subset.
360 QualType CanonicalSubT = Context.getCanonicalType(*SubI);
Sebastian Redlc3a3b7b2009-10-14 14:38:54 +0000361 // Unwrap pointers and references so that we can do checks within a class
362 // hierarchy. Don't unwrap member pointers; they don't have hierarchy
363 // conversions on the pointee.
Sebastian Redldced2262009-10-11 09:03:14 +0000364 bool SubIsPointer = false;
365 if (const ReferenceType *RefTy = CanonicalSubT->getAs<ReferenceType>())
366 CanonicalSubT = RefTy->getPointeeType();
367 if (const PointerType *PtrTy = CanonicalSubT->getAs<PointerType>()) {
368 CanonicalSubT = PtrTy->getPointeeType();
369 SubIsPointer = true;
370 }
371 bool SubIsClass = CanonicalSubT->isRecordType();
Douglas Gregora4923eb2009-11-16 21:35:15 +0000372 CanonicalSubT = CanonicalSubT.getLocalUnqualifiedType();
Sebastian Redldced2262009-10-11 09:03:14 +0000373
374 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
375 /*DetectVirtual=*/false);
376
377 bool Contained = false;
378 // Make sure it's in the superset.
379 for (FunctionProtoType::exception_iterator SuperI =
380 Superset->exception_begin(), SuperE = Superset->exception_end();
381 SuperI != SuperE; ++SuperI) {
382 QualType CanonicalSuperT = Context.getCanonicalType(*SuperI);
383 // SubT must be SuperT or derived from it, or pointer or reference to
384 // such types.
385 if (const ReferenceType *RefTy = CanonicalSuperT->getAs<ReferenceType>())
386 CanonicalSuperT = RefTy->getPointeeType();
387 if (SubIsPointer) {
388 if (const PointerType *PtrTy = CanonicalSuperT->getAs<PointerType>())
389 CanonicalSuperT = PtrTy->getPointeeType();
390 else {
391 continue;
392 }
393 }
Douglas Gregora4923eb2009-11-16 21:35:15 +0000394 CanonicalSuperT = CanonicalSuperT.getLocalUnqualifiedType();
Sebastian Redldced2262009-10-11 09:03:14 +0000395 // If the types are the same, move on to the next type in the subset.
396 if (CanonicalSubT == CanonicalSuperT) {
397 Contained = true;
398 break;
399 }
400
401 // Otherwise we need to check the inheritance.
402 if (!SubIsClass || !CanonicalSuperT->isRecordType())
403 continue;
404
405 Paths.clear();
406 if (!IsDerivedFrom(CanonicalSubT, CanonicalSuperT, Paths))
407 continue;
408
Douglas Gregore0d5fe22010-05-21 20:29:55 +0000409 if (Paths.isAmbiguous(Context.getCanonicalType(CanonicalSuperT)))
Sebastian Redldced2262009-10-11 09:03:14 +0000410 continue;
411
John McCall6b2accb2010-02-10 09:31:12 +0000412 // Do this check from a context without privileges.
John McCall58e6f342010-03-16 05:22:47 +0000413 switch (CheckBaseClassAccess(SourceLocation(),
John McCall6b2accb2010-02-10 09:31:12 +0000414 CanonicalSuperT, CanonicalSubT,
415 Paths.front(),
John McCall58e6f342010-03-16 05:22:47 +0000416 /*Diagnostic*/ 0,
John McCall6b2accb2010-02-10 09:31:12 +0000417 /*ForceCheck*/ true,
John McCall58e6f342010-03-16 05:22:47 +0000418 /*ForceUnprivileged*/ true)) {
John McCall6b2accb2010-02-10 09:31:12 +0000419 case AR_accessible: break;
420 case AR_inaccessible: continue;
421 case AR_dependent:
422 llvm_unreachable("access check dependent for unprivileged context");
423 break;
424 case AR_delayed:
425 llvm_unreachable("access check delayed in non-declaration");
426 break;
427 }
Sebastian Redldced2262009-10-11 09:03:14 +0000428
429 Contained = true;
430 break;
431 }
432 if (!Contained) {
433 Diag(SubLoc, DiagID);
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000434 if (NoteID.getDiagID() != 0)
Sebastian Redldced2262009-10-11 09:03:14 +0000435 Diag(SuperLoc, NoteID);
436 return true;
437 }
438 }
439 // We've run half the gauntlet.
440 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
441}
442
443static bool CheckSpecForTypesEquivalent(Sema &S,
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000444 const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID,
Sebastian Redldced2262009-10-11 09:03:14 +0000445 QualType Target, SourceLocation TargetLoc,
446 QualType Source, SourceLocation SourceLoc)
447{
448 const FunctionProtoType *TFunc = GetUnderlyingFunction(Target);
449 if (!TFunc)
450 return false;
451 const FunctionProtoType *SFunc = GetUnderlyingFunction(Source);
452 if (!SFunc)
453 return false;
454
455 return S.CheckEquivalentExceptionSpec(DiagID, NoteID, TFunc, TargetLoc,
456 SFunc, SourceLoc);
457}
458
459/// CheckParamExceptionSpec - Check if the parameter and return types of the
460/// two functions have equivalent exception specs. This is part of the
461/// assignment and override compatibility check. We do not check the parameters
462/// of parameter function pointers recursively, as no sane programmer would
463/// even be able to write such a function type.
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000464bool Sema::CheckParamExceptionSpec(const PartialDiagnostic & NoteID,
Sebastian Redldced2262009-10-11 09:03:14 +0000465 const FunctionProtoType *Target, SourceLocation TargetLoc,
466 const FunctionProtoType *Source, SourceLocation SourceLoc)
467{
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000468 if (CheckSpecForTypesEquivalent(*this,
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000469 PDiag(diag::err_deep_exception_specs_differ) << 0,
470 PDiag(),
Sebastian Redldced2262009-10-11 09:03:14 +0000471 Target->getResultType(), TargetLoc,
472 Source->getResultType(), SourceLoc))
473 return true;
474
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000475 // We shouldn't even be testing this unless the arguments are otherwise
Sebastian Redldced2262009-10-11 09:03:14 +0000476 // compatible.
477 assert(Target->getNumArgs() == Source->getNumArgs() &&
478 "Functions have different argument counts.");
479 for (unsigned i = 0, E = Target->getNumArgs(); i != E; ++i) {
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000480 if (CheckSpecForTypesEquivalent(*this,
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000481 PDiag(diag::err_deep_exception_specs_differ) << 1,
482 PDiag(),
Sebastian Redldced2262009-10-11 09:03:14 +0000483 Target->getArgType(i), TargetLoc,
484 Source->getArgType(i), SourceLoc))
485 return true;
486 }
487 return false;
488}
489
490bool Sema::CheckExceptionSpecCompatibility(Expr *From, QualType ToType)
491{
492 // First we check for applicability.
493 // Target type must be a function, function pointer or function reference.
494 const FunctionProtoType *ToFunc = GetUnderlyingFunction(ToType);
495 if (!ToFunc)
496 return false;
497
498 // SourceType must be a function or function pointer.
499 const FunctionProtoType *FromFunc = GetUnderlyingFunction(From->getType());
500 if (!FromFunc)
501 return false;
502
503 // Now we've got the correct types on both sides, check their compatibility.
504 // This means that the source of the conversion can only throw a subset of
505 // the exceptions of the target, and any exception specs on arguments or
506 // return types must be equivalent.
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000507 return CheckExceptionSpecSubset(PDiag(diag::err_incompatible_exception_specs),
508 PDiag(), ToFunc,
509 From->getSourceRange().getBegin(),
Sebastian Redldced2262009-10-11 09:03:14 +0000510 FromFunc, SourceLocation());
511}
512
513bool Sema::CheckOverridingFunctionExceptionSpec(const CXXMethodDecl *New,
514 const CXXMethodDecl *Old) {
Douglas Gregorfe6b2d42010-03-29 23:34:08 +0000515 return CheckExceptionSpecSubset(PDiag(diag::err_override_exception_spec),
516 PDiag(diag::note_overridden_virtual_function),
Sebastian Redldced2262009-10-11 09:03:14 +0000517 Old->getType()->getAs<FunctionProtoType>(),
518 Old->getLocation(),
519 New->getType()->getAs<FunctionProtoType>(),
520 New->getLocation());
521}
522
523} // end namespace clang