Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 1 | //===--- SemaExceptionSpec.cpp - C++ Exception Specifications ---*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides Sema routines for C++ exception specification testing. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Sebastian Redl | 4915e63 | 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 | d6bc5e6 | 2010-03-24 07:14:45 +0000 | [diff] [blame] | 18 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | f40863c | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 19 | #include "clang/Basic/Diagnostic.h" |
| 20 | #include "clang/Basic/SourceManager.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "clang/Lex/Preprocessor.h" |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallPtrSet.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallString.h" |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 24 | |
| 25 | namespace clang { |
| 26 | |
| 27 | static const FunctionProtoType *GetUnderlyingFunction(QualType T) |
| 28 | { |
| 29 | if (const PointerType *PtrTy = T->getAs<PointerType>()) |
| 30 | T = PtrTy->getPointeeType(); |
| 31 | else if (const ReferenceType *RefTy = T->getAs<ReferenceType>()) |
| 32 | T = RefTy->getPointeeType(); |
Sebastian Redl | 075b21d | 2009-10-14 14:38:54 +0000 | [diff] [blame] | 33 | else if (const MemberPointerType *MPTy = T->getAs<MemberPointerType>()) |
| 34 | T = MPTy->getPointeeType(); |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 35 | return T->getAs<FunctionProtoType>(); |
| 36 | } |
| 37 | |
| 38 | /// CheckSpecifiedExceptionType - Check if the given type is valid in an |
| 39 | /// exception specification. Incomplete types, or pointers to incomplete types |
| 40 | /// other than void are not allowed. |
Richard Smith | 8606d75 | 2012-11-28 22:33:28 +0000 | [diff] [blame] | 41 | /// |
| 42 | /// \param[in,out] T The exception type. This will be decayed to a pointer type |
| 43 | /// when the input is an array or a function type. |
| 44 | bool Sema::CheckSpecifiedExceptionType(QualType &T, const SourceRange &Range) { |
Richard Smith | a118c6a | 2012-11-28 22:52:42 +0000 | [diff] [blame] | 45 | // C++11 [except.spec]p2: |
| 46 | // A type cv T, "array of T", or "function returning T" denoted |
Richard Smith | 8606d75 | 2012-11-28 22:33:28 +0000 | [diff] [blame] | 47 | // in an exception-specification is adjusted to type T, "pointer to T", or |
| 48 | // "pointer to function returning T", respectively. |
Richard Smith | a118c6a | 2012-11-28 22:52:42 +0000 | [diff] [blame] | 49 | // |
| 50 | // We also apply this rule in C++98. |
Richard Smith | 8606d75 | 2012-11-28 22:33:28 +0000 | [diff] [blame] | 51 | if (T->isArrayType()) |
| 52 | T = Context.getArrayDecayedType(T); |
| 53 | else if (T->isFunctionType()) |
| 54 | T = Context.getPointerType(T); |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 55 | |
Richard Smith | a118c6a | 2012-11-28 22:52:42 +0000 | [diff] [blame] | 56 | int Kind = 0; |
Richard Smith | 8606d75 | 2012-11-28 22:33:28 +0000 | [diff] [blame] | 57 | QualType PointeeT = T; |
Richard Smith | a118c6a | 2012-11-28 22:52:42 +0000 | [diff] [blame] | 58 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 59 | PointeeT = PT->getPointeeType(); |
| 60 | Kind = 1; |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 61 | |
Richard Smith | a118c6a | 2012-11-28 22:52:42 +0000 | [diff] [blame] | 62 | // cv void* is explicitly permitted, despite being a pointer to an |
| 63 | // incomplete type. |
| 64 | if (PointeeT->isVoidType()) |
| 65 | return false; |
| 66 | } else if (const ReferenceType *RT = T->getAs<ReferenceType>()) { |
| 67 | PointeeT = RT->getPointeeType(); |
| 68 | Kind = 2; |
Richard Smith | 8606d75 | 2012-11-28 22:33:28 +0000 | [diff] [blame] | 69 | |
Richard Smith | a118c6a | 2012-11-28 22:52:42 +0000 | [diff] [blame] | 70 | if (RT->isRValueReferenceType()) { |
| 71 | // C++11 [except.spec]p2: |
| 72 | // A type denoted in an exception-specification shall not denote [...] |
| 73 | // an rvalue reference type. |
| 74 | Diag(Range.getBegin(), diag::err_rref_in_exception_spec) |
| 75 | << T << Range; |
| 76 | return true; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // C++11 [except.spec]p2: |
| 81 | // A type denoted in an exception-specification shall not denote an |
| 82 | // incomplete type other than a class currently being defined [...]. |
| 83 | // A type denoted in an exception-specification shall not denote a |
| 84 | // pointer or reference to an incomplete type, other than (cv) void* or a |
| 85 | // pointer or reference to a class currently being defined. |
| 86 | if (!(PointeeT->isRecordType() && |
| 87 | PointeeT->getAs<RecordType>()->isBeingDefined()) && |
Richard Smith | 8606d75 | 2012-11-28 22:33:28 +0000 | [diff] [blame] | 88 | RequireCompleteType(Range.getBegin(), PointeeT, |
Richard Smith | a118c6a | 2012-11-28 22:52:42 +0000 | [diff] [blame] | 89 | diag::err_incomplete_in_exception_spec, Kind, Range)) |
Sebastian Redl | 7eb5d37 | 2009-10-14 14:59:48 +0000 | [diff] [blame] | 90 | return true; |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 91 | |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | /// CheckDistantExceptionSpec - Check if the given type is a pointer or pointer |
| 96 | /// to member to a function with an exception specification. This means that |
| 97 | /// it is invalid to add another level of indirection. |
| 98 | bool Sema::CheckDistantExceptionSpec(QualType T) { |
| 99 | if (const PointerType *PT = T->getAs<PointerType>()) |
| 100 | T = PT->getPointeeType(); |
| 101 | else if (const MemberPointerType *PT = T->getAs<MemberPointerType>()) |
| 102 | T = PT->getPointeeType(); |
| 103 | else |
| 104 | return false; |
| 105 | |
| 106 | const FunctionProtoType *FnT = T->getAs<FunctionProtoType>(); |
| 107 | if (!FnT) |
| 108 | return false; |
| 109 | |
| 110 | return FnT->hasExceptionSpec(); |
| 111 | } |
| 112 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 113 | const FunctionProtoType * |
| 114 | Sema::ResolveExceptionSpec(SourceLocation Loc, const FunctionProtoType *FPT) { |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 115 | if (!isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 116 | return FPT; |
| 117 | |
| 118 | FunctionDecl *SourceDecl = FPT->getExceptionSpecDecl(); |
| 119 | const FunctionProtoType *SourceFPT = |
| 120 | SourceDecl->getType()->castAs<FunctionProtoType>(); |
| 121 | |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 122 | // If the exception specification has already been resolved, just return it. |
| 123 | if (!isUnresolvedExceptionSpec(SourceFPT->getExceptionSpecType())) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 124 | return SourceFPT; |
| 125 | |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 126 | // Compute or instantiate the exception specification now. |
Richard Smith | 3901dfe | 2013-03-27 00:22:47 +0000 | [diff] [blame] | 127 | if (SourceFPT->getExceptionSpecType() == EST_Unevaluated) |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 128 | EvaluateImplicitExceptionSpec(Loc, cast<CXXMethodDecl>(SourceDecl)); |
| 129 | else |
| 130 | InstantiateExceptionSpec(Loc, SourceDecl); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 131 | |
| 132 | return SourceDecl->getType()->castAs<FunctionProtoType>(); |
| 133 | } |
| 134 | |
Richard Smith | 66f3ac9 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 135 | /// Determine whether a function has an implicitly-generated exception |
Richard Smith | 1ee6352 | 2012-10-16 23:30:16 +0000 | [diff] [blame] | 136 | /// specification. |
Richard Smith | 66f3ac9 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 137 | static bool hasImplicitExceptionSpec(FunctionDecl *Decl) { |
| 138 | if (!isa<CXXDestructorDecl>(Decl) && |
| 139 | Decl->getDeclName().getCXXOverloadedOperator() != OO_Delete && |
| 140 | Decl->getDeclName().getCXXOverloadedOperator() != OO_Array_Delete) |
| 141 | return false; |
Richard Smith | 1ee6352 | 2012-10-16 23:30:16 +0000 | [diff] [blame] | 142 | |
Richard Smith | 66f3ac9 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 143 | // If the user didn't declare the function, its exception specification must |
| 144 | // be implicit. |
| 145 | if (!Decl->getTypeSourceInfo()) |
| 146 | return true; |
| 147 | |
| 148 | const FunctionProtoType *Ty = |
| 149 | Decl->getTypeSourceInfo()->getType()->getAs<FunctionProtoType>(); |
| 150 | return !Ty->hasExceptionSpec(); |
Richard Smith | 1ee6352 | 2012-10-16 23:30:16 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Douglas Gregor | f40863c | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 153 | bool Sema::CheckEquivalentExceptionSpec(FunctionDecl *Old, FunctionDecl *New) { |
Sebastian Redl | cb5dd00 | 2011-03-15 19:52:30 +0000 | [diff] [blame] | 154 | OverloadedOperatorKind OO = New->getDeclName().getCXXOverloadedOperator(); |
| 155 | bool IsOperatorNew = OO == OO_New || OO == OO_Array_New; |
Douglas Gregor | d6bc5e6 | 2010-03-24 07:14:45 +0000 | [diff] [blame] | 156 | bool MissingExceptionSpecification = false; |
Douglas Gregor | f40863c | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 157 | bool MissingEmptyExceptionSpecification = false; |
Francois Pichet | 13b4e68 | 2011-03-19 23:05:18 +0000 | [diff] [blame] | 158 | unsigned DiagID = diag::err_mismatched_exception_spec; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 159 | if (getLangOpts().MicrosoftExt) |
Francois Pichet | 9392165 | 2011-04-22 08:25:24 +0000 | [diff] [blame] | 160 | DiagID = diag::warn_mismatched_exception_spec; |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 161 | |
Richard Smith | 1ee6352 | 2012-10-16 23:30:16 +0000 | [diff] [blame] | 162 | // Check the types as written: they must match before any exception |
| 163 | // specification adjustment is applied. |
| 164 | if (!CheckEquivalentExceptionSpec( |
| 165 | PDiag(DiagID), PDiag(diag::note_previous_declaration), |
Richard Smith | 66f3ac9 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 166 | Old->getType()->getAs<FunctionProtoType>(), Old->getLocation(), |
| 167 | New->getType()->getAs<FunctionProtoType>(), New->getLocation(), |
Richard Smith | 1ee6352 | 2012-10-16 23:30:16 +0000 | [diff] [blame] | 168 | &MissingExceptionSpecification, &MissingEmptyExceptionSpecification, |
Richard Smith | 66f3ac9 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 169 | /*AllowNoexceptAllMatchWithNoSpec=*/true, IsOperatorNew)) { |
| 170 | // C++11 [except.spec]p4 [DR1492]: |
| 171 | // If a declaration of a function has an implicit |
| 172 | // exception-specification, other declarations of the function shall |
| 173 | // not specify an exception-specification. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 174 | if (getLangOpts().CPlusPlus11 && |
Richard Smith | 66f3ac9 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 175 | hasImplicitExceptionSpec(Old) != hasImplicitExceptionSpec(New)) { |
| 176 | Diag(New->getLocation(), diag::ext_implicit_exception_spec_mismatch) |
| 177 | << hasImplicitExceptionSpec(Old); |
| 178 | if (!Old->getLocation().isInvalid()) |
| 179 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 180 | } |
Douglas Gregor | f40863c | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 181 | return false; |
Richard Smith | 66f3ac9 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 182 | } |
Douglas Gregor | f40863c | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 183 | |
Eli Friedman | 96dbf6f | 2013-06-25 00:46:32 +0000 | [diff] [blame] | 184 | // The failure was something other than an missing exception |
Douglas Gregor | f40863c | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 185 | // specification; return an error. |
Eli Friedman | 96dbf6f | 2013-06-25 00:46:32 +0000 | [diff] [blame] | 186 | if (!MissingExceptionSpecification) |
Douglas Gregor | f40863c | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 187 | return true; |
| 188 | |
Richard Smith | 66f3ac9 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 189 | const FunctionProtoType *NewProto = |
Eli Friedman | 96dbf6f | 2013-06-25 00:46:32 +0000 | [diff] [blame] | 190 | New->getType()->castAs<FunctionProtoType>(); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 191 | |
Douglas Gregor | f40863c | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 192 | // The new function declaration is only missing an empty exception |
| 193 | // specification "throw()". If the throw() specification came from a |
| 194 | // function in a system header that has C linkage, just add an empty |
| 195 | // exception specification to the "new" declaration. This is an |
| 196 | // egregious workaround for glibc, which adds throw() specifications |
| 197 | // to many libc functions as an optimization. Unfortunately, that |
| 198 | // optimization isn't permitted by the C++ standard, so we're forced |
| 199 | // to work around it here. |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 200 | if (MissingEmptyExceptionSpecification && NewProto && |
Douglas Gregor | d6bc5e6 | 2010-03-24 07:14:45 +0000 | [diff] [blame] | 201 | (Old->getLocation().isInvalid() || |
| 202 | Context.getSourceManager().isInSystemHeader(Old->getLocation())) && |
Douglas Gregor | f40863c | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 203 | Old->isExternC()) { |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 204 | FunctionProtoType::ExtProtoInfo EPI = NewProto->getExtProtoInfo(); |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 205 | EPI.ExceptionSpecType = EST_DynamicNone; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame^] | 206 | QualType NewType = Context.getFunctionType(NewProto->getReturnType(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 207 | NewProto->getParamTypes(), EPI); |
Douglas Gregor | f40863c | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 208 | New->setType(NewType); |
| 209 | return false; |
| 210 | } |
| 211 | |
Eli Friedman | 96dbf6f | 2013-06-25 00:46:32 +0000 | [diff] [blame] | 212 | const FunctionProtoType *OldProto = |
| 213 | Old->getType()->castAs<FunctionProtoType>(); |
Douglas Gregor | d6bc5e6 | 2010-03-24 07:14:45 +0000 | [diff] [blame] | 214 | |
Eli Friedman | 96dbf6f | 2013-06-25 00:46:32 +0000 | [diff] [blame] | 215 | FunctionProtoType::ExtProtoInfo EPI = NewProto->getExtProtoInfo(); |
| 216 | EPI.ExceptionSpecType = OldProto->getExceptionSpecType(); |
| 217 | if (EPI.ExceptionSpecType == EST_Dynamic) { |
| 218 | EPI.NumExceptions = OldProto->getNumExceptions(); |
| 219 | EPI.Exceptions = OldProto->exception_begin(); |
| 220 | } else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) { |
| 221 | // FIXME: We can't just take the expression from the old prototype. It |
| 222 | // likely contains references to the old prototype's parameters. |
Douglas Gregor | d6bc5e6 | 2010-03-24 07:14:45 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Eli Friedman | 96dbf6f | 2013-06-25 00:46:32 +0000 | [diff] [blame] | 225 | // Update the type of the function with the appropriate exception |
| 226 | // specification. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame^] | 227 | QualType NewType = Context.getFunctionType(NewProto->getReturnType(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 228 | NewProto->getParamTypes(), EPI); |
Eli Friedman | 96dbf6f | 2013-06-25 00:46:32 +0000 | [diff] [blame] | 229 | New->setType(NewType); |
| 230 | |
| 231 | // Warn about the lack of exception specification. |
| 232 | SmallString<128> ExceptionSpecString; |
| 233 | llvm::raw_svector_ostream OS(ExceptionSpecString); |
| 234 | switch (OldProto->getExceptionSpecType()) { |
| 235 | case EST_DynamicNone: |
| 236 | OS << "throw()"; |
| 237 | break; |
| 238 | |
| 239 | case EST_Dynamic: { |
| 240 | OS << "throw("; |
| 241 | bool OnFirstException = true; |
| 242 | for (FunctionProtoType::exception_iterator E = OldProto->exception_begin(), |
| 243 | EEnd = OldProto->exception_end(); |
| 244 | E != EEnd; |
| 245 | ++E) { |
| 246 | if (OnFirstException) |
| 247 | OnFirstException = false; |
| 248 | else |
| 249 | OS << ", "; |
| 250 | |
| 251 | OS << E->getAsString(getPrintingPolicy()); |
| 252 | } |
| 253 | OS << ")"; |
| 254 | break; |
| 255 | } |
| 256 | |
| 257 | case EST_BasicNoexcept: |
| 258 | OS << "noexcept"; |
| 259 | break; |
| 260 | |
| 261 | case EST_ComputedNoexcept: |
| 262 | OS << "noexcept("; |
| 263 | OldProto->getNoexceptExpr()->printPretty(OS, 0, getPrintingPolicy()); |
| 264 | OS << ")"; |
| 265 | break; |
| 266 | |
| 267 | default: |
| 268 | llvm_unreachable("This spec type is compatible with none."); |
| 269 | } |
| 270 | OS.flush(); |
| 271 | |
| 272 | SourceLocation FixItLoc; |
| 273 | if (TypeSourceInfo *TSInfo = New->getTypeSourceInfo()) { |
| 274 | TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens(); |
| 275 | if (FunctionTypeLoc FTLoc = TL.getAs<FunctionTypeLoc>()) |
| 276 | FixItLoc = PP.getLocForEndOfToken(FTLoc.getLocalRangeEnd()); |
| 277 | } |
| 278 | |
| 279 | if (FixItLoc.isInvalid()) |
| 280 | Diag(New->getLocation(), diag::warn_missing_exception_specification) |
| 281 | << New << OS.str(); |
| 282 | else { |
| 283 | // FIXME: This will get more complicated with C++0x |
| 284 | // late-specified return types. |
| 285 | Diag(New->getLocation(), diag::warn_missing_exception_specification) |
| 286 | << New << OS.str() |
| 287 | << FixItHint::CreateInsertion(FixItLoc, " " + OS.str().str()); |
| 288 | } |
| 289 | |
| 290 | if (!Old->getLocation().isInvalid()) |
| 291 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 292 | |
| 293 | return false; |
Douglas Gregor | f40863c | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 296 | /// CheckEquivalentExceptionSpec - Check if the two types have equivalent |
| 297 | /// exception specifications. Exception specifications are equivalent if |
| 298 | /// they allow exactly the same set of exception types. It does not matter how |
| 299 | /// that is achieved. See C++ [except.spec]p2. |
| 300 | bool Sema::CheckEquivalentExceptionSpec( |
| 301 | const FunctionProtoType *Old, SourceLocation OldLoc, |
| 302 | const FunctionProtoType *New, SourceLocation NewLoc) { |
Francois Pichet | 13b4e68 | 2011-03-19 23:05:18 +0000 | [diff] [blame] | 303 | unsigned DiagID = diag::err_mismatched_exception_spec; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 304 | if (getLangOpts().MicrosoftExt) |
Francois Pichet | 9392165 | 2011-04-22 08:25:24 +0000 | [diff] [blame] | 305 | DiagID = diag::warn_mismatched_exception_spec; |
Richard Smith | 1ee6352 | 2012-10-16 23:30:16 +0000 | [diff] [blame] | 306 | return CheckEquivalentExceptionSpec(PDiag(DiagID), |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 307 | PDiag(diag::note_previous_declaration), |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 308 | Old, OldLoc, New, NewLoc); |
| 309 | } |
| 310 | |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 311 | /// CheckEquivalentExceptionSpec - Check if the two types have compatible |
| 312 | /// exception specifications. See C++ [except.spec]p3. |
Richard Smith | 66f3ac9 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 313 | /// |
| 314 | /// \return \c false if the exception specifications match, \c true if there is |
| 315 | /// a problem. If \c true is returned, either a diagnostic has already been |
| 316 | /// produced or \c *MissingExceptionSpecification is set to \c true. |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 317 | bool Sema::CheckEquivalentExceptionSpec(const PartialDiagnostic &DiagID, |
Douglas Gregor | d6bc5e6 | 2010-03-24 07:14:45 +0000 | [diff] [blame] | 318 | const PartialDiagnostic & NoteID, |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 319 | const FunctionProtoType *Old, |
Douglas Gregor | d6bc5e6 | 2010-03-24 07:14:45 +0000 | [diff] [blame] | 320 | SourceLocation OldLoc, |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 321 | const FunctionProtoType *New, |
Douglas Gregor | d6bc5e6 | 2010-03-24 07:14:45 +0000 | [diff] [blame] | 322 | SourceLocation NewLoc, |
| 323 | bool *MissingExceptionSpecification, |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 324 | bool*MissingEmptyExceptionSpecification, |
Sebastian Redl | cb5dd00 | 2011-03-15 19:52:30 +0000 | [diff] [blame] | 325 | bool AllowNoexceptAllMatchWithNoSpec, |
| 326 | bool IsOperatorNew) { |
John McCall | f9c9409 | 2010-05-28 08:37:35 +0000 | [diff] [blame] | 327 | // Just completely ignore this under -fno-exceptions. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 328 | if (!getLangOpts().CXXExceptions) |
John McCall | f9c9409 | 2010-05-28 08:37:35 +0000 | [diff] [blame] | 329 | return false; |
| 330 | |
Douglas Gregor | d6bc5e6 | 2010-03-24 07:14:45 +0000 | [diff] [blame] | 331 | if (MissingExceptionSpecification) |
| 332 | *MissingExceptionSpecification = false; |
| 333 | |
Douglas Gregor | f40863c | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 334 | if (MissingEmptyExceptionSpecification) |
| 335 | *MissingEmptyExceptionSpecification = false; |
| 336 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 337 | Old = ResolveExceptionSpec(NewLoc, Old); |
| 338 | if (!Old) |
| 339 | return false; |
| 340 | New = ResolveExceptionSpec(NewLoc, New); |
| 341 | if (!New) |
| 342 | return false; |
| 343 | |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 344 | // C++0x [except.spec]p3: Two exception-specifications are compatible if: |
| 345 | // - both are non-throwing, regardless of their form, |
| 346 | // - both have the form noexcept(constant-expression) and the constant- |
| 347 | // expressions are equivalent, |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 348 | // - both are dynamic-exception-specifications that have the same set of |
| 349 | // adjusted types. |
| 350 | // |
| 351 | // C++0x [except.spec]p12: An exception-specifcation is non-throwing if it is |
| 352 | // of the form throw(), noexcept, or noexcept(constant-expression) where the |
| 353 | // constant-expression yields true. |
| 354 | // |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 355 | // C++0x [except.spec]p4: If any declaration of a function has an exception- |
| 356 | // specifier that is not a noexcept-specification allowing all exceptions, |
| 357 | // all declarations [...] of that function shall have a compatible |
| 358 | // exception-specification. |
| 359 | // |
| 360 | // That last point basically means that noexcept(false) matches no spec. |
| 361 | // It's considered when AllowNoexceptAllMatchWithNoSpec is true. |
| 362 | |
| 363 | ExceptionSpecificationType OldEST = Old->getExceptionSpecType(); |
| 364 | ExceptionSpecificationType NewEST = New->getExceptionSpecType(); |
| 365 | |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 366 | assert(!isUnresolvedExceptionSpec(OldEST) && |
| 367 | !isUnresolvedExceptionSpec(NewEST) && |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 368 | "Shouldn't see unknown exception specifications here"); |
| 369 | |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 370 | // Shortcut the case where both have no spec. |
| 371 | if (OldEST == EST_None && NewEST == EST_None) |
| 372 | return false; |
| 373 | |
Sebastian Redl | 31ad754 | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 374 | FunctionProtoType::NoexceptResult OldNR = Old->getNoexceptSpec(Context); |
| 375 | FunctionProtoType::NoexceptResult NewNR = New->getNoexceptSpec(Context); |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 376 | if (OldNR == FunctionProtoType::NR_BadNoexcept || |
| 377 | NewNR == FunctionProtoType::NR_BadNoexcept) |
| 378 | return false; |
| 379 | |
| 380 | // Dependent noexcept specifiers are compatible with each other, but nothing |
| 381 | // else. |
| 382 | // One noexcept is compatible with another if the argument is the same |
| 383 | if (OldNR == NewNR && |
| 384 | OldNR != FunctionProtoType::NR_NoNoexcept && |
| 385 | NewNR != FunctionProtoType::NR_NoNoexcept) |
| 386 | return false; |
| 387 | if (OldNR != NewNR && |
| 388 | OldNR != FunctionProtoType::NR_NoNoexcept && |
| 389 | NewNR != FunctionProtoType::NR_NoNoexcept) { |
| 390 | Diag(NewLoc, DiagID); |
| 391 | if (NoteID.getDiagID() != 0) |
| 392 | Diag(OldLoc, NoteID); |
| 393 | return true; |
Douglas Gregor | f62c529 | 2010-08-30 15:04:51 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 396 | // The MS extension throw(...) is compatible with itself. |
| 397 | if (OldEST == EST_MSAny && NewEST == EST_MSAny) |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 398 | return false; |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 399 | |
| 400 | // It's also compatible with no spec. |
| 401 | if ((OldEST == EST_None && NewEST == EST_MSAny) || |
| 402 | (OldEST == EST_MSAny && NewEST == EST_None)) |
| 403 | return false; |
| 404 | |
| 405 | // It's also compatible with noexcept(false). |
| 406 | if (OldEST == EST_MSAny && NewNR == FunctionProtoType::NR_Throw) |
| 407 | return false; |
| 408 | if (NewEST == EST_MSAny && OldNR == FunctionProtoType::NR_Throw) |
| 409 | return false; |
| 410 | |
| 411 | // As described above, noexcept(false) matches no spec only for functions. |
| 412 | if (AllowNoexceptAllMatchWithNoSpec) { |
| 413 | if (OldEST == EST_None && NewNR == FunctionProtoType::NR_Throw) |
| 414 | return false; |
| 415 | if (NewEST == EST_None && OldNR == FunctionProtoType::NR_Throw) |
| 416 | return false; |
| 417 | } |
| 418 | |
| 419 | // Any non-throwing specifications are compatible. |
| 420 | bool OldNonThrowing = OldNR == FunctionProtoType::NR_Nothrow || |
| 421 | OldEST == EST_DynamicNone; |
| 422 | bool NewNonThrowing = NewNR == FunctionProtoType::NR_Nothrow || |
| 423 | NewEST == EST_DynamicNone; |
| 424 | if (OldNonThrowing && NewNonThrowing) |
| 425 | return false; |
| 426 | |
Sebastian Redl | cb5dd00 | 2011-03-15 19:52:30 +0000 | [diff] [blame] | 427 | // As a special compatibility feature, under C++0x we accept no spec and |
| 428 | // throw(std::bad_alloc) as equivalent for operator new and operator new[]. |
| 429 | // This is because the implicit declaration changed, but old code would break. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 430 | if (getLangOpts().CPlusPlus11 && IsOperatorNew) { |
Sebastian Redl | cb5dd00 | 2011-03-15 19:52:30 +0000 | [diff] [blame] | 431 | const FunctionProtoType *WithExceptions = 0; |
| 432 | if (OldEST == EST_None && NewEST == EST_Dynamic) |
| 433 | WithExceptions = New; |
| 434 | else if (OldEST == EST_Dynamic && NewEST == EST_None) |
| 435 | WithExceptions = Old; |
| 436 | if (WithExceptions && WithExceptions->getNumExceptions() == 1) { |
| 437 | // One has no spec, the other throw(something). If that something is |
| 438 | // std::bad_alloc, all conditions are met. |
| 439 | QualType Exception = *WithExceptions->exception_begin(); |
| 440 | if (CXXRecordDecl *ExRecord = Exception->getAsCXXRecordDecl()) { |
| 441 | IdentifierInfo* Name = ExRecord->getIdentifier(); |
| 442 | if (Name && Name->getName() == "bad_alloc") { |
| 443 | // It's called bad_alloc, but is it in std? |
| 444 | DeclContext* DC = ExRecord->getDeclContext(); |
Sebastian Redl | c34c29f | 2011-03-15 20:41:09 +0000 | [diff] [blame] | 445 | DC = DC->getEnclosingNamespaceContext(); |
| 446 | if (NamespaceDecl* NS = dyn_cast<NamespaceDecl>(DC)) { |
Sebastian Redl | cb5dd00 | 2011-03-15 19:52:30 +0000 | [diff] [blame] | 447 | IdentifierInfo* NSName = NS->getIdentifier(); |
Sebastian Redl | c34c29f | 2011-03-15 20:41:09 +0000 | [diff] [blame] | 448 | DC = DC->getParent(); |
Sebastian Redl | cb5dd00 | 2011-03-15 19:52:30 +0000 | [diff] [blame] | 449 | if (NSName && NSName->getName() == "std" && |
Sebastian Redl | c34c29f | 2011-03-15 20:41:09 +0000 | [diff] [blame] | 450 | DC->getEnclosingNamespaceContext()->isTranslationUnit()) { |
Sebastian Redl | cb5dd00 | 2011-03-15 19:52:30 +0000 | [diff] [blame] | 451 | return false; |
Sebastian Redl | c34c29f | 2011-03-15 20:41:09 +0000 | [diff] [blame] | 452 | } |
Sebastian Redl | cb5dd00 | 2011-03-15 19:52:30 +0000 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 459 | // At this point, the only remaining valid case is two matching dynamic |
| 460 | // specifications. We return here unless both specifications are dynamic. |
| 461 | if (OldEST != EST_Dynamic || NewEST != EST_Dynamic) { |
Douglas Gregor | d6bc5e6 | 2010-03-24 07:14:45 +0000 | [diff] [blame] | 462 | if (MissingExceptionSpecification && Old->hasExceptionSpec() && |
Douglas Gregor | f40863c | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 463 | !New->hasExceptionSpec()) { |
Douglas Gregor | d6bc5e6 | 2010-03-24 07:14:45 +0000 | [diff] [blame] | 464 | // The old type has an exception specification of some sort, but |
| 465 | // the new type does not. |
| 466 | *MissingExceptionSpecification = true; |
| 467 | |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 468 | if (MissingEmptyExceptionSpecification && OldNonThrowing) { |
| 469 | // The old type has a throw() or noexcept(true) exception specification |
| 470 | // and the new type has no exception specification, and the caller asked |
Douglas Gregor | d6bc5e6 | 2010-03-24 07:14:45 +0000 | [diff] [blame] | 471 | // to handle this itself. |
| 472 | *MissingEmptyExceptionSpecification = true; |
| 473 | } |
| 474 | |
Douglas Gregor | f40863c | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 475 | return true; |
| 476 | } |
| 477 | |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 478 | Diag(NewLoc, DiagID); |
Sebastian Redl | a44822f | 2009-10-14 16:09:29 +0000 | [diff] [blame] | 479 | if (NoteID.getDiagID() != 0) |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 480 | Diag(OldLoc, NoteID); |
| 481 | return true; |
| 482 | } |
| 483 | |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 484 | assert(OldEST == EST_Dynamic && NewEST == EST_Dynamic && |
| 485 | "Exception compatibility logic error: non-dynamic spec slipped through."); |
| 486 | |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 487 | bool Success = true; |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 488 | // Both have a dynamic exception spec. Collect the first set, then compare |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 489 | // to the second. |
Sebastian Redl | 184edca | 2009-10-14 15:06:25 +0000 | [diff] [blame] | 490 | llvm::SmallPtrSet<CanQualType, 8> OldTypes, NewTypes; |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 491 | for (FunctionProtoType::exception_iterator I = Old->exception_begin(), |
| 492 | E = Old->exception_end(); I != E; ++I) |
Sebastian Redl | 184edca | 2009-10-14 15:06:25 +0000 | [diff] [blame] | 493 | OldTypes.insert(Context.getCanonicalType(*I).getUnqualifiedType()); |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 494 | |
| 495 | for (FunctionProtoType::exception_iterator I = New->exception_begin(), |
Sebastian Redl | 6e4c871 | 2009-10-11 09:11:23 +0000 | [diff] [blame] | 496 | E = New->exception_end(); I != E && Success; ++I) { |
Sebastian Redl | 184edca | 2009-10-14 15:06:25 +0000 | [diff] [blame] | 497 | CanQualType TypePtr = Context.getCanonicalType(*I).getUnqualifiedType(); |
Sebastian Redl | 6e4c871 | 2009-10-11 09:11:23 +0000 | [diff] [blame] | 498 | if(OldTypes.count(TypePtr)) |
| 499 | NewTypes.insert(TypePtr); |
| 500 | else |
| 501 | Success = false; |
| 502 | } |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 503 | |
Sebastian Redl | 6e4c871 | 2009-10-11 09:11:23 +0000 | [diff] [blame] | 504 | Success = Success && OldTypes.size() == NewTypes.size(); |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 505 | |
| 506 | if (Success) { |
| 507 | return false; |
| 508 | } |
| 509 | Diag(NewLoc, DiagID); |
Sebastian Redl | a44822f | 2009-10-14 16:09:29 +0000 | [diff] [blame] | 510 | if (NoteID.getDiagID() != 0) |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 511 | Diag(OldLoc, NoteID); |
| 512 | return true; |
| 513 | } |
| 514 | |
| 515 | /// CheckExceptionSpecSubset - Check whether the second function type's |
| 516 | /// exception specification is a subset (or equivalent) of the first function |
| 517 | /// type. This is used by override and pointer assignment checks. |
Sebastian Redl | a44822f | 2009-10-14 16:09:29 +0000 | [diff] [blame] | 518 | bool Sema::CheckExceptionSpecSubset( |
| 519 | const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID, |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 520 | const FunctionProtoType *Superset, SourceLocation SuperLoc, |
| 521 | const FunctionProtoType *Subset, SourceLocation SubLoc) { |
John McCall | f9c9409 | 2010-05-28 08:37:35 +0000 | [diff] [blame] | 522 | |
| 523 | // Just auto-succeed under -fno-exceptions. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 524 | if (!getLangOpts().CXXExceptions) |
John McCall | f9c9409 | 2010-05-28 08:37:35 +0000 | [diff] [blame] | 525 | return false; |
| 526 | |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 527 | // FIXME: As usual, we could be more specific in our error messages, but |
| 528 | // that better waits until we've got types with source locations. |
| 529 | |
| 530 | if (!SubLoc.isValid()) |
| 531 | SubLoc = SuperLoc; |
| 532 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 533 | // Resolve the exception specifications, if needed. |
| 534 | Superset = ResolveExceptionSpec(SuperLoc, Superset); |
| 535 | if (!Superset) |
| 536 | return false; |
| 537 | Subset = ResolveExceptionSpec(SubLoc, Subset); |
| 538 | if (!Subset) |
| 539 | return false; |
| 540 | |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 541 | ExceptionSpecificationType SuperEST = Superset->getExceptionSpecType(); |
| 542 | |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 543 | // If superset contains everything, we're done. |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 544 | if (SuperEST == EST_None || SuperEST == EST_MSAny) |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 545 | return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc); |
| 546 | |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 547 | // If there are dependent noexcept specs, assume everything is fine. Unlike |
| 548 | // with the equivalency check, this is safe in this case, because we don't |
| 549 | // want to merge declarations. Checks after instantiation will catch any |
| 550 | // omissions we make here. |
| 551 | // We also shortcut checking if a noexcept expression was bad. |
| 552 | |
Sebastian Redl | 31ad754 | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 553 | FunctionProtoType::NoexceptResult SuperNR =Superset->getNoexceptSpec(Context); |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 554 | if (SuperNR == FunctionProtoType::NR_BadNoexcept || |
| 555 | SuperNR == FunctionProtoType::NR_Dependent) |
| 556 | return false; |
| 557 | |
| 558 | // Another case of the superset containing everything. |
| 559 | if (SuperNR == FunctionProtoType::NR_Throw) |
| 560 | return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc); |
| 561 | |
| 562 | ExceptionSpecificationType SubEST = Subset->getExceptionSpecType(); |
| 563 | |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 564 | assert(!isUnresolvedExceptionSpec(SuperEST) && |
| 565 | !isUnresolvedExceptionSpec(SubEST) && |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 566 | "Shouldn't see unknown exception specifications here"); |
| 567 | |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 568 | // It does not. If the subset contains everything, we've failed. |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 569 | if (SubEST == EST_None || SubEST == EST_MSAny) { |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 570 | Diag(SubLoc, DiagID); |
Sebastian Redl | a44822f | 2009-10-14 16:09:29 +0000 | [diff] [blame] | 571 | if (NoteID.getDiagID() != 0) |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 572 | Diag(SuperLoc, NoteID); |
| 573 | return true; |
| 574 | } |
| 575 | |
Sebastian Redl | 31ad754 | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 576 | FunctionProtoType::NoexceptResult SubNR = Subset->getNoexceptSpec(Context); |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 577 | if (SubNR == FunctionProtoType::NR_BadNoexcept || |
| 578 | SubNR == FunctionProtoType::NR_Dependent) |
| 579 | return false; |
| 580 | |
| 581 | // Another case of the subset containing everything. |
| 582 | if (SubNR == FunctionProtoType::NR_Throw) { |
| 583 | Diag(SubLoc, DiagID); |
| 584 | if (NoteID.getDiagID() != 0) |
| 585 | Diag(SuperLoc, NoteID); |
| 586 | return true; |
| 587 | } |
| 588 | |
| 589 | // If the subset contains nothing, we're done. |
| 590 | if (SubEST == EST_DynamicNone || SubNR == FunctionProtoType::NR_Nothrow) |
| 591 | return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc); |
| 592 | |
| 593 | // Otherwise, if the superset contains nothing, we've failed. |
| 594 | if (SuperEST == EST_DynamicNone || SuperNR == FunctionProtoType::NR_Nothrow) { |
| 595 | Diag(SubLoc, DiagID); |
| 596 | if (NoteID.getDiagID() != 0) |
| 597 | Diag(SuperLoc, NoteID); |
| 598 | return true; |
| 599 | } |
| 600 | |
| 601 | assert(SuperEST == EST_Dynamic && SubEST == EST_Dynamic && |
| 602 | "Exception spec subset: non-dynamic case slipped through."); |
| 603 | |
| 604 | // Neither contains everything or nothing. Do a proper comparison. |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 605 | for (FunctionProtoType::exception_iterator SubI = Subset->exception_begin(), |
| 606 | SubE = Subset->exception_end(); SubI != SubE; ++SubI) { |
| 607 | // Take one type from the subset. |
| 608 | QualType CanonicalSubT = Context.getCanonicalType(*SubI); |
Sebastian Redl | 075b21d | 2009-10-14 14:38:54 +0000 | [diff] [blame] | 609 | // Unwrap pointers and references so that we can do checks within a class |
| 610 | // hierarchy. Don't unwrap member pointers; they don't have hierarchy |
| 611 | // conversions on the pointee. |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 612 | bool SubIsPointer = false; |
| 613 | if (const ReferenceType *RefTy = CanonicalSubT->getAs<ReferenceType>()) |
| 614 | CanonicalSubT = RefTy->getPointeeType(); |
| 615 | if (const PointerType *PtrTy = CanonicalSubT->getAs<PointerType>()) { |
| 616 | CanonicalSubT = PtrTy->getPointeeType(); |
| 617 | SubIsPointer = true; |
| 618 | } |
| 619 | bool SubIsClass = CanonicalSubT->isRecordType(); |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 620 | CanonicalSubT = CanonicalSubT.getLocalUnqualifiedType(); |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 621 | |
| 622 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 623 | /*DetectVirtual=*/false); |
| 624 | |
| 625 | bool Contained = false; |
| 626 | // Make sure it's in the superset. |
| 627 | for (FunctionProtoType::exception_iterator SuperI = |
| 628 | Superset->exception_begin(), SuperE = Superset->exception_end(); |
| 629 | SuperI != SuperE; ++SuperI) { |
| 630 | QualType CanonicalSuperT = Context.getCanonicalType(*SuperI); |
| 631 | // SubT must be SuperT or derived from it, or pointer or reference to |
| 632 | // such types. |
| 633 | if (const ReferenceType *RefTy = CanonicalSuperT->getAs<ReferenceType>()) |
| 634 | CanonicalSuperT = RefTy->getPointeeType(); |
| 635 | if (SubIsPointer) { |
| 636 | if (const PointerType *PtrTy = CanonicalSuperT->getAs<PointerType>()) |
| 637 | CanonicalSuperT = PtrTy->getPointeeType(); |
| 638 | else { |
| 639 | continue; |
| 640 | } |
| 641 | } |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 642 | CanonicalSuperT = CanonicalSuperT.getLocalUnqualifiedType(); |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 643 | // If the types are the same, move on to the next type in the subset. |
| 644 | if (CanonicalSubT == CanonicalSuperT) { |
| 645 | Contained = true; |
| 646 | break; |
| 647 | } |
| 648 | |
| 649 | // Otherwise we need to check the inheritance. |
| 650 | if (!SubIsClass || !CanonicalSuperT->isRecordType()) |
| 651 | continue; |
| 652 | |
| 653 | Paths.clear(); |
| 654 | if (!IsDerivedFrom(CanonicalSubT, CanonicalSuperT, Paths)) |
| 655 | continue; |
| 656 | |
Douglas Gregor | 27ac429 | 2010-05-21 20:29:55 +0000 | [diff] [blame] | 657 | if (Paths.isAmbiguous(Context.getCanonicalType(CanonicalSuperT))) |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 658 | continue; |
| 659 | |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 660 | // Do this check from a context without privileges. |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 661 | switch (CheckBaseClassAccess(SourceLocation(), |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 662 | CanonicalSuperT, CanonicalSubT, |
| 663 | Paths.front(), |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 664 | /*Diagnostic*/ 0, |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 665 | /*ForceCheck*/ true, |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 666 | /*ForceUnprivileged*/ true)) { |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 667 | case AR_accessible: break; |
| 668 | case AR_inaccessible: continue; |
| 669 | case AR_dependent: |
| 670 | llvm_unreachable("access check dependent for unprivileged context"); |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 671 | case AR_delayed: |
| 672 | llvm_unreachable("access check delayed in non-declaration"); |
John McCall | 5b0829a | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 673 | } |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 674 | |
| 675 | Contained = true; |
| 676 | break; |
| 677 | } |
| 678 | if (!Contained) { |
| 679 | Diag(SubLoc, DiagID); |
Sebastian Redl | a44822f | 2009-10-14 16:09:29 +0000 | [diff] [blame] | 680 | if (NoteID.getDiagID() != 0) |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 681 | Diag(SuperLoc, NoteID); |
| 682 | return true; |
| 683 | } |
| 684 | } |
| 685 | // We've run half the gauntlet. |
| 686 | return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc); |
| 687 | } |
| 688 | |
| 689 | static bool CheckSpecForTypesEquivalent(Sema &S, |
Sebastian Redl | a44822f | 2009-10-14 16:09:29 +0000 | [diff] [blame] | 690 | const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID, |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 691 | QualType Target, SourceLocation TargetLoc, |
| 692 | QualType Source, SourceLocation SourceLoc) |
| 693 | { |
| 694 | const FunctionProtoType *TFunc = GetUnderlyingFunction(Target); |
| 695 | if (!TFunc) |
| 696 | return false; |
| 697 | const FunctionProtoType *SFunc = GetUnderlyingFunction(Source); |
| 698 | if (!SFunc) |
| 699 | return false; |
| 700 | |
| 701 | return S.CheckEquivalentExceptionSpec(DiagID, NoteID, TFunc, TargetLoc, |
| 702 | SFunc, SourceLoc); |
| 703 | } |
| 704 | |
| 705 | /// CheckParamExceptionSpec - Check if the parameter and return types of the |
| 706 | /// two functions have equivalent exception specs. This is part of the |
| 707 | /// assignment and override compatibility check. We do not check the parameters |
| 708 | /// of parameter function pointers recursively, as no sane programmer would |
| 709 | /// even be able to write such a function type. |
Sebastian Redl | a44822f | 2009-10-14 16:09:29 +0000 | [diff] [blame] | 710 | bool Sema::CheckParamExceptionSpec(const PartialDiagnostic & NoteID, |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 711 | const FunctionProtoType *Target, SourceLocation TargetLoc, |
| 712 | const FunctionProtoType *Source, SourceLocation SourceLoc) |
| 713 | { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame^] | 714 | if (CheckSpecForTypesEquivalent( |
| 715 | *this, PDiag(diag::err_deep_exception_specs_differ) << 0, PDiag(), |
| 716 | Target->getReturnType(), TargetLoc, Source->getReturnType(), |
| 717 | SourceLoc)) |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 718 | return true; |
| 719 | |
Sebastian Redl | a44822f | 2009-10-14 16:09:29 +0000 | [diff] [blame] | 720 | // We shouldn't even be testing this unless the arguments are otherwise |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 721 | // compatible. |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 722 | assert(Target->getNumParams() == Source->getNumParams() && |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 723 | "Functions have different argument counts."); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 724 | for (unsigned i = 0, E = Target->getNumParams(); i != E; ++i) { |
| 725 | if (CheckSpecForTypesEquivalent( |
| 726 | *this, PDiag(diag::err_deep_exception_specs_differ) << 1, PDiag(), |
| 727 | Target->getParamType(i), TargetLoc, Source->getParamType(i), |
| 728 | SourceLoc)) |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 729 | return true; |
| 730 | } |
| 731 | return false; |
| 732 | } |
| 733 | |
| 734 | bool Sema::CheckExceptionSpecCompatibility(Expr *From, QualType ToType) |
| 735 | { |
| 736 | // First we check for applicability. |
| 737 | // Target type must be a function, function pointer or function reference. |
| 738 | const FunctionProtoType *ToFunc = GetUnderlyingFunction(ToType); |
| 739 | if (!ToFunc) |
| 740 | return false; |
| 741 | |
| 742 | // SourceType must be a function or function pointer. |
| 743 | const FunctionProtoType *FromFunc = GetUnderlyingFunction(From->getType()); |
| 744 | if (!FromFunc) |
| 745 | return false; |
| 746 | |
| 747 | // Now we've got the correct types on both sides, check their compatibility. |
| 748 | // This means that the source of the conversion can only throw a subset of |
| 749 | // the exceptions of the target, and any exception specs on arguments or |
| 750 | // return types must be equivalent. |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 751 | return CheckExceptionSpecSubset(PDiag(diag::err_incompatible_exception_specs), |
| 752 | PDiag(), ToFunc, |
| 753 | From->getSourceRange().getBegin(), |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 754 | FromFunc, SourceLocation()); |
| 755 | } |
| 756 | |
| 757 | bool Sema::CheckOverridingFunctionExceptionSpec(const CXXMethodDecl *New, |
| 758 | const CXXMethodDecl *Old) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 759 | if (getLangOpts().CPlusPlus11 && isa<CXXDestructorDecl>(New)) { |
Sebastian Redl | 645d958 | 2011-05-20 05:57:18 +0000 | [diff] [blame] | 760 | // Don't check uninstantiated template destructors at all. We can only |
| 761 | // synthesize correct specs after the template is instantiated. |
| 762 | if (New->getParent()->isDependentType()) |
| 763 | return false; |
| 764 | if (New->getParent()->isBeingDefined()) { |
| 765 | // The destructor might be updated once the definition is finished. So |
| 766 | // remember it and check later. |
| 767 | DelayedDestructorExceptionSpecChecks.push_back(std::make_pair( |
| 768 | cast<CXXDestructorDecl>(New), cast<CXXDestructorDecl>(Old))); |
| 769 | return false; |
| 770 | } |
Sebastian Redl | 623ea82 | 2011-05-19 05:13:44 +0000 | [diff] [blame] | 771 | } |
Francois Pichet | a8032e9 | 2011-05-24 02:11:43 +0000 | [diff] [blame] | 772 | unsigned DiagID = diag::err_override_exception_spec; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 773 | if (getLangOpts().MicrosoftExt) |
Francois Pichet | a8032e9 | 2011-05-24 02:11:43 +0000 | [diff] [blame] | 774 | DiagID = diag::warn_override_exception_spec; |
| 775 | return CheckExceptionSpecSubset(PDiag(DiagID), |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 776 | PDiag(diag::note_overridden_virtual_function), |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 777 | Old->getType()->getAs<FunctionProtoType>(), |
| 778 | Old->getLocation(), |
| 779 | New->getType()->getAs<FunctionProtoType>(), |
| 780 | New->getLocation()); |
| 781 | } |
| 782 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 783 | static CanThrowResult canSubExprsThrow(Sema &S, const Expr *CE) { |
| 784 | Expr *E = const_cast<Expr*>(CE); |
| 785 | CanThrowResult R = CT_Cannot; |
| 786 | for (Expr::child_range I = E->children(); I && R != CT_Can; ++I) |
| 787 | R = mergeCanThrow(R, S.canThrow(cast<Expr>(*I))); |
| 788 | return R; |
| 789 | } |
| 790 | |
Eli Friedman | 0423b76 | 2013-06-25 01:24:22 +0000 | [diff] [blame] | 791 | static CanThrowResult canCalleeThrow(Sema &S, const Expr *E, const Decl *D) { |
| 792 | assert(D && "Expected decl"); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 793 | |
| 794 | // See if we can get a function type from the decl somehow. |
| 795 | const ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 796 | if (!VD) // If we have no clue what we're calling, assume the worst. |
| 797 | return CT_Can; |
| 798 | |
| 799 | // As an extension, we assume that __attribute__((nothrow)) functions don't |
| 800 | // throw. |
| 801 | if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>()) |
| 802 | return CT_Cannot; |
| 803 | |
| 804 | QualType T = VD->getType(); |
| 805 | const FunctionProtoType *FT; |
| 806 | if ((FT = T->getAs<FunctionProtoType>())) { |
| 807 | } else if (const PointerType *PT = T->getAs<PointerType>()) |
| 808 | FT = PT->getPointeeType()->getAs<FunctionProtoType>(); |
| 809 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 810 | FT = RT->getPointeeType()->getAs<FunctionProtoType>(); |
| 811 | else if (const MemberPointerType *MT = T->getAs<MemberPointerType>()) |
| 812 | FT = MT->getPointeeType()->getAs<FunctionProtoType>(); |
| 813 | else if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) |
| 814 | FT = BT->getPointeeType()->getAs<FunctionProtoType>(); |
| 815 | |
| 816 | if (!FT) |
| 817 | return CT_Can; |
| 818 | |
| 819 | FT = S.ResolveExceptionSpec(E->getLocStart(), FT); |
| 820 | if (!FT) |
| 821 | return CT_Can; |
| 822 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 823 | return FT->isNothrow(S.Context) ? CT_Cannot : CT_Can; |
| 824 | } |
| 825 | |
| 826 | static CanThrowResult canDynamicCastThrow(const CXXDynamicCastExpr *DC) { |
| 827 | if (DC->isTypeDependent()) |
| 828 | return CT_Dependent; |
| 829 | |
| 830 | if (!DC->getTypeAsWritten()->isReferenceType()) |
| 831 | return CT_Cannot; |
| 832 | |
| 833 | if (DC->getSubExpr()->isTypeDependent()) |
| 834 | return CT_Dependent; |
| 835 | |
| 836 | return DC->getCastKind() == clang::CK_Dynamic? CT_Can : CT_Cannot; |
| 837 | } |
| 838 | |
| 839 | static CanThrowResult canTypeidThrow(Sema &S, const CXXTypeidExpr *DC) { |
| 840 | if (DC->isTypeOperand()) |
| 841 | return CT_Cannot; |
| 842 | |
| 843 | Expr *Op = DC->getExprOperand(); |
| 844 | if (Op->isTypeDependent()) |
| 845 | return CT_Dependent; |
| 846 | |
| 847 | const RecordType *RT = Op->getType()->getAs<RecordType>(); |
| 848 | if (!RT) |
| 849 | return CT_Cannot; |
| 850 | |
| 851 | if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic()) |
| 852 | return CT_Cannot; |
| 853 | |
| 854 | if (Op->Classify(S.Context).isPRValue()) |
| 855 | return CT_Cannot; |
| 856 | |
| 857 | return CT_Can; |
| 858 | } |
| 859 | |
| 860 | CanThrowResult Sema::canThrow(const Expr *E) { |
| 861 | // C++ [expr.unary.noexcept]p3: |
| 862 | // [Can throw] if in a potentially-evaluated context the expression would |
| 863 | // contain: |
| 864 | switch (E->getStmtClass()) { |
| 865 | case Expr::CXXThrowExprClass: |
| 866 | // - a potentially evaluated throw-expression |
| 867 | return CT_Can; |
| 868 | |
| 869 | case Expr::CXXDynamicCastExprClass: { |
| 870 | // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v), |
| 871 | // where T is a reference type, that requires a run-time check |
| 872 | CanThrowResult CT = canDynamicCastThrow(cast<CXXDynamicCastExpr>(E)); |
| 873 | if (CT == CT_Can) |
| 874 | return CT; |
| 875 | return mergeCanThrow(CT, canSubExprsThrow(*this, E)); |
| 876 | } |
| 877 | |
| 878 | case Expr::CXXTypeidExprClass: |
| 879 | // - a potentially evaluated typeid expression applied to a glvalue |
| 880 | // expression whose type is a polymorphic class type |
| 881 | return canTypeidThrow(*this, cast<CXXTypeidExpr>(E)); |
| 882 | |
| 883 | // - a potentially evaluated call to a function, member function, function |
| 884 | // pointer, or member function pointer that does not have a non-throwing |
| 885 | // exception-specification |
| 886 | case Expr::CallExprClass: |
| 887 | case Expr::CXXMemberCallExprClass: |
| 888 | case Expr::CXXOperatorCallExprClass: |
| 889 | case Expr::UserDefinedLiteralClass: { |
| 890 | const CallExpr *CE = cast<CallExpr>(E); |
| 891 | CanThrowResult CT; |
| 892 | if (E->isTypeDependent()) |
| 893 | CT = CT_Dependent; |
| 894 | else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens())) |
| 895 | CT = CT_Cannot; |
Eli Friedman | 5a8738f | 2013-06-25 01:55:41 +0000 | [diff] [blame] | 896 | else if (CE->getCalleeDecl()) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 897 | CT = canCalleeThrow(*this, E, CE->getCalleeDecl()); |
Eli Friedman | 5a8738f | 2013-06-25 01:55:41 +0000 | [diff] [blame] | 898 | else |
| 899 | CT = CT_Can; |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 900 | if (CT == CT_Can) |
| 901 | return CT; |
| 902 | return mergeCanThrow(CT, canSubExprsThrow(*this, E)); |
| 903 | } |
| 904 | |
| 905 | case Expr::CXXConstructExprClass: |
| 906 | case Expr::CXXTemporaryObjectExprClass: { |
| 907 | CanThrowResult CT = canCalleeThrow(*this, E, |
| 908 | cast<CXXConstructExpr>(E)->getConstructor()); |
| 909 | if (CT == CT_Can) |
| 910 | return CT; |
| 911 | return mergeCanThrow(CT, canSubExprsThrow(*this, E)); |
| 912 | } |
| 913 | |
| 914 | case Expr::LambdaExprClass: { |
| 915 | const LambdaExpr *Lambda = cast<LambdaExpr>(E); |
| 916 | CanThrowResult CT = CT_Cannot; |
| 917 | for (LambdaExpr::capture_init_iterator Cap = Lambda->capture_init_begin(), |
| 918 | CapEnd = Lambda->capture_init_end(); |
| 919 | Cap != CapEnd; ++Cap) |
| 920 | CT = mergeCanThrow(CT, canThrow(*Cap)); |
| 921 | return CT; |
| 922 | } |
| 923 | |
| 924 | case Expr::CXXNewExprClass: { |
| 925 | CanThrowResult CT; |
| 926 | if (E->isTypeDependent()) |
| 927 | CT = CT_Dependent; |
| 928 | else |
| 929 | CT = canCalleeThrow(*this, E, cast<CXXNewExpr>(E)->getOperatorNew()); |
| 930 | if (CT == CT_Can) |
| 931 | return CT; |
| 932 | return mergeCanThrow(CT, canSubExprsThrow(*this, E)); |
| 933 | } |
| 934 | |
| 935 | case Expr::CXXDeleteExprClass: { |
| 936 | CanThrowResult CT; |
| 937 | QualType DTy = cast<CXXDeleteExpr>(E)->getDestroyedType(); |
| 938 | if (DTy.isNull() || DTy->isDependentType()) { |
| 939 | CT = CT_Dependent; |
| 940 | } else { |
| 941 | CT = canCalleeThrow(*this, E, |
| 942 | cast<CXXDeleteExpr>(E)->getOperatorDelete()); |
| 943 | if (const RecordType *RT = DTy->getAs<RecordType>()) { |
| 944 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Eli Friedman | 0423b76 | 2013-06-25 01:24:22 +0000 | [diff] [blame] | 945 | const CXXDestructorDecl *DD = RD->getDestructor(); |
| 946 | if (DD) |
| 947 | CT = mergeCanThrow(CT, canCalleeThrow(*this, E, DD)); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 948 | } |
| 949 | if (CT == CT_Can) |
| 950 | return CT; |
| 951 | } |
| 952 | return mergeCanThrow(CT, canSubExprsThrow(*this, E)); |
| 953 | } |
| 954 | |
| 955 | case Expr::CXXBindTemporaryExprClass: { |
| 956 | // The bound temporary has to be destroyed again, which might throw. |
| 957 | CanThrowResult CT = canCalleeThrow(*this, E, |
| 958 | cast<CXXBindTemporaryExpr>(E)->getTemporary()->getDestructor()); |
| 959 | if (CT == CT_Can) |
| 960 | return CT; |
| 961 | return mergeCanThrow(CT, canSubExprsThrow(*this, E)); |
| 962 | } |
| 963 | |
| 964 | // ObjC message sends are like function calls, but never have exception |
| 965 | // specs. |
| 966 | case Expr::ObjCMessageExprClass: |
| 967 | case Expr::ObjCPropertyRefExprClass: |
| 968 | case Expr::ObjCSubscriptRefExprClass: |
| 969 | return CT_Can; |
| 970 | |
| 971 | // All the ObjC literals that are implemented as calls are |
| 972 | // potentially throwing unless we decide to close off that |
| 973 | // possibility. |
| 974 | case Expr::ObjCArrayLiteralClass: |
| 975 | case Expr::ObjCDictionaryLiteralClass: |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 976 | case Expr::ObjCBoxedExprClass: |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 977 | return CT_Can; |
| 978 | |
| 979 | // Many other things have subexpressions, so we have to test those. |
| 980 | // Some are simple: |
| 981 | case Expr::ConditionalOperatorClass: |
| 982 | case Expr::CompoundLiteralExprClass: |
| 983 | case Expr::CXXConstCastExprClass: |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 984 | case Expr::CXXReinterpretCastExprClass: |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 985 | case Expr::CXXStdInitializerListExprClass: |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 986 | case Expr::DesignatedInitExprClass: |
| 987 | case Expr::ExprWithCleanupsClass: |
| 988 | case Expr::ExtVectorElementExprClass: |
| 989 | case Expr::InitListExprClass: |
| 990 | case Expr::MemberExprClass: |
| 991 | case Expr::ObjCIsaExprClass: |
| 992 | case Expr::ObjCIvarRefExprClass: |
| 993 | case Expr::ParenExprClass: |
| 994 | case Expr::ParenListExprClass: |
| 995 | case Expr::ShuffleVectorExprClass: |
Hal Finkel | c4d7c82 | 2013-09-18 03:29:45 +0000 | [diff] [blame] | 996 | case Expr::ConvertVectorExprClass: |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 997 | case Expr::VAArgExprClass: |
| 998 | return canSubExprsThrow(*this, E); |
| 999 | |
| 1000 | // Some might be dependent for other reasons. |
| 1001 | case Expr::ArraySubscriptExprClass: |
| 1002 | case Expr::BinaryOperatorClass: |
| 1003 | case Expr::CompoundAssignOperatorClass: |
| 1004 | case Expr::CStyleCastExprClass: |
| 1005 | case Expr::CXXStaticCastExprClass: |
| 1006 | case Expr::CXXFunctionalCastExprClass: |
| 1007 | case Expr::ImplicitCastExprClass: |
| 1008 | case Expr::MaterializeTemporaryExprClass: |
| 1009 | case Expr::UnaryOperatorClass: { |
| 1010 | CanThrowResult CT = E->isTypeDependent() ? CT_Dependent : CT_Cannot; |
| 1011 | return mergeCanThrow(CT, canSubExprsThrow(*this, E)); |
| 1012 | } |
| 1013 | |
| 1014 | // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms. |
| 1015 | case Expr::StmtExprClass: |
| 1016 | return CT_Can; |
| 1017 | |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1018 | case Expr::CXXDefaultArgExprClass: |
| 1019 | return canThrow(cast<CXXDefaultArgExpr>(E)->getExpr()); |
| 1020 | |
| 1021 | case Expr::CXXDefaultInitExprClass: |
| 1022 | return canThrow(cast<CXXDefaultInitExpr>(E)->getExpr()); |
| 1023 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1024 | case Expr::ChooseExprClass: |
| 1025 | if (E->isTypeDependent() || E->isValueDependent()) |
| 1026 | return CT_Dependent; |
Eli Friedman | 75807f2 | 2013-07-20 00:40:58 +0000 | [diff] [blame] | 1027 | return canThrow(cast<ChooseExpr>(E)->getChosenSubExpr()); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1028 | |
| 1029 | case Expr::GenericSelectionExprClass: |
| 1030 | if (cast<GenericSelectionExpr>(E)->isResultDependent()) |
| 1031 | return CT_Dependent; |
| 1032 | return canThrow(cast<GenericSelectionExpr>(E)->getResultExpr()); |
| 1033 | |
| 1034 | // Some expressions are always dependent. |
| 1035 | case Expr::CXXDependentScopeMemberExprClass: |
| 1036 | case Expr::CXXUnresolvedConstructExprClass: |
| 1037 | case Expr::DependentScopeDeclRefExprClass: |
| 1038 | return CT_Dependent; |
| 1039 | |
| 1040 | case Expr::AsTypeExprClass: |
| 1041 | case Expr::BinaryConditionalOperatorClass: |
| 1042 | case Expr::BlockExprClass: |
| 1043 | case Expr::CUDAKernelCallExprClass: |
| 1044 | case Expr::DeclRefExprClass: |
| 1045 | case Expr::ObjCBridgedCastExprClass: |
| 1046 | case Expr::ObjCIndirectCopyRestoreExprClass: |
| 1047 | case Expr::ObjCProtocolExprClass: |
| 1048 | case Expr::ObjCSelectorExprClass: |
| 1049 | case Expr::OffsetOfExprClass: |
| 1050 | case Expr::PackExpansionExprClass: |
| 1051 | case Expr::PseudoObjectExprClass: |
| 1052 | case Expr::SubstNonTypeTemplateParmExprClass: |
| 1053 | case Expr::SubstNonTypeTemplateParmPackExprClass: |
Richard Smith | b15fe3a | 2012-09-12 00:56:43 +0000 | [diff] [blame] | 1054 | case Expr::FunctionParmPackExprClass: |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1055 | case Expr::UnaryExprOrTypeTraitExprClass: |
| 1056 | case Expr::UnresolvedLookupExprClass: |
| 1057 | case Expr::UnresolvedMemberExprClass: |
| 1058 | // FIXME: Can any of the above throw? If so, when? |
| 1059 | return CT_Cannot; |
| 1060 | |
| 1061 | case Expr::AddrLabelExprClass: |
| 1062 | case Expr::ArrayTypeTraitExprClass: |
| 1063 | case Expr::AtomicExprClass: |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1064 | case Expr::TypeTraitExprClass: |
| 1065 | case Expr::CXXBoolLiteralExprClass: |
| 1066 | case Expr::CXXNoexceptExprClass: |
| 1067 | case Expr::CXXNullPtrLiteralExprClass: |
| 1068 | case Expr::CXXPseudoDestructorExprClass: |
| 1069 | case Expr::CXXScalarValueInitExprClass: |
| 1070 | case Expr::CXXThisExprClass: |
| 1071 | case Expr::CXXUuidofExprClass: |
| 1072 | case Expr::CharacterLiteralClass: |
| 1073 | case Expr::ExpressionTraitExprClass: |
| 1074 | case Expr::FloatingLiteralClass: |
| 1075 | case Expr::GNUNullExprClass: |
| 1076 | case Expr::ImaginaryLiteralClass: |
| 1077 | case Expr::ImplicitValueInitExprClass: |
| 1078 | case Expr::IntegerLiteralClass: |
| 1079 | case Expr::ObjCEncodeExprClass: |
| 1080 | case Expr::ObjCStringLiteralClass: |
| 1081 | case Expr::ObjCBoolLiteralExprClass: |
| 1082 | case Expr::OpaqueValueExprClass: |
| 1083 | case Expr::PredefinedExprClass: |
| 1084 | case Expr::SizeOfPackExprClass: |
| 1085 | case Expr::StringLiteralClass: |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1086 | // These expressions can never throw. |
| 1087 | return CT_Cannot; |
| 1088 | |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 1089 | case Expr::MSPropertyRefExprClass: |
| 1090 | llvm_unreachable("Invalid class for expression"); |
| 1091 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1092 | #define STMT(CLASS, PARENT) case Expr::CLASS##Class: |
| 1093 | #define STMT_RANGE(Base, First, Last) |
| 1094 | #define LAST_STMT_RANGE(BASE, FIRST, LAST) |
| 1095 | #define EXPR(CLASS, PARENT) |
| 1096 | #define ABSTRACT_STMT(STMT) |
| 1097 | #include "clang/AST/StmtNodes.inc" |
| 1098 | case Expr::NoStmtClass: |
| 1099 | llvm_unreachable("Invalid class for expression"); |
| 1100 | } |
| 1101 | llvm_unreachable("Bogus StmtClass"); |
| 1102 | } |
| 1103 | |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 1104 | } // end namespace clang |