File: general-ref-tests-original.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (248 lines) | stat: -rw-r--r-- 6,230 bytes parent folder | download | duplicates (9)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <iostream>
#include <sstream>
#include <string>
#include <vector>

struct MySubStruct {
  MySubStruct(const int& num);
  int member;
  virtual int get_member() const { return member; }
  virtual ~MySubStruct() = default;

  struct inner_s {
    inner_s(const inner_s&);
    // Expected rewrite: const raw_ref<int> inner_int;
    int& inner_int;
  };
};

// This was added due to a bug in the matcher that was doing erroneous rewrites
// here. No rewrite expected.
MySubStruct::inner_s::inner_s(const inner_s&) = default;

struct MyStruct {
  MyStruct(MySubStruct& s) : ref(s) {}

  // No rewrite expected for `other.ref` since `ref` itself with become a
  // raw_ref.
  MyStruct(const MyStruct& other) : ref(other.ref) {}

  const MySubStruct& get() const;
  // Expected rewrite: const raw_ref<MySubStruct> ref;
  MySubStruct& ref;
};

template <class T>
struct MyTemplatedStruct {
  MyTemplatedStruct(T& t) : ref(t) {}

  // No rewrite expected for `other.ref` since `ref` itself with become a
  // raw_ref.
  MyTemplatedStruct(const MyTemplatedStruct& other) : ref(other.ref) {}

  // Expected rewrite: ref->member
  void setSubmember(int n) { ref.member = n; }

  // Expected rewrite: return *ref;
  T& get() { return ref; }

  const T& get() const;
  // Expected rewrite: return *ref;
  T get_by_value() const { return ref; }

  // Expected rewrite: ref->get_member();
  int getInt() { return ref.get_member(); }

  // Expected rewrite: const raw_ref<T> ref;
  T& ref;
};

template <class T>
void func(T& ref, const T& ref2) {}

int main() {
  MySubStruct sub{1};
  MyStruct s(sub);
  // Expected rewrite: s.ref->member;
  s.ref.member = 11;
  // Expected rewrite: s.ref->get_member();
  s.ref.get_member();

  // Expected rewrite: MyStruct* s2 = new MyStruct(*s.ref);
  MyStruct* s2 = new MyStruct(s.ref);

  MyTemplatedStruct<MySubStruct> my_template_inst(sub);
  my_template_inst.setSubmember(1234);

  // Expected rewrites:
  // func(*my_template_inst.ref, my_template_inst.get());
  func(my_template_inst.ref, my_template_inst.get());
  // func(my_template_inst.get(), *my_template_inst.ref);
  func(my_template_inst.get(), my_template_inst.ref);
  // func(*my_template_inst.ref, *my_template_inst.ref);
  func(my_template_inst.ref, my_template_inst.ref);

  // Expected rewrite: MySubStruct* ptr = &*s.ref;
  MySubStruct* ptr = &s.ref;

  // Expected rewrite:
  //  auto &ref = *s.ref, &ref2 = *s.ref;
  auto &ref = s.ref, &ref2 = s.ref;

  int a = 0;
  int b = 0;

  struct {
    // Expected rewrite: const raw_ref<int> in;
    int& in;
    // Expected rewrite: const raw_ref<int> out;
    int& out;
    // Expected rewrite:
    // report_lists[]{{raw_ref(a), raw_ref(b)}, {raw_ref(a), raw_ref(b)}};
  } report_lists[]{{a, b}, {a, b}};

  // Reference members of nested anonymous structs declared in a
  // function body are rewritten because they can end up on the heap.
  struct A {
    struct {
      int& i;
    } member;
  };

  // Expected rewrite:  A obj{raw_ref(a)};
  A obj{a};
  obj.member.i++;

  // Expected rewrite:  A obj{.member = {raw_ref(a)}};
  A obj2{.member = {a}};
  obj2.member.i++;
  ++obj2.member.i;
  obj2.member.i--;
  --obj2.member.i;

  // No rewrite expected here for obj2.member.i since A::member::i itself will
  // be rewritten into raw_ref.
  A obj3{obj2.member.i};
  obj3.member.i++;

  static struct {
    // Expected rewrite: const raw_ref<int> member;
    int& member;
    // Expected rewrite: st{raw_ref(a)};
  } st{a};

  struct Temp {
    Temp(int& ref) : member(ref) {}
    // Expected rewrite: const raw_ref<int> member;
    int& member;
  };
  // No need to add raw_ref() around `a` here because the constructor will be
  // called.
  Temp tmp{a};
  tmp.member++;

  struct StringRefObj {
    const std::string& member;
  };

  // No need to add raw_ref() around "abcde" here because it results in a
  // temporary string object. A manual fix need to be applied.
  StringRefObj r{"abcde"};
  (void)r;
}

struct B {
  struct {
    // Expected rewrite: const raw_ref<int> i;
    int& i;
  } member;
};

template <typename T>
const T& MyTemplatedStruct<T>::get() const {
  // Expected rewrite: return *ref;
  return ref;
}

const MySubStruct& MyStruct::get() const {
  // Expected rewrite: return *ref;
  return ref;
}

struct key_compare {
  bool operator()(const int& a, const int& b) const { return a < b; }
};

struct KeyValueCompare {
  // The key comparison object must outlive this class.
  explicit KeyValueCompare(const key_compare& comp) : comp_(comp) {}

  bool operator()(const int& lhs, const int& rhs) const {
    // Expected rewrite: return (*comp_)(lhs, rhs);
    return comp_(lhs, rhs);
  }

  template <typename T, typename U>
  bool operator()(const T& lhs, const U& rhs) const {
    // Expected rewrite: return (*comp_)(lhs, rhs);
    return comp_(lhs, rhs);
  }

 private:
  const key_compare& comp_;
};

struct KeyValueCompare2 {
  // The key comparison object must outlive this class.
  explicit KeyValueCompare2(const key_compare& comp) : comp_(comp) {}

  template <typename T, typename U>
  bool operator()(const T& lhs, const U& rhs) const {
    // Expected rewrite: return (*comp_)(lhs, rhs);
    return comp_(lhs, rhs);
  }

 private:
  const key_compare& comp_;
};

template <typename T>
void doSomething(T& t) {
  (void)t;
}

template <typename T>
struct VectorMemberRef {
  void iterate() {
    for (T& t : v) {
      doSomething(t);
    }
  }

  T get_first() { return v[0]; }

  std::vector<T>& v;
};

struct MyStruct2 {
  MyStruct2(const MyStruct2& other)
      :  // No rewrite expected for `other.int_ref` since `int_ref` itself with
         // become a raw_ref.
        int_ref(other.int_ref),
        // Expected rewrite: i(*other.int_ref)
        i(other.int_ref),
        func_ref(other.func_ref),
        ref_to_array_of_ints(other.ref_to_array_of_ints) {}

  // Expected rewrite: const raw_ref<int> int_ref;
  int& int_ref;
  int i;
  // Function references are not rewritten.
  int (&func_ref)();
  // References to arrays are not rewritten.
  int (&ref_to_array_of_ints)[123];
};