File: extract-reference-of-captured-variable.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (287 lines) | stat: -rw-r--r-- 12,622 bytes parent folder | download
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287

void takesPtr(int *x) { }

typedef struct {
  int width, height;
} Rectangle;

void takesStructPtr(Rectangle *sp) { }

void variableTakesRef(int x, Rectangle r) {
  int &y = x;
  takesPtr(&y);
// CHECK1: (int &x) {\nint &y = x;\n  takesPtr(&y);\n}
  Rectangle p = r;
  Rectangle &rp = p;
  takesStructPtr(&rp);
// CHECK1: (const Rectangle &r) {\nRectangle p = r;\n  Rectangle &rp = p;\n  takesStructPtr(&rp);\n}
// CHECK1: (Rectangle &p) {\nRectangle &rp = p;\n  takesStructPtr(&rp);\n}
  int &member = ((r).width);
  int z = member;
// CHECK1: (Rectangle &r) {\nint &member = ((r).width);\n  int z = member;\n}

// Even though y takes a reference to x, we still want to pass it by value here.
  int a = x;
// CHECK1: (int x) {\nint a = x;\n}
}

// RUN: clang-refactor-test perform -action extract -selected=%s:11:3-12:15 -selected=%s:14:3-16:22 -selected=%s:15:3-16:22 -selected=%s:19:3-20:17 -selected=%s:24:3-24:12 %s | FileCheck --check-prefix=CHECK1 %s

class PrivateInstanceVariables {
  int x;
  Rectangle r;

  void method() {
    int &y = x;
// CHECK2: extracted(int &x) {\nint &y = x;\n}
    Rectangle &rr = r;
// CHECK2: extracted(Rectangle &r) {\nRectangle &rr = r;\n}
    int &z = ((r).width);
// CHECK2: extracted(Rectangle &r) {\nint &z = ((r).width);\n}
  }
};

// RUN: clang-refactor-test perform -action extract -selected=%s:35:5-35:15 -selected=%s:37:5-37:22 -selected=%s:39:5-39:25 %s | FileCheck --check-prefix=CHECK2 %s

#ifdef USECONST
#define CONST const
#else
#define CONST
#endif

void takesRef(CONST int &x) { }

void takesStructRef(CONST Rectangle &r) { }

void takesValue(int x) { }

struct ConsTakesRef {
  ConsTakesRef(CONST int &x) { }

  void takesRef(CONST int &x) const { }
  void takesValue(int x) const { }
};

int operator << (CONST Rectangle &r, CONST int &x) { return 0; }

void callTakesRef(int x, Rectangle r) {
  takesRef(x);
// CHECK3: extracted(int &x) {\ntakesRef(x);\n}
// CHECK4: extracted(int x) {\ntakesRef(x);\n}
  takesValue(x);
// CHECK3: extracted(int x) {\ntakesValue(x);\n}
// CHECK4: extracted(int x) {\ntakesValue(x);\n}
  auto k = ConsTakesRef(x); auto y = ConsTakesRef(x);
// CHECK3: extracted(int &x) {\nauto k = ConsTakesRef(x);\n}
// CHECK4: extracted(int x) {\nauto k = ConsTakesRef(x);\n}
  y.takesRef((x));
// CHECK3: extracted(int &x, const ConsTakesRef &y) {\ny.takesRef((x));\n}
// CHECK4: extracted(int x, const ConsTakesRef &y) {\ny.takesRef((x));\n}
  y.takesValue(x);
// CHECK3: extracted(int x, const ConsTakesRef &y) {\ny.takesValue(x);\n}
// CHECK4: extracted(int x, const ConsTakesRef &y) {\ny.takesValue(x);\n}
  takesStructRef((r));
// CHECK3: extracted(Rectangle &r) {\ntakesStructRef((r));\n}
// CHECK4: extracted(const Rectangle &r) {\ntakesStructRef((r));\n}
  takesRef((r).height);
// CHECK3: extracted(Rectangle &r) {\ntakesRef((r).height);\n}
// CHECK4: extracted(const Rectangle &r) {\ntakesRef((r).height);\n}
  y.takesRef(r.width);
// CHECK3: extracted(Rectangle &r, const ConsTakesRef &y) {\ny.takesRef(r.width);\n}
// CHECK4: extracted(const Rectangle &r, const ConsTakesRef &y) {\ny.takesRef(r.width);\n}
  takesValue(r.width);
// CHECK3: extracted(const Rectangle &r) {\ntakesValue(r.width);\n}
// CHECK4: extracted(const Rectangle &r) {\ntakesValue(r.width);\n}
  r << x;
// CHECK3: extracted(Rectangle &r, int &x) {\nreturn r << x;\n}
// CHECK4: extracted(const Rectangle &r, int x) {\nreturn r << x;\n}

  int &r1 = x;
  takesRef(x);
// CHECK3: extracted(int &x) {\nint &r1 = x;\n  takesRef(x);\n}
// CHECK4: extracted(int &x) {\nint &r1 = x;\n  takesRef(x);\n}
}

