https://github.com/TPPPP72 updated https://github.com/llvm/llvm-project/pull/194856
>From ebacd80fc75b2091105f1faf268344ba238b1906 Mon Sep 17 00:00:00 2001 From: TPPPP <[email protected]> Date: Wed, 29 Apr 2026 21:04:38 +0800 Subject: [PATCH 1/4] [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 c17143e3c0398..222aeacc7d960 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -601,6 +601,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/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 3bee288b30e029f61507d86de1dd8a1a94a194d4 Mon Sep 17 00:00:00 2001 From: TPPPP <[email protected]> Date: Wed, 29 Apr 2026 21:14:51 +0800 Subject: [PATCH 2/4] 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 + +@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 >From 4093f43b219cbb51332da3f8d23592164a9c1cd5 Mon Sep 17 00:00:00 2001 From: TPPPP <[email protected]> Date: Thu, 30 Apr 2026 00:17:17 +0800 Subject: [PATCH 3/4] Update clang/docs/ReleaseNotes.rst Co-authored-by: Aaron Ballman <[email protected]> --- 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 222aeacc7d960..f31ed2be602b7 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -601,7 +601,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) +- Fixed a assertion when ``__block`` is used on global variables in C mode. (#GH183974) Miscellaneous Bug Fixes ^^^^^^^^^^^^^^^^^^^^^^^ >From 6706106bf3242b8c390298686f3287dedbf98f2a Mon Sep 17 00:00:00 2001 From: TPPPP <[email protected]> Date: Tue, 12 May 2026 10:54:51 +0800 Subject: [PATCH 4/4] update --- clang/lib/Sema/SemaDecl.cpp | 6 ------ clang/lib/Sema/SemaObjC.cpp | 1 + 2 files changed, 1 insertion(+), 6 deletions(-) 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 c351e1e2079c1..ff11e928df982 100644 --- a/clang/lib/Sema/SemaObjC.cpp +++ b/clang/lib/Sema/SemaObjC.cpp @@ -1714,6 +1714,7 @@ void SemaObjC::handleBlocksAttr(Decl *D, const ParsedAttr &AL) { VarDecl *VD = dyn_cast<VarDecl>(D); if (VD && !VD->hasLocalStorage()) { Diag(AL.getLoc(), diag::err_block_on_nonlocal) << AL; + D->setInvalidDecl(); return; } _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
