File: extract-address-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 (194 lines) | stat: -rw-r--r-- 7,775 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

void takesVoidPtr(void *x) { }
void takesPtr(int *x) { }
void takesPtrPtr(int **x) { }

void addressOfVariableImpliesPossibleMutation(int x, int *ip) {
  takesPtr(&x);
// CHECK1-C: (int *x) {\ntakesPtr(x);\n}
// CHECK1-CPP: (int &x) {\ntakesPtr(&x);\n}
  takesVoidPtr(&x);
// CHECK1-C: (int *x) {\ntakesVoidPtr(x);\n}
// CHECK1-CPP: (int &x) {\ntakesVoidPtr(&x);\n}
  &x;
// CHECK1-C: (int *x) {\nreturn x;\n}
// CHECK1-CPP: (int &x) {\nreturn &x;\n}
  *(&x) = 0;
// CHECK1-C: extracted(int *x) {\n*(x) = 0;\n}
// CHECK1-CPP: extracted(int &x) {\n*(&x) = 0;\n}
  takesPtrPtr(&ip);
// CHECK1-C: (int **ip) {\ntakesPtrPtr(ip);\n}
// CHECK1-CPP: (int *&ip) {\ntakesPtrPtr(&ip);\n}
  takesPtr(ip);
// CHECK1-C: (int *ip) {\ntakesPtr(ip);\n}
// CHECK1-CPP: (int *ip) {\ntakesPtr(ip);\n}
  takesVoidPtr(ip);
// CHECK1-C: (int *ip) {\ntakesVoidPtr(ip);\n}
// CHECK1-CPP: (int *ip) {\ntakesVoidPtr(ip);\n}
  takesPtr(&((x)));
// CHECK1-C: (int *x) {\ntakesPtr(((x)));\n}
// CHECK1-CPP: (int &x) {\ntakesPtr(&((x)));\n}
}

// RUN: clang-refactor-test perform -action extract -selected=%s:7:3-7:15 -selected=%s:10:3-10:19 -selected=%s:13:3-13:5 -selected=%s:16:3-16:12 -selected=%s:19:3-19:19 -selected=%s:22:3-22:15 -selected=%s:25:3-25:19 -selected=%s:28:3-28:19 %s -x c | FileCheck --check-prefix=CHECK1-C %s
// RUN: clang-refactor-test perform -action extract -selected=%s:7:3-7:15 -selected=%s:10:3-10:19 -selected=%s:13:3-13:5 -selected=%s:16:3-16:12 -selected=%s:19:3-19:19 -selected=%s:22:3-22:15 -selected=%s:25:3-25:19 -selected=%s:28:3-28:19 %s | FileCheck --check-prefix=CHECK1-CPP %s

typedef struct {
  int width, height;
} Rectangle;

void takesStructPtr(Rectangle *sp) { }

#ifdef __cplusplus

void addressOfRef(int &x, int *&ip, Rectangle &r) {
  takesPtr(&x);
// CHECK2: (int &x) {\ntakesPtr(&x);\n}
  takesPtrPtr(&ip);
// CHECK2: (int *&ip) {\ntakesPtrPtr(&ip);\n}
  takesStructPtr(&r);
// CHECK2: (Rectangle &r) {\ntakesStructPtr(&r);\n}
}

// RUN: clang-refactor-test perform -action extract -selected=%s:45:3-45:15 -selected=%s:47:3-47:19 -selected=%s:49:3-49:21  %s | FileCheck --check-prefix=CHECK2 %s

#endif

void addressOfArray(int x[]) {
  takesPtrPtr(&x);
// CHECK3-C: (int **x) {\ntakesPtrPtr(x);\n}
// CHECK3-CPP: (int *&x) {\ntakesPtrPtr(&x);\n}
}

// RUN: clang-refactor-test perform -action extract -selected=%s:58:3-58:18 %s -x c | FileCheck --check-prefix=CHECK3-C %s
// RUN: clang-refactor-test perform -action extract -selected=%s:58:3-58:18 %s | FileCheck --check-prefix=CHECK3-CPP %s

typedef struct {
  Rectangle r;
} RectangleInStruct;

void addressOfMember(Rectangle r, RectangleInStruct rs) {
  takesPtr(&r.width);
// CHECK4-C: (Rectangle *r) {\ntakesPtr(&r->width);\n}
// CHECK4-CPP: (Rectangle &r) {\ntakesPtr(&r.width);\n}
  takesPtr(&(rs).r.width);
// CHECK4-C: (RectangleInStruct *rs) {\ntakesPtr(&(rs)->r.width);\n}
// CHECK4-CPP: (RectangleInStruct &rs) {\ntakesPtr(&(rs).r.width);\n}
}

// RUN: clang-refactor-test perform -action extract -selected=%s:71:3-71:21 -selected=%s:74:3-74:26 %s -x c | FileCheck --check-prefix=CHECK4-C %s
// RUN: clang-refactor-test perform -action extract -selected=%s:71:3-71:21 -selected=%s:74:3-74:26 %s | FileCheck --check-prefix=CHECK4-CPP %s

void takesConstPtr(const int *x) { }

#ifdef __cplusplus

void addressOfMember(const Rectangle &r) {
  takesConstPtr(&r.width);
// CHECK5: (const Rectangle &r) {\ntakesConstPtr(&r.width);\n}
}

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

class PrivateInstanceVariables {
  int x;
  Rectangle r;

  void method() {
    takesPtr(&x);
// CHECK6: extracted(int &x) {\ntakesPtr(&x);\n}
    takesStructPtr(&(r));
// CHECK6: extracted(Rectangle &r) {\ntakesStructPtr(&(r));\n}
    takesPtr(&((r).width));
// CHECK6: extracted(Rectangle &r) {\ntakesPtr(&((r).width));\n}
  }
};

