blob: 4171ecea8a1b74375e0f315a3459c0616bfcdebe [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();
29 return T->getAs<FunctionProtoType>();
30}
31
32/// CheckSpecifiedExceptionType - Check if the given type is valid in an
33/// exception specification. Incomplete types, or pointers to incomplete types
34/// other than void are not allowed.
35bool Sema::CheckSpecifiedExceptionType(QualType T, const SourceRange &Range) {
36 // FIXME: This may not correctly work with the fix for core issue 437,
37 // where a class's own type is considered complete within its body. But
38 // perhaps RequireCompleteType itself should contain this logic?
39
40 // C++ 15.4p2: A type denoted in an exception-specification shall not denote
41 // an incomplete type.
42 // FIXME: This isn't right. This will supress diagnostics from template
43 // instantiation and then simply emit the invalid type diagnostic.
44 if (RequireCompleteType(Range.getBegin(), T, 0))
45 return Diag(Range.getBegin(), diag::err_incomplete_in_exception_spec)
46 << Range << T << /*direct*/0;
47
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
61 if (!T->isVoidType() && RequireCompleteType(Range.getBegin(), T, 0))
62 return Diag(Range.getBegin(), diag::err_incomplete_in_exception_spec)
63 << Range << T << /*indirect*/kind;
64
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);
174 bool SubIsPointer = false;
175 if (const ReferenceType *RefTy = CanonicalSubT->getAs<ReferenceType>())
176 CanonicalSubT = RefTy->getPointeeType();
177 if (const PointerType *PtrTy = CanonicalSubT->getAs<PointerType>()) {
178 CanonicalSubT = PtrTy->getPointeeType();
179 SubIsPointer = true;
180 }
181 bool SubIsClass = CanonicalSubT->isRecordType();
182 CanonicalSubT = CanonicalSubT.getUnqualifiedType();
183
184 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
185 /*DetectVirtual=*/false);
186
187 bool Contained = false;
188 // Make sure it's in the superset.
189 for (FunctionProtoType::exception_iterator SuperI =
190 Superset->exception_begin(), SuperE = Superset->exception_end();
191 SuperI != SuperE; ++SuperI) {
192 QualType CanonicalSuperT = Context.getCanonicalType(*SuperI);
193 // SubT must be SuperT or derived from it, or pointer or reference to
194 // such types.
195 if (const ReferenceType *RefTy = CanonicalSuperT->getAs<ReferenceType>())
196 CanonicalSuperT = RefTy->getPointeeType();
197 if (SubIsPointer) {
198 if (const PointerType *PtrTy = CanonicalSuperT->getAs<PointerType>())
199 CanonicalSuperT = PtrTy->getPointeeType();
200 else {
201 continue;
202 }
203 }
204 CanonicalSuperT = CanonicalSuperT.getUnqualifiedType();
205 // If the types are the same, move on to the next type in the subset.
206 if (CanonicalSubT == CanonicalSuperT) {
207 Contained = true;
208 break;
209 }
210
211 // Otherwise we need to check the inheritance.
212 if (!SubIsClass || !CanonicalSuperT->isRecordType())
213 continue;
214
215 Paths.clear();
216 if (!IsDerivedFrom(CanonicalSubT, CanonicalSuperT, Paths))
217 continue;
218
219 if (Paths.isAmbiguous(CanonicalSuperT))
220 continue;
221
222 if (FindInaccessibleBase(CanonicalSubT, CanonicalSuperT, Paths, true))
223 continue;
224
225 Contained = true;
226 break;
227 }
228 if (!Contained) {
229 Diag(SubLoc, DiagID);
230 if (NoteID != 0)
231 Diag(SuperLoc, NoteID);
232 return true;
233 }
234 }
235 // We've run half the gauntlet.
236 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
237}
238
239static bool CheckSpecForTypesEquivalent(Sema &S,
240 unsigned DiagID, unsigned NoteID,
241 QualType Target, SourceLocation TargetLoc,
242 QualType Source, SourceLocation SourceLoc)
243{
244 const FunctionProtoType *TFunc = GetUnderlyingFunction(Target);
245 if (!TFunc)
246 return false;
247 const FunctionProtoType *SFunc = GetUnderlyingFunction(Source);
248 if (!SFunc)
249 return false;
250
251 return S.CheckEquivalentExceptionSpec(DiagID, NoteID, TFunc, TargetLoc,
252 SFunc, SourceLoc);
253}
254
255/// CheckParamExceptionSpec - Check if the parameter and return types of the
256/// two functions have equivalent exception specs. This is part of the
257/// assignment and override compatibility check. We do not check the parameters
258/// of parameter function pointers recursively, as no sane programmer would
259/// even be able to write such a function type.
260bool Sema::CheckParamExceptionSpec(unsigned NoteID,
261 const FunctionProtoType *Target, SourceLocation TargetLoc,
262 const FunctionProtoType *Source, SourceLocation SourceLoc)
263{
264 if (CheckSpecForTypesEquivalent(*this, diag::err_return_type_specs_differ, 0,
265 Target->getResultType(), TargetLoc,
266 Source->getResultType(), SourceLoc))
267 return true;
268
269 // We shouldn't even testing this unless the arguments are otherwise
270 // compatible.
271 assert(Target->getNumArgs() == Source->getNumArgs() &&
272 "Functions have different argument counts.");
273 for (unsigned i = 0, E = Target->getNumArgs(); i != E; ++i) {
274 if (CheckSpecForTypesEquivalent(*this, diag::err_arg_type_specs_differ, 0,
275 Target->getArgType(i), TargetLoc,
276 Source->getArgType(i), SourceLoc))
277 return true;
278 }
279 return false;
280}
281
282bool Sema::CheckExceptionSpecCompatibility(Expr *From, QualType ToType)
283{
284 // First we check for applicability.
285 // Target type must be a function, function pointer or function reference.
286 const FunctionProtoType *ToFunc = GetUnderlyingFunction(ToType);
287 if (!ToFunc)
288 return false;
289
290 // SourceType must be a function or function pointer.
291 const FunctionProtoType *FromFunc = GetUnderlyingFunction(From->getType());
292 if (!FromFunc)
293 return false;
294
295 // Now we've got the correct types on both sides, check their compatibility.
296 // This means that the source of the conversion can only throw a subset of
297 // the exceptions of the target, and any exception specs on arguments or
298 // return types must be equivalent.
299 return CheckExceptionSpecSubset(diag::err_incompatible_exception_specs,
300 0, ToFunc, From->getSourceRange().getBegin(),
301 FromFunc, SourceLocation());
302}
303
304bool Sema::CheckOverridingFunctionExceptionSpec(const CXXMethodDecl *New,
305 const CXXMethodDecl *Old) {
306 return CheckExceptionSpecSubset(diag::err_override_exception_spec,
307 diag::note_overridden_virtual_function,
308 Old->getType()->getAs<FunctionProtoType>(),
309 Old->getLocation(),
310 New->getType()->getAs<FunctionProtoType>(),
311 New->getLocation());
312}
313
314} // end namespace clang