// RUN: clang-refactor-test perform -action extract -selected=%s:68:3-68:14 -selected=%s:71:3-71:16 -selected=%s:74:3-74:27 -selected=%s:77:3-77:18 -selected=%s:80:3-80:18 -selected=%s:83:3-83:22 -selected=%s:86:3-86:23 -selected=%s:89:3-89:22 -selected=%s:92:3-92:22 -selected=%s:95:3-95:9 -selected=%s:99:3-100:14 %s | FileCheck --check-prefix=CHECK3 %s
// RUN: clang-refactor-test perform -action extract -selected=%s:68:3-68:14 -selected=%s:71:3-71:16 -selected=%s:74:3-74:27 -selected=%s:77:3-77:18 -selected=%s:80:3-80:18 -selected=%s:83:3-83:22 -selected=%s:86:3-86:23 -selected=%s:89:3-89:22 -selected=%s:92:3-92:22 -selected=%s:95:3-95:9 -selected=%s:99:3-100:14 %s -DUSECONST | FileCheck --check-prefix=CHECK4 %s

void takesConstRef(const int &x) { }

void callTakeRefAndConstRef(int x) {
  takesRef(x);
  takesConstRef(x);
// CHECK5: extracted(int &x) {\ntakesRef(x);\n  takesConstRef(x);\n}
}

// RUN: clang-refactor-test perform -action extract -selected=%s:111:3-112:19 %s | FileCheck --check-prefix=CHECK5 %s

class PrivateInstanceVariablesCallRefs {
  int x;
  Rectangle r;

  void callsTakeRef() {
    takesRef(x);
// CHECK6: extracted(int &x) {\ntakesRef(x);\n}
// CHECK7: extracted(int x) {\ntakesRef(x);\n}
    takesStructRef(r);
// CHECK6: extracted(Rectangle &r) {\ntakesStructRef(r);\n}
// CHECK7: extracted(const Rectangle &r) {\ntakesStructRef(r);\n}
    takesRef(r.width);
// CHECK6: extracted(Rectangle &r) {\ntakesRef(r.width);\n}
// CHECK7: extracted(const Rectangle &r) {\ntakesRef(r.width);\n}
  }
};

// RUN: clang-refactor-test perform -action extract -selected=%s:123:5-123:16 -selected=%s:126:5-126:22 -selected=%s:129:5-129:22 %s | FileCheck --check-prefix=CHECK6 %s
// RUN: clang-refactor-test perform -action extract -selected=%s:123:5-123:16 -selected=%s:126:5-126:22 -selected=%s:129:5-129:22 %s -DUSECONST | FileCheck --check-prefix=CHECK7 %s

void variableTakesConstRef(int x, Rectangle r) {
  const int &y = x;
// CHECK8: extracted(int x) {\nconst int &y = x;\n}
  const Rectangle &p = r;
// CHECK8: extracted(const Rectangle &r) {\nconst Rectangle &p = r;\n}
  const int &z = r.width;
// CHECK8: extracted(const Rectangle &r) {\nconst int &z = r.width;\n}
}

// RUN: clang-refactor-test perform -action extract -selected=%s:139:3-139:19 -selected=%s:141:3-141:25 -selected=%s:143:3-143:25 %s | FileCheck --check-prefix=CHECK8 %s

class ClassWithMethod {
public:
  int method() CONST { return 0; }
  int operator + (int x) CONST { return x; }
};

