]> granicus.if.org Git - clang/commitdiff
[c++1z] Permit constant evaluation of a call through a function pointer whose
authorRichard Smith <richard-llvm@metafoo.co.uk>
Thu, 15 Dec 2016 02:35:39 +0000 (02:35 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Thu, 15 Dec 2016 02:35:39 +0000 (02:35 +0000)
type differs from the type of the actual function due to having a different
exception specification.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@289754 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/ASTContext.h
lib/AST/ASTContext.cpp
lib/AST/ExprConstant.cpp
lib/Sema/SemaDecl.cpp
test/SemaCXX/constant-expression-cxx1z.cpp

index 4f35508ebac5c75e4d59ed4644dfa4c4fef7e095..47d29bc20920d0b3415aebba1c498566bcb3b03d 100644 (file)
@@ -1109,6 +1109,10 @@ public:
   /// \brief Change the result type of a function type once it is deduced.
   void adjustDeducedFunctionResultType(FunctionDecl *FD, QualType ResultType);
 
+  /// \brief Determine whether two function types are the same, ignoring
+  /// exception specifications in cases where they're part of the type.
+  bool hasSameFunctionTypeIgnoringExceptionSpec(QualType T, QualType U);
+
   /// \brief Change the exception specification on a function once it is
   /// delay-parsed, instantiated, or computed.
   void adjustExceptionSpec(FunctionDecl *FD,
index 22be71a7272d6f2465872ed62f709f0daf076714..73ea66e98e87703acb66ba83c8168fc3d1411034 100644 (file)
@@ -2382,6 +2382,14 @@ static QualType getFunctionTypeWithExceptionSpec(
       Proto->getExtProtoInfo().withExceptionSpec(ESI));
 }
 
+bool ASTContext::hasSameFunctionTypeIgnoringExceptionSpec(QualType T,
+                                                          QualType U) {
+  return hasSameType(T, U) ||
+         (getLangOpts().CPlusPlus1z &&
+          hasSameType(getFunctionTypeWithExceptionSpec(*this, T, EST_None),
+                      getFunctionTypeWithExceptionSpec(*this, U, EST_None)));
+}
+
 void ASTContext::adjustExceptionSpec(
     FunctionDecl *FD, const FunctionProtoType::ExceptionSpecInfo &ESI,
     bool AsWritten) {
index 5dd493cfff58a6d0b01fad004a7e0cb23a198cc8..36f5e6aae554b5d93edefc1db7b4ca81e69cfb09 100644 (file)
@@ -4434,8 +4434,11 @@ public:
       }
 
       // Don't call function pointers which have been cast to some other type.
-      if (!Info.Ctx.hasSameType(CalleeType->getPointeeType(), FD->getType()))
+      // Per DR (no number yet), the caller and callee can differ in noexcept.
+      if (!Info.Ctx.hasSameFunctionTypeIgnoringExceptionSpec(
+              CalleeType->getPointeeType(), FD->getType())) {
         return Error(E);
+      }
     } else
       return Error(E);
 
index 89cb4398638f1a8393cfa503dbafd616b6b1f3be..7878355fc1928958c0b1ccb37bf231e3d05640eb 100644 (file)
@@ -9076,27 +9076,16 @@ bool Sema::CheckFunctionDeclaration(Scope *S, FunctionDecl *NewFD,
       ASTContext::GetBuiltinTypeError Error;
       LookupPredefedObjCSuperType(*this, S, NewFD->getIdentifier());
       QualType T = Context.GetBuiltinType(BuiltinID, Error);
-      if (!T.isNull() && !Context.hasSameType(T, NewFD->getType())) {
-        auto WithoutExceptionSpec = [&](QualType T) -> QualType {
-          auto *Proto = T->getAs<FunctionProtoType>();
-          if (!Proto)
-            return T;
-          return Context.getFunctionType(
-              Proto->getReturnType(), Proto->getParamTypes(),
-              Proto->getExtProtoInfo().withExceptionSpec(EST_None));
-        };
-
-        // If the type of the builtin differs only in its exception
-        // specification, that's OK.
-        // FIXME: If the types do differ in this way, it would be better to
-        // retain the 'noexcept' form of the type.
-        if (!getLangOpts().CPlusPlus1z ||
-            !Context.hasSameType(WithoutExceptionSpec(T),
-                                 WithoutExceptionSpec(NewFD->getType())))
-          // The type of this function differs from the type of the builtin,
-          // so forget about the builtin entirely.
-          Context.BuiltinInfo.forgetBuiltin(BuiltinID, Context.Idents);
-      }
+      // If the type of the builtin differs only in its exception
+      // specification, that's OK.
+      // FIXME: If the types do differ in this way, it would be better to
+      // retain the 'noexcept' form of the type.
+      if (!T.isNull() &&
+          !Context.hasSameFunctionTypeIgnoringExceptionSpec(T,
+                                                            NewFD->getType()))
+        // The type of this function differs from the type of the builtin,
+        // so forget about the builtin entirely.
+        Context.BuiltinInfo.forgetBuiltin(BuiltinID, Context.Idents);
     }
 
     // If this function is declared as being extern "C", then check to see if
index 1563586e1968daa6b98c267c49f40d7d9d13af58..a48c9b11b886ba9b6490ddf3b7ac83b7de44ad56 100644 (file)
@@ -28,7 +28,9 @@ namespace BaseClassAggregateInit {
 
 namespace NoexceptFunctionTypes {
   template<typename T> constexpr bool f() noexcept(true) { return true; }
+  constexpr bool (*fp)() = f<int>;
   static_assert(f<int>());
+  static_assert(fp());
 
   template<typename T> struct A {
     constexpr bool f() noexcept(true) { return true; }