Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 1 | //===--- SemaExceptionSpec.cpp - C++ Exception Specifications ---*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file provides Sema routines for C++ exception specification testing. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 13 | #include "clang/Sema/SemaInternal.h" |
Richard Smith | 564417a | 2014-03-20 21:47:22 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTMutationListener.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" |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 18 | #include "clang/AST/StmtObjC.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 |
Richard Smith | 6289546 | 2016-10-19 23:47:37 +0000 | [diff] [blame] | 46 | // templates declared directly within namespace std or std::__debug or |
| 47 | // std::__profile. |
| 48 | if (!RD || !RD->getIdentifier() || !RD->getDescribedClassTemplate() || |
Richard Smith | 6403e93 | 2014-11-14 00:37:55 +0000 | [diff] [blame] | 49 | !D.getIdentifier() || !D.getIdentifier()->isStr("swap")) |
| 50 | return false; |
| 51 | |
Richard Smith | 6289546 | 2016-10-19 23:47:37 +0000 | [diff] [blame] | 52 | auto *ND = dyn_cast<NamespaceDecl>(RD->getDeclContext()); |
| 53 | if (!ND) |
| 54 | return false; |
| 55 | |
| 56 | bool IsInStd = ND->isStdNamespace(); |
| 57 | if (!IsInStd) { |
| 58 | // This isn't a direct member of namespace std, but it might still be |
| 59 | // libstdc++'s std::__debug::array or std::__profile::array. |
| 60 | IdentifierInfo *II = ND->getIdentifier(); |
| 61 | if (!II || !(II->isStr("__debug") || II->isStr("__profile")) || |
| 62 | !ND->isInStdNamespace()) |
| 63 | return false; |
| 64 | } |
| 65 | |
Richard Smith | 6403e93 | 2014-11-14 00:37:55 +0000 | [diff] [blame] | 66 | // Only apply this hack within a system header. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 67 | if (!Context.getSourceManager().isInSystemHeader(D.getBeginLoc())) |
Richard Smith | 6403e93 | 2014-11-14 00:37:55 +0000 | [diff] [blame] | 68 | return false; |
| 69 | |
| 70 | return llvm::StringSwitch<bool>(RD->getIdentifier()->getName()) |
| 71 | .Case("array", true) |
Richard Smith | 6289546 | 2016-10-19 23:47:37 +0000 | [diff] [blame] | 72 | .Case("pair", IsInStd) |
| 73 | .Case("priority_queue", IsInStd) |
| 74 | .Case("stack", IsInStd) |
| 75 | .Case("queue", IsInStd) |
Richard Smith | 6403e93 | 2014-11-14 00:37:55 +0000 | [diff] [blame] | 76 | .Default(false); |
| 77 | } |
| 78 | |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 79 | ExprResult Sema::ActOnNoexceptSpec(SourceLocation NoexceptLoc, |
| 80 | Expr *NoexceptExpr, |
| 81 | ExceptionSpecificationType &EST) { |
| 82 | // FIXME: This is bogus, a noexcept expression is not a condition. |
| 83 | ExprResult Converted = CheckBooleanCondition(NoexceptLoc, NoexceptExpr); |
Erich Keane | f0719bf | 2020-01-13 07:45:17 -0800 | [diff] [blame] | 84 | if (Converted.isInvalid()) { |
| 85 | EST = EST_NoexceptFalse; |
| 86 | |
| 87 | // Fill in an expression of 'false' as a fixup. |
| 88 | auto *BoolExpr = new (Context) |
| 89 | CXXBoolLiteralExpr(false, Context.BoolTy, NoexceptExpr->getBeginLoc()); |
| 90 | llvm::APSInt Value{1}; |
| 91 | Value = 0; |
| 92 | return ConstantExpr::Create(Context, BoolExpr, APValue{Value}); |
| 93 | } |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 94 | |
| 95 | if (Converted.get()->isValueDependent()) { |
| 96 | EST = EST_DependentNoexcept; |
| 97 | return Converted; |
| 98 | } |
| 99 | |
| 100 | llvm::APSInt Result; |
| 101 | Converted = VerifyIntegerConstantExpression( |
| 102 | Converted.get(), &Result, |
| 103 | diag::err_noexcept_needs_constant_expression, |
| 104 | /*AllowFold*/ false); |
| 105 | if (!Converted.isInvalid()) |
| 106 | EST = !Result ? EST_NoexceptFalse : EST_NoexceptTrue; |
| 107 | return Converted; |
| 108 | } |
| 109 | |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 110 | /// CheckSpecifiedExceptionType - Check if the given type is valid in an |
| 111 | /// exception specification. Incomplete types, or pointers to incomplete types |
| 112 | /// other than void are not allowed. |
Richard Smith | 8606d75 | 2012-11-28 22:33:28 +0000 | [diff] [blame] | 113 | /// |
| 114 | /// \param[in,out] T The exception type. This will be decayed to a pointer type |
| 115 | /// when the input is an array or a function type. |
Craig Topper | e335f25 | 2015-10-04 04:53:55 +0000 | [diff] [blame] | 116 | bool Sema::CheckSpecifiedExceptionType(QualType &T, SourceRange Range) { |
Richard Smith | a118c6a | 2012-11-28 22:52:42 +0000 | [diff] [blame] | 117 | // C++11 [except.spec]p2: |
| 118 | // A type cv T, "array of T", or "function returning T" denoted |
Richard Smith | 8606d75 | 2012-11-28 22:33:28 +0000 | [diff] [blame] | 119 | // in an exception-specification is adjusted to type T, "pointer to T", or |
| 120 | // "pointer to function returning T", respectively. |
Richard Smith | a118c6a | 2012-11-28 22:52:42 +0000 | [diff] [blame] | 121 | // |
| 122 | // We also apply this rule in C++98. |
Richard Smith | 8606d75 | 2012-11-28 22:33:28 +0000 | [diff] [blame] | 123 | if (T->isArrayType()) |
| 124 | T = Context.getArrayDecayedType(T); |
| 125 | else if (T->isFunctionType()) |
| 126 | T = Context.getPointerType(T); |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 127 | |
Richard Smith | a118c6a | 2012-11-28 22:52:42 +0000 | [diff] [blame] | 128 | int Kind = 0; |
Richard Smith | 8606d75 | 2012-11-28 22:33:28 +0000 | [diff] [blame] | 129 | QualType PointeeT = T; |
Richard Smith | a118c6a | 2012-11-28 22:52:42 +0000 | [diff] [blame] | 130 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 131 | PointeeT = PT->getPointeeType(); |
| 132 | Kind = 1; |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 133 | |
Richard Smith | a118c6a | 2012-11-28 22:52:42 +0000 | [diff] [blame] | 134 | // cv void* is explicitly permitted, despite being a pointer to an |
| 135 | // incomplete type. |
| 136 | if (PointeeT->isVoidType()) |
| 137 | return false; |
| 138 | } else if (const ReferenceType *RT = T->getAs<ReferenceType>()) { |
| 139 | PointeeT = RT->getPointeeType(); |
| 140 | Kind = 2; |
Richard Smith | 8606d75 | 2012-11-28 22:33:28 +0000 | [diff] [blame] | 141 | |
Richard Smith | a118c6a | 2012-11-28 22:52:42 +0000 | [diff] [blame] | 142 | if (RT->isRValueReferenceType()) { |
| 143 | // C++11 [except.spec]p2: |
| 144 | // A type denoted in an exception-specification shall not denote [...] |
| 145 | // an rvalue reference type. |
| 146 | Diag(Range.getBegin(), diag::err_rref_in_exception_spec) |
| 147 | << T << Range; |
| 148 | return true; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // C++11 [except.spec]p2: |
| 153 | // A type denoted in an exception-specification shall not denote an |
| 154 | // incomplete type other than a class currently being defined [...]. |
| 155 | // A type denoted in an exception-specification shall not denote a |
| 156 | // pointer or reference to an incomplete type, other than (cv) void* or a |
| 157 | // pointer or reference to a class currently being defined. |
David Majnemer | b2b0da4 | 2016-06-10 18:24:41 +0000 | [diff] [blame] | 158 | // In Microsoft mode, downgrade this to a warning. |
| 159 | unsigned DiagID = diag::err_incomplete_in_exception_spec; |
David Majnemer | 5d321e6 | 2016-06-11 01:25:04 +0000 | [diff] [blame] | 160 | bool ReturnValueOnError = true; |
Reid Kleckner | 39aa895 | 2019-08-27 17:52:03 +0000 | [diff] [blame] | 161 | if (getLangOpts().MSVCCompat) { |
David Majnemer | b2b0da4 | 2016-06-10 18:24:41 +0000 | [diff] [blame] | 162 | DiagID = diag::ext_incomplete_in_exception_spec; |
David Majnemer | 5d321e6 | 2016-06-11 01:25:04 +0000 | [diff] [blame] | 163 | ReturnValueOnError = false; |
| 164 | } |
Richard Smith | a118c6a | 2012-11-28 22:52:42 +0000 | [diff] [blame] | 165 | if (!(PointeeT->isRecordType() && |
Simon Pilgrim | 1cd399c | 2019-10-03 11:22:48 +0000 | [diff] [blame] | 166 | PointeeT->castAs<RecordType>()->isBeingDefined()) && |
David Majnemer | b2b0da4 | 2016-06-10 18:24:41 +0000 | [diff] [blame] | 167 | RequireCompleteType(Range.getBegin(), PointeeT, DiagID, Kind, Range)) |
David Majnemer | 5d321e6 | 2016-06-11 01:25:04 +0000 | [diff] [blame] | 168 | return ReturnValueOnError; |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 169 | |
Richard Sandiford | 0947296 | 2020-03-03 10:39:57 +0000 | [diff] [blame] | 170 | // The MSVC compatibility mode doesn't extend to sizeless types, |
| 171 | // so diagnose them separately. |
| 172 | if (PointeeT->isSizelessType() && Kind != 1) { |
| 173 | Diag(Range.getBegin(), diag::err_sizeless_in_exception_spec) |
| 174 | << (Kind == 2 ? 1 : 0) << PointeeT << Range; |
| 175 | return true; |
| 176 | } |
| 177 | |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 178 | return false; |
| 179 | } |
| 180 | |
| 181 | /// CheckDistantExceptionSpec - Check if the given type is a pointer or pointer |
| 182 | /// to member to a function with an exception specification. This means that |
| 183 | /// it is invalid to add another level of indirection. |
| 184 | bool Sema::CheckDistantExceptionSpec(QualType T) { |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 185 | // C++17 removes this rule in favor of putting exception specifications into |
| 186 | // the type system. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 187 | if (getLangOpts().CPlusPlus17) |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 188 | return false; |
| 189 | |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 190 | if (const PointerType *PT = T->getAs<PointerType>()) |
| 191 | T = PT->getPointeeType(); |
| 192 | else if (const MemberPointerType *PT = T->getAs<MemberPointerType>()) |
| 193 | T = PT->getPointeeType(); |
| 194 | else |
| 195 | return false; |
| 196 | |
| 197 | const FunctionProtoType *FnT = T->getAs<FunctionProtoType>(); |
| 198 | if (!FnT) |
| 199 | return false; |
| 200 | |
| 201 | return FnT->hasExceptionSpec(); |
| 202 | } |
| 203 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 204 | const FunctionProtoType * |
| 205 | Sema::ResolveExceptionSpec(SourceLocation Loc, const FunctionProtoType *FPT) { |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 206 | if (FPT->getExceptionSpecType() == EST_Unparsed) { |
| 207 | Diag(Loc, diag::err_exception_spec_not_parsed); |
| 208 | return nullptr; |
| 209 | } |
| 210 | |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 211 | if (!isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 212 | return FPT; |
| 213 | |
| 214 | FunctionDecl *SourceDecl = FPT->getExceptionSpecDecl(); |
| 215 | const FunctionProtoType *SourceFPT = |
| 216 | SourceDecl->getType()->castAs<FunctionProtoType>(); |
| 217 | |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 218 | // If the exception specification has already been resolved, just return it. |
| 219 | if (!isUnresolvedExceptionSpec(SourceFPT->getExceptionSpecType())) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 220 | return SourceFPT; |
| 221 | |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 222 | // Compute or instantiate the exception specification now. |
Richard Smith | 3901dfe | 2013-03-27 00:22:47 +0000 | [diff] [blame] | 223 | if (SourceFPT->getExceptionSpecType() == EST_Unevaluated) |
Richard Smith | 4a4e90a | 2019-12-13 14:11:04 -0800 | [diff] [blame] | 224 | EvaluateImplicitExceptionSpec(Loc, SourceDecl); |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 225 | else |
| 226 | InstantiateExceptionSpec(Loc, SourceDecl); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 227 | |
Davide Italiano | 922b702 | 2015-07-25 01:19:32 +0000 | [diff] [blame] | 228 | const FunctionProtoType *Proto = |
| 229 | SourceDecl->getType()->castAs<FunctionProtoType>(); |
| 230 | if (Proto->getExceptionSpecType() == clang::EST_Unparsed) { |
| 231 | Diag(Loc, diag::err_exception_spec_not_parsed); |
| 232 | Proto = nullptr; |
| 233 | } |
| 234 | return Proto; |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 237 | void |
| 238 | Sema::UpdateExceptionSpec(FunctionDecl *FD, |
| 239 | const FunctionProtoType::ExceptionSpecInfo &ESI) { |
Richard Smith | 564417a | 2014-03-20 21:47:22 +0000 | [diff] [blame] | 240 | // If we've fully resolved the exception specification, notify listeners. |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 241 | if (!isUnresolvedExceptionSpec(ESI.Type)) |
Richard Smith | 564417a | 2014-03-20 21:47:22 +0000 | [diff] [blame] | 242 | if (auto *Listener = getASTMutationListener()) |
| 243 | Listener->ResolvedExceptionSpec(FD); |
Richard Smith | 9e2341d | 2015-03-23 03:25:59 +0000 | [diff] [blame] | 244 | |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 245 | for (FunctionDecl *Redecl : FD->redecls()) |
| 246 | Context.adjustExceptionSpec(Redecl, ESI); |
Richard Smith | 564417a | 2014-03-20 21:47:22 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Richard Smith | 5159bbad | 2018-09-05 22:30:37 +0000 | [diff] [blame] | 249 | static bool exceptionSpecNotKnownYet(const FunctionDecl *FD) { |
| 250 | auto *MD = dyn_cast<CXXMethodDecl>(FD); |
| 251 | if (!MD) |
| 252 | return false; |
| 253 | |
| 254 | auto EST = MD->getType()->castAs<FunctionProtoType>()->getExceptionSpecType(); |
| 255 | return EST == EST_Unparsed || |
| 256 | (EST == EST_Unevaluated && MD->getParent()->isBeingDefined()); |
Simon Pilgrim | 58310ec | 2018-09-06 15:16:17 +0000 | [diff] [blame] | 257 | } |
Richard Smith | 5159bbad | 2018-09-05 22:30:37 +0000 | [diff] [blame] | 258 | |
Richard Smith | 13b40bc | 2016-11-30 00:13:55 +0000 | [diff] [blame] | 259 | static bool CheckEquivalentExceptionSpecImpl( |
| 260 | Sema &S, const PartialDiagnostic &DiagID, const PartialDiagnostic &NoteID, |
| 261 | const FunctionProtoType *Old, SourceLocation OldLoc, |
| 262 | const FunctionProtoType *New, SourceLocation NewLoc, |
| 263 | bool *MissingExceptionSpecification = nullptr, |
| 264 | bool *MissingEmptyExceptionSpecification = nullptr, |
| 265 | bool AllowNoexceptAllMatchWithNoSpec = false, bool IsOperatorNew = false); |
| 266 | |
Richard Smith | 66f3ac9 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 267 | /// Determine whether a function has an implicitly-generated exception |
Richard Smith | 1ee6352 | 2012-10-16 23:30:16 +0000 | [diff] [blame] | 268 | /// specification. |
Richard Smith | 66f3ac9 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 269 | static bool hasImplicitExceptionSpec(FunctionDecl *Decl) { |
| 270 | if (!isa<CXXDestructorDecl>(Decl) && |
| 271 | Decl->getDeclName().getCXXOverloadedOperator() != OO_Delete && |
| 272 | Decl->getDeclName().getCXXOverloadedOperator() != OO_Array_Delete) |
| 273 | return false; |
Richard Smith | 1ee6352 | 2012-10-16 23:30:16 +0000 | [diff] [blame] | 274 | |
Richard Smith | c7fb225 | 2014-02-07 22:51:16 +0000 | [diff] [blame] | 275 | // For a function that the user didn't declare: |
| 276 | // - if this is a destructor, its exception specification is implicit. |
| 277 | // - if this is 'operator delete' or 'operator delete[]', the exception |
| 278 | // specification is as-if an explicit exception specification was given |
| 279 | // (per [basic.stc.dynamic]p2). |
Richard Smith | 66f3ac9 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 280 | if (!Decl->getTypeSourceInfo()) |
Richard Smith | c7fb225 | 2014-02-07 22:51:16 +0000 | [diff] [blame] | 281 | return isa<CXXDestructorDecl>(Decl); |
Richard Smith | 66f3ac9 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 282 | |
Simon Pilgrim | afb163f | 2019-10-21 18:28:31 +0000 | [diff] [blame] | 283 | auto *Ty = Decl->getTypeSourceInfo()->getType()->castAs<FunctionProtoType>(); |
Richard Smith | 66f3ac9 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 284 | return !Ty->hasExceptionSpec(); |
Richard Smith | 1ee6352 | 2012-10-16 23:30:16 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Douglas Gregor | f40863c | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 287 | bool Sema::CheckEquivalentExceptionSpec(FunctionDecl *Old, FunctionDecl *New) { |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 288 | // Just completely ignore this under -fno-exceptions prior to C++17. |
| 289 | // In C++17 onwards, the exception specification is part of the type and |
Richard Smith | 13b40bc | 2016-11-30 00:13:55 +0000 | [diff] [blame] | 290 | // we will diagnose mismatches anyway, so it's better to check for them here. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 291 | if (!getLangOpts().CXXExceptions && !getLangOpts().CPlusPlus17) |
Richard Smith | 13b40bc | 2016-11-30 00:13:55 +0000 | [diff] [blame] | 292 | return false; |
| 293 | |
Sebastian Redl | cb5dd00 | 2011-03-15 19:52:30 +0000 | [diff] [blame] | 294 | OverloadedOperatorKind OO = New->getDeclName().getCXXOverloadedOperator(); |
| 295 | bool IsOperatorNew = OO == OO_New || OO == OO_Array_New; |
Douglas Gregor | d6bc5e6 | 2010-03-24 07:14:45 +0000 | [diff] [blame] | 296 | bool MissingExceptionSpecification = false; |
Douglas Gregor | f40863c | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 297 | bool MissingEmptyExceptionSpecification = false; |
Hans Wennborg | 39a509a | 2014-02-05 02:37:58 +0000 | [diff] [blame] | 298 | |
Francois Pichet | 13b4e68 | 2011-03-19 23:05:18 +0000 | [diff] [blame] | 299 | unsigned DiagID = diag::err_mismatched_exception_spec; |
Hans Wennborg | 39a509a | 2014-02-05 02:37:58 +0000 | [diff] [blame] | 300 | bool ReturnValueOnError = true; |
Reid Kleckner | 39aa895 | 2019-08-27 17:52:03 +0000 | [diff] [blame] | 301 | if (getLangOpts().MSVCCompat) { |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 302 | DiagID = diag::ext_mismatched_exception_spec; |
Hans Wennborg | 39a509a | 2014-02-05 02:37:58 +0000 | [diff] [blame] | 303 | ReturnValueOnError = false; |
| 304 | } |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 305 | |
Richard Smith | 5159bbad | 2018-09-05 22:30:37 +0000 | [diff] [blame] | 306 | // If we're befriending a member function of a class that's currently being |
| 307 | // defined, we might not be able to work out its exception specification yet. |
| 308 | // If not, defer the check until later. |
| 309 | if (exceptionSpecNotKnownYet(Old) || exceptionSpecNotKnownYet(New)) { |
| 310 | DelayedEquivalentExceptionSpecChecks.push_back({New, Old}); |
| 311 | return false; |
| 312 | } |
| 313 | |
Richard Smith | 1ee6352 | 2012-10-16 23:30:16 +0000 | [diff] [blame] | 314 | // Check the types as written: they must match before any exception |
| 315 | // specification adjustment is applied. |
Richard Smith | 13b40bc | 2016-11-30 00:13:55 +0000 | [diff] [blame] | 316 | if (!CheckEquivalentExceptionSpecImpl( |
| 317 | *this, PDiag(DiagID), PDiag(diag::note_previous_declaration), |
Richard Smith | 66f3ac9 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 318 | Old->getType()->getAs<FunctionProtoType>(), Old->getLocation(), |
| 319 | New->getType()->getAs<FunctionProtoType>(), New->getLocation(), |
Richard Smith | 1ee6352 | 2012-10-16 23:30:16 +0000 | [diff] [blame] | 320 | &MissingExceptionSpecification, &MissingEmptyExceptionSpecification, |
Richard Smith | 66f3ac9 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 321 | /*AllowNoexceptAllMatchWithNoSpec=*/true, IsOperatorNew)) { |
| 322 | // C++11 [except.spec]p4 [DR1492]: |
| 323 | // If a declaration of a function has an implicit |
| 324 | // exception-specification, other declarations of the function shall |
| 325 | // not specify an exception-specification. |
Richard Smith | e3ea001 | 2016-08-31 20:38:32 +0000 | [diff] [blame] | 326 | if (getLangOpts().CPlusPlus11 && getLangOpts().CXXExceptions && |
Richard Smith | 66f3ac9 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 327 | hasImplicitExceptionSpec(Old) != hasImplicitExceptionSpec(New)) { |
| 328 | Diag(New->getLocation(), diag::ext_implicit_exception_spec_mismatch) |
| 329 | << hasImplicitExceptionSpec(Old); |
Yaron Keren | 8b56366 | 2015-10-03 10:46:20 +0000 | [diff] [blame] | 330 | if (Old->getLocation().isValid()) |
Richard Smith | 66f3ac9 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 331 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 332 | } |
Douglas Gregor | f40863c | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 333 | return false; |
Richard Smith | 66f3ac9 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 334 | } |
Douglas Gregor | f40863c | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 335 | |
Eli Friedman | 96dbf6f | 2013-06-25 00:46:32 +0000 | [diff] [blame] | 336 | // The failure was something other than an missing exception |
Hans Wennborg | 39a509a | 2014-02-05 02:37:58 +0000 | [diff] [blame] | 337 | // 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] | 338 | if (!MissingExceptionSpecification) |
Hans Wennborg | 39a509a | 2014-02-05 02:37:58 +0000 | [diff] [blame] | 339 | return ReturnValueOnError; |
Douglas Gregor | f40863c | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 340 | |
Richard Smith | 66f3ac9 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 341 | const FunctionProtoType *NewProto = |
Eli Friedman | 96dbf6f | 2013-06-25 00:46:32 +0000 | [diff] [blame] | 342 | New->getType()->castAs<FunctionProtoType>(); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 343 | |
Douglas Gregor | f40863c | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 344 | // The new function declaration is only missing an empty exception |
| 345 | // specification "throw()". If the throw() specification came from a |
| 346 | // function in a system header that has C linkage, just add an empty |
Richard Smith | 836de6b | 2016-12-19 23:59:34 +0000 | [diff] [blame] | 347 | // exception specification to the "new" declaration. Note that C library |
| 348 | // implementations are permitted to add these nothrow exception |
| 349 | // specifications. |
| 350 | // |
| 351 | // Likewise if the old function is a builtin. |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 352 | if (MissingEmptyExceptionSpecification && NewProto && |
Douglas Gregor | d6bc5e6 | 2010-03-24 07:14:45 +0000 | [diff] [blame] | 353 | (Old->getLocation().isInvalid() || |
Richard Smith | 836de6b | 2016-12-19 23:59:34 +0000 | [diff] [blame] | 354 | Context.getSourceManager().isInSystemHeader(Old->getLocation()) || |
| 355 | Old->getBuiltinID()) && |
Douglas Gregor | f40863c | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 356 | Old->isExternC()) { |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 357 | New->setType(Context.getFunctionType( |
| 358 | NewProto->getReturnType(), NewProto->getParamTypes(), |
| 359 | NewProto->getExtProtoInfo().withExceptionSpec(EST_DynamicNone))); |
Douglas Gregor | f40863c | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 360 | return false; |
| 361 | } |
| 362 | |
Eli Friedman | 96dbf6f | 2013-06-25 00:46:32 +0000 | [diff] [blame] | 363 | const FunctionProtoType *OldProto = |
| 364 | Old->getType()->castAs<FunctionProtoType>(); |
Douglas Gregor | d6bc5e6 | 2010-03-24 07:14:45 +0000 | [diff] [blame] | 365 | |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 366 | FunctionProtoType::ExceptionSpecInfo ESI = OldProto->getExceptionSpecType(); |
| 367 | if (ESI.Type == EST_Dynamic) { |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 368 | // FIXME: What if the exceptions are described in terms of the old |
| 369 | // prototype's parameters? |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 370 | ESI.Exceptions = OldProto->exceptions(); |
Douglas Gregor | d6bc5e6 | 2010-03-24 07:14:45 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 373 | if (ESI.Type == EST_NoexceptFalse) |
| 374 | ESI.Type = EST_None; |
| 375 | if (ESI.Type == EST_NoexceptTrue) |
| 376 | ESI.Type = EST_BasicNoexcept; |
| 377 | |
| 378 | // For dependent noexcept, we can't just take the expression from the old |
| 379 | // prototype. It likely contains references to the old prototype's parameters. |
| 380 | if (ESI.Type == EST_DependentNoexcept) { |
Richard Smith | a91de37 | 2015-09-30 00:48:50 +0000 | [diff] [blame] | 381 | New->setInvalidDecl(); |
| 382 | } else { |
| 383 | // Update the type of the function with the appropriate exception |
| 384 | // specification. |
| 385 | New->setType(Context.getFunctionType( |
| 386 | NewProto->getReturnType(), NewProto->getParamTypes(), |
| 387 | NewProto->getExtProtoInfo().withExceptionSpec(ESI))); |
| 388 | } |
| 389 | |
Reid Kleckner | 39aa895 | 2019-08-27 17:52:03 +0000 | [diff] [blame] | 390 | if (getLangOpts().MSVCCompat && ESI.Type != EST_DependentNoexcept) { |
David Majnemer | 06ce8a4 | 2015-10-20 20:49:21 +0000 | [diff] [blame] | 391 | // Allow missing exception specifications in redeclarations as an extension. |
| 392 | DiagID = diag::ext_ms_missing_exception_specification; |
| 393 | ReturnValueOnError = false; |
| 394 | } else if (New->isReplaceableGlobalAllocationFunction() && |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 395 | ESI.Type != EST_DependentNoexcept) { |
David Majnemer | 06ce8a4 | 2015-10-20 20:49:21 +0000 | [diff] [blame] | 396 | // Allow missing exception specifications in redeclarations as an extension, |
| 397 | // when declaring a replaceable global allocation function. |
Richard Smith | a91de37 | 2015-09-30 00:48:50 +0000 | [diff] [blame] | 398 | DiagID = diag::ext_missing_exception_specification; |
| 399 | ReturnValueOnError = false; |
Erich Keane | 54182eb | 2019-05-31 14:26:19 +0000 | [diff] [blame] | 400 | } else if (ESI.Type == EST_NoThrow) { |
| 401 | // Allow missing attribute 'nothrow' in redeclarations, since this is a very |
| 402 | // common omission. |
| 403 | DiagID = diag::ext_missing_exception_specification; |
| 404 | ReturnValueOnError = false; |
Richard Smith | a91de37 | 2015-09-30 00:48:50 +0000 | [diff] [blame] | 405 | } else { |
| 406 | DiagID = diag::err_missing_exception_specification; |
| 407 | ReturnValueOnError = true; |
| 408 | } |
Eli Friedman | 96dbf6f | 2013-06-25 00:46:32 +0000 | [diff] [blame] | 409 | |
| 410 | // Warn about the lack of exception specification. |
| 411 | SmallString<128> ExceptionSpecString; |
| 412 | llvm::raw_svector_ostream OS(ExceptionSpecString); |
| 413 | switch (OldProto->getExceptionSpecType()) { |
| 414 | case EST_DynamicNone: |
| 415 | OS << "throw()"; |
| 416 | break; |
| 417 | |
| 418 | case EST_Dynamic: { |
| 419 | OS << "throw("; |
| 420 | bool OnFirstException = true; |
Aaron Ballman | b088fbe | 2014-03-17 15:38:09 +0000 | [diff] [blame] | 421 | for (const auto &E : OldProto->exceptions()) { |
Eli Friedman | 96dbf6f | 2013-06-25 00:46:32 +0000 | [diff] [blame] | 422 | if (OnFirstException) |
| 423 | OnFirstException = false; |
| 424 | else |
| 425 | OS << ", "; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 426 | |
Aaron Ballman | b088fbe | 2014-03-17 15:38:09 +0000 | [diff] [blame] | 427 | OS << E.getAsString(getPrintingPolicy()); |
Eli Friedman | 96dbf6f | 2013-06-25 00:46:32 +0000 | [diff] [blame] | 428 | } |
| 429 | OS << ")"; |
| 430 | break; |
| 431 | } |
| 432 | |
| 433 | case EST_BasicNoexcept: |
| 434 | OS << "noexcept"; |
| 435 | break; |
| 436 | |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 437 | case EST_DependentNoexcept: |
| 438 | case EST_NoexceptFalse: |
| 439 | case EST_NoexceptTrue: |
Eli Friedman | 96dbf6f | 2013-06-25 00:46:32 +0000 | [diff] [blame] | 440 | OS << "noexcept("; |
Richard Trieu | ddd01ce | 2014-06-09 22:53:25 +0000 | [diff] [blame] | 441 | assert(OldProto->getNoexceptExpr() != nullptr && "Expected non-null Expr"); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 442 | OldProto->getNoexceptExpr()->printPretty(OS, nullptr, getPrintingPolicy()); |
Eli Friedman | 96dbf6f | 2013-06-25 00:46:32 +0000 | [diff] [blame] | 443 | OS << ")"; |
| 444 | break; |
Erich Keane | 54182eb | 2019-05-31 14:26:19 +0000 | [diff] [blame] | 445 | case EST_NoThrow: |
| 446 | OS <<"__attribute__((nothrow))"; |
| 447 | break; |
Erich Keane | 68fa6dd | 2019-05-31 17:00:48 +0000 | [diff] [blame] | 448 | case EST_None: |
| 449 | case EST_MSAny: |
| 450 | case EST_Unevaluated: |
| 451 | case EST_Uninstantiated: |
| 452 | case EST_Unparsed: |
Eli Friedman | 96dbf6f | 2013-06-25 00:46:32 +0000 | [diff] [blame] | 453 | llvm_unreachable("This spec type is compatible with none."); |
| 454 | } |
Eli Friedman | 96dbf6f | 2013-06-25 00:46:32 +0000 | [diff] [blame] | 455 | |
| 456 | SourceLocation FixItLoc; |
| 457 | if (TypeSourceInfo *TSInfo = New->getTypeSourceInfo()) { |
| 458 | TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens(); |
Richard Smith | a91de37 | 2015-09-30 00:48:50 +0000 | [diff] [blame] | 459 | // FIXME: Preserve enough information so that we can produce a correct fixit |
| 460 | // location when there is a trailing return type. |
| 461 | if (auto FTLoc = TL.getAs<FunctionProtoTypeLoc>()) |
| 462 | if (!FTLoc.getTypePtr()->hasTrailingReturn()) |
| 463 | FixItLoc = getLocForEndOfToken(FTLoc.getLocalRangeEnd()); |
Eli Friedman | 96dbf6f | 2013-06-25 00:46:32 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | if (FixItLoc.isInvalid()) |
Richard Smith | a91de37 | 2015-09-30 00:48:50 +0000 | [diff] [blame] | 467 | Diag(New->getLocation(), DiagID) |
Eli Friedman | 96dbf6f | 2013-06-25 00:46:32 +0000 | [diff] [blame] | 468 | << New << OS.str(); |
| 469 | else { |
Richard Smith | a91de37 | 2015-09-30 00:48:50 +0000 | [diff] [blame] | 470 | Diag(New->getLocation(), DiagID) |
Eli Friedman | 96dbf6f | 2013-06-25 00:46:32 +0000 | [diff] [blame] | 471 | << New << OS.str() |
| 472 | << FixItHint::CreateInsertion(FixItLoc, " " + OS.str().str()); |
| 473 | } |
| 474 | |
Yaron Keren | 8b56366 | 2015-10-03 10:46:20 +0000 | [diff] [blame] | 475 | if (Old->getLocation().isValid()) |
Eli Friedman | 96dbf6f | 2013-06-25 00:46:32 +0000 | [diff] [blame] | 476 | Diag(Old->getLocation(), diag::note_previous_declaration); |
| 477 | |
Richard Smith | a91de37 | 2015-09-30 00:48:50 +0000 | [diff] [blame] | 478 | return ReturnValueOnError; |
Douglas Gregor | f40863c | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 481 | /// CheckEquivalentExceptionSpec - Check if the two types have equivalent |
| 482 | /// exception specifications. Exception specifications are equivalent if |
| 483 | /// they allow exactly the same set of exception types. It does not matter how |
| 484 | /// that is achieved. See C++ [except.spec]p2. |
| 485 | bool Sema::CheckEquivalentExceptionSpec( |
| 486 | const FunctionProtoType *Old, SourceLocation OldLoc, |
| 487 | const FunctionProtoType *New, SourceLocation NewLoc) { |
Richard Smith | 13b40bc | 2016-11-30 00:13:55 +0000 | [diff] [blame] | 488 | if (!getLangOpts().CXXExceptions) |
| 489 | return false; |
| 490 | |
Francois Pichet | 13b4e68 | 2011-03-19 23:05:18 +0000 | [diff] [blame] | 491 | unsigned DiagID = diag::err_mismatched_exception_spec; |
Reid Kleckner | 39aa895 | 2019-08-27 17:52:03 +0000 | [diff] [blame] | 492 | if (getLangOpts().MSVCCompat) |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 493 | DiagID = diag::ext_mismatched_exception_spec; |
Richard Smith | 13b40bc | 2016-11-30 00:13:55 +0000 | [diff] [blame] | 494 | bool Result = CheckEquivalentExceptionSpecImpl( |
| 495 | *this, PDiag(DiagID), PDiag(diag::note_previous_declaration), |
| 496 | Old, OldLoc, New, NewLoc); |
Hans Wennborg | 39a509a | 2014-02-05 02:37:58 +0000 | [diff] [blame] | 497 | |
| 498 | // In Microsoft mode, mismatching exception specifications just cause a warning. |
Reid Kleckner | 39aa895 | 2019-08-27 17:52:03 +0000 | [diff] [blame] | 499 | if (getLangOpts().MSVCCompat) |
Hans Wennborg | 39a509a | 2014-02-05 02:37:58 +0000 | [diff] [blame] | 500 | return false; |
| 501 | return Result; |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 504 | /// CheckEquivalentExceptionSpec - Check if the two types have compatible |
| 505 | /// exception specifications. See C++ [except.spec]p3. |
Richard Smith | 66f3ac9 | 2012-10-20 08:26:51 +0000 | [diff] [blame] | 506 | /// |
| 507 | /// \return \c false if the exception specifications match, \c true if there is |
| 508 | /// a problem. If \c true is returned, either a diagnostic has already been |
| 509 | /// produced or \c *MissingExceptionSpecification is set to \c true. |
Richard Smith | 13b40bc | 2016-11-30 00:13:55 +0000 | [diff] [blame] | 510 | static bool CheckEquivalentExceptionSpecImpl( |
| 511 | Sema &S, const PartialDiagnostic &DiagID, const PartialDiagnostic &NoteID, |
| 512 | const FunctionProtoType *Old, SourceLocation OldLoc, |
| 513 | const FunctionProtoType *New, SourceLocation NewLoc, |
| 514 | bool *MissingExceptionSpecification, |
| 515 | bool *MissingEmptyExceptionSpecification, |
| 516 | bool AllowNoexceptAllMatchWithNoSpec, bool IsOperatorNew) { |
Douglas Gregor | d6bc5e6 | 2010-03-24 07:14:45 +0000 | [diff] [blame] | 517 | if (MissingExceptionSpecification) |
| 518 | *MissingExceptionSpecification = false; |
| 519 | |
Douglas Gregor | f40863c | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 520 | if (MissingEmptyExceptionSpecification) |
| 521 | *MissingEmptyExceptionSpecification = false; |
| 522 | |
Richard Smith | 13b40bc | 2016-11-30 00:13:55 +0000 | [diff] [blame] | 523 | Old = S.ResolveExceptionSpec(NewLoc, Old); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 524 | if (!Old) |
| 525 | return false; |
Richard Smith | 13b40bc | 2016-11-30 00:13:55 +0000 | [diff] [blame] | 526 | New = S.ResolveExceptionSpec(NewLoc, New); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 527 | if (!New) |
| 528 | return false; |
| 529 | |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 530 | // C++0x [except.spec]p3: Two exception-specifications are compatible if: |
| 531 | // - both are non-throwing, regardless of their form, |
| 532 | // - both have the form noexcept(constant-expression) and the constant- |
| 533 | // expressions are equivalent, |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 534 | // - both are dynamic-exception-specifications that have the same set of |
| 535 | // adjusted types. |
| 536 | // |
Eric Christopher | e6b7cf4 | 2015-07-10 18:25:52 +0000 | [diff] [blame] | 537 | // C++0x [except.spec]p12: An exception-specification is non-throwing if it is |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 538 | // of the form throw(), noexcept, or noexcept(constant-expression) where the |
| 539 | // constant-expression yields true. |
| 540 | // |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 541 | // C++0x [except.spec]p4: If any declaration of a function has an exception- |
| 542 | // specifier that is not a noexcept-specification allowing all exceptions, |
| 543 | // all declarations [...] of that function shall have a compatible |
| 544 | // exception-specification. |
| 545 | // |
| 546 | // That last point basically means that noexcept(false) matches no spec. |
| 547 | // It's considered when AllowNoexceptAllMatchWithNoSpec is true. |
| 548 | |
| 549 | ExceptionSpecificationType OldEST = Old->getExceptionSpecType(); |
| 550 | ExceptionSpecificationType NewEST = New->getExceptionSpecType(); |
| 551 | |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 552 | assert(!isUnresolvedExceptionSpec(OldEST) && |
| 553 | !isUnresolvedExceptionSpec(NewEST) && |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 554 | "Shouldn't see unknown exception specifications here"); |
| 555 | |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 556 | CanThrowResult OldCanThrow = Old->canThrow(); |
| 557 | CanThrowResult NewCanThrow = New->canThrow(); |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 558 | |
| 559 | // Any non-throwing specifications are compatible. |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 560 | if (OldCanThrow == CT_Cannot && NewCanThrow == CT_Cannot) |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 561 | return false; |
| 562 | |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 563 | // Any throws-anything specifications are usually compatible. |
| 564 | if (OldCanThrow == CT_Can && OldEST != EST_Dynamic && |
| 565 | NewCanThrow == CT_Can && NewEST != EST_Dynamic) { |
| 566 | // The exception is that the absence of an exception specification only |
| 567 | // matches noexcept(false) for functions, as described above. |
| 568 | if (!AllowNoexceptAllMatchWithNoSpec && |
| 569 | ((OldEST == EST_None && NewEST == EST_NoexceptFalse) || |
| 570 | (OldEST == EST_NoexceptFalse && NewEST == EST_None))) { |
| 571 | // This is the disallowed case. |
| 572 | } else { |
| 573 | return false; |
| 574 | } |
| 575 | } |
| 576 | |
Richard Smith | b64764a | 2018-07-12 21:11:25 +0000 | [diff] [blame] | 577 | // C++14 [except.spec]p3: |
| 578 | // Two exception-specifications are compatible if [...] both have the form |
| 579 | // noexcept(constant-expression) and the constant-expressions are equivalent |
| 580 | if (OldEST == EST_DependentNoexcept && NewEST == EST_DependentNoexcept) { |
| 581 | llvm::FoldingSetNodeID OldFSN, NewFSN; |
| 582 | Old->getNoexceptExpr()->Profile(OldFSN, S.Context, true); |
| 583 | New->getNoexceptExpr()->Profile(NewFSN, S.Context, true); |
| 584 | if (OldFSN == NewFSN) |
| 585 | return false; |
| 586 | } |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 587 | |
| 588 | // Dynamic exception specifications with the same set of adjusted types |
| 589 | // are compatible. |
| 590 | if (OldEST == EST_Dynamic && NewEST == EST_Dynamic) { |
| 591 | bool Success = true; |
| 592 | // Both have a dynamic exception spec. Collect the first set, then compare |
| 593 | // to the second. |
| 594 | llvm::SmallPtrSet<CanQualType, 8> OldTypes, NewTypes; |
| 595 | for (const auto &I : Old->exceptions()) |
| 596 | OldTypes.insert(S.Context.getCanonicalType(I).getUnqualifiedType()); |
| 597 | |
| 598 | for (const auto &I : New->exceptions()) { |
| 599 | CanQualType TypePtr = S.Context.getCanonicalType(I).getUnqualifiedType(); |
| 600 | if (OldTypes.count(TypePtr)) |
| 601 | NewTypes.insert(TypePtr); |
| 602 | else { |
| 603 | Success = false; |
| 604 | break; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | if (Success && OldTypes.size() == NewTypes.size()) |
| 609 | return false; |
| 610 | } |
| 611 | |
Sebastian Redl | cb5dd00 | 2011-03-15 19:52:30 +0000 | [diff] [blame] | 612 | // As a special compatibility feature, under C++0x we accept no spec and |
| 613 | // throw(std::bad_alloc) as equivalent for operator new and operator new[]. |
| 614 | // This is because the implicit declaration changed, but old code would break. |
Richard Smith | 13b40bc | 2016-11-30 00:13:55 +0000 | [diff] [blame] | 615 | if (S.getLangOpts().CPlusPlus11 && IsOperatorNew) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 616 | const FunctionProtoType *WithExceptions = nullptr; |
Sebastian Redl | cb5dd00 | 2011-03-15 19:52:30 +0000 | [diff] [blame] | 617 | if (OldEST == EST_None && NewEST == EST_Dynamic) |
| 618 | WithExceptions = New; |
| 619 | else if (OldEST == EST_Dynamic && NewEST == EST_None) |
| 620 | WithExceptions = Old; |
| 621 | if (WithExceptions && WithExceptions->getNumExceptions() == 1) { |
| 622 | // One has no spec, the other throw(something). If that something is |
| 623 | // std::bad_alloc, all conditions are met. |
| 624 | QualType Exception = *WithExceptions->exception_begin(); |
| 625 | if (CXXRecordDecl *ExRecord = Exception->getAsCXXRecordDecl()) { |
| 626 | IdentifierInfo* Name = ExRecord->getIdentifier(); |
| 627 | if (Name && Name->getName() == "bad_alloc") { |
| 628 | // It's called bad_alloc, but is it in std? |
Richard Trieu | c771d5d | 2014-05-28 02:16:01 +0000 | [diff] [blame] | 629 | if (ExRecord->isInStdNamespace()) { |
| 630 | return false; |
Sebastian Redl | cb5dd00 | 2011-03-15 19:52:30 +0000 | [diff] [blame] | 631 | } |
| 632 | } |
| 633 | } |
| 634 | } |
| 635 | } |
| 636 | |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 637 | // If the caller wants to handle the case that the new function is |
| 638 | // incompatible due to a missing exception specification, let it. |
| 639 | if (MissingExceptionSpecification && OldEST != EST_None && |
| 640 | NewEST == EST_None) { |
| 641 | // The old type has an exception specification of some sort, but |
| 642 | // the new type does not. |
| 643 | *MissingExceptionSpecification = true; |
Douglas Gregor | d6bc5e6 | 2010-03-24 07:14:45 +0000 | [diff] [blame] | 644 | |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 645 | if (MissingEmptyExceptionSpecification && OldCanThrow == CT_Cannot) { |
| 646 | // The old type has a throw() or noexcept(true) exception specification |
| 647 | // and the new type has no exception specification, and the caller asked |
| 648 | // to handle this itself. |
| 649 | *MissingEmptyExceptionSpecification = true; |
Douglas Gregor | f40863c | 2010-02-12 07:32:17 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 652 | return true; |
| 653 | } |
| 654 | |
Richard Smith | 13b40bc | 2016-11-30 00:13:55 +0000 | [diff] [blame] | 655 | S.Diag(NewLoc, DiagID); |
David Majnemer | 7da2302 | 2015-02-19 07:28:55 +0000 | [diff] [blame] | 656 | if (NoteID.getDiagID() != 0 && OldLoc.isValid()) |
Richard Smith | 13b40bc | 2016-11-30 00:13:55 +0000 | [diff] [blame] | 657 | S.Diag(OldLoc, NoteID); |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 658 | return true; |
| 659 | } |
| 660 | |
Richard Smith | 13b40bc | 2016-11-30 00:13:55 +0000 | [diff] [blame] | 661 | bool Sema::CheckEquivalentExceptionSpec(const PartialDiagnostic &DiagID, |
| 662 | const PartialDiagnostic &NoteID, |
| 663 | const FunctionProtoType *Old, |
| 664 | SourceLocation OldLoc, |
| 665 | const FunctionProtoType *New, |
| 666 | SourceLocation NewLoc) { |
| 667 | if (!getLangOpts().CXXExceptions) |
| 668 | return false; |
| 669 | return CheckEquivalentExceptionSpecImpl(*this, DiagID, NoteID, Old, OldLoc, |
| 670 | New, NewLoc); |
| 671 | } |
| 672 | |
Richard Smith | 56ae0a6 | 2018-01-13 05:05:45 +0000 | [diff] [blame] | 673 | bool Sema::handlerCanCatch(QualType HandlerType, QualType ExceptionType) { |
| 674 | // [except.handle]p3: |
| 675 | // A handler is a match for an exception object of type E if: |
| 676 | |
| 677 | // HandlerType must be ExceptionType or derived from it, or pointer or |
| 678 | // reference to such types. |
| 679 | const ReferenceType *RefTy = HandlerType->getAs<ReferenceType>(); |
| 680 | if (RefTy) |
| 681 | HandlerType = RefTy->getPointeeType(); |
| 682 | |
| 683 | // -- the handler is of type cv T or cv T& and E and T are the same type |
| 684 | if (Context.hasSameUnqualifiedType(ExceptionType, HandlerType)) |
| 685 | return true; |
| 686 | |
| 687 | // FIXME: ObjC pointer types? |
| 688 | if (HandlerType->isPointerType() || HandlerType->isMemberPointerType()) { |
| 689 | if (RefTy && (!HandlerType.isConstQualified() || |
| 690 | HandlerType.isVolatileQualified())) |
| 691 | return false; |
| 692 | |
| 693 | // -- the handler is of type cv T or const T& where T is a pointer or |
| 694 | // pointer to member type and E is std::nullptr_t |
| 695 | if (ExceptionType->isNullPtrType()) |
| 696 | return true; |
| 697 | |
| 698 | // -- the handler is of type cv T or const T& where T is a pointer or |
| 699 | // pointer to member type and E is a pointer or pointer to member type |
| 700 | // that can be converted to T by one or more of |
| 701 | // -- a qualification conversion |
| 702 | // -- a function pointer conversion |
| 703 | bool LifetimeConv; |
| 704 | QualType Result; |
| 705 | // FIXME: Should we treat the exception as catchable if a lifetime |
| 706 | // conversion is required? |
| 707 | if (IsQualificationConversion(ExceptionType, HandlerType, false, |
| 708 | LifetimeConv) || |
| 709 | IsFunctionConversion(ExceptionType, HandlerType, Result)) |
| 710 | return true; |
| 711 | |
| 712 | // -- a standard pointer conversion [...] |
| 713 | if (!ExceptionType->isPointerType() || !HandlerType->isPointerType()) |
| 714 | return false; |
| 715 | |
| 716 | // Handle the "qualification conversion" portion. |
| 717 | Qualifiers EQuals, HQuals; |
| 718 | ExceptionType = Context.getUnqualifiedArrayType( |
| 719 | ExceptionType->getPointeeType(), EQuals); |
| 720 | HandlerType = Context.getUnqualifiedArrayType( |
| 721 | HandlerType->getPointeeType(), HQuals); |
| 722 | if (!HQuals.compatiblyIncludes(EQuals)) |
| 723 | return false; |
| 724 | |
| 725 | if (HandlerType->isVoidType() && ExceptionType->isObjectType()) |
| 726 | return true; |
| 727 | |
| 728 | // The only remaining case is a derived-to-base conversion. |
| 729 | } |
| 730 | |
| 731 | // -- the handler is of type cg T or cv T& and T is an unambiguous public |
| 732 | // base class of E |
| 733 | if (!ExceptionType->isRecordType() || !HandlerType->isRecordType()) |
| 734 | return false; |
| 735 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 736 | /*DetectVirtual=*/false); |
| 737 | if (!IsDerivedFrom(SourceLocation(), ExceptionType, HandlerType, Paths) || |
| 738 | Paths.isAmbiguous(Context.getCanonicalType(HandlerType))) |
| 739 | return false; |
| 740 | |
| 741 | // Do this check from a context without privileges. |
| 742 | switch (CheckBaseClassAccess(SourceLocation(), HandlerType, ExceptionType, |
| 743 | Paths.front(), |
| 744 | /*Diagnostic*/ 0, |
| 745 | /*ForceCheck*/ true, |
| 746 | /*ForceUnprivileged*/ true)) { |
| 747 | case AR_accessible: return true; |
| 748 | case AR_inaccessible: return false; |
| 749 | case AR_dependent: |
| 750 | llvm_unreachable("access check dependent for unprivileged context"); |
| 751 | case AR_delayed: |
| 752 | llvm_unreachable("access check delayed in non-declaration"); |
| 753 | } |
| 754 | llvm_unreachable("unexpected access check result"); |
| 755 | } |
| 756 | |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 757 | /// CheckExceptionSpecSubset - Check whether the second function type's |
| 758 | /// exception specification is a subset (or equivalent) of the first function |
| 759 | /// type. This is used by override and pointer assignment checks. |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 760 | bool Sema::CheckExceptionSpecSubset(const PartialDiagnostic &DiagID, |
| 761 | const PartialDiagnostic &NestedDiagID, |
| 762 | const PartialDiagnostic &NoteID, |
Erich Keane | 81ef625 | 2019-06-03 18:36:26 +0000 | [diff] [blame] | 763 | const PartialDiagnostic &NoThrowDiagID, |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 764 | const FunctionProtoType *Superset, |
| 765 | SourceLocation SuperLoc, |
| 766 | const FunctionProtoType *Subset, |
| 767 | SourceLocation SubLoc) { |
John McCall | f9c9409 | 2010-05-28 08:37:35 +0000 | [diff] [blame] | 768 | |
| 769 | // Just auto-succeed under -fno-exceptions. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 770 | if (!getLangOpts().CXXExceptions) |
John McCall | f9c9409 | 2010-05-28 08:37:35 +0000 | [diff] [blame] | 771 | return false; |
| 772 | |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 773 | // FIXME: As usual, we could be more specific in our error messages, but |
| 774 | // that better waits until we've got types with source locations. |
| 775 | |
| 776 | if (!SubLoc.isValid()) |
| 777 | SubLoc = SuperLoc; |
| 778 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 779 | // Resolve the exception specifications, if needed. |
| 780 | Superset = ResolveExceptionSpec(SuperLoc, Superset); |
| 781 | if (!Superset) |
| 782 | return false; |
| 783 | Subset = ResolveExceptionSpec(SubLoc, Subset); |
| 784 | if (!Subset) |
| 785 | return false; |
| 786 | |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 787 | ExceptionSpecificationType SuperEST = Superset->getExceptionSpecType(); |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 788 | ExceptionSpecificationType SubEST = Subset->getExceptionSpecType(); |
| 789 | assert(!isUnresolvedExceptionSpec(SuperEST) && |
| 790 | !isUnresolvedExceptionSpec(SubEST) && |
| 791 | "Shouldn't see unknown exception specifications here"); |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 792 | |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 793 | // If there are dependent noexcept specs, assume everything is fine. Unlike |
| 794 | // with the equivalency check, this is safe in this case, because we don't |
| 795 | // want to merge declarations. Checks after instantiation will catch any |
| 796 | // omissions we make here. |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 797 | if (SuperEST == EST_DependentNoexcept || SubEST == EST_DependentNoexcept) |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 798 | return false; |
| 799 | |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 800 | CanThrowResult SuperCanThrow = Superset->canThrow(); |
| 801 | CanThrowResult SubCanThrow = Subset->canThrow(); |
| 802 | |
| 803 | // If the superset contains everything or the subset contains nothing, we're |
| 804 | // done. |
| 805 | if ((SuperCanThrow == CT_Can && SuperEST != EST_Dynamic) || |
| 806 | SubCanThrow == CT_Cannot) |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 807 | return CheckParamExceptionSpec(NestedDiagID, NoteID, Superset, SuperLoc, |
| 808 | Subset, SubLoc); |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 809 | |
Erich Keane | 81ef625 | 2019-06-03 18:36:26 +0000 | [diff] [blame] | 810 | // Allow __declspec(nothrow) to be missing on redeclaration as an extension in |
| 811 | // some cases. |
| 812 | if (NoThrowDiagID.getDiagID() != 0 && SubCanThrow == CT_Can && |
| 813 | SuperCanThrow == CT_Cannot && SuperEST == EST_NoThrow) { |
| 814 | Diag(SubLoc, NoThrowDiagID); |
| 815 | if (NoteID.getDiagID() != 0) |
| 816 | Diag(SuperLoc, NoteID); |
| 817 | return true; |
| 818 | } |
| 819 | |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 820 | // If the subset contains everything or the superset contains nothing, we've |
| 821 | // failed. |
| 822 | if ((SubCanThrow == CT_Can && SubEST != EST_Dynamic) || |
| 823 | SuperCanThrow == CT_Cannot) { |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 824 | Diag(SubLoc, DiagID); |
| 825 | if (NoteID.getDiagID() != 0) |
| 826 | Diag(SuperLoc, NoteID); |
| 827 | return true; |
| 828 | } |
| 829 | |
| 830 | assert(SuperEST == EST_Dynamic && SubEST == EST_Dynamic && |
| 831 | "Exception spec subset: non-dynamic case slipped through."); |
| 832 | |
| 833 | // Neither contains everything or nothing. Do a proper comparison. |
Richard Smith | 56ae0a6 | 2018-01-13 05:05:45 +0000 | [diff] [blame] | 834 | for (QualType SubI : Subset->exceptions()) { |
| 835 | if (const ReferenceType *RefTy = SubI->getAs<ReferenceType>()) |
| 836 | SubI = RefTy->getPointeeType(); |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 837 | |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 838 | // Make sure it's in the superset. |
Richard Smith | 56ae0a6 | 2018-01-13 05:05:45 +0000 | [diff] [blame] | 839 | bool Contained = false; |
| 840 | for (QualType SuperI : Superset->exceptions()) { |
| 841 | // [except.spec]p5: |
| 842 | // the target entity shall allow at least the exceptions allowed by the |
| 843 | // source |
| 844 | // |
| 845 | // We interpret this as meaning that a handler for some target type would |
| 846 | // catch an exception of each source type. |
| 847 | if (handlerCanCatch(SuperI, SubI)) { |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 848 | Contained = true; |
| 849 | break; |
| 850 | } |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 851 | } |
| 852 | if (!Contained) { |
| 853 | Diag(SubLoc, DiagID); |
Sebastian Redl | a44822f | 2009-10-14 16:09:29 +0000 | [diff] [blame] | 854 | if (NoteID.getDiagID() != 0) |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 855 | Diag(SuperLoc, NoteID); |
| 856 | return true; |
| 857 | } |
| 858 | } |
| 859 | // We've run half the gauntlet. |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 860 | return CheckParamExceptionSpec(NestedDiagID, NoteID, Superset, SuperLoc, |
| 861 | Subset, SubLoc); |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 862 | } |
| 863 | |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 864 | static bool |
| 865 | CheckSpecForTypesEquivalent(Sema &S, const PartialDiagnostic &DiagID, |
| 866 | const PartialDiagnostic &NoteID, QualType Target, |
| 867 | SourceLocation TargetLoc, QualType Source, |
| 868 | SourceLocation SourceLoc) { |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 869 | const FunctionProtoType *TFunc = GetUnderlyingFunction(Target); |
| 870 | if (!TFunc) |
| 871 | return false; |
| 872 | const FunctionProtoType *SFunc = GetUnderlyingFunction(Source); |
| 873 | if (!SFunc) |
| 874 | return false; |
| 875 | |
| 876 | return S.CheckEquivalentExceptionSpec(DiagID, NoteID, TFunc, TargetLoc, |
| 877 | SFunc, SourceLoc); |
| 878 | } |
| 879 | |
| 880 | /// CheckParamExceptionSpec - Check if the parameter and return types of the |
| 881 | /// two functions have equivalent exception specs. This is part of the |
| 882 | /// assignment and override compatibility check. We do not check the parameters |
| 883 | /// of parameter function pointers recursively, as no sane programmer would |
| 884 | /// even be able to write such a function type. |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 885 | bool Sema::CheckParamExceptionSpec(const PartialDiagnostic &DiagID, |
| 886 | const PartialDiagnostic &NoteID, |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 887 | const FunctionProtoType *Target, |
| 888 | SourceLocation TargetLoc, |
| 889 | const FunctionProtoType *Source, |
| 890 | SourceLocation SourceLoc) { |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 891 | auto RetDiag = DiagID; |
| 892 | RetDiag << 0; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 893 | if (CheckSpecForTypesEquivalent( |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 894 | *this, RetDiag, PDiag(), |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 895 | Target->getReturnType(), TargetLoc, Source->getReturnType(), |
| 896 | SourceLoc)) |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 897 | return true; |
| 898 | |
Sebastian Redl | a44822f | 2009-10-14 16:09:29 +0000 | [diff] [blame] | 899 | // We shouldn't even be testing this unless the arguments are otherwise |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 900 | // compatible. |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 901 | assert(Target->getNumParams() == Source->getNumParams() && |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 902 | "Functions have different argument counts."); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 903 | for (unsigned i = 0, E = Target->getNumParams(); i != E; ++i) { |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 904 | auto ParamDiag = DiagID; |
| 905 | ParamDiag << 1; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 906 | if (CheckSpecForTypesEquivalent( |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 907 | *this, ParamDiag, PDiag(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 908 | Target->getParamType(i), TargetLoc, Source->getParamType(i), |
| 909 | SourceLoc)) |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 910 | return true; |
| 911 | } |
| 912 | return false; |
| 913 | } |
| 914 | |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 915 | bool Sema::CheckExceptionSpecCompatibility(Expr *From, QualType ToType) { |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 916 | // First we check for applicability. |
| 917 | // Target type must be a function, function pointer or function reference. |
| 918 | const FunctionProtoType *ToFunc = GetUnderlyingFunction(ToType); |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 919 | if (!ToFunc || ToFunc->hasDependentExceptionSpec()) |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 920 | return false; |
| 921 | |
| 922 | // SourceType must be a function or function pointer. |
| 923 | const FunctionProtoType *FromFunc = GetUnderlyingFunction(From->getType()); |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 924 | if (!FromFunc || FromFunc->hasDependentExceptionSpec()) |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 925 | return false; |
| 926 | |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 927 | unsigned DiagID = diag::err_incompatible_exception_specs; |
| 928 | unsigned NestedDiagID = diag::err_deep_exception_specs_differ; |
| 929 | // This is not an error in C++17 onwards, unless the noexceptness doesn't |
| 930 | // match, but in that case we have a full-on type mismatch, not just a |
| 931 | // type sugar mismatch. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 932 | if (getLangOpts().CPlusPlus17) { |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 933 | DiagID = diag::warn_incompatible_exception_specs; |
| 934 | NestedDiagID = diag::warn_deep_exception_specs_differ; |
| 935 | } |
| 936 | |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 937 | // Now we've got the correct types on both sides, check their compatibility. |
| 938 | // This means that the source of the conversion can only throw a subset of |
| 939 | // the exceptions of the target, and any exception specs on arguments or |
| 940 | // return types must be equivalent. |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 941 | // |
| 942 | // FIXME: If there is a nested dependent exception specification, we should |
| 943 | // not be checking it here. This is fine: |
| 944 | // template<typename T> void f() { |
| 945 | // void (*p)(void (*) throw(T)); |
| 946 | // void (*q)(void (*) throw(int)) = p; |
| 947 | // } |
| 948 | // ... because it might be instantiated with T=int. |
Erich Keane | 81ef625 | 2019-06-03 18:36:26 +0000 | [diff] [blame] | 949 | return CheckExceptionSpecSubset( |
| 950 | PDiag(DiagID), PDiag(NestedDiagID), PDiag(), PDiag(), ToFunc, |
| 951 | From->getSourceRange().getBegin(), FromFunc, SourceLocation()) && |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 952 | !getLangOpts().CPlusPlus17; |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | bool Sema::CheckOverridingFunctionExceptionSpec(const CXXMethodDecl *New, |
| 956 | const CXXMethodDecl *Old) { |
Richard Smith | 88f4549 | 2014-11-22 03:09:05 +0000 | [diff] [blame] | 957 | // If the new exception specification hasn't been parsed yet, skip the check. |
| 958 | // We'll get called again once it's been parsed. |
| 959 | if (New->getType()->castAs<FunctionProtoType>()->getExceptionSpecType() == |
| 960 | EST_Unparsed) |
| 961 | return false; |
Richard Smith | 5159bbad | 2018-09-05 22:30:37 +0000 | [diff] [blame] | 962 | |
| 963 | // Don't check uninstantiated template destructors at all. We can only |
| 964 | // synthesize correct specs after the template is instantiated. |
| 965 | if (isa<CXXDestructorDecl>(New) && New->getParent()->isDependentType()) |
| 966 | return false; |
| 967 | |
| 968 | // If the old exception specification hasn't been parsed yet, or the new |
| 969 | // exception specification can't be computed yet, remember that we need to |
| 970 | // perform this check when we get to the end of the outermost |
Richard Smith | 88f4549 | 2014-11-22 03:09:05 +0000 | [diff] [blame] | 971 | // lexically-surrounding class. |
Richard Smith | 5159bbad | 2018-09-05 22:30:37 +0000 | [diff] [blame] | 972 | if (exceptionSpecNotKnownYet(Old) || exceptionSpecNotKnownYet(New)) { |
| 973 | DelayedOverridingExceptionSpecChecks.push_back({New, Old}); |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 974 | return false; |
Richard Smith | 88f4549 | 2014-11-22 03:09:05 +0000 | [diff] [blame] | 975 | } |
Richard Smith | 5159bbad | 2018-09-05 22:30:37 +0000 | [diff] [blame] | 976 | |
Francois Pichet | a8032e9 | 2011-05-24 02:11:43 +0000 | [diff] [blame] | 977 | unsigned DiagID = diag::err_override_exception_spec; |
Reid Kleckner | 39aa895 | 2019-08-27 17:52:03 +0000 | [diff] [blame] | 978 | if (getLangOpts().MSVCCompat) |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 979 | DiagID = diag::ext_override_exception_spec; |
Francois Pichet | a8032e9 | 2011-05-24 02:11:43 +0000 | [diff] [blame] | 980 | return CheckExceptionSpecSubset(PDiag(DiagID), |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 981 | PDiag(diag::err_deep_exception_specs_differ), |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 982 | PDiag(diag::note_overridden_virtual_function), |
Erich Keane | 81ef625 | 2019-06-03 18:36:26 +0000 | [diff] [blame] | 983 | PDiag(diag::ext_override_exception_spec), |
Simon Pilgrim | afb163f | 2019-10-21 18:28:31 +0000 | [diff] [blame] | 984 | Old->getType()->castAs<FunctionProtoType>(), |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 985 | Old->getLocation(), |
Simon Pilgrim | afb163f | 2019-10-21 18:28:31 +0000 | [diff] [blame] | 986 | New->getType()->castAs<FunctionProtoType>(), |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 987 | New->getLocation()); |
| 988 | } |
| 989 | |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 990 | static CanThrowResult canSubStmtsThrow(Sema &Self, const Stmt *S) { |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 991 | CanThrowResult R = CT_Cannot; |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 992 | for (const Stmt *SubStmt : S->children()) { |
| 993 | if (!SubStmt) |
| 994 | continue; |
| 995 | R = mergeCanThrow(R, Self.canThrow(SubStmt)); |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 996 | if (R == CT_Can) |
| 997 | break; |
| 998 | } |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 999 | return R; |
| 1000 | } |
| 1001 | |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1002 | /// Determine whether the callee of a particular function call can throw. |
| 1003 | /// E and D are both optional, but at least one of E and Loc must be specified. |
| 1004 | static CanThrowResult canCalleeThrow(Sema &S, const Expr *E, const Decl *D, |
| 1005 | SourceLocation Loc = SourceLocation()) { |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1006 | // As an extension, we assume that __attribute__((nothrow)) functions don't |
| 1007 | // throw. |
Richard Smith | 3a8f13a | 2016-12-03 00:29:06 +0000 | [diff] [blame] | 1008 | if (D && isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>()) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1009 | return CT_Cannot; |
| 1010 | |
Richard Smith | 3a8f13a | 2016-12-03 00:29:06 +0000 | [diff] [blame] | 1011 | QualType T; |
| 1012 | |
| 1013 | // In C++1z, just look at the function type of the callee. |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1014 | if (S.getLangOpts().CPlusPlus17 && E && isa<CallExpr>(E)) { |
Richard Smith | 3a8f13a | 2016-12-03 00:29:06 +0000 | [diff] [blame] | 1015 | E = cast<CallExpr>(E)->getCallee(); |
| 1016 | T = E->getType(); |
| 1017 | if (T->isSpecificPlaceholderType(BuiltinType::BoundMember)) { |
| 1018 | // Sadly we don't preserve the actual type as part of the "bound member" |
| 1019 | // placeholder, so we need to reconstruct it. |
| 1020 | E = E->IgnoreParenImpCasts(); |
| 1021 | |
| 1022 | // Could be a call to a pointer-to-member or a plain member access. |
| 1023 | if (auto *Op = dyn_cast<BinaryOperator>(E)) { |
| 1024 | assert(Op->getOpcode() == BO_PtrMemD || Op->getOpcode() == BO_PtrMemI); |
| 1025 | T = Op->getRHS()->getType() |
| 1026 | ->castAs<MemberPointerType>()->getPointeeType(); |
| 1027 | } else { |
| 1028 | T = cast<MemberExpr>(E)->getMemberDecl()->getType(); |
| 1029 | } |
| 1030 | } |
| 1031 | } else if (const ValueDecl *VD = dyn_cast_or_null<ValueDecl>(D)) |
| 1032 | T = VD->getType(); |
| 1033 | else |
| 1034 | // If we have no clue what we're calling, assume the worst. |
| 1035 | return CT_Can; |
| 1036 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1037 | const FunctionProtoType *FT; |
| 1038 | if ((FT = T->getAs<FunctionProtoType>())) { |
| 1039 | } else if (const PointerType *PT = T->getAs<PointerType>()) |
| 1040 | FT = PT->getPointeeType()->getAs<FunctionProtoType>(); |
| 1041 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 1042 | FT = RT->getPointeeType()->getAs<FunctionProtoType>(); |
| 1043 | else if (const MemberPointerType *MT = T->getAs<MemberPointerType>()) |
| 1044 | FT = MT->getPointeeType()->getAs<FunctionProtoType>(); |
| 1045 | else if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) |
| 1046 | FT = BT->getPointeeType()->getAs<FunctionProtoType>(); |
| 1047 | |
| 1048 | if (!FT) |
| 1049 | return CT_Can; |
| 1050 | |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1051 | FT = S.ResolveExceptionSpec(Loc.isInvalid() ? E->getBeginLoc() : Loc, FT); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1052 | if (!FT) |
| 1053 | return CT_Can; |
| 1054 | |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 1055 | return FT->canThrow(); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1058 | static CanThrowResult canVarDeclThrow(Sema &Self, const VarDecl *VD) { |
| 1059 | CanThrowResult CT = CT_Cannot; |
| 1060 | |
| 1061 | // Initialization might throw. |
| 1062 | if (!VD->isUsableInConstantExpressions(Self.Context)) |
| 1063 | if (const Expr *Init = VD->getInit()) |
| 1064 | CT = mergeCanThrow(CT, Self.canThrow(Init)); |
| 1065 | |
| 1066 | // Destructor might throw. |
| 1067 | if (VD->needsDestruction(Self.Context) == QualType::DK_cxx_destructor) { |
| 1068 | if (auto *RD = |
| 1069 | VD->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl()) { |
| 1070 | if (auto *Dtor = RD->getDestructor()) { |
| 1071 | CT = mergeCanThrow( |
| 1072 | CT, canCalleeThrow(Self, nullptr, Dtor, VD->getLocation())); |
| 1073 | } |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | // If this is a decomposition declaration, bindings might throw. |
| 1078 | if (auto *DD = dyn_cast<DecompositionDecl>(VD)) |
| 1079 | for (auto *B : DD->bindings()) |
| 1080 | if (auto *HD = B->getHoldingVar()) |
| 1081 | CT = mergeCanThrow(CT, canVarDeclThrow(Self, HD)); |
| 1082 | |
| 1083 | return CT; |
| 1084 | } |
| 1085 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1086 | static CanThrowResult canDynamicCastThrow(const CXXDynamicCastExpr *DC) { |
| 1087 | if (DC->isTypeDependent()) |
| 1088 | return CT_Dependent; |
| 1089 | |
| 1090 | if (!DC->getTypeAsWritten()->isReferenceType()) |
| 1091 | return CT_Cannot; |
| 1092 | |
| 1093 | if (DC->getSubExpr()->isTypeDependent()) |
| 1094 | return CT_Dependent; |
| 1095 | |
| 1096 | return DC->getCastKind() == clang::CK_Dynamic? CT_Can : CT_Cannot; |
| 1097 | } |
| 1098 | |
| 1099 | static CanThrowResult canTypeidThrow(Sema &S, const CXXTypeidExpr *DC) { |
| 1100 | if (DC->isTypeOperand()) |
| 1101 | return CT_Cannot; |
| 1102 | |
| 1103 | Expr *Op = DC->getExprOperand(); |
| 1104 | if (Op->isTypeDependent()) |
| 1105 | return CT_Dependent; |
| 1106 | |
| 1107 | const RecordType *RT = Op->getType()->getAs<RecordType>(); |
| 1108 | if (!RT) |
| 1109 | return CT_Cannot; |
| 1110 | |
| 1111 | if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic()) |
| 1112 | return CT_Cannot; |
| 1113 | |
| 1114 | if (Op->Classify(S.Context).isPRValue()) |
| 1115 | return CT_Cannot; |
| 1116 | |
| 1117 | return CT_Can; |
| 1118 | } |
| 1119 | |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1120 | CanThrowResult Sema::canThrow(const Stmt *S) { |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1121 | // C++ [expr.unary.noexcept]p3: |
| 1122 | // [Can throw] if in a potentially-evaluated context the expression would |
| 1123 | // contain: |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1124 | switch (S->getStmtClass()) { |
Bill Wendling | 7c44da2 | 2018-10-31 03:48:47 +0000 | [diff] [blame] | 1125 | case Expr::ConstantExprClass: |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1126 | return canThrow(cast<ConstantExpr>(S)->getSubExpr()); |
Bill Wendling | 7c44da2 | 2018-10-31 03:48:47 +0000 | [diff] [blame] | 1127 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1128 | case Expr::CXXThrowExprClass: |
| 1129 | // - a potentially evaluated throw-expression |
| 1130 | return CT_Can; |
| 1131 | |
| 1132 | case Expr::CXXDynamicCastExprClass: { |
| 1133 | // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v), |
| 1134 | // where T is a reference type, that requires a run-time check |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1135 | auto *CE = cast<CXXDynamicCastExpr>(S); |
| 1136 | // FIXME: Properly determine whether a variably-modified type can throw. |
| 1137 | if (CE->getType()->isVariablyModifiedType()) |
| 1138 | return CT_Can; |
| 1139 | CanThrowResult CT = canDynamicCastThrow(CE); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1140 | if (CT == CT_Can) |
| 1141 | return CT; |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1142 | return mergeCanThrow(CT, canSubStmtsThrow(*this, CE)); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | case Expr::CXXTypeidExprClass: |
| 1146 | // - a potentially evaluated typeid expression applied to a glvalue |
| 1147 | // expression whose type is a polymorphic class type |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1148 | return canTypeidThrow(*this, cast<CXXTypeidExpr>(S)); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1149 | |
| 1150 | // - a potentially evaluated call to a function, member function, function |
| 1151 | // pointer, or member function pointer that does not have a non-throwing |
| 1152 | // exception-specification |
| 1153 | case Expr::CallExprClass: |
| 1154 | case Expr::CXXMemberCallExprClass: |
| 1155 | case Expr::CXXOperatorCallExprClass: |
| 1156 | case Expr::UserDefinedLiteralClass: { |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1157 | const CallExpr *CE = cast<CallExpr>(S); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1158 | CanThrowResult CT; |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1159 | if (CE->isTypeDependent()) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1160 | CT = CT_Dependent; |
| 1161 | else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens())) |
| 1162 | CT = CT_Cannot; |
Eli Friedman | 5a8738f | 2013-06-25 01:55:41 +0000 | [diff] [blame] | 1163 | else |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1164 | CT = canCalleeThrow(*this, CE, CE->getCalleeDecl()); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1165 | if (CT == CT_Can) |
| 1166 | return CT; |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1167 | return mergeCanThrow(CT, canSubStmtsThrow(*this, CE)); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | case Expr::CXXConstructExprClass: |
| 1171 | case Expr::CXXTemporaryObjectExprClass: { |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1172 | auto *CE = cast<CXXConstructExpr>(S); |
| 1173 | // FIXME: Properly determine whether a variably-modified type can throw. |
| 1174 | if (CE->getType()->isVariablyModifiedType()) |
| 1175 | return CT_Can; |
| 1176 | CanThrowResult CT = canCalleeThrow(*this, CE, CE->getConstructor()); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1177 | if (CT == CT_Can) |
| 1178 | return CT; |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1179 | return mergeCanThrow(CT, canSubStmtsThrow(*this, CE)); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1182 | case Expr::CXXInheritedCtorInitExprClass: { |
| 1183 | auto *ICIE = cast<CXXInheritedCtorInitExpr>(S); |
| 1184 | return canCalleeThrow(*this, ICIE, ICIE->getConstructor()); |
| 1185 | } |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 1186 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1187 | case Expr::LambdaExprClass: { |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1188 | const LambdaExpr *Lambda = cast<LambdaExpr>(S); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1189 | CanThrowResult CT = CT_Cannot; |
James Y Knight | 53c7616 | 2015-07-17 18:21:37 +0000 | [diff] [blame] | 1190 | for (LambdaExpr::const_capture_init_iterator |
| 1191 | Cap = Lambda->capture_init_begin(), |
| 1192 | CapEnd = Lambda->capture_init_end(); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1193 | Cap != CapEnd; ++Cap) |
| 1194 | CT = mergeCanThrow(CT, canThrow(*Cap)); |
| 1195 | return CT; |
| 1196 | } |
| 1197 | |
| 1198 | case Expr::CXXNewExprClass: { |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1199 | auto *NE = cast<CXXNewExpr>(S); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1200 | CanThrowResult CT; |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1201 | if (NE->isTypeDependent()) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1202 | CT = CT_Dependent; |
| 1203 | else |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1204 | CT = canCalleeThrow(*this, NE, NE->getOperatorNew()); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1205 | if (CT == CT_Can) |
| 1206 | return CT; |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1207 | return mergeCanThrow(CT, canSubStmtsThrow(*this, NE)); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | case Expr::CXXDeleteExprClass: { |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1211 | auto *DE = cast<CXXDeleteExpr>(S); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1212 | CanThrowResult CT; |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1213 | QualType DTy = DE->getDestroyedType(); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1214 | if (DTy.isNull() || DTy->isDependentType()) { |
| 1215 | CT = CT_Dependent; |
| 1216 | } else { |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1217 | CT = canCalleeThrow(*this, DE, DE->getOperatorDelete()); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1218 | if (const RecordType *RT = DTy->getAs<RecordType>()) { |
| 1219 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Eli Friedman | 0423b76 | 2013-06-25 01:24:22 +0000 | [diff] [blame] | 1220 | const CXXDestructorDecl *DD = RD->getDestructor(); |
| 1221 | if (DD) |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1222 | CT = mergeCanThrow(CT, canCalleeThrow(*this, DE, DD)); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1223 | } |
| 1224 | if (CT == CT_Can) |
| 1225 | return CT; |
| 1226 | } |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1227 | return mergeCanThrow(CT, canSubStmtsThrow(*this, DE)); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | case Expr::CXXBindTemporaryExprClass: { |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1231 | auto *BTE = cast<CXXBindTemporaryExpr>(S); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1232 | // The bound temporary has to be destroyed again, which might throw. |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1233 | CanThrowResult CT = |
| 1234 | canCalleeThrow(*this, BTE, BTE->getTemporary()->getDestructor()); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1235 | if (CT == CT_Can) |
| 1236 | return CT; |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1237 | return mergeCanThrow(CT, canSubStmtsThrow(*this, BTE)); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
Richard Smith | 4a4e90a | 2019-12-13 14:11:04 -0800 | [diff] [blame] | 1240 | case Expr::PseudoObjectExprClass: { |
| 1241 | auto *POE = cast<PseudoObjectExpr>(S); |
| 1242 | CanThrowResult CT = CT_Cannot; |
| 1243 | for (const Expr *E : POE->semantics()) { |
| 1244 | CT = mergeCanThrow(CT, canThrow(E)); |
| 1245 | if (CT == CT_Can) |
| 1246 | break; |
| 1247 | } |
| 1248 | return CT; |
| 1249 | } |
| 1250 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1251 | // ObjC message sends are like function calls, but never have exception |
| 1252 | // specs. |
| 1253 | case Expr::ObjCMessageExprClass: |
| 1254 | case Expr::ObjCPropertyRefExprClass: |
| 1255 | case Expr::ObjCSubscriptRefExprClass: |
| 1256 | return CT_Can; |
| 1257 | |
| 1258 | // All the ObjC literals that are implemented as calls are |
| 1259 | // potentially throwing unless we decide to close off that |
| 1260 | // possibility. |
| 1261 | case Expr::ObjCArrayLiteralClass: |
| 1262 | case Expr::ObjCDictionaryLiteralClass: |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 1263 | case Expr::ObjCBoxedExprClass: |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1264 | return CT_Can; |
| 1265 | |
| 1266 | // Many other things have subexpressions, so we have to test those. |
| 1267 | // Some are simple: |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 1268 | case Expr::CoawaitExprClass: |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1269 | case Expr::ConditionalOperatorClass: |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 1270 | case Expr::CoyieldExprClass: |
Richard Smith | 778dc0f | 2019-10-19 00:04:38 +0000 | [diff] [blame] | 1271 | case Expr::CXXRewrittenBinaryOperatorClass: |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 1272 | case Expr::CXXStdInitializerListExprClass: |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1273 | case Expr::DesignatedInitExprClass: |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1274 | case Expr::DesignatedInitUpdateExprClass: |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1275 | case Expr::ExprWithCleanupsClass: |
| 1276 | case Expr::ExtVectorElementExprClass: |
| 1277 | case Expr::InitListExprClass: |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 1278 | case Expr::ArrayInitLoopExprClass: |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1279 | case Expr::MemberExprClass: |
| 1280 | case Expr::ObjCIsaExprClass: |
| 1281 | case Expr::ObjCIvarRefExprClass: |
| 1282 | case Expr::ParenExprClass: |
| 1283 | case Expr::ParenListExprClass: |
| 1284 | case Expr::ShuffleVectorExprClass: |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1285 | case Expr::StmtExprClass: |
Hal Finkel | c4d7c82 | 2013-09-18 03:29:45 +0000 | [diff] [blame] | 1286 | case Expr::ConvertVectorExprClass: |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1287 | case Expr::VAArgExprClass: |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1288 | return canSubStmtsThrow(*this, S); |
| 1289 | |
| 1290 | case Expr::CompoundLiteralExprClass: |
| 1291 | case Expr::CXXConstCastExprClass: |
| 1292 | case Expr::CXXReinterpretCastExprClass: |
| 1293 | case Expr::BuiltinBitCastExprClass: |
| 1294 | // FIXME: Properly determine whether a variably-modified type can throw. |
| 1295 | if (cast<Expr>(S)->getType()->isVariablyModifiedType()) |
| 1296 | return CT_Can; |
| 1297 | return canSubStmtsThrow(*this, S); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1298 | |
| 1299 | // Some might be dependent for other reasons. |
| 1300 | case Expr::ArraySubscriptExprClass: |
Alexey Bataev | 1a3320e | 2015-08-25 14:24:04 +0000 | [diff] [blame] | 1301 | case Expr::OMPArraySectionExprClass: |
Alexey Bataev | 7ac9efb | 2020-02-05 09:33:05 -0500 | [diff] [blame] | 1302 | case Expr::OMPArrayShapingExprClass: |
Alexey Bataev | 13a1504 | 2020-04-01 15:06:38 -0400 | [diff] [blame] | 1303 | case Expr::OMPIteratorExprClass: |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1304 | case Expr::BinaryOperatorClass: |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 1305 | case Expr::DependentCoawaitExprClass: |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1306 | case Expr::CompoundAssignOperatorClass: |
| 1307 | case Expr::CStyleCastExprClass: |
| 1308 | case Expr::CXXStaticCastExprClass: |
| 1309 | case Expr::CXXFunctionalCastExprClass: |
| 1310 | case Expr::ImplicitCastExprClass: |
| 1311 | case Expr::MaterializeTemporaryExprClass: |
| 1312 | case Expr::UnaryOperatorClass: { |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1313 | // FIXME: Properly determine whether a variably-modified type can throw. |
| 1314 | if (auto *CE = dyn_cast<CastExpr>(S)) |
| 1315 | if (CE->getType()->isVariablyModifiedType()) |
| 1316 | return CT_Can; |
| 1317 | CanThrowResult CT = |
| 1318 | cast<Expr>(S)->isTypeDependent() ? CT_Dependent : CT_Cannot; |
| 1319 | return mergeCanThrow(CT, canSubStmtsThrow(*this, S)); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1322 | case Expr::CXXDefaultArgExprClass: |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1323 | return canThrow(cast<CXXDefaultArgExpr>(S)->getExpr()); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1324 | |
| 1325 | case Expr::CXXDefaultInitExprClass: |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1326 | return canThrow(cast<CXXDefaultInitExpr>(S)->getExpr()); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1327 | |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1328 | case Expr::ChooseExprClass: { |
| 1329 | auto *CE = cast<ChooseExpr>(S); |
| 1330 | if (CE->isTypeDependent() || CE->isValueDependent()) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1331 | return CT_Dependent; |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1332 | return canThrow(CE->getChosenSubExpr()); |
| 1333 | } |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1334 | |
| 1335 | case Expr::GenericSelectionExprClass: |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1336 | if (cast<GenericSelectionExpr>(S)->isResultDependent()) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1337 | return CT_Dependent; |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1338 | return canThrow(cast<GenericSelectionExpr>(S)->getResultExpr()); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1339 | |
| 1340 | // Some expressions are always dependent. |
| 1341 | case Expr::CXXDependentScopeMemberExprClass: |
| 1342 | case Expr::CXXUnresolvedConstructExprClass: |
| 1343 | case Expr::DependentScopeDeclRefExprClass: |
Richard Smith | 0f0af19 | 2014-11-08 05:07:16 +0000 | [diff] [blame] | 1344 | case Expr::CXXFoldExprClass: |
Haojian Wu | 733edf9 | 2020-03-19 16:30:40 +0100 | [diff] [blame] | 1345 | case Expr::RecoveryExprClass: |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1346 | return CT_Dependent; |
| 1347 | |
| 1348 | case Expr::AsTypeExprClass: |
| 1349 | case Expr::BinaryConditionalOperatorClass: |
| 1350 | case Expr::BlockExprClass: |
| 1351 | case Expr::CUDAKernelCallExprClass: |
| 1352 | case Expr::DeclRefExprClass: |
| 1353 | case Expr::ObjCBridgedCastExprClass: |
| 1354 | case Expr::ObjCIndirectCopyRestoreExprClass: |
| 1355 | case Expr::ObjCProtocolExprClass: |
| 1356 | case Expr::ObjCSelectorExprClass: |
Erik Pilkington | 29099de | 2016-07-16 00:35:23 +0000 | [diff] [blame] | 1357 | case Expr::ObjCAvailabilityCheckExprClass: |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1358 | case Expr::OffsetOfExprClass: |
| 1359 | case Expr::PackExpansionExprClass: |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1360 | case Expr::SubstNonTypeTemplateParmExprClass: |
| 1361 | case Expr::SubstNonTypeTemplateParmPackExprClass: |
Richard Smith | b15fe3a | 2012-09-12 00:56:43 +0000 | [diff] [blame] | 1362 | case Expr::FunctionParmPackExprClass: |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1363 | case Expr::UnaryExprOrTypeTraitExprClass: |
| 1364 | case Expr::UnresolvedLookupExprClass: |
| 1365 | case Expr::UnresolvedMemberExprClass: |
Kaelyn Takata | e1f49d5 | 2014-10-27 18:07:20 +0000 | [diff] [blame] | 1366 | case Expr::TypoExprClass: |
Richard Smith | 4a4e90a | 2019-12-13 14:11:04 -0800 | [diff] [blame] | 1367 | // FIXME: Many of the above can throw. |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1368 | return CT_Cannot; |
| 1369 | |
| 1370 | case Expr::AddrLabelExprClass: |
| 1371 | case Expr::ArrayTypeTraitExprClass: |
| 1372 | case Expr::AtomicExprClass: |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1373 | case Expr::TypeTraitExprClass: |
| 1374 | case Expr::CXXBoolLiteralExprClass: |
| 1375 | case Expr::CXXNoexceptExprClass: |
| 1376 | case Expr::CXXNullPtrLiteralExprClass: |
| 1377 | case Expr::CXXPseudoDestructorExprClass: |
| 1378 | case Expr::CXXScalarValueInitExprClass: |
| 1379 | case Expr::CXXThisExprClass: |
| 1380 | case Expr::CXXUuidofExprClass: |
| 1381 | case Expr::CharacterLiteralClass: |
| 1382 | case Expr::ExpressionTraitExprClass: |
| 1383 | case Expr::FloatingLiteralClass: |
| 1384 | case Expr::GNUNullExprClass: |
| 1385 | case Expr::ImaginaryLiteralClass: |
| 1386 | case Expr::ImplicitValueInitExprClass: |
| 1387 | case Expr::IntegerLiteralClass: |
Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 1388 | case Expr::FixedPointLiteralClass: |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 1389 | case Expr::ArrayInitIndexExprClass: |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 1390 | case Expr::NoInitExprClass: |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1391 | case Expr::ObjCEncodeExprClass: |
| 1392 | case Expr::ObjCStringLiteralClass: |
| 1393 | case Expr::ObjCBoolLiteralExprClass: |
| 1394 | case Expr::OpaqueValueExprClass: |
| 1395 | case Expr::PredefinedExprClass: |
| 1396 | case Expr::SizeOfPackExprClass: |
| 1397 | case Expr::StringLiteralClass: |
Eric Fiselier | 708afb5 | 2019-05-16 21:04:15 +0000 | [diff] [blame] | 1398 | case Expr::SourceLocExprClass: |
Saar Raz | 5d98ba6 | 2019-10-15 15:24:26 +0000 | [diff] [blame] | 1399 | case Expr::ConceptSpecializationExprClass: |
Saar Raz | a0f50d7 | 2020-01-18 09:11:43 +0200 | [diff] [blame] | 1400 | case Expr::RequiresExprClass: |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1401 | // These expressions can never throw. |
| 1402 | return CT_Cannot; |
| 1403 | |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 1404 | case Expr::MSPropertyRefExprClass: |
Alexey Bataev | f763027 | 2015-11-25 12:01:00 +0000 | [diff] [blame] | 1405 | case Expr::MSPropertySubscriptExprClass: |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 1406 | llvm_unreachable("Invalid class for expression"); |
| 1407 | |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1408 | // Most statements can throw if any substatement can throw. |
| 1409 | case Stmt::AttributedStmtClass: |
| 1410 | case Stmt::BreakStmtClass: |
| 1411 | case Stmt::CapturedStmtClass: |
| 1412 | case Stmt::CaseStmtClass: |
| 1413 | case Stmt::CompoundStmtClass: |
| 1414 | case Stmt::ContinueStmtClass: |
| 1415 | case Stmt::CoreturnStmtClass: |
| 1416 | case Stmt::CoroutineBodyStmtClass: |
| 1417 | case Stmt::CXXCatchStmtClass: |
| 1418 | case Stmt::CXXForRangeStmtClass: |
| 1419 | case Stmt::DefaultStmtClass: |
| 1420 | case Stmt::DoStmtClass: |
| 1421 | case Stmt::ForStmtClass: |
| 1422 | case Stmt::GCCAsmStmtClass: |
| 1423 | case Stmt::GotoStmtClass: |
| 1424 | case Stmt::IndirectGotoStmtClass: |
| 1425 | case Stmt::LabelStmtClass: |
| 1426 | case Stmt::MSAsmStmtClass: |
| 1427 | case Stmt::MSDependentExistsStmtClass: |
| 1428 | case Stmt::NullStmtClass: |
| 1429 | case Stmt::ObjCAtCatchStmtClass: |
| 1430 | case Stmt::ObjCAtFinallyStmtClass: |
| 1431 | case Stmt::ObjCAtSynchronizedStmtClass: |
| 1432 | case Stmt::ObjCAutoreleasePoolStmtClass: |
| 1433 | case Stmt::ObjCForCollectionStmtClass: |
| 1434 | case Stmt::OMPAtomicDirectiveClass: |
| 1435 | case Stmt::OMPBarrierDirectiveClass: |
| 1436 | case Stmt::OMPCancelDirectiveClass: |
| 1437 | case Stmt::OMPCancellationPointDirectiveClass: |
| 1438 | case Stmt::OMPCriticalDirectiveClass: |
| 1439 | case Stmt::OMPDistributeDirectiveClass: |
| 1440 | case Stmt::OMPDistributeParallelForDirectiveClass: |
| 1441 | case Stmt::OMPDistributeParallelForSimdDirectiveClass: |
| 1442 | case Stmt::OMPDistributeSimdDirectiveClass: |
| 1443 | case Stmt::OMPFlushDirectiveClass: |
Alexey Bataev | c112e94 | 2020-02-28 09:52:15 -0500 | [diff] [blame] | 1444 | case Stmt::OMPDepobjDirectiveClass: |
Alexey Bataev | fcba7c3 | 2020-03-20 07:03:01 -0400 | [diff] [blame] | 1445 | case Stmt::OMPScanDirectiveClass: |
Richard Smith | fbf60b7 | 2019-12-13 14:10:13 -0800 | [diff] [blame] | 1446 | case Stmt::OMPForDirectiveClass: |
| 1447 | case Stmt::OMPForSimdDirectiveClass: |
| 1448 | case Stmt::OMPMasterDirectiveClass: |
| 1449 | case Stmt::OMPMasterTaskLoopDirectiveClass: |
| 1450 | case Stmt::OMPMasterTaskLoopSimdDirectiveClass: |
| 1451 | case Stmt::OMPOrderedDirectiveClass: |
| 1452 | case Stmt::OMPParallelDirectiveClass: |
| 1453 | case Stmt::OMPParallelForDirectiveClass: |
| 1454 | case Stmt::OMPParallelForSimdDirectiveClass: |
| 1455 | case Stmt::OMPParallelMasterDirectiveClass: |
| 1456 | case Stmt::OMPParallelMasterTaskLoopDirectiveClass: |
| 1457 | case Stmt::OMPParallelMasterTaskLoopSimdDirectiveClass: |
| 1458 | case Stmt::OMPParallelSectionsDirectiveClass: |
| 1459 | case Stmt::OMPSectionDirectiveClass: |
| 1460 | case Stmt::OMPSectionsDirectiveClass: |
| 1461 | case Stmt::OMPSimdDirectiveClass: |
| 1462 | case Stmt::OMPSingleDirectiveClass: |
| 1463 | case Stmt::OMPTargetDataDirectiveClass: |
| 1464 | case Stmt::OMPTargetDirectiveClass: |
| 1465 | case Stmt::OMPTargetEnterDataDirectiveClass: |
| 1466 | case Stmt::OMPTargetExitDataDirectiveClass: |
| 1467 | case Stmt::OMPTargetParallelDirectiveClass: |
| 1468 | case Stmt::OMPTargetParallelForDirectiveClass: |
| 1469 | case Stmt::OMPTargetParallelForSimdDirectiveClass: |
| 1470 | case Stmt::OMPTargetSimdDirectiveClass: |
| 1471 | case Stmt::OMPTargetTeamsDirectiveClass: |
| 1472 | case Stmt::OMPTargetTeamsDistributeDirectiveClass: |
| 1473 | case Stmt::OMPTargetTeamsDistributeParallelForDirectiveClass: |
| 1474 | case Stmt::OMPTargetTeamsDistributeParallelForSimdDirectiveClass: |
| 1475 | case Stmt::OMPTargetTeamsDistributeSimdDirectiveClass: |
| 1476 | case Stmt::OMPTargetUpdateDirectiveClass: |
| 1477 | case Stmt::OMPTaskDirectiveClass: |
| 1478 | case Stmt::OMPTaskgroupDirectiveClass: |
| 1479 | case Stmt::OMPTaskLoopDirectiveClass: |
| 1480 | case Stmt::OMPTaskLoopSimdDirectiveClass: |
| 1481 | case Stmt::OMPTaskwaitDirectiveClass: |
| 1482 | case Stmt::OMPTaskyieldDirectiveClass: |
| 1483 | case Stmt::OMPTeamsDirectiveClass: |
| 1484 | case Stmt::OMPTeamsDistributeDirectiveClass: |
| 1485 | case Stmt::OMPTeamsDistributeParallelForDirectiveClass: |
| 1486 | case Stmt::OMPTeamsDistributeParallelForSimdDirectiveClass: |
| 1487 | case Stmt::OMPTeamsDistributeSimdDirectiveClass: |
| 1488 | case Stmt::ReturnStmtClass: |
| 1489 | case Stmt::SEHExceptStmtClass: |
| 1490 | case Stmt::SEHFinallyStmtClass: |
| 1491 | case Stmt::SEHLeaveStmtClass: |
| 1492 | case Stmt::SEHTryStmtClass: |
| 1493 | case Stmt::SwitchStmtClass: |
| 1494 | case Stmt::WhileStmtClass: |
| 1495 | return canSubStmtsThrow(*this, S); |
| 1496 | |
| 1497 | case Stmt::DeclStmtClass: { |
| 1498 | CanThrowResult CT = CT_Cannot; |
| 1499 | for (const Decl *D : cast<DeclStmt>(S)->decls()) { |
| 1500 | if (auto *VD = dyn_cast<VarDecl>(D)) |
| 1501 | CT = mergeCanThrow(CT, canVarDeclThrow(*this, VD)); |
| 1502 | |
| 1503 | // FIXME: Properly determine whether a variably-modified type can throw. |
| 1504 | if (auto *TND = dyn_cast<TypedefNameDecl>(D)) |
| 1505 | if (TND->getUnderlyingType()->isVariablyModifiedType()) |
| 1506 | return CT_Can; |
| 1507 | if (auto *VD = dyn_cast<ValueDecl>(D)) |
| 1508 | if (VD->getType()->isVariablyModifiedType()) |
| 1509 | return CT_Can; |
| 1510 | } |
| 1511 | return CT; |
| 1512 | } |
| 1513 | |
| 1514 | case Stmt::IfStmtClass: { |
| 1515 | auto *IS = cast<IfStmt>(S); |
| 1516 | CanThrowResult CT = CT_Cannot; |
| 1517 | if (const Stmt *Init = IS->getInit()) |
| 1518 | CT = mergeCanThrow(CT, canThrow(Init)); |
| 1519 | if (const Stmt *CondDS = IS->getConditionVariableDeclStmt()) |
| 1520 | CT = mergeCanThrow(CT, canThrow(CondDS)); |
| 1521 | CT = mergeCanThrow(CT, canThrow(IS->getCond())); |
| 1522 | |
| 1523 | // For 'if constexpr', consider only the non-discarded case. |
| 1524 | // FIXME: We should add a DiscardedStmt marker to the AST. |
| 1525 | if (Optional<const Stmt *> Case = IS->getNondiscardedCase(Context)) |
| 1526 | return *Case ? mergeCanThrow(CT, canThrow(*Case)) : CT; |
| 1527 | |
| 1528 | CanThrowResult Then = canThrow(IS->getThen()); |
| 1529 | CanThrowResult Else = IS->getElse() ? canThrow(IS->getElse()) : CT_Cannot; |
| 1530 | if (Then == Else) |
| 1531 | return mergeCanThrow(CT, Then); |
| 1532 | |
| 1533 | // For a dependent 'if constexpr', the result is dependent if it depends on |
| 1534 | // the value of the condition. |
| 1535 | return mergeCanThrow(CT, IS->isConstexpr() ? CT_Dependent |
| 1536 | : mergeCanThrow(Then, Else)); |
| 1537 | } |
| 1538 | |
| 1539 | case Stmt::CXXTryStmtClass: { |
| 1540 | auto *TS = cast<CXXTryStmt>(S); |
| 1541 | // try /*...*/ catch (...) { H } can throw only if H can throw. |
| 1542 | // Any other try-catch can throw if any substatement can throw. |
| 1543 | const CXXCatchStmt *FinalHandler = TS->getHandler(TS->getNumHandlers() - 1); |
| 1544 | if (!FinalHandler->getExceptionDecl()) |
| 1545 | return canThrow(FinalHandler->getHandlerBlock()); |
| 1546 | return canSubStmtsThrow(*this, S); |
| 1547 | } |
| 1548 | |
| 1549 | case Stmt::ObjCAtThrowStmtClass: |
| 1550 | return CT_Can; |
| 1551 | |
| 1552 | case Stmt::ObjCAtTryStmtClass: { |
| 1553 | auto *TS = cast<ObjCAtTryStmt>(S); |
| 1554 | |
| 1555 | // @catch(...) need not be last in Objective-C. Walk backwards until we |
| 1556 | // see one or hit the @try. |
| 1557 | CanThrowResult CT = CT_Cannot; |
| 1558 | if (const Stmt *Finally = TS->getFinallyStmt()) |
| 1559 | CT = mergeCanThrow(CT, canThrow(Finally)); |
| 1560 | for (unsigned I = TS->getNumCatchStmts(); I != 0; --I) { |
| 1561 | const ObjCAtCatchStmt *Catch = TS->getCatchStmt(I - 1); |
| 1562 | CT = mergeCanThrow(CT, canThrow(Catch)); |
| 1563 | // If we reach a @catch(...), no earlier exceptions can escape. |
| 1564 | if (Catch->hasEllipsis()) |
| 1565 | return CT; |
| 1566 | } |
| 1567 | |
| 1568 | // Didn't find an @catch(...). Exceptions from the @try body can escape. |
| 1569 | return mergeCanThrow(CT, canThrow(TS->getTryBody())); |
| 1570 | } |
| 1571 | |
| 1572 | case Stmt::NoStmtClass: |
| 1573 | llvm_unreachable("Invalid class for statement"); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 1574 | } |
| 1575 | llvm_unreachable("Bogus StmtClass"); |
| 1576 | } |
| 1577 | |
Sebastian Redl | 4915e63 | 2009-10-11 09:03:14 +0000 | [diff] [blame] | 1578 | } // end namespace clang |