[clang] [Clang] Fix assertion when __block is used on global variables in C mode (PR #183988)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 9 06:03:34 PDT 2026
https://github.com/TPPPP72 updated https://github.com/llvm/llvm-project/pull/183988
>From 931971a735a896d150e47a880e66323d34e4f995 Mon Sep 17 00:00:00 2001
From: TPPPP72 <906483498 at qq.com>
Date: Mon, 9 Mar 2026 21:02:48 +0800
Subject: [PATCH] [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 5d07bfc210e05..283448f96c633 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -351,6 +351,7 @@ Bug Fixes to C++ Support
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 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..dcd1b35e8fbc7 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;
More information about the cfe-commits
mailing list