r256838 - [OpenCL] Disallow taking an address of a function.

Anastasia Stulova via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 5 06:39:28 PST 2016


Author: stulova
Date: Tue Jan  5 08:39:27 2016
New Revision: 256838

URL: http://llvm.org/viewvc/llvm-project?rev=256838&view=rev
Log:
[OpenCL] Disallow taking an address of a function.

An undecorated function designator implies taking the address of a function,
which is illegal in OpenCL. Implementing a check for this earlier to allow
the error to be reported even in the presence of other more obvious errors.

Patch by Neil Hickey!

http://reviews.llvm.org/D15691


Modified:
    cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td
    cfe/trunk/lib/Parse/ParseExpr.cpp
    cfe/trunk/test/SemaOpenCL/cond.cl
    cfe/trunk/test/SemaOpenCL/func_ptr.cl

Modified: cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td?rev=256838&r1=256837&r2=256838&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td Tue Jan  5 08:39:27 2016
@@ -910,6 +910,10 @@ def warn_pragma_expected_enable_disable
 def warn_pragma_unknown_extension : Warning<
   "unknown OpenCL extension %0 - ignoring">, InGroup<IgnoredPragmas>;
 
+// OpenCL error
+def err_opencl_taking_function_address_parser : Error<
+  "taking address of function is not allowed">;
+
 // OpenMP support.
 def warn_pragma_omp_ignored : Warning<
   "unexpected '#pragma omp ...' in program">, InGroup<SourceUsesOpenMP>, DefaultIgnore;

Modified: cfe/trunk/lib/Parse/ParseExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseExpr.cpp?rev=256838&r1=256837&r2=256838&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParseExpr.cpp (original)
+++ cfe/trunk/lib/Parse/ParseExpr.cpp Tue Jan  5 08:39:27 2016
@@ -1334,8 +1334,23 @@ ExprResult Parser::ParseCastExpression(b
     return ExprError();
   }
 
+  // Check to see whether Res is a function designator only. If it is and we
+  // are compiling for OpenCL, we need to return an error as this implies
+  // that the address of the function is being taken, which is illegal in CL.
+
   // These can be followed by postfix-expr pieces.
-  return ParsePostfixExpressionSuffix(Res);
+  Res = ParsePostfixExpressionSuffix(Res);
+  if (getLangOpts().OpenCL)
+    if (Expr *PostfixExpr = Res.get()) {
+      QualType Ty = PostfixExpr->getType();
+      if (!Ty.isNull() && Ty->isFunctionType()) {
+        Diag(PostfixExpr->getExprLoc(),
+             diag::err_opencl_taking_function_address_parser);
+        return ExprError();
+      }
+    }
+
+  return Res;
 }
 
 /// \brief Once the leading part of a postfix-expression is parsed, this

Modified: cfe/trunk/test/SemaOpenCL/cond.cl
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaOpenCL/cond.cl?rev=256838&r1=256837&r2=256838&view=diff
==============================================================================
--- cfe/trunk/test/SemaOpenCL/cond.cl (original)
+++ cfe/trunk/test/SemaOpenCL/cond.cl Tue Jan  5 08:39:27 2016
@@ -128,5 +128,5 @@ int foo2(int);
 
 unsigned int ntest12(int2 C)
 {
-  return (unsigned int)(C ? foo1 : foo2); // expected-error {{taking address of function is not allowed}}
+  return (unsigned int)(C ? foo1 : foo2); // expected-error {{taking address of function is not allowed}} expected-error {{taking address of function is not allowed}}
 }

Modified: cfe/trunk/test/SemaOpenCL/func_ptr.cl
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaOpenCL/func_ptr.cl?rev=256838&r1=256837&r2=256838&view=diff
==============================================================================
--- cfe/trunk/test/SemaOpenCL/func_ptr.cl (original)
+++ cfe/trunk/test/SemaOpenCL/func_ptr.cl Tue Jan  5 08:39:27 2016
@@ -11,6 +11,9 @@ void bar()
   foo((void*)foo); // expected-error{{taking address of function is not allowed}}
   foo(&foo); // expected-error{{taking address of function is not allowed}}
 
+  // initializing an array with the address of functions is an error
+  void* vptrarr[2] = {foo, &foo}; // expected-error{{taking address of function is not allowed}} expected-error{{taking address of function is not allowed}}
+
   // just calling a function is correct
   foo(0);
 }




More information about the cfe-commits mailing list