// RUN: clang-refactor-test perform -action extract -selected=%s:98:5-98:17 -selected=%s:100:5-100:25 -selected=%s:102:5-102:27 %s | FileCheck --check-prefix=CHECK6 %s

#endif

void takesCVoidPtr(const void *x) { }
void takesCPtr(const int *x) { }
void takesCPtrPtr(int * const *x) { }
void takesBothPtrs(const int *x, int *y) {}
void addressForConstUseShouldPassAsConst(int x, int *ip) {
  takesCPtr(&x);
// CHECK7-C: (const int *x) {\ntakesCPtr(x);\n}
// CHECK7-CPP: (const int &x) {\ntakesCPtr(&x);\n}
  takesCVoidPtr((&(x)));
// CHECK7-C: (const int *x) {\ntakesCVoidPtr(((x)));\n}
// CHECK7-CPP: (const int &x) {\ntakesCVoidPtr((&(x)));\n}
  takesCPtrPtr(&ip);
// CHECK7-C: (int *const *ip) {\ntakesCPtrPtr(ip);\n}
// CHECK7-CPP: (int *const &ip) {\ntakesCPtrPtr(&ip);\n}
  takesCPtr(ip);
// CHECK7-C: (int *ip) {\ntakesCPtr(ip);\n}
// CHECK7-CPP: (int *ip) {\ntakesCPtr(ip);\n}
  takesBothPtrs(&x, &x);
// CHECK7-C: (int *x) {\ntakesBothPtrs(x, x);\n}
// CHECK7-CPP: (int &x) {\ntakesBothPtrs(&x, &x);\n}
}

// RUN: clang-refactor-test perform -action extract -selected=%s:116:3-116:16 -selected=%s:119:3-119:22 -selected=%s:122:3-122:20 -selected=%s:125:3-125:16 -selected=%s:128:3-128:24 %s -x c | FileCheck --check-prefix=CHECK7-C %s
// RUN: clang-refactor-test perform -action extract -selected=%s:116:3-116:16 -selected=%s:119:3-119:22 -selected=%s:122:3-122:20 -selected=%s:125:3-125:16 -selected=%s:128:3-128:24 %s | FileCheck --check-prefix=CHECK7-CPP %s

void addressOfConstUseAndMutation(int x) {
  x = 0;
  takesCPtr(&x);
  x = 1;
}
// CHECK8: extracted(int &x) {\nx = 0;\n  takesCPtr(&x);\n}
// CHECK8: extracted(int &x) {\ntakesCPtr(&x);\n  x = 1;\n}

// RUN: clang-refactor-test perform -action extract -selected=%s:137:3-138:16 -selected=%s:138:3-139:8 %s | FileCheck --check-prefix=CHECK8 %s

void takesCStructPtr(const Rectangle *r) { }

void constAddressOfMember(Rectangle r, RectangleInStruct rs) {
  takesCStructPtr(&r);
// CHECK9-C: extracted(const Rectangle *r) {\ntakesCStructPtr(r);\n}
// CHECK9-CPP: extracted(const Rectangle &r) {\ntakesCStructPtr(&r);\n}
  takesCPtr(&r.width);
// CHECK9-C: (const Rectangle *r) {\ntakesCPtr(&r->width);\n}
// CHECK9-CPP: (const Rectangle &r) {\ntakesCPtr(&r.width);\n}
  takesCPtr((&(rs).r.height));
// CHECK9-C: (const RectangleInStruct *rs) {\ntakesCPtr((&(rs)->r.height));\n}
// CHECK9-CPP: (const RectangleInStruct &rs) {\ntakesCPtr((&(rs).r.height));\n}
}

// RUN: clang-refactor-test perform -action extract -selected=%s:149:3-149:22 -selected=%s:152:3-152:22 -selected=%s:155:3-155:30 %s -x c | FileCheck --check-prefix=CHECK9-C %s
// RUN: clang-refactor-test perform -action extract -selected=%s:149:3-149:22 -selected=%s:152:3-152:22 -selected=%s:155:3-155:30  %s | FileCheck --check-prefix=CHECK9-CPP %s

#ifdef __cplusplus

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

class PrivateInstanceVariablesConstAddress {
  int x;
  Rectangle r;

  void method() {
    takesCPtr(&x);
// CHECK10: extracted(const int &x) {\ntakesCPtr(&x);\n}
    takesCStructPtr(&r);
// CHECK10: extracted(const Rectangle &r) {\ntakesCStructPtr(&r);\n}
  }
};

// RUN: clang-refactor-test perform -action extract -selected=%s:172:5-172:18 -selected=%s:174:5-174:24 %s | FileCheck --check-prefix=CHECK10 %s

#endif

void rewriteToPtrWithDerefParensForArrow(Rectangle *r) {
  int y = r->width;
  r = 0;
// CHECK11: (Rectangle **r) {\nint y = (*r)->width;\n  *r = 0;\n}\n\n"
}
// RUN: clang-refactor-test perform -action extract -selected=%s:184:3-185:8 %s -x c | FileCheck --check-prefix=CHECK11 %s

void constAddressWithConditionalOperator(int x, int y) {
  takesCPtr(&(x == 0 ? x : y));
// CHECK12: (const int &x, const int &y) {\ntakesCPtr(&(x == 0 ? x : y));\n}
}
// RUN: clang-refactor-test perform -action extract -selected=%s:191:3-191:31 %s | FileCheck --check-prefix=CHECK12 %s