Skip to content

Commit 767a203

Browse files
authored
[clang][bytecode] Fix discarded LValueToRValueBitCasts (#140034)
We handle discarding fine, but we used to ignore all discarded cast expressions. Handle bitcasts differently.
1 parent f8f11c5 commit 767a203

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -474,10 +474,6 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
474474
return false;
475475
return this->emitDecayPtr(*FromT, *ToT, CE);
476476
}
477-
478-
case CK_LValueToRValueBitCast:
479-
return this->emitBuiltinBitCast(CE);
480-
481477
case CK_IntegralToBoolean:
482478
case CK_FixedPointToBoolean: {
483479
// HLSL uses this to cast to one-element vectors.
@@ -735,6 +731,11 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
735731
llvm_unreachable("Unhandled clang::CastKind enum");
736732
}
737733

734+
template <class Emitter>
735+
bool Compiler<Emitter>::VisitBuiltinBitCastExpr(const BuiltinBitCastExpr *E) {
736+
return this->emitBuiltinBitCast(E);
737+
}
738+
738739
template <class Emitter>
739740
bool Compiler<Emitter>::VisitIntegerLiteral(const IntegerLiteral *LE) {
740741
if (DiscardResult)

clang/lib/AST/ByteCode/Compiler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ class Compiler : public ConstStmtVisitor<Compiler<Emitter>, bool>,
126126

127127
// Expressions.
128128
bool VisitCastExpr(const CastExpr *E);
129+
bool VisitBuiltinBitCastExpr(const BuiltinBitCastExpr *E);
129130
bool VisitIntegerLiteral(const IntegerLiteral *E);
130131
bool VisitFloatingLiteral(const FloatingLiteral *E);
131132
bool VisitImaginaryLiteral(const ImaginaryLiteral *E);

clang/test/AST/ByteCode/builtin-bit-cast.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,16 @@ namespace OversizedBitField {
503503
#endif
504504
}
505505

506+
namespace Discarded {
507+
enum my_byte : unsigned char {};
508+
struct pad {
509+
char a;
510+
int b;
511+
};
512+
constexpr int bad_my_byte = (__builtin_bit_cast(my_byte[8], pad{1, 2}), 0); // both-error {{must be initialized by a constant expression}} \
513+
// both-note {{indeterminate value can only initialize an object of type 'unsigned char' or 'std::byte';}}
514+
}
515+
506516
typedef bool bool9 __attribute__((ext_vector_type(9)));
507517
// both-error@+2 {{constexpr variable 'bad_bool9_to_short' must be initialized by a constant expression}}
508518
// both-note@+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}}

0 commit comments

Comments
 (0)