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