[clang] [clang][bytecode] Fix discarded LValueToRValueBitCasts (PR #140034)
via cfe-commits
cfe-commits at lists.llvm.org
Thu May 15 03:08:41 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
We handle discarding fine, but we used to ignore all discarded cast expressions. Handle bitcasts differently.
---
Full diff: https://github.com/llvm/llvm-project/pull/140034.diff
3 Files Affected:
- (modified) clang/lib/AST/ByteCode/Compiler.cpp (+5-4)
- (modified) clang/lib/AST/ByteCode/Compiler.h (+1)
- (modified) clang/test/AST/ByteCode/builtin-bit-cast.cpp (+10)
``````````diff
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 67b6ae4a393e9..2580fb17ce5e3 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -474,10 +474,6 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
return false;
return this->emitDecayPtr(*FromT, *ToT, CE);
}
-
- case CK_LValueToRValueBitCast:
- return this->emitBuiltinBitCast(CE);
-
case CK_IntegralToBoolean:
case CK_FixedPointToBoolean: {
// HLSL uses this to cast to one-element vectors.
@@ -735,6 +731,11 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
llvm_unreachable("Unhandled clang::CastKind enum");
}
+template <class Emitter>
+bool Compiler<Emitter>::VisitBuiltinBitCastExpr(const BuiltinBitCastExpr *E) {
+ return this->emitBuiltinBitCast(E);
+}
+
template <class Emitter>
bool Compiler<Emitter>::VisitIntegerLiteral(const IntegerLiteral *LE) {
if (DiscardResult)
diff --git a/clang/lib/AST/ByteCode/Compiler.h b/clang/lib/AST/ByteCode/Compiler.h
index ec5bd637453c5..56a972f452af9 100644
--- a/clang/lib/AST/ByteCode/Compiler.h
+++ b/clang/lib/AST/ByteCode/Compiler.h
@@ -126,6 +126,7 @@ class Compiler : public ConstStmtVisitor<Compiler<Emitter>, bool>,
// Expressions.
bool VisitCastExpr(const CastExpr *E);
+ bool VisitBuiltinBitCastExpr(const BuiltinBitCastExpr *E);
bool VisitIntegerLiteral(const IntegerLiteral *E);
bool VisitFloatingLiteral(const FloatingLiteral *E);
bool VisitImaginaryLiteral(const ImaginaryLiteral *E);
diff --git a/clang/test/AST/ByteCode/builtin-bit-cast.cpp b/clang/test/AST/ByteCode/builtin-bit-cast.cpp
index 187f180afd3da..3c5e89d7d5a74 100644
--- a/clang/test/AST/ByteCode/builtin-bit-cast.cpp
+++ b/clang/test/AST/ByteCode/builtin-bit-cast.cpp
@@ -503,6 +503,16 @@ namespace OversizedBitField {
#endif
}
+namespace Discarded {
+ enum my_byte : unsigned char {};
+ struct pad {
+ char a;
+ int b;
+ };
+ constexpr int bad_my_byte = (__builtin_bit_cast(my_byte[8], pad{1, 2}), 0); // both-error {{must be initialized by a constant expression}} \
+ // both-note {{indeterminate value can only initialize an object of type 'unsigned char' or 'std::byte';}}
+}
+
typedef bool bool9 __attribute__((ext_vector_type(9)));
// both-error at +2 {{constexpr variable 'bad_bool9_to_short' must be initialized by a constant expression}}
// both-note at +1 {{bit_cast involving type 'bool __attribute__((ext_vector_type(9)))' (vector of 9 'bool' values) is not allowed in a constant expression; element size 1 * element count 9 is not a multiple of the byte size 8}}
``````````
</details>
https://github.com/llvm/llvm-project/pull/140034
More information about the cfe-commits
mailing list