blob: deb6cbb53aff59b78fd8c01e54014b995f2b84db [file] [log] [blame]
Sebastian Redl4915e632009-10-11 09:03:14 +00001//===--- 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 McCall83024632010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Richard Smith564417a2014-03-20 21:47:22 +000015#include "clang/AST/ASTMutationListener.h"
Sebastian Redl4915e632009-10-11 09:03:14 +000016#include "clang/AST/CXXInheritance.h"
17#include "clang/AST/Expr.h"
18#include "clang/AST/ExprCXX.h"
Douglas Gregord6bc5e62010-03-24 07:14:45 +000019#include "clang/AST/TypeLoc.h"
Douglas Gregorf40863c2010-02-12 07:32:17 +000020#include "clang/Basic/Diagnostic.h"
21#include "clang/Basic/SourceManager.h"
Sebastian Redl4915e632009-10-11 09:03:14 +000022#include "llvm/ADT/SmallPtrSet.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000023#include "llvm/ADT/SmallString.h"
Sebastian Redl4915e632009-10-11 09:03:14 +000024
25namespace clang {
26
27static 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 Redl075b21d2009-10-14 14:38:54 +000033 else if (const MemberPointerType *MPTy = T->getAs<MemberPointerType>())
34 T = MPTy->getPointeeType();
Sebastian Redl4915e632009-10-11 09:03:14 +000035 return T->getAs<FunctionProtoType>();
36}
37
Richard Smith6403e932014-11-14 00:37:55 +000038/// 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.
42bool Sema::isLibstdcxxEagerExceptionSpecHack(const Declarator &D) {
43 auto *RD = dyn_cast<CXXRecordDecl>(CurContext);
44
45 // All the problem cases are member functions named "swap" within class
Richard Smith62895462016-10-19 23:47:37 +000046 // templates declared directly within namespace std or std::__debug or
47 // std::__profile.
48 if (!RD || !RD->getIdentifier() || !RD->getDescribedClassTemplate() ||
Richard Smith6403e932014-11-14 00:37:55 +000049 !D.getIdentifier() || !D.getIdentifier()->isStr("swap"))
50 return false;
51
Richard Smith62895462016-10-19 23:47:37 +000052 auto *ND = dyn_cast<NamespaceDecl>(RD->getDeclContext());
53 if (!ND)
54 return false;
55
56 bool IsInStd = ND->isStdNamespace();
57 if (!IsInStd) {
58 // This isn't a direct member of namespace std, but it might still be
59 // libstdc++'s std::__debug::array or std::__profile::array.
60 IdentifierInfo *II = ND->getIdentifier();
61 if (!II || !(II->isStr("__debug") || II->isStr("__profile")) ||
62 !ND->isInStdNamespace())
63 return false;
64 }
65
Richard Smith6403e932014-11-14 00:37:55 +000066 // Only apply this hack within a system header.
67 if (!Context.getSourceManager().isInSystemHeader(D.getLocStart()))
68 return false;
69
70 return llvm::StringSwitch<bool>(RD->getIdentifier()->getName())
71 .Case("array", true)
Richard Smith62895462016-10-19 23:47:37 +000072 .Case("pair", IsInStd)
73 .Case("priority_queue", IsInStd)
74 .Case("stack", IsInStd)
75 .Case("queue", IsInStd)
Richard Smith6403e932014-11-14 00:37:55 +000076 .Default(false);
77}
78
Sebastian Redl4915e632009-10-11 09:03:14 +000079/// CheckSpecifiedExceptionType - Check if the given type is valid in an
80/// exception specification. Incomplete types, or pointers to incomplete types
81/// other than void are not allowed.
Richard Smith8606d752012-11-28 22:33:28 +000082///
83/// \param[in,out] T The exception type. This will be decayed to a pointer type
84/// when the input is an array or a function type.
Craig Toppere335f252015-10-04 04:53:55 +000085bool Sema::CheckSpecifiedExceptionType(QualType &T, SourceRange Range) {
Richard Smitha118c6a2012-11-28 22:52:42 +000086 // C++11 [except.spec]p2:
87 // A type cv T, "array of T", or "function returning T" denoted
Richard Smith8606d752012-11-28 22:33:28 +000088 // in an exception-specification is adjusted to type T, "pointer to T", or
89 // "pointer to function returning T", respectively.
Richard Smitha118c6a2012-11-28 22:52:42 +000090 //
91 // We also apply this rule in C++98.
Richard Smith8606d752012-11-28 22:33:28 +000092 if (T->isArrayType())
93 T = Context.getArrayDecayedType(T);
94 else if (T->isFunctionType())
95 T = Context.getPointerType(T);
Sebastian Redl4915e632009-10-11 09:03:14 +000096
Richard Smitha118c6a2012-11-28 22:52:42 +000097 int Kind = 0;
Richard Smith8606d752012-11-28 22:33:28 +000098 QualType PointeeT = T;
Richard Smitha118c6a2012-11-28 22:52:42 +000099 if (const PointerType *PT = T->getAs<PointerType>()) {
100 PointeeT = PT->getPointeeType();
101 Kind = 1;
Sebastian Redl4915e632009-10-11 09:03:14 +0000102
Richard Smitha118c6a2012-11-28 22:52:42 +0000103 // cv void* is explicitly permitted, despite being a pointer to an
104 // incomplete type.
105 if (PointeeT->isVoidType())
106 return false;
107 } else if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
108 PointeeT = RT->getPointeeType();
109 Kind = 2;
Richard Smith8606d752012-11-28 22:33:28 +0000110
Richard Smitha118c6a2012-11-28 22:52:42 +0000111 if (RT->isRValueReferenceType()) {
112 // C++11 [except.spec]p2:
113 // A type denoted in an exception-specification shall not denote [...]
114 // an rvalue reference type.
115 Diag(Range.getBegin(), diag::err_rref_in_exception_spec)
116 << T << Range;
117 return true;
118 }
119 }
120
121 // C++11 [except.spec]p2:
122 // A type denoted in an exception-specification shall not denote an
123 // incomplete type other than a class currently being defined [...].
124 // A type denoted in an exception-specification shall not denote a
125 // pointer or reference to an incomplete type, other than (cv) void* or a
126 // pointer or reference to a class currently being defined.
David Majnemerb2b0da42016-06-10 18:24:41 +0000127 // In Microsoft mode, downgrade this to a warning.
128 unsigned DiagID = diag::err_incomplete_in_exception_spec;
David Majnemer5d321e62016-06-11 01:25:04 +0000129 bool ReturnValueOnError = true;
130 if (getLangOpts().MicrosoftExt) {
David Majnemerb2b0da42016-06-10 18:24:41 +0000131 DiagID = diag::ext_incomplete_in_exception_spec;
David Majnemer5d321e62016-06-11 01:25:04 +0000132 ReturnValueOnError = false;
133 }
Richard Smitha118c6a2012-11-28 22:52:42 +0000134 if (!(PointeeT->isRecordType() &&
135 PointeeT->getAs<RecordType>()->isBeingDefined()) &&
David Majnemerb2b0da42016-06-10 18:24:41 +0000136 RequireCompleteType(Range.getBegin(), PointeeT, DiagID, Kind, Range))
David Majnemer5d321e62016-06-11 01:25:04 +0000137 return ReturnValueOnError;
Sebastian Redl4915e632009-10-11 09:03:14 +0000138
139 return false;
140}
141
142/// CheckDistantExceptionSpec - Check if the given type is a pointer or pointer
143/// to member to a function with an exception specification. This means that
144/// it is invalid to add another level of indirection.
145bool Sema::CheckDistantExceptionSpec(QualType T) {
Richard Smith3c4f8d22016-10-16 17:54:23 +0000146 // C++17 removes this rule in favor of putting exception specifications into
147 // the type system.
148 if (getLangOpts().CPlusPlus1z)
149 return false;
150
Sebastian Redl4915e632009-10-11 09:03:14 +0000151 if (const PointerType *PT = T->getAs<PointerType>())
152 T = PT->getPointeeType();
153 else if (const MemberPointerType *PT = T->getAs<MemberPointerType>())
154 T = PT->getPointeeType();
155 else
156 return false;
157
158 const FunctionProtoType *FnT = T->getAs<FunctionProtoType>();
159 if (!FnT)
160 return false;
161
162 return FnT->hasExceptionSpec();
163}
164
Richard Smithf623c962012-04-17 00:58:00 +0000165const FunctionProtoType *
166Sema::ResolveExceptionSpec(SourceLocation Loc, const FunctionProtoType *FPT) {
Richard Smith0b3a4622014-11-13 20:01:57 +0000167 if (FPT->getExceptionSpecType() == EST_Unparsed) {
168 Diag(Loc, diag::err_exception_spec_not_parsed);
169 return nullptr;
170 }
171
Richard Smithd3b5c9082012-07-27 04:22:15 +0000172 if (!isUnresolvedExceptionSpec(FPT->getExceptionSpecType()))
Richard Smithf623c962012-04-17 00:58:00 +0000173 return FPT;
174
175 FunctionDecl *SourceDecl = FPT->getExceptionSpecDecl();
176 const FunctionProtoType *SourceFPT =
177 SourceDecl->getType()->castAs<FunctionProtoType>();
178
Richard Smithd3b5c9082012-07-27 04:22:15 +0000179 // If the exception specification has already been resolved, just return it.
180 if (!isUnresolvedExceptionSpec(SourceFPT->getExceptionSpecType()))
Richard Smithf623c962012-04-17 00:58:00 +0000181 return SourceFPT;
182
Richard Smithd3b5c9082012-07-27 04:22:15 +0000183 // Compute or instantiate the exception specification now.
Richard Smith3901dfe2013-03-27 00:22:47 +0000184 if (SourceFPT->getExceptionSpecType() == EST_Unevaluated)
Richard Smithd3b5c9082012-07-27 04:22:15 +0000185 EvaluateImplicitExceptionSpec(Loc, cast<CXXMethodDecl>(SourceDecl));
186 else
187 InstantiateExceptionSpec(Loc, SourceDecl);
Richard Smithf623c962012-04-17 00:58:00 +0000188
Davide Italiano922b7022015-07-25 01:19:32 +0000189 const FunctionProtoType *Proto =
190 SourceDecl->getType()->castAs<FunctionProtoType>();
191 if (Proto->getExceptionSpecType() == clang::EST_Unparsed) {
192 Diag(Loc, diag::err_exception_spec_not_parsed);
193 Proto = nullptr;
194 }
195 return Proto;
Richard Smithf623c962012-04-17 00:58:00 +0000196}
197
Richard Smith8acb4282014-07-31 21:57:55 +0000198void
199Sema::UpdateExceptionSpec(FunctionDecl *FD,
200 const FunctionProtoType::ExceptionSpecInfo &ESI) {
Richard Smith564417a2014-03-20 21:47:22 +0000201 // If we've fully resolved the exception specification, notify listeners.
Richard Smith8acb4282014-07-31 21:57:55 +0000202 if (!isUnresolvedExceptionSpec(ESI.Type))
Richard Smith564417a2014-03-20 21:47:22 +0000203 if (auto *Listener = getASTMutationListener())
204 Listener->ResolvedExceptionSpec(FD);
Richard Smith9e2341d2015-03-23 03:25:59 +0000205
206 for (auto *Redecl : FD->redecls())
207 Context.adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
Richard Smith564417a2014-03-20 21:47:22 +0000208}
209
Richard Smith13b40bc2016-11-30 00:13:55 +0000210static bool CheckEquivalentExceptionSpecImpl(
211 Sema &S, const PartialDiagnostic &DiagID, const PartialDiagnostic &NoteID,
212 const FunctionProtoType *Old, SourceLocation OldLoc,
213 const FunctionProtoType *New, SourceLocation NewLoc,
214 bool *MissingExceptionSpecification = nullptr,
215 bool *MissingEmptyExceptionSpecification = nullptr,
216 bool AllowNoexceptAllMatchWithNoSpec = false, bool IsOperatorNew = false);
217
Richard Smith66f3ac92012-10-20 08:26:51 +0000218/// Determine whether a function has an implicitly-generated exception
Richard Smith1ee63522012-10-16 23:30:16 +0000219/// specification.
Richard Smith66f3ac92012-10-20 08:26:51 +0000220static bool hasImplicitExceptionSpec(FunctionDecl *Decl) {
221 if (!isa<CXXDestructorDecl>(Decl) &&
222 Decl->getDeclName().getCXXOverloadedOperator() != OO_Delete &&
223 Decl->getDeclName().getCXXOverloadedOperator() != OO_Array_Delete)
224 return false;
Richard Smith1ee63522012-10-16 23:30:16 +0000225
Richard Smithc7fb2252014-02-07 22:51:16 +0000226 // For a function that the user didn't declare:
227 // - if this is a destructor, its exception specification is implicit.
228 // - if this is 'operator delete' or 'operator delete[]', the exception
229 // specification is as-if an explicit exception specification was given
230 // (per [basic.stc.dynamic]p2).
Richard Smith66f3ac92012-10-20 08:26:51 +0000231 if (!Decl->getTypeSourceInfo())
Richard Smithc7fb2252014-02-07 22:51:16 +0000232 return isa<CXXDestructorDecl>(Decl);
Richard Smith66f3ac92012-10-20 08:26:51 +0000233
234 const FunctionProtoType *Ty =
235 Decl->getTypeSourceInfo()->getType()->getAs<FunctionProtoType>();
236 return !Ty->hasExceptionSpec();
Richard Smith1ee63522012-10-16 23:30:16 +0000237}
238
Douglas Gregorf40863c2010-02-12 07:32:17 +0000239bool Sema::CheckEquivalentExceptionSpec(FunctionDecl *Old, FunctionDecl *New) {
Richard Smith13b40bc2016-11-30 00:13:55 +0000240 // Just completely ignore this under -fno-exceptions prior to C++1z.
241 // In C++1z onwards, the exception specification is part of the type and
242 // we will diagnose mismatches anyway, so it's better to check for them here.
243 if (!getLangOpts().CXXExceptions && !getLangOpts().CPlusPlus1z)
244 return false;
245
Sebastian Redlcb5dd002011-03-15 19:52:30 +0000246 OverloadedOperatorKind OO = New->getDeclName().getCXXOverloadedOperator();
247 bool IsOperatorNew = OO == OO_New || OO == OO_Array_New;
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000248 bool MissingExceptionSpecification = false;
Douglas Gregorf40863c2010-02-12 07:32:17 +0000249 bool MissingEmptyExceptionSpecification = false;
Hans Wennborg39a509a2014-02-05 02:37:58 +0000250
Francois Pichet13b4e682011-03-19 23:05:18 +0000251 unsigned DiagID = diag::err_mismatched_exception_spec;
Hans Wennborg39a509a2014-02-05 02:37:58 +0000252 bool ReturnValueOnError = true;
253 if (getLangOpts().MicrosoftExt) {
Richard Smith1b98ccc2014-07-19 01:39:17 +0000254 DiagID = diag::ext_mismatched_exception_spec;
Hans Wennborg39a509a2014-02-05 02:37:58 +0000255 ReturnValueOnError = false;
256 }
Richard Smithf623c962012-04-17 00:58:00 +0000257
Richard Smith1ee63522012-10-16 23:30:16 +0000258 // Check the types as written: they must match before any exception
259 // specification adjustment is applied.
Richard Smith13b40bc2016-11-30 00:13:55 +0000260 if (!CheckEquivalentExceptionSpecImpl(
261 *this, PDiag(DiagID), PDiag(diag::note_previous_declaration),
Richard Smith66f3ac92012-10-20 08:26:51 +0000262 Old->getType()->getAs<FunctionProtoType>(), Old->getLocation(),
263 New->getType()->getAs<FunctionProtoType>(), New->getLocation(),
Richard Smith1ee63522012-10-16 23:30:16 +0000264 &MissingExceptionSpecification, &MissingEmptyExceptionSpecification,
Richard Smith66f3ac92012-10-20 08:26:51 +0000265 /*AllowNoexceptAllMatchWithNoSpec=*/true, IsOperatorNew)) {
266 // C++11 [except.spec]p4 [DR1492]:
267 // If a declaration of a function has an implicit
268 // exception-specification, other declarations of the function shall
269 // not specify an exception-specification.
Richard Smithe3ea0012016-08-31 20:38:32 +0000270 if (getLangOpts().CPlusPlus11 && getLangOpts().CXXExceptions &&
Richard Smith66f3ac92012-10-20 08:26:51 +0000271 hasImplicitExceptionSpec(Old) != hasImplicitExceptionSpec(New)) {
272 Diag(New->getLocation(), diag::ext_implicit_exception_spec_mismatch)
273 << hasImplicitExceptionSpec(Old);
Yaron Keren8b563662015-10-03 10:46:20 +0000274 if (Old->getLocation().isValid())
Richard Smith66f3ac92012-10-20 08:26:51 +0000275 Diag(Old->getLocation(), diag::note_previous_declaration);
276 }
Douglas Gregorf40863c2010-02-12 07:32:17 +0000277 return false;
Richard Smith66f3ac92012-10-20 08:26:51 +0000278 }
Douglas Gregorf40863c2010-02-12 07:32:17 +0000279
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000280 // The failure was something other than an missing exception
Hans Wennborg39a509a2014-02-05 02:37:58 +0000281 // specification; return an error, except in MS mode where this is a warning.
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000282 if (!MissingExceptionSpecification)
Hans Wennborg39a509a2014-02-05 02:37:58 +0000283 return ReturnValueOnError;
Douglas Gregorf40863c2010-02-12 07:32:17 +0000284
Richard Smith66f3ac92012-10-20 08:26:51 +0000285 const FunctionProtoType *NewProto =
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000286 New->getType()->castAs<FunctionProtoType>();
John McCalldb40c7f2010-12-14 08:05:40 +0000287
Douglas Gregorf40863c2010-02-12 07:32:17 +0000288 // The new function declaration is only missing an empty exception
289 // specification "throw()". If the throw() specification came from a
290 // function in a system header that has C linkage, just add an empty
Richard Smith836de6b2016-12-19 23:59:34 +0000291 // exception specification to the "new" declaration. Note that C library
292 // implementations are permitted to add these nothrow exception
293 // specifications.
294 //
295 // Likewise if the old function is a builtin.
John McCalldb40c7f2010-12-14 08:05:40 +0000296 if (MissingEmptyExceptionSpecification && NewProto &&
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000297 (Old->getLocation().isInvalid() ||
Richard Smith836de6b2016-12-19 23:59:34 +0000298 Context.getSourceManager().isInSystemHeader(Old->getLocation()) ||
299 Old->getBuiltinID()) &&
Douglas Gregorf40863c2010-02-12 07:32:17 +0000300 Old->isExternC()) {
Richard Smith8acb4282014-07-31 21:57:55 +0000301 New->setType(Context.getFunctionType(
302 NewProto->getReturnType(), NewProto->getParamTypes(),
303 NewProto->getExtProtoInfo().withExceptionSpec(EST_DynamicNone)));
Douglas Gregorf40863c2010-02-12 07:32:17 +0000304 return false;
305 }
306
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000307 const FunctionProtoType *OldProto =
308 Old->getType()->castAs<FunctionProtoType>();
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000309
Richard Smith8acb4282014-07-31 21:57:55 +0000310 FunctionProtoType::ExceptionSpecInfo ESI = OldProto->getExceptionSpecType();
311 if (ESI.Type == EST_Dynamic) {
312 ESI.Exceptions = OldProto->exceptions();
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000313 }
314
Richard Smitha91de372015-09-30 00:48:50 +0000315 if (ESI.Type == EST_ComputedNoexcept) {
316 // For computed noexcept, we can't just take the expression from the old
317 // prototype. It likely contains references to the old prototype's
318 // parameters.
319 New->setInvalidDecl();
320 } else {
321 // Update the type of the function with the appropriate exception
322 // specification.
323 New->setType(Context.getFunctionType(
324 NewProto->getReturnType(), NewProto->getParamTypes(),
325 NewProto->getExtProtoInfo().withExceptionSpec(ESI)));
326 }
327
David Majnemer06ce8a42015-10-20 20:49:21 +0000328 if (getLangOpts().MicrosoftExt && ESI.Type != EST_ComputedNoexcept) {
329 // Allow missing exception specifications in redeclarations as an extension.
330 DiagID = diag::ext_ms_missing_exception_specification;
331 ReturnValueOnError = false;
332 } else if (New->isReplaceableGlobalAllocationFunction() &&
333 ESI.Type != EST_ComputedNoexcept) {
334 // Allow missing exception specifications in redeclarations as an extension,
335 // when declaring a replaceable global allocation function.
Richard Smitha91de372015-09-30 00:48:50 +0000336 DiagID = diag::ext_missing_exception_specification;
337 ReturnValueOnError = false;
338 } else {
339 DiagID = diag::err_missing_exception_specification;
340 ReturnValueOnError = true;
341 }
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000342
343 // Warn about the lack of exception specification.
344 SmallString<128> ExceptionSpecString;
345 llvm::raw_svector_ostream OS(ExceptionSpecString);
346 switch (OldProto->getExceptionSpecType()) {
347 case EST_DynamicNone:
348 OS << "throw()";
349 break;
350
351 case EST_Dynamic: {
352 OS << "throw(";
353 bool OnFirstException = true;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000354 for (const auto &E : OldProto->exceptions()) {
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000355 if (OnFirstException)
356 OnFirstException = false;
357 else
358 OS << ", ";
359
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000360 OS << E.getAsString(getPrintingPolicy());
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000361 }
362 OS << ")";
363 break;
364 }
365
366 case EST_BasicNoexcept:
367 OS << "noexcept";
368 break;
369
370 case EST_ComputedNoexcept:
371 OS << "noexcept(";
Richard Trieuddd01ce2014-06-09 22:53:25 +0000372 assert(OldProto->getNoexceptExpr() != nullptr && "Expected non-null Expr");
Craig Topperc3ec1492014-05-26 06:22:03 +0000373 OldProto->getNoexceptExpr()->printPretty(OS, nullptr, getPrintingPolicy());
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000374 OS << ")";
375 break;
376
377 default:
378 llvm_unreachable("This spec type is compatible with none.");
379 }
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000380
381 SourceLocation FixItLoc;
382 if (TypeSourceInfo *TSInfo = New->getTypeSourceInfo()) {
383 TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens();
Richard Smitha91de372015-09-30 00:48:50 +0000384 // FIXME: Preserve enough information so that we can produce a correct fixit
385 // location when there is a trailing return type.
386 if (auto FTLoc = TL.getAs<FunctionProtoTypeLoc>())
387 if (!FTLoc.getTypePtr()->hasTrailingReturn())
388 FixItLoc = getLocForEndOfToken(FTLoc.getLocalRangeEnd());
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000389 }
390
391 if (FixItLoc.isInvalid())
Richard Smitha91de372015-09-30 00:48:50 +0000392 Diag(New->getLocation(), DiagID)
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000393 << New << OS.str();
394 else {
Richard Smitha91de372015-09-30 00:48:50 +0000395 Diag(New->getLocation(), DiagID)
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000396 << New << OS.str()
397 << FixItHint::CreateInsertion(FixItLoc, " " + OS.str().str());
398 }
399
Yaron Keren8b563662015-10-03 10:46:20 +0000400 if (Old->getLocation().isValid())
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000401 Diag(Old->getLocation(), diag::note_previous_declaration);
402
Richard Smitha91de372015-09-30 00:48:50 +0000403 return ReturnValueOnError;
Douglas Gregorf40863c2010-02-12 07:32:17 +0000404}
405
Sebastian Redl4915e632009-10-11 09:03:14 +0000406/// CheckEquivalentExceptionSpec - Check if the two types have equivalent
407/// exception specifications. Exception specifications are equivalent if
408/// they allow exactly the same set of exception types. It does not matter how
409/// that is achieved. See C++ [except.spec]p2.
410bool Sema::CheckEquivalentExceptionSpec(
411 const FunctionProtoType *Old, SourceLocation OldLoc,
412 const FunctionProtoType *New, SourceLocation NewLoc) {
Richard Smith13b40bc2016-11-30 00:13:55 +0000413 if (!getLangOpts().CXXExceptions)
414 return false;
415
Francois Pichet13b4e682011-03-19 23:05:18 +0000416 unsigned DiagID = diag::err_mismatched_exception_spec;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000417 if (getLangOpts().MicrosoftExt)
Richard Smith1b98ccc2014-07-19 01:39:17 +0000418 DiagID = diag::ext_mismatched_exception_spec;
Richard Smith13b40bc2016-11-30 00:13:55 +0000419 bool Result = CheckEquivalentExceptionSpecImpl(
420 *this, PDiag(DiagID), PDiag(diag::note_previous_declaration),
421 Old, OldLoc, New, NewLoc);
Hans Wennborg39a509a2014-02-05 02:37:58 +0000422
423 // In Microsoft mode, mismatching exception specifications just cause a warning.
424 if (getLangOpts().MicrosoftExt)
425 return false;
426 return Result;
Sebastian Redl4915e632009-10-11 09:03:14 +0000427}
428
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000429/// CheckEquivalentExceptionSpec - Check if the two types have compatible
430/// exception specifications. See C++ [except.spec]p3.
Richard Smith66f3ac92012-10-20 08:26:51 +0000431///
432/// \return \c false if the exception specifications match, \c true if there is
433/// a problem. If \c true is returned, either a diagnostic has already been
434/// produced or \c *MissingExceptionSpecification is set to \c true.
Richard Smith13b40bc2016-11-30 00:13:55 +0000435static bool CheckEquivalentExceptionSpecImpl(
436 Sema &S, const PartialDiagnostic &DiagID, const PartialDiagnostic &NoteID,
437 const FunctionProtoType *Old, SourceLocation OldLoc,
438 const FunctionProtoType *New, SourceLocation NewLoc,
439 bool *MissingExceptionSpecification,
440 bool *MissingEmptyExceptionSpecification,
441 bool AllowNoexceptAllMatchWithNoSpec, bool IsOperatorNew) {
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000442 if (MissingExceptionSpecification)
443 *MissingExceptionSpecification = false;
444
Douglas Gregorf40863c2010-02-12 07:32:17 +0000445 if (MissingEmptyExceptionSpecification)
446 *MissingEmptyExceptionSpecification = false;
447
Richard Smith13b40bc2016-11-30 00:13:55 +0000448 Old = S.ResolveExceptionSpec(NewLoc, Old);
Richard Smithf623c962012-04-17 00:58:00 +0000449 if (!Old)
450 return false;
Richard Smith13b40bc2016-11-30 00:13:55 +0000451 New = S.ResolveExceptionSpec(NewLoc, New);
Richard Smithf623c962012-04-17 00:58:00 +0000452 if (!New)
453 return false;
454
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000455 // C++0x [except.spec]p3: Two exception-specifications are compatible if:
456 // - both are non-throwing, regardless of their form,
457 // - both have the form noexcept(constant-expression) and the constant-
458 // expressions are equivalent,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000459 // - both are dynamic-exception-specifications that have the same set of
460 // adjusted types.
461 //
Eric Christophere6b7cf42015-07-10 18:25:52 +0000462 // C++0x [except.spec]p12: An exception-specification is non-throwing if it is
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000463 // of the form throw(), noexcept, or noexcept(constant-expression) where the
464 // constant-expression yields true.
465 //
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000466 // C++0x [except.spec]p4: If any declaration of a function has an exception-
467 // specifier that is not a noexcept-specification allowing all exceptions,
468 // all declarations [...] of that function shall have a compatible
469 // exception-specification.
470 //
471 // That last point basically means that noexcept(false) matches no spec.
472 // It's considered when AllowNoexceptAllMatchWithNoSpec is true.
473
474 ExceptionSpecificationType OldEST = Old->getExceptionSpecType();
475 ExceptionSpecificationType NewEST = New->getExceptionSpecType();
476
Richard Smithd3b5c9082012-07-27 04:22:15 +0000477 assert(!isUnresolvedExceptionSpec(OldEST) &&
478 !isUnresolvedExceptionSpec(NewEST) &&
Richard Smith938f40b2011-06-11 17:19:42 +0000479 "Shouldn't see unknown exception specifications here");
480
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000481 // Shortcut the case where both have no spec.
482 if (OldEST == EST_None && NewEST == EST_None)
483 return false;
484
Richard Smith13b40bc2016-11-30 00:13:55 +0000485 FunctionProtoType::NoexceptResult OldNR = Old->getNoexceptSpec(S.Context);
486 FunctionProtoType::NoexceptResult NewNR = New->getNoexceptSpec(S.Context);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000487 if (OldNR == FunctionProtoType::NR_BadNoexcept ||
488 NewNR == FunctionProtoType::NR_BadNoexcept)
489 return false;
490
491 // Dependent noexcept specifiers are compatible with each other, but nothing
492 // else.
493 // One noexcept is compatible with another if the argument is the same
494 if (OldNR == NewNR &&
495 OldNR != FunctionProtoType::NR_NoNoexcept &&
496 NewNR != FunctionProtoType::NR_NoNoexcept)
497 return false;
498 if (OldNR != NewNR &&
499 OldNR != FunctionProtoType::NR_NoNoexcept &&
500 NewNR != FunctionProtoType::NR_NoNoexcept) {
Richard Smith13b40bc2016-11-30 00:13:55 +0000501 S.Diag(NewLoc, DiagID);
David Majnemer7da23022015-02-19 07:28:55 +0000502 if (NoteID.getDiagID() != 0 && OldLoc.isValid())
Richard Smith13b40bc2016-11-30 00:13:55 +0000503 S.Diag(OldLoc, NoteID);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000504 return true;
Douglas Gregorf62c5292010-08-30 15:04:51 +0000505 }
506
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000507 // The MS extension throw(...) is compatible with itself.
508 if (OldEST == EST_MSAny && NewEST == EST_MSAny)
Sebastian Redl4915e632009-10-11 09:03:14 +0000509 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000510
511 // It's also compatible with no spec.
512 if ((OldEST == EST_None && NewEST == EST_MSAny) ||
513 (OldEST == EST_MSAny && NewEST == EST_None))
514 return false;
515
516 // It's also compatible with noexcept(false).
517 if (OldEST == EST_MSAny && NewNR == FunctionProtoType::NR_Throw)
518 return false;
519 if (NewEST == EST_MSAny && OldNR == FunctionProtoType::NR_Throw)
520 return false;
521
522 // As described above, noexcept(false) matches no spec only for functions.
523 if (AllowNoexceptAllMatchWithNoSpec) {
524 if (OldEST == EST_None && NewNR == FunctionProtoType::NR_Throw)
525 return false;
526 if (NewEST == EST_None && OldNR == FunctionProtoType::NR_Throw)
527 return false;
528 }
529
530 // Any non-throwing specifications are compatible.
531 bool OldNonThrowing = OldNR == FunctionProtoType::NR_Nothrow ||
532 OldEST == EST_DynamicNone;
533 bool NewNonThrowing = NewNR == FunctionProtoType::NR_Nothrow ||
534 NewEST == EST_DynamicNone;
535 if (OldNonThrowing && NewNonThrowing)
536 return false;
537
Sebastian Redlcb5dd002011-03-15 19:52:30 +0000538 // As a special compatibility feature, under C++0x we accept no spec and
539 // throw(std::bad_alloc) as equivalent for operator new and operator new[].
540 // This is because the implicit declaration changed, but old code would break.
Richard Smith13b40bc2016-11-30 00:13:55 +0000541 if (S.getLangOpts().CPlusPlus11 && IsOperatorNew) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000542 const FunctionProtoType *WithExceptions = nullptr;
Sebastian Redlcb5dd002011-03-15 19:52:30 +0000543 if (OldEST == EST_None && NewEST == EST_Dynamic)
544 WithExceptions = New;
545 else if (OldEST == EST_Dynamic && NewEST == EST_None)
546 WithExceptions = Old;
547 if (WithExceptions && WithExceptions->getNumExceptions() == 1) {
548 // One has no spec, the other throw(something). If that something is
549 // std::bad_alloc, all conditions are met.
550 QualType Exception = *WithExceptions->exception_begin();
551 if (CXXRecordDecl *ExRecord = Exception->getAsCXXRecordDecl()) {
552 IdentifierInfo* Name = ExRecord->getIdentifier();
553 if (Name && Name->getName() == "bad_alloc") {
554 // It's called bad_alloc, but is it in std?
Richard Trieuc771d5d2014-05-28 02:16:01 +0000555 if (ExRecord->isInStdNamespace()) {
556 return false;
Sebastian Redlcb5dd002011-03-15 19:52:30 +0000557 }
558 }
559 }
560 }
561 }
562
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000563 // At this point, the only remaining valid case is two matching dynamic
564 // specifications. We return here unless both specifications are dynamic.
565 if (OldEST != EST_Dynamic || NewEST != EST_Dynamic) {
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000566 if (MissingExceptionSpecification && Old->hasExceptionSpec() &&
Douglas Gregorf40863c2010-02-12 07:32:17 +0000567 !New->hasExceptionSpec()) {
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000568 // The old type has an exception specification of some sort, but
569 // the new type does not.
570 *MissingExceptionSpecification = true;
571
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000572 if (MissingEmptyExceptionSpecification && OldNonThrowing) {
573 // The old type has a throw() or noexcept(true) exception specification
574 // and the new type has no exception specification, and the caller asked
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000575 // to handle this itself.
576 *MissingEmptyExceptionSpecification = true;
577 }
578
Douglas Gregorf40863c2010-02-12 07:32:17 +0000579 return true;
580 }
581
Richard Smith13b40bc2016-11-30 00:13:55 +0000582 S.Diag(NewLoc, DiagID);
David Majnemer7da23022015-02-19 07:28:55 +0000583 if (NoteID.getDiagID() != 0 && OldLoc.isValid())
Richard Smith13b40bc2016-11-30 00:13:55 +0000584 S.Diag(OldLoc, NoteID);
Sebastian Redl4915e632009-10-11 09:03:14 +0000585 return true;
586 }
587
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000588 assert(OldEST == EST_Dynamic && NewEST == EST_Dynamic &&
589 "Exception compatibility logic error: non-dynamic spec slipped through.");
590
Sebastian Redl4915e632009-10-11 09:03:14 +0000591 bool Success = true;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000592 // Both have a dynamic exception spec. Collect the first set, then compare
Sebastian Redl4915e632009-10-11 09:03:14 +0000593 // to the second.
Sebastian Redl184edca2009-10-14 15:06:25 +0000594 llvm::SmallPtrSet<CanQualType, 8> OldTypes, NewTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000595 for (const auto &I : Old->exceptions())
Richard Smith13b40bc2016-11-30 00:13:55 +0000596 OldTypes.insert(S.Context.getCanonicalType(I).getUnqualifiedType());
Sebastian Redl4915e632009-10-11 09:03:14 +0000597
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000598 for (const auto &I : New->exceptions()) {
Richard Smith13b40bc2016-11-30 00:13:55 +0000599 CanQualType TypePtr = S.Context.getCanonicalType(I).getUnqualifiedType();
600 if (OldTypes.count(TypePtr))
Sebastian Redl6e4c8712009-10-11 09:11:23 +0000601 NewTypes.insert(TypePtr);
602 else
603 Success = false;
604 }
Sebastian Redl4915e632009-10-11 09:03:14 +0000605
Sebastian Redl6e4c8712009-10-11 09:11:23 +0000606 Success = Success && OldTypes.size() == NewTypes.size();
Sebastian Redl4915e632009-10-11 09:03:14 +0000607
608 if (Success) {
609 return false;
610 }
Richard Smith13b40bc2016-11-30 00:13:55 +0000611 S.Diag(NewLoc, DiagID);
David Majnemer7da23022015-02-19 07:28:55 +0000612 if (NoteID.getDiagID() != 0 && OldLoc.isValid())
Richard Smith13b40bc2016-11-30 00:13:55 +0000613 S.Diag(OldLoc, NoteID);
Sebastian Redl4915e632009-10-11 09:03:14 +0000614 return true;
615}
616
Richard Smith13b40bc2016-11-30 00:13:55 +0000617bool Sema::CheckEquivalentExceptionSpec(const PartialDiagnostic &DiagID,
618 const PartialDiagnostic &NoteID,
619 const FunctionProtoType *Old,
620 SourceLocation OldLoc,
621 const FunctionProtoType *New,
622 SourceLocation NewLoc) {
623 if (!getLangOpts().CXXExceptions)
624 return false;
625 return CheckEquivalentExceptionSpecImpl(*this, DiagID, NoteID, Old, OldLoc,
626 New, NewLoc);
627}
628
Sebastian Redl4915e632009-10-11 09:03:14 +0000629/// CheckExceptionSpecSubset - Check whether the second function type's
630/// exception specification is a subset (or equivalent) of the first function
631/// type. This is used by override and pointer assignment checks.
Richard Smith1be59c52016-10-22 01:32:19 +0000632bool Sema::CheckExceptionSpecSubset(const PartialDiagnostic &DiagID,
633 const PartialDiagnostic &NestedDiagID,
634 const PartialDiagnostic &NoteID,
635 const FunctionProtoType *Superset,
636 SourceLocation SuperLoc,
637 const FunctionProtoType *Subset,
638 SourceLocation SubLoc) {
John McCallf9c94092010-05-28 08:37:35 +0000639
640 // Just auto-succeed under -fno-exceptions.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000641 if (!getLangOpts().CXXExceptions)
John McCallf9c94092010-05-28 08:37:35 +0000642 return false;
643
Sebastian Redl4915e632009-10-11 09:03:14 +0000644 // FIXME: As usual, we could be more specific in our error messages, but
645 // that better waits until we've got types with source locations.
646
647 if (!SubLoc.isValid())
648 SubLoc = SuperLoc;
649
Richard Smithf623c962012-04-17 00:58:00 +0000650 // Resolve the exception specifications, if needed.
651 Superset = ResolveExceptionSpec(SuperLoc, Superset);
652 if (!Superset)
653 return false;
654 Subset = ResolveExceptionSpec(SubLoc, Subset);
655 if (!Subset)
656 return false;
657
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000658 ExceptionSpecificationType SuperEST = Superset->getExceptionSpecType();
659
Sebastian Redl4915e632009-10-11 09:03:14 +0000660 // If superset contains everything, we're done.
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000661 if (SuperEST == EST_None || SuperEST == EST_MSAny)
Richard Smith1be59c52016-10-22 01:32:19 +0000662 return CheckParamExceptionSpec(NestedDiagID, NoteID, Superset, SuperLoc,
663 Subset, SubLoc);
Sebastian Redl4915e632009-10-11 09:03:14 +0000664
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000665 // If there are dependent noexcept specs, assume everything is fine. Unlike
666 // with the equivalency check, this is safe in this case, because we don't
667 // want to merge declarations. Checks after instantiation will catch any
668 // omissions we make here.
669 // We also shortcut checking if a noexcept expression was bad.
670
Sebastian Redl31ad7542011-03-13 17:09:40 +0000671 FunctionProtoType::NoexceptResult SuperNR =Superset->getNoexceptSpec(Context);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000672 if (SuperNR == FunctionProtoType::NR_BadNoexcept ||
673 SuperNR == FunctionProtoType::NR_Dependent)
674 return false;
675
676 // Another case of the superset containing everything.
677 if (SuperNR == FunctionProtoType::NR_Throw)
Richard Smith1be59c52016-10-22 01:32:19 +0000678 return CheckParamExceptionSpec(NestedDiagID, NoteID, Superset, SuperLoc,
679 Subset, SubLoc);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000680
681 ExceptionSpecificationType SubEST = Subset->getExceptionSpecType();
682
Richard Smithd3b5c9082012-07-27 04:22:15 +0000683 assert(!isUnresolvedExceptionSpec(SuperEST) &&
684 !isUnresolvedExceptionSpec(SubEST) &&
Richard Smith938f40b2011-06-11 17:19:42 +0000685 "Shouldn't see unknown exception specifications here");
686
Sebastian Redl4915e632009-10-11 09:03:14 +0000687 // It does not. If the subset contains everything, we've failed.
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000688 if (SubEST == EST_None || SubEST == EST_MSAny) {
Sebastian Redl4915e632009-10-11 09:03:14 +0000689 Diag(SubLoc, DiagID);
Sebastian Redla44822f2009-10-14 16:09:29 +0000690 if (NoteID.getDiagID() != 0)
Sebastian Redl4915e632009-10-11 09:03:14 +0000691 Diag(SuperLoc, NoteID);
692 return true;
693 }
694
Sebastian Redl31ad7542011-03-13 17:09:40 +0000695 FunctionProtoType::NoexceptResult SubNR = Subset->getNoexceptSpec(Context);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000696 if (SubNR == FunctionProtoType::NR_BadNoexcept ||
697 SubNR == FunctionProtoType::NR_Dependent)
698 return false;
699
700 // Another case of the subset containing everything.
701 if (SubNR == FunctionProtoType::NR_Throw) {
702 Diag(SubLoc, DiagID);
703 if (NoteID.getDiagID() != 0)
704 Diag(SuperLoc, NoteID);
705 return true;
706 }
707
708 // If the subset contains nothing, we're done.
709 if (SubEST == EST_DynamicNone || SubNR == FunctionProtoType::NR_Nothrow)
Richard Smith1be59c52016-10-22 01:32:19 +0000710 return CheckParamExceptionSpec(NestedDiagID, NoteID, Superset, SuperLoc,
711 Subset, SubLoc);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000712
713 // Otherwise, if the superset contains nothing, we've failed.
714 if (SuperEST == EST_DynamicNone || SuperNR == FunctionProtoType::NR_Nothrow) {
715 Diag(SubLoc, DiagID);
716 if (NoteID.getDiagID() != 0)
717 Diag(SuperLoc, NoteID);
718 return true;
719 }
720
721 assert(SuperEST == EST_Dynamic && SubEST == EST_Dynamic &&
722 "Exception spec subset: non-dynamic case slipped through.");
723
724 // Neither contains everything or nothing. Do a proper comparison.
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000725 for (const auto &SubI : Subset->exceptions()) {
Sebastian Redl4915e632009-10-11 09:03:14 +0000726 // Take one type from the subset.
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000727 QualType CanonicalSubT = Context.getCanonicalType(SubI);
Sebastian Redl075b21d2009-10-14 14:38:54 +0000728 // Unwrap pointers and references so that we can do checks within a class
729 // hierarchy. Don't unwrap member pointers; they don't have hierarchy
730 // conversions on the pointee.
Sebastian Redl4915e632009-10-11 09:03:14 +0000731 bool SubIsPointer = false;
732 if (const ReferenceType *RefTy = CanonicalSubT->getAs<ReferenceType>())
733 CanonicalSubT = RefTy->getPointeeType();
734 if (const PointerType *PtrTy = CanonicalSubT->getAs<PointerType>()) {
735 CanonicalSubT = PtrTy->getPointeeType();
736 SubIsPointer = true;
737 }
738 bool SubIsClass = CanonicalSubT->isRecordType();
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +0000739 CanonicalSubT = CanonicalSubT.getLocalUnqualifiedType();
Sebastian Redl4915e632009-10-11 09:03:14 +0000740
741 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
742 /*DetectVirtual=*/false);
743
744 bool Contained = false;
745 // Make sure it's in the superset.
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000746 for (const auto &SuperI : Superset->exceptions()) {
747 QualType CanonicalSuperT = Context.getCanonicalType(SuperI);
Sebastian Redl4915e632009-10-11 09:03:14 +0000748 // SubT must be SuperT or derived from it, or pointer or reference to
749 // such types.
750 if (const ReferenceType *RefTy = CanonicalSuperT->getAs<ReferenceType>())
751 CanonicalSuperT = RefTy->getPointeeType();
752 if (SubIsPointer) {
753 if (const PointerType *PtrTy = CanonicalSuperT->getAs<PointerType>())
754 CanonicalSuperT = PtrTy->getPointeeType();
755 else {
756 continue;
757 }
758 }
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +0000759 CanonicalSuperT = CanonicalSuperT.getLocalUnqualifiedType();
Sebastian Redl4915e632009-10-11 09:03:14 +0000760 // If the types are the same, move on to the next type in the subset.
761 if (CanonicalSubT == CanonicalSuperT) {
762 Contained = true;
763 break;
764 }
765
766 // Otherwise we need to check the inheritance.
767 if (!SubIsClass || !CanonicalSuperT->isRecordType())
768 continue;
769
770 Paths.clear();
Richard Smith0f59cb32015-12-18 21:45:41 +0000771 if (!IsDerivedFrom(SubLoc, CanonicalSubT, CanonicalSuperT, Paths))
Sebastian Redl4915e632009-10-11 09:03:14 +0000772 continue;
773
Douglas Gregor27ac4292010-05-21 20:29:55 +0000774 if (Paths.isAmbiguous(Context.getCanonicalType(CanonicalSuperT)))
Sebastian Redl4915e632009-10-11 09:03:14 +0000775 continue;
776
John McCall5b0829a2010-02-10 09:31:12 +0000777 // Do this check from a context without privileges.
John McCall1064d7e2010-03-16 05:22:47 +0000778 switch (CheckBaseClassAccess(SourceLocation(),
John McCall5b0829a2010-02-10 09:31:12 +0000779 CanonicalSuperT, CanonicalSubT,
780 Paths.front(),
John McCall1064d7e2010-03-16 05:22:47 +0000781 /*Diagnostic*/ 0,
John McCall5b0829a2010-02-10 09:31:12 +0000782 /*ForceCheck*/ true,
John McCall1064d7e2010-03-16 05:22:47 +0000783 /*ForceUnprivileged*/ true)) {
John McCall5b0829a2010-02-10 09:31:12 +0000784 case AR_accessible: break;
785 case AR_inaccessible: continue;
786 case AR_dependent:
787 llvm_unreachable("access check dependent for unprivileged context");
John McCall5b0829a2010-02-10 09:31:12 +0000788 case AR_delayed:
789 llvm_unreachable("access check delayed in non-declaration");
John McCall5b0829a2010-02-10 09:31:12 +0000790 }
Sebastian Redl4915e632009-10-11 09:03:14 +0000791
792 Contained = true;
793 break;
794 }
795 if (!Contained) {
796 Diag(SubLoc, DiagID);
Sebastian Redla44822f2009-10-14 16:09:29 +0000797 if (NoteID.getDiagID() != 0)
Sebastian Redl4915e632009-10-11 09:03:14 +0000798 Diag(SuperLoc, NoteID);
799 return true;
800 }
801 }
802 // We've run half the gauntlet.
Richard Smith1be59c52016-10-22 01:32:19 +0000803 return CheckParamExceptionSpec(NestedDiagID, NoteID, Superset, SuperLoc,
804 Subset, SubLoc);
Sebastian Redl4915e632009-10-11 09:03:14 +0000805}
806
Richard Smith1be59c52016-10-22 01:32:19 +0000807static bool
808CheckSpecForTypesEquivalent(Sema &S, const PartialDiagnostic &DiagID,
809 const PartialDiagnostic &NoteID, QualType Target,
810 SourceLocation TargetLoc, QualType Source,
811 SourceLocation SourceLoc) {
Sebastian Redl4915e632009-10-11 09:03:14 +0000812 const FunctionProtoType *TFunc = GetUnderlyingFunction(Target);
813 if (!TFunc)
814 return false;
815 const FunctionProtoType *SFunc = GetUnderlyingFunction(Source);
816 if (!SFunc)
817 return false;
818
819 return S.CheckEquivalentExceptionSpec(DiagID, NoteID, TFunc, TargetLoc,
820 SFunc, SourceLoc);
821}
822
823/// CheckParamExceptionSpec - Check if the parameter and return types of the
824/// two functions have equivalent exception specs. This is part of the
825/// assignment and override compatibility check. We do not check the parameters
826/// of parameter function pointers recursively, as no sane programmer would
827/// even be able to write such a function type.
Richard Smith1be59c52016-10-22 01:32:19 +0000828bool Sema::CheckParamExceptionSpec(const PartialDiagnostic &DiagID,
829 const PartialDiagnostic &NoteID,
Richard Smith2e321552014-11-12 02:00:47 +0000830 const FunctionProtoType *Target,
831 SourceLocation TargetLoc,
832 const FunctionProtoType *Source,
833 SourceLocation SourceLoc) {
Richard Smith1be59c52016-10-22 01:32:19 +0000834 auto RetDiag = DiagID;
835 RetDiag << 0;
Alp Toker314cc812014-01-25 16:55:45 +0000836 if (CheckSpecForTypesEquivalent(
Richard Smith1be59c52016-10-22 01:32:19 +0000837 *this, RetDiag, PDiag(),
Alp Toker314cc812014-01-25 16:55:45 +0000838 Target->getReturnType(), TargetLoc, Source->getReturnType(),
839 SourceLoc))
Sebastian Redl4915e632009-10-11 09:03:14 +0000840 return true;
841
Sebastian Redla44822f2009-10-14 16:09:29 +0000842 // We shouldn't even be testing this unless the arguments are otherwise
Sebastian Redl4915e632009-10-11 09:03:14 +0000843 // compatible.
Alp Toker9cacbab2014-01-20 20:26:09 +0000844 assert(Target->getNumParams() == Source->getNumParams() &&
Sebastian Redl4915e632009-10-11 09:03:14 +0000845 "Functions have different argument counts.");
Alp Toker9cacbab2014-01-20 20:26:09 +0000846 for (unsigned i = 0, E = Target->getNumParams(); i != E; ++i) {
Richard Smith1be59c52016-10-22 01:32:19 +0000847 auto ParamDiag = DiagID;
848 ParamDiag << 1;
Alp Toker9cacbab2014-01-20 20:26:09 +0000849 if (CheckSpecForTypesEquivalent(
Richard Smith1be59c52016-10-22 01:32:19 +0000850 *this, ParamDiag, PDiag(),
Alp Toker9cacbab2014-01-20 20:26:09 +0000851 Target->getParamType(i), TargetLoc, Source->getParamType(i),
852 SourceLoc))
Sebastian Redl4915e632009-10-11 09:03:14 +0000853 return true;
854 }
855 return false;
856}
857
Richard Smith2e321552014-11-12 02:00:47 +0000858bool Sema::CheckExceptionSpecCompatibility(Expr *From, QualType ToType) {
Sebastian Redl4915e632009-10-11 09:03:14 +0000859 // First we check for applicability.
860 // Target type must be a function, function pointer or function reference.
861 const FunctionProtoType *ToFunc = GetUnderlyingFunction(ToType);
Richard Smith2e321552014-11-12 02:00:47 +0000862 if (!ToFunc || ToFunc->hasDependentExceptionSpec())
Sebastian Redl4915e632009-10-11 09:03:14 +0000863 return false;
864
865 // SourceType must be a function or function pointer.
866 const FunctionProtoType *FromFunc = GetUnderlyingFunction(From->getType());
Richard Smith2e321552014-11-12 02:00:47 +0000867 if (!FromFunc || FromFunc->hasDependentExceptionSpec())
Sebastian Redl4915e632009-10-11 09:03:14 +0000868 return false;
869
Richard Smith1be59c52016-10-22 01:32:19 +0000870 unsigned DiagID = diag::err_incompatible_exception_specs;
871 unsigned NestedDiagID = diag::err_deep_exception_specs_differ;
872 // This is not an error in C++17 onwards, unless the noexceptness doesn't
873 // match, but in that case we have a full-on type mismatch, not just a
874 // type sugar mismatch.
875 if (getLangOpts().CPlusPlus1z) {
876 DiagID = diag::warn_incompatible_exception_specs;
877 NestedDiagID = diag::warn_deep_exception_specs_differ;
878 }
879
Sebastian Redl4915e632009-10-11 09:03:14 +0000880 // Now we've got the correct types on both sides, check their compatibility.
881 // This means that the source of the conversion can only throw a subset of
882 // the exceptions of the target, and any exception specs on arguments or
883 // return types must be equivalent.
Richard Smith2e321552014-11-12 02:00:47 +0000884 //
885 // FIXME: If there is a nested dependent exception specification, we should
886 // not be checking it here. This is fine:
887 // template<typename T> void f() {
888 // void (*p)(void (*) throw(T));
889 // void (*q)(void (*) throw(int)) = p;
890 // }
891 // ... because it might be instantiated with T=int.
Richard Smith1be59c52016-10-22 01:32:19 +0000892 return CheckExceptionSpecSubset(PDiag(DiagID), PDiag(NestedDiagID), PDiag(),
893 ToFunc, From->getSourceRange().getBegin(),
894 FromFunc, SourceLocation()) &&
895 !getLangOpts().CPlusPlus1z;
Sebastian Redl4915e632009-10-11 09:03:14 +0000896}
897
898bool Sema::CheckOverridingFunctionExceptionSpec(const CXXMethodDecl *New,
899 const CXXMethodDecl *Old) {
Richard Smith88f45492014-11-22 03:09:05 +0000900 // If the new exception specification hasn't been parsed yet, skip the check.
901 // We'll get called again once it's been parsed.
902 if (New->getType()->castAs<FunctionProtoType>()->getExceptionSpecType() ==
903 EST_Unparsed)
904 return false;
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000905 if (getLangOpts().CPlusPlus11 && isa<CXXDestructorDecl>(New)) {
Sebastian Redl645d9582011-05-20 05:57:18 +0000906 // Don't check uninstantiated template destructors at all. We can only
907 // synthesize correct specs after the template is instantiated.
908 if (New->getParent()->isDependentType())
909 return false;
910 if (New->getParent()->isBeingDefined()) {
911 // The destructor might be updated once the definition is finished. So
912 // remember it and check later.
Richard Smith88f45492014-11-22 03:09:05 +0000913 DelayedExceptionSpecChecks.push_back(std::make_pair(New, Old));
Sebastian Redl645d9582011-05-20 05:57:18 +0000914 return false;
915 }
Sebastian Redl623ea822011-05-19 05:13:44 +0000916 }
Richard Smith88f45492014-11-22 03:09:05 +0000917 // If the old exception specification hasn't been parsed yet, remember that
918 // we need to perform this check when we get to the end of the outermost
919 // lexically-surrounding class.
920 if (Old->getType()->castAs<FunctionProtoType>()->getExceptionSpecType() ==
921 EST_Unparsed) {
922 DelayedExceptionSpecChecks.push_back(std::make_pair(New, Old));
Richard Smith0b3a4622014-11-13 20:01:57 +0000923 return false;
Richard Smith88f45492014-11-22 03:09:05 +0000924 }
Francois Picheta8032e92011-05-24 02:11:43 +0000925 unsigned DiagID = diag::err_override_exception_spec;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000926 if (getLangOpts().MicrosoftExt)
Richard Smith1b98ccc2014-07-19 01:39:17 +0000927 DiagID = diag::ext_override_exception_spec;
Francois Picheta8032e92011-05-24 02:11:43 +0000928 return CheckExceptionSpecSubset(PDiag(DiagID),
Richard Smith1be59c52016-10-22 01:32:19 +0000929 PDiag(diag::err_deep_exception_specs_differ),
Douglas Gregor89336232010-03-29 23:34:08 +0000930 PDiag(diag::note_overridden_virtual_function),
Sebastian Redl4915e632009-10-11 09:03:14 +0000931 Old->getType()->getAs<FunctionProtoType>(),
932 Old->getLocation(),
933 New->getType()->getAs<FunctionProtoType>(),
934 New->getLocation());
935}
936
Benjamin Kramer642f1732015-07-02 21:03:14 +0000937static CanThrowResult canSubExprsThrow(Sema &S, const Expr *E) {
Richard Smithf623c962012-04-17 00:58:00 +0000938 CanThrowResult R = CT_Cannot;
Benjamin Kramer642f1732015-07-02 21:03:14 +0000939 for (const Stmt *SubStmt : E->children()) {
940 R = mergeCanThrow(R, S.canThrow(cast<Expr>(SubStmt)));
941 if (R == CT_Can)
942 break;
943 }
Richard Smithf623c962012-04-17 00:58:00 +0000944 return R;
945}
946
Eli Friedman0423b762013-06-25 01:24:22 +0000947static CanThrowResult canCalleeThrow(Sema &S, const Expr *E, const Decl *D) {
Richard Smithf623c962012-04-17 00:58:00 +0000948 // As an extension, we assume that __attribute__((nothrow)) functions don't
949 // throw.
Richard Smith3a8f13a2016-12-03 00:29:06 +0000950 if (D && isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
Richard Smithf623c962012-04-17 00:58:00 +0000951 return CT_Cannot;
952
Richard Smith3a8f13a2016-12-03 00:29:06 +0000953 QualType T;
954
955 // In C++1z, just look at the function type of the callee.
956 if (S.getLangOpts().CPlusPlus1z && isa<CallExpr>(E)) {
957 E = cast<CallExpr>(E)->getCallee();
958 T = E->getType();
959 if (T->isSpecificPlaceholderType(BuiltinType::BoundMember)) {
960 // Sadly we don't preserve the actual type as part of the "bound member"
961 // placeholder, so we need to reconstruct it.
962 E = E->IgnoreParenImpCasts();
963
964 // Could be a call to a pointer-to-member or a plain member access.
965 if (auto *Op = dyn_cast<BinaryOperator>(E)) {
966 assert(Op->getOpcode() == BO_PtrMemD || Op->getOpcode() == BO_PtrMemI);
967 T = Op->getRHS()->getType()
968 ->castAs<MemberPointerType>()->getPointeeType();
969 } else {
970 T = cast<MemberExpr>(E)->getMemberDecl()->getType();
971 }
972 }
973 } else if (const ValueDecl *VD = dyn_cast_or_null<ValueDecl>(D))
974 T = VD->getType();
975 else
976 // If we have no clue what we're calling, assume the worst.
977 return CT_Can;
978
Richard Smithf623c962012-04-17 00:58:00 +0000979 const FunctionProtoType *FT;
980 if ((FT = T->getAs<FunctionProtoType>())) {
981 } else if (const PointerType *PT = T->getAs<PointerType>())
982 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
983 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
984 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
985 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
986 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
987 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
988 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
989
990 if (!FT)
991 return CT_Can;
992
993 FT = S.ResolveExceptionSpec(E->getLocStart(), FT);
994 if (!FT)
995 return CT_Can;
996
Richard Smithf623c962012-04-17 00:58:00 +0000997 return FT->isNothrow(S.Context) ? CT_Cannot : CT_Can;
998}
999
1000static CanThrowResult canDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1001 if (DC->isTypeDependent())
1002 return CT_Dependent;
1003
1004 if (!DC->getTypeAsWritten()->isReferenceType())
1005 return CT_Cannot;
1006
1007 if (DC->getSubExpr()->isTypeDependent())
1008 return CT_Dependent;
1009
1010 return DC->getCastKind() == clang::CK_Dynamic? CT_Can : CT_Cannot;
1011}
1012
1013static CanThrowResult canTypeidThrow(Sema &S, const CXXTypeidExpr *DC) {
1014 if (DC->isTypeOperand())
1015 return CT_Cannot;
1016
1017 Expr *Op = DC->getExprOperand();
1018 if (Op->isTypeDependent())
1019 return CT_Dependent;
1020
1021 const RecordType *RT = Op->getType()->getAs<RecordType>();
1022 if (!RT)
1023 return CT_Cannot;
1024
1025 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1026 return CT_Cannot;
1027
1028 if (Op->Classify(S.Context).isPRValue())
1029 return CT_Cannot;
1030
1031 return CT_Can;
1032}
1033
1034CanThrowResult Sema::canThrow(const Expr *E) {
1035 // C++ [expr.unary.noexcept]p3:
1036 // [Can throw] if in a potentially-evaluated context the expression would
1037 // contain:
1038 switch (E->getStmtClass()) {
1039 case Expr::CXXThrowExprClass:
1040 // - a potentially evaluated throw-expression
1041 return CT_Can;
1042
1043 case Expr::CXXDynamicCastExprClass: {
1044 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1045 // where T is a reference type, that requires a run-time check
1046 CanThrowResult CT = canDynamicCastThrow(cast<CXXDynamicCastExpr>(E));
1047 if (CT == CT_Can)
1048 return CT;
1049 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1050 }
1051
1052 case Expr::CXXTypeidExprClass:
1053 // - a potentially evaluated typeid expression applied to a glvalue
1054 // expression whose type is a polymorphic class type
1055 return canTypeidThrow(*this, cast<CXXTypeidExpr>(E));
1056
1057 // - a potentially evaluated call to a function, member function, function
1058 // pointer, or member function pointer that does not have a non-throwing
1059 // exception-specification
1060 case Expr::CallExprClass:
1061 case Expr::CXXMemberCallExprClass:
1062 case Expr::CXXOperatorCallExprClass:
1063 case Expr::UserDefinedLiteralClass: {
1064 const CallExpr *CE = cast<CallExpr>(E);
1065 CanThrowResult CT;
1066 if (E->isTypeDependent())
1067 CT = CT_Dependent;
1068 else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
1069 CT = CT_Cannot;
Eli Friedman5a8738f2013-06-25 01:55:41 +00001070 else
Richard Smith3a8f13a2016-12-03 00:29:06 +00001071 CT = canCalleeThrow(*this, E, CE->getCalleeDecl());
Richard Smithf623c962012-04-17 00:58:00 +00001072 if (CT == CT_Can)
1073 return CT;
1074 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1075 }
1076
1077 case Expr::CXXConstructExprClass:
1078 case Expr::CXXTemporaryObjectExprClass: {
1079 CanThrowResult CT = canCalleeThrow(*this, E,
1080 cast<CXXConstructExpr>(E)->getConstructor());
1081 if (CT == CT_Can)
1082 return CT;
1083 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1084 }
1085
Richard Smith5179eb72016-06-28 19:03:57 +00001086 case Expr::CXXInheritedCtorInitExprClass:
1087 return canCalleeThrow(*this, E,
1088 cast<CXXInheritedCtorInitExpr>(E)->getConstructor());
1089
Richard Smithf623c962012-04-17 00:58:00 +00001090 case Expr::LambdaExprClass: {
1091 const LambdaExpr *Lambda = cast<LambdaExpr>(E);
1092 CanThrowResult CT = CT_Cannot;
James Y Knight53c76162015-07-17 18:21:37 +00001093 for (LambdaExpr::const_capture_init_iterator
1094 Cap = Lambda->capture_init_begin(),
1095 CapEnd = Lambda->capture_init_end();
Richard Smithf623c962012-04-17 00:58:00 +00001096 Cap != CapEnd; ++Cap)
1097 CT = mergeCanThrow(CT, canThrow(*Cap));
1098 return CT;
1099 }
1100
1101 case Expr::CXXNewExprClass: {
1102 CanThrowResult CT;
1103 if (E->isTypeDependent())
1104 CT = CT_Dependent;
1105 else
1106 CT = canCalleeThrow(*this, E, cast<CXXNewExpr>(E)->getOperatorNew());
1107 if (CT == CT_Can)
1108 return CT;
1109 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1110 }
1111
1112 case Expr::CXXDeleteExprClass: {
1113 CanThrowResult CT;
1114 QualType DTy = cast<CXXDeleteExpr>(E)->getDestroyedType();
1115 if (DTy.isNull() || DTy->isDependentType()) {
1116 CT = CT_Dependent;
1117 } else {
1118 CT = canCalleeThrow(*this, E,
1119 cast<CXXDeleteExpr>(E)->getOperatorDelete());
1120 if (const RecordType *RT = DTy->getAs<RecordType>()) {
1121 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Eli Friedman0423b762013-06-25 01:24:22 +00001122 const CXXDestructorDecl *DD = RD->getDestructor();
1123 if (DD)
1124 CT = mergeCanThrow(CT, canCalleeThrow(*this, E, DD));
Richard Smithf623c962012-04-17 00:58:00 +00001125 }
1126 if (CT == CT_Can)
1127 return CT;
1128 }
1129 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1130 }
1131
1132 case Expr::CXXBindTemporaryExprClass: {
1133 // The bound temporary has to be destroyed again, which might throw.
1134 CanThrowResult CT = canCalleeThrow(*this, E,
1135 cast<CXXBindTemporaryExpr>(E)->getTemporary()->getDestructor());
1136 if (CT == CT_Can)
1137 return CT;
1138 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1139 }
1140
1141 // ObjC message sends are like function calls, but never have exception
1142 // specs.
1143 case Expr::ObjCMessageExprClass:
1144 case Expr::ObjCPropertyRefExprClass:
1145 case Expr::ObjCSubscriptRefExprClass:
1146 return CT_Can;
1147
1148 // All the ObjC literals that are implemented as calls are
1149 // potentially throwing unless we decide to close off that
1150 // possibility.
1151 case Expr::ObjCArrayLiteralClass:
1152 case Expr::ObjCDictionaryLiteralClass:
Patrick Beard0caa3942012-04-19 00:25:12 +00001153 case Expr::ObjCBoxedExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001154 return CT_Can;
1155
1156 // Many other things have subexpressions, so we have to test those.
1157 // Some are simple:
Richard Smith9f690bd2015-10-27 06:02:45 +00001158 case Expr::CoawaitExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001159 case Expr::ConditionalOperatorClass:
1160 case Expr::CompoundLiteralExprClass:
Richard Smith9f690bd2015-10-27 06:02:45 +00001161 case Expr::CoyieldExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001162 case Expr::CXXConstCastExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001163 case Expr::CXXReinterpretCastExprClass:
Richard Smithcc1b96d2013-06-12 22:31:48 +00001164 case Expr::CXXStdInitializerListExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001165 case Expr::DesignatedInitExprClass:
Yunzhong Gaocb779302015-06-10 00:27:52 +00001166 case Expr::DesignatedInitUpdateExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001167 case Expr::ExprWithCleanupsClass:
1168 case Expr::ExtVectorElementExprClass:
1169 case Expr::InitListExprClass:
Richard Smith410306b2016-12-12 02:53:20 +00001170 case Expr::ArrayInitLoopExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001171 case Expr::MemberExprClass:
1172 case Expr::ObjCIsaExprClass:
1173 case Expr::ObjCIvarRefExprClass:
1174 case Expr::ParenExprClass:
1175 case Expr::ParenListExprClass:
1176 case Expr::ShuffleVectorExprClass:
Hal Finkelc4d7c822013-09-18 03:29:45 +00001177 case Expr::ConvertVectorExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001178 case Expr::VAArgExprClass:
1179 return canSubExprsThrow(*this, E);
1180
1181 // Some might be dependent for other reasons.
1182 case Expr::ArraySubscriptExprClass:
Alexey Bataev1a3320e2015-08-25 14:24:04 +00001183 case Expr::OMPArraySectionExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001184 case Expr::BinaryOperatorClass:
Eric Fiselier20f25cb2017-03-06 23:38:15 +00001185 case Expr::DependentCoawaitExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001186 case Expr::CompoundAssignOperatorClass:
1187 case Expr::CStyleCastExprClass:
1188 case Expr::CXXStaticCastExprClass:
1189 case Expr::CXXFunctionalCastExprClass:
1190 case Expr::ImplicitCastExprClass:
1191 case Expr::MaterializeTemporaryExprClass:
1192 case Expr::UnaryOperatorClass: {
1193 CanThrowResult CT = E->isTypeDependent() ? CT_Dependent : CT_Cannot;
1194 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1195 }
1196
1197 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1198 case Expr::StmtExprClass:
1199 return CT_Can;
1200
Richard Smith852c9db2013-04-20 22:23:05 +00001201 case Expr::CXXDefaultArgExprClass:
1202 return canThrow(cast<CXXDefaultArgExpr>(E)->getExpr());
1203
1204 case Expr::CXXDefaultInitExprClass:
1205 return canThrow(cast<CXXDefaultInitExpr>(E)->getExpr());
1206
Richard Smithf623c962012-04-17 00:58:00 +00001207 case Expr::ChooseExprClass:
1208 if (E->isTypeDependent() || E->isValueDependent())
1209 return CT_Dependent;
Eli Friedman75807f22013-07-20 00:40:58 +00001210 return canThrow(cast<ChooseExpr>(E)->getChosenSubExpr());
Richard Smithf623c962012-04-17 00:58:00 +00001211
1212 case Expr::GenericSelectionExprClass:
1213 if (cast<GenericSelectionExpr>(E)->isResultDependent())
1214 return CT_Dependent;
1215 return canThrow(cast<GenericSelectionExpr>(E)->getResultExpr());
1216
1217 // Some expressions are always dependent.
1218 case Expr::CXXDependentScopeMemberExprClass:
1219 case Expr::CXXUnresolvedConstructExprClass:
1220 case Expr::DependentScopeDeclRefExprClass:
Richard Smith0f0af192014-11-08 05:07:16 +00001221 case Expr::CXXFoldExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001222 return CT_Dependent;
1223
1224 case Expr::AsTypeExprClass:
1225 case Expr::BinaryConditionalOperatorClass:
1226 case Expr::BlockExprClass:
1227 case Expr::CUDAKernelCallExprClass:
1228 case Expr::DeclRefExprClass:
1229 case Expr::ObjCBridgedCastExprClass:
1230 case Expr::ObjCIndirectCopyRestoreExprClass:
1231 case Expr::ObjCProtocolExprClass:
1232 case Expr::ObjCSelectorExprClass:
Erik Pilkington29099de2016-07-16 00:35:23 +00001233 case Expr::ObjCAvailabilityCheckExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001234 case Expr::OffsetOfExprClass:
1235 case Expr::PackExpansionExprClass:
1236 case Expr::PseudoObjectExprClass:
1237 case Expr::SubstNonTypeTemplateParmExprClass:
1238 case Expr::SubstNonTypeTemplateParmPackExprClass:
Richard Smithb15fe3a2012-09-12 00:56:43 +00001239 case Expr::FunctionParmPackExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001240 case Expr::UnaryExprOrTypeTraitExprClass:
1241 case Expr::UnresolvedLookupExprClass:
1242 case Expr::UnresolvedMemberExprClass:
Kaelyn Takatae1f49d52014-10-27 18:07:20 +00001243 case Expr::TypoExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001244 // FIXME: Can any of the above throw? If so, when?
1245 return CT_Cannot;
1246
1247 case Expr::AddrLabelExprClass:
1248 case Expr::ArrayTypeTraitExprClass:
1249 case Expr::AtomicExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001250 case Expr::TypeTraitExprClass:
1251 case Expr::CXXBoolLiteralExprClass:
1252 case Expr::CXXNoexceptExprClass:
1253 case Expr::CXXNullPtrLiteralExprClass:
1254 case Expr::CXXPseudoDestructorExprClass:
1255 case Expr::CXXScalarValueInitExprClass:
1256 case Expr::CXXThisExprClass:
1257 case Expr::CXXUuidofExprClass:
1258 case Expr::CharacterLiteralClass:
1259 case Expr::ExpressionTraitExprClass:
1260 case Expr::FloatingLiteralClass:
1261 case Expr::GNUNullExprClass:
1262 case Expr::ImaginaryLiteralClass:
1263 case Expr::ImplicitValueInitExprClass:
1264 case Expr::IntegerLiteralClass:
Richard Smith410306b2016-12-12 02:53:20 +00001265 case Expr::ArrayInitIndexExprClass:
Yunzhong Gaocb779302015-06-10 00:27:52 +00001266 case Expr::NoInitExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001267 case Expr::ObjCEncodeExprClass:
1268 case Expr::ObjCStringLiteralClass:
1269 case Expr::ObjCBoolLiteralExprClass:
1270 case Expr::OpaqueValueExprClass:
1271 case Expr::PredefinedExprClass:
1272 case Expr::SizeOfPackExprClass:
1273 case Expr::StringLiteralClass:
Richard Smithf623c962012-04-17 00:58:00 +00001274 // These expressions can never throw.
1275 return CT_Cannot;
1276
John McCall5e77d762013-04-16 07:28:30 +00001277 case Expr::MSPropertyRefExprClass:
Alexey Bataevf7630272015-11-25 12:01:00 +00001278 case Expr::MSPropertySubscriptExprClass:
John McCall5e77d762013-04-16 07:28:30 +00001279 llvm_unreachable("Invalid class for expression");
1280
Richard Smithf623c962012-04-17 00:58:00 +00001281#define STMT(CLASS, PARENT) case Expr::CLASS##Class:
1282#define STMT_RANGE(Base, First, Last)
1283#define LAST_STMT_RANGE(BASE, FIRST, LAST)
1284#define EXPR(CLASS, PARENT)
1285#define ABSTRACT_STMT(STMT)
1286#include "clang/AST/StmtNodes.inc"
1287 case Expr::NoStmtClass:
1288 llvm_unreachable("Invalid class for expression");
1289 }
1290 llvm_unreachable("Bogus StmtClass");
1291}
1292
Sebastian Redl4915e632009-10-11 09:03:14 +00001293} // end namespace clang