File: rvalue-references.cpp

package info (click to toggle)
llvm-toolchain-17 1%3A17.0.6-22
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,799,624 kB
  • sloc: cpp: 6,428,607; ansic: 1,383,196; asm: 793,408; python: 223,504; objc: 75,364; f90: 60,502; lisp: 33,869; pascal: 15,282; sh: 9,684; perl: 7,453; ml: 4,937; awk: 3,523; makefile: 2,889; javascript: 2,149; xml: 888; fortran: 619; cs: 573
file content (109 lines) | stat: -rw-r--r-- 2,852 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// RUN: %clang_cc1 -std=c++11 -triple x86_64-apple-darwin10 -emit-llvm -o - %s | FileCheck %s


struct Spacer { int x; };
struct A { double array[2]; };
struct B : Spacer, A { };

B &getB();

// CHECK-LABEL: define{{.*}} nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) ptr @_Z4getAv()
// CHECK: call noundef nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) ptr @_Z4getBv()
// CHECK-NEXT: getelementptr inbounds i8, ptr
// CHECK-NEXT: ret ptr
A &&getA() { return static_cast<A&&>(getB()); }

int &getIntLValue();
int &&getIntXValue();
int getIntPRValue();

// CHECK-LABEL: define{{.*}} nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) ptr @_Z2f0v()
// CHECK: call noundef nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) ptr @_Z12getIntLValuev()
// CHECK-NEXT: ret ptr
int &&f0() { return static_cast<int&&>(getIntLValue()); }

// CHECK-LABEL: define{{.*}} nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) ptr @_Z2f1v()
// CHECK: call noundef nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) ptr @_Z12getIntXValuev()
// CHECK-NEXT: ret ptr
int &&f1() { return static_cast<int&&>(getIntXValue()); }

// CHECK-LABEL: define{{.*}} nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) ptr @_Z2f2v
// CHECK: call noundef i32 @_Z13getIntPRValuev()
// CHECK-NEXT: store i32 {{.*}}, ptr
// CHECK-NEXT: ret ptr
int &&f2() { return static_cast<int&&>(getIntPRValue()); }

bool ok;

class C
{
   int* state_;

   C(const C&) = delete;
   C& operator=(const C&) = delete;
public:
  C(int state) : state_(new int(state)) { }
  
  C(C&& a) {
    state_ = a.state_; 
    a.state_ = 0;
  }

  ~C() {
    delete state_; 
    state_ = 0;
  }
};

C test();

// CHECK-LABEL: define{{.*}} void @_Z15elide_copy_initv
void elide_copy_init() {
  ok = false;
  // CHECK: call void @_Z4testv
  C a = test();
  // CHECK-NEXT: call void @_ZN1CD1Ev
  // CHECK-NEXT: ret void
}

// CHECK-LABEL: define{{.*}} void @_Z16test_move_returnv
C test_move_return() {
  // CHECK: call void @_ZN1CC1Ei
  C a1(3);
  // CHECK: call void @_ZN1CC1Ei
  C a2(4);
  if (ok)
    // CHECK: call void @_ZN1CC1EOS_
    return a1;
  // CHECK: call void @_ZN1CC1EOS_
  return a2;
  // CHECK: call void @_ZN1CD1Ev
  // CHECK: call void @_ZN1CD1Ev
  //CHECK:  ret void
}

// PR10800: don't crash
namespace test1 {
  int &&move(int&);

  struct A { A(int); };
  struct B {
    A a;
    B(int i);
  };

  // CHECK-LABEL:    define{{.*}} void @_ZN5test11BC2Ei(
  // CHECK:      [[T0:%.*]] = call noundef nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) ptr @_ZN5test14moveERi(
  // CHECK-NEXT: [[T1:%.*]] = load i32, ptr [[T0]]
  // CHECK-NEXT: call void @_ZN5test11AC1Ei({{.*}}, i32 noundef [[T1]])
  // CHECK-NEXT: ret void
  B::B(int i) : a(move(i)) {}
}

// PR11009
struct MoveConvertible {
  operator int&& () const;
};
void moveConstruct() {
  (void)(int)MoveConvertible();
}