[clang] [Clang] Fix integer promotion of bit-fields in statement expressions (PR #225597)
ahmed mohamed kamel via cfe-commits
cfe-commits at lists.llvm.org
Tue Sep 22 22:13:24 PDT 2026
https://github.com/AhmedKamel10 created https://github.com/llvm/llvm-project/pull/225597
Fixes integer promotion for bit-fields used in statement expressions (#221542).
`Expr::getSourceBitField()` now looks through the final expression of a statement expression to preserve the bit-field information. This allows the existing integer promotion rules to correctly promote the bit-field to `int`.
Also added a regression test for the issue.
>From 24f6a72b872a1ee6ef34435e2ffe55cb6661714a Mon Sep 17 00:00:00 2001
From: ahmedkamel10 <amkzaher at gmail.com>
Date: Wed, 23 Sep 2026 07:53:34 +0300
Subject: [PATCH] [Clang] Fix integer promotion of bit-fields in statement
expressions
---
clang/lib/AST/Expr.cpp | 7 +++++++
clang/test/Sema/stmtexprs.c | 9 +++++++++
2 files changed, 16 insertions(+)
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index 6ce0a29aa3bd78..728be799c50a39 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -4273,6 +4273,13 @@ FieldDecl *Expr::getSourceBitField() {
break;
}
+ if (StmtExpr *SE = dyn_cast<StmtExpr>(E)) {
+ CompoundStmt *CS = SE->getSubStmt();
+ if (ValueStmt *VS = dyn_cast_or_null<ValueStmt>(CS->body_back()))
+ if (Expr *EX = VS->getExprStmt())
+ return EX->getSourceBitField();
+ }
+
if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
if (Field->isBitField())
diff --git a/clang/test/Sema/stmtexprs.c b/clang/test/Sema/stmtexprs.c
index 7493bbcef363d9..b5fd9c21226b44 100644
--- a/clang/test/Sema/stmtexprs.c
+++ b/clang/test/Sema/stmtexprs.c
@@ -7,3 +7,12 @@ void stmtexprs(int i) {
// expected-warning at +1 {{assumption is ignored because it contains (potential) side-effects}}
__builtin_assume( ({ if (i) ({ stmtexpr_fn(); }); 1; }) );
}
+
+struct S {
+ unsigned b : 3;
+};
+
+void test_bitfield_promotion(struct S s) {
+ _Static_assert(_Generic(+({ s.b; }), int: 1, unsigned: 2) == 1,
+ "bit-field in statement expression should be promoted");
+}
More information about the cfe-commits
mailing list