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" |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 15 | #include "clang/AST/CXXInheritance.h" |
| 16 | #include "clang/AST/Expr.h" |
| 17 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 2eef829 | 2010-03-24 07:14:45 +0000 | [diff] [blame^] | 18 | #include "clang/AST/TypeLoc.h" |
| 19 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | e13ad83 | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 20 | #include "clang/Basic/Diagnostic.h" |
| 21 | #include "clang/Basic/SourceManager.h" |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallPtrSet.h" |
| 23 | |
| 24 | namespace clang { |
| 25 | |
| 26 | static const FunctionProtoType *GetUnderlyingFunction(QualType T) |
| 27 | { |
| 28 | if (const PointerType *PtrTy = T->getAs<PointerType>()) |
| 29 | T = PtrTy->getPointeeType(); |
| 30 | else if (const ReferenceType *RefTy = T->getAs<ReferenceType>()) |
| 31 | T = RefTy->getPointeeType(); |
Sebastian Redl | c3a3b7b | 2009-10-14 14:38:54 +0000 | [diff] [blame] | 32 | else if (const MemberPointerType *MPTy = T->getAs<MemberPointerType>()) |
| 33 | T = MPTy->getPointeeType(); |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 34 | return T->getAs<FunctionProtoType>(); |
| 35 | } |
| 36 | |
| 37 | /// CheckSpecifiedExceptionType - Check if the given type is valid in an |
| 38 | /// exception specification. Incomplete types, or pointers to incomplete types |
| 39 | /// other than void are not allowed. |
| 40 | bool Sema::CheckSpecifiedExceptionType(QualType T, const SourceRange &Range) { |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 41 | |
Douglas Gregor | 0966f35 | 2009-12-10 18:13:52 +0000 | [diff] [blame] | 42 | // This check (and the similar one below) deals with issue 437, that changes |
| 43 | // C++ 9.2p2 this way: |
| 44 | // Within the class member-specification, the class is regarded as complete |
| 45 | // within function bodies, default arguments, exception-specifications, and |
| 46 | // constructor ctor-initializers (including such things in nested classes). |
| 47 | if (T->isRecordType() && T->getAs<RecordType>()->isBeingDefined()) |
| 48 | return false; |
| 49 | |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 50 | // C++ 15.4p2: A type denoted in an exception-specification shall not denote |
| 51 | // an incomplete type. |
Sebastian Redl | 491b84c | 2009-10-14 14:59:48 +0000 | [diff] [blame] | 52 | if (RequireCompleteType(Range.getBegin(), T, |
| 53 | PDiag(diag::err_incomplete_in_exception_spec) << /*direct*/0 << Range)) |
| 54 | return true; |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 55 | |
| 56 | // C++ 15.4p2: A type denoted in an exception-specification shall not denote |
| 57 | // an incomplete type a pointer or reference to an incomplete type, other |
| 58 | // than (cv) void*. |
| 59 | int kind; |
| 60 | if (const PointerType* IT = T->getAs<PointerType>()) { |
| 61 | T = IT->getPointeeType(); |
| 62 | kind = 1; |
| 63 | } else if (const ReferenceType* IT = T->getAs<ReferenceType>()) { |
| 64 | T = IT->getPointeeType(); |
| 65 | kind = 2; |
| 66 | } else |
| 67 | return false; |
| 68 | |
Douglas Gregor | 0966f35 | 2009-12-10 18:13:52 +0000 | [diff] [blame] | 69 | // Again as before |
| 70 | if (T->isRecordType() && T->getAs<RecordType>()->isBeingDefined()) |
| 71 | return false; |
| 72 | |
Sebastian Redl | 491b84c | 2009-10-14 14:59:48 +0000 | [diff] [blame] | 73 | if (!T->isVoidType() && RequireCompleteType(Range.getBegin(), T, |
Douglas Gregor | 0966f35 | 2009-12-10 18:13:52 +0000 | [diff] [blame] | 74 | PDiag(diag::err_incomplete_in_exception_spec) << kind << Range)) |
Sebastian Redl | 491b84c | 2009-10-14 14:59:48 +0000 | [diff] [blame] | 75 | return true; |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 76 | |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | /// CheckDistantExceptionSpec - Check if the given type is a pointer or pointer |
| 81 | /// to member to a function with an exception specification. This means that |
| 82 | /// it is invalid to add another level of indirection. |
| 83 | bool Sema::CheckDistantExceptionSpec(QualType T) { |
| 84 | if (const PointerType *PT = T->getAs<PointerType>()) |
| 85 | T = PT->getPointeeType(); |
| 86 | else if (const MemberPointerType *PT = T->getAs<MemberPointerType>()) |
| 87 | T = PT->getPointeeType(); |
| 88 | else |
| 89 | return false; |
| 90 | |
| 91 | const FunctionProtoType *FnT = T->getAs<FunctionProtoType>(); |
| 92 | if (!FnT) |
| 93 | return false; |
| 94 | |
| 95 | return FnT->hasExceptionSpec(); |
| 96 | } |
| 97 | |
Douglas Gregor | e13ad83 | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 98 | bool Sema::CheckEquivalentExceptionSpec(FunctionDecl *Old, FunctionDecl *New) { |
Douglas Gregor | 2eef829 | 2010-03-24 07:14:45 +0000 | [diff] [blame^] | 99 | bool MissingExceptionSpecification = false; |
Douglas Gregor | e13ad83 | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 100 | bool MissingEmptyExceptionSpecification = false; |
| 101 | if (!CheckEquivalentExceptionSpec(diag::err_mismatched_exception_spec, |
| 102 | diag::note_previous_declaration, |
| 103 | Old->getType()->getAs<FunctionProtoType>(), |
| 104 | Old->getLocation(), |
| 105 | New->getType()->getAs<FunctionProtoType>(), |
| 106 | New->getLocation(), |
Douglas Gregor | 2eef829 | 2010-03-24 07:14:45 +0000 | [diff] [blame^] | 107 | &MissingExceptionSpecification, |
Douglas Gregor | e13ad83 | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 108 | &MissingEmptyExceptionSpecification)) |
| 109 | return false; |
| 110 | |
| 111 | // The failure was something other than an empty exception |
| 112 | // specification; return an error. |
Douglas Gregor | 2eef829 | 2010-03-24 07:14:45 +0000 | [diff] [blame^] | 113 | if (!MissingExceptionSpecification && !MissingEmptyExceptionSpecification) |
Douglas Gregor | e13ad83 | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 114 | return true; |
| 115 | |
| 116 | // The new function declaration is only missing an empty exception |
| 117 | // specification "throw()". If the throw() specification came from a |
| 118 | // function in a system header that has C linkage, just add an empty |
| 119 | // exception specification to the "new" declaration. This is an |
| 120 | // egregious workaround for glibc, which adds throw() specifications |
| 121 | // to many libc functions as an optimization. Unfortunately, that |
| 122 | // optimization isn't permitted by the C++ standard, so we're forced |
| 123 | // to work around it here. |
Douglas Gregor | 2eef829 | 2010-03-24 07:14:45 +0000 | [diff] [blame^] | 124 | if (MissingEmptyExceptionSpecification && |
| 125 | isa<FunctionProtoType>(New->getType()) && |
| 126 | (Old->getLocation().isInvalid() || |
| 127 | Context.getSourceManager().isInSystemHeader(Old->getLocation())) && |
Douglas Gregor | e13ad83 | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 128 | Old->isExternC()) { |
| 129 | const FunctionProtoType *NewProto |
| 130 | = cast<FunctionProtoType>(New->getType()); |
| 131 | QualType NewType = Context.getFunctionType(NewProto->getResultType(), |
| 132 | NewProto->arg_type_begin(), |
| 133 | NewProto->getNumArgs(), |
| 134 | NewProto->isVariadic(), |
| 135 | NewProto->getTypeQuals(), |
| 136 | true, false, 0, 0, |
| 137 | NewProto->getNoReturnAttr(), |
| 138 | NewProto->getCallConv()); |
| 139 | New->setType(NewType); |
| 140 | return false; |
| 141 | } |
| 142 | |
Douglas Gregor | 2eef829 | 2010-03-24 07:14:45 +0000 | [diff] [blame^] | 143 | if (MissingExceptionSpecification && isa<FunctionProtoType>(New->getType())) { |
| 144 | const FunctionProtoType *NewProto |
| 145 | = cast<FunctionProtoType>(New->getType()); |
| 146 | const FunctionProtoType *OldProto |
| 147 | = Old->getType()->getAs<FunctionProtoType>(); |
| 148 | |
| 149 | // Update the type of the function with the appropriate exception |
| 150 | // specification. |
| 151 | QualType NewType = Context.getFunctionType(NewProto->getResultType(), |
| 152 | NewProto->arg_type_begin(), |
| 153 | NewProto->getNumArgs(), |
| 154 | NewProto->isVariadic(), |
| 155 | NewProto->getTypeQuals(), |
| 156 | OldProto->hasExceptionSpec(), |
| 157 | OldProto->hasAnyExceptionSpec(), |
| 158 | OldProto->getNumExceptions(), |
| 159 | OldProto->exception_begin(), |
| 160 | NewProto->getNoReturnAttr(), |
| 161 | NewProto->getCallConv()); |
| 162 | New->setType(NewType); |
| 163 | |
| 164 | // If exceptions are disabled, suppress the warning about missing |
| 165 | // exception specifications for new and delete operators. |
| 166 | if (!getLangOptions().Exceptions) { |
| 167 | switch (New->getDeclName().getCXXOverloadedOperator()) { |
| 168 | case OO_New: |
| 169 | case OO_Array_New: |
| 170 | case OO_Delete: |
| 171 | case OO_Array_Delete: |
| 172 | if (New->getDeclContext()->isTranslationUnit()) |
| 173 | return false; |
| 174 | break; |
| 175 | |
| 176 | default: |
| 177 | break; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // Warn about the lack of exception specification. |
| 182 | llvm::SmallString<128> ExceptionSpecString; |
| 183 | llvm::raw_svector_ostream OS(ExceptionSpecString); |
| 184 | OS << "throw("; |
| 185 | bool OnFirstException = true; |
| 186 | for (FunctionProtoType::exception_iterator E = OldProto->exception_begin(), |
| 187 | EEnd = OldProto->exception_end(); |
| 188 | E != EEnd; |
| 189 | ++E) { |
| 190 | if (OnFirstException) |
| 191 | OnFirstException = false; |
| 192 | else |
| 193 | OS << ", "; |
| 194 | |
| 195 | OS << E->getAsString(Context.PrintingPolicy); |
| 196 | } |
| 197 | OS << ")"; |
| 198 | OS.flush(); |
| 199 | |
| 200 | SourceLocation AfterParenLoc; |
| 201 | if (TypeSourceInfo *TSInfo = New->getTypeSourceInfo()) { |
| 202 | TypeLoc TL = TSInfo->getTypeLoc(); |
| 203 | if (const FunctionTypeLoc *FTLoc = dyn_cast<FunctionTypeLoc>(&TL)) |
| 204 | AfterParenLoc = PP.getLocForEndOfToken(FTLoc->getRParenLoc()); |
| 205 | } |
| 206 | |
| 207 | if (AfterParenLoc.isInvalid()) |
| 208 | Diag(New->getLocation(), diag::warn_missing_exception_specification) |
| 209 | << New << OS.str(); |
| 210 | else { |
| 211 | // FIXME: This will get more complicated with C++0x |
| 212 | // late-specified return types. |
| 213 | Diag(New->getLocation(), diag::warn_missing_exception_specification) |
| 214 | << New << OS.str() |
| 215 | << CodeModificationHint::CreateInsertion(AfterParenLoc, |
| 216 | " " + OS.str().str()); |
| 217 | } |
| 218 | |
| 219 | if (!Old->getLocation().isInvalid()) |
| 220 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 221 | |
| 222 | return false; |
| 223 | } |
| 224 | |
Douglas Gregor | e13ad83 | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 225 | Diag(New->getLocation(), diag::err_mismatched_exception_spec); |
| 226 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 227 | return true; |
| 228 | } |
| 229 | |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 230 | /// CheckEquivalentExceptionSpec - Check if the two types have equivalent |
| 231 | /// exception specifications. Exception specifications are equivalent if |
| 232 | /// they allow exactly the same set of exception types. It does not matter how |
| 233 | /// that is achieved. See C++ [except.spec]p2. |
| 234 | bool Sema::CheckEquivalentExceptionSpec( |
| 235 | const FunctionProtoType *Old, SourceLocation OldLoc, |
| 236 | const FunctionProtoType *New, SourceLocation NewLoc) { |
| 237 | return CheckEquivalentExceptionSpec(diag::err_mismatched_exception_spec, |
| 238 | diag::note_previous_declaration, |
| 239 | Old, OldLoc, New, NewLoc); |
| 240 | } |
| 241 | |
| 242 | /// CheckEquivalentExceptionSpec - Check if the two types have equivalent |
| 243 | /// exception specifications. Exception specifications are equivalent if |
| 244 | /// they allow exactly the same set of exception types. It does not matter how |
| 245 | /// that is achieved. See C++ [except.spec]p2. |
Douglas Gregor | 2eef829 | 2010-03-24 07:14:45 +0000 | [diff] [blame^] | 246 | bool Sema::CheckEquivalentExceptionSpec(const PartialDiagnostic &DiagID, |
| 247 | const PartialDiagnostic & NoteID, |
| 248 | const FunctionProtoType *Old, |
| 249 | SourceLocation OldLoc, |
| 250 | const FunctionProtoType *New, |
| 251 | SourceLocation NewLoc, |
| 252 | bool *MissingExceptionSpecification, |
| 253 | bool *MissingEmptyExceptionSpecification) { |
| 254 | if (MissingExceptionSpecification) |
| 255 | *MissingExceptionSpecification = false; |
| 256 | |
Douglas Gregor | e13ad83 | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 257 | if (MissingEmptyExceptionSpecification) |
| 258 | *MissingEmptyExceptionSpecification = false; |
| 259 | |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 260 | bool OldAny = !Old->hasExceptionSpec() || Old->hasAnyExceptionSpec(); |
| 261 | bool NewAny = !New->hasExceptionSpec() || New->hasAnyExceptionSpec(); |
| 262 | if (OldAny && NewAny) |
| 263 | return false; |
| 264 | if (OldAny || NewAny) { |
Douglas Gregor | 2eef829 | 2010-03-24 07:14:45 +0000 | [diff] [blame^] | 265 | if (MissingExceptionSpecification && Old->hasExceptionSpec() && |
Douglas Gregor | e13ad83 | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 266 | !New->hasExceptionSpec()) { |
Douglas Gregor | 2eef829 | 2010-03-24 07:14:45 +0000 | [diff] [blame^] | 267 | // The old type has an exception specification of some sort, but |
| 268 | // the new type does not. |
| 269 | *MissingExceptionSpecification = true; |
| 270 | |
| 271 | if (MissingEmptyExceptionSpecification && |
| 272 | !Old->hasAnyExceptionSpec() && Old->getNumExceptions() == 0) { |
| 273 | // The old type has a throw() exception specification and the |
| 274 | // new type has no exception specification, and the caller asked |
| 275 | // to handle this itself. |
| 276 | *MissingEmptyExceptionSpecification = true; |
| 277 | } |
| 278 | |
Douglas Gregor | e13ad83 | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 279 | return true; |
| 280 | } |
| 281 | |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 282 | Diag(NewLoc, DiagID); |
Sebastian Redl | 37c38ec | 2009-10-14 16:09:29 +0000 | [diff] [blame] | 283 | if (NoteID.getDiagID() != 0) |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 284 | Diag(OldLoc, NoteID); |
| 285 | return true; |
| 286 | } |
| 287 | |
| 288 | bool Success = true; |
| 289 | // Both have a definite exception spec. Collect the first set, then compare |
| 290 | // to the second. |
Sebastian Redl | 1219d15 | 2009-10-14 15:06:25 +0000 | [diff] [blame] | 291 | llvm::SmallPtrSet<CanQualType, 8> OldTypes, NewTypes; |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 292 | for (FunctionProtoType::exception_iterator I = Old->exception_begin(), |
| 293 | E = Old->exception_end(); I != E; ++I) |
Sebastian Redl | 1219d15 | 2009-10-14 15:06:25 +0000 | [diff] [blame] | 294 | OldTypes.insert(Context.getCanonicalType(*I).getUnqualifiedType()); |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 295 | |
| 296 | for (FunctionProtoType::exception_iterator I = New->exception_begin(), |
Sebastian Redl | 5db4d90 | 2009-10-11 09:11:23 +0000 | [diff] [blame] | 297 | E = New->exception_end(); I != E && Success; ++I) { |
Sebastian Redl | 1219d15 | 2009-10-14 15:06:25 +0000 | [diff] [blame] | 298 | CanQualType TypePtr = Context.getCanonicalType(*I).getUnqualifiedType(); |
Sebastian Redl | 5db4d90 | 2009-10-11 09:11:23 +0000 | [diff] [blame] | 299 | if(OldTypes.count(TypePtr)) |
| 300 | NewTypes.insert(TypePtr); |
| 301 | else |
| 302 | Success = false; |
| 303 | } |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 304 | |
Sebastian Redl | 5db4d90 | 2009-10-11 09:11:23 +0000 | [diff] [blame] | 305 | Success = Success && OldTypes.size() == NewTypes.size(); |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 306 | |
| 307 | if (Success) { |
| 308 | return false; |
| 309 | } |
| 310 | Diag(NewLoc, DiagID); |
Sebastian Redl | 37c38ec | 2009-10-14 16:09:29 +0000 | [diff] [blame] | 311 | if (NoteID.getDiagID() != 0) |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 312 | Diag(OldLoc, NoteID); |
| 313 | return true; |
| 314 | } |
| 315 | |
| 316 | /// CheckExceptionSpecSubset - Check whether the second function type's |
| 317 | /// exception specification is a subset (or equivalent) of the first function |
| 318 | /// type. This is used by override and pointer assignment checks. |
Sebastian Redl | 37c38ec | 2009-10-14 16:09:29 +0000 | [diff] [blame] | 319 | bool Sema::CheckExceptionSpecSubset( |
| 320 | const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID, |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 321 | const FunctionProtoType *Superset, SourceLocation SuperLoc, |
| 322 | const FunctionProtoType *Subset, SourceLocation SubLoc) { |
| 323 | // FIXME: As usual, we could be more specific in our error messages, but |
| 324 | // that better waits until we've got types with source locations. |
| 325 | |
| 326 | if (!SubLoc.isValid()) |
| 327 | SubLoc = SuperLoc; |
| 328 | |
| 329 | // If superset contains everything, we're done. |
| 330 | if (!Superset->hasExceptionSpec() || Superset->hasAnyExceptionSpec()) |
| 331 | return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc); |
| 332 | |
| 333 | // It does not. If the subset contains everything, we've failed. |
| 334 | if (!Subset->hasExceptionSpec() || Subset->hasAnyExceptionSpec()) { |
| 335 | Diag(SubLoc, DiagID); |
Sebastian Redl | 37c38ec | 2009-10-14 16:09:29 +0000 | [diff] [blame] | 336 | if (NoteID.getDiagID() != 0) |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 337 | Diag(SuperLoc, NoteID); |
| 338 | return true; |
| 339 | } |
| 340 | |
| 341 | // Neither contains everything. Do a proper comparison. |
| 342 | for (FunctionProtoType::exception_iterator SubI = Subset->exception_begin(), |
| 343 | SubE = Subset->exception_end(); SubI != SubE; ++SubI) { |
| 344 | // Take one type from the subset. |
| 345 | QualType CanonicalSubT = Context.getCanonicalType(*SubI); |
Sebastian Redl | c3a3b7b | 2009-10-14 14:38:54 +0000 | [diff] [blame] | 346 | // Unwrap pointers and references so that we can do checks within a class |
| 347 | // hierarchy. Don't unwrap member pointers; they don't have hierarchy |
| 348 | // conversions on the pointee. |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 349 | bool SubIsPointer = false; |
| 350 | if (const ReferenceType *RefTy = CanonicalSubT->getAs<ReferenceType>()) |
| 351 | CanonicalSubT = RefTy->getPointeeType(); |
| 352 | if (const PointerType *PtrTy = CanonicalSubT->getAs<PointerType>()) { |
| 353 | CanonicalSubT = PtrTy->getPointeeType(); |
| 354 | SubIsPointer = true; |
| 355 | } |
| 356 | bool SubIsClass = CanonicalSubT->isRecordType(); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 357 | CanonicalSubT = CanonicalSubT.getLocalUnqualifiedType(); |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 358 | |
| 359 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 360 | /*DetectVirtual=*/false); |
| 361 | |
| 362 | bool Contained = false; |
| 363 | // Make sure it's in the superset. |
| 364 | for (FunctionProtoType::exception_iterator SuperI = |
| 365 | Superset->exception_begin(), SuperE = Superset->exception_end(); |
| 366 | SuperI != SuperE; ++SuperI) { |
| 367 | QualType CanonicalSuperT = Context.getCanonicalType(*SuperI); |
| 368 | // SubT must be SuperT or derived from it, or pointer or reference to |
| 369 | // such types. |
| 370 | if (const ReferenceType *RefTy = CanonicalSuperT->getAs<ReferenceType>()) |
| 371 | CanonicalSuperT = RefTy->getPointeeType(); |
| 372 | if (SubIsPointer) { |
| 373 | if (const PointerType *PtrTy = CanonicalSuperT->getAs<PointerType>()) |
| 374 | CanonicalSuperT = PtrTy->getPointeeType(); |
| 375 | else { |
| 376 | continue; |
| 377 | } |
| 378 | } |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 379 | CanonicalSuperT = CanonicalSuperT.getLocalUnqualifiedType(); |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 380 | // If the types are the same, move on to the next type in the subset. |
| 381 | if (CanonicalSubT == CanonicalSuperT) { |
| 382 | Contained = true; |
| 383 | break; |
| 384 | } |
| 385 | |
| 386 | // Otherwise we need to check the inheritance. |
| 387 | if (!SubIsClass || !CanonicalSuperT->isRecordType()) |
| 388 | continue; |
| 389 | |
| 390 | Paths.clear(); |
| 391 | if (!IsDerivedFrom(CanonicalSubT, CanonicalSuperT, Paths)) |
| 392 | continue; |
| 393 | |
| 394 | if (Paths.isAmbiguous(CanonicalSuperT)) |
| 395 | continue; |
| 396 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 397 | // Do this check from a context without privileges. |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 398 | switch (CheckBaseClassAccess(SourceLocation(), |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 399 | CanonicalSuperT, CanonicalSubT, |
| 400 | Paths.front(), |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 401 | /*Diagnostic*/ 0, |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 402 | /*ForceCheck*/ true, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 403 | /*ForceUnprivileged*/ true)) { |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 404 | case AR_accessible: break; |
| 405 | case AR_inaccessible: continue; |
| 406 | case AR_dependent: |
| 407 | llvm_unreachable("access check dependent for unprivileged context"); |
| 408 | break; |
| 409 | case AR_delayed: |
| 410 | llvm_unreachable("access check delayed in non-declaration"); |
| 411 | break; |
| 412 | } |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 413 | |
| 414 | Contained = true; |
| 415 | break; |
| 416 | } |
| 417 | if (!Contained) { |
| 418 | Diag(SubLoc, DiagID); |
Sebastian Redl | 37c38ec | 2009-10-14 16:09:29 +0000 | [diff] [blame] | 419 | if (NoteID.getDiagID() != 0) |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 420 | Diag(SuperLoc, NoteID); |
| 421 | return true; |
| 422 | } |
| 423 | } |
| 424 | // We've run half the gauntlet. |
| 425 | return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc); |
| 426 | } |
| 427 | |
| 428 | static bool CheckSpecForTypesEquivalent(Sema &S, |
Sebastian Redl | 37c38ec | 2009-10-14 16:09:29 +0000 | [diff] [blame] | 429 | const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID, |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 430 | QualType Target, SourceLocation TargetLoc, |
| 431 | QualType Source, SourceLocation SourceLoc) |
| 432 | { |
| 433 | const FunctionProtoType *TFunc = GetUnderlyingFunction(Target); |
| 434 | if (!TFunc) |
| 435 | return false; |
| 436 | const FunctionProtoType *SFunc = GetUnderlyingFunction(Source); |
| 437 | if (!SFunc) |
| 438 | return false; |
| 439 | |
| 440 | return S.CheckEquivalentExceptionSpec(DiagID, NoteID, TFunc, TargetLoc, |
| 441 | SFunc, SourceLoc); |
| 442 | } |
| 443 | |
| 444 | /// CheckParamExceptionSpec - Check if the parameter and return types of the |
| 445 | /// two functions have equivalent exception specs. This is part of the |
| 446 | /// assignment and override compatibility check. We do not check the parameters |
| 447 | /// of parameter function pointers recursively, as no sane programmer would |
| 448 | /// even be able to write such a function type. |
Sebastian Redl | 37c38ec | 2009-10-14 16:09:29 +0000 | [diff] [blame] | 449 | bool Sema::CheckParamExceptionSpec(const PartialDiagnostic & NoteID, |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 450 | const FunctionProtoType *Target, SourceLocation TargetLoc, |
| 451 | const FunctionProtoType *Source, SourceLocation SourceLoc) |
| 452 | { |
Sebastian Redl | 37c38ec | 2009-10-14 16:09:29 +0000 | [diff] [blame] | 453 | if (CheckSpecForTypesEquivalent(*this, |
| 454 | PDiag(diag::err_deep_exception_specs_differ) << 0, 0, |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 455 | Target->getResultType(), TargetLoc, |
| 456 | Source->getResultType(), SourceLoc)) |
| 457 | return true; |
| 458 | |
Sebastian Redl | 37c38ec | 2009-10-14 16:09:29 +0000 | [diff] [blame] | 459 | // We shouldn't even be testing this unless the arguments are otherwise |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 460 | // compatible. |
| 461 | assert(Target->getNumArgs() == Source->getNumArgs() && |
| 462 | "Functions have different argument counts."); |
| 463 | for (unsigned i = 0, E = Target->getNumArgs(); i != E; ++i) { |
Sebastian Redl | 37c38ec | 2009-10-14 16:09:29 +0000 | [diff] [blame] | 464 | if (CheckSpecForTypesEquivalent(*this, |
| 465 | PDiag(diag::err_deep_exception_specs_differ) << 1, 0, |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 466 | Target->getArgType(i), TargetLoc, |
| 467 | Source->getArgType(i), SourceLoc)) |
| 468 | return true; |
| 469 | } |
| 470 | return false; |
| 471 | } |
| 472 | |
| 473 | bool Sema::CheckExceptionSpecCompatibility(Expr *From, QualType ToType) |
| 474 | { |
| 475 | // First we check for applicability. |
| 476 | // Target type must be a function, function pointer or function reference. |
| 477 | const FunctionProtoType *ToFunc = GetUnderlyingFunction(ToType); |
| 478 | if (!ToFunc) |
| 479 | return false; |
| 480 | |
| 481 | // SourceType must be a function or function pointer. |
| 482 | const FunctionProtoType *FromFunc = GetUnderlyingFunction(From->getType()); |
| 483 | if (!FromFunc) |
| 484 | return false; |
| 485 | |
| 486 | // Now we've got the correct types on both sides, check their compatibility. |
| 487 | // This means that the source of the conversion can only throw a subset of |
| 488 | // the exceptions of the target, and any exception specs on arguments or |
| 489 | // return types must be equivalent. |
| 490 | return CheckExceptionSpecSubset(diag::err_incompatible_exception_specs, |
| 491 | 0, ToFunc, From->getSourceRange().getBegin(), |
| 492 | FromFunc, SourceLocation()); |
| 493 | } |
| 494 | |
| 495 | bool Sema::CheckOverridingFunctionExceptionSpec(const CXXMethodDecl *New, |
| 496 | const CXXMethodDecl *Old) { |
| 497 | return CheckExceptionSpecSubset(diag::err_override_exception_spec, |
| 498 | diag::note_overridden_virtual_function, |
| 499 | Old->getType()->getAs<FunctionProtoType>(), |
| 500 | Old->getLocation(), |
| 501 | New->getType()->getAs<FunctionProtoType>(), |
| 502 | New->getLocation()); |
| 503 | } |
| 504 | |
| 505 | } // end namespace clang |