blob: ae728422abc69a0ad728f7b9cf9be4bfe2e79b1d [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 Smith66f3ac92012-10-20 08:26:51 +0000210/// Determine whether a function has an implicitly-generated exception
Richard Smith1ee63522012-10-16 23:30:16 +0000211/// specification.
Richard Smith66f3ac92012-10-20 08:26:51 +0000212static bool hasImplicitExceptionSpec(FunctionDecl *Decl) {
213 if (!isa<CXXDestructorDecl>(Decl) &&
214 Decl->getDeclName().getCXXOverloadedOperator() != OO_Delete &&
215 Decl->getDeclName().getCXXOverloadedOperator() != OO_Array_Delete)
216 return false;
Richard Smith1ee63522012-10-16 23:30:16 +0000217
Richard Smithc7fb2252014-02-07 22:51:16 +0000218 // For a function that the user didn't declare:
219 // - if this is a destructor, its exception specification is implicit.
220 // - if this is 'operator delete' or 'operator delete[]', the exception
221 // specification is as-if an explicit exception specification was given
222 // (per [basic.stc.dynamic]p2).
Richard Smith66f3ac92012-10-20 08:26:51 +0000223 if (!Decl->getTypeSourceInfo())
Richard Smithc7fb2252014-02-07 22:51:16 +0000224 return isa<CXXDestructorDecl>(Decl);
Richard Smith66f3ac92012-10-20 08:26:51 +0000225
226 const FunctionProtoType *Ty =
227 Decl->getTypeSourceInfo()->getType()->getAs<FunctionProtoType>();
228 return !Ty->hasExceptionSpec();
Richard Smith1ee63522012-10-16 23:30:16 +0000229}
230
Douglas Gregorf40863c2010-02-12 07:32:17 +0000231bool Sema::CheckEquivalentExceptionSpec(FunctionDecl *Old, FunctionDecl *New) {
Sebastian Redlcb5dd002011-03-15 19:52:30 +0000232 OverloadedOperatorKind OO = New->getDeclName().getCXXOverloadedOperator();
233 bool IsOperatorNew = OO == OO_New || OO == OO_Array_New;
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000234 bool MissingExceptionSpecification = false;
Douglas Gregorf40863c2010-02-12 07:32:17 +0000235 bool MissingEmptyExceptionSpecification = false;
Hans Wennborg39a509a2014-02-05 02:37:58 +0000236
Francois Pichet13b4e682011-03-19 23:05:18 +0000237 unsigned DiagID = diag::err_mismatched_exception_spec;
Hans Wennborg39a509a2014-02-05 02:37:58 +0000238 bool ReturnValueOnError = true;
239 if (getLangOpts().MicrosoftExt) {
Richard Smith1b98ccc2014-07-19 01:39:17 +0000240 DiagID = diag::ext_mismatched_exception_spec;
Hans Wennborg39a509a2014-02-05 02:37:58 +0000241 ReturnValueOnError = false;
242 }
Richard Smithf623c962012-04-17 00:58:00 +0000243
Richard Smith1ee63522012-10-16 23:30:16 +0000244 // Check the types as written: they must match before any exception
245 // specification adjustment is applied.
246 if (!CheckEquivalentExceptionSpec(
247 PDiag(DiagID), PDiag(diag::note_previous_declaration),
Richard Smith66f3ac92012-10-20 08:26:51 +0000248 Old->getType()->getAs<FunctionProtoType>(), Old->getLocation(),
249 New->getType()->getAs<FunctionProtoType>(), New->getLocation(),
Richard Smith1ee63522012-10-16 23:30:16 +0000250 &MissingExceptionSpecification, &MissingEmptyExceptionSpecification,
Richard Smith66f3ac92012-10-20 08:26:51 +0000251 /*AllowNoexceptAllMatchWithNoSpec=*/true, IsOperatorNew)) {
252 // C++11 [except.spec]p4 [DR1492]:
253 // If a declaration of a function has an implicit
254 // exception-specification, other declarations of the function shall
255 // not specify an exception-specification.
Richard Smithe3ea0012016-08-31 20:38:32 +0000256 if (getLangOpts().CPlusPlus11 && getLangOpts().CXXExceptions &&
Richard Smith66f3ac92012-10-20 08:26:51 +0000257 hasImplicitExceptionSpec(Old) != hasImplicitExceptionSpec(New)) {
258 Diag(New->getLocation(), diag::ext_implicit_exception_spec_mismatch)
259 << hasImplicitExceptionSpec(Old);
Yaron Keren8b563662015-10-03 10:46:20 +0000260 if (Old->getLocation().isValid())
Richard Smith66f3ac92012-10-20 08:26:51 +0000261 Diag(Old->getLocation(), diag::note_previous_declaration);
262 }
Douglas Gregorf40863c2010-02-12 07:32:17 +0000263 return false;
Richard Smith66f3ac92012-10-20 08:26:51 +0000264 }
Douglas Gregorf40863c2010-02-12 07:32:17 +0000265
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000266 // The failure was something other than an missing exception
Hans Wennborg39a509a2014-02-05 02:37:58 +0000267 // specification; return an error, except in MS mode where this is a warning.
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000268 if (!MissingExceptionSpecification)
Hans Wennborg39a509a2014-02-05 02:37:58 +0000269 return ReturnValueOnError;
Douglas Gregorf40863c2010-02-12 07:32:17 +0000270
Richard Smith66f3ac92012-10-20 08:26:51 +0000271 const FunctionProtoType *NewProto =
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000272 New->getType()->castAs<FunctionProtoType>();
John McCalldb40c7f2010-12-14 08:05:40 +0000273
Douglas Gregorf40863c2010-02-12 07:32:17 +0000274 // The new function declaration is only missing an empty exception
275 // specification "throw()". If the throw() specification came from a
276 // function in a system header that has C linkage, just add an empty
277 // exception specification to the "new" declaration. This is an
278 // egregious workaround for glibc, which adds throw() specifications
279 // to many libc functions as an optimization. Unfortunately, that
280 // optimization isn't permitted by the C++ standard, so we're forced
281 // to work around it here.
John McCalldb40c7f2010-12-14 08:05:40 +0000282 if (MissingEmptyExceptionSpecification && NewProto &&
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000283 (Old->getLocation().isInvalid() ||
284 Context.getSourceManager().isInSystemHeader(Old->getLocation())) &&
Douglas Gregorf40863c2010-02-12 07:32:17 +0000285 Old->isExternC()) {
Richard Smith8acb4282014-07-31 21:57:55 +0000286 New->setType(Context.getFunctionType(
287 NewProto->getReturnType(), NewProto->getParamTypes(),
288 NewProto->getExtProtoInfo().withExceptionSpec(EST_DynamicNone)));
Douglas Gregorf40863c2010-02-12 07:32:17 +0000289 return false;
290 }
291
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000292 const FunctionProtoType *OldProto =
293 Old->getType()->castAs<FunctionProtoType>();
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000294
Richard Smith8acb4282014-07-31 21:57:55 +0000295 FunctionProtoType::ExceptionSpecInfo ESI = OldProto->getExceptionSpecType();
296 if (ESI.Type == EST_Dynamic) {
297 ESI.Exceptions = OldProto->exceptions();
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000298 }
299
Richard Smitha91de372015-09-30 00:48:50 +0000300 if (ESI.Type == EST_ComputedNoexcept) {
301 // For computed noexcept, we can't just take the expression from the old
302 // prototype. It likely contains references to the old prototype's
303 // parameters.
304 New->setInvalidDecl();
305 } else {
306 // Update the type of the function with the appropriate exception
307 // specification.
308 New->setType(Context.getFunctionType(
309 NewProto->getReturnType(), NewProto->getParamTypes(),
310 NewProto->getExtProtoInfo().withExceptionSpec(ESI)));
311 }
312
David Majnemer06ce8a42015-10-20 20:49:21 +0000313 if (getLangOpts().MicrosoftExt && ESI.Type != EST_ComputedNoexcept) {
314 // Allow missing exception specifications in redeclarations as an extension.
315 DiagID = diag::ext_ms_missing_exception_specification;
316 ReturnValueOnError = false;
317 } else if (New->isReplaceableGlobalAllocationFunction() &&
318 ESI.Type != EST_ComputedNoexcept) {
319 // Allow missing exception specifications in redeclarations as an extension,
320 // when declaring a replaceable global allocation function.
Richard Smitha91de372015-09-30 00:48:50 +0000321 DiagID = diag::ext_missing_exception_specification;
322 ReturnValueOnError = false;
323 } else {
324 DiagID = diag::err_missing_exception_specification;
325 ReturnValueOnError = true;
326 }
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000327
328 // Warn about the lack of exception specification.
329 SmallString<128> ExceptionSpecString;
330 llvm::raw_svector_ostream OS(ExceptionSpecString);
331 switch (OldProto->getExceptionSpecType()) {
332 case EST_DynamicNone:
333 OS << "throw()";
334 break;
335
336 case EST_Dynamic: {
337 OS << "throw(";
338 bool OnFirstException = true;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000339 for (const auto &E : OldProto->exceptions()) {
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000340 if (OnFirstException)
341 OnFirstException = false;
342 else
343 OS << ", ";
344
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000345 OS << E.getAsString(getPrintingPolicy());
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000346 }
347 OS << ")";
348 break;
349 }
350
351 case EST_BasicNoexcept:
352 OS << "noexcept";
353 break;
354
355 case EST_ComputedNoexcept:
356 OS << "noexcept(";
Richard Trieuddd01ce2014-06-09 22:53:25 +0000357 assert(OldProto->getNoexceptExpr() != nullptr && "Expected non-null Expr");
Craig Topperc3ec1492014-05-26 06:22:03 +0000358 OldProto->getNoexceptExpr()->printPretty(OS, nullptr, getPrintingPolicy());
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000359 OS << ")";
360 break;
361
362 default:
363 llvm_unreachable("This spec type is compatible with none.");
364 }
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000365
366 SourceLocation FixItLoc;
367 if (TypeSourceInfo *TSInfo = New->getTypeSourceInfo()) {
368 TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens();
Richard Smitha91de372015-09-30 00:48:50 +0000369 // FIXME: Preserve enough information so that we can produce a correct fixit
370 // location when there is a trailing return type.
371 if (auto FTLoc = TL.getAs<FunctionProtoTypeLoc>())
372 if (!FTLoc.getTypePtr()->hasTrailingReturn())
373 FixItLoc = getLocForEndOfToken(FTLoc.getLocalRangeEnd());
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000374 }
375
376 if (FixItLoc.isInvalid())
Richard Smitha91de372015-09-30 00:48:50 +0000377 Diag(New->getLocation(), DiagID)
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000378 << New << OS.str();
379 else {
Richard Smitha91de372015-09-30 00:48:50 +0000380 Diag(New->getLocation(), DiagID)
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000381 << New << OS.str()
382 << FixItHint::CreateInsertion(FixItLoc, " " + OS.str().str());
383 }
384
Yaron Keren8b563662015-10-03 10:46:20 +0000385 if (Old->getLocation().isValid())
Eli Friedman96dbf6f2013-06-25 00:46:32 +0000386 Diag(Old->getLocation(), diag::note_previous_declaration);
387
Richard Smitha91de372015-09-30 00:48:50 +0000388 return ReturnValueOnError;
Douglas Gregorf40863c2010-02-12 07:32:17 +0000389}
390
Sebastian Redl4915e632009-10-11 09:03:14 +0000391/// CheckEquivalentExceptionSpec - Check if the two types have equivalent
392/// exception specifications. Exception specifications are equivalent if
393/// they allow exactly the same set of exception types. It does not matter how
394/// that is achieved. See C++ [except.spec]p2.
395bool Sema::CheckEquivalentExceptionSpec(
396 const FunctionProtoType *Old, SourceLocation OldLoc,
397 const FunctionProtoType *New, SourceLocation NewLoc) {
Francois Pichet13b4e682011-03-19 23:05:18 +0000398 unsigned DiagID = diag::err_mismatched_exception_spec;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000399 if (getLangOpts().MicrosoftExt)
Richard Smith1b98ccc2014-07-19 01:39:17 +0000400 DiagID = diag::ext_mismatched_exception_spec;
Hans Wennborg39a509a2014-02-05 02:37:58 +0000401 bool Result = CheckEquivalentExceptionSpec(PDiag(DiagID),
402 PDiag(diag::note_previous_declaration), Old, OldLoc, New, NewLoc);
403
404 // In Microsoft mode, mismatching exception specifications just cause a warning.
405 if (getLangOpts().MicrosoftExt)
406 return false;
407 return Result;
Sebastian Redl4915e632009-10-11 09:03:14 +0000408}
409
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000410/// CheckEquivalentExceptionSpec - Check if the two types have compatible
411/// exception specifications. See C++ [except.spec]p3.
Richard Smith66f3ac92012-10-20 08:26:51 +0000412///
413/// \return \c false if the exception specifications match, \c true if there is
414/// a problem. If \c true is returned, either a diagnostic has already been
415/// produced or \c *MissingExceptionSpecification is set to \c true.
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000416bool Sema::CheckEquivalentExceptionSpec(const PartialDiagnostic &DiagID,
Richard Smith1be59c52016-10-22 01:32:19 +0000417 const PartialDiagnostic &NoteID,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000418 const FunctionProtoType *Old,
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000419 SourceLocation OldLoc,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000420 const FunctionProtoType *New,
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000421 SourceLocation NewLoc,
422 bool *MissingExceptionSpecification,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000423 bool*MissingEmptyExceptionSpecification,
Sebastian Redlcb5dd002011-03-15 19:52:30 +0000424 bool AllowNoexceptAllMatchWithNoSpec,
425 bool IsOperatorNew) {
John McCallf9c94092010-05-28 08:37:35 +0000426 // Just completely ignore this under -fno-exceptions.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000427 if (!getLangOpts().CXXExceptions)
John McCallf9c94092010-05-28 08:37:35 +0000428 return false;
429
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000430 if (MissingExceptionSpecification)
431 *MissingExceptionSpecification = false;
432
Douglas Gregorf40863c2010-02-12 07:32:17 +0000433 if (MissingEmptyExceptionSpecification)
434 *MissingEmptyExceptionSpecification = false;
435
Richard Smithf623c962012-04-17 00:58:00 +0000436 Old = ResolveExceptionSpec(NewLoc, Old);
437 if (!Old)
438 return false;
439 New = ResolveExceptionSpec(NewLoc, New);
440 if (!New)
441 return false;
442
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000443 // C++0x [except.spec]p3: Two exception-specifications are compatible if:
444 // - both are non-throwing, regardless of their form,
445 // - both have the form noexcept(constant-expression) and the constant-
446 // expressions are equivalent,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000447 // - both are dynamic-exception-specifications that have the same set of
448 // adjusted types.
449 //
Eric Christophere6b7cf42015-07-10 18:25:52 +0000450 // C++0x [except.spec]p12: An exception-specification is non-throwing if it is
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000451 // of the form throw(), noexcept, or noexcept(constant-expression) where the
452 // constant-expression yields true.
453 //
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000454 // C++0x [except.spec]p4: If any declaration of a function has an exception-
455 // specifier that is not a noexcept-specification allowing all exceptions,
456 // all declarations [...] of that function shall have a compatible
457 // exception-specification.
458 //
459 // That last point basically means that noexcept(false) matches no spec.
460 // It's considered when AllowNoexceptAllMatchWithNoSpec is true.
461
462 ExceptionSpecificationType OldEST = Old->getExceptionSpecType();
463 ExceptionSpecificationType NewEST = New->getExceptionSpecType();
464
Richard Smithd3b5c9082012-07-27 04:22:15 +0000465 assert(!isUnresolvedExceptionSpec(OldEST) &&
466 !isUnresolvedExceptionSpec(NewEST) &&
Richard Smith938f40b2011-06-11 17:19:42 +0000467 "Shouldn't see unknown exception specifications here");
468
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000469 // Shortcut the case where both have no spec.
470 if (OldEST == EST_None && NewEST == EST_None)
471 return false;
472
Sebastian Redl31ad7542011-03-13 17:09:40 +0000473 FunctionProtoType::NoexceptResult OldNR = Old->getNoexceptSpec(Context);
474 FunctionProtoType::NoexceptResult NewNR = New->getNoexceptSpec(Context);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000475 if (OldNR == FunctionProtoType::NR_BadNoexcept ||
476 NewNR == FunctionProtoType::NR_BadNoexcept)
477 return false;
478
479 // Dependent noexcept specifiers are compatible with each other, but nothing
480 // else.
481 // One noexcept is compatible with another if the argument is the same
482 if (OldNR == NewNR &&
483 OldNR != FunctionProtoType::NR_NoNoexcept &&
484 NewNR != FunctionProtoType::NR_NoNoexcept)
485 return false;
486 if (OldNR != NewNR &&
487 OldNR != FunctionProtoType::NR_NoNoexcept &&
488 NewNR != FunctionProtoType::NR_NoNoexcept) {
489 Diag(NewLoc, DiagID);
David Majnemer7da23022015-02-19 07:28:55 +0000490 if (NoteID.getDiagID() != 0 && OldLoc.isValid())
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000491 Diag(OldLoc, NoteID);
492 return true;
Douglas Gregorf62c5292010-08-30 15:04:51 +0000493 }
494
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000495 // The MS extension throw(...) is compatible with itself.
496 if (OldEST == EST_MSAny && NewEST == EST_MSAny)
Sebastian Redl4915e632009-10-11 09:03:14 +0000497 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000498
499 // It's also compatible with no spec.
500 if ((OldEST == EST_None && NewEST == EST_MSAny) ||
501 (OldEST == EST_MSAny && NewEST == EST_None))
502 return false;
503
504 // It's also compatible with noexcept(false).
505 if (OldEST == EST_MSAny && NewNR == FunctionProtoType::NR_Throw)
506 return false;
507 if (NewEST == EST_MSAny && OldNR == FunctionProtoType::NR_Throw)
508 return false;
509
510 // As described above, noexcept(false) matches no spec only for functions.
511 if (AllowNoexceptAllMatchWithNoSpec) {
512 if (OldEST == EST_None && NewNR == FunctionProtoType::NR_Throw)
513 return false;
514 if (NewEST == EST_None && OldNR == FunctionProtoType::NR_Throw)
515 return false;
516 }
517
518 // Any non-throwing specifications are compatible.
519 bool OldNonThrowing = OldNR == FunctionProtoType::NR_Nothrow ||
520 OldEST == EST_DynamicNone;
521 bool NewNonThrowing = NewNR == FunctionProtoType::NR_Nothrow ||
522 NewEST == EST_DynamicNone;
523 if (OldNonThrowing && NewNonThrowing)
524 return false;
525
Sebastian Redlcb5dd002011-03-15 19:52:30 +0000526 // As a special compatibility feature, under C++0x we accept no spec and
527 // throw(std::bad_alloc) as equivalent for operator new and operator new[].
528 // This is because the implicit declaration changed, but old code would break.
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000529 if (getLangOpts().CPlusPlus11 && IsOperatorNew) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000530 const FunctionProtoType *WithExceptions = nullptr;
Sebastian Redlcb5dd002011-03-15 19:52:30 +0000531 if (OldEST == EST_None && NewEST == EST_Dynamic)
532 WithExceptions = New;
533 else if (OldEST == EST_Dynamic && NewEST == EST_None)
534 WithExceptions = Old;
535 if (WithExceptions && WithExceptions->getNumExceptions() == 1) {
536 // One has no spec, the other throw(something). If that something is
537 // std::bad_alloc, all conditions are met.
538 QualType Exception = *WithExceptions->exception_begin();
539 if (CXXRecordDecl *ExRecord = Exception->getAsCXXRecordDecl()) {
540 IdentifierInfo* Name = ExRecord->getIdentifier();
541 if (Name && Name->getName() == "bad_alloc") {
542 // It's called bad_alloc, but is it in std?
Richard Trieuc771d5d2014-05-28 02:16:01 +0000543 if (ExRecord->isInStdNamespace()) {
544 return false;
Sebastian Redlcb5dd002011-03-15 19:52:30 +0000545 }
546 }
547 }
548 }
549 }
550
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000551 // At this point, the only remaining valid case is two matching dynamic
552 // specifications. We return here unless both specifications are dynamic.
553 if (OldEST != EST_Dynamic || NewEST != EST_Dynamic) {
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000554 if (MissingExceptionSpecification && Old->hasExceptionSpec() &&
Douglas Gregorf40863c2010-02-12 07:32:17 +0000555 !New->hasExceptionSpec()) {
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000556 // The old type has an exception specification of some sort, but
557 // the new type does not.
558 *MissingExceptionSpecification = true;
559
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000560 if (MissingEmptyExceptionSpecification && OldNonThrowing) {
561 // The old type has a throw() or noexcept(true) exception specification
562 // and the new type has no exception specification, and the caller asked
Douglas Gregord6bc5e62010-03-24 07:14:45 +0000563 // to handle this itself.
564 *MissingEmptyExceptionSpecification = true;
565 }
566
Douglas Gregorf40863c2010-02-12 07:32:17 +0000567 return true;
568 }
569
Sebastian Redl4915e632009-10-11 09:03:14 +0000570 Diag(NewLoc, DiagID);
David Majnemer7da23022015-02-19 07:28:55 +0000571 if (NoteID.getDiagID() != 0 && OldLoc.isValid())
Sebastian Redl4915e632009-10-11 09:03:14 +0000572 Diag(OldLoc, NoteID);
573 return true;
574 }
575
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000576 assert(OldEST == EST_Dynamic && NewEST == EST_Dynamic &&
577 "Exception compatibility logic error: non-dynamic spec slipped through.");
578
Sebastian Redl4915e632009-10-11 09:03:14 +0000579 bool Success = true;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000580 // Both have a dynamic exception spec. Collect the first set, then compare
Sebastian Redl4915e632009-10-11 09:03:14 +0000581 // to the second.
Sebastian Redl184edca2009-10-14 15:06:25 +0000582 llvm::SmallPtrSet<CanQualType, 8> OldTypes, NewTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000583 for (const auto &I : Old->exceptions())
584 OldTypes.insert(Context.getCanonicalType(I).getUnqualifiedType());
Sebastian Redl4915e632009-10-11 09:03:14 +0000585
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000586 for (const auto &I : New->exceptions()) {
587 CanQualType TypePtr = Context.getCanonicalType(I).getUnqualifiedType();
Sebastian Redl6e4c8712009-10-11 09:11:23 +0000588 if(OldTypes.count(TypePtr))
589 NewTypes.insert(TypePtr);
590 else
591 Success = false;
592 }
Sebastian Redl4915e632009-10-11 09:03:14 +0000593
Sebastian Redl6e4c8712009-10-11 09:11:23 +0000594 Success = Success && OldTypes.size() == NewTypes.size();
Sebastian Redl4915e632009-10-11 09:03:14 +0000595
596 if (Success) {
597 return false;
598 }
599 Diag(NewLoc, DiagID);
David Majnemer7da23022015-02-19 07:28:55 +0000600 if (NoteID.getDiagID() != 0 && OldLoc.isValid())
Sebastian Redl4915e632009-10-11 09:03:14 +0000601 Diag(OldLoc, NoteID);
602 return true;
603}
604
605/// CheckExceptionSpecSubset - Check whether the second function type's
606/// exception specification is a subset (or equivalent) of the first function
607/// type. This is used by override and pointer assignment checks.
Richard Smith1be59c52016-10-22 01:32:19 +0000608bool Sema::CheckExceptionSpecSubset(const PartialDiagnostic &DiagID,
609 const PartialDiagnostic &NestedDiagID,
610 const PartialDiagnostic &NoteID,
611 const FunctionProtoType *Superset,
612 SourceLocation SuperLoc,
613 const FunctionProtoType *Subset,
614 SourceLocation SubLoc) {
John McCallf9c94092010-05-28 08:37:35 +0000615
616 // Just auto-succeed under -fno-exceptions.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000617 if (!getLangOpts().CXXExceptions)
John McCallf9c94092010-05-28 08:37:35 +0000618 return false;
619
Sebastian Redl4915e632009-10-11 09:03:14 +0000620 // FIXME: As usual, we could be more specific in our error messages, but
621 // that better waits until we've got types with source locations.
622
623 if (!SubLoc.isValid())
624 SubLoc = SuperLoc;
625
Richard Smithf623c962012-04-17 00:58:00 +0000626 // Resolve the exception specifications, if needed.
627 Superset = ResolveExceptionSpec(SuperLoc, Superset);
628 if (!Superset)
629 return false;
630 Subset = ResolveExceptionSpec(SubLoc, Subset);
631 if (!Subset)
632 return false;
633
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000634 ExceptionSpecificationType SuperEST = Superset->getExceptionSpecType();
635
Sebastian Redl4915e632009-10-11 09:03:14 +0000636 // If superset contains everything, we're done.
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000637 if (SuperEST == EST_None || SuperEST == EST_MSAny)
Richard Smith1be59c52016-10-22 01:32:19 +0000638 return CheckParamExceptionSpec(NestedDiagID, NoteID, Superset, SuperLoc,
639 Subset, SubLoc);
Sebastian Redl4915e632009-10-11 09:03:14 +0000640
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000641 // If there are dependent noexcept specs, assume everything is fine. Unlike
642 // with the equivalency check, this is safe in this case, because we don't
643 // want to merge declarations. Checks after instantiation will catch any
644 // omissions we make here.
645 // We also shortcut checking if a noexcept expression was bad.
646
Sebastian Redl31ad7542011-03-13 17:09:40 +0000647 FunctionProtoType::NoexceptResult SuperNR =Superset->getNoexceptSpec(Context);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000648 if (SuperNR == FunctionProtoType::NR_BadNoexcept ||
649 SuperNR == FunctionProtoType::NR_Dependent)
650 return false;
651
652 // Another case of the superset containing everything.
653 if (SuperNR == FunctionProtoType::NR_Throw)
Richard Smith1be59c52016-10-22 01:32:19 +0000654 return CheckParamExceptionSpec(NestedDiagID, NoteID, Superset, SuperLoc,
655 Subset, SubLoc);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000656
657 ExceptionSpecificationType SubEST = Subset->getExceptionSpecType();
658
Richard Smithd3b5c9082012-07-27 04:22:15 +0000659 assert(!isUnresolvedExceptionSpec(SuperEST) &&
660 !isUnresolvedExceptionSpec(SubEST) &&
Richard Smith938f40b2011-06-11 17:19:42 +0000661 "Shouldn't see unknown exception specifications here");
662
Sebastian Redl4915e632009-10-11 09:03:14 +0000663 // It does not. If the subset contains everything, we've failed.
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000664 if (SubEST == EST_None || SubEST == EST_MSAny) {
Sebastian Redl4915e632009-10-11 09:03:14 +0000665 Diag(SubLoc, DiagID);
Sebastian Redla44822f2009-10-14 16:09:29 +0000666 if (NoteID.getDiagID() != 0)
Sebastian Redl4915e632009-10-11 09:03:14 +0000667 Diag(SuperLoc, NoteID);
668 return true;
669 }
670
Sebastian Redl31ad7542011-03-13 17:09:40 +0000671 FunctionProtoType::NoexceptResult SubNR = Subset->getNoexceptSpec(Context);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000672 if (SubNR == FunctionProtoType::NR_BadNoexcept ||
673 SubNR == FunctionProtoType::NR_Dependent)
674 return false;
675
676 // Another case of the subset containing everything.
677 if (SubNR == FunctionProtoType::NR_Throw) {
678 Diag(SubLoc, DiagID);
679 if (NoteID.getDiagID() != 0)
680 Diag(SuperLoc, NoteID);
681 return true;
682 }
683
684 // If the subset contains nothing, we're done.
685 if (SubEST == EST_DynamicNone || SubNR == FunctionProtoType::NR_Nothrow)
Richard Smith1be59c52016-10-22 01:32:19 +0000686 return CheckParamExceptionSpec(NestedDiagID, NoteID, Superset, SuperLoc,
687 Subset, SubLoc);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000688
689 // Otherwise, if the superset contains nothing, we've failed.
690 if (SuperEST == EST_DynamicNone || SuperNR == FunctionProtoType::NR_Nothrow) {
691 Diag(SubLoc, DiagID);
692 if (NoteID.getDiagID() != 0)
693 Diag(SuperLoc, NoteID);
694 return true;
695 }
696
697 assert(SuperEST == EST_Dynamic && SubEST == EST_Dynamic &&
698 "Exception spec subset: non-dynamic case slipped through.");
699
700 // Neither contains everything or nothing. Do a proper comparison.
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000701 for (const auto &SubI : Subset->exceptions()) {
Sebastian Redl4915e632009-10-11 09:03:14 +0000702 // Take one type from the subset.
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000703 QualType CanonicalSubT = Context.getCanonicalType(SubI);
Sebastian Redl075b21d2009-10-14 14:38:54 +0000704 // Unwrap pointers and references so that we can do checks within a class
705 // hierarchy. Don't unwrap member pointers; they don't have hierarchy
706 // conversions on the pointee.
Sebastian Redl4915e632009-10-11 09:03:14 +0000707 bool SubIsPointer = false;
708 if (const ReferenceType *RefTy = CanonicalSubT->getAs<ReferenceType>())
709 CanonicalSubT = RefTy->getPointeeType();
710 if (const PointerType *PtrTy = CanonicalSubT->getAs<PointerType>()) {
711 CanonicalSubT = PtrTy->getPointeeType();
712 SubIsPointer = true;
713 }
714 bool SubIsClass = CanonicalSubT->isRecordType();
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +0000715 CanonicalSubT = CanonicalSubT.getLocalUnqualifiedType();
Sebastian Redl4915e632009-10-11 09:03:14 +0000716
717 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
718 /*DetectVirtual=*/false);
719
720 bool Contained = false;
721 // Make sure it's in the superset.
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000722 for (const auto &SuperI : Superset->exceptions()) {
723 QualType CanonicalSuperT = Context.getCanonicalType(SuperI);
Sebastian Redl4915e632009-10-11 09:03:14 +0000724 // SubT must be SuperT or derived from it, or pointer or reference to
725 // such types.
726 if (const ReferenceType *RefTy = CanonicalSuperT->getAs<ReferenceType>())
727 CanonicalSuperT = RefTy->getPointeeType();
728 if (SubIsPointer) {
729 if (const PointerType *PtrTy = CanonicalSuperT->getAs<PointerType>())
730 CanonicalSuperT = PtrTy->getPointeeType();
731 else {
732 continue;
733 }
734 }
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +0000735 CanonicalSuperT = CanonicalSuperT.getLocalUnqualifiedType();
Sebastian Redl4915e632009-10-11 09:03:14 +0000736 // If the types are the same, move on to the next type in the subset.
737 if (CanonicalSubT == CanonicalSuperT) {
738 Contained = true;
739 break;
740 }
741
742 // Otherwise we need to check the inheritance.
743 if (!SubIsClass || !CanonicalSuperT->isRecordType())
744 continue;
745
746 Paths.clear();
Richard Smith0f59cb32015-12-18 21:45:41 +0000747 if (!IsDerivedFrom(SubLoc, CanonicalSubT, CanonicalSuperT, Paths))
Sebastian Redl4915e632009-10-11 09:03:14 +0000748 continue;
749
Douglas Gregor27ac4292010-05-21 20:29:55 +0000750 if (Paths.isAmbiguous(Context.getCanonicalType(CanonicalSuperT)))
Sebastian Redl4915e632009-10-11 09:03:14 +0000751 continue;
752
John McCall5b0829a2010-02-10 09:31:12 +0000753 // Do this check from a context without privileges.
John McCall1064d7e2010-03-16 05:22:47 +0000754 switch (CheckBaseClassAccess(SourceLocation(),
John McCall5b0829a2010-02-10 09:31:12 +0000755 CanonicalSuperT, CanonicalSubT,
756 Paths.front(),
John McCall1064d7e2010-03-16 05:22:47 +0000757 /*Diagnostic*/ 0,
John McCall5b0829a2010-02-10 09:31:12 +0000758 /*ForceCheck*/ true,
John McCall1064d7e2010-03-16 05:22:47 +0000759 /*ForceUnprivileged*/ true)) {
John McCall5b0829a2010-02-10 09:31:12 +0000760 case AR_accessible: break;
761 case AR_inaccessible: continue;
762 case AR_dependent:
763 llvm_unreachable("access check dependent for unprivileged context");
John McCall5b0829a2010-02-10 09:31:12 +0000764 case AR_delayed:
765 llvm_unreachable("access check delayed in non-declaration");
John McCall5b0829a2010-02-10 09:31:12 +0000766 }
Sebastian Redl4915e632009-10-11 09:03:14 +0000767
768 Contained = true;
769 break;
770 }
771 if (!Contained) {
772 Diag(SubLoc, DiagID);
Sebastian Redla44822f2009-10-14 16:09:29 +0000773 if (NoteID.getDiagID() != 0)
Sebastian Redl4915e632009-10-11 09:03:14 +0000774 Diag(SuperLoc, NoteID);
775 return true;
776 }
777 }
778 // We've run half the gauntlet.
Richard Smith1be59c52016-10-22 01:32:19 +0000779 return CheckParamExceptionSpec(NestedDiagID, NoteID, Superset, SuperLoc,
780 Subset, SubLoc);
Sebastian Redl4915e632009-10-11 09:03:14 +0000781}
782
Richard Smith1be59c52016-10-22 01:32:19 +0000783static bool
784CheckSpecForTypesEquivalent(Sema &S, const PartialDiagnostic &DiagID,
785 const PartialDiagnostic &NoteID, QualType Target,
786 SourceLocation TargetLoc, QualType Source,
787 SourceLocation SourceLoc) {
Sebastian Redl4915e632009-10-11 09:03:14 +0000788 const FunctionProtoType *TFunc = GetUnderlyingFunction(Target);
789 if (!TFunc)
790 return false;
791 const FunctionProtoType *SFunc = GetUnderlyingFunction(Source);
792 if (!SFunc)
793 return false;
794
795 return S.CheckEquivalentExceptionSpec(DiagID, NoteID, TFunc, TargetLoc,
796 SFunc, SourceLoc);
797}
798
799/// CheckParamExceptionSpec - Check if the parameter and return types of the
800/// two functions have equivalent exception specs. This is part of the
801/// assignment and override compatibility check. We do not check the parameters
802/// of parameter function pointers recursively, as no sane programmer would
803/// even be able to write such a function type.
Richard Smith1be59c52016-10-22 01:32:19 +0000804bool Sema::CheckParamExceptionSpec(const PartialDiagnostic &DiagID,
805 const PartialDiagnostic &NoteID,
Richard Smith2e321552014-11-12 02:00:47 +0000806 const FunctionProtoType *Target,
807 SourceLocation TargetLoc,
808 const FunctionProtoType *Source,
809 SourceLocation SourceLoc) {
Richard Smith1be59c52016-10-22 01:32:19 +0000810 auto RetDiag = DiagID;
811 RetDiag << 0;
Alp Toker314cc812014-01-25 16:55:45 +0000812 if (CheckSpecForTypesEquivalent(
Richard Smith1be59c52016-10-22 01:32:19 +0000813 *this, RetDiag, PDiag(),
Alp Toker314cc812014-01-25 16:55:45 +0000814 Target->getReturnType(), TargetLoc, Source->getReturnType(),
815 SourceLoc))
Sebastian Redl4915e632009-10-11 09:03:14 +0000816 return true;
817
Sebastian Redla44822f2009-10-14 16:09:29 +0000818 // We shouldn't even be testing this unless the arguments are otherwise
Sebastian Redl4915e632009-10-11 09:03:14 +0000819 // compatible.
Alp Toker9cacbab2014-01-20 20:26:09 +0000820 assert(Target->getNumParams() == Source->getNumParams() &&
Sebastian Redl4915e632009-10-11 09:03:14 +0000821 "Functions have different argument counts.");
Alp Toker9cacbab2014-01-20 20:26:09 +0000822 for (unsigned i = 0, E = Target->getNumParams(); i != E; ++i) {
Richard Smith1be59c52016-10-22 01:32:19 +0000823 auto ParamDiag = DiagID;
824 ParamDiag << 1;
Alp Toker9cacbab2014-01-20 20:26:09 +0000825 if (CheckSpecForTypesEquivalent(
Richard Smith1be59c52016-10-22 01:32:19 +0000826 *this, ParamDiag, PDiag(),
Alp Toker9cacbab2014-01-20 20:26:09 +0000827 Target->getParamType(i), TargetLoc, Source->getParamType(i),
828 SourceLoc))
Sebastian Redl4915e632009-10-11 09:03:14 +0000829 return true;
830 }
831 return false;
832}
833
Richard Smith2e321552014-11-12 02:00:47 +0000834bool Sema::CheckExceptionSpecCompatibility(Expr *From, QualType ToType) {
Sebastian Redl4915e632009-10-11 09:03:14 +0000835 // First we check for applicability.
836 // Target type must be a function, function pointer or function reference.
837 const FunctionProtoType *ToFunc = GetUnderlyingFunction(ToType);
Richard Smith2e321552014-11-12 02:00:47 +0000838 if (!ToFunc || ToFunc->hasDependentExceptionSpec())
Sebastian Redl4915e632009-10-11 09:03:14 +0000839 return false;
840
841 // SourceType must be a function or function pointer.
842 const FunctionProtoType *FromFunc = GetUnderlyingFunction(From->getType());
Richard Smith2e321552014-11-12 02:00:47 +0000843 if (!FromFunc || FromFunc->hasDependentExceptionSpec())
Sebastian Redl4915e632009-10-11 09:03:14 +0000844 return false;
845
Richard Smith1be59c52016-10-22 01:32:19 +0000846 unsigned DiagID = diag::err_incompatible_exception_specs;
847 unsigned NestedDiagID = diag::err_deep_exception_specs_differ;
848 // This is not an error in C++17 onwards, unless the noexceptness doesn't
849 // match, but in that case we have a full-on type mismatch, not just a
850 // type sugar mismatch.
851 if (getLangOpts().CPlusPlus1z) {
852 DiagID = diag::warn_incompatible_exception_specs;
853 NestedDiagID = diag::warn_deep_exception_specs_differ;
854 }
855
Sebastian Redl4915e632009-10-11 09:03:14 +0000856 // Now we've got the correct types on both sides, check their compatibility.
857 // This means that the source of the conversion can only throw a subset of
858 // the exceptions of the target, and any exception specs on arguments or
859 // return types must be equivalent.
Richard Smith2e321552014-11-12 02:00:47 +0000860 //
861 // FIXME: If there is a nested dependent exception specification, we should
862 // not be checking it here. This is fine:
863 // template<typename T> void f() {
864 // void (*p)(void (*) throw(T));
865 // void (*q)(void (*) throw(int)) = p;
866 // }
867 // ... because it might be instantiated with T=int.
Richard Smith1be59c52016-10-22 01:32:19 +0000868 return CheckExceptionSpecSubset(PDiag(DiagID), PDiag(NestedDiagID), PDiag(),
869 ToFunc, From->getSourceRange().getBegin(),
870 FromFunc, SourceLocation()) &&
871 !getLangOpts().CPlusPlus1z;
Sebastian Redl4915e632009-10-11 09:03:14 +0000872}
873
874bool Sema::CheckOverridingFunctionExceptionSpec(const CXXMethodDecl *New,
875 const CXXMethodDecl *Old) {
Richard Smith88f45492014-11-22 03:09:05 +0000876 // If the new exception specification hasn't been parsed yet, skip the check.
877 // We'll get called again once it's been parsed.
878 if (New->getType()->castAs<FunctionProtoType>()->getExceptionSpecType() ==
879 EST_Unparsed)
880 return false;
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000881 if (getLangOpts().CPlusPlus11 && isa<CXXDestructorDecl>(New)) {
Sebastian Redl645d9582011-05-20 05:57:18 +0000882 // Don't check uninstantiated template destructors at all. We can only
883 // synthesize correct specs after the template is instantiated.
884 if (New->getParent()->isDependentType())
885 return false;
886 if (New->getParent()->isBeingDefined()) {
887 // The destructor might be updated once the definition is finished. So
888 // remember it and check later.
Richard Smith88f45492014-11-22 03:09:05 +0000889 DelayedExceptionSpecChecks.push_back(std::make_pair(New, Old));
Sebastian Redl645d9582011-05-20 05:57:18 +0000890 return false;
891 }
Sebastian Redl623ea822011-05-19 05:13:44 +0000892 }
Richard Smith88f45492014-11-22 03:09:05 +0000893 // If the old exception specification hasn't been parsed yet, remember that
894 // we need to perform this check when we get to the end of the outermost
895 // lexically-surrounding class.
896 if (Old->getType()->castAs<FunctionProtoType>()->getExceptionSpecType() ==
897 EST_Unparsed) {
898 DelayedExceptionSpecChecks.push_back(std::make_pair(New, Old));
Richard Smith0b3a4622014-11-13 20:01:57 +0000899 return false;
Richard Smith88f45492014-11-22 03:09:05 +0000900 }
Francois Picheta8032e92011-05-24 02:11:43 +0000901 unsigned DiagID = diag::err_override_exception_spec;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000902 if (getLangOpts().MicrosoftExt)
Richard Smith1b98ccc2014-07-19 01:39:17 +0000903 DiagID = diag::ext_override_exception_spec;
Francois Picheta8032e92011-05-24 02:11:43 +0000904 return CheckExceptionSpecSubset(PDiag(DiagID),
Richard Smith1be59c52016-10-22 01:32:19 +0000905 PDiag(diag::err_deep_exception_specs_differ),
Douglas Gregor89336232010-03-29 23:34:08 +0000906 PDiag(diag::note_overridden_virtual_function),
Sebastian Redl4915e632009-10-11 09:03:14 +0000907 Old->getType()->getAs<FunctionProtoType>(),
908 Old->getLocation(),
909 New->getType()->getAs<FunctionProtoType>(),
910 New->getLocation());
911}
912
Benjamin Kramer642f1732015-07-02 21:03:14 +0000913static CanThrowResult canSubExprsThrow(Sema &S, const Expr *E) {
Richard Smithf623c962012-04-17 00:58:00 +0000914 CanThrowResult R = CT_Cannot;
Benjamin Kramer642f1732015-07-02 21:03:14 +0000915 for (const Stmt *SubStmt : E->children()) {
916 R = mergeCanThrow(R, S.canThrow(cast<Expr>(SubStmt)));
917 if (R == CT_Can)
918 break;
919 }
Richard Smithf623c962012-04-17 00:58:00 +0000920 return R;
921}
922
Eli Friedman0423b762013-06-25 01:24:22 +0000923static CanThrowResult canCalleeThrow(Sema &S, const Expr *E, const Decl *D) {
924 assert(D && "Expected decl");
Richard Smithf623c962012-04-17 00:58:00 +0000925
926 // See if we can get a function type from the decl somehow.
927 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
Richard Smith1be59c52016-10-22 01:32:19 +0000928 if (!VD) {
929 // In C++17, we may have a canonical exception specification. If so, use it.
930 if (auto *FT = E->getType().getCanonicalType()->getAs<FunctionProtoType>())
931 return FT->isNothrow(S.Context) ? CT_Cannot : CT_Can;
932 // If we have no clue what we're calling, assume the worst.
Richard Smithf623c962012-04-17 00:58:00 +0000933 return CT_Can;
Richard Smith1be59c52016-10-22 01:32:19 +0000934 }
Richard Smithf623c962012-04-17 00:58:00 +0000935
936 // As an extension, we assume that __attribute__((nothrow)) functions don't
937 // throw.
938 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
939 return CT_Cannot;
940
941 QualType T = VD->getType();
942 const FunctionProtoType *FT;
943 if ((FT = T->getAs<FunctionProtoType>())) {
944 } else if (const PointerType *PT = T->getAs<PointerType>())
945 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
946 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
947 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
948 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
949 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
950 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
951 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
952
953 if (!FT)
954 return CT_Can;
955
956 FT = S.ResolveExceptionSpec(E->getLocStart(), FT);
957 if (!FT)
958 return CT_Can;
959
Richard Smithf623c962012-04-17 00:58:00 +0000960 return FT->isNothrow(S.Context) ? CT_Cannot : CT_Can;
961}
962
963static CanThrowResult canDynamicCastThrow(const CXXDynamicCastExpr *DC) {
964 if (DC->isTypeDependent())
965 return CT_Dependent;
966
967 if (!DC->getTypeAsWritten()->isReferenceType())
968 return CT_Cannot;
969
970 if (DC->getSubExpr()->isTypeDependent())
971 return CT_Dependent;
972
973 return DC->getCastKind() == clang::CK_Dynamic? CT_Can : CT_Cannot;
974}
975
976static CanThrowResult canTypeidThrow(Sema &S, const CXXTypeidExpr *DC) {
977 if (DC->isTypeOperand())
978 return CT_Cannot;
979
980 Expr *Op = DC->getExprOperand();
981 if (Op->isTypeDependent())
982 return CT_Dependent;
983
984 const RecordType *RT = Op->getType()->getAs<RecordType>();
985 if (!RT)
986 return CT_Cannot;
987
988 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
989 return CT_Cannot;
990
991 if (Op->Classify(S.Context).isPRValue())
992 return CT_Cannot;
993
994 return CT_Can;
995}
996
997CanThrowResult Sema::canThrow(const Expr *E) {
998 // C++ [expr.unary.noexcept]p3:
999 // [Can throw] if in a potentially-evaluated context the expression would
1000 // contain:
1001 switch (E->getStmtClass()) {
1002 case Expr::CXXThrowExprClass:
1003 // - a potentially evaluated throw-expression
1004 return CT_Can;
1005
1006 case Expr::CXXDynamicCastExprClass: {
1007 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1008 // where T is a reference type, that requires a run-time check
1009 CanThrowResult CT = canDynamicCastThrow(cast<CXXDynamicCastExpr>(E));
1010 if (CT == CT_Can)
1011 return CT;
1012 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1013 }
1014
1015 case Expr::CXXTypeidExprClass:
1016 // - a potentially evaluated typeid expression applied to a glvalue
1017 // expression whose type is a polymorphic class type
1018 return canTypeidThrow(*this, cast<CXXTypeidExpr>(E));
1019
1020 // - a potentially evaluated call to a function, member function, function
1021 // pointer, or member function pointer that does not have a non-throwing
1022 // exception-specification
1023 case Expr::CallExprClass:
1024 case Expr::CXXMemberCallExprClass:
1025 case Expr::CXXOperatorCallExprClass:
1026 case Expr::UserDefinedLiteralClass: {
1027 const CallExpr *CE = cast<CallExpr>(E);
1028 CanThrowResult CT;
1029 if (E->isTypeDependent())
1030 CT = CT_Dependent;
1031 else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
1032 CT = CT_Cannot;
Eli Friedman5a8738f2013-06-25 01:55:41 +00001033 else if (CE->getCalleeDecl())
Richard Smithf623c962012-04-17 00:58:00 +00001034 CT = canCalleeThrow(*this, E, CE->getCalleeDecl());
Eli Friedman5a8738f2013-06-25 01:55:41 +00001035 else
1036 CT = CT_Can;
Richard Smithf623c962012-04-17 00:58:00 +00001037 if (CT == CT_Can)
1038 return CT;
1039 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1040 }
1041
1042 case Expr::CXXConstructExprClass:
1043 case Expr::CXXTemporaryObjectExprClass: {
1044 CanThrowResult CT = canCalleeThrow(*this, E,
1045 cast<CXXConstructExpr>(E)->getConstructor());
1046 if (CT == CT_Can)
1047 return CT;
1048 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1049 }
1050
Richard Smith5179eb72016-06-28 19:03:57 +00001051 case Expr::CXXInheritedCtorInitExprClass:
1052 return canCalleeThrow(*this, E,
1053 cast<CXXInheritedCtorInitExpr>(E)->getConstructor());
1054
Richard Smithf623c962012-04-17 00:58:00 +00001055 case Expr::LambdaExprClass: {
1056 const LambdaExpr *Lambda = cast<LambdaExpr>(E);
1057 CanThrowResult CT = CT_Cannot;
James Y Knight53c76162015-07-17 18:21:37 +00001058 for (LambdaExpr::const_capture_init_iterator
1059 Cap = Lambda->capture_init_begin(),
1060 CapEnd = Lambda->capture_init_end();
Richard Smithf623c962012-04-17 00:58:00 +00001061 Cap != CapEnd; ++Cap)
1062 CT = mergeCanThrow(CT, canThrow(*Cap));
1063 return CT;
1064 }
1065
1066 case Expr::CXXNewExprClass: {
1067 CanThrowResult CT;
1068 if (E->isTypeDependent())
1069 CT = CT_Dependent;
1070 else
1071 CT = canCalleeThrow(*this, E, cast<CXXNewExpr>(E)->getOperatorNew());
1072 if (CT == CT_Can)
1073 return CT;
1074 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1075 }
1076
1077 case Expr::CXXDeleteExprClass: {
1078 CanThrowResult CT;
1079 QualType DTy = cast<CXXDeleteExpr>(E)->getDestroyedType();
1080 if (DTy.isNull() || DTy->isDependentType()) {
1081 CT = CT_Dependent;
1082 } else {
1083 CT = canCalleeThrow(*this, E,
1084 cast<CXXDeleteExpr>(E)->getOperatorDelete());
1085 if (const RecordType *RT = DTy->getAs<RecordType>()) {
1086 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Eli Friedman0423b762013-06-25 01:24:22 +00001087 const CXXDestructorDecl *DD = RD->getDestructor();
1088 if (DD)
1089 CT = mergeCanThrow(CT, canCalleeThrow(*this, E, DD));
Richard Smithf623c962012-04-17 00:58:00 +00001090 }
1091 if (CT == CT_Can)
1092 return CT;
1093 }
1094 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1095 }
1096
1097 case Expr::CXXBindTemporaryExprClass: {
1098 // The bound temporary has to be destroyed again, which might throw.
1099 CanThrowResult CT = canCalleeThrow(*this, E,
1100 cast<CXXBindTemporaryExpr>(E)->getTemporary()->getDestructor());
1101 if (CT == CT_Can)
1102 return CT;
1103 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1104 }
1105
1106 // ObjC message sends are like function calls, but never have exception
1107 // specs.
1108 case Expr::ObjCMessageExprClass:
1109 case Expr::ObjCPropertyRefExprClass:
1110 case Expr::ObjCSubscriptRefExprClass:
1111 return CT_Can;
1112
1113 // All the ObjC literals that are implemented as calls are
1114 // potentially throwing unless we decide to close off that
1115 // possibility.
1116 case Expr::ObjCArrayLiteralClass:
1117 case Expr::ObjCDictionaryLiteralClass:
Patrick Beard0caa3942012-04-19 00:25:12 +00001118 case Expr::ObjCBoxedExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001119 return CT_Can;
1120
1121 // Many other things have subexpressions, so we have to test those.
1122 // Some are simple:
Richard Smith9f690bd2015-10-27 06:02:45 +00001123 case Expr::CoawaitExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001124 case Expr::ConditionalOperatorClass:
1125 case Expr::CompoundLiteralExprClass:
Richard Smith9f690bd2015-10-27 06:02:45 +00001126 case Expr::CoyieldExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001127 case Expr::CXXConstCastExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001128 case Expr::CXXReinterpretCastExprClass:
Richard Smithcc1b96d2013-06-12 22:31:48 +00001129 case Expr::CXXStdInitializerListExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001130 case Expr::DesignatedInitExprClass:
Yunzhong Gaocb779302015-06-10 00:27:52 +00001131 case Expr::DesignatedInitUpdateExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001132 case Expr::ExprWithCleanupsClass:
1133 case Expr::ExtVectorElementExprClass:
1134 case Expr::InitListExprClass:
1135 case Expr::MemberExprClass:
1136 case Expr::ObjCIsaExprClass:
1137 case Expr::ObjCIvarRefExprClass:
1138 case Expr::ParenExprClass:
1139 case Expr::ParenListExprClass:
1140 case Expr::ShuffleVectorExprClass:
Hal Finkelc4d7c822013-09-18 03:29:45 +00001141 case Expr::ConvertVectorExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001142 case Expr::VAArgExprClass:
1143 return canSubExprsThrow(*this, E);
1144
1145 // Some might be dependent for other reasons.
1146 case Expr::ArraySubscriptExprClass:
Alexey Bataev1a3320e2015-08-25 14:24:04 +00001147 case Expr::OMPArraySectionExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001148 case Expr::BinaryOperatorClass:
1149 case Expr::CompoundAssignOperatorClass:
1150 case Expr::CStyleCastExprClass:
1151 case Expr::CXXStaticCastExprClass:
1152 case Expr::CXXFunctionalCastExprClass:
1153 case Expr::ImplicitCastExprClass:
1154 case Expr::MaterializeTemporaryExprClass:
1155 case Expr::UnaryOperatorClass: {
1156 CanThrowResult CT = E->isTypeDependent() ? CT_Dependent : CT_Cannot;
1157 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1158 }
1159
1160 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1161 case Expr::StmtExprClass:
1162 return CT_Can;
1163
Richard Smith852c9db2013-04-20 22:23:05 +00001164 case Expr::CXXDefaultArgExprClass:
1165 return canThrow(cast<CXXDefaultArgExpr>(E)->getExpr());
1166
1167 case Expr::CXXDefaultInitExprClass:
1168 return canThrow(cast<CXXDefaultInitExpr>(E)->getExpr());
1169
Richard Smithf623c962012-04-17 00:58:00 +00001170 case Expr::ChooseExprClass:
1171 if (E->isTypeDependent() || E->isValueDependent())
1172 return CT_Dependent;
Eli Friedman75807f22013-07-20 00:40:58 +00001173 return canThrow(cast<ChooseExpr>(E)->getChosenSubExpr());
Richard Smithf623c962012-04-17 00:58:00 +00001174
1175 case Expr::GenericSelectionExprClass:
1176 if (cast<GenericSelectionExpr>(E)->isResultDependent())
1177 return CT_Dependent;
1178 return canThrow(cast<GenericSelectionExpr>(E)->getResultExpr());
1179
1180 // Some expressions are always dependent.
1181 case Expr::CXXDependentScopeMemberExprClass:
1182 case Expr::CXXUnresolvedConstructExprClass:
1183 case Expr::DependentScopeDeclRefExprClass:
Richard Smith0f0af192014-11-08 05:07:16 +00001184 case Expr::CXXFoldExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001185 return CT_Dependent;
1186
1187 case Expr::AsTypeExprClass:
1188 case Expr::BinaryConditionalOperatorClass:
1189 case Expr::BlockExprClass:
1190 case Expr::CUDAKernelCallExprClass:
1191 case Expr::DeclRefExprClass:
1192 case Expr::ObjCBridgedCastExprClass:
1193 case Expr::ObjCIndirectCopyRestoreExprClass:
1194 case Expr::ObjCProtocolExprClass:
1195 case Expr::ObjCSelectorExprClass:
Erik Pilkington29099de2016-07-16 00:35:23 +00001196 case Expr::ObjCAvailabilityCheckExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001197 case Expr::OffsetOfExprClass:
1198 case Expr::PackExpansionExprClass:
1199 case Expr::PseudoObjectExprClass:
1200 case Expr::SubstNonTypeTemplateParmExprClass:
1201 case Expr::SubstNonTypeTemplateParmPackExprClass:
Richard Smithb15fe3a2012-09-12 00:56:43 +00001202 case Expr::FunctionParmPackExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001203 case Expr::UnaryExprOrTypeTraitExprClass:
1204 case Expr::UnresolvedLookupExprClass:
1205 case Expr::UnresolvedMemberExprClass:
Kaelyn Takatae1f49d52014-10-27 18:07:20 +00001206 case Expr::TypoExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001207 // FIXME: Can any of the above throw? If so, when?
1208 return CT_Cannot;
1209
1210 case Expr::AddrLabelExprClass:
1211 case Expr::ArrayTypeTraitExprClass:
1212 case Expr::AtomicExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001213 case Expr::TypeTraitExprClass:
1214 case Expr::CXXBoolLiteralExprClass:
1215 case Expr::CXXNoexceptExprClass:
1216 case Expr::CXXNullPtrLiteralExprClass:
1217 case Expr::CXXPseudoDestructorExprClass:
1218 case Expr::CXXScalarValueInitExprClass:
1219 case Expr::CXXThisExprClass:
1220 case Expr::CXXUuidofExprClass:
1221 case Expr::CharacterLiteralClass:
1222 case Expr::ExpressionTraitExprClass:
1223 case Expr::FloatingLiteralClass:
1224 case Expr::GNUNullExprClass:
1225 case Expr::ImaginaryLiteralClass:
1226 case Expr::ImplicitValueInitExprClass:
1227 case Expr::IntegerLiteralClass:
Yunzhong Gaocb779302015-06-10 00:27:52 +00001228 case Expr::NoInitExprClass:
Richard Smithf623c962012-04-17 00:58:00 +00001229 case Expr::ObjCEncodeExprClass:
1230 case Expr::ObjCStringLiteralClass:
1231 case Expr::ObjCBoolLiteralExprClass:
1232 case Expr::OpaqueValueExprClass:
1233 case Expr::PredefinedExprClass:
1234 case Expr::SizeOfPackExprClass:
1235 case Expr::StringLiteralClass:
Richard Smithf623c962012-04-17 00:58:00 +00001236 // These expressions can never throw.
1237 return CT_Cannot;
1238
John McCall5e77d762013-04-16 07:28:30 +00001239 case Expr::MSPropertyRefExprClass:
Alexey Bataevf7630272015-11-25 12:01:00 +00001240 case Expr::MSPropertySubscriptExprClass:
John McCall5e77d762013-04-16 07:28:30 +00001241 llvm_unreachable("Invalid class for expression");
1242
Richard Smithf623c962012-04-17 00:58:00 +00001243#define STMT(CLASS, PARENT) case Expr::CLASS##Class:
1244#define STMT_RANGE(Base, First, Last)
1245#define LAST_STMT_RANGE(BASE, FIRST, LAST)
1246#define EXPR(CLASS, PARENT)
1247#define ABSTRACT_STMT(STMT)
1248#include "clang/AST/StmtNodes.inc"
1249 case Expr::NoStmtClass:
1250 llvm_unreachable("Invalid class for expression");
1251 }
1252 llvm_unreachable("Bogus StmtClass");
1253}
1254
Sebastian Redl4915e632009-10-11 09:03:14 +00001255} // end namespace clang