]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | #include <iostream> |
2 | #include <iomanip> | |
3 | #include <string> | |
4 | #include <boost/preprocessor/config/config.hpp> | |
5 | ||
6 | static unsigned int indent = 4; | |
7 | static unsigned int width = 90; | |
8 | ||
9 | using std::cout; | |
10 | using std::istream; | |
11 | ||
12 | void print_separator() | |
13 | { | |
14 | std::cout << | |
15 | "\n\n*********************************************************************\n\n"; | |
16 | } | |
17 | ||
18 | std::string remove_spaces(const std::string ss) | |
19 | { | |
20 | ||
21 | bool inquotes(false); | |
22 | bool escape(false); | |
23 | char qchar; | |
24 | int len(static_cast<int>(ss.length())); | |
25 | ||
26 | std::string ret; | |
27 | ||
28 | for (int i = 0; i < len; ++i) | |
29 | { | |
30 | ||
31 | char ch(ss[i]); | |
32 | ||
33 | if (inquotes) | |
34 | { | |
35 | if (escape) | |
36 | { | |
37 | escape = false; | |
38 | } | |
39 | else if (ch == '\\') | |
40 | { | |
41 | escape = true; | |
42 | } | |
43 | else if (ch == qchar) | |
44 | { | |
45 | inquotes = false; | |
46 | } | |
47 | ret.push_back(ch); | |
48 | } | |
49 | else | |
50 | { | |
51 | if (ch == '\'' || ch == '"') | |
52 | { | |
53 | inquotes = true; | |
54 | qchar = ch; | |
55 | ret.push_back(ch); | |
56 | } | |
57 | else if (ch != ' ') | |
58 | { | |
59 | ret.push_back(ch); | |
60 | } | |
61 | } | |
62 | } | |
63 | ||
64 | return ret; | |
65 | } | |
66 | ||
67 | int print_macro(const std::string name,const std::string expected, const std::string expansion) | |
68 | { | |
69 | int bret(0); | |
70 | const std::string sg("Success: "); | |
71 | const std::string sb("Failure: "); | |
72 | for(unsigned i = 0; i < indent; ++i) std::cout.put(' '); | |
73 | if (name == expansion) | |
74 | { | |
75 | if (expected == expansion) | |
76 | { | |
77 | std::cout << sg; | |
78 | std::cout << std::setw(width); | |
79 | cout.setf(istream::left, istream::adjustfield); | |
80 | std::cout << name; | |
81 | std::cout << " [no value]\n"; | |
82 | } | |
83 | else | |
84 | { | |
85 | std::cout << sb; | |
86 | std::cout << std::setw(width); | |
87 | cout.setf(istream::left, istream::adjustfield); | |
88 | std::cout << name; | |
89 | std::cout << " [no value]: "; | |
90 | std::cout << " [expected]: "; | |
91 | std::cout << expected << "\n"; | |
92 | bret = 1; | |
93 | } | |
94 | } | |
95 | else | |
96 | { | |
97 | ||
98 | std::string sexpected(remove_spaces(expected)); | |
99 | std::string sexpansion(remove_spaces(expansion)); | |
100 | ||
101 | if (sexpected == sexpansion) | |
102 | { | |
103 | std::cout << sg; | |
104 | std::cout << std::setw(width); | |
105 | cout.setf(istream::left, istream::adjustfield); | |
106 | std::cout << name; | |
107 | std::cout << expansion << "\n"; | |
108 | } | |
109 | else | |
110 | { | |
111 | std::cout << sb; | |
112 | std::cout << std::setw(width); | |
113 | cout.setf(istream::left, istream::adjustfield); | |
114 | std::cout << name; | |
115 | std::cout << expansion; | |
116 | std::cout << " [expected]: "; | |
117 | std::cout << expected << "\n"; | |
118 | bret = 1; | |
119 | } | |
120 | } | |
121 | return bret; | |
122 | } | |
123 | ||
f67539c2 TL |
124 | #define STRINGIZE(...) # __VA_ARGS__ |
125 | ||
126 | #define PRINT_MACRO_RESULTS(X,...) print_macro(std::string(# X), std::string(# __VA_ARGS__), std::string(STRINGIZE(X))) | |
127 | #define PRINT_MACRO(...) std::string(# __VA_ARGS__) | |
128 | #define PRINT_EXPECTED(...) std::string(# __VA_ARGS__) | |
129 | #define PRINT_EXPANSION(...) std::string(STRINGIZE(__VA_ARGS__)) | |
130 | ||
f67539c2 TL |
131 | #if __cplusplus > 201703L |
132 | ||
133 | int print_macros_common_c20() | |
134 | { | |
135 | ||
136 | int bret = 0; | |
137 | ||
138 | #define LPAREN() ( | |
139 | #define G(Q) 42 | |
140 | #define F(R, X, ...) __VA_OPT__(G R X) ) | |
141 | ||
142 | // int x = F(LPAREN(), 0, <:-); | |
143 | ||
144 | // replaced by | |
145 | ||
146 | // int x = 42; | |
147 | ||
148 | bret += PRINT_MACRO_RESULTS(int x = F(LPAREN(), 0, <:-);,int x = 42;); | |
149 | ||
150 | #undef LPAREN | |
151 | #undef G | |
152 | #undef F | |
153 | ||
154 | #define F(...) f(0 __VA_OPT__(,) __VA_ARGS__) | |
155 | #define G(X, ...) f(0, X __VA_OPT__(,) __VA_ARGS__) | |
156 | #define SDEF(sname, ...) S sname __VA_OPT__(= { __VA_ARGS__ }) | |
157 | #define EMP | |
158 | ||
159 | // F(a, b, c) | |
160 | // F() | |
161 | // F(EMP) | |
162 | ||
163 | // replaced by | |
164 | ||
165 | // f(0, a, b, c) | |
166 | // f(0) | |
167 | // f(0) | |
168 | ||
169 | // G(a, b, c) | |
170 | // G(a, ) | |
171 | // G(a) | |
172 | ||
173 | // replaced by | |
174 | ||
175 | // f(0, a, b, c) | |
176 | // f(0, a) | |
177 | // f(0, a) | |
178 | ||
179 | // SDEF(foo); | |
180 | // SDEF(bar, 1, 2); | |
181 | ||
182 | // replaced by | |
183 | ||
184 | // S foo; | |
185 | // S bar = { 1, 2 }; | |
186 | ||
187 | bret += PRINT_MACRO_RESULTS(F(a, b, c),f(0, a, b, c)); | |
188 | bret += PRINT_MACRO_RESULTS(F(),f(0)); | |
189 | bret += PRINT_MACRO_RESULTS(F(EMP),f(0)); | |
190 | bret += PRINT_MACRO_RESULTS(G(a, b, c),f(0, a, b, c)); | |
191 | bret += PRINT_MACRO_RESULTS(G(a, ),f(0, a)); | |
192 | bret += PRINT_MACRO_RESULTS(G(a),f(0, a)); | |
193 | bret += PRINT_MACRO_RESULTS(SDEF(foo);,S foo;); | |
194 | bret += PRINT_MACRO_RESULTS(SDEF(bar, 1, 2);,S bar = { 1, 2 };); | |
195 | ||
196 | #undef F | |
197 | #undef G | |
198 | #undef SDEF | |
199 | #undef EMP | |
200 | ||
201 | #define H2(X, Y, ...) __VA_OPT__(X ## Y,) __VA_ARGS__ | |
202 | #define H3(X, ...) #__VA_OPT__(X##X X##X) | |
203 | #define H4(X, ...) __VA_OPT__(a X ## X) ## b | |
204 | #define H5A(...) __VA_OPT__()/**/__VA_OPT__() | |
205 | #define H5B(X) a ## X ## b | |
206 | #define H5C(X) H5B(X) | |
207 | ||
208 | // H2(a, b, c, d) | |
209 | ||
210 | // replaced by | |
211 | ||
212 | // ab, c, d | |
213 | ||
214 | // H3(, 0) | |
215 | ||
216 | // replaced by | |
217 | ||
218 | // "" | |
219 | ||
220 | // H4(, 1) | |
221 | ||
222 | // replaced by | |
223 | ||
224 | // a b | |
225 | ||
226 | // H5C(H5A()) | |
227 | ||
228 | // replaced by | |
229 | ||
230 | // ab | |
231 | ||
232 | bret += PRINT_MACRO_RESULTS(H2(a, b, c, d),ab, c, d); | |
233 | bret += PRINT_MACRO_RESULTS(H3(, 0),""); | |
234 | bret += PRINT_MACRO_RESULTS(H4(, 1),a b); | |
235 | bret += PRINT_MACRO_RESULTS(H5C(H5A()),ab); | |
236 | ||
237 | #undef H2 | |
238 | #undef H3 | |
239 | #undef H4 | |
240 | #undef H5A | |
241 | #undef H5B | |
242 | #undef H5C | |
243 | ||
244 | return bret; | |
245 | ||
246 | } | |
247 | ||
248 | #endif | |
249 | ||
250 | int print_macros_common_1() | |
251 | { | |
252 | ||
253 | int bret = 0; | |
254 | ||
255 | #define x 3 | |
256 | #define f(a) f(x * (a)) | |
257 | #undef x | |
258 | ||
259 | #define x 2 | |
260 | #define g f | |
261 | #define z z[0] | |
262 | #define h g(~ | |
263 | #define m(a) a(w) | |
264 | #define w 0,1 | |
265 | #define t(a) a | |
266 | ||
267 | // f(y+1) + f(f(z)) % t(t(g)(0) + t)(1); | |
268 | // g(x+(3,4)-w) | h 5) & m(f)^m(m); | |
269 | ||
270 | // results in | |
271 | ||
272 | // f(2 * (y+1)) + f(2 * (f(2 * (z[0])))) % f(2 * (0)) + t(1); | |
273 | // f(2 * (2+(3,4)-0,1)) | f(2 * ( ~ 5)) & f(2 * (0,1))^m(0,1); | |
274 | ||
275 | bret += PRINT_MACRO_RESULTS(f(y+1) + f(f(z)) % t(t(g)(0) + t)(1);,f(2 * (y+1)) + f(2 * (f(2 * (z[0])))) % f(2 * (0)) + t(1);); | |
276 | ||
277 | #define PRINT_INPUT g(x+(3,4)-w) | h 5) & m(f)^m(m); | |
278 | ||
279 | bret += print_macro | |
280 | ( | |
281 | std::string("g(x+(3,4)-w) | h 5) & m(f)^m(m);"), | |
282 | PRINT_EXPECTED(f(2 * (2+(3,4)-0,1)) | f(2 * ( ~ 5)) & f(2 * (0,1))^m(0,1);), | |
283 | PRINT_EXPANSION(PRINT_INPUT) | |
284 | ); | |
285 | ||
286 | #undef PRINT_INPUT | |
287 | ||
288 | #undef f | |
289 | #undef x | |
290 | #undef g | |
291 | #undef z | |
292 | #undef h | |
293 | #undef m | |
294 | #undef w | |
295 | #undef t | |
296 | ||
297 | return bret; | |
298 | ||
299 | } | |
300 | ||
301 | int print_macros_common_4() | |
302 | { | |
303 | ||
304 | int bret = 0; | |
305 | ||
306 | #define str(s) # s | |
307 | #define xstr(s) str(s) | |
308 | #define debug(s, t) printf("x" # s "= %d, x" # t "= %s", x ## s, x ## t) | |
309 | #define INCFILE(n) vers ## n | |
310 | #define glue(a, b) a ## b | |
311 | #define xglue(a, b) glue(a, b) | |
312 | #define HIGHLOW "hello" | |
313 | #define LOW LOW ", world" | |
314 | ||
315 | // debug(1, 2); | |
316 |