1 //===--- SemaExceptionSpec.cpp - C++ Exception Specifications ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides Sema routines for C++ exception specification testing.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Sema/SemaInternal.h"
15 #include "clang/AST/ASTMutationListener.h"
16 #include "clang/AST/CXXInheritance.h"
17 #include "clang/AST/Expr.h"
18 #include "clang/AST/ExprCXX.h"
19 #include "clang/AST/TypeLoc.h"
20 #include "clang/Basic/Diagnostic.h"
21 #include "clang/Basic/SourceManager.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/SmallString.h"
27 static const FunctionProtoType *GetUnderlyingFunction(QualType T)
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();
33 else if (const MemberPointerType *MPTy = T->getAs<MemberPointerType>())
34 T = MPTy->getPointeeType();
35 return T->getAs<FunctionProtoType>();
38 /// HACK: libstdc++ has a bug where it shadows std::swap with a member
39 /// swap function then tries to call std::swap unqualified from the exception
40 /// specification of that function. This function detects whether we're in
41 /// such a case and turns off delay-parsing of exception specifications.
42 bool Sema::isLibstdcxxEagerExceptionSpecHack(const Declarator &D) {
43 auto *RD = dyn_cast<CXXRecordDecl>(CurContext);
45 // All the problem cases are member functions named "swap" within class
46 // templates declared directly within namespace std.
47 if (!RD || RD->getEnclosingNamespaceContext() != getStdNamespace() ||
48 !RD->getIdentifier() || !RD->getDescribedClassTemplate() ||
49 !D.getIdentifier() || !D.getIdentifier()->isStr("swap"))
52 // Only apply this hack within a system header.
53 if (!Context.getSourceManager().isInSystemHeader(D.getLocStart()))
56 return llvm::StringSwitch<bool>(RD->getIdentifier()->getName())
59 .Case("priority_queue", true)
65 /// CheckSpecifiedExceptionType - Check if the given type is valid in an
66 /// exception specification. Incomplete types, or pointers to incomplete types
67 /// other than void are not allowed.
69 /// \param[in,out] T The exception type. This will be decayed to a pointer type
70 /// when the input is an array or a function type.
71 bool Sema::CheckSpecifiedExceptionType(QualType &T, SourceRange Range) {
72 // C++11 [except.spec]p2:
73 // A type cv T, "array of T", or "function returning T" denoted
74 // in an exception-specification is adjusted to type T, "pointer to T", or
75 // "pointer to function returning T", respectively.
77 // We also apply this rule in C++98.
79 T = Context.getArrayDecayedType(T);
80 else if (T->isFunctionType())
81 T = Context.getPointerType(T);
84 QualType PointeeT = T;
85 if (const PointerType *PT = T->getAs<PointerType>()) {
86 PointeeT = PT->getPointeeType();
89 // cv void* is explicitly permitted, despite being a pointer to an
91 if (PointeeT->isVoidType())
93 } else if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
94 PointeeT = RT->getPointeeType();
97 if (RT->isRValueReferenceType()) {
98 // C++11 [except.spec]p2:
99 // A type denoted in an exception-specification shall not denote [...]
100 // an rvalue reference type.
101 Diag(Range.getBegin(), diag::err_rref_in_exception_spec)
107 // C++11 [except.spec]p2:
108 // A type denoted in an exception-specification shall not denote an
109 // incomplete type other than a class currently being defined [...].
110 // A type denoted in an exception-specification shall not denote a
111 // pointer or reference to an incomplete type, other than (cv) void* or a
112 // pointer or reference to a class currently being defined.
113 // In Microsoft mode, downgrade this to a warning.
114 unsigned DiagID = diag::err_incomplete_in_exception_spec;
115 bool ReturnValueOnError = true;
116 if (getLangOpts().MicrosoftExt) {
117 DiagID = diag::ext_incomplete_in_exception_spec;
118 ReturnValueOnError = false;
120 if (!(PointeeT->isRecordType() &&
121 PointeeT->getAs<RecordType>()->isBeingDefined()) &&
122 RequireCompleteType(Range.getBegin(), PointeeT, DiagID, Kind, Range))
123 return ReturnValueOnError;
128 /// CheckDistantExceptionSpec - Check if the given type is a pointer or pointer
129 /// to member to a function with an exception specification. This means that
130 /// it is invalid to add another level of indirection.
131 bool Sema::CheckDistantExceptionSpec(QualType T) {
132 if (const PointerType *PT = T->getAs<PointerType>())
133 T = PT->getPointeeType();
134 else if (const MemberPointerType *PT = T->getAs<MemberPointerType>())
135 T = PT->getPointeeType();
139 const FunctionProtoType *FnT = T->getAs<FunctionProtoType>();
143 return FnT->hasExceptionSpec();
146 const FunctionProtoType *
147 Sema::ResolveExceptionSpec(SourceLocation Loc, const FunctionProtoType *FPT) {
148 if (FPT->getExceptionSpecType() == EST_Unparsed) {
149 Diag(Loc, diag::err_exception_spec_not_parsed);
153 if (!isUnresolvedExceptionSpec(FPT->getExceptionSpecType()))
156 FunctionDecl *SourceDecl = FPT->getExceptionSpecDecl();
157 const FunctionProtoType *SourceFPT =
158 SourceDecl->getType()->castAs<FunctionProtoType>();
160 // If the exception specification has already been resolved, just return it.
161 if (!isUnresolvedExceptionSpec(SourceFPT->getExceptionSpecType()))
164 // Compute or instantiate the exception specification now.
165 if (SourceFPT->getExceptionSpecType() == EST_Unevaluated)
166 EvaluateImplicitExceptionSpec(Loc, cast<CXXMethodDecl>(SourceDecl));
168 InstantiateExceptionSpec(Loc, SourceDecl);
170 const FunctionProtoType *Proto =
171 SourceDecl->getType()->castAs<FunctionProtoType>();
172 if (Proto->getExceptionSpecType() == clang::EST_Unparsed) {
173 Diag(Loc, diag::err_exception_spec_not_parsed);
180 Sema::UpdateExceptionSpec(FunctionDecl *FD,
181 const FunctionProtoType::ExceptionSpecInfo &ESI) {
182 // If we've fully resolved the exception specification, notify listeners.
183 if (!isUnresolvedExceptionSpec(ESI.Type))
184 if (auto *Listener = getASTMutationListener())
185 Listener->ResolvedExceptionSpec(FD);
187 for (auto *Redecl : FD->redecls())
188 Context.adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
191 /// Determine whether a function has an implicitly-generated exception
193 static bool hasImplicitExceptionSpec(FunctionDecl *Decl) {
194 if (!isa<CXXDestructorDecl>(Decl) &&
195 Decl->getDeclName().getCXXOverloadedOperator() != OO_Delete &&
196 Decl->getDeclName().getCXXOverloadedOperator() != OO_Array_Delete)
199 // For a function that the user didn't declare:
200 // - if this is a destructor, its exception specification is implicit.
201 // - if this is 'operator delete' or 'operator delete[]', the exception
202 // specification is as-if an explicit exception specification was given
203 // (per [basic.stc.dynamic]p2).
204 if (!Decl->getTypeSourceInfo())
205 return isa<CXXDestructorDecl>(Decl);
207 const FunctionProtoType *Ty =
208 Decl->getTypeSourceInfo()->getType()->getAs<FunctionProtoType>();
209 return !Ty->hasExceptionSpec();
212 bool Sema::CheckEquivalentExceptionSpec(FunctionDecl *Old, FunctionDecl *New) {
213 OverloadedOperatorKind OO = New->getDeclName().getCXXOverloadedOperator();
214 bool IsOperatorNew = OO == OO_New || OO == OO_Array_New;
215 bool MissingExceptionSpecification = false;
216 bool MissingEmptyExceptionSpecification = false;
218 unsigned DiagID = diag::err_mismatched_exception_spec;
219 bool ReturnValueOnError = true;
220 if (getLangOpts().MicrosoftExt) {
221 DiagID = diag::ext_mismatched_exception_spec;
222 ReturnValueOnError = false;
225 // Check the types as written: they must match before any exception
226 // specification adjustment is applied.
227 if (!CheckEquivalentExceptionSpec(
228 PDiag(DiagID), PDiag(diag::note_previous_declaration),
229 Old->getType()->getAs<FunctionProtoType>(), Old->getLocation(),
230 New->getType()->getAs<FunctionProtoType>(), New->getLocation(),
231 &MissingExceptionSpecification, &MissingEmptyExceptionSpecification,
232 /*AllowNoexceptAllMatchWithNoSpec=*/true, IsOperatorNew)) {
233 // C++11 [except.spec]p4 [DR1492]:
234 // If a declaration of a function has an implicit
235 // exception-specification, other declarations of the function shall
236 // not specify an exception-specification.
237 if (getLangOpts().CPlusPlus11 &&
238 hasImplicitExceptionSpec(Old) != hasImplicitExceptionSpec(New)) {
239 Diag(New->getLocation(), diag::ext_implicit_exception_spec_mismatch)
240 << hasImplicitExceptionSpec(Old);
241 if (Old->getLocation().isValid())
242 Diag(Old->getLocation(), diag::note_previous_declaration);
247 // The failure was something other than an missing exception
248 // specification; return an error, except in MS mode where this is a warning.
249 if (!MissingExceptionSpecification)
250 return ReturnValueOnError;
252 const FunctionProtoType *NewProto =
253 New->getType()->castAs<FunctionProtoType>();
255 // The new function declaration is only missing an empty exception
256 // specification "throw()". If the throw() specification came from a
257 // function in a system header that has C linkage, just add an empty
258 // exception specification to the "new" declaration. This is an
259 // egregious workaround for glibc, which adds throw() specifications
260 // to many libc functions as an optimization. Unfortunately, that
261 // optimization isn't permitted by the C++ standard, so we're forced
262 // to work around it here.
263 if (MissingEmptyExceptionSpecification && NewProto &&
264 (Old->getLocation().isInvalid() ||
265 Context.getSourceManager().isInSystemHeader(Old->getLocation())) &&
267 New->setType(Context.getFunctionType(
268 NewProto->getReturnType(), NewProto->getParamTypes(),
269 NewProto->getExtProtoInfo().withExceptionSpec(EST_DynamicNone)));
273 const FunctionProtoType *OldProto =
274 Old->getType()->castAs<FunctionProtoType>();
276 FunctionProtoType::ExceptionSpecInfo ESI = OldProto->getExceptionSpecType();
277 if (ESI.Type == EST_Dynamic) {
278 ESI.Exceptions = OldProto->exceptions();
281 if (ESI.Type == EST_ComputedNoexcept) {
282 // For computed noexcept, we can't just take the expression from the old
283 // prototype. It likely contains references to the old prototype's
285 New->setInvalidDecl();
287 // Update the type of the function with the appropriate exception
289 New->setType(Context.getFunctionType(
290 NewProto->getReturnType(), NewProto->getParamTypes(),
291 NewProto->getExtProtoInfo().withExceptionSpec(ESI)));
294 if (getLangOpts().MicrosoftExt && ESI.Type != EST_ComputedNoexcept) {
295 // Allow missing exception specifications in redeclarations as an extension.
296 DiagID = diag::ext_ms_missing_exception_specification;
297 ReturnValueOnError = false;
298 } else if (New->isReplaceableGlobalAllocationFunction() &&
299 ESI.Type != EST_ComputedNoexcept) {
300 // Allow missing exception specifications in redeclarations as an extension,
301 // when declaring a replaceable global allocation function.
302 DiagID = diag::ext_missing_exception_specification;
303 ReturnValueOnError = false;
305 DiagID = diag::err_missing_exception_specification;
306 ReturnValueOnError = true;
309 // Warn about the lack of exception specification.
310 SmallString<128> ExceptionSpecString;
311 llvm::raw_svector_ostream OS(ExceptionSpecString);
312 switch (OldProto->getExceptionSpecType()) {
313 case EST_DynamicNone:
319 bool OnFirstException = true;
320 for (const auto &E : OldProto->exceptions()) {
321 if (OnFirstException)
322 OnFirstException = false;
326 OS << E.getAsString(getPrintingPolicy());
332 case EST_BasicNoexcept:
336 case EST_ComputedNoexcept:
338 assert(OldProto->getNoexceptExpr() != nullptr && "Expected non-null Expr");
339 OldProto->getNoexceptExpr()->printPretty(OS, nullptr, getPrintingPolicy());
344 llvm_unreachable("This spec type is compatible with none.");
347 SourceLocation FixItLoc;
348 if (TypeSourceInfo *TSInfo = New->getTypeSourceInfo()) {
349 TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens();
350 // FIXME: Preserve enough information so that we can produce a correct fixit
351 // location when there is a trailing return type.
352 if (auto FTLoc = TL.getAs<FunctionProtoTypeLoc>())
353 if (!FTLoc.getTypePtr()->hasTrailingReturn())
354 FixItLoc = getLocForEndOfToken(FTLoc.getLocalRangeEnd());
357 if (FixItLoc.isInvalid())
358 Diag(New->getLocation(), DiagID)
361 Diag(New->getLocation(), DiagID)
363 << FixItHint::CreateInsertion(FixItLoc, " " + OS.str().str());
366 if (Old->getLocation().isValid())
367 Diag(Old->getLocation(), diag::note_previous_declaration);
369 return ReturnValueOnError;
372 /// CheckEquivalentExceptionSpec - Check if the two types have equivalent
373 /// exception specifications. Exception specifications are equivalent if
374 /// they allow exactly the same set of exception types. It does not matter how
375 /// that is achieved. See C++ [except.spec]p2.
376 bool Sema::CheckEquivalentExceptionSpec(
377 const FunctionProtoType *Old, SourceLocation OldLoc,
378 const FunctionProtoType *New, SourceLocation NewLoc) {
379 unsigned DiagID = diag::err_mismatched_exception_spec;
380 if (getLangOpts().MicrosoftExt)
381 DiagID = diag::ext_mismatched_exception_spec;
382 bool Result = CheckEquivalentExceptionSpec(PDiag(DiagID),
383 PDiag(diag::note_previous_declaration), Old, OldLoc, New, NewLoc);
385 // In Microsoft mode, mismatching exception specifications just cause a warning.
386 if (getLangOpts().MicrosoftExt)
391 /// CheckEquivalentExceptionSpec - Check if the two types have compatible
392 /// exception specifications. See C++ [except.spec]p3.
394 /// \return \c false if the exception specifications match, \c true if there is
395 /// a problem. If \c true is returned, either a diagnostic has already been
396 /// produced or \c *MissingExceptionSpecification is set to \c true.
397 bool Sema::CheckEquivalentExceptionSpec(const PartialDiagnostic &DiagID,
398 const PartialDiagnostic & NoteID,
399 const FunctionProtoType *Old,
400 SourceLocation OldLoc,
401 const FunctionProtoType *New,
402 SourceLocation NewLoc,
403 bool *MissingExceptionSpecification,
404 bool*MissingEmptyExceptionSpecification,
405 bool AllowNoexceptAllMatchWithNoSpec,
406 bool IsOperatorNew) {
407 // Just completely ignore this under -fno-exceptions.
408 if (!getLangOpts().CXXExceptions)
411 if (MissingExceptionSpecification)
412 *MissingExceptionSpecification = false;
414 if (MissingEmptyExceptionSpecification)
415 *MissingEmptyExceptionSpecification = false;
417 Old = ResolveExceptionSpec(NewLoc, Old);
420 New = ResolveExceptionSpec(NewLoc, New);
424 // C++0x [except.spec]p3: Two exception-specifications are compatible if:
425 // - both are non-throwing, regardless of their form,
426 // - both have the form noexcept(constant-expression) and the constant-
427 // expressions are equivalent,
428 // - both are dynamic-exception-specifications that have the same set of
431 // C++0x [except.spec]p12: An exception-specification is non-throwing if it is
432 // of the form throw(), noexcept, or noexcept(constant-expression) where the
433 // constant-expression yields true.
435 // C++0x [except.spec]p4: If any declaration of a function has an exception-
436 // specifier that is not a noexcept-specification allowing all exceptions,
437 // all declarations [...] of that function shall have a compatible
438 // exception-specification.
440 // That last point basically means that noexcept(false) matches no spec.
441 // It's considered when AllowNoexceptAllMatchWithNoSpec is true.
443 ExceptionSpecificationType OldEST = Old->getExceptionSpecType();
444 ExceptionSpecificationType NewEST = New->getExceptionSpecType();
446 assert(!isUnresolvedExceptionSpec(OldEST) &&
447 !isUnresolvedExceptionSpec(NewEST) &&
448 "Shouldn't see unknown exception specifications here");
450 // Shortcut the case where both have no spec.
451 if (OldEST == EST_None && NewEST == EST_None)
454 FunctionProtoType::NoexceptResult OldNR = Old->getNoexceptSpec(Context);
455 FunctionProtoType::NoexceptResult NewNR = New->getNoexceptSpec(Context);
456 if (OldNR == FunctionProtoType::NR_BadNoexcept ||
457 NewNR == FunctionProtoType::NR_BadNoexcept)
460 // Dependent noexcept specifiers are compatible with each other, but nothing
462 // One noexcept is compatible with another if the argument is the same
463 if (OldNR == NewNR &&
464 OldNR != FunctionProtoType::NR_NoNoexcept &&
465 NewNR != FunctionProtoType::NR_NoNoexcept)
467 if (OldNR != NewNR &&
468 OldNR != FunctionProtoType::NR_NoNoexcept &&
469 NewNR != FunctionProtoType::NR_NoNoexcept) {
470 Diag(NewLoc, DiagID);
471 if (NoteID.getDiagID() != 0 && OldLoc.isValid())
472 Diag(OldLoc, NoteID);
476 // The MS extension throw(...) is compatible with itself.
477 if (OldEST == EST_MSAny && NewEST == EST_MSAny)
480 // It's also compatible with no spec.
481 if ((OldEST == EST_None && NewEST == EST_MSAny) ||
482 (OldEST == EST_MSAny && NewEST == EST_None))
485 // It's also compatible with noexcept(false).
486 if (OldEST == EST_MSAny && NewNR == FunctionProtoType::NR_Throw)
488 if (NewEST == EST_MSAny && OldNR == FunctionProtoType::NR_Throw)
491 // As described above, noexcept(false) matches no spec only for functions.
492 if (AllowNoexceptAllMatchWithNoSpec) {
493 if (OldEST == EST_None && NewNR == FunctionProtoType::NR_Throw)
495 if (NewEST == EST_None && OldNR == FunctionProtoType::NR_Throw)
499 // Any non-throwing specifications are compatible.
500 bool OldNonThrowing = OldNR == FunctionProtoType::NR_Nothrow ||
501 OldEST == EST_DynamicNone;
502 bool NewNonThrowing = NewNR == FunctionProtoType::NR_Nothrow ||
503 NewEST == EST_DynamicNone;
504 if (OldNonThrowing && NewNonThrowing)
507 // As a special compatibility feature, under C++0x we accept no spec and
508 // throw(std::bad_alloc) as equivalent for operator new and operator new[].
509 // This is because the implicit declaration changed, but old code would break.
510 if (getLangOpts().CPlusPlus11 && IsOperatorNew) {
511 const FunctionProtoType *WithExceptions = nullptr;
512 if (OldEST == EST_None && NewEST == EST_Dynamic)
513 WithExceptions = New;
514 else if (OldEST == EST_Dynamic && NewEST == EST_None)
515 WithExceptions = Old;
516 if (WithExceptions && WithExceptions->getNumExceptions() == 1) {
517 // One has no spec, the other throw(something). If that something is
518 // std::bad_alloc, all conditions are met.
519 QualType Exception = *WithExceptions->exception_begin();
520 if (CXXRecordDecl *ExRecord = Exception->getAsCXXRecordDecl()) {
521 IdentifierInfo* Name = ExRecord->getIdentifier();
522 if (Name && Name->getName() == "bad_alloc") {
523 // It's called bad_alloc, but is it in std?
524 if (ExRecord->isInStdNamespace()) {
532 // At this point, the only remaining valid case is two matching dynamic
533 // specifications. We return here unless both specifications are dynamic.
534 if (OldEST != EST_Dynamic || NewEST != EST_Dynamic) {
535 if (MissingExceptionSpecification && Old->hasExceptionSpec() &&
536 !New->hasExceptionSpec()) {
537 // The old type has an exception specification of some sort, but
538 // the new type does not.
539 *MissingExceptionSpecification = true;
541 if (MissingEmptyExceptionSpecification && OldNonThrowing) {
542 // The old type has a throw() or noexcept(true) exception specification
543 // and the new type has no exception specification, and the caller asked
544 // to handle this itself.
545 *MissingEmptyExceptionSpecification = true;
551 Diag(NewLoc, DiagID);
552 if (NoteID.getDiagID() != 0 && OldLoc.isValid())
553 Diag(OldLoc, NoteID);
557 assert(OldEST == EST_Dynamic && NewEST == EST_Dynamic &&
558 "Exception compatibility logic error: non-dynamic spec slipped through.");
561 // Both have a dynamic exception spec. Collect the first set, then compare
563 llvm::SmallPtrSet<CanQualType, 8> OldTypes, NewTypes;
564 for (const auto &I : Old->exceptions())
565 OldTypes.insert(Context.getCanonicalType(I).getUnqualifiedType());
567 for (const auto &I : New->exceptions()) {
568 CanQualType TypePtr = Context.getCanonicalType(I).getUnqualifiedType();
569 if(OldTypes.count(TypePtr))
570 NewTypes.insert(TypePtr);
575 Success = Success && OldTypes.size() == NewTypes.size();
580 Diag(NewLoc, DiagID);
581 if (NoteID.getDiagID() != 0 && OldLoc.isValid())
582 Diag(OldLoc, NoteID);
586 /// CheckExceptionSpecSubset - Check whether the second function type's
587 /// exception specification is a subset (or equivalent) of the first function
588 /// type. This is used by override and pointer assignment checks.
589 bool Sema::CheckExceptionSpecSubset(
590 const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID,
591 const FunctionProtoType *Superset, SourceLocation SuperLoc,
592 const FunctionProtoType *Subset, SourceLocation SubLoc) {
594 // Just auto-succeed under -fno-exceptions.
595 if (!getLangOpts().CXXExceptions)
598 // FIXME: As usual, we could be more specific in our error messages, but
599 // that better waits until we've got types with source locations.
601 if (!SubLoc.isValid())
604 // Resolve the exception specifications, if needed.
605 Superset = ResolveExceptionSpec(SuperLoc, Superset);
608 Subset = ResolveExceptionSpec(SubLoc, Subset);
612 ExceptionSpecificationType SuperEST = Superset->getExceptionSpecType();
614 // If superset contains everything, we're done.
615 if (SuperEST == EST_None || SuperEST == EST_MSAny)
616 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
618 // If there are dependent noexcept specs, assume everything is fine. Unlike
619 // with the equivalency check, this is safe in this case, because we don't
620 // want to merge declarations. Checks after instantiation will catch any
621 // omissions we make here.
622 // We also shortcut checking if a noexcept expression was bad.
624 FunctionProtoType::NoexceptResult SuperNR =Superset->getNoexceptSpec(Context);
625 if (SuperNR == FunctionProtoType::NR_BadNoexcept ||
626 SuperNR == FunctionProtoType::NR_Dependent)
629 // Another case of the superset containing everything.
630 if (SuperNR == FunctionProtoType::NR_Throw)
631 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
633 ExceptionSpecificationType SubEST = Subset->getExceptionSpecType();
635 assert(!isUnresolvedExceptionSpec(SuperEST) &&
636 !isUnresolvedExceptionSpec(SubEST) &&
637 "Shouldn't see unknown exception specifications here");
639 // It does not. If the subset contains everything, we've failed.
640 if (SubEST == EST_None || SubEST == EST_MSAny) {
641 Diag(SubLoc, DiagID);
642 if (NoteID.getDiagID() != 0)
643 Diag(SuperLoc, NoteID);
647 FunctionProtoType::NoexceptResult SubNR = Subset->getNoexceptSpec(Context);
648 if (SubNR == FunctionProtoType::NR_BadNoexcept ||
649 SubNR == FunctionProtoType::NR_Dependent)
652 // Another case of the subset containing everything.
653 if (SubNR == FunctionProtoType::NR_Throw) {
654 Diag(SubLoc, DiagID);
655 if (NoteID.getDiagID() != 0)
656 Diag(SuperLoc, NoteID);
660 // If the subset contains nothing, we're done.
661 if (SubEST == EST_DynamicNone || SubNR == FunctionProtoType::NR_Nothrow)
662 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
664 // Otherwise, if the superset contains nothing, we've failed.
665 if (SuperEST == EST_DynamicNone || SuperNR == FunctionProtoType::NR_Nothrow) {
666 Diag(SubLoc, DiagID);
667 if (NoteID.getDiagID() != 0)
668 Diag(SuperLoc, NoteID);
672 assert(SuperEST == EST_Dynamic && SubEST == EST_Dynamic &&
673 "Exception spec subset: non-dynamic case slipped through.");
675 // Neither contains everything or nothing. Do a proper comparison.
676 for (const auto &SubI : Subset->exceptions()) {
677 // Take one type from the subset.
678 QualType CanonicalSubT = Context.getCanonicalType(SubI);
679 // Unwrap pointers and references so that we can do checks within a class
680 // hierarchy. Don't unwrap member pointers; they don't have hierarchy
681 // conversions on the pointee.
682 bool SubIsPointer = false;
683 if (const ReferenceType *RefTy = CanonicalSubT->getAs<ReferenceType>())
684 CanonicalSubT = RefTy->getPointeeType();
685 if (const PointerType *PtrTy = CanonicalSubT->getAs<PointerType>()) {
686 CanonicalSubT = PtrTy->getPointeeType();
689 bool SubIsClass = CanonicalSubT->isRecordType();
690 CanonicalSubT = CanonicalSubT.getLocalUnqualifiedType();
692 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
693 /*DetectVirtual=*/false);
695 bool Contained = false;
696 // Make sure it's in the superset.
697 for (const auto &SuperI : Superset->exceptions()) {
698 QualType CanonicalSuperT = Context.getCanonicalType(SuperI);
699 // SubT must be SuperT or derived from it, or pointer or reference to
701 if (const ReferenceType *RefTy = CanonicalSuperT->getAs<ReferenceType>())
702 CanonicalSuperT = RefTy->getPointeeType();
704 if (const PointerType *PtrTy = CanonicalSuperT->getAs<PointerType>())
705 CanonicalSuperT = PtrTy->getPointeeType();
710 CanonicalSuperT = CanonicalSuperT.getLocalUnqualifiedType();
711 // If the types are the same, move on to the next type in the subset.
712 if (CanonicalSubT == CanonicalSuperT) {
717 // Otherwise we need to check the inheritance.
718 if (!SubIsClass || !CanonicalSuperT->isRecordType())
722 if (!IsDerivedFrom(SubLoc, CanonicalSubT, CanonicalSuperT, Paths))
725 if (Paths.isAmbiguous(Context.getCanonicalType(CanonicalSuperT)))
728 // Do this check from a context without privileges.
729 switch (CheckBaseClassAccess(SourceLocation(),
730 CanonicalSuperT, CanonicalSubT,
734 /*ForceUnprivileged*/ true)) {
735 case AR_accessible: break;
736 case AR_inaccessible: continue;
738 llvm_unreachable("access check dependent for unprivileged context");
740 llvm_unreachable("access check delayed in non-declaration");
747 Diag(SubLoc, DiagID);
748 if (NoteID.getDiagID() != 0)
749 Diag(SuperLoc, NoteID);
753 // We've run half the gauntlet.
754 return CheckParamExceptionSpec(NoteID, Superset, SuperLoc, Subset, SubLoc);
757 static bool CheckSpecForTypesEquivalent(Sema &S,
758 const PartialDiagnostic &DiagID, const PartialDiagnostic & NoteID,
759 QualType Target, SourceLocation TargetLoc,
760 QualType Source, SourceLocation SourceLoc)
762 const FunctionProtoType *TFunc = GetUnderlyingFunction(Target);
765 const FunctionProtoType *SFunc = GetUnderlyingFunction(Source);
769 return S.CheckEquivalentExceptionSpec(DiagID, NoteID, TFunc, TargetLoc,
773 /// CheckParamExceptionSpec - Check if the parameter and return types of the
774 /// two functions have equivalent exception specs. This is part of the
775 /// assignment and override compatibility check. We do not check the parameters
776 /// of parameter function pointers recursively, as no sane programmer would
777 /// even be able to write such a function type.
778 bool Sema::CheckParamExceptionSpec(const PartialDiagnostic &NoteID,
779 const FunctionProtoType *Target,
780 SourceLocation TargetLoc,
781 const FunctionProtoType *Source,
782 SourceLocation SourceLoc) {
783 if (CheckSpecForTypesEquivalent(
784 *this, PDiag(diag::err_deep_exception_specs_differ) << 0, PDiag(),
785 Target->getReturnType(), TargetLoc, Source->getReturnType(),
789 // We shouldn't even be testing this unless the arguments are otherwise
791 assert(Target->getNumParams() == Source->getNumParams() &&
792 "Functions have different argument counts.");
793 for (unsigned i = 0, E = Target->getNumParams(); i != E; ++i) {
794 if (CheckSpecForTypesEquivalent(
795 *this, PDiag(diag::err_deep_exception_specs_differ) << 1, PDiag(),
796 Target->getParamType(i), TargetLoc, Source->getParamType(i),
803 bool Sema::CheckExceptionSpecCompatibility(Expr *From, QualType ToType) {
804 // First we check for applicability.
805 // Target type must be a function, function pointer or function reference.
806 const FunctionProtoType *ToFunc = GetUnderlyingFunction(ToType);
807 if (!ToFunc || ToFunc->hasDependentExceptionSpec())
810 // SourceType must be a function or function pointer.
811 const FunctionProtoType *FromFunc = GetUnderlyingFunction(From->getType());
812 if (!FromFunc || FromFunc->hasDependentExceptionSpec())
815 // Now we've got the correct types on both sides, check their compatibility.
816 // This means that the source of the conversion can only throw a subset of
817 // the exceptions of the target, and any exception specs on arguments or
818 // return types must be equivalent.
820 // FIXME: If there is a nested dependent exception specification, we should
821 // not be checking it here. This is fine:
822 // template<typename T> void f() {
823 // void (*p)(void (*) throw(T));
824 // void (*q)(void (*) throw(int)) = p;
826 // ... because it might be instantiated with T=int.
827 return CheckExceptionSpecSubset(PDiag(diag::err_incompatible_exception_specs),
829 From->getSourceRange().getBegin(),
830 FromFunc, SourceLocation());
833 bool Sema::CheckOverridingFunctionExceptionSpec(const CXXMethodDecl *New,
834 const CXXMethodDecl *Old) {
835 // If the new exception specification hasn't been parsed yet, skip the check.
836 // We'll get called again once it's been parsed.
837 if (New->getType()->castAs<FunctionProtoType>()->getExceptionSpecType() ==
840 if (getLangOpts().CPlusPlus11 && isa<CXXDestructorDecl>(New)) {
841 // Don't check uninstantiated template destructors at all. We can only
842 // synthesize correct specs after the template is instantiated.
843 if (New->getParent()->isDependentType())
845 if (New->getParent()->isBeingDefined()) {
846 // The destructor might be updated once the definition is finished. So
847 // remember it and check later.
848 DelayedExceptionSpecChecks.push_back(std::make_pair(New, Old));
852 // If the old exception specification hasn't been parsed yet, remember that
853 // we need to perform this check when we get to the end of the outermost
854 // lexically-surrounding class.
855 if (Old->getType()->castAs<FunctionProtoType>()->getExceptionSpecType() ==
857 DelayedExceptionSpecChecks.push_back(std::make_pair(New, Old));
860 unsigned DiagID = diag::err_override_exception_spec;
861 if (getLangOpts().MicrosoftExt)
862 DiagID = diag::ext_override_exception_spec;
863 return CheckExceptionSpecSubset(PDiag(DiagID),
864 PDiag(diag::note_overridden_virtual_function),
865 Old->getType()->getAs<FunctionProtoType>(),
867 New->getType()->getAs<FunctionProtoType>(),
871 static CanThrowResult canSubExprsThrow(Sema &S, const Expr *E) {
872 CanThrowResult R = CT_Cannot;
873 for (const Stmt *SubStmt : E->children()) {
874 R = mergeCanThrow(R, S.canThrow(cast<Expr>(SubStmt)));
881 static CanThrowResult canCalleeThrow(Sema &S, const Expr *E, const Decl *D) {
882 assert(D && "Expected decl");
884 // See if we can get a function type from the decl somehow.
885 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
886 if (!VD) // If we have no clue what we're calling, assume the worst.
889 // As an extension, we assume that __attribute__((nothrow)) functions don't
891 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
894 QualType T = VD->getType();
895 const FunctionProtoType *FT;
896 if ((FT = T->getAs<FunctionProtoType>())) {
897 } else if (const PointerType *PT = T->getAs<PointerType>())
898 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
899 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
900 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
901 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
902 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
903 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
904 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
909 FT = S.ResolveExceptionSpec(E->getLocStart(), FT);
913 return FT->isNothrow(S.Context) ? CT_Cannot : CT_Can;
916 static CanThrowResult canDynamicCastThrow(const CXXDynamicCastExpr *DC) {
917 if (DC->isTypeDependent())
920 if (!DC->getTypeAsWritten()->isReferenceType())
923 if (DC->getSubExpr()->isTypeDependent())
926 return DC->getCastKind() == clang::CK_Dynamic? CT_Can : CT_Cannot;
929 static CanThrowResult canTypeidThrow(Sema &S, const CXXTypeidExpr *DC) {
930 if (DC->isTypeOperand())
933 Expr *Op = DC->getExprOperand();
934 if (Op->isTypeDependent())
937 const RecordType *RT = Op->getType()->getAs<RecordType>();
941 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
944 if (Op->Classify(S.Context).isPRValue())
950 CanThrowResult Sema::canThrow(const Expr *E) {
951 // C++ [expr.unary.noexcept]p3:
952 // [Can throw] if in a potentially-evaluated context the expression would
954 switch (E->getStmtClass()) {
955 case Expr::CXXThrowExprClass:
956 // - a potentially evaluated throw-expression
959 case Expr::CXXDynamicCastExprClass: {
960 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
961 // where T is a reference type, that requires a run-time check
962 CanThrowResult CT = canDynamicCastThrow(cast<CXXDynamicCastExpr>(E));
965 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
968 case Expr::CXXTypeidExprClass:
969 // - a potentially evaluated typeid expression applied to a glvalue
970 // expression whose type is a polymorphic class type
971 return canTypeidThrow(*this, cast<CXXTypeidExpr>(E));
973 // - a potentially evaluated call to a function, member function, function
974 // pointer, or member function pointer that does not have a non-throwing
975 // exception-specification
976 case Expr::CallExprClass:
977 case Expr::CXXMemberCallExprClass:
978 case Expr::CXXOperatorCallExprClass:
979 case Expr::UserDefinedLiteralClass: {
980 const CallExpr *CE = cast<CallExpr>(E);
982 if (E->isTypeDependent())
984 else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
986 else if (CE->getCalleeDecl())
987 CT = canCalleeThrow(*this, E, CE->getCalleeDecl());
992 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
995 case Expr::CXXConstructExprClass:
996 case Expr::CXXTemporaryObjectExprClass: {
997 CanThrowResult CT = canCalleeThrow(*this, E,
998 cast<CXXConstructExpr>(E)->getConstructor());
1001 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1004 case Expr::CXXInheritedCtorInitExprClass:
1005 return canCalleeThrow(*this, E,
1006 cast<CXXInheritedCtorInitExpr>(E)->getConstructor());
1008 case Expr::LambdaExprClass: {
1009 const LambdaExpr *Lambda = cast<LambdaExpr>(E);
1010 CanThrowResult CT = CT_Cannot;
1011 for (LambdaExpr::const_capture_init_iterator
1012 Cap = Lambda->capture_init_begin(),
1013 CapEnd = Lambda->capture_init_end();
1014 Cap != CapEnd; ++Cap)
1015 CT = mergeCanThrow(CT, canThrow(*Cap));
1019 case Expr::CXXNewExprClass: {
1021 if (E->isTypeDependent())
1024 CT = canCalleeThrow(*this, E, cast<CXXNewExpr>(E)->getOperatorNew());
1027 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1030 case Expr::CXXDeleteExprClass: {
1032 QualType DTy = cast<CXXDeleteExpr>(E)->getDestroyedType();
1033 if (DTy.isNull() || DTy->isDependentType()) {
1036 CT = canCalleeThrow(*this, E,
1037 cast<CXXDeleteExpr>(E)->getOperatorDelete());
1038 if (const RecordType *RT = DTy->getAs<RecordType>()) {
1039 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1040 const CXXDestructorDecl *DD = RD->getDestructor();
1042 CT = mergeCanThrow(CT, canCalleeThrow(*this, E, DD));
1047 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1050 case Expr::CXXBindTemporaryExprClass: {
1051 // The bound temporary has to be destroyed again, which might throw.
1052 CanThrowResult CT = canCalleeThrow(*this, E,
1053 cast<CXXBindTemporaryExpr>(E)->getTemporary()->getDestructor());
1056 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1059 // ObjC message sends are like function calls, but never have exception
1061 case Expr::ObjCMessageExprClass:
1062 case Expr::ObjCPropertyRefExprClass:
1063 case Expr::ObjCSubscriptRefExprClass:
1066 // All the ObjC literals that are implemented as calls are
1067 // potentially throwing unless we decide to close off that
1069 case Expr::ObjCArrayLiteralClass:
1070 case Expr::ObjCDictionaryLiteralClass:
1071 case Expr::ObjCBoxedExprClass:
1074 // Many other things have subexpressions, so we have to test those.
1076 case Expr::CoawaitExprClass:
1077 case Expr::ConditionalOperatorClass:
1078 case Expr::CompoundLiteralExprClass:
1079 case Expr::CoyieldExprClass:
1080 case Expr::CXXConstCastExprClass:
1081 case Expr::CXXReinterpretCastExprClass:
1082 case Expr::CXXStdInitializerListExprClass:
1083 case Expr::DesignatedInitExprClass:
1084 case Expr::DesignatedInitUpdateExprClass:
1085 case Expr::ExprWithCleanupsClass:
1086 case Expr::ExtVectorElementExprClass:
1087 case Expr::InitListExprClass:
1088 case Expr::MemberExprClass:
1089 case Expr::ObjCIsaExprClass:
1090 case Expr::ObjCIvarRefExprClass:
1091 case Expr::ParenExprClass:
1092 case Expr::ParenListExprClass:
1093 case Expr::ShuffleVectorExprClass:
1094 case Expr::ConvertVectorExprClass:
1095 case Expr::VAArgExprClass:
1096 return canSubExprsThrow(*this, E);
1098 // Some might be dependent for other reasons.
1099 case Expr::ArraySubscriptExprClass:
1100 case Expr::OMPArraySectionExprClass:
1101 case Expr::BinaryOperatorClass:
1102 case Expr::CompoundAssignOperatorClass:
1103 case Expr::CStyleCastExprClass:
1104 case Expr::CXXStaticCastExprClass:
1105 case Expr::CXXFunctionalCastExprClass:
1106 case Expr::ImplicitCastExprClass:
1107 case Expr::MaterializeTemporaryExprClass:
1108 case Expr::UnaryOperatorClass: {
1109 CanThrowResult CT = E->isTypeDependent() ? CT_Dependent : CT_Cannot;
1110 return mergeCanThrow(CT, canSubExprsThrow(*this, E));
1113 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1114 case Expr::StmtExprClass:
1117 case Expr::CXXDefaultArgExprClass:
1118 return canThrow(cast<CXXDefaultArgExpr>(E)->getExpr());
1120 case Expr::CXXDefaultInitExprClass:
1121 return canThrow(cast<CXXDefaultInitExpr>(E)->getExpr());
1123 case Expr::ChooseExprClass:
1124 if (E->isTypeDependent() || E->isValueDependent())
1125 return CT_Dependent;
1126 return canThrow(cast<ChooseExpr>(E)->getChosenSubExpr());
1128 case Expr::GenericSelectionExprClass:
1129 if (cast<GenericSelectionExpr>(E)->isResultDependent())
1130 return CT_Dependent;
1131 return canThrow(cast<GenericSelectionExpr>(E)->getResultExpr());
1133 // Some expressions are always dependent.
1134 case Expr::CXXDependentScopeMemberExprClass:
1135 case Expr::CXXUnresolvedConstructExprClass:
1136 case Expr::DependentScopeDeclRefExprClass:
1137 case Expr::CXXFoldExprClass:
1138 return CT_Dependent;
1140 case Expr::AsTypeExprClass:
1141 case Expr::BinaryConditionalOperatorClass:
1142 case Expr::BlockExprClass:
1143 case Expr::CUDAKernelCallExprClass:
1144 case Expr::DeclRefExprClass:
1145 case Expr::ObjCBridgedCastExprClass:
1146 case Expr::ObjCIndirectCopyRestoreExprClass:
1147 case Expr::ObjCProtocolExprClass:
1148 case Expr::ObjCSelectorExprClass:
1149 case Expr::ObjCAvailabilityCheckExprClass:
1150 case Expr::OffsetOfExprClass:
1151 case Expr::PackExpansionExprClass:
1152 case Expr::PseudoObjectExprClass:
1153 case Expr::SubstNonTypeTemplateParmExprClass:
1154 case Expr::SubstNonTypeTemplateParmPackExprClass:
1155 case Expr::FunctionParmPackExprClass:
1156 case Expr::UnaryExprOrTypeTraitExprClass:
1157 case Expr::UnresolvedLookupExprClass:
1158 case Expr::UnresolvedMemberExprClass:
1159 case Expr::TypoExprClass:
1160 // FIXME: Can any of the above throw? If so, when?
1163 case Expr::AddrLabelExprClass:
1164 case Expr::ArrayTypeTraitExprClass:
1165 case Expr::AtomicExprClass:
1166 case Expr::TypeTraitExprClass:
1167 case Expr::CXXBoolLiteralExprClass:
1168 case Expr::CXXNoexceptExprClass:
1169 case Expr::CXXNullPtrLiteralExprClass:
1170 case Expr::CXXPseudoDestructorExprClass:
1171 case Expr::CXXScalarValueInitExprClass:
1172 case Expr::CXXThisExprClass:
1173 case Expr::CXXUuidofExprClass:
1174 case Expr::CharacterLiteralClass:
1175 case Expr::ExpressionTraitExprClass:
1176 case Expr::FloatingLiteralClass:
1177 case Expr::GNUNullExprClass:
1178 case Expr::ImaginaryLiteralClass:
1179 case Expr::ImplicitValueInitExprClass:
1180 case Expr::IntegerLiteralClass:
1181 case Expr::NoInitExprClass:
1182 case Expr::ObjCEncodeExprClass:
1183 case Expr::ObjCStringLiteralClass:
1184 case Expr::ObjCBoolLiteralExprClass:
1185 case Expr::OpaqueValueExprClass:
1186 case Expr::PredefinedExprClass:
1187 case Expr::SizeOfPackExprClass:
1188 case Expr::StringLiteralClass:
1189 // These expressions can never throw.
1192 case Expr::MSPropertyRefExprClass:
1193 case Expr::MSPropertySubscriptExprClass:
1194 llvm_unreachable("Invalid class for expression");
1196 #define STMT(CLASS, PARENT) case Expr::CLASS##Class:
1197 #define STMT_RANGE(Base, First, Last)
1198 #define LAST_STMT_RANGE(BASE, FIRST, LAST)
1199 #define EXPR(CLASS, PARENT)
1200 #define ABSTRACT_STMT(STMT)
1201 #include "clang/AST/StmtNodes.inc"
1202 case Expr::NoStmtClass:
1203 llvm_unreachable("Invalid class for expression");
1205 llvm_unreachable("Bogus StmtClass");
1208 } // end namespace clang