blob: 12d06b4905b364d6f600509e8119b2504df27ba7 [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(
103 unsigned DiagID, unsigned NoteID,
104 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);
112 if (NoteID != 0)
113 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 Redl5db4d902009-10-11 09:11:23 +0000120 llvm::SmallPtrSet<const Type*, 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 Redl5db4d902009-10-11 09:11:23 +0000123 OldTypes.insert(Context.getCanonicalType(*I).getTypePtr());
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) {
127 const Type *TypePtr = Context.getCanonicalType(*I).getTypePtr();
128 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);
140 if (NoteID != 0)
141 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.
148bool Sema::CheckExceptionSpecSubset(unsigned DiagID, unsigned NoteID,
149 const FunctionProtoType *Superset, SourceLocation SuperLoc,
150 const FunctionProtoType *Subset, SourceLocation SubLoc) {
151 // FIXME: As usual, we could be more specific in our error messages, but
152 // that better waits until we've got types with source locations.
153
154 if (!SubLoc.isValid())
155 SubLoc = SuperLoc;
156
157 // If superset contains everything, we're done.
158 if (!Superset->hasExceptionSpec() || Superset->hasAnyExceptionSpec())
159 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
160
161 // It does not. If the subset contains everything, we've failed.
162 if (!Subset->hasExceptionSpec() || Subset->hasAnyExceptionSpec()) {
163 Diag(SubLoc, DiagID);
164 if (NoteID != 0)
165 Diag(SuperLoc, NoteID);
166 return true;
167 }
168
169 // Neither contains everything. Do a proper comparison.
170 for (FunctionProtoType::exception_iterator SubI = Subset->exception_begin(),
171 SubE = Subset->exception_end(); SubI != SubE; ++SubI) {
172 // Take one type from the subset.
173 QualType CanonicalSubT = Context.getCanonicalType(*SubI);
Sebastian Redlc3a3b7b2009-10-14 14:38:54 +0000174 // Unwrap pointers and references so that we can do checks within a class
175 // hierarchy. Don't unwrap member pointers; they don't have hierarchy
176 // conversions on the pointee.
Sebastian Redldced2262009-10-11 09:03:14 +0000177 bool SubIsPointer = false;
178 if (const ReferenceType *RefTy = CanonicalSubT->getAs<ReferenceType>())
179 CanonicalSubT = RefTy->getPointeeType();
180 if (const PointerType *PtrTy = CanonicalSubT->getAs<PointerType>()) {
181 CanonicalSubT = PtrTy->getPointeeType();
182 SubIsPointer = true;
183 }
184 bool SubIsClass = CanonicalSubT->isRecordType();
185 CanonicalSubT = CanonicalSubT.getUnqualifiedType();
186
187 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
188 /*DetectVirtual=*/false);
189
190 bool Contained = false;
191 // Make sure it's in the superset.
192 for (FunctionProtoType::exception_iterator SuperI =
193 Superset->exception_begin(), SuperE = Superset->exception_end();
194 SuperI != SuperE; ++SuperI) {
195 QualType CanonicalSuperT = Context.getCanonicalType(*SuperI);
196 // SubT must be SuperT or derived from it, or pointer or reference to
197 // such types.
198 if (const ReferenceType *RefTy = CanonicalSuperT->getAs<ReferenceType>())
199 CanonicalSuperT = RefTy->getPointeeType();
200 if (SubIsPointer) {
201 if (const PointerType *PtrTy = CanonicalSuperT->getAs<PointerType>())
202 CanonicalSuperT = PtrTy->getPointeeType();
203 else {
204 continue;
205 }
206 }
207 CanonicalSuperT = CanonicalSuperT.getUnqualifiedType();
208 // If the types are the same, move on to the next type in the subset.
209 if (CanonicalSubT == CanonicalSuperT) {
210 Contained = true;
211 break;
212 }
213
214 // Otherwise we need to check the inheritance.
215 if (!SubIsClass || !CanonicalSuperT->isRecordType())
216 continue;
217
218 Paths.clear();
219 if (!IsDerivedFrom(CanonicalSubT, CanonicalSuperT, Paths))
220 continue;
221
222 if (Paths.isAmbiguous(CanonicalSuperT))
223 continue;
224
225 if (FindInaccessibleBase(CanonicalSubT, CanonicalSuperT, Paths, true))
226 continue;
227
228 Contained = true;
229 break;
230 }
231 if (!Contained) {
232 Diag(SubLoc, DiagID);
233 if (NoteID != 0)
234 Diag(SuperLoc, NoteID);
235 return true;
236 }
237 }
238 // We've run half the gauntlet.
239 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
240}
241
242static bool CheckSpecForTypesEquivalent(Sema &S,
243 unsigned DiagID, unsigned NoteID,
244 QualType Target, SourceLocation TargetLoc,
245 QualType Source, SourceLocation SourceLoc)
246{
247 const FunctionProtoType *TFunc = GetUnderlyingFunction(Target);
248 if (!TFunc)
249 return false;
250 const FunctionProtoType *SFunc = GetUnderlyingFunction(Source);
251 if (!SFunc)
252 return false;
253
254 return S.CheckEquivalentExceptionSpec(DiagID, NoteID, TFunc, TargetLoc,
255 SFunc, SourceLoc);
256}
257
258/// CheckParamExceptionSpec - Check if the parameter and return types of the
259/// two functions have equivalent exception specs. This is part of the
260/// assignment and override compatibility check. We do not check the parameters
261/// of parameter function pointers recursively, as no sane programmer would
262/// even be able to write such a function type.
263bool Sema::CheckParamExceptionSpec(unsigned NoteID,
264 const FunctionProtoType *Target, SourceLocation TargetLoc,
265 const FunctionProtoType *Source, SourceLocation SourceLoc)
266{
267 if (CheckSpecForTypesEquivalent(*this, diag::err_return_type_specs_differ, 0,
268 Target->getResultType(), TargetLoc,
269 Source->getResultType(), SourceLoc))
270 return true;
271
272 // We shouldn't even testing this unless the arguments are otherwise
273 // compatible.
274 assert(Target->getNumArgs() == Source->getNumArgs() &&
275 "Functions have different argument counts.");
276 for (unsigned i = 0, E = Target->getNumArgs(); i != E; ++i) {
277 if (CheckSpecForTypesEquivalent(*this, diag::err_arg_type_specs_differ, 0,
278 Target->getArgType(i), TargetLoc,
279 Source->getArgType(i), SourceLoc))
280 return true;
281 }
282 return false;
283}
284
285bool Sema::CheckExceptionSpecCompatibility(Expr *From, QualType ToType)
286{
287 // First we check for applicability.
288 // Target type must be a function, function pointer or function reference.
289 const FunctionProtoType *ToFunc = GetUnderlyingFunction(ToType);
290 if (!ToFunc)
291 return false;
292
293 // SourceType must be a function or function pointer.
294 const FunctionProtoType *FromFunc = GetUnderlyingFunction(From->getType());
295 if (!FromFunc)
296 return false;
297
298 // Now we've got the correct types on both sides, check their compatibility.
299 // This means that the source of the conversion can only throw a subset of
300 // the exceptions of the target, and any exception specs on arguments or
301 // return types must be equivalent.
302 return CheckExceptionSpecSubset(diag::err_incompatible_exception_specs,
303 0, ToFunc, From->getSourceRange().getBegin(),
304 FromFunc, SourceLocation());
305}
306
307bool Sema::CheckOverridingFunctionExceptionSpec(const CXXMethodDecl *New,
308 const CXXMethodDecl *Old) {
309 return CheckExceptionSpecSubset(diag::err_override_exception_spec,
310 diag::note_overridden_virtual_function,
311 Old->getType()->getAs<FunctionProtoType>(),
312 Old->getLocation(),
313 New->getType()->getAs<FunctionProtoType>(),
314 New->getLocation());
315}
316
317} // end namespace clang