void nonConstMethodCallImpliesNonConstReceiver(ClassWithMethod x) {
  x.method();
// CHECK10: extracted(ClassWithMethod &x) {\nreturn x.method();\n}
// CHECK11: extracted(const ClassWithMethod &x) {\nreturn x.method();\n}
  x.operator +(2);
// CHECK10: extracted(ClassWithMethod &x) {\nreturn x.operator +(2);\n}
// CHECK11: extracted(const ClassWithMethod &x) {\nreturn x.operator +(2);\n}
}

// RUN: clang-refactor-test perform -action extract -selected=%s:156:3-156:14 -selected=%s:159:3-159:18 %s | FileCheck --check-prefix=CHECK10 %s
// RUN: clang-refactor-test perform -action extract -selected=%s:156:3-156:14 -selected=%s:159:3-159:18 %s -DUSECONST | FileCheck --check-prefix=CHECK11 %s

void ignoreMethodCallsOnPointer(ClassWithMethod *x) {
  x->method();
// CHECK12: extracted(ClassWithMethod *x) {\nreturn x->method();\n}
  x->operator +(2);
// CHECK12: extracted(ClassWithMethod *x) {\nreturn x->operator +(2);\n}
}

// RUN: clang-refactor-test perform -action extract -selected=%s:168:3-168:13 -selected=%s:170:3-170:19 %s | FileCheck --check-prefix=CHECK12 %s

void takesRValueRef(int &&x) { }
void takesRValueStructRef(Rectangle &&r) { }

void callTakesRValueRef(int x) {
  takesRValueRef(static_cast<int&&>(x));
// CHECK13: extracted(int &x) {\ntakesRValueRef(static_cast<int&&>(x));\n}
  Rectangle r;
  takesRValueStructRef((static_cast<Rectangle&&>(r)));
// CHECK13: extracted(Rectangle &r) {\ntakesRValueStructRef((static_cast<Rectangle&&>(r)));\n}
  int &&y = static_cast<int&&>(r.height);
// CHECK13: extracted(Rectangle &r) {\nint &&y = static_cast<int&&>(r.height);\n}
}

// RUN: clang-refactor-test perform -action extract -selected=%s:180:3-180:20 -selected=%s:183:3-183:54 -selected=%s:185:3-185:41 %s | FileCheck --check-prefix=CHECK13 %s

void referencesInConditionalOperator(int x, int y) {
  takesRef(x == 0 ? x : y);
// CHECK14: (int &x, int &y) {\ntakesRef(x == 0 ? x : y);\n}
// CHECK15: (int x, int y) {\ntakesRef(x == 0 ? x : y);\n}
  Rectangle a, b;
  takesStructRef(y == 0 ? (a) : b);
// CHECK14: (Rectangle &a, Rectangle &b, int y) {\ntakesStructRef(y == 0 ? (a) : b);\n}
// CHECK15: (const Rectangle &a, const Rectangle &b, int y) {\ntakesStructRef(y == 0 ? (a) : b);\n}
  takesRef(x == 0 ? (a).width : (y == 0 ? y : b.height));
// CHECK14: (Rectangle &a, Rectangle &b, int x, int &y) {\ntakesRef(x == 0 ? (a).width : (y == 0 ? y : b.height));\n}
// CHECK15: (const Rectangle &a, const Rectangle &b, int x, int y) {\ntakesRef(x == 0 ? (a).width : (y == 0 ? y : b.height));\n}
  takesRef((x == 0 ? a : (b)).width);
// CHECK14: (Rectangle &a, Rectangle &b, int x) {\ntakesRef((x == 0 ? a : (b)).width);\n}
// CHECK15: (const Rectangle &a, const Rectangle &b, int x) {\ntakesRef((x == 0 ? a : (b)).width);\n}
  takesRef(x == 0 ? y : y);
// CHECK14: (int x, int &y) {\ntakesRef(x == 0 ? y : y);\n}
// CHECK15: (int x, int y) {\ntakesRef(x == 0 ? y : y);\n}
  ClassWithMethod caller1, caller2;
  (x == 0 ? caller1 : caller2).method();
// CHECK14: (ClassWithMethod &caller1, ClassWithMethod &caller2, int x) {\nreturn (x == 0 ? caller1 : caller2).method();\n}
// CHECK15: (const ClassWithMethod &caller1, const ClassWithMethod &caller2, int x) {\nreturn (x == 0 ? caller1 : caller2).method();\n}
}

