Skip to content

Conversation

@svenvh
Copy link
Member

@svenvh svenvh commented Nov 4, 2024

In addition to the invocation case that is already diagnosed, also diagnose when a block reference appears on either side of a ternary selection operator.

Until now, clang would accept the added test case only to crash during code generation.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Nov 4, 2024
@llvmbot
Copy link
Member

llvmbot commented Nov 4, 2024

@llvm/pr-subscribers-clang

Author: Sven van Haastregt (svenvh)

Changes

In addition to the invocation case that is already diagnosed, also diagnose when a block reference appears on either side of a ternary selection operator.


Full diff: https://github.com/llvm/llvm-project/pull/114824.diff

2 Files Affected:

  • (modified) clang/lib/Sema/SemaExpr.cpp (+5)
  • (modified) clang/test/SemaOpenCL/invalid-block.cl (+2)
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 7f3cff1054aeed..cbaca2fb5af56e 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -8374,6 +8374,11 @@ OpenCLCheckVectorConditional(Sema &S, ExprResult &Cond,
 
 /// Return true if the Expr is block type
 static bool checkBlockType(Sema &S, const Expr *E) {
+  if (E->getType()->isBlockPointerType()) {
+    S.Diag(E->getExprLoc(), diag::err_opencl_ternary_with_block);
+    return true;
+  }
+
   if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
     QualType Ty = CE->getCallee()->getType();
     if (Ty->isBlockPointerType()) {
diff --git a/clang/test/SemaOpenCL/invalid-block.cl b/clang/test/SemaOpenCL/invalid-block.cl
index 6c918d302f8018..16053690004297 100644
--- a/clang/test/SemaOpenCL/invalid-block.cl
+++ b/clang/test/SemaOpenCL/invalid-block.cl
@@ -65,6 +65,8 @@ void f5(int i) {
   bl2_t arr[] = {bl1, bl2}; // expected-error {{array of 'bl2_t' (aka 'int (__generic ^const)(__private int)') type is invalid in OpenCL}}
   int tmp = i ? bl1(i)      // expected-error {{block type cannot be used as expression in ternary expression in OpenCL}}
               : bl2(i);     // expected-error {{block type cannot be used as expression in ternary expression in OpenCL}}
+  bl2_t bref = i ? bl1      // expected-error {{block type cannot be used as expression in ternary expression in OpenCL}}
+                 : bl2;     // expected-error {{block type cannot be used as expression in ternary expression in OpenCL}}
 }
 // A block pointer type and all pointer operations are disallowed
 void f6(bl2_t *bl_ptr) { // expected-error{{pointer to type 'bl2_t' (aka 'int (__generic ^const)(__private int)') is invalid in OpenCL}}

@silverclaw
Copy link
Contributor

Would be great to see this upstreamed before it just gets ignored...

In addition to the case that is already diagnosed, also diagnose when
a block reference appears on either side of a ternary selection
operator.
@svenvh svenvh force-pushed the block-selection-sema branch from 386b891 to 20c2eac Compare January 21, 2025 15:47
@svenvh svenvh merged commit 957213f into llvm:main Jan 22, 2025
8 checks passed
@svenvh svenvh deleted the block-selection-sema branch January 22, 2025 08:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants