diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp index adfec6387ca4..b711c19b78e0 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp @@ -2710,18 +2710,15 @@ LValue CIRGenFunction::emitLValue(const Expr *E) { return emitCompoundLiteralLValue(cast(E)); case Expr::PredefinedExprClass: return emitPredefinedLValue(cast(E)); + case Expr::ImplicitCastExprClass: + case Expr::CStyleCastExprClass: case Expr::CXXFunctionalCastExprClass: + case Expr::CXXStaticCastExprClass: + case Expr::CXXDynamicCastExprClass: case Expr::CXXReinterpretCastExprClass: case Expr::CXXConstCastExprClass: case Expr::CXXAddrspaceCastExprClass: case Expr::ObjCBridgedCastExprClass: - emitError(getLoc(E->getExprLoc()), "l-value not implemented for '") - << E->getStmtClassName() << "'"; - assert(0 && "Use emitCastLValue below, remove me when adding testcase"); - case Expr::CStyleCastExprClass: - case Expr::CXXStaticCastExprClass: - case Expr::CXXDynamicCastExprClass: - case Expr::ImplicitCastExprClass: return emitCastLValue(cast(E)); case Expr::OpaqueValueExprClass: return emitOpaqueValueLValue(cast(E)); diff --git a/clang/test/CIR/CodeGen/cast-lvalue.cpp b/clang/test/CIR/CodeGen/cast-lvalue.cpp new file mode 100644 index 000000000000..fef6bdafdede --- /dev/null +++ b/clang/test/CIR/CodeGen/cast-lvalue.cpp @@ -0,0 +1,47 @@ +// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir +// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s +// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll +// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s +// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.orig.ll +// RUN: FileCheck --check-prefix=LLVM --input-file=%t.orig.ll %s +// +// Test for cast expressions as l-values (const_cast, reinterpret_cast, etc.) + +void const_cast_lvalue() { + const int x = 0; + const_cast(x) = 1; +} + +// CIR-LABEL: cir.func dso_local @_Z17const_cast_lvaluev +// CIR: %[[X:.*]] = cir.alloca !s32i, {{.*}}, ["x", init, const] +// CIR: %[[ZERO:.*]] = cir.const #cir.int<0> : !s32i +// CIR: cir.store {{.*}} %[[ZERO]], %[[X]] : !s32i, !cir.ptr +// CIR: %[[ONE:.*]] = cir.const #cir.int<1> : !s32i +// CIR: cir.store {{.*}} %[[ONE]], %[[X]] : !s32i, !cir.ptr +// CIR: cir.return + +// LLVM-LABEL: define {{.*}}void @_Z17const_cast_lvaluev +// LLVM: %[[X:.*]] = alloca i32 +// LLVM: store i32 0, ptr %[[X]] +// LLVM: store i32 1, ptr %[[X]] +// LLVM: ret void + +void reinterpret_cast_lvalue() { + long x = 0; + reinterpret_cast(x) = 1; +} + +// CIR-LABEL: cir.func dso_local @_Z23reinterpret_cast_lvaluev +// CIR: %[[X:.*]] = cir.alloca !s64i, {{.*}}, ["x", init] +// CIR: %[[ZERO:.*]] = cir.const #cir.int<0> : !s32i +// CIR: cir.store {{.*}} %{{.*}}, %[[X]] : !s64i, !cir.ptr +// CIR: %[[ONE:.*]] = cir.const #cir.int<1> : !s32i +// CIR: %[[CAST:.*]] = cir.cast bitcast %[[X]] : !cir.ptr -> !cir.ptr +// CIR: cir.store {{.*}} %[[ONE]], %[[CAST]] : !s32i, !cir.ptr +// CIR: cir.return + +// LLVM-LABEL: define {{.*}}void @_Z23reinterpret_cast_lvaluev +// LLVM: %[[X:.*]] = alloca i64 +// LLVM: store i64 0, ptr %[[X]] +// LLVM: store i32 1, ptr %[[X]] +// LLVM: ret void diff --git a/clang/test/CIR/crashes/const-cast-expr-lvalue.cpp b/clang/test/CIR/crashes/const-cast-expr-lvalue.cpp deleted file mode 100644 index c62bf9d74535..000000000000 --- a/clang/test/CIR/crashes/const-cast-expr-lvalue.cpp +++ /dev/null @@ -1,10 +0,0 @@ -// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir -// XFAIL: * -// -// CXXConstCastExpr l-value emission not implemented -// Location: CIRGenExpr.cpp:2720 - -void f() { - const int x = 0; - const_cast(x) = 1; -} diff --git a/clang/test/CIR/crashes/const-cast-lvalue.cpp b/clang/test/CIR/crashes/const-cast-lvalue.cpp index 593dfb618801..e2566bc1061e 100644 --- a/clang/test/CIR/crashes/const-cast-lvalue.cpp +++ b/clang/test/CIR/crashes/const-cast-lvalue.cpp @@ -1,5 +1,4 @@ // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir -// XFAIL: * // // CXXConstCastExpr l-value emission not implemented // Location: CIRGenExpr.cpp:2799