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