[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 09:17:28 PDT 2026
https://github.com/TPPPP72 updated https://github.com/llvm/llvm-project/pull/194856
>From c7312786a2782d8792006b9a27ce9fe9bcdca44d Mon Sep 17 00:00:00 2001
From: TPPPP <TPPPP72 at outlook.com>
Date: Wed, 29 Apr 2026 21:04:38 +0800
Subject: [PATCH 1/3] [Clang] Fix crash when __block is used on global
variables in C mode
---
clang/docs/ReleaseNotes.rst | 1 +
clang/lib/Sema/SemaObjC.cpp | 6 ++++++
clang/test/Sema/gh183974.c | 5 +++++
3 files changed, 12 insertions(+)
create mode 100644 clang/test/Sema/gh183974.c
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/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;
>From d229157b043893af0dbf8920006faa23ee80f96f Mon Sep 17 00:00:00 2001
From: TPPPP <TPPPP72 at outlook.com>
Date: Wed, 29 Apr 2026 21:14:51 +0800
Subject: [PATCH 2/3] Add a block check
---
clang/test/Sema/block-on-objc-ivars.m | 11 +++++++++++
1 file changed, 11 insertions(+)
create mode 100644 clang/test/Sema/block-on-objc-ivars.m
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
>From d054c6bd403f9658180142b7fe39e1fe04276b36 Mon Sep 17 00:00:00 2001
From: TPPPP <TPPPP72 at outlook.com>
Date: Thu, 30 Apr 2026 00:17:17 +0800
Subject: [PATCH 3/3] Update clang/docs/ReleaseNotes.rst
Co-authored-by: Aaron Ballman <aaron at aaronballman.com>
---
clang/docs/ReleaseNotes.rst | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index a5e6b82bca9ff..1210445f37ff3 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -573,7 +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)
+- Fixed a assertion when ``__block`` is used on global variables in C mode. (#GH183974)
Miscellaneous Bug Fixes
^^^^^^^^^^^^^^^^^^^^^^^
More information about the cfe-commits
mailing list