Skip to content

Commit abbdb56

Browse files
author
Anastasia Stulova
committed
[OpenCL] Allow taking address of functions as an extension.
When '__cl_clang_function_pointers' extension is enabled the parser should allow obtaining the function address. This fixes PR49264! Differential Revision: https://reviews.llvm.org/D97203
1 parent 2105912 commit abbdb56

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

clang/lib/Parse/ParseExpr.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1807,7 +1807,8 @@ ExprResult Parser::ParseCastExpression(CastParseKind ParseKind,
18071807
// These can be followed by postfix-expr pieces.
18081808
PreferredType = SavedType;
18091809
Res = ParsePostfixExpressionSuffix(Res);
1810-
if (getLangOpts().OpenCL)
1810+
if (getLangOpts().OpenCL && !getActions().getOpenCLOptions().isEnabled(
1811+
"__cl_clang_function_pointers"))
18111812
if (Expr *PostfixExpr = Res.get()) {
18121813
QualType Ty = PostfixExpr->getType();
18131814
if (!Ty.isNull() && Ty->isFunctionType()) {

clang/test/SemaOpenCL/func.cl

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ typedef struct s
3838

3939
//Function pointer
4040
void foo(void*);
41+
#ifdef FUNCPTREXT
42+
//expected-note@-2{{passing argument to parameter here}}
43+
#endif
4144

4245
// Expect no diagnostics for an empty parameter list.
4346
void bar();
@@ -51,11 +54,30 @@ void bar()
5154
#endif
5255

5356
// taking the address of a function is an error
54-
foo((void*)foo); // expected-error{{taking address of function is not allowed}}
55-
foo(&foo); // expected-error{{taking address of function is not allowed}}
57+
foo((void*)foo);
58+
#ifndef FUNCPTREXT
59+
// expected-error@-2{{taking address of function is not allowed}}
60+
#else
61+
// FIXME: Functions should probably be in the address space defined by the
62+
// implementation. It might make sense to put them into the Default address
63+
// space that is bind to a physical segment by the target rather than fixing
64+
// it to any of the concrete OpenCL address spaces during parsing.
65+
// expected-error@-8{{casting 'void (*)(__private void *__private)' to type '__private void *' changes address space}}
66+
#endif
5667

68+
foo(&foo);
69+
#ifndef FUNCPTREXT
70+
// expected-error@-2{{taking address of function is not allowed}}
71+
#else
72+
// expected-error@-4{{passing 'void (*)(__private void *__private)' to parameter of type '__private void *' changes address space of pointer}}
73+
#endif
74+
75+
// FIXME: If we stop rejecting the line below a bug (PR49315) gets
76+
// hit due to incorrectly handled pointer conversion.
77+
#ifndef FUNCPTREXT
5778
// initializing an array with the address of functions is an error
5879
void* vptrarr[2] = {foo, &foo}; // expected-error{{taking address of function is not allowed}} expected-error{{taking address of function is not allowed}}
80+
#endif
5981

6082
// just calling a function is correct
6183
foo(0);

0 commit comments

Comments
 (0)