Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 1 | //===--- 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 | |
| 21 | namespace clang { |
| 22 | |
| 23 | static 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 Redl | 075b21d | 2009-10-14 14:38:54 +0000 | [diff] [blame^] | 29 | else if (const MemberPointerType *MPTy = T->getAs<MemberPointerType>()) |
| 30 | T = MPTy->getPointeeType(); |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 31 | 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. |
| 37 | bool 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. |
| 44 | // FIXME: This isn't right. This will supress diagnostics from template |
| 45 | // instantiation and then simply emit the invalid type diagnostic. |
| 46 | if (RequireCompleteType(Range.getBegin(), T, 0)) |
| 47 | return Diag(Range.getBegin(), diag::err_incomplete_in_exception_spec) |
| 48 | << Range << T << /*direct*/0; |
| 49 | |
| 50 | // C++ 15.4p2: A type denoted in an exception-specification shall not denote |
| 51 | // an incomplete type a pointer or reference to an incomplete type, other |
| 52 | // than (cv) void*. |
| 53 | int kind; |
| 54 | if (const PointerType* IT = T->getAs<PointerType>()) { |
| 55 | T = IT->getPointeeType(); |
| 56 | kind = 1; |
| 57 | } else if (const ReferenceType* IT = T->getAs<ReferenceType>()) { |
| 58 | T = IT->getPointeeType(); |
| 59 | kind = 2; |
| 60 | } else |
| 61 | return false; |
| 62 | |
| 63 | if (!T->isVoidType() && RequireCompleteType(Range.getBegin(), T, 0)) |
| 64 | return Diag(Range.getBegin(), diag::err_incomplete_in_exception_spec) |
| 65 | << Range << T << /*indirect*/kind; |
| 66 | |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | /// CheckDistantExceptionSpec - Check if the given type is a pointer or pointer |
| 71 | /// to member to a function with an exception specification. This means that |
| 72 | /// it is invalid to add another level of indirection. |
| 73 | bool Sema::CheckDistantExceptionSpec(QualType T) { |
| 74 | if (const PointerType *PT = T->getAs<PointerType>()) |
| 75 | T = PT->getPointeeType(); |
| 76 | else if (const MemberPointerType *PT = T->getAs<MemberPointerType>()) |
| 77 | T = PT->getPointeeType(); |
| 78 | else |
| 79 | return false; |
| 80 | |
| 81 | const FunctionProtoType *FnT = T->getAs<FunctionProtoType>(); |
| 82 | if (!FnT) |
| 83 | return false; |
| 84 | |
| 85 | return FnT->hasExceptionSpec(); |
| 86 | } |
| 87 | |
| 88 | /// CheckEquivalentExceptionSpec - Check if the two types have equivalent |
| 89 | /// exception specifications. Exception specifications are equivalent if |
| 90 | /// they allow exactly the same set of exception types. It does not matter how |
| 91 | /// that is achieved. See C++ [except.spec]p2. |
| 92 | bool Sema::CheckEquivalentExceptionSpec( |
| 93 | const FunctionProtoType *Old, SourceLocation OldLoc, |
| 94 | const FunctionProtoType *New, SourceLocation NewLoc) { |
| 95 | return CheckEquivalentExceptionSpec(diag::err_mismatched_exception_spec, |
| 96 | diag::note_previous_declaration, |
| 97 | Old, OldLoc, New, NewLoc); |
| 98 | } |
| 99 | |
| 100 | /// CheckEquivalentExceptionSpec - Check if the two types have equivalent |
| 101 | /// exception specifications. Exception specifications are equivalent if |
| 102 | /// they allow exactly the same set of exception types. It does not matter how |
| 103 | /// that is achieved. See C++ [except.spec]p2. |
| 104 | bool Sema::CheckEquivalentExceptionSpec( |
| 105 | unsigned DiagID, unsigned NoteID, |
| 106 | const FunctionProtoType *Old, SourceLocation OldLoc, |
| 107 | const FunctionProtoType *New, SourceLocation NewLoc) { |
| 108 | bool OldAny = !Old->hasExceptionSpec() || Old->hasAnyExceptionSpec(); |
| 109 | bool NewAny = !New->hasExceptionSpec() || New->hasAnyExceptionSpec(); |
| 110 | if (OldAny && NewAny) |
| 111 | return false; |
| 112 | if (OldAny || NewAny) { |
| 113 | Diag(NewLoc, DiagID); |
| 114 | if (NoteID != 0) |
| 115 | Diag(OldLoc, NoteID); |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | bool Success = true; |
| 120 | // Both have a definite exception spec. Collect the first set, then compare |
| 121 | // to the second. |
Sebastian Redl | 6e4c871 | 2009-10-11 09:11:23 +0000 | [diff] [blame] | 122 | llvm::SmallPtrSet<const Type*, 8> OldTypes, NewTypes; |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 123 | for (FunctionProtoType::exception_iterator I = Old->exception_begin(), |
| 124 | E = Old->exception_end(); I != E; ++I) |
Sebastian Redl | 6e4c871 | 2009-10-11 09:11:23 +0000 | [diff] [blame] | 125 | OldTypes.insert(Context.getCanonicalType(*I).getTypePtr()); |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 126 | |
| 127 | for (FunctionProtoType::exception_iterator I = New->exception_begin(), |
Sebastian Redl | 6e4c871 | 2009-10-11 09:11:23 +0000 | [diff] [blame] | 128 | E = New->exception_end(); I != E && Success; ++I) { |
| 129 | const Type *TypePtr = Context.getCanonicalType(*I).getTypePtr(); |
| 130 | if(OldTypes.count(TypePtr)) |
| 131 | NewTypes.insert(TypePtr); |
| 132 | else |
| 133 | Success = false; |
| 134 | } |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 135 | |
Sebastian Redl | 6e4c871 | 2009-10-11 09:11:23 +0000 | [diff] [blame] | 136 | Success = Success && OldTypes.size() == NewTypes.size(); |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 137 | |
| 138 | if (Success) { |
| 139 | return false; |
| 140 | } |
| 141 | Diag(NewLoc, DiagID); |
| 142 | if (NoteID != 0) |
| 143 | Diag(OldLoc, NoteID); |
| 144 | return true; |
| 145 | } |
| 146 | |
| 147 | /// CheckExceptionSpecSubset - Check whether the second function type's |
| 148 | /// exception specification is a subset (or equivalent) of the first function |
| 149 | /// type. This is used by override and pointer assignment checks. |
| 150 | bool Sema::CheckExceptionSpecSubset(unsigned DiagID, unsigned NoteID, |
| 151 | const FunctionProtoType *Superset, SourceLocation SuperLoc, |
| 152 | const FunctionProtoType *Subset, SourceLocation SubLoc) { |
| 153 | // FIXME: As usual, we could be more specific in our error messages, but |
| 154 | // that better waits until we've got types with source locations. |
| 155 | |
| 156 | if (!SubLoc.isValid()) |
| 157 | SubLoc = SuperLoc; |
| 158 | |
| 159 | // If superset contains everything, we're done. |
| 160 | if (!Superset->hasExceptionSpec() || Superset->hasAnyExceptionSpec()) |
| 161 | return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc); |
| 162 | |
| 163 | // It does not. If the subset contains everything, we've failed. |
| 164 | if (!Subset->hasExceptionSpec() || Subset->hasAnyExceptionSpec()) { |
| 165 | Diag(SubLoc, DiagID); |
| 166 | if (NoteID != 0) |
| 167 | Diag(SuperLoc, NoteID); |
| 168 | return true; |
| 169 | } |
| 170 | |
| 171 | // Neither contains everything. Do a proper comparison. |
| 172 | for (FunctionProtoType::exception_iterator SubI = Subset->exception_begin(), |
| 173 | SubE = Subset->exception_end(); SubI != SubE; ++SubI) { |
| 174 | // Take one type from the subset. |
| 175 | QualType CanonicalSubT = Context.getCanonicalType(*SubI); |
Sebastian Redl | 075b21d | 2009-10-14 14:38:54 +0000 | [diff] [blame^] | 176 | // Unwrap pointers and references so that we can do checks within a class |
| 177 | // hierarchy. Don't unwrap member pointers; they don't have hierarchy |
| 178 | // conversions on the pointee. |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 179 | bool SubIsPointer = false; |
| 180 | if (const ReferenceType *RefTy = CanonicalSubT->getAs<ReferenceType>()) |
| 181 | CanonicalSubT = RefTy->getPointeeType(); |
| 182 | if (const PointerType *PtrTy = CanonicalSubT->getAs<PointerType>()) { |
| 183 | CanonicalSubT = PtrTy->getPointeeType(); |
| 184 | SubIsPointer = true; |
| 185 | } |
| 186 | bool SubIsClass = CanonicalSubT->isRecordType(); |
| 187 | CanonicalSubT = CanonicalSubT.getUnqualifiedType(); |
| 188 | |
| 189 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 190 | /*DetectVirtual=*/false); |
| 191 | |
| 192 | bool Contained = false; |
| 193 | // Make sure it's in the superset. |
| 194 | for (FunctionProtoType::exception_iterator SuperI = |
| 195 | Superset->exception_begin(), SuperE = Superset->exception_end(); |
| 196 | SuperI != SuperE; ++SuperI) { |
| 197 | QualType CanonicalSuperT = Context.getCanonicalType(*SuperI); |
| 198 | // SubT must be SuperT or derived from it, or pointer or reference to |
| 199 | // such types. |
| 200 | if (const ReferenceType *RefTy = CanonicalSuperT->getAs<ReferenceType>()) |
| 201 | CanonicalSuperT = RefTy->getPointeeType(); |
| 202 | if (SubIsPointer) { |
| 203 | if (const PointerType *PtrTy = CanonicalSuperT->getAs<PointerType>()) |
| 204 | CanonicalSuperT = PtrTy->getPointeeType(); |
| 205 | else { |
| 206 | continue; |
| 207 | } |
| 208 | } |
| 209 | CanonicalSuperT = CanonicalSuperT.getUnqualifiedType(); |
| 210 | // If the types are the same, move on to the next type in the subset. |
| 211 | if (CanonicalSubT == CanonicalSuperT) { |
| 212 | Contained = true; |
| 213 | break; |
| 214 | } |
| 215 | |
| 216 | // Otherwise we need to check the inheritance. |
| 217 | if (!SubIsClass || !CanonicalSuperT->isRecordType()) |
| 218 | continue; |
| 219 | |
| 220 | Paths.clear(); |
| 221 | if (!IsDerivedFrom(CanonicalSubT, CanonicalSuperT, Paths)) |
| 222 | continue; |
| 223 | |
| 224 | if (Paths.isAmbiguous(CanonicalSuperT)) |
| 225 | continue; |
| 226 | |
| 227 | if (FindInaccessibleBase(CanonicalSubT, CanonicalSuperT, Paths, true)) |
| 228 | continue; |
| 229 | |
| 230 | Contained = true; |
| 231 | break; |
| 232 | } |
| 233 | if (!Contained) { |
| 234 | Diag(SubLoc, DiagID); |
| 235 | if (NoteID != 0) |
| 236 | Diag(SuperLoc, NoteID); |
| 237 | return true; |
| 238 | } |
| 239 | } |
| 240 | // We've run half the gauntlet. |
| 241 | return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc); |
| 242 | } |
| 243 | |
| 244 | static bool CheckSpecForTypesEquivalent(Sema &S, |
| 245 | unsigned DiagID, unsigned NoteID, |
| 246 | QualType Target, SourceLocation TargetLoc, |
| 247 | QualType Source, SourceLocation SourceLoc) |
| 248 | { |
| 249 | const FunctionProtoType *TFunc = GetUnderlyingFunction(Target); |
| 250 | if (!TFunc) |
| 251 | return false; |
| 252 | const FunctionProtoType *SFunc = GetUnderlyingFunction(Source); |
| 253 | if (!SFunc) |
| 254 | return false; |
| 255 | |
| 256 | return S.CheckEquivalentExceptionSpec(DiagID, NoteID, TFunc, TargetLoc, |
| 257 | SFunc, SourceLoc); |
| 258 | } |
| 259 | |
| 260 | /// CheckParamExceptionSpec - Check if the parameter and return types of the |
| 261 | /// two functions have equivalent exception specs. This is part of the |
| 262 | /// assignment and override compatibility check. We do not check the parameters |
| 263 | /// of parameter function pointers recursively, as no sane programmer would |
| 264 | /// even be able to write such a function type. |
| 265 | bool Sema::CheckParamExceptionSpec(unsigned NoteID, |
| 266 | const FunctionProtoType *Target, SourceLocation TargetLoc, |
| 267 | const FunctionProtoType *Source, SourceLocation SourceLoc) |
| 268 | { |
| 269 | if (CheckSpecForTypesEquivalent(*this, diag::err_return_type_specs_differ, 0, |
| 270 | Target->getResultType(), TargetLoc, |
| 271 | Source->getResultType(), SourceLoc)) |
| 272 | return true; |
| 273 | |
| 274 | // We shouldn't even testing this unless the arguments are otherwise |
| 275 | // compatible. |
| 276 | assert(Target->getNumArgs() == Source->getNumArgs() && |
| 277 | "Functions have different argument counts."); |
| 278 | for (unsigned i = 0, E = Target->getNumArgs(); i != E; ++i) { |
| 279 | if (CheckSpecForTypesEquivalent(*this, diag::err_arg_type_specs_differ, 0, |
| 280 | Target->getArgType(i), TargetLoc, |
| 281 | Source->getArgType(i), SourceLoc)) |
| 282 | return true; |
| 283 | } |
| 284 | return false; |
| 285 | } |
| 286 | |
| 287 | bool Sema::CheckExceptionSpecCompatibility(Expr *From, QualType ToType) |
| 288 | { |
| 289 | // First we check for applicability. |
| 290 | // Target type must be a function, function pointer or function reference. |
| 291 | const FunctionProtoType *ToFunc = GetUnderlyingFunction(ToType); |
| 292 | if (!ToFunc) |
| 293 | return false; |
| 294 | |
| 295 | // SourceType must be a function or function pointer. |
| 296 | const FunctionProtoType *FromFunc = GetUnderlyingFunction(From->getType()); |
| 297 | if (!FromFunc) |
| 298 | return false; |
| 299 | |
| 300 | // Now we've got the correct types on both sides, check their compatibility. |
| 301 | // This means that the source of the conversion can only throw a subset of |
| 302 | // the exceptions of the target, and any exception specs on arguments or |
| 303 | // return types must be equivalent. |
| 304 | return CheckExceptionSpecSubset(diag::err_incompatible_exception_specs, |
| 305 | 0, ToFunc, From->getSourceRange().getBegin(), |
| 306 | FromFunc, SourceLocation()); |
| 307 | } |
| 308 | |
| 309 | bool Sema::CheckOverridingFunctionExceptionSpec(const CXXMethodDecl *New, |
| 310 | const CXXMethodDecl *Old) { |
| 311 | return CheckExceptionSpecSubset(diag::err_override_exception_spec, |
| 312 | diag::note_overridden_virtual_function, |
| 313 | Old->getType()->getAs<FunctionProtoType>(), |
| 314 | Old->getLocation(), |
| 315 | New->getType()->getAs<FunctionProtoType>(), |
| 316 | New->getLocation()); |
| 317 | } |
| 318 | |
| 319 | } // end namespace clang |