[clang] [Clang] Fix integer promotion of bit-fields in statement expressions (PR #225597)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Sep 22 22:14:04 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: ahmed mohamed kamel (AhmedKamel10)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/225597.diff
2 Files Affected:
- (modified) clang/lib/AST/Expr.cpp (+7)
- (modified) clang/test/Sema/stmtexprs.c (+9)
``````````diff
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index 6ce0a29aa3bd7..728be799c50a3 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 7493bbcef363d..b5fd9c21226b4 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");
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/225597
More information about the cfe-commits
mailing list