File: extract-capture-used-after-extraction.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 (214 lines) | stat: -rw-r--r-- 9,654 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

void takesInt(int x);

typedef struct { int width; int height; } Rectangle;

#ifdef USEINIT1
#define INIT1 r.width = 0; { int r, y; }
#else
#define INIT1 { int r, y; }
#endif

void extractCaptureUsedAfterSimple(int x) {
  Rectangle r;
  INIT1;
  int y = x * x;
#ifdef USEAFTER1
  takesInt(y);
#endif
#ifdef USEAFTER2
  takesInt(r.height);
#endif
#ifdef USEAFTER3
  r.width = 0;
#endif
#ifdef USEAFTER4
  y += 1;
#endif
}
// CHECK1: "static void extracted(int x) {\nRectangle r;\n  INIT1;\n  int y = x * x;\n}\n\n"
// CHECK1-NEXT: "extracted(x);"
// RUN: clang-refactor-test perform -action extract -selected=%s:13:3-15:16 %s | FileCheck --check-prefix=CHECK1 %s

// CHECK2: "static void extracted(Rectangle &r, int x, int &y) {\n\n  INIT1;\n  y = x * x;\n}\n\n"
// CHECK2-NEXT: "Rectangle r;\nint y;\nextracted(r, x, y);"
// RUN: clang-refactor-test perform -action extract -selected=%s:13:3-15:16 %s -DUSEINIT1 -DUSEAFTER1 -DUSEAFTER2 | FileCheck --check-prefix=CHECK2 %s
// RUN: clang-refactor-test perform -action extract -selected=%s:13:3-15:16 %s -DUSEINIT1 -DUSEAFTER3 -DUSEAFTER4 | FileCheck --check-prefix=CHECK2 %s
// RUN: clang-refactor-test perform -action extract -selected=%s:13:3-15:16 %s -DUSEINIT1 -DUSEAFTER1 -DUSEAFTER2 -DUSEAFTER3 -DUSEAFTER4 | FileCheck --check-prefix=CHECK2 %s

// CHECK3: "static void extracted(int x, int &y) {\n\n  INIT1;\n  y = x * x;\n}\n\n"
// CHECK3-NEXT: "Rectangle r;\nint y;\nextracted(x, y);"
// RUN: clang-refactor-test perform -action extract -selected=%s:13:3-15:16 %s -DUSEAFTER1 -DUSEAFTER2 | FileCheck --check-prefix=CHECK3 %s
// RUN: clang-refactor-test perform -action extract -selected=%s:13:3-15:16 %s -DUSEAFTER3 -DUSEAFTER4 | FileCheck --check-prefix=CHECK3 %s
// RUN: clang-refactor-test perform -action extract -selected=%s:13:3-15:16 %s -DUSEAFTER1 -DUSEAFTER2 -DUSEAFTER3 -DUSEAFTER4 | FileCheck --check-prefix=CHECK3 %s

// CHECK4: "static void extracted(int x, int &y) {\nRectangle r;\n  INIT1;\n  y = x * x;\n}\n\n"
// CHECK4-NEXT: "int y;\nextracted(x, y);"
// RUN: clang-refactor-test perform -action extract -selected=%s:13:3-15:16 %s -DUSEINIT1 -DUSEAFTER1 | FileCheck --check-prefix=CHECK4 %s

// CHECK5: "static void extracted(Rectangle &r, int x) {\n\n  INIT1;\n  int y = x * x;\n}\n\n"
// CHECK5-NEXT: "Rectangle r;\nextracted(r, x);"
// RUN: clang-refactor-test perform -action extract -selected=%s:13:3-15:16 %s -DUSEINIT1 -DUSEAFTER3 | FileCheck --check-prefix=CHECK5 %s

// CHECK6: "static void extracted(int x) {\n\n  INIT1;\n  int y = x * x;\n}\n\n"
// CHECK6-NEXT: "Rectangle r;\nextracted(x);"
// RUN: clang-refactor-test perform -action extract -selected=%s:13:3-15:16 %s -DUSEAFTER3 | FileCheck --check-prefix=CHECK6 %s

void extractCaptureAfterUseMultipleDecls() {
#ifdef MULTIPLE_DECL1
  int x = 1, y = 2, z = 3; { int x, y, z; };
#endif
#ifdef MULTIPLE_DECL2
  int x, y = 2, *z; { int y; }
#endif
#ifdef MULTIPLE_DECL3
  int x = 1, y, * z = 0, a, b = {0}; { int a; }
#endif
#ifdef USEX
  x;
#endif
#ifdef USEY
  y;
#endif
#ifdef USEZ
  z;
#endif
#ifdef USEA
  a;
#endif
#ifdef USEB
  b;
#endif
}

// CHECK7: "static void extracted() {\nint x = 1, y = 2, z = 3;\n}\n\n"
// CHECK7-NEXT; "extracted();"
// RUN: clang-refactor-test perform -action extract -selected=%s:59:3-59:26 %s -DMULTIPLE_DECL1 | FileCheck --check-prefix=CHECK7 %s
// CHECK8: "static void extracted(int &x, int &y, int &z) {\nx = 1; y = 2; z = 3;\n}\n\n"
// CHECK8-NEXT: "int x;\nint y;\nint z;\nextracted(x, y, z);"
// RUN: clang-refactor-test perform -action extract -selected=%s:59:3-59:26 %s -DMULTIPLE_DECL1 -DUSEX -DUSEY -DUSEZ | FileCheck --check-prefix=CHECK8 %s
// CHECK9: "static void extracted(int &x) {\nx = 1; int y = 2; int z = 3; { int x, y, z; }\n}\n\n"
// CHECK9-NEXT: "int x;\nextracted(x);"
// RUN: clang-refactor-test perform -action extract -selected=%s:59:3-59:44 %s -DMULTIPLE_DECL1 -DUSEX | FileCheck --check-prefix=CHECK9 %s
// CHECK10: "static void extracted(int &y) {\nint x = 1; y = 2; int z = 3; { int x, y, z; }\n}\n\n"
// CHECK10-NEXT: "int y;\nextracted(y);"
// RUN: clang-refactor-test perform -action extract -selected=%s:59:3-59:44 %s -DMULTIPLE_DECL1 -DUSEY | FileCheck --check-prefix=CHECK10 %s
// CHECK11: "static void extracted(int &z) {\nint x = 1; int y = 2; z = 3; { int x, y, z; }\n}\n\n"
// CHECK11-NEXT: "int z;\nextracted(z);"
// RUN: clang-refactor-test perform -action extract -selected=%s:59:3-59:44 %s -DMULTIPLE_DECL1 -DUSEZ | FileCheck --check-prefix=CHECK11 %s

// CHECK12: "static void extracted() {\nint x, y = 2, *z;\n}\n\n"
// CHECK12-NEXT: "extracted();"
// RUN: clang-refactor-test perform -action extract -selected=%s:62:3-62:19 %s -DMULTIPLE_DECL2 | FileCheck --check-prefix=CHECK12 %s
// CHECK13: "static void extracted(int &y) {\ny = 2;\n}\n\n"
// CHECK13-NEXT: "int x;\nint y;\nint * z;\nextracted(y);"
// RUN: clang-refactor-test perform -action extract -selected=%s:62:3-62:19 %s -DMULTIPLE_DECL2 -DUSEX -DUSEY -DUSEZ | FileCheck --check-prefix=CHECK13 %s
// CHECK14: "static void extracted(int &y) {\nint x; y = 2; int * z; { int y; }\n}\n\n"
// CHECK14-NEXT: "int y;\nextracted(y);"
// RUN: clang-refactor-test perform -action extract -selected=%s:62:3-62:31 %s -DMULTIPLE_DECL2 -DUSEY | FileCheck --check-prefix=CHECK14 %s

// CHECK15: "static void extracted() {\nint x = 1, y, * z = 0, a, b = {0};\n}\n\n"
// CHECK15-NEXT: "extracted();"
// RUN: clang-refactor-test perform -action extract -selected=%s:65:3-65:36 %s -DMULTIPLE_DECL3 | FileCheck --check-prefix=CHECK15 %s
// CHECK16: "static void extracted(int &x, int *&z) {\nx = 1; z = 0; int a; int b = {0};\n}\n\n"
// CHECK16-NEXT: "int x;\nint y;\nint * z;\nextracted(x, z);"
// RUN: clang-refactor-test perform -action extract -selected=%s:65:3-65:36 %s -DMULTIPLE_DECL3 -DUSEX -DUSEY -DUSEZ | FileCheck --check-prefix=CHECK16 %s
// CHECK17: "static void extracted(int &b, int &x, int *&z) {\nx = 1; z = 0; b = {0};\n}\n\n"
// CHECK17-NEXT: "int x;\nint y;\nint * z;\nint a;\nint b;\nextracted(b, x, z);"
// RUN: clang-refactor-test perform -action extract -selected=%s:65:3-65:36 %s -DMULTIPLE_DECL3 -DUSEX -DUSEY -DUSEZ -DUSEA -DUSEB | FileCheck --check-prefix=CHECK17 %s
// CHECK18: "static void extracted() {\nint x = 1; int y; int * z = 0; int b = {0}; { int a; }\n}\n\n"
// CHECK18-NEXT: "int a;\nextracted();"
// RUN: clang-refactor-test perform -action extract -selected=%s:65:3-65:48 %s -DMULTIPLE_DECL3 -DUSEA | FileCheck --check-prefix=CHECK18 %s

#define ONE 1

void preserveInitExpressionText() {
  int a = ONE;
  int x = ONE, y = ONE;
  a, y;
}

// CHECK19: "static void extracted(int &a, int &y) {\na = ONE;\n  int x = ONE; y = ONE;\n}\n\n
// CHECK19-NEXT: "int a;\nint y;\nextracted(a, y);"
// RUN: clang-refactor-test perform -action extract -selected=%s:126:3-127:23 %s | FileCheck --check-prefix=CHECK19 %s

class Construct {
public:
  Construct();
  Construct(int x, int y);
};

void handleConstruct() {
  Construct a(1, 2);
  Construct b(3, 4), c(5, 6);
  a, c;
}
// CHECK20: "static void extracted(Construct &a, Construct &c) {\na = Construct(1, 2);\n  Construct b(3, 4); c = Construct(5, 6);\n}\n\n"
// CHECK20-NEXT: "Construct a;\nConstruct c;\nextracted(a, c);"
// RUN: clang-refactor-test perform -action extract -selected=%s:142:3-143:29 %s | FileCheck --check-prefix=CHECK20 %s

struct Construct2 {
  int x, y;
};

void handleConstruct2() {
  Construct2 a = {1, 2};
  Construct2 b = {3, 4}, c = {5, 6};
  a, c;
}
// CHECK21: "static void extracted(Construct2 &a, Construct2 &c) {\na = {1, 2};\n  Construct2 b = {3, 4}; c = {5, 6};\n}\n\n"
// CHECK21-NEXT: "Construct2 a;\nConstruct2 c;\nextracted(a, c);"
// RUN: clang-refactor-test perform -action extract -selected=%s:155:3-156:29 %s -std=c++11 | FileCheck --check-prefix=CHECK21 %s

class Construct3 {
public:
  Construct3();
  Construct3(int x);
};

void handleConstruct3() {
  Construct3 a = 1;
  Construct3 b = 2, c = 3 + 3;
  a, c;
}
// CHECK22: "static void extracted(Construct3 &a, Construct3 &c) {\na = 1;\n  Construct3 b = 2; c = 3 + 3;\n}\n\n"
// CHECK22-NEXT: "Construct3 a;\nConstruct3 c;\nextracted(a, c);"
// RUN: clang-refactor-test perform -action extract -selected=%s:170:3-171:26 %s -std=c++11 | FileCheck --check-prefix=CHECK22 %s

void handleConstruct2InitList() {
  Construct2 a { 5, 6 };
  Construct2 b { 1, 2 }, c { 3, 4 };
  a, c;
}
// CHECK23: "static void extracted(Construct2 &a, Construct2 &c) {\na = { 5, 6 };\n  Construct2 b = { 1, 2 }; c = { 3, 4 };\n}\n\n"
// CHECK23-NEXT: "Construct2 a;\nConstruct2 c;\nextracted(a, c);"
// RUN: clang-refactor-test perform -action extract -selected=%s:179:3-180:36 %s -std=c++11 | FileCheck --check-prefix=CHECK23 %s

void onlyOneUsedAfterExtractionIsReturned() {
  const int x = 0;
  x;
  Construct a(1, 2);
  a;
}
// CHECK24: "static int extracted() {\nconst int x = 0;\nreturn x;\n}\n\n"
// CHECK24-NEXT: "const int x = extracted();"
// CHECK24: "static Construct extracted() {\nConstruct a(1, 2);\nreturn a;\n}\n\n"
// CHECK24-NEXT: "Construct a = extracted();"

int avoidReturningWhenReturnUsed() {
  int x = 0;
  if (x != 0) { return 22; }
  x;
}
// CHECK24: "static int extracted(int &x) {\nx = 0;\n  if (x != 0) { return 22; }\n}\n\n"
// CHECK24-NEXT: "int x;\nextracted(x);"

void returnOnlyWhenReturnReturnsNothing() {
  int x = 0;
  if (x != 0) { return; }
  x;
}
// CHECK24: "static int extracted() {\nint x = 0;\n  if (x != 0) { return x; }\nreturn x;\n}\n\n"
// CHECK24-NEXT: "int x = extracted();"

// RUN: clang-refactor-test perform -action extract -selected=%s:188:3-188:18 -selected=%s:190:3-190:20 -selected=%s:199:3-200:29 -selected=%s:207:3-208:26 %s -std=c++11 | FileCheck --check-prefix=CHECK24 %s