// RUN: clang-refactor-test perform -action extract -selected=%s:192:3-192:27 -selected=%s:196:3-196:35 -selected=%s:199:3-199:57 -selected=%s:202:3-202:37 -selected=%s:205:3-205:27 -selected=%s:209:3-209:40 %s | FileCheck --check-prefix=CHECK14 %s
// RUN: clang-refactor-test perform -action extract -selected=%s:192:3-192:27 -selected=%s:196:3-196:35 -selected=%s:199:3-199:57 -selected=%s:202:3-202:37 -selected=%s:205:3-205:27 -selected=%s:209:3-209:40 %s -DUSECONST | FileCheck --check-prefix=CHECK15 %s

class PrivateInstanceVariablesConditionalOperatorRefs {
  int x;
  Rectangle r;

  void callsTakeRef(int y) {
    takesRef(y == 0 ? x : r.width);
  }
};
// CHECK16: (Rectangle &r, int &x, int y) {\ntakesRef(y == 0 ? x : r.width);\n}
// RUN: clang-refactor-test perform -action extract -selected=%s:222:5-222:35 %s | FileCheck --check-prefix=CHECK16 %s

class ReferencesInCommaOperator {
  int x;

  void callsTakeRef(int y, Rectangle r) {
    takesRef((x, y));
// CHECK17: (int x, int &y) {\ntakesRef((x, y));\n}
    takesRef((y, x));
// CHECK17: (int &x, int y) {\ntakesRef((y, x));\n}
    takesStructRef((takesValue(x), r));
// CHECK17: (Rectangle &r, int x) {\ntakesStructRef((takesValue(x), r));\n}
  }
};

// RUN: clang-refactor-test perform -action extract -selected=%s:232:5-232:21 -selected=%s:234:5-234:21 -selected=%s:236:5-236:39 %s | FileCheck --check-prefix=CHECK17 %s

struct StaticMember {
  static int staticMember;
};

void memberMustBeNonStaticField(StaticMember s) {
  takesRef(s.staticMember);
// CHECK18: (const StaticMember &s) {\ntakesRef(s.staticMember);\n}
}
// RUN: clang-refactor-test perform -action extract -selected=%s:248:3-248:27 %s | FileCheck --check-prefix=CHECK18 %s

class ClassWithMethod2 {
public:
  ClassWithMethod member;
};

class ClassWithMethod;

void nonConstMethodCallImpliesNonConstReceiver2(ClassWithMethod2 x) {
  x.member.method();
// CHECK19: (ClassWithMethod2 &x) {\nreturn x.member.method();\n}
// CHECK20: (const ClassWithMethod2 &x) {\nreturn x.member.method();\n}
}
// RUN: clang-refactor-test perform -action extract -selected=%s:261:3-261:20 %s | FileCheck --check-prefix=CHECK19 %s
// RUN: clang-refactor-test perform -action extract -selected=%s:261:3-261:20 %s -DUSECONST | FileCheck --check-prefix=CHECK20 %s

class PrivateInstaceVariablesCallRefsBase {
  int x;
};

class PrivateInstanceVariablesCallRefs2: public PrivateInstaceVariablesCallRefsBase {
  int y;
  Rectangle r;

  void callsTakeRef() {
    takesRef(this->x);
    takesRef((this)->y);
    takesRef(static_cast<PrivateInstaceVariablesCallRefsBase *>(this)->x);
    takesRef((0, ((const_cast<PrivateInstanceVariablesCallRefs2 *>(this)->r.width))));
  }
// CHECK21: (PrivateInstanceVariablesCallRefs2 &object) {\ntakesRef(object.x);\n}
// CHECK21: (PrivateInstanceVariablesCallRefs2 &object) {\ntakesRef((object).y);\n}
// CHECK21: (PrivateInstanceVariablesCallRefs2 &object) {\ntakesRef(static_cast<PrivateInstaceVariablesCallRefsBase *>(&object)->x);\n}
// CHECK21: (PrivateInstanceVariablesCallRefs2 &object) {\ntakesRef((0, ((const_cast<PrivateInstanceVariablesCallRefs2 *>(&object)->r.width))));\n}
};
// RUN: clang-refactor-test perform -action extract -selected=%s:277:5-277:22 -selected=%s:278:5-278:24 -selected=%s:279:5-279:74 -selected=%s:280:5-280:86 %s | FileCheck --check-prefix=CHECK21 %s