blob: a81c17bb7fa4224ebe05f371b041dc59f94ad975 [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
Richard Smitheaf11ad2018-05-03 03:58:32 +000079ExprResult Sema::ActOnNoexceptSpec(SourceLocation NoexceptLoc,
80 Expr *NoexceptExpr,
81 ExceptionSpecificationType &EST) {
82 // FIXME: This is bogus, a noexcept expression is not a condition.
83 ExprResult Converted = CheckBooleanCondition(NoexceptLoc, NoexceptExpr);
84 if (Converted.isInvalid())
85 return Converted;
86
87 if (Converted.get()->isValueDependent()) {
88 EST = EST_DependentNoexcept;
89 return Converted;
90 }
91
92 llvm::APSInt Result;
93 Converted = VerifyIntegerConstantExpression(
94 Converted.get(), &Result,
95 diag::err_noexcept_needs_constant_expression,
96 /*AllowFold*/ false);
97 if (!Converted.isInvalid())
98 EST = !Result ? EST_NoexceptFalse : EST_NoexceptTrue;
99 return Converted;
100}
101
Sebastian Redl4915e632009-10-11 09:03:14 +0000102/// CheckSpecifiedExceptionType - Check if the given type is valid in an
103/// exception specification. Incomplete types, or pointers to incomplete types
104/// other than void are not allowed.
Richard Smith8606d752012-11-28 22:33:28 +0000105///
106/// \param[in,out] T The exception type. This will be decayed to a pointer type
107/// when the input is an array or a function type.
Craig Toppere335f252015-10-04 04:53:55 +0000108bool Sema::CheckSpecifiedExceptionType(QualType &T, SourceRange Range) {
Richard Smitha118c6a2012-11-28 22:52:42 +0000109 // C++11 [except.spec]p2:
110 // A type cv T, "array of T", or "function returning T" denoted
Richard Smith8606d752012-11-28 22:33:28 +0000111 // in an exception-specification is adjusted to type T, "pointer to T", or
112 // "pointer to function returning T", respectively.
Richard Smitha118c6a2012-11-28 22:52:42 +0000113 //
114 // We also apply this rule in C++98.
Richard Smith8606d752012-11-28 22:33:28 +0000115 if (T->isArrayType())
116 T = Context.getArrayDecayedType(T);
117 else if (T->isFunctionType())
118 T = Context.getPointerType(T);
Sebastian Redl4915e632009-10-11 09:03:14 +0000119
Richard Smitha118c6a2012-11-28 22:52:42 +0000120 int Kind = 0;
Richard Smith8606d752012-11-28 22:33:28 +0000121 QualType PointeeT = T;
Richard Smitha118c6a2012-11-28 22:52:42 +0000122 if (const PointerType *PT = T->getAs<PointerType>()) {
123 PointeeT = PT->getPointeeType();
124 Kind = 1;
Sebastian Redl4915e632009-10-11 09:03:14 +0000125
Richard Smitha118c6a2012-11-28 22:52:42 +0000126 // cv void* is explicitly permitted, despite being a pointer to an
127 // incomplete type.
128 if (PointeeT->isVoidType())
129 return false;
130 } else if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
131 PointeeT = RT->getPointeeType();
132 Kind = 2;
Richard Smith8606d752012-11-28 22:33:28 +0000133
Richard Smitha118c6a2012-11-28 22:52:42 +0000134 if (RT->isRValueReferenceType()) {
135 // C++11 [except.spec]p2:
136 // A type denoted in an exception-specification shall not denote [...]
137 // an rvalue reference type.
138 Diag(Range.getBegin(), diag::err_rref_in_exception_spec)
139 << T << Range;
140 return true;
141 }
142 }
143
144 // C++11 [except.spec]p2:
145 // A type denoted in an exception-specification shall not denote an
146 // incomplete type other than a class currently being defined [...].
147 // A type denoted in an exception-specification shall not denote a
148 // pointer or reference to an incomplete type, other than (cv) void* or a
149 // pointer or reference to a class currently being defined.
David Majnemerb2b0da42016-06-10 18:24:41 +0000150 // In Microsoft mode, downgrade this to a warning.
151 unsigned DiagID = diag::err_incomplete_in_exception_spec;
David Majnemer5d321e62016-06-11 01:25:04 +0000152 bool ReturnValueOnError = true;
153 if (getLangOpts().MicrosoftExt) {
David Majnemerb2b0da42016-06-10 18:24:41 +0000154 DiagID = diag::ext_incomplete_in_exception_spec;
David Majnemer5d321e62016-06-11 01:25:04 +0000155 ReturnValueOnError = false;
156 }
Richard Smitha118c6a2012-11-28 22:52:42 +0000157 if (!(PointeeT->isRecordType() &&
158 PointeeT->getAs<RecordType>()->isBeingDefined()) &&
David Majnemerb2b0da42016-06-10 18:24:41 +0000159 RequireCompleteType(Range.getBegin(), PointeeT, DiagID, Kind, Range))
David Majnemer5d321e62016-06-11 01:25:04 +0000160 return ReturnValueOnError;
Sebastian Redl4915e632009-10-11 09:03:14 +0000161
162 return false;
163}
164
165/// CheckDistantExceptionSpec - Check if the given type is a pointer or pointer
166/// to member to a function with an exception specification. This means that
167/// it is invalid to add another level of indirection.
168bool Sema::CheckDistantExceptionSpec(QualType T) {
Richard Smith3c4f8d22016-10-16 17:54:23 +0000169 // C++17 removes this rule in favor of putting exception specifications into
170 // the type system.
Aaron Ballmanc351fba2017-12-04 20:27:34 +0000171 if (getLangOpts().CPlusPlus17)
Richard Smith3c4f8d22016-10-16 17:54:23 +0000172 return false;
173
Sebastian Redl4915e632009-10-11 09:03:14 +0000174 if (const PointerType *PT = T->getAs<PointerType>())
175 T = PT->getPointeeType();
176 else if (const MemberPointerType *PT = T->getAs<MemberPointerType>())
177 T = PT->getPointeeType();
178 else
179 return false;
180
181 const FunctionProtoType *FnT = T->getAs<FunctionProtoType>();
182 if (!FnT)
183 return false;
184
185 return FnT->hasExceptionSpec();
186}
187
Richard Smithf623c962012-04-17 00:58:00 +0000188const FunctionProtoType *
189Sema::ResolveExceptionSpec(SourceLocation Loc, const FunctionProtoType *FPT) {
Richard Smith0b3a4622014-11-13 20:01:57 +0000190 if (FPT->getExceptionSpecType() == EST_Unparsed) {
191 Diag(Loc, diag::err_exception_spec_not_parsed);
192 return nullptr;
193 }
194
Richard Smithd3b5c9082012-07-27 04:22:15 +0000195 if (!isUnresolvedExceptionSpec(FPT->getExceptionSpecType()))
Richard Smithf623c962012-04-17 00:58:00 +0000196 return FPT;
197
198 FunctionDecl *SourceDecl = FPT->getExceptionSpecDecl();
199 const FunctionProtoType *SourceFPT =
200 SourceDecl->getType()->castAs<FunctionProtoType>();
201
Richard Smithd3b5c9082012-07-27 04:22:15 +0000202 // If the exception specification has already been resolved, just return it.
203 if (!isUnresolvedExceptionSpec(SourceFPT->getExceptionSpecType()))
Richard Smithf623c962012-04-17 00:58:00 +0000204 return SourceFPT;
205
Richard Smithd3b5c9082012-07-27 04:22:15 +0000206 // Compute or instantiate the exception specification now.
Richard Smith3901dfe2013-03-27 00:22:47 +0000207 if (SourceFPT->getExceptionSpecType() == EST_Unevaluated)
Richard Smithd3b5c9082012-07-27 04:22:15 +0000208 EvaluateImplicitExceptionSpec(Loc, cast<CXXMethodDecl>(SourceDecl));
209 else
210 InstantiateExceptionSpec(Loc, SourceDecl);
Richard Smithf623c962012-04-17 00:58:00 +0000211
Davide Italiano922b7022015-07-25 01:19:32 +0000212 const FunctionProtoType *Proto =
213 SourceDecl->getType()->castAs<FunctionProtoType>();
214 if (Proto->getExceptionSpecType() == clang::EST_Unparsed) {
215 Diag(Loc, diag::err_exception_spec_not_parsed);
216 Proto = nullptr;
217 }
218 return Proto;
Richard Smithf623c962012-04-17 00:58:00 +0000219}
220
Richard Smith8acb4282014-07-31 21:57:55 +0000221void
222Sema::UpdateExceptionSpec(FunctionDecl *FD,
223 const FunctionProtoType::ExceptionSpecInfo &ESI) {
Richard Smith564417a2014-03-20 21:47:22 +0000224 // If we've fully resolved the exception specification, notify listeners.
Richard Smith8acb4282014-07-31 21:57:55 +0000225 if (!isUnresolvedExceptionSpec(ESI.Type))
Richard Smith564417a2014-03-20 21:47:22 +0000226 if (auto *Listener = getASTMutationListener())
227 Listener->ResolvedExceptionSpec(FD);
Richard Smith9e2341d2015-03-23 03:25:59 +0000228
George Burgess IV00f70bd2018-03-01 05:43:23 +0000229 for (FunctionDecl *Redecl : FD->redecls())
230 Context.adjustExceptionSpec(Redecl, ESI);
Richard Smith564417a2014-03-20 21:47:22 +0000231}
232
Richard Smith13b40bc2016-11-30 00:13:55 +0000233static bool CheckEquivalentExceptionSpecImpl(
234 Sema &S, const PartialDiagnostic &DiagID, const PartialDiagnostic &NoteID,
235 const FunctionProtoType *Old, SourceLocation OldLoc,
236 const FunctionProtoType *New, SourceLocation NewLoc,
237 bool *MissingExceptionSpecification = nullptr,
238 bool *MissingEmptyExceptionSpecification = nullptr,
239 bool AllowNoexceptAllMatchWithNoSpec = false, bool IsOperatorNew = false);
240
Richard Smith66f3ac92012-10-20 08:26:51 +0000241/// Determine whether a function has an implicitly-generated exception
Richard Smith1ee63522012-10-16 23:30:16 +0000242/// specification.
Richard Smith66f3ac92012-10-20 08:26:51 +0000243static bool hasImplicitExceptionSpec(FunctionDecl *Decl) {
244 if (!isa<CXXDestructorDecl>(Decl) &&
245 Decl->getDeclName().getCXXOverloadedOperator() != OO_Delete &&
246 Decl->getDeclName().getCXXOverloadedOperator() != OO_Array_Delete)
247 return false;
Richard Smith1ee63522012-10-16 23:30:16 +0000248
Richard Smithc7fb2252014-02-07 22:51:16 +0000249 // For a function that the user didn't declare:
250 // - if this is a destructor, its exception specification is implicit.
251 // - if this is 'operator delete' or 'operator delete[]', the exception
252 // specification is as-if an explicit exception specification was given
253 // (per [basic.stc.dynamic]p2).
Richard Smith66f3ac92012-10-20 08:26:51 +0000254 if (!Decl->getTypeSourceInfo())
Richard Smithc7fb2252014-02-07 22:51:16 +0000255 return isa<CXXDestructorDecl>(Decl);
Richard Smith66f3ac92012-10-20 08:26:51 +0000256
257 const FunctionProtoType *Ty =
258 Decl->getTypeSourceInfo()->getType()->getAs<FunctionProtoType>();
259 return !Ty->hasExceptionSpec();
Richard Smith1ee63522012-10-16 23:30:16 +0000260}
261
Douglas Gregorf40863c2010-02-12 07:32:17 +0000262bool Sema::CheckEquivalentExceptionSpec(FunctionDecl *Old, FunctionDecl *New) {
Aaron Ballmanc351fba2017-12-04 20:27:34 +0000263 // Just completely ignore this under -fno-exceptions prior to C++17.
264 // In C++17 onwards, the exception specification is part of the type and
Richard Smith13b40bc2016-11-30 00:13:55 +0000265 // we will diagnose mismatches anyway, so it's better to check for them here.
Aaron Ballmanc351fba2017-12-04 20:27:34 +0000266 if (!getLangOpts().CXXExceptions && !getLangOpts().CPlusPlus17)
Richard Smith13b40bc2016-11-30 00:13:55 +0000267 return false;
268
Sebastian Redlcb5dd002011-03-15 19:52:30 +0000269 OverloadedOperatorKind OO = New->getDeclName().getCXXOverloadedOperator();
270 bool IsOperatorNew = OO == OO_New || OO == OO_Array_New;
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000271 bool MissingExceptionSpecification = false;
Douglas Gregorf40863c2010-02-12 07:32:17 +0000272 bool MissingEmptyExceptionSpecification = false;
Hans Wennborg39a509a2014-02-05 02:37:58 +0000273
Francois Pichet13b4e682011-03-19 23:05:18 +0000274 unsigned DiagID = diag::err_mismatched_exception_spec;
Hans Wennborg39a509a2014-02-05 02:37:58 +0000275 bool ReturnValueOnError = true;
276 if (getLangOpts().MicrosoftExt) {
Richard Smith1b98ccc2014-07-19 01:39:17 +0000277 DiagID = diag::ext_mismatched_exception_spec;
Hans Wennborg39a509a2014-02-05 02:37:58 +0000278 ReturnValueOnError = false;
279 }
Richard Smithf623c962012-04-17 00:58:00 +0000280
Richard Smith1ee63522012-10-16 23:30:16 +0000281 // Check the types as written: they must match before any exception
282 // specification adjustment is applied.
Richard Smith13b40bc2016-11-30 00:13:55 +0000283 if (!CheckEquivalentExceptionSpecImpl(
284 *this, PDiag(DiagID), PDiag(diag::note_previous_declaration),
Richard Smith66f3ac92012-10-20 08:26:51 +0000285 Old->getType()->getAs<FunctionProtoType>(), Old->getLocation(),
286 New->getType()->getAs<FunctionProtoType>(), New->getLocation(),
Richard Smith1ee63522012-10-16 23:30:16 +0000287 &MissingExceptionSpecification, &MissingEmptyExceptionSpecification,
Richard Smith66f3ac92012-10-20 08:26:51 +0000288 /*AllowNoexceptAllMatchWithNoSpec=*/true, IsOperatorNew)) {
289 // C++11 [except.spec]p4 [DR1492]:
290 // If a declaration of a function has an implicit
291 // exception-specification, other declarations of the function shall
292 // not specify an exception-specification.
Richard Smithe3ea0012016-08-31 20:38:32 +0000293 if (getLangOpts().CPlusPlus11 && getLangOpts().CXXExceptions &&
Richard Smith66f3ac92012-10-20 08:26:51 +0000294 hasImplicitExceptionSpec(Old) != hasImplicitExceptionSpec(New)) {
295 Diag(New->getLocation(), diag::ext_implicit_exception_spec_mismatch)
296 << hasImplicitExceptionSpec(Old);
Yaron Keren8b563662015-10-03 10:46:20 +0000297 if (Old->getLocation().isValid())
Richard Smith66f3ac92012-10-20 08:26:51 +0000298 Diag(Old->getLocation(), diag::note_previous_declaration);
299 }
Douglas Gregorf40863c2010-02-12 07:32:17 +0000300 return false;
Richard Smith66f3ac92012-10-20 08:26:51 +0000301 }
Douglas Gregorf40863c2010-02-12 07:32:17 +0000302
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000303 // The failure was something other than an missing exception
Hans Wennborg39a509a2014-02-05 02:37:58 +0000304 // specification; return an error, except in MS mode where this is a warning.
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000305 if (!MissingExceptionSpecification)
Hans Wennborg39a509a2014-02-05 02:37:58 +0000306 return ReturnValueOnError;
Douglas Gregorf40863c2010-02-12 07:32:17 +0000307
Richard Smith66f3ac92012-10-20 08:26:51 +0000308 const FunctionProtoType *NewProto =
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000309 New->getType()->castAs<FunctionProtoType>();
John McCalldb40c7f2010-12-14 08:05:40 +0000310
Douglas Gregorf40863c2010-02-12 07:32:17 +0000311 // The new function declaration is only missing an empty exception
312 // specification "throw()". If the throw() specification came from a
313 // function in a system header that has C linkage, just add an empty
Richard Smith836de6b2016-12-19 23:59:34 +0000314 // exception specification to the "new" declaration. Note that C library
315 // implementations are permitted to add these nothrow exception
316 // specifications.
317 //
318 // Likewise if the old function is a builtin.
John McCalldb40c7f2010-12-14 08:05:40 +0000319 if (MissingEmptyExceptionSpecification && NewProto &&
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000320 (Old->getLocation().isInvalid() ||
Richard Smith836de6b2016-12-19 23:59:34 +0000321 Context.getSourceManager().isInSystemHeader(Old->getLocation()) ||
322 Old->getBuiltinID()) &&
Douglas Gregorf40863c2010-02-12 07:32:17 +0000323 Old->isExternC()) {
Richard Smith8acb4282014-07-31 21:57:55 +0000324 New->setType(Context.getFunctionType(
325 NewProto->getReturnType(), NewProto->getParamTypes(),
326 NewProto->getExtProtoInfo().withExceptionSpec(EST_DynamicNone)));
Douglas Gregorf40863c2010-02-12 07:32:17 +0000327 return false;
328 }
329
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000330 const FunctionProtoType *OldProto =
331 Old->getType()->castAs<FunctionProtoType>();
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000332
Richard Smith8acb4282014-07-31 21:57:55 +0000333 FunctionProtoType::ExceptionSpecInfo ESI = OldProto->getExceptionSpecType();
334 if (ESI.Type == EST_Dynamic) {
Richard Smitheaf11ad2018-05-03 03:58:32 +0000335 // FIXME: What if the exceptions are described in terms of the old
336 // prototype's parameters?
Richard Smith8acb4282014-07-31 21:57:55 +0000337 ESI.Exceptions = OldProto->exceptions();
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000338 }
339
Richard Smitheaf11ad2018-05-03 03:58:32 +0000340 if (ESI.Type == EST_NoexceptFalse)
341 ESI.Type = EST_None;
342 if (ESI.Type == EST_NoexceptTrue)
343 ESI.Type = EST_BasicNoexcept;
344
345 // For dependent noexcept, we can't just take the expression from the old
346 // prototype. It likely contains references to the old prototype's parameters.
347 if (ESI.Type == EST_DependentNoexcept) {
Richard Smitha91de372015-09-30 00:48:50 +0000348 New->setInvalidDecl();
349 } else {
350 // Update the type of the function with the appropriate exception
351 // specification.
352 New->setType(Context.getFunctionType(
353 NewProto->getReturnType(), NewProto->getParamTypes(),
354 NewProto->getExtProtoInfo().withExceptionSpec(ESI)));
355 }
356
Richard Smitheaf11ad2018-05-03 03:58:32 +0000357 if (getLangOpts().MicrosoftExt && ESI.Type != EST_DependentNoexcept) {
David Majnemer06ce8a42015-10-20 20:49:21 +0000358 // Allow missing exception specifications in redeclarations as an extension.
359 DiagID = diag::ext_ms_missing_exception_specification;
360 ReturnValueOnError = false;
361 } else if (New->isReplaceableGlobalAllocationFunction() &&
Richard Smitheaf11ad2018-05-03 03:58:32 +0000362 ESI.Type != EST_DependentNoexcept) {
David Majnemer06ce8a42015-10-20 20:49:21 +0000363 // Allow missing exception specifications in redeclarations as an extension,
364 // when declaring a replaceable global allocation function.
Richard Smitha91de372015-09-30 00:48:50 +0000365 DiagID = diag::ext_missing_exception_specification;
366 ReturnValueOnError = false;
367 } else {
368 DiagID = diag::err_missing_exception_specification;
369 ReturnValueOnError = true;
370 }
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000371
372 // Warn about the lack of exception specification.
373 SmallString<128> ExceptionSpecString;
374 llvm::raw_svector_ostream OS(ExceptionSpecString);
375 switch (OldProto->getExceptionSpecType()) {
376 case EST_DynamicNone:
377 OS << "throw()";
378 break;
379
380 case EST_Dynamic: {
381 OS << "throw(";
382 bool OnFirstException = true;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000383 for (const auto &E : OldProto->exceptions()) {
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000384 if (OnFirstException)
385 OnFirstException = false;
386 else
387 OS << ", ";
388
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000389 OS << E.getAsString(getPrintingPolicy());
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000390 }
391 OS << ")";
392 break;
393 }
394
395 case EST_BasicNoexcept:
396 OS << "noexcept";
397 break;
398
Richard Smitheaf11ad2018-05-03 03:58:32 +0000399 case EST_DependentNoexcept:
400 case EST_NoexceptFalse:
401 case EST_NoexceptTrue:
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000402 OS << "noexcept(";
Richard Trieuddd01ce2014-06-09 22:53:25 +0000403 assert(OldProto->getNoexceptExpr() != nullptr && "Expected non-null Expr");
Craig Topperc3ec1492014-05-26 06:22:03 +0000404 OldProto->getNoexceptExpr()->printPretty(OS, nullptr, getPrintingPolicy());
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000405 OS << ")";
406 break;
407
408 default:
409 llvm_unreachable("This spec type is compatible with none.");
410 }
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000411
412 SourceLocation FixItLoc;
413 if (TypeSourceInfo *TSInfo = New->getTypeSourceInfo()) {
414 TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens();
Richard Smitha91de372015-09-30 00:48:50 +0000415 // FIXME: Preserve enough information so that we can produce a correct fixit
416 // location when there is a trailing return type.
417 if (auto FTLoc = TL.getAs<FunctionProtoTypeLoc>())
418 if (!FTLoc.getTypePtr()->hasTrailingReturn())
419 FixItLoc = getLocForEndOfToken(FTLoc.getLocalRangeEnd());
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000420 }
421
422 if (FixItLoc.isInvalid())
Richard Smitha91de372015-09-30 00:48:50 +0000423 Diag(New->getLocation(), DiagID)
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000424 << New << OS.str();
425 else {
Richard Smitha91de372015-09-30 00:48:50 +0000426 Diag(New->getLocation(), DiagID)
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000427 << New << OS.str()
428 << FixItHint::CreateInsertion(FixItLoc, " " + OS.str().str());
429 }
430
Yaron Keren8b563662015-10-03 10:46:20 +0000431 if (Old->getLocation().isValid())
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000432 Diag(Old->getLocation(), diag::note_previous_declaration);
433
Richard Smitha91de372015-09-30 00:48:50 +0000434 return ReturnValueOnError;
Douglas Gregorf40863c2010-02-12 07:32:17 +0000435}
436
Sebastian Redl4915e632009-10-11 09:03:14 +0000437/// CheckEquivalentExceptionSpec - Check if the two types have equivalent
438/// exception specifications. Exception specifications are equivalent if
439/// they allow exactly the same set of exception types. It does not matter how
440/// that is achieved. See C++ [except.spec]p2.
441bool Sema::CheckEquivalentExceptionSpec(
442 const FunctionProtoType *Old, SourceLocation OldLoc,
443 const FunctionProtoType *New, SourceLocation NewLoc) {
Richard Smith13b40bc2016-11-30 00:13:55 +0000444 if (!getLangOpts().CXXExceptions)
445 return false;
446
Francois Pichet13b4e682011-03-19 23:05:18 +0000447 unsigned DiagID = diag::err_mismatched_exception_spec;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000448 if (getLangOpts().MicrosoftExt)
Richard Smith1b98ccc2014-07-19 01:39:17 +0000449 DiagID = diag::ext_mismatched_exception_spec;
Richard Smith13b40bc2016-11-30 00:13:55 +0000450 bool Result = CheckEquivalentExceptionSpecImpl(
451 *this, PDiag(DiagID), PDiag(diag::note_previous_declaration),
452 Old, OldLoc, New, NewLoc);
Hans Wennborg39a509a2014-02-05 02:37:58 +0000453
454 // In Microsoft mode, mismatching exception specifications just cause a warning.
455 if (getLangOpts().MicrosoftExt)
456 return false;
457 return Result;
Sebastian Redl4915e632009-10-11 09:03:14 +0000458}
459
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000460/// CheckEquivalentExceptionSpec - Check if the two types have compatible
461/// exception specifications. See C++ [except.spec]p3.
Richard Smith66f3ac92012-10-20 08:26:51 +0000462///
463/// \return \c false if the exception specifications match, \c true if there is
464/// a problem. If \c true is returned, either a diagnostic has already been
465/// produced or \c *MissingExceptionSpecification is set to \c true.
Richard Smith13b40bc2016-11-30 00:13:55 +0000466static bool CheckEquivalentExceptionSpecImpl(
467 Sema &S, const PartialDiagnostic &DiagID, const PartialDiagnostic &NoteID,
468 const FunctionProtoType *Old, SourceLocation OldLoc,
469 const FunctionProtoType *New, SourceLocation NewLoc,
470 bool *MissingExceptionSpecification,
471 bool *MissingEmptyExceptionSpecification,
472 bool AllowNoexceptAllMatchWithNoSpec, bool IsOperatorNew) {
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000473 if (MissingExceptionSpecification)
474 *MissingExceptionSpecification = false;
475
Douglas Gregorf40863c2010-02-12 07:32:17 +0000476 if (MissingEmptyExceptionSpecification)
477 *MissingEmptyExceptionSpecification = false;
478
Richard Smith13b40bc2016-11-30 00:13:55 +0000479 Old = S.ResolveExceptionSpec(NewLoc, Old);
Richard Smithf623c962012-04-17 00:58:00 +0000480 if (!Old)
481 return false;
Richard Smith13b40bc2016-11-30 00:13:55 +0000482 New = S.ResolveExceptionSpec(NewLoc, New);
Richard Smithf623c962012-04-17 00:58:00 +0000483 if (!New)
484 return false;
485
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000486 // C++0x [except.spec]p3: Two exception-specifications are compatible if:
487 // - both are non-throwing, regardless of their form,
488 // - both have the form noexcept(constant-expression) and the constant-
489 // expressions are equivalent,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000490 // - both are dynamic-exception-specifications that have the same set of
491 // adjusted types.
492 //
Eric Christophere6b7cf42015-07-10 18:25:52 +0000493 // C++0x [except.spec]p12: An exception-specification is non-throwing if it is
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000494 // of the form throw(), noexcept, or noexcept(constant-expression) where the
495 // constant-expression yields true.
496 //
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000497 // C++0x [except.spec]p4: If any declaration of a function has an exception-
498 // specifier that is not a noexcept-specification allowing all exceptions,
499 // all declarations [...] of that function shall have a compatible
500 // exception-specification.
501 //
502 // That last point basically means that noexcept(false) matches no spec.
503 // It's considered when AllowNoexceptAllMatchWithNoSpec is true.
504
505 ExceptionSpecificationType OldEST = Old->getExceptionSpecType();
506 ExceptionSpecificationType NewEST = New->getExceptionSpecType();
507
Richard Smithd3b5c9082012-07-27 04:22:15 +0000508 assert(!isUnresolvedExceptionSpec(OldEST) &&
509 !isUnresolvedExceptionSpec(NewEST) &&
Richard Smith938f40b2011-06-11 17:19:42 +0000510 "Shouldn't see unknown exception specifications here");
511
Richard Smitheaf11ad2018-05-03 03:58:32 +0000512 CanThrowResult OldCanThrow = Old->canThrow();
513 CanThrowResult NewCanThrow = New->canThrow();
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000514
515 // Any non-throwing specifications are compatible.
Richard Smitheaf11ad2018-05-03 03:58:32 +0000516 if (OldCanThrow == CT_Cannot && NewCanThrow == CT_Cannot)
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000517 return false;
518
Richard Smitheaf11ad2018-05-03 03:58:32 +0000519 // Any throws-anything specifications are usually compatible.
520 if (OldCanThrow == CT_Can && OldEST != EST_Dynamic &&
521 NewCanThrow == CT_Can && NewEST != EST_Dynamic) {
522 // The exception is that the absence of an exception specification only
523 // matches noexcept(false) for functions, as described above.
524 if (!AllowNoexceptAllMatchWithNoSpec &&
525 ((OldEST == EST_None && NewEST == EST_NoexceptFalse) ||
526 (OldEST == EST_NoexceptFalse && NewEST == EST_None))) {
527 // This is the disallowed case.
528 } else {
529 return false;
530 }
531 }
532
533 // FIXME: We treat dependent noexcept specifications as compatible even if
534 // their expressions are not equivalent.
535 if (OldEST == EST_DependentNoexcept && NewEST == EST_DependentNoexcept)
536 return false;
537
538 // Dynamic exception specifications with the same set of adjusted types
539 // are compatible.
540 if (OldEST == EST_Dynamic && NewEST == EST_Dynamic) {
541 bool Success = true;
542 // Both have a dynamic exception spec. Collect the first set, then compare
543 // to the second.
544 llvm::SmallPtrSet<CanQualType, 8> OldTypes, NewTypes;
545 for (const auto &I : Old->exceptions())
546 OldTypes.insert(S.Context.getCanonicalType(I).getUnqualifiedType());
547
548 for (const auto &I : New->exceptions()) {
549 CanQualType TypePtr = S.Context.getCanonicalType(I).getUnqualifiedType();
550 if (OldTypes.count(TypePtr))
551 NewTypes.insert(TypePtr);
552 else {
553 Success = false;
554 break;
555 }
556 }
557
558 if (Success && OldTypes.size() == NewTypes.size())
559 return false;
560 }
561
Sebastian Redlcb5dd002011-03-15 19:52:30 +0000562 // As a special compatibility feature, under C++0x we accept no spec and
563 // throw(std::bad_alloc) as equivalent for operator new and operator new[].
564 // This is because the implicit declaration changed, but old code would break.
Richard Smith13b40bc2016-11-30 00:13:55 +0000565 if (S.getLangOpts().CPlusPlus11 && IsOperatorNew) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000566 const FunctionProtoType *WithExceptions = nullptr;
Sebastian Redlcb5dd002011-03-15 19:52:30 +0000567 if (OldEST == EST_None && NewEST == EST_Dynamic)
568 WithExceptions = New;
569 else if (OldEST == EST_Dynamic && NewEST == EST_None)
570 WithExceptions = Old;
571 if (WithExceptions && WithExceptions->getNumExceptions() == 1) {
572 // One has no spec, the other throw(something). If that something is
573 // std::bad_alloc, all conditions are met.
574 QualType Exception = *WithExceptions->exception_begin();
575 if (CXXRecordDecl *ExRecord = Exception->getAsCXXRecordDecl()) {
576 IdentifierInfo* Name = ExRecord->getIdentifier();
577 if (Name && Name->getName() == "bad_alloc") {
578 // It's called bad_alloc, but is it in std?
Richard Trieuc771d5d2014-05-28 02:16:01 +0000579 if (ExRecord->isInStdNamespace()) {
580 return false;
Sebastian Redlcb5dd002011-03-15 19:52:30 +0000581 }
582 }
583 }
584 }
585 }
586
Richard Smitheaf11ad2018-05-03 03:58:32 +0000587 // If the caller wants to handle the case that the new function is
588 // incompatible due to a missing exception specification, let it.
589 if (MissingExceptionSpecification && OldEST != EST_None &&
590 NewEST == EST_None) {
591 // The old type has an exception specification of some sort, but
592 // the new type does not.
593 *MissingExceptionSpecification = true;
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000594
Richard Smitheaf11ad2018-05-03 03:58:32 +0000595 if (MissingEmptyExceptionSpecification && OldCanThrow == CT_Cannot) {
596 // The old type has a throw() or noexcept(true) exception specification
597 // and the new type has no exception specification, and the caller asked
598 // to handle this itself.
599 *MissingEmptyExceptionSpecification = true;
Douglas Gregorf40863c2010-02-12 07:32:17 +0000600 }
601
Sebastian Redl4915e632009-10-11 09:03:14 +0000602 return true;
603 }
604
Richard Smith13b40bc2016-11-30 00:13:55 +0000605 S.Diag(NewLoc, DiagID);
David Majnemer7da23022015-02-19 07:28:55 +0000606 if (NoteID.getDiagID() != 0 && OldLoc.isValid())
Richard Smith13b40bc2016-11-30 00:13:55 +0000607 S.Diag(OldLoc, NoteID);
Sebastian Redl4915e632009-10-11 09:03:14 +0000608 return true;
609}
610
Richard Smith13b40bc2016-11-30 00:13:55 +0000611bool Sema::CheckEquivalentExceptionSpec(const PartialDiagnostic &DiagID,
612 const PartialDiagnostic &NoteID,
613 const FunctionProtoType *Old,
614 SourceLocation OldLoc,
615 const FunctionProtoType *New,
616 SourceLocation NewLoc) {
617 if (!getLangOpts().CXXExceptions)
618 return false;
619 return CheckEquivalentExceptionSpecImpl(*this, DiagID, NoteID, Old, OldLoc,
620 New, NewLoc);
621}
622
Richard Smith56ae0a62018-01-13 05:05:45 +0000623bool Sema::handlerCanCatch(QualType HandlerType, QualType ExceptionType) {
624 // [except.handle]p3:
625 // A handler is a match for an exception object of type E if:
626
627 // HandlerType must be ExceptionType or derived from it, or pointer or
628 // reference to such types.
629 const ReferenceType *RefTy = HandlerType->getAs<ReferenceType>();
630 if (RefTy)
631 HandlerType = RefTy->getPointeeType();
632
633 // -- the handler is of type cv T or cv T& and E and T are the same type
634 if (Context.hasSameUnqualifiedType(ExceptionType, HandlerType))
635 return true;
636
637 // FIXME: ObjC pointer types?
638 if (HandlerType->isPointerType() || HandlerType->isMemberPointerType()) {
639 if (RefTy && (!HandlerType.isConstQualified() ||
640 HandlerType.isVolatileQualified()))
641 return false;
642
643 // -- the handler is of type cv T or const T& where T is a pointer or
644 // pointer to member type and E is std::nullptr_t
645 if (ExceptionType->isNullPtrType())
646 return true;
647
648 // -- the handler is of type cv T or const T& where T is a pointer or
649 // pointer to member type and E is a pointer or pointer to member type
650 // that can be converted to T by one or more of
651 // -- a qualification conversion
652 // -- a function pointer conversion
653 bool LifetimeConv;
654 QualType Result;
655 // FIXME: Should we treat the exception as catchable if a lifetime
656 // conversion is required?
657 if (IsQualificationConversion(ExceptionType, HandlerType, false,
658 LifetimeConv) ||
659 IsFunctionConversion(ExceptionType, HandlerType, Result))
660 return true;
661
662 // -- a standard pointer conversion [...]
663 if (!ExceptionType->isPointerType() || !HandlerType->isPointerType())
664 return false;
665
666 // Handle the "qualification conversion" portion.
667 Qualifiers EQuals, HQuals;
668 ExceptionType = Context.getUnqualifiedArrayType(
669 ExceptionType->getPointeeType(), EQuals);
670 HandlerType = Context.getUnqualifiedArrayType(
671 HandlerType->getPointeeType(), HQuals);
672 if (!HQuals.compatiblyIncludes(EQuals))
673 return false;
674
675 if (HandlerType->isVoidType() && ExceptionType->isObjectType())
676 return true;
677
678 // The only remaining case is a derived-to-base conversion.
679 }
680
681 // -- the handler is of type cg T or cv T& and T is an unambiguous public
682 // base class of E
683 if (!ExceptionType->isRecordType() || !HandlerType->isRecordType())
684 return false;
685 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
686 /*DetectVirtual=*/false);
687 if (!IsDerivedFrom(SourceLocation(), ExceptionType, HandlerType, Paths) ||
688 Paths.isAmbiguous(Context.getCanonicalType(HandlerType)))
689 return false;
690
691 // Do this check from a context without privileges.
692 switch (CheckBaseClassAccess(SourceLocation(), HandlerType, ExceptionType,
693 Paths.front(),
694 /*Diagnostic*/ 0,
695 /*ForceCheck*/ true,
696 /*ForceUnprivileged*/ true)) {
697 case AR_accessible: return true;
698 case AR_inaccessible: return false;
699 case AR_dependent:
700 llvm_unreachable("access check dependent for unprivileged context");
701 case AR_delayed:
702 llvm_unreachable("access check delayed in non-declaration");
703 }
704 llvm_unreachable("unexpected access check result");
705}
706
Sebastian Redl4915e632009-10-11 09:03:14 +0000707/// CheckExceptionSpecSubset - Check whether the second function type's
708/// exception specification is a subset (or equivalent) of the first function
709/// type. This is used by override and pointer assignment checks.
Richard Smith1be59c52016-10-22 01:32:19 +0000710bool Sema::CheckExceptionSpecSubset(const PartialDiagnostic &DiagID,
711 const PartialDiagnostic &NestedDiagID,
712 const PartialDiagnostic &NoteID,
713 const FunctionProtoType *Superset,
714 SourceLocation SuperLoc,
715 const FunctionProtoType *Subset,
716 SourceLocation SubLoc) {
John McCallf9c94092010-05-28 08:37:35 +0000717
718 // Just auto-succeed under -fno-exceptions.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000719 if (!getLangOpts().CXXExceptions)
John McCallf9c94092010-05-28 08:37:35 +0000720 return false;
721
Sebastian Redl4915e632009-10-11 09:03:14 +0000722 // FIXME: As usual, we could be more specific in our error messages, but
723 // that better waits until we've got types with source locations.
724
725 if (!SubLoc.isValid())
726 SubLoc = SuperLoc;
727
Richard Smithf623c962012-04-17 00:58:00 +0000728 // Resolve the exception specifications, if needed.
729 Superset = ResolveExceptionSpec(SuperLoc, Superset);
730 if (!Superset)
731 return false;
732 Subset = ResolveExceptionSpec(SubLoc, Subset);
733 if (!Subset)
734 return false;
735
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000736 ExceptionSpecificationType SuperEST = Superset->getExceptionSpecType();
Richard Smitheaf11ad2018-05-03 03:58:32 +0000737 ExceptionSpecificationType SubEST = Subset->getExceptionSpecType();
738 assert(!isUnresolvedExceptionSpec(SuperEST) &&
739 !isUnresolvedExceptionSpec(SubEST) &&
740 "Shouldn't see unknown exception specifications here");
Sebastian Redl4915e632009-10-11 09:03:14 +0000741
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000742 // If there are dependent noexcept specs, assume everything is fine. Unlike
743 // with the equivalency check, this is safe in this case, because we don't
744 // want to merge declarations. Checks after instantiation will catch any
745 // omissions we make here.
Richard Smitheaf11ad2018-05-03 03:58:32 +0000746 if (SuperEST == EST_DependentNoexcept || SubEST == EST_DependentNoexcept)
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000747 return false;
748
Richard Smitheaf11ad2018-05-03 03:58:32 +0000749 CanThrowResult SuperCanThrow = Superset->canThrow();
750 CanThrowResult SubCanThrow = Subset->canThrow();
751
752 // If the superset contains everything or the subset contains nothing, we're
753 // done.
754 if ((SuperCanThrow == CT_Can && SuperEST != EST_Dynamic) ||
755 SubCanThrow == CT_Cannot)
Richard Smith1be59c52016-10-22 01:32:19 +0000756 return CheckParamExceptionSpec(NestedDiagID, NoteID, Superset, SuperLoc,
757 Subset, SubLoc);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000758
Richard Smitheaf11ad2018-05-03 03:58:32 +0000759 // If the subset contains everything or the superset contains nothing, we've
760 // failed.
761 if ((SubCanThrow == CT_Can && SubEST != EST_Dynamic) ||
762 SuperCanThrow == CT_Cannot) {
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000763 Diag(SubLoc, DiagID);
764 if (NoteID.getDiagID() != 0)
765 Diag(SuperLoc, NoteID);
766 return true;
767 }
768
769 assert(SuperEST == EST_Dynamic && SubEST == EST_Dynamic &&
770 "Exception spec subset: non-dynamic case slipped through.");
771
772 // Neither contains everything or nothing. Do a proper comparison.
Richard Smith56ae0a62018-01-13 05:05:45 +0000773 for (QualType SubI : Subset->exceptions()) {
774 if (const ReferenceType *RefTy = SubI->getAs<ReferenceType>())
775 SubI = RefTy->getPointeeType();
Sebastian Redl4915e632009-10-11 09:03:14 +0000776
Sebastian Redl4915e632009-10-11 09:03:14 +0000777 // Make sure it's in the superset.
Richard Smith56ae0a62018-01-13 05:05:45 +0000778 bool Contained = false;
779 for (QualType SuperI : Superset->exceptions()) {
780 // [except.spec]p5:
781 // the target entity shall allow at least the exceptions allowed by the
782 // source
783 //
784 // We interpret this as meaning that a handler for some target type would
785 // catch an exception of each source type.
786 if (handlerCanCatch(SuperI, SubI)) {
Sebastian Redl4915e632009-10-11 09:03:14 +0000787 Contained = true;
788 break;
789 }
Sebastian Redl4915e632009-10-11 09:03:14 +0000790 }
791 if (!Contained) {
792 Diag(SubLoc, DiagID);
Sebastian Redla44822f2009-10-14 16:09:29 +0000793 if (NoteID.getDiagID() != 0)
Sebastian Redl4915e632009-10-11 09:03:14 +0000794 Diag(SuperLoc, NoteID);
795 return true;
796 }
797 }
798 // We've run half the gauntlet.
Richard Smith1be59c52016-10-22 01:32:19 +0000799 return CheckParamExceptionSpec(NestedDiagID, NoteID, Superset, SuperLoc,
800 Subset, SubLoc);
Sebastian Redl4915e632009-10-11 09:03:14 +0000801}
802
Richard Smith1be59c52016-10-22 01:32:19 +0000803static bool
804CheckSpecForTypesEquivalent(Sema &S, const PartialDiagnostic &DiagID,
805 const PartialDiagnostic &NoteID, QualType Target,
806 SourceLocation TargetLoc, QualType Source,
807 SourceLocation SourceLoc) {
Sebastian Redl4915e632009-10-11 09:03:14 +0000808 const FunctionProtoType *TFunc = GetUnderlyingFunction(Target);
809 if (!TFunc)
810 return false;
811 const FunctionProtoType *SFunc = GetUnderlyingFunction(Source);
812 if (!SFunc)
813 return false;
814
815 return S.CheckEquivalentExceptionSpec(DiagID, NoteID, TFunc, TargetLoc,
816 SFunc, SourceLoc);
817}
818
819/// CheckParamExceptionSpec - Check if the parameter and return types of the
820/// two functions have equivalent exception specs. This is part of the
821/// assignment and override compatibility check. We do not check the parameters
822/// of parameter function pointers recursively, as no sane programmer would
823/// even be able to write such a function type.
Richard Smith1be59c52016-10-22 01:32:19 +0000824bool Sema::CheckParamExceptionSpec(const PartialDiagnostic &DiagID,
825 const PartialDiagnostic &NoteID,
Richard Smith2e321552014-11-12 02:00:47 +0000826 const FunctionProtoType *Target,
827 SourceLocation TargetLoc,
828 const FunctionProtoType *Source,
829 SourceLocation SourceLoc) {
Richard Smith1be59c52016-10-22 01:32:19 +0000830 auto RetDiag = DiagID;
831 RetDiag << 0;
Alp Toker314cc812014-01-25 16:55:45 +0000832 if (CheckSpecForTypesEquivalent(
Richard Smith1be59c52016-10-22 01:32:19 +0000833 *this, RetDiag, PDiag(),
Alp Toker314cc812014-01-25 16:55:45 +0000834 Target->getReturnType(), TargetLoc, Source->getReturnType(),
835 SourceLoc))
Sebastian Redl4915e632009-10-11 09:03:14 +0000836 return true;
837
Sebastian Redla44822f2009-10-14 16:09:29 +0000838 // We shouldn't even be testing this unless the arguments are otherwise
Sebastian Redl4915e632009-10-11 09:03:14 +0000839 // compatible.
Alp Toker9cacbab2014-01-20 20:26:09 +0000840 assert(Target->getNumParams() == Source->getNumParams() &&
Sebastian Redl4915e632009-10-11 09:03:14 +0000841 "Functions have different argument counts.");
Alp Toker9cacbab2014-01-20 20:26:09 +0000842 for (unsigned i = 0, E = Target->getNumParams(); i != E; ++i) {
Richard Smith1be59c52016-10-22 01:32:19 +0000843 auto ParamDiag = DiagID;
844 ParamDiag << 1;
Alp Toker9cacbab2014-01-20 20:26:09 +0000845 if (CheckSpecForTypesEquivalent(
Richard Smith1be59c52016-10-22 01:32:19 +0000846 *this, ParamDiag, PDiag(),
Alp Toker9cacbab2014-01-20 20:26:09 +0000847 Target->getParamType(i), TargetLoc, Source->getParamType(i),
848 SourceLoc))
Sebastian Redl4915e632009-10-11 09:03:14 +0000849 return true;
850 }
851 return false;
852}
853
Richard Smith2e321552014-11-12 02:00:47 +0000854bool Sema::CheckExceptionSpecCompatibility(Expr *From, QualType ToType) {
Sebastian Redl4915e632009-10-11 09:03:14 +0000855 // First we check for applicability.
856 // Target type must be a function, function pointer or function reference.
857 const FunctionProtoType *ToFunc = GetUnderlyingFunction(ToType);
Richard Smith2e321552014-11-12 02:00:47 +0000858 if (!ToFunc || ToFunc->hasDependentExceptionSpec())
Sebastian Redl4915e632009-10-11 09:03:14 +0000859 return false;
860
861 // SourceType must be a function or function pointer.
862 const FunctionProtoType *FromFunc = GetUnderlyingFunction(From->getType());
Richard Smith2e321552014-11-12 02:00:47 +0000863 if (!FromFunc || FromFunc->hasDependentExceptionSpec())
Sebastian Redl4915e632009-10-11 09:03:14 +0000864 return false;
865
Richard Smith1be59c52016-10-22 01:32:19 +0000866 unsigned DiagID = diag::err_incompatible_exception_specs;
867 unsigned NestedDiagID = diag::err_deep_exception_specs_differ;
868 // This is not an error in C++17 onwards, unless the noexceptness doesn't
869 // match, but in that case we have a full-on type mismatch, not just a
870 // type sugar mismatch.
Aaron Ballmanc351fba2017-12-04 20:27:34 +0000871 if (getLangOpts().CPlusPlus17) {
Richard Smith1be59c52016-10-22 01:32:19 +0000872 DiagID = diag::warn_incompatible_exception_specs;
873 NestedDiagID = diag::warn_deep_exception_specs_differ;
874 }
875
Sebastian Redl4915e632009-10-11 09:03:14 +0000876 // Now we've got the correct types on both sides, check their compatibility.
877 // This means that the source of the conversion can only throw a subset of
878 // the exceptions of the target, and any exception specs on arguments or
879 // return types must be equivalent.
Richard Smith2e321552014-11-12 02:00:47 +0000880 //
881 // FIXME: If there is a nested dependent exception specification, we should
882 // not be checking it here. This is fine:
883 // template<typename T> void f() {
884 // void (*p)(void (*) throw(T));
885 // void (*q)(void (*) throw(int)) = p;
886 // }
887 // ... because it might be instantiated with T=int.
Richard Smith1be59c52016-10-22 01:32:19 +0000888 return CheckExceptionSpecSubset(PDiag(DiagID), PDiag(NestedDiagID), PDiag(),
889 ToFunc, From->getSourceRange().getBegin(),
890 FromFunc, SourceLocation()) &&
Aaron Ballmanc351fba2017-12-04 20:27:34 +0000891 !getLangOpts().CPlusPlus17;
Sebastian Redl4915e632009-10-11 09:03:14 +0000892}
893
894bool Sema::CheckOverridingFunctionExceptionSpec(const CXXMethodDecl *New,
895 const CXXMethodDecl *Old) {
Richard Smith88f45492014-11-22 03:09:05 +0000896 // If the new exception specification hasn't been parsed yet, skip the check.
897 // We'll get called again once it's been parsed.
898 if (New->getType()->castAs<FunctionProtoType>()->getExceptionSpecType() ==
899 EST_Unparsed)
900 return false;
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000901 if (getLangOpts().CPlusPlus11 && isa<CXXDestructorDecl>(New)) {
Sebastian Redl645d9582011-05-20 05:57:18 +0000902 // Don't check uninstantiated template destructors at all. We can only
903 // synthesize correct specs after the template is instantiated.
904 if (New->getParent()->isDependentType())
905 return false;
906 if (New->getParent()->isBeingDefined()) {
907 // The destructor might be updated once the definition is finished. So
908 // remember it and check later.
Richard Smith88f45492014-11-22 03:09:05 +0000909 DelayedExceptionSpecChecks.push_back(std::make_pair(New, Old));
Sebastian Redl645d9582011-05-20 05:57:18 +0000910 return false;
911 }
Sebastian Redl623ea822011-05-19 05:13:44 +0000912 }
Richard Smith88f45492014-11-22 03:09:05 +0000913 // If the old exception specification hasn't been parsed yet, remember that
914 // we need to perform this check when we get to the end of the outermost
915 // lexically-surrounding class.
916 if (Old->getType()->castAs<FunctionProtoType>()->getExceptionSpecType() ==
917 EST_Unparsed) {
918 DelayedExceptionSpecChecks.push_back(std::make_pair(New, Old));
Richard Smith0b3a4622014-11-13 20:01:57 +0000919 return false;
Richard Smith88f45492014-11-22 03:09:05 +0000920 }
Francois Picheta8032e92011-05-24 02:11:43 +0000921 unsigned DiagID = diag::err_override_exception_spec;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000922 if (getLangOpts().MicrosoftExt)
Richard Smith1b98ccc2014-07-19 01:39:17 +0000923 DiagID = diag::ext_override_exception_spec;
Francois Picheta8032e92011-05-24 02:11:43 +0000924 return CheckExceptionSpecSubset(PDiag(DiagID),
Richard Smith1be59c52016-10-22 01:32:19 +0000925 PDiag(diag::err_deep_exception_specs_differ),
Douglas Gregor89336232010-03-29 23:34:08 +0000926 PDiag(diag::note_overridden_virtual_function),
Sebastian Redl4915e632009-10-11 09:03:14 +0000927 Old->getType()->getAs<FunctionProtoType>(),
928 Old->getLocation(),
929 New->getType()->getAs<FunctionProtoType>(),
930 New->getLocation());
931}
932
Benjamin Kramer642f1732015-07-02 21:03:14 +0000933static CanThrowResult canSubExprsThrow(Sema &S, const Expr *E) {
Richard Smithf623c962012-04-17 00:58:00 +0000934 CanThrowResult R = CT_Cannot;
Benjamin Kramer642f1732015-07-02 21:03:14 +0000935 for (const Stmt *SubStmt : E->children()) {
936 R = mergeCanThrow(R, S.canThrow(cast<Expr>(SubStmt)));
937 if (R == CT_Can)
938 break;
939 }
Richard Smithf623c962012-04-17 00:58:00 +0000940 return R;
941}
942
Eli Friedman0423b762013-06-25 01:24:22 +0000943static CanThrowResult canCalleeThrow(Sema &S, const Expr *E, const Decl *D) {
Richard Smithf623c962012-04-17 00:58:00 +0000944 // As an extension, we assume that __attribute__((nothrow)) functions don't
945 // throw.
Richard Smith3a8f13a2016-12-03 00:29:06 +0000946 if (D && isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
Richard Smithf623c962012-04-17 00:58:00 +0000947 return CT_Cannot;
948
Richard Smith3a8f13a2016-12-03 00:29:06 +0000949 QualType T;
950
951 // In C++1z, just look at the function type of the callee.
Aaron Ballmanc351fba2017-12-04 20:27:34 +0000952 if (S.getLangOpts().CPlusPlus17 && isa<CallExpr>(E)) {
Richard Smith3a8f13a2016-12-03 00:29:06 +0000953 E = cast<CallExpr>(E)->getCallee();
954 T = E->getType();
955 if (T->isSpecificPlaceholderType(BuiltinType::BoundMember)) {
956 // Sadly we don't preserve the actual type as part of the "bound member"
957 // placeholder, so we need to reconstruct it.
958 E = E->IgnoreParenImpCasts();
959
960 // Could be a call to a pointer-to-member or a plain member access.
961 if (auto *Op = dyn_cast<BinaryOperator>(E)) {
962 assert(Op->getOpcode() == BO_PtrMemD || Op->getOpcode() == BO_PtrMemI);
963 T = Op->getRHS()->getType()
964 ->castAs<MemberPointerType>()->getPointeeType();
965 } else {
966 T = cast<MemberExpr>(E)->getMemberDecl()->getType();
967 }
968 }
969 } else if (const ValueDecl *VD = dyn_cast_or_null<ValueDecl>(D))
970 T = VD->getType();
971 else
972 // If we have no clue what we're calling, assume the worst.
973 return CT_Can;
974
Richard Smithf623c962012-04-17 00:58:00 +0000975 const FunctionProtoType *FT;
976 if ((FT = T->getAs<FunctionProtoType>())) {
977 } else if (const PointerType *PT = T->getAs<PointerType>())
978 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
979 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
980 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
981 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
982 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
983 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
984 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
985
986 if (!FT)
987 return CT_Can;
988
989 FT = S.ResolveExceptionSpec(E->getLocStart(), FT);
990 if (!FT)
991 return CT_Can;
992
Richard Smitheaf11ad2018-05-03 03:58:32 +0000993 return FT->canThrow();
Richard Smithf623c962012-04-17 00:58:00 +0000994}
995
996static CanThrowResult canDynamicCastThrow(const CXXDynamicCastExpr *DC) {
997 if (DC->isTypeDependent())
998 return CT_Dependent;
999
1000 if (!DC->getTypeAsWritten()->isReferenceType())
1001 return CT_Cannot;
1002
1003 if (DC->getSubExpr()->isTypeDependent())
1004 return CT_Dependent;
1005
1006 return DC->getCastKind() == clang::CK_Dynamic? CT_Can : CT_Cannot;
1007}
1008
1009static CanThrowResult canTypeidThrow(Sema &S, const CXXTypeidExpr *DC) {
1010 if (DC->isTypeOperand())
1011 return CT_Cannot;
1012
1013 Expr *Op = DC->getExprOperand();
1014 if (Op->isTypeDependent())
1015 return CT_Dependent;
1016
1017 const RecordType *RT = Op->getType()->getAs<RecordType>();
1018 if (!RT)
1019 return CT_Cannot;
1020
1021 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1022 return CT_Cannot;
1023
1024 if (Op->Classify(S.Context).isPRValue())
1025 return CT_Cannot;
1026
1027 return CT_Can;
1028}
1029
1030CanThrowResult Sema::canThrow(const Expr *E) {
1031 // C++ [expr.unary.noexcept]p3:
1032 // [Can throw] if in a potentially-evaluated context the expression would
1033 // contain:
1034 switch (E->getStmtClass()) {
1035 case Expr::CXXThrowExprClass:
1036 // - a potentially evaluated throw-expression
1037 return CT_Can;
1038
1039 case Expr::CXXDynamicCastExprClass: {
1040 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1041 // where T is a reference type, that requires a run-time check
1042 CanThrowResult CT = canDynamicCastThrow(cast<CXXDynamicCastExpr>(E));
1043 if (CT == CT_Can)
1044 return CT;
1045 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1046 }
1047
1048 case Expr::CXXTypeidExprClass:
1049 // - a potentially evaluated typeid expression applied to a glvalue
1050 // expression whose type is a polymorphic class type
1051 return canTypeidThrow(*this, cast<CXXTypeidExpr>(E));
1052
1053 // - a potentially evaluated call to a function, member function, function
1054 // pointer, or member function pointer that does not have a non-throwing
1055 // exception-specification
1056 case Expr::CallExprClass:
1057 case Expr::CXXMemberCallExprClass:
1058 case Expr::CXXOperatorCallExprClass:
1059 case Expr::UserDefinedLiteralClass: {
1060 const CallExpr *CE = cast<CallExpr>(E);
1061 CanThrowResult CT;
1062 if (E->isTypeDependent())
1063 CT = CT_Dependent;
1064 else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
1065 CT = CT_Cannot;
Eli Friedman5a8738f2013-06-25 01:55:41 +00001066 else
Richard Smith3a8f13a2016-12-03 00:29:06 +00001067 CT = canCalleeThrow(*this, E, CE->getCalleeDecl());
Richard Smithf623c962012-04-17 00:58:00 +00001068 if (CT == CT_Can)
1069 return CT;
1070 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1071 }
1072
1073 case Expr::CXXConstructExprClass:
1074 case Expr::CXXTemporaryObjectExprClass: {
1075 CanThrowResult CT = canCalleeThrow(*this, E,
1076 cast<CXXConstructExpr>(E)->getConstructor());
1077 if (CT == CT_Can)
1078 return CT;
1079 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1080 }
1081
Richard Smith5179eb72016-06-28 19:03:57 +00001082 case Expr::CXXInheritedCtorInitExprClass:
1083 return canCalleeThrow(*this, E,
1084 cast<CXXInheritedCtorInitExpr>(E)->getConstructor());
1085
Richard Smithf623c962012-04-17 00:58:00 +00001086 case Expr::LambdaExprClass: {
1087 const LambdaExpr *Lambda = cast<LambdaExpr>(E);
1088 CanThrowResult CT = CT_Cannot;
James Y Knight53c76162015-07-17 18:21:37 +00001089 for (LambdaExpr::const_capture_init_iterator
1090 Cap = Lambda->capture_init_begin(),
1091 CapEnd = Lambda->capture_init_end();
Richard Smithf623c962012-04-17 00:58:00 +00001092 Cap != CapEnd; ++Cap)
1093 CT = mergeCanThrow(CT, canThrow(*Cap));
1094 return CT;
1095 }
1096
1097 case Expr::CXXNewExprClass: {
1098 CanThrowResult CT;
1099 if (E->isTypeDependent())
1100 CT = CT_Dependent;
1101 else
1102 CT = canCalleeThrow(*this, E, cast<CXXNewExpr>(E)->getOperatorNew());
1103 if (CT == CT_Can)
1104 return CT;
1105 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1106 }
1107
1108 case Expr::CXXDeleteExprClass: {
1109 CanThrowResult CT;
1110 QualType DTy = cast<CXXDeleteExpr>(E)->getDestroyedType();
1111 if (DTy.isNull() || DTy->isDependentType()) {
1112 CT = CT_Dependent;
1113 } else {
1114 CT = canCalleeThrow(*this, E,
1115 cast<CXXDeleteExpr>(E)->getOperatorDelete());
1116 if (const RecordType *RT = DTy->getAs<RecordType>()) {
1117 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Eli Friedman0423b762013-06-25 01:24:22 +00001118 const CXXDestructorDecl *DD = RD->getDestructor();
1119 if (DD)
1120 CT = mergeCanThrow(CT, canCalleeThrow(*this, E, DD));
Richard Smithf623c962012-04-17 00:58:00 +00001121 }
1122 if (CT == CT_Can)
1123 return CT;
1124 }
1125 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1126 }
1127
1128 case Expr::CXXBindTemporaryExprClass: {
1129 // The bound temporary has to be destroyed again, which might throw.
1130 CanThrowResult CT = canCalleeThrow(*this, E,
1131 cast<CXXBindTemporaryExpr>(E)->getTemporary()->getDestructor());
1132 if (CT == CT_Can)
1133 return CT;
1134 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1135 }
1136
1137 // ObjC message sends are like function calls, but never have exception
1138 // specs.
1139 case Expr::ObjCMessageExprClass:
1140 case Expr::ObjCPropertyRefExprClass:
1141 case Expr::ObjCSubscriptRefExprClass:
1142 return CT_Can;
1143
1144 // All the ObjC literals that are implemented as calls are
1145 // potentially throwing unless we decide to close off that
1146 // possibility.
1147 case Expr::ObjCArrayLiteralClass:
1148 case Expr::ObjCDictionaryLiteralClass:
Patrick Beard0caa3942012-04-19 00:25:12 +00001149 case Expr::ObjCBoxedExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001150 return CT_Can;
1151
1152 // Many other things have subexpressions, so we have to test those.
1153 // Some are simple:
Richard Smith9f690bd2015-10-27 06:02:45 +00001154 case Expr::CoawaitExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001155 case Expr::ConditionalOperatorClass:
1156 case Expr::CompoundLiteralExprClass:
Richard Smith9f690bd2015-10-27 06:02:45 +00001157 case Expr::CoyieldExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001158 case Expr::CXXConstCastExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001159 case Expr::CXXReinterpretCastExprClass:
Richard Smithcc1b96d2013-06-12 22:31:48 +00001160 case Expr::CXXStdInitializerListExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001161 case Expr::DesignatedInitExprClass:
Yunzhong Gaocb779302015-06-10 00:27:52 +00001162 case Expr::DesignatedInitUpdateExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001163 case Expr::ExprWithCleanupsClass:
1164 case Expr::ExtVectorElementExprClass:
1165 case Expr::InitListExprClass:
Richard Smith410306b2016-12-12 02:53:20 +00001166 case Expr::ArrayInitLoopExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001167 case Expr::MemberExprClass:
1168 case Expr::ObjCIsaExprClass:
1169 case Expr::ObjCIvarRefExprClass:
1170 case Expr::ParenExprClass:
1171 case Expr::ParenListExprClass:
1172 case Expr::ShuffleVectorExprClass:
Hal Finkelc4d7c822013-09-18 03:29:45 +00001173 case Expr::ConvertVectorExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001174 case Expr::VAArgExprClass:
1175 return canSubExprsThrow(*this, E);
1176
1177 // Some might be dependent for other reasons.
1178 case Expr::ArraySubscriptExprClass:
Alexey Bataev1a3320e2015-08-25 14:24:04 +00001179 case Expr::OMPArraySectionExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001180 case Expr::BinaryOperatorClass:
Eric Fiselier20f25cb2017-03-06 23:38:15 +00001181 case Expr::DependentCoawaitExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001182 case Expr::CompoundAssignOperatorClass:
1183 case Expr::CStyleCastExprClass:
1184 case Expr::CXXStaticCastExprClass:
1185 case Expr::CXXFunctionalCastExprClass:
1186 case Expr::ImplicitCastExprClass:
1187 case Expr::MaterializeTemporaryExprClass:
1188 case Expr::UnaryOperatorClass: {
1189 CanThrowResult CT = E->isTypeDependent() ? CT_Dependent : CT_Cannot;
1190 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1191 }
1192
1193 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1194 case Expr::StmtExprClass:
1195 return CT_Can;
1196
Richard Smith852c9db2013-04-20 22:23:05 +00001197 case Expr::CXXDefaultArgExprClass:
1198 return canThrow(cast<CXXDefaultArgExpr>(E)->getExpr());
1199
1200 case Expr::CXXDefaultInitExprClass:
1201 return canThrow(cast<CXXDefaultInitExpr>(E)->getExpr());
1202
Richard Smithf623c962012-04-17 00:58:00 +00001203 case Expr::ChooseExprClass:
1204 if (E->isTypeDependent() || E->isValueDependent())
1205 return CT_Dependent;
Eli Friedman75807f22013-07-20 00:40:58 +00001206 return canThrow(cast<ChooseExpr>(E)->getChosenSubExpr());
Richard Smithf623c962012-04-17 00:58:00 +00001207
1208 case Expr::GenericSelectionExprClass:
1209 if (cast<GenericSelectionExpr>(E)->isResultDependent())
1210 return CT_Dependent;
1211 return canThrow(cast<GenericSelectionExpr>(E)->getResultExpr());
1212
1213 // Some expressions are always dependent.
1214 case Expr::CXXDependentScopeMemberExprClass:
1215 case Expr::CXXUnresolvedConstructExprClass:
1216 case Expr::DependentScopeDeclRefExprClass:
Richard Smith0f0af192014-11-08 05:07:16 +00001217 case Expr::CXXFoldExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001218 return CT_Dependent;
1219
1220 case Expr::AsTypeExprClass:
1221 case Expr::BinaryConditionalOperatorClass:
1222 case Expr::BlockExprClass:
1223 case Expr::CUDAKernelCallExprClass:
1224 case Expr::DeclRefExprClass:
1225 case Expr::ObjCBridgedCastExprClass:
1226 case Expr::ObjCIndirectCopyRestoreExprClass:
1227 case Expr::ObjCProtocolExprClass:
1228 case Expr::ObjCSelectorExprClass:
Erik Pilkington29099de2016-07-16 00:35:23 +00001229 case Expr::ObjCAvailabilityCheckExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001230 case Expr::OffsetOfExprClass:
1231 case Expr::PackExpansionExprClass:
1232 case Expr::PseudoObjectExprClass:
1233 case Expr::SubstNonTypeTemplateParmExprClass:
1234 case Expr::SubstNonTypeTemplateParmPackExprClass:
Richard Smithb15fe3a2012-09-12 00:56:43 +00001235 case Expr::FunctionParmPackExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001236 case Expr::UnaryExprOrTypeTraitExprClass:
1237 case Expr::UnresolvedLookupExprClass:
1238 case Expr::UnresolvedMemberExprClass:
Kaelyn Takatae1f49d52014-10-27 18:07:20 +00001239 case Expr::TypoExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001240 // FIXME: Can any of the above throw? If so, when?
1241 return CT_Cannot;
1242
1243 case Expr::AddrLabelExprClass:
1244 case Expr::ArrayTypeTraitExprClass:
1245 case Expr::AtomicExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001246 case Expr::TypeTraitExprClass:
1247 case Expr::CXXBoolLiteralExprClass:
1248 case Expr::CXXNoexceptExprClass:
1249 case Expr::CXXNullPtrLiteralExprClass:
1250 case Expr::CXXPseudoDestructorExprClass:
1251 case Expr::CXXScalarValueInitExprClass:
1252 case Expr::CXXThisExprClass:
1253 case Expr::CXXUuidofExprClass:
1254 case Expr::CharacterLiteralClass:
1255 case Expr::ExpressionTraitExprClass:
1256 case Expr::FloatingLiteralClass:
1257 case Expr::GNUNullExprClass:
1258 case Expr::ImaginaryLiteralClass:
1259 case Expr::ImplicitValueInitExprClass:
1260 case Expr::IntegerLiteralClass:
Richard Smith410306b2016-12-12 02:53:20 +00001261 case Expr::ArrayInitIndexExprClass:
Yunzhong Gaocb779302015-06-10 00:27:52 +00001262 case Expr::NoInitExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001263 case Expr::ObjCEncodeExprClass:
1264 case Expr::ObjCStringLiteralClass:
1265 case Expr::ObjCBoolLiteralExprClass:
1266 case Expr::OpaqueValueExprClass:
1267 case Expr::PredefinedExprClass:
1268 case Expr::SizeOfPackExprClass:
1269 case Expr::StringLiteralClass:
Richard Smithf623c962012-04-17 00:58:00 +00001270 // These expressions can never throw.
1271 return CT_Cannot;
1272
John McCall5e77d762013-04-16 07:28:30 +00001273 case Expr::MSPropertyRefExprClass:
Alexey Bataevf7630272015-11-25 12:01:00 +00001274 case Expr::MSPropertySubscriptExprClass:
John McCall5e77d762013-04-16 07:28:30 +00001275 llvm_unreachable("Invalid class for expression");
1276
Richard Smithf623c962012-04-17 00:58:00 +00001277#define STMT(CLASS, PARENT) case Expr::CLASS##Class:
1278#define STMT_RANGE(Base, First, Last)
1279#define LAST_STMT_RANGE(BASE, FIRST, LAST)
1280#define EXPR(CLASS, PARENT)
1281#define ABSTRACT_STMT(STMT)
1282#include "clang/AST/StmtNodes.inc"
1283 case Expr::NoStmtClass:
1284 llvm_unreachable("Invalid class for expression");
1285 }
1286 llvm_unreachable("Bogus StmtClass");
1287}
1288
Sebastian Redl4915e632009-10-11 09:03:14 +00001289} // end namespace clang