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