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