Author: TPPPP
Date: 2026-05-12T09:59:13-04:00
New Revision: ef6df9dcea99d7dad003539c82264cf8777374e7

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

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

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.
And remove unnecessary check in `CheckCompleteVariableDeclaration`.

Also added a extra regression test.

Fixes #183974

Added: 
    clang/test/Sema/block-on-objc-ivars.m
    clang/test/Sema/gh183974.c

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

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index bd91b8723a5c6..7ef88461c11a7 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -605,6 +605,7 @@ Bug Fixes to AST Handling
   parameter list. This also adds asserts to prevent this from happening again.
 - 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/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index a9a4cb89d115f..bec351528760a 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -9133,12 +9133,6 @@ void Sema::CheckVariableDeclarationType(VarDecl *NewVD) {
     }
   }
 
-  if (!NewVD->hasLocalStorage() && NewVD->hasAttr<BlocksAttr>()) {
-    Diag(NewVD->getLocation(), diag::err_block_on_nonlocal);
-    NewVD->setInvalidDecl();
-    return;
-  }
-
   if (!NewVD->hasLocalStorage() && T->isSizelessType() &&
       !T.isWebAssemblyReferenceType() && !T->isHLSLSpecificType()) {
     Diag(NewVD->getLocation(), diag::err_sizeless_nonlocal) << T;

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
+
+@interface MyClass {
+    // expected-warning@-1 {{class 'MyClass' defined without specifying a base 
class}}
+    // expected-note@-2 {{add a super class to fix this problem}}
+    __block int _myIvar;
+}
+@end
+
+@implementation MyClass
+@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;


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

Reply via email to