[clang] [Clang] Fix assertion when __block is used on global variables in C mode (PR #194856)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 29 06:21:52 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: TPPPP (TPPPP72)
<details>
<summary>Changes</summary>
This is a reland PR, related to #<!-- -->183988
I added an extra check in handleBlocksAttr to ensure that illegal Decl values are not passed to downstream functions.
Fixes #<!-- -->183974
Also added a extra regression test.
---
Full diff: https://github.com/llvm/llvm-project/pull/194856.diff
4 Files Affected:
- (modified) clang/docs/ReleaseNotes.rst (+1)
- (modified) clang/lib/Sema/SemaObjC.cpp (+6)
- (added) clang/test/Sema/block-on-objc-ivars.m (+11)
- (added) clang/test/Sema/gh183974.c (+5)
``````````diff
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 00987436a8e81..a5e6b82bca9ff 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -573,6 +573,7 @@ Bug Fixes to AST Handling
- Fixed a bug where explicit nullability property attributes were not stored in AST nodes in Objective-C. (#GH179703)
- Fixed a crash when parsing Doxygen ``@param`` commands attached to invalid declarations or non-function entities. (#GH182737)
- Fixed the SourceLocation and SourceRange of reversed rewritten CXXOperatorCallExpr. (#GH192467)
+- Fixed a assertion when __block is used on global variables in C mode. (#GH183974)
Miscellaneous Bug Fixes
^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaObjC.cpp b/clang/lib/Sema/SemaObjC.cpp
index dae30b7e941d1..c351e1e2079c1 100644
--- a/clang/lib/Sema/SemaObjC.cpp
+++ b/clang/lib/Sema/SemaObjC.cpp
@@ -1711,6 +1711,12 @@ void SemaObjC::handleBlocksAttr(Decl *D, const ParsedAttr &AL) {
return;
}
+ VarDecl *VD = dyn_cast<VarDecl>(D);
+ if (VD && !VD->hasLocalStorage()) {
+ Diag(AL.getLoc(), diag::err_block_on_nonlocal) << AL;
+ return;
+ }
+
D->addAttr(::new (getASTContext()) BlocksAttr(getASTContext(), AL, type));
}
diff --git a/clang/test/Sema/block-on-objc-ivars.m b/clang/test/Sema/block-on-objc-ivars.m
new file mode 100644
index 0000000000000..f37dc12fc109c
--- /dev/null
+++ b/clang/test/Sema/block-on-objc-ivars.m
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -fblocks -fsyntax-only -verify %s
+
+ at interface MyClass {
+ // expected-warning at -1 {{class 'MyClass' defined without specifying a base class}}
+ // expected-note at -2 {{add a super class to fix this problem}}
+ __block int _myIvar;
+}
+ at end
+
+ at implementation MyClass
+ at end
diff --git a/clang/test/Sema/gh183974.c b/clang/test/Sema/gh183974.c
new file mode 100644
index 0000000000000..642a622761f69
--- /dev/null
+++ b/clang/test/Sema/gh183974.c
@@ -0,0 +1,5 @@
+// RUN: %clang_cc1 -fblocks -fsyntax-only -verify %s
+
+__block int x; // expected-error {{__block attribute not allowed, only allowed on local variables}}
+
+int x;
``````````
</details>
https://github.com/llvm/llvm-project/pull/194856
More information about the cfe-commits
mailing list