[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 19:26:55 PDT 2026


================
@@ -1711,6 +1711,13 @@ 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;
+    D->setInvalidDecl();
----------------
TPPPP72 wrote:

@JustinStitt So I think just add this `setInvalidDecl()` can correct the AST.
```
./clang -x c -fblocks -Xclang -ast-dump test.c
test.c:1:1: error: __block attribute not allowed, only allowed on local variables
    1 | __block int x;
      | ^
<built-in>:42:32: note: expanded from macro '__block'
   42 | #define __block __attribute__((__blocks__(byref)))
      |                                ^
test.c:2:1: error: type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int [-Wimplicit-int]
    2 | y = x;
      | ^
      | int
TranslationUnitDecl 0x7d3d831fb908 <<invalid sloc>> <invalid sloc>
|-TypedefDecl 0x7d3d832509a8 <<invalid sloc>> <invalid sloc> implicit __int128_t '__int128'
| `-BuiltinType 0x7d3d831fc190 '__int128'
|-TypedefDecl 0x7d3d83250a20 <<invalid sloc>> <invalid sloc> implicit __uint128_t 'unsigned __int128'
| `-BuiltinType 0x7d3d831fc1c0 'unsigned __int128'
|-TypedefDecl 0x7d3d83250da8 <<invalid sloc>> <invalid sloc> implicit __NSConstantString 'struct __NSConstantString_tag'
| `-RecordType 0x7d3d83250d60 'struct __NSConstantString_tag' canonical
|   `-Record 0x7d3d83250a80 '__NSConstantString_tag'
|-TypedefDecl 0x7d3d831fc580 <<invalid sloc>> <invalid sloc> implicit __builtin_ms_va_list 'char *'
| `-PointerType 0x7d3d831fc530 'char *'
|   `-BuiltinType 0x7d3d831fb9e0 'char'
|-TypedefDecl 0x7d3d83250930 <<invalid sloc>> <invalid sloc> implicit __builtin_va_list 'struct __va_list_tag[1]'
| `-ConstantArrayType 0x7d3d831fc8a0 'struct __va_list_tag[1]' 1
|   `-RecordType 0x7d3d831fc840 'struct __va_list_tag' canonical
|     `-Record 0x7d3d831fc5e0 '__va_list_tag'
|-VarDecl 0x7d3d83250e38 <<built-in>:42:17, test.c:1:13> col:13 invalid x 'int'
`-VarDecl 0x7d3d83250ec0 <line:2:1, col:5> col:1 y 'int' cinit
  `-RecoveryExpr 0x7d3d83250f80 <col:5> '<dependent type>' contains-errors lvalue
2 errors generated.
``` 

https://github.com/llvm/llvm-project/pull/194856


More information about the cfe-commits mailing list