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