Author: TPPPP
Date: 2026-03-18T20:12:41-07:00
New Revision: f1c8b9b4aad9e04501102b48d9abedd7f48b7af3

URL: 
https://github.com/llvm/llvm-project/commit/f1c8b9b4aad9e04501102b48d9abedd7f48b7af3
DIFF: 
https://github.com/llvm/llvm-project/commit/f1c8b9b4aad9e04501102b48d9abedd7f48b7af3.diff

LOG: [Clang] Fix assertion when __block is used on global variables in C mode 
(#183988)

I added an extra check in handleBlocksAttr to ensure that illegal Decl
values ​​are not passed to downstream functions.

Fixes #183974

Added: 
    clang/test/Sema/gh183974.c

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/SemaObjC.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index e1adaba4be7fc..d6a0a88960d6b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -377,6 +377,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 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;


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to