blob: 25af0528d8b5fd8215cdd09079c0b65f687ad22f [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
14#include "Sema.h"
15#include "clang/Basic/Diagnostic.h"
16#include "clang/AST/CXXInheritance.h"
17#include "clang/AST/Expr.h"
18#include "clang/AST/ExprCXX.h"
19#include "llvm/ADT/SmallPtrSet.h"
20
21namespace clang {
22
23static const FunctionProtoType *GetUnderlyingFunction(QualType T)
24{
25 if (const PointerType *PtrTy = T->getAs<PointerType>())
26 T = PtrTy->getPointeeType();
27 else if (const ReferenceType *RefTy = T->getAs<ReferenceType>())
28 T = RefTy->getPointeeType();
Sebastian Redlc3a3b7b2009-10-14 14:38:54 +000029 else if (const MemberPointerType *MPTy = T->getAs<MemberPointerType>())
30 T = MPTy->getPointeeType();
Sebastian Redldced2262009-10-11 09:03:14 +000031 return T->getAs<FunctionProtoType>();
32}
33
34/// CheckSpecifiedExceptionType - Check if the given type is valid in an
35/// exception specification. Incomplete types, or pointers to incomplete types
36/// other than void are not allowed.
37bool Sema::CheckSpecifiedExceptionType(QualType T, const SourceRange &Range) {
38 // FIXME: This may not correctly work with the fix for core issue 437,
39 // where a class's own type is considered complete within its body. But
40 // perhaps RequireCompleteType itself should contain this logic?
41
42 // C++ 15.4p2: A type denoted in an exception-specification shall not denote
43 // an incomplete type.
Sebastian Redl491b84c2009-10-14 14:59:48 +000044 if (RequireCompleteType(Range.getBegin(), T,
45 PDiag(diag::err_incomplete_in_exception_spec) << /*direct*/0 << Range))
46 return true;
Sebastian Redldced2262009-10-11 09:03:14 +000047
48 // C++ 15.4p2: A type denoted in an exception-specification shall not denote
49 // an incomplete type a pointer or reference to an incomplete type, other
50 // than (cv) void*.
51 int kind;
52 if (const PointerType* IT = T->getAs<PointerType>()) {
53 T = IT->getPointeeType();
54 kind = 1;
55 } else if (const ReferenceType* IT = T->getAs<ReferenceType>()) {
56 T = IT->getPointeeType();
57 kind = 2;
58 } else
59 return false;
60
Sebastian Redl491b84c2009-10-14 14:59:48 +000061 if (!T->isVoidType() && RequireCompleteType(Range.getBegin(), T,
62 PDiag(diag::err_incomplete_in_exception_spec) << /*direct*/kind << Range))
63 return true;
Sebastian Redldced2262009-10-11 09:03:14 +000064
65 return false;
66}
67
68/// CheckDistantExceptionSpec - Check if the given type is a pointer or pointer
69/// to member to a function with an exception specification. This means that
70/// it is invalid to add another level of indirection.
71bool Sema::CheckDistantExceptionSpec(QualType T) {
72 if (const PointerType *PT = T->getAs<PointerType>())
73 T = PT->getPointeeType();
74 else if (const MemberPointerType *PT = T->getAs<MemberPointerType>())
75 T = PT->getPointeeType();
76 else
77 return false;
78
79 const FunctionProtoType *FnT = T->getAs<FunctionProtoType>();
80 if (!FnT)
81 return false;
82
83 return FnT->hasExceptionSpec();
84}
85
86/// CheckEquivalentExceptionSpec - Check if the two types have equivalent
87/// exception specifications. Exception specifications are equivalent if
88/// they allow exactly the same set of exception types. It does not matter how
89/// that is achieved. See C++ [except.spec]p2.
90bool Sema::CheckEquivalentExceptionSpec(
91 const FunctionProtoType *Old, SourceLocation OldLoc,
92 const FunctionProtoType *New, SourceLocation NewLoc) {
93 return CheckEquivalentExceptionSpec(diag::err_mismatched_exception_spec,
94 diag::note_previous_declaration,
95 Old, OldLoc, New, NewLoc);
96}
97
98/// CheckEquivalentExceptionSpec - Check if the two types have equivalent
99/// exception specifications. Exception specifications are equivalent if
100/// they allow exactly the same set of exception types. It does not matter how
101/// that is achieved. See C++ [except.spec]p2.
102bool Sema::CheckEquivalentExceptionSpec(
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000103 const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID,
Sebastian Redldced2262009-10-11 09:03:14 +0000104 const FunctionProtoType *Old, SourceLocation OldLoc,
105 const FunctionProtoType *New, SourceLocation NewLoc) {
106 bool OldAny = !Old->hasExceptionSpec() || Old->hasAnyExceptionSpec();
107 bool NewAny = !New->hasExceptionSpec() || New->hasAnyExceptionSpec();
108 if (OldAny && NewAny)
109 return false;
110 if (OldAny || NewAny) {
111 Diag(NewLoc, DiagID);
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000112 if (NoteID.getDiagID() != 0)
Sebastian Redldced2262009-10-11 09:03:14 +0000113 Diag(OldLoc, NoteID);
114 return true;
115 }
116
117 bool Success = true;
118 // Both have a definite exception spec. Collect the first set, then compare
119 // to the second.
Sebastian Redl1219d152009-10-14 15:06:25 +0000120 llvm::SmallPtrSet<CanQualType, 8> OldTypes, NewTypes;
Sebastian Redldced2262009-10-11 09:03:14 +0000121 for (FunctionProtoType::exception_iterator I = Old->exception_begin(),
122 E = Old->exception_end(); I != E; ++I)
Sebastian Redl1219d152009-10-14 15:06:25 +0000123 OldTypes.insert(Context.getCanonicalType(*I).getUnqualifiedType());
Sebastian Redldced2262009-10-11 09:03:14 +0000124
125 for (FunctionProtoType::exception_iterator I = New->exception_begin(),
Sebastian Redl5db4d902009-10-11 09:11:23 +0000126 E = New->exception_end(); I != E && Success; ++I) {
Sebastian Redl1219d152009-10-14 15:06:25 +0000127 CanQualType TypePtr = Context.getCanonicalType(*I).getUnqualifiedType();
Sebastian Redl5db4d902009-10-11 09:11:23 +0000128 if(OldTypes.count(TypePtr))
129 NewTypes.insert(TypePtr);
130 else
131 Success = false;
132 }
Sebastian Redldced2262009-10-11 09:03:14 +0000133
Sebastian Redl5db4d902009-10-11 09:11:23 +0000134 Success = Success && OldTypes.size() == NewTypes.size();
Sebastian Redldced2262009-10-11 09:03:14 +0000135
136 if (Success) {
137 return false;
138 }
139 Diag(NewLoc, DiagID);
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000140 if (NoteID.getDiagID() != 0)
Sebastian Redldced2262009-10-11 09:03:14 +0000141 Diag(OldLoc, NoteID);
142 return true;
143}
144
145/// CheckExceptionSpecSubset - Check whether the second function type's
146/// exception specification is a subset (or equivalent) of the first function
147/// type. This is used by override and pointer assignment checks.
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000148bool Sema::CheckExceptionSpecSubset(
149 const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID,
Sebastian Redldced2262009-10-11 09:03:14 +0000150 const FunctionProtoType *Superset, SourceLocation SuperLoc,
151 const FunctionProtoType *Subset, SourceLocation SubLoc) {
152 // FIXME: As usual, we could be more specific in our error messages, but
153 // that better waits until we've got types with source locations.
154
155 if (!SubLoc.isValid())
156 SubLoc = SuperLoc;
157
158 // If superset contains everything, we're done.
159 if (!Superset->hasExceptionSpec() || Superset->hasAnyExceptionSpec())
160 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
161
162 // It does not. If the subset contains everything, we've failed.
163 if (!Subset->hasExceptionSpec() || Subset->hasAnyExceptionSpec()) {
164 Diag(SubLoc, DiagID);
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000165 if (NoteID.getDiagID() != 0)
Sebastian Redldced2262009-10-11 09:03:14 +0000166 Diag(SuperLoc, NoteID);
167 return true;
168 }
169
170 // Neither contains everything. Do a proper comparison.
171 for (FunctionProtoType::exception_iterator SubI = Subset->exception_begin(),
172 SubE = Subset->exception_end(); SubI != SubE; ++SubI) {
173 // Take one type from the subset.
174 QualType CanonicalSubT = Context.getCanonicalType(*SubI);
Sebastian Redlc3a3b7b2009-10-14 14:38:54 +0000175 // Unwrap pointers and references so that we can do checks within a class
176 // hierarchy. Don't unwrap member pointers; they don't have hierarchy
177 // conversions on the pointee.
Sebastian Redldced2262009-10-11 09:03:14 +0000178 bool SubIsPointer = false;
179 if (const ReferenceType *RefTy = CanonicalSubT->getAs<ReferenceType>())
180 CanonicalSubT = RefTy->getPointeeType();
181 if (const PointerType *PtrTy = CanonicalSubT->getAs<PointerType>()) {
182 CanonicalSubT = PtrTy->getPointeeType();
183 SubIsPointer = true;
184 }
185 bool SubIsClass = CanonicalSubT->isRecordType();
Douglas Gregora4923eb2009-11-16 21:35:15 +0000186 CanonicalSubT = CanonicalSubT.getLocalUnqualifiedType();
Sebastian Redldced2262009-10-11 09:03:14 +0000187
188 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
189 /*DetectVirtual=*/false);
190
191 bool Contained = false;
192 // Make sure it's in the superset.
193 for (FunctionProtoType::exception_iterator SuperI =
194 Superset->exception_begin(), SuperE = Superset->exception_end();
195 SuperI != SuperE; ++SuperI) {
196 QualType CanonicalSuperT = Context.getCanonicalType(*SuperI);
197 // SubT must be SuperT or derived from it, or pointer or reference to
198 // such types.
199 if (const ReferenceType *RefTy = CanonicalSuperT->getAs<ReferenceType>())
200 CanonicalSuperT = RefTy->getPointeeType();
201 if (SubIsPointer) {
202 if (const PointerType *PtrTy = CanonicalSuperT->getAs<PointerType>())
203 CanonicalSuperT = PtrTy->getPointeeType();
204 else {
205 continue;
206 }
207 }
Douglas Gregora4923eb2009-11-16 21:35:15 +0000208 CanonicalSuperT = CanonicalSuperT.getLocalUnqualifiedType();
Sebastian Redldced2262009-10-11 09:03:14 +0000209 // If the types are the same, move on to the next type in the subset.
210 if (CanonicalSubT == CanonicalSuperT) {
211 Contained = true;
212 break;
213 }
214
215 // Otherwise we need to check the inheritance.
216 if (!SubIsClass || !CanonicalSuperT->isRecordType())
217 continue;
218
219 Paths.clear();
220 if (!IsDerivedFrom(CanonicalSubT, CanonicalSuperT, Paths))
221 continue;
222
223 if (Paths.isAmbiguous(CanonicalSuperT))
224 continue;
225
226 if (FindInaccessibleBase(CanonicalSubT, CanonicalSuperT, Paths, true))
227 continue;
228
229 Contained = true;
230 break;
231 }
232 if (!Contained) {
233 Diag(SubLoc, DiagID);
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000234 if (NoteID.getDiagID() != 0)
Sebastian Redldced2262009-10-11 09:03:14 +0000235 Diag(SuperLoc, NoteID);
236 return true;
237 }
238 }
239 // We've run half the gauntlet.
240 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
241}
242
243static bool CheckSpecForTypesEquivalent(Sema &S,
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000244 const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID,
Sebastian Redldced2262009-10-11 09:03:14 +0000245 QualType Target, SourceLocation TargetLoc,
246 QualType Source, SourceLocation SourceLoc)
247{
248 const FunctionProtoType *TFunc = GetUnderlyingFunction(Target);
249 if (!TFunc)
250 return false;
251 const FunctionProtoType *SFunc = GetUnderlyingFunction(Source);
252 if (!SFunc)
253 return false;
254
255 return S.CheckEquivalentExceptionSpec(DiagID, NoteID, TFunc, TargetLoc,
256 SFunc, SourceLoc);
257}
258
259/// CheckParamExceptionSpec - Check if the parameter and return types of the
260/// two functions have equivalent exception specs. This is part of the
261/// assignment and override compatibility check. We do not check the parameters
262/// of parameter function pointers recursively, as no sane programmer would
263/// even be able to write such a function type.
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000264bool Sema::CheckParamExceptionSpec(const PartialDiagnostic & NoteID,
Sebastian Redldced2262009-10-11 09:03:14 +0000265 const FunctionProtoType *Target, SourceLocation TargetLoc,
266 const FunctionProtoType *Source, SourceLocation SourceLoc)
267{
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000268 if (CheckSpecForTypesEquivalent(*this,
269 PDiag(diag::err_deep_exception_specs_differ) << 0, 0,
Sebastian Redldced2262009-10-11 09:03:14 +0000270 Target->getResultType(), TargetLoc,
271 Source->getResultType(), SourceLoc))
272 return true;
273
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000274 // We shouldn't even be testing this unless the arguments are otherwise
Sebastian Redldced2262009-10-11 09:03:14 +0000275 // compatible.
276 assert(Target->getNumArgs() == Source->getNumArgs() &&
277 "Functions have different argument counts.");
278 for (unsigned i = 0, E = Target->getNumArgs(); i != E; ++i) {
Sebastian Redl37c38ec2009-10-14 16:09:29 +0000279 if (CheckSpecForTypesEquivalent(*this,
280 PDiag(diag::err_deep_exception_specs_differ) << 1, 0,
Sebastian Redldced2262009-10-11 09:03:14 +0000281 Target->getArgType(i), TargetLoc,
282 Source->getArgType(i), SourceLoc))
283 return true;
284 }
285 return false;
286}
287
288bool Sema::CheckExceptionSpecCompatibility(Expr *From, QualType ToType)
289{
290 // First we check for applicability.
291 // Target type must be a function, function pointer or function reference.
292 const FunctionProtoType *ToFunc = GetUnderlyingFunction(ToType);
293 if (!ToFunc)
294 return false;
295
296 // SourceType must be a function or function pointer.
297 const FunctionProtoType *FromFunc = GetUnderlyingFunction(From->getType());
298 if (!FromFunc)
299 return false;
300
301 // Now we've got the correct types on both sides, check their compatibility.
302 // This means that the source of the conversion can only throw a subset of
303 // the exceptions of the target, and any exception specs on arguments or
304 // return types must be equivalent.
305 return CheckExceptionSpecSubset(diag::err_incompatible_exception_specs,
306 0, ToFunc, From->getSourceRange().getBegin(),
307 FromFunc, SourceLocation());
308}
309
310bool Sema::CheckOverridingFunctionExceptionSpec(const CXXMethodDecl *New,
311 const CXXMethodDecl *Old) {
312 return CheckExceptionSpecSubset(diag::err_override_exception_spec,
313 diag::note_overridden_virtual_function,
314 Old->getType()->getAs<FunctionProtoType>(),
315 Old->getLocation(),
316 New->getType()->getAs<FunctionProtoType>(),
317 New->getLocation());
318}
319
320} // end namespace clang