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