From: Richard Smith Date: Fri, 19 Oct 2018 19:01:31 +0000 (+0000) Subject: Add basic test that we perform lifetime extension in the expected X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=396dc1658c22d495834ce71a9c975770ed3fca40;p=clang Add basic test that we perform lifetime extension in the expected situations. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@344800 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/test/CXX/special/class.temporary/p6.cpp b/test/CXX/special/class.temporary/p6.cpp new file mode 100644 index 0000000000..e868090216 --- /dev/null +++ b/test/CXX/special/class.temporary/p6.cpp @@ -0,0 +1,190 @@ +// RUN: %clang_cc1 -std=c++17 %s -emit-llvm -o - | FileCheck %s --implicit-check-not='call{{.*}}dtor' + +void then(); + +struct dtor { + ~dtor(); +}; + +dtor ctor(); + +// [lifetime extension occurs if the object was obtained by] +// -- a temporary materialization conversion +// CHECK-LABEL: ref_binding +void ref_binding() { + // CHECK: call {{.*}}ctor + auto &&x = ctor(); + // CHECK: call {{.*}}then + then(); + // CHECK: call {{.*}}dtor + // CHECK: } +} + +// -- ( expression ) +// CHECK-LABEL: parens +void parens() { + // CHECK: call {{.*}}ctor + auto &&x = ctor(); + // CHECK: call {{.*}}then + then(); + // CHECK: call {{.*}}dtor + // CHECK: } +} + +// -- subscripting of an array +// CHECK-LABEL: array_subscript_1 +void array_subscript_1() { + using T = dtor[1]; + // CHECK: call {{.*}}ctor + auto &&x = T{ctor()}[0]; + // CHECK: call {{.*}}then + then(); + // CHECK: call {{.*}}dtor + // CHECK: } +} +// CHECK-LABEL: array_subscript_2 +void array_subscript_2() { + using T = dtor[1]; + // CHECK: call {{.*}}ctor + auto &&x = ((dtor*)T{ctor()})[0]; + // CHECK: call {{.*}}dtor + // CHECK: call {{.*}}then + then(); + // CHECK: } +} + +struct with_member { dtor d; ~with_member(); }; +struct with_ref_member { dtor &&d; ~with_ref_member(); }; + +// -- a class member access using the . operator [...] +// CHECK-LABEL: member_access_1 +void member_access_1() { + // CHECK: call {{.*}}ctor + auto &&x = with_member{ctor()}.d; + // CHECK: call {{.*}}then + then(); + // CHECK: call {{.*}}with_member + // CHECK: } +} +// CHECK-LABEL: member_access_2 +void member_access_2() { + // CHECK: call {{.*}}ctor + auto &&x = with_ref_member{ctor()}.d; + // CHECK: call {{.*}}with_ref_member + // CHECK: call {{.*}}dtor + // CHECK: call {{.*}}then + then(); + // CHECK: } +} +// CHECK-LABEL: member_access_3 +void member_access_3() { + // CHECK: call {{.*}}ctor + auto &&x = (&(const with_member&)with_member{ctor()})->d; + // CHECK: call {{.*}}with_member + // CHECK: call {{.*}}then + then(); + // CHECK: } +} + +// -- a pointer-to-member operation using the .* operator [...] +// CHECK-LABEL: member_ptr_access_1 +void member_ptr_access_1() { + // CHECK: call {{.*}}ctor + auto &&x = with_member{ctor()}.*&with_member::d; + // CHECK: call {{.*}}then + then(); + // CHECK: call {{.*}}with_member + // CHECK: } +} +// CHECK-LABEL: member_ptr_access_2 +void member_ptr_access_2() { + // CHECK: call {{.*}}ctor + auto &&x = (&(const with_member&)with_member{ctor()})->*&with_member::d; + // CHECK: call {{.*}}with_member + // CHECK: call {{.*}}then + then(); + // CHECK: } +} + +// -- a [named] cast [...] +// CHECK-LABEL: static_cast +void test_static_cast() { + // CHECK: call {{.*}}ctor + auto &&x = static_cast(ctor()); + // CHECK: call {{.*}}then + then(); + // CHECK: call {{.*}}dtor + // CHECK: } +} +// CHECK-LABEL: const_cast +void test_const_cast() { + // CHECK: call {{.*}}ctor + auto &&x = const_cast(ctor()); + // CHECK: call {{.*}}then + then(); + // CHECK: call {{.*}}dtor + // CHECK: } +} +// CHECK-LABEL: reinterpret_cast +void test_reinterpret_cast() { + // CHECK: call {{.*}}ctor + auto &&x = reinterpret_cast(static_cast(ctor())); + // CHECK: call {{.*}}then + then(); + // CHECK: call {{.*}}dtor + // CHECK: } +} +// CHECK-LABEL: dynamic_cast +void test_dynamic_cast() { + // CHECK: call {{.*}}ctor + auto &&x = dynamic_cast(ctor()); + // CHECK: call {{.*}}then + then(); + // CHECK: call {{.*}}dtor + // CHECK: } +} + +// -- [explicit cast notation is defined in terms of the above] +// CHECK-LABEL: c_style_cast +void c_style_cast() { + // CHECK: call {{.*}}ctor + auto &&x = (dtor&&)ctor(); + // CHECK: call {{.*}}then + then(); + // CHECK: call {{.*}}dtor + // CHECK: } +} +// CHECK-LABEL: function_style_cast +void function_style_cast() { + // CHECK: call {{.*}}ctor + using R = dtor&&; + auto &&x = R(ctor()); + // CHECK: call {{.*}}then + then(); + // CHECK: call {{.*}}dtor + // CHECK: } +} + +// -- a conditional operator +// CHECK-LABEL: conditional +void conditional(bool b) { + // CHECK: call {{.*}}ctor + // CHECK: call {{.*}}ctor + auto &&x = b ? (dtor&&)ctor() : (dtor&&)ctor(); + // CHECK: call {{.*}}then + then(); + // CHECK: call {{.*}}dtor + // CHECK: call {{.*}}dtor + // CHECK: } +} + +// -- a comma expression +// CHECK-LABEL: comma +void comma() { + // CHECK: call {{.*}}ctor + auto &&x = (true, (dtor&&)ctor()); + // CHECK: call {{.*}}then + then(); + // CHECK: call {{.*}}dtor + // CHECK: } +}