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; |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 101 | if (!CheckEquivalentExceptionSpec(PDiag(diag::err_mismatched_exception_spec), |
| 102 | PDiag(diag::note_previous_declaration), |
Douglas Gregor | e13ad83 | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 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, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame^] | 137 | NewProto->getExtInfo()); |
Douglas Gregor | e13ad83 | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 138 | New->setType(NewType); |
| 139 | return false; |
| 140 | } |
| 141 | |
Douglas Gregor | 2eef829 | 2010-03-24 07:14:45 +0000 | [diff] [blame] | 142 | if (MissingExceptionSpecification && isa<FunctionProtoType>(New->getType())) { |
| 143 | const FunctionProtoType *NewProto |
| 144 | = cast<FunctionProtoType>(New->getType()); |
| 145 | const FunctionProtoType *OldProto |
| 146 | = Old->getType()->getAs<FunctionProtoType>(); |
| 147 | |
| 148 | // Update the type of the function with the appropriate exception |
| 149 | // specification. |
| 150 | QualType NewType = Context.getFunctionType(NewProto->getResultType(), |
| 151 | NewProto->arg_type_begin(), |
| 152 | NewProto->getNumArgs(), |
| 153 | NewProto->isVariadic(), |
| 154 | NewProto->getTypeQuals(), |
| 155 | OldProto->hasExceptionSpec(), |
| 156 | OldProto->hasAnyExceptionSpec(), |
| 157 | OldProto->getNumExceptions(), |
| 158 | OldProto->exception_begin(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame^] | 159 | NewProto->getExtInfo()); |
Douglas Gregor | 2eef829 | 2010-03-24 07:14:45 +0000 | [diff] [blame] | 160 | New->setType(NewType); |
| 161 | |
| 162 | // If exceptions are disabled, suppress the warning about missing |
| 163 | // exception specifications for new and delete operators. |
| 164 | if (!getLangOptions().Exceptions) { |
| 165 | switch (New->getDeclName().getCXXOverloadedOperator()) { |
| 166 | case OO_New: |
| 167 | case OO_Array_New: |
| 168 | case OO_Delete: |
| 169 | case OO_Array_Delete: |
| 170 | if (New->getDeclContext()->isTranslationUnit()) |
| 171 | return false; |
| 172 | break; |
| 173 | |
| 174 | default: |
| 175 | break; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | // Warn about the lack of exception specification. |
| 180 | llvm::SmallString<128> ExceptionSpecString; |
| 181 | llvm::raw_svector_ostream OS(ExceptionSpecString); |
| 182 | OS << "throw("; |
| 183 | bool OnFirstException = true; |
| 184 | for (FunctionProtoType::exception_iterator E = OldProto->exception_begin(), |
| 185 | EEnd = OldProto->exception_end(); |
| 186 | E != EEnd; |
| 187 | ++E) { |
| 188 | if (OnFirstException) |
| 189 | OnFirstException = false; |
| 190 | else |
| 191 | OS << ", "; |
| 192 | |
| 193 | OS << E->getAsString(Context.PrintingPolicy); |
| 194 | } |
| 195 | OS << ")"; |
| 196 | OS.flush(); |
| 197 | |
| 198 | SourceLocation AfterParenLoc; |
| 199 | if (TypeSourceInfo *TSInfo = New->getTypeSourceInfo()) { |
| 200 | TypeLoc TL = TSInfo->getTypeLoc(); |
| 201 | if (const FunctionTypeLoc *FTLoc = dyn_cast<FunctionTypeLoc>(&TL)) |
| 202 | AfterParenLoc = PP.getLocForEndOfToken(FTLoc->getRParenLoc()); |
| 203 | } |
| 204 | |
| 205 | if (AfterParenLoc.isInvalid()) |
| 206 | Diag(New->getLocation(), diag::warn_missing_exception_specification) |
| 207 | << New << OS.str(); |
| 208 | else { |
| 209 | // FIXME: This will get more complicated with C++0x |
| 210 | // late-specified return types. |
| 211 | Diag(New->getLocation(), diag::warn_missing_exception_specification) |
| 212 | << New << OS.str() |
| 213 | << CodeModificationHint::CreateInsertion(AfterParenLoc, |
| 214 | " " + OS.str().str()); |
| 215 | } |
| 216 | |
| 217 | if (!Old->getLocation().isInvalid()) |
| 218 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 219 | |
| 220 | return false; |
| 221 | } |
| 222 | |
Douglas Gregor | e13ad83 | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 223 | Diag(New->getLocation(), diag::err_mismatched_exception_spec); |
| 224 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 225 | return true; |
| 226 | } |
| 227 | |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 228 | /// CheckEquivalentExceptionSpec - Check if the two types have equivalent |
| 229 | /// exception specifications. Exception specifications are equivalent if |
| 230 | /// they allow exactly the same set of exception types. It does not matter how |
| 231 | /// that is achieved. See C++ [except.spec]p2. |
| 232 | bool Sema::CheckEquivalentExceptionSpec( |
| 233 | const FunctionProtoType *Old, SourceLocation OldLoc, |
| 234 | const FunctionProtoType *New, SourceLocation NewLoc) { |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 235 | return CheckEquivalentExceptionSpec( |
| 236 | PDiag(diag::err_mismatched_exception_spec), |
| 237 | PDiag(diag::note_previous_declaration), |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 238 | Old, OldLoc, New, NewLoc); |
| 239 | } |
| 240 | |
| 241 | /// CheckEquivalentExceptionSpec - Check if the two types have equivalent |
| 242 | /// exception specifications. Exception specifications are equivalent if |
| 243 | /// they allow exactly the same set of exception types. It does not matter how |
| 244 | /// that is achieved. See C++ [except.spec]p2. |
Douglas Gregor | 2eef829 | 2010-03-24 07:14:45 +0000 | [diff] [blame] | 245 | bool Sema::CheckEquivalentExceptionSpec(const PartialDiagnostic &DiagID, |
| 246 | const PartialDiagnostic & NoteID, |
| 247 | const FunctionProtoType *Old, |
| 248 | SourceLocation OldLoc, |
| 249 | const FunctionProtoType *New, |
| 250 | SourceLocation NewLoc, |
| 251 | bool *MissingExceptionSpecification, |
| 252 | bool *MissingEmptyExceptionSpecification) { |
| 253 | if (MissingExceptionSpecification) |
| 254 | *MissingExceptionSpecification = false; |
| 255 | |
Douglas Gregor | e13ad83 | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 256 | if (MissingEmptyExceptionSpecification) |
| 257 | *MissingEmptyExceptionSpecification = false; |
| 258 | |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 259 | bool OldAny = !Old->hasExceptionSpec() || Old->hasAnyExceptionSpec(); |
| 260 | bool NewAny = !New->hasExceptionSpec() || New->hasAnyExceptionSpec(); |
| 261 | if (OldAny && NewAny) |
| 262 | return false; |
| 263 | if (OldAny || NewAny) { |
Douglas Gregor | 2eef829 | 2010-03-24 07:14:45 +0000 | [diff] [blame] | 264 | if (MissingExceptionSpecification && Old->hasExceptionSpec() && |
Douglas Gregor | e13ad83 | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 265 | !New->hasExceptionSpec()) { |
Douglas Gregor | 2eef829 | 2010-03-24 07:14:45 +0000 | [diff] [blame] | 266 | // The old type has an exception specification of some sort, but |
| 267 | // the new type does not. |
| 268 | *MissingExceptionSpecification = true; |
| 269 | |
| 270 | if (MissingEmptyExceptionSpecification && |
| 271 | !Old->hasAnyExceptionSpec() && Old->getNumExceptions() == 0) { |
| 272 | // The old type has a throw() exception specification and the |
| 273 | // new type has no exception specification, and the caller asked |
| 274 | // to handle this itself. |
| 275 | *MissingEmptyExceptionSpecification = true; |
| 276 | } |
| 277 | |
Douglas Gregor | e13ad83 | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 278 | return true; |
| 279 | } |
| 280 | |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 281 | Diag(NewLoc, DiagID); |
Sebastian Redl | 37c38ec | 2009-10-14 16:09:29 +0000 | [diff] [blame] | 282 | if (NoteID.getDiagID() != 0) |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 283 | Diag(OldLoc, NoteID); |
| 284 | return true; |
| 285 | } |
| 286 | |
| 287 | bool Success = true; |
| 288 | // Both have a definite exception spec. Collect the first set, then compare |
| 289 | // to the second. |
Sebastian Redl | 1219d15 | 2009-10-14 15:06:25 +0000 | [diff] [blame] | 290 | llvm::SmallPtrSet<CanQualType, 8> OldTypes, NewTypes; |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 291 | for (FunctionProtoType::exception_iterator I = Old->exception_begin(), |
| 292 | E = Old->exception_end(); I != E; ++I) |
Sebastian Redl | 1219d15 | 2009-10-14 15:06:25 +0000 | [diff] [blame] | 293 | OldTypes.insert(Context.getCanonicalType(*I).getUnqualifiedType()); |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 294 | |
| 295 | for (FunctionProtoType::exception_iterator I = New->exception_begin(), |
Sebastian Redl | 5db4d90 | 2009-10-11 09:11:23 +0000 | [diff] [blame] | 296 | E = New->exception_end(); I != E && Success; ++I) { |
Sebastian Redl | 1219d15 | 2009-10-14 15:06:25 +0000 | [diff] [blame] | 297 | CanQualType TypePtr = Context.getCanonicalType(*I).getUnqualifiedType(); |
Sebastian Redl | 5db4d90 | 2009-10-11 09:11:23 +0000 | [diff] [blame] | 298 | if(OldTypes.count(TypePtr)) |
| 299 | NewTypes.insert(TypePtr); |
| 300 | else |
| 301 | Success = false; |
| 302 | } |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 303 | |
Sebastian Redl | 5db4d90 | 2009-10-11 09:11:23 +0000 | [diff] [blame] | 304 | Success = Success && OldTypes.size() == NewTypes.size(); |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 305 | |
| 306 | if (Success) { |
| 307 | return false; |
| 308 | } |
| 309 | Diag(NewLoc, DiagID); |
Sebastian Redl | 37c38ec | 2009-10-14 16:09:29 +0000 | [diff] [blame] | 310 | if (NoteID.getDiagID() != 0) |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 311 | Diag(OldLoc, NoteID); |
| 312 | return true; |
| 313 | } |
| 314 | |
| 315 | /// CheckExceptionSpecSubset - Check whether the second function type's |
| 316 | /// exception specification is a subset (or equivalent) of the first function |
| 317 | /// type. This is used by override and pointer assignment checks. |
Sebastian Redl | 37c38ec | 2009-10-14 16:09:29 +0000 | [diff] [blame] | 318 | bool Sema::CheckExceptionSpecSubset( |
| 319 | const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID, |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 320 | const FunctionProtoType *Superset, SourceLocation SuperLoc, |
| 321 | const FunctionProtoType *Subset, SourceLocation SubLoc) { |
| 322 | // FIXME: As usual, we could be more specific in our error messages, but |
| 323 | // that better waits until we've got types with source locations. |
| 324 | |
| 325 | if (!SubLoc.isValid()) |
| 326 | SubLoc = SuperLoc; |
| 327 | |
| 328 | // If superset contains everything, we're done. |
| 329 | if (!Superset->hasExceptionSpec() || Superset->hasAnyExceptionSpec()) |
| 330 | return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc); |
| 331 | |
| 332 | // It does not. If the subset contains everything, we've failed. |
| 333 | if (!Subset->hasExceptionSpec() || Subset->hasAnyExceptionSpec()) { |
| 334 | Diag(SubLoc, DiagID); |
Sebastian Redl | 37c38ec | 2009-10-14 16:09:29 +0000 | [diff] [blame] | 335 | if (NoteID.getDiagID() != 0) |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 336 | Diag(SuperLoc, NoteID); |
| 337 | return true; |
| 338 | } |
| 339 | |
| 340 | // Neither contains everything. Do a proper comparison. |
| 341 | for (FunctionProtoType::exception_iterator SubI = Subset->exception_begin(), |
| 342 | SubE = Subset->exception_end(); SubI != SubE; ++SubI) { |
| 343 | // Take one type from the subset. |
| 344 | QualType CanonicalSubT = Context.getCanonicalType(*SubI); |
Sebastian Redl | c3a3b7b | 2009-10-14 14:38:54 +0000 | [diff] [blame] | 345 | // Unwrap pointers and references so that we can do checks within a class |
| 346 | // hierarchy. Don't unwrap member pointers; they don't have hierarchy |
| 347 | // conversions on the pointee. |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 348 | bool SubIsPointer = false; |
| 349 | if (const ReferenceType *RefTy = CanonicalSubT->getAs<ReferenceType>()) |
| 350 | CanonicalSubT = RefTy->getPointeeType(); |
| 351 | if (const PointerType *PtrTy = CanonicalSubT->getAs<PointerType>()) { |
| 352 | CanonicalSubT = PtrTy->getPointeeType(); |
| 353 | SubIsPointer = true; |
| 354 | } |
| 355 | bool SubIsClass = CanonicalSubT->isRecordType(); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 356 | CanonicalSubT = CanonicalSubT.getLocalUnqualifiedType(); |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 357 | |
| 358 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 359 | /*DetectVirtual=*/false); |
| 360 | |
| 361 | bool Contained = false; |
| 362 | // Make sure it's in the superset. |
| 363 | for (FunctionProtoType::exception_iterator SuperI = |
| 364 | Superset->exception_begin(), SuperE = Superset->exception_end(); |
| 365 | SuperI != SuperE; ++SuperI) { |
| 366 | QualType CanonicalSuperT = Context.getCanonicalType(*SuperI); |
| 367 | // SubT must be SuperT or derived from it, or pointer or reference to |
| 368 | // such types. |
| 369 | if (const ReferenceType *RefTy = CanonicalSuperT->getAs<ReferenceType>()) |
| 370 | CanonicalSuperT = RefTy->getPointeeType(); |
| 371 | if (SubIsPointer) { |
| 372 | if (const PointerType *PtrTy = CanonicalSuperT->getAs<PointerType>()) |
| 373 | CanonicalSuperT = PtrTy->getPointeeType(); |
| 374 | else { |
| 375 | continue; |
| 376 | } |
| 377 | } |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 378 | CanonicalSuperT = CanonicalSuperT.getLocalUnqualifiedType(); |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 379 | // If the types are the same, move on to the next type in the subset. |
| 380 | if (CanonicalSubT == CanonicalSuperT) { |
| 381 | Contained = true; |
| 382 | break; |
| 383 | } |
| 384 | |
| 385 | // Otherwise we need to check the inheritance. |
| 386 | if (!SubIsClass || !CanonicalSuperT->isRecordType()) |
| 387 | continue; |
| 388 | |
| 389 | Paths.clear(); |
| 390 | if (!IsDerivedFrom(CanonicalSubT, CanonicalSuperT, Paths)) |
| 391 | continue; |
| 392 | |
| 393 | if (Paths.isAmbiguous(CanonicalSuperT)) |
| 394 | continue; |
| 395 | |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 396 | // Do this check from a context without privileges. |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 397 | switch (CheckBaseClassAccess(SourceLocation(), |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 398 | CanonicalSuperT, CanonicalSubT, |
| 399 | Paths.front(), |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 400 | /*Diagnostic*/ 0, |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 401 | /*ForceCheck*/ true, |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 402 | /*ForceUnprivileged*/ true)) { |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 403 | case AR_accessible: break; |
| 404 | case AR_inaccessible: continue; |
| 405 | case AR_dependent: |
| 406 | llvm_unreachable("access check dependent for unprivileged context"); |
| 407 | break; |
| 408 | case AR_delayed: |
| 409 | llvm_unreachable("access check delayed in non-declaration"); |
| 410 | break; |
| 411 | } |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 412 | |
| 413 | Contained = true; |
| 414 | break; |
| 415 | } |
| 416 | if (!Contained) { |
| 417 | Diag(SubLoc, DiagID); |
Sebastian Redl | 37c38ec | 2009-10-14 16:09:29 +0000 | [diff] [blame] | 418 | if (NoteID.getDiagID() != 0) |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 419 | Diag(SuperLoc, NoteID); |
| 420 | return true; |
| 421 | } |
| 422 | } |
| 423 | // We've run half the gauntlet. |
| 424 | return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc); |
| 425 | } |
| 426 | |
| 427 | static bool CheckSpecForTypesEquivalent(Sema &S, |
Sebastian Redl | 37c38ec | 2009-10-14 16:09:29 +0000 | [diff] [blame] | 428 | const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID, |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 429 | QualType Target, SourceLocation TargetLoc, |
| 430 | QualType Source, SourceLocation SourceLoc) |
| 431 | { |
| 432 | const FunctionProtoType *TFunc = GetUnderlyingFunction(Target); |
| 433 | if (!TFunc) |
| 434 | return false; |
| 435 | const FunctionProtoType *SFunc = GetUnderlyingFunction(Source); |
| 436 | if (!SFunc) |
| 437 | return false; |
| 438 | |
| 439 | return S.CheckEquivalentExceptionSpec(DiagID, NoteID, TFunc, TargetLoc, |
| 440 | SFunc, SourceLoc); |
| 441 | } |
| 442 | |
| 443 | /// CheckParamExceptionSpec - Check if the parameter and return types of the |
| 444 | /// two functions have equivalent exception specs. This is part of the |
| 445 | /// assignment and override compatibility check. We do not check the parameters |
| 446 | /// of parameter function pointers recursively, as no sane programmer would |
| 447 | /// even be able to write such a function type. |
Sebastian Redl | 37c38ec | 2009-10-14 16:09:29 +0000 | [diff] [blame] | 448 | bool Sema::CheckParamExceptionSpec(const PartialDiagnostic & NoteID, |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 449 | const FunctionProtoType *Target, SourceLocation TargetLoc, |
| 450 | const FunctionProtoType *Source, SourceLocation SourceLoc) |
| 451 | { |
Sebastian Redl | 37c38ec | 2009-10-14 16:09:29 +0000 | [diff] [blame] | 452 | if (CheckSpecForTypesEquivalent(*this, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 453 | PDiag(diag::err_deep_exception_specs_differ) << 0, |
| 454 | PDiag(), |
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, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 465 | PDiag(diag::err_deep_exception_specs_differ) << 1, |
| 466 | PDiag(), |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 467 | Target->getArgType(i), TargetLoc, |
| 468 | Source->getArgType(i), SourceLoc)) |
| 469 | return true; |
| 470 | } |
| 471 | return false; |
| 472 | } |
| 473 | |
| 474 | bool Sema::CheckExceptionSpecCompatibility(Expr *From, QualType ToType) |
| 475 | { |
| 476 | // First we check for applicability. |
| 477 | // Target type must be a function, function pointer or function reference. |
| 478 | const FunctionProtoType *ToFunc = GetUnderlyingFunction(ToType); |
| 479 | if (!ToFunc) |
| 480 | return false; |
| 481 | |
| 482 | // SourceType must be a function or function pointer. |
| 483 | const FunctionProtoType *FromFunc = GetUnderlyingFunction(From->getType()); |
| 484 | if (!FromFunc) |
| 485 | return false; |
| 486 | |
| 487 | // Now we've got the correct types on both sides, check their compatibility. |
| 488 | // This means that the source of the conversion can only throw a subset of |
| 489 | // the exceptions of the target, and any exception specs on arguments or |
| 490 | // return types must be equivalent. |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 491 | return CheckExceptionSpecSubset(PDiag(diag::err_incompatible_exception_specs), |
| 492 | PDiag(), ToFunc, |
| 493 | From->getSourceRange().getBegin(), |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 494 | FromFunc, SourceLocation()); |
| 495 | } |
| 496 | |
| 497 | bool Sema::CheckOverridingFunctionExceptionSpec(const CXXMethodDecl *New, |
| 498 | const CXXMethodDecl *Old) { |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 499 | return CheckExceptionSpecSubset(PDiag(diag::err_override_exception_spec), |
| 500 | PDiag(diag::note_overridden_virtual_function), |
Sebastian Redl | dced226 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 501 | Old->getType()->getAs<FunctionProtoType>(), |
| 502 | Old->getLocation(), |
| 503 | New->getType()->getAs<FunctionProtoType>(), |
| 504 | New->getLocation()); |
| 505 | } |
| 506 | |
| 507 | } // end namespace clang |