]> granicus.if.org Git - clang/commitdiff
[Sema] Reject value-initialization of function types
authorDavid Majnemer <david.majnemer@gmail.com>
Mon, 14 Sep 2015 07:05:00 +0000 (07:05 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Mon, 14 Sep 2015 07:05:00 +0000 (07:05 +0000)
T in the expression T() must be a non-array complete object type or
the void type.  Function types are neither.

This fixes PR24798.

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

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaExprCXX.cpp
test/SemaCXX/type-convert-construct.cpp

index 2fa7f01d080153c7e4b8723e98d6de5767fc3d27..49e5d698e3fbf6a2c3ddec20fe73a4c96e7a8f3f 100644 (file)
@@ -5219,6 +5219,8 @@ def err_builtin_func_cast_more_than_one_arg : Error<
   "function-style cast to a builtin type can only take one argument">;
 def err_value_init_for_array_type : Error<
   "array types cannot be value-initialized">;
+def err_value_init_for_function_type : Error<
+  "function types cannot be value-initialized">;
 def warn_format_nonliteral_noargs : Warning<
   "format string is not a string literal (potentially insecure)">,
   InGroup<FormatSecurity>;
index a48e634c3cda1edaeea4c966d39c4e90e40788bf..f75ec70cbeb3c93f6922d286ac8a8fdce7a473ad 100644 (file)
@@ -1031,6 +1031,11 @@ Sema::BuildCXXTypeConstructExpr(TypeSourceInfo *TInfo,
     return BuildCXXFunctionalCastExpr(TInfo, LParenLoc, Arg, RParenLoc);
   }
 
+  // C++14 [expr.type.conv]p2: The expression T(), where T is a
+  //   simple-type-specifier or typename-specifier for a non-array complete
+  //   object type or the (possibly cv-qualified) void type, creates a prvalue
+  //   of the specified type, whose value is that produced by value-initializing
+  //   an object of type T.
   QualType ElemTy = Ty;
   if (Ty->isArrayType()) {
     if (!ListInitialization)
@@ -1039,6 +1044,10 @@ Sema::BuildCXXTypeConstructExpr(TypeSourceInfo *TInfo,
     ElemTy = Context.getBaseElementType(Ty);
   }
 
+  if (!ListInitialization && Ty->isFunctionType())
+    return ExprError(Diag(TyBeginLoc, diag::err_value_init_for_function_type)
+                     << FullRange);
+
   if (!Ty->isVoidType() &&
       RequireCompleteType(TyBeginLoc, ElemTy,
                           diag::err_invalid_incomplete_type_use, FullRange))
index 479af21476be6984b7b6ba8e2c0969c13293ebda..2dec50abebf28cb706b59f505531ab011c571f7f 100644 (file)
@@ -5,6 +5,8 @@ void f() {
   int v2 = typeof(int)(1,2); // expected-error {{excess elements in scalar initializer}}
   typedef int arr[];
   int v3 = arr(); // expected-error {{array types cannot be value-initialized}}
+  typedef void fn_ty();
+  fn_ty(); // expected-error {{function types cannot be value-initialized}}
   int v4 = int();
   int v5 = int; // expected-error {{expected '(' for function-style cast or type construction}}
   typedef int T;