[clang] d4efbf1 - [Clang][ExprConst] Handle APValue::LValue in BitCast (#184865)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Mar 6 10:06:08 PST 2026
Author: Tommy Chiang
Date: 2026-03-07T02:06:03+08:00
New Revision: d4efbf10f183657c85bfed31b747f3d8f493ab31
URL: https://github.com/llvm/llvm-project/commit/d4efbf10f183657c85bfed31b747f3d8f493ab31
DIFF: https://github.com/llvm/llvm-project/commit/d4efbf10f183657c85bfed31b747f3d8f493ab31.diff
LOG: [Clang][ExprConst] Handle APValue::LValue in BitCast (#184865)
Instead of marking the branch unreachable, emit an unsupported type
diagnostic and return false when encountering `APValue::LValue` in
`APValueToBufferConverter`.
A good example of this is:
```
long fn() {
return __builtin_bit_cast(long, (long)&fn);
}
```
Although `&fn` itself is a pointer type, the cast `(long)` converts it
to an integer type, which passes `checkBitCastConstexprEligibilityType`.
Thus, eventually, we see it in Converter, which does not expect an
LValue.
Fixes https://github.com/llvm/llvm-project/issues/44991
Added:
clang/test/CodeGen/bitcast-based-lvalue.c
Modified:
clang/lib/AST/ExprConstant.cpp
Removed:
################################################################################
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 43409fd2af82e..429fef0a1afa8 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -7776,6 +7776,7 @@ class APValueToBufferConverter {
case APValue::FixedPoint:
// FIXME: We should support these.
+ case APValue::LValue:
case APValue::Matrix:
case APValue::Union:
case APValue::MemberPointer:
@@ -7785,9 +7786,6 @@ class APValueToBufferConverter {
<< Ty;
return false;
}
-
- case APValue::LValue:
- llvm_unreachable("LValue subobject in bit_cast?");
}
llvm_unreachable("Unhandled APValue::ValueKind");
}
diff --git a/clang/test/CodeGen/bitcast-based-lvalue.c b/clang/test/CodeGen/bitcast-based-lvalue.c
new file mode 100644
index 0000000000000..af64c6fe97ce9
--- /dev/null
+++ b/clang/test/CodeGen/bitcast-based-lvalue.c
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm -o - %s | FileCheck %s
+
+long fn(void) { return __builtin_bit_cast(long, (long)&fn); }
+
+// CHECK-LABEL: define{{.*}} i64 @fn()
+// CHECK: store i64 ptrtoint (ptr @fn to i64), ptr %ref.tmp, align 8
+// CHECK: ret i64 %{{.*}}
More information about the cfe-commits
mailing list