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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
|
#include <iostream>
#include <iomanip>
#include <string>
#include <boost/preprocessor/config/config.hpp>
static unsigned int indent = 4;
static unsigned int width = 90;
using std::cout;
using std::istream;
void print_separator()
{
std::cout <<
"\n\n*********************************************************************\n\n";
}
std::string remove_spaces(const std::string ss)
{
bool inquotes(false);
bool escape(false);
char qchar;
int len(static_cast<int>(ss.length()));
std::string ret;
for (int i = 0; i < len; ++i)
{
char ch(ss[i]);
if (inquotes)
{
if (escape)
{
escape = false;
}
else if (ch == '\\')
{
escape = true;
}
else if (ch == qchar)
{
inquotes = false;
}
ret.push_back(ch);
}
else
{
if (ch == '\'' || ch == '"')
{
inquotes = true;
qchar = ch;
ret.push_back(ch);
}
else if (ch != ' ')
{
ret.push_back(ch);
}
}
}
return ret;
}
int print_macro(const std::string name,const std::string expected, const std::string expansion)
{
int bret(0);
const std::string sg("Success: ");
const std::string sb("Failure: ");
for(unsigned i = 0; i < indent; ++i) std::cout.put(' ');
if (name == expansion)
{
if (expected == expansion)
{
std::cout << sg;
std::cout << std::setw(width);
cout.setf(istream::left, istream::adjustfield);
std::cout << name;
std::cout << " [no value]\n";
}
else
{
std::cout << sb;
std::cout << std::setw(width);
cout.setf(istream::left, istream::adjustfield);
std::cout << name;
std::cout << " [no value]: ";
std::cout << " [expected]: ";
std::cout << expected << "\n";
bret = 1;
}
}
else
{
std::string sexpected(remove_spaces(expected));
std::string sexpansion(remove_spaces(expansion));
if (sexpected == sexpansion)
{
std::cout << sg;
std::cout << std::setw(width);
cout.setf(istream::left, istream::adjustfield);
std::cout << name;
std::cout << expansion << "\n";
}
else
{
std::cout << sb;
std::cout << std::setw(width);
cout.setf(istream::left, istream::adjustfield);
std::cout << name;
std::cout << expansion;
std::cout << " [expected]: ";
std::cout << expected << "\n";
bret = 1;
}
}
return bret;
}
#if !BOOST_PP_VARIADICS
#define STRINGIZE(arg) # arg
#define PRINT_MACRO_RESULTS(X,exp) print_macro(std::string(# X), std::string(# exp), std::string(STRINGIZE(X)))
#define PRINT_MACRO(x) std::string(# x)
#define PRINT_EXPECTED(x) std::string(# x)
#define PRINT_EXPANSION(x) std::string(STRINGIZE(x))
#else
#define STRINGIZE(...) # __VA_ARGS__
#define PRINT_MACRO_RESULTS(X,...) print_macro(std::string(# X), std::string(# __VA_ARGS__), std::string(STRINGIZE(X)))
#define PRINT_MACRO(...) std::string(# __VA_ARGS__)
#define PRINT_EXPECTED(...) std::string(# __VA_ARGS__)
#define PRINT_EXPANSION(...) std::string(STRINGIZE(__VA_ARGS__))
#endif
#if __cplusplus > 201703L
int print_macros_common_c20()
{
int bret = 0;
#define LPAREN() (
#define G(Q) 42
#define F(R, X, ...) __VA_OPT__(G R X) )
// int x = F(LPAREN(), 0, <:-);
// replaced by
// int x = 42;
bret += PRINT_MACRO_RESULTS(int x = F(LPAREN(), 0, <:-);,int x = 42;);
#undef LPAREN
#undef G
#undef F
#define F(...) f(0 __VA_OPT__(,) __VA_ARGS__)
#define G(X, ...) f(0, X __VA_OPT__(,) __VA_ARGS__)
#define SDEF(sname, ...) S sname __VA_OPT__(= { __VA_ARGS__ })
#define EMP
// F(a, b, c)
// F()
// F(EMP)
// replaced by
// f(0, a, b, c)
// f(0)
// f(0)
// G(a, b, c)
// G(a, )
// G(a)
// replaced by
// f(0, a, b, c)
// f(0, a)
// f(0, a)
// SDEF(foo);
// SDEF(bar, 1, 2);
// replaced by
// S foo;
// S bar = { 1, 2 };
bret += PRINT_MACRO_RESULTS(F(a, b, c),f(0, a, b, c));
bret += PRINT_MACRO_RESULTS(F(),f(0));
bret += PRINT_MACRO_RESULTS(F(EMP),f(0));
bret += PRINT_MACRO_RESULTS(G(a, b, c),f(0, a, b, c));
bret += PRINT_MACRO_RESULTS(G(a, ),f(0, a));
bret += PRINT_MACRO_RESULTS(G(a),f(0, a));
bret += PRINT_MACRO_RESULTS(SDEF(foo);,S foo;);
bret += PRINT_MACRO_RESULTS(SDEF(bar, 1, 2);,S bar = { 1, 2 };);
#undef F
#undef G
#undef SDEF
#undef EMP
#define H2(X, Y, ...) __VA_OPT__(X ## Y,) __VA_ARGS__
#define H3(X, ...) #__VA_OPT__(X##X X##X)
#define H4(X, ...) __VA_OPT__(a X ## X) ## b
#define H5A(...) __VA_OPT__()/**/__VA_OPT__()
#define H5B(X) a ## X ## b
#define H5C(X) H5B(X)
// H2(a, b, c, d)
// replaced by
// ab, c, d
// H3(, 0)
// replaced by
// ""
// H4(, 1)
// replaced by
// a b
// H5C(H5A())
// replaced by
// ab
bret += PRINT_MACRO_RESULTS(H2(a, b, c, d),ab, c, d);
bret += PRINT_MACRO_RESULTS(H3(, 0),"");
bret += PRINT_MACRO_RESULTS(H4(, 1),a b);
bret += PRINT_MACRO_RESULTS(H5C(H5A()),ab);
#undef H2
#undef H3
#undef H4
#undef H5A
#undef H5B
#undef H5C
return bret;
}
#endif
int print_macros_common_1()
{
int bret = 0;
#define x 3
#define f(a) f(x * (a))
#undef x
#define x 2
#define g f
#define z z[0]
#define h g(~
#define m(a) a(w)
#define w 0,1
#define t(a) a
// f(y+1) + f(f(z)) % t(t(g)(0) + t)(1);
// g(x+(3,4)-w) | h 5) & m(f)^m(m);
// results in
// f(2 * (y+1)) + f(2 * (f(2 * (z[0])))) % f(2 * (0)) + t(1);
// f(2 * (2+(3,4)-0,1)) | f(2 * ( ~ 5)) & f(2 * (0,1))^m(0,1);
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););
#define PRINT_INPUT g(x+(3,4)-w) | h 5) & m(f)^m(m);
bret += print_macro
(
std::string("g(x+(3,4)-w) | h 5) & m(f)^m(m);"),
PRINT_EXPECTED(f(2 * (2+(3,4)-0,1)) | f(2 * ( ~ 5)) & f(2 * (0,1))^m(0,1);),
PRINT_EXPANSION(PRINT_INPUT)
);
#undef PRINT_INPUT
#undef f
#undef x
#undef g
#undef z
#undef h
#undef m
#undef w
#undef t
return bret;
}
int print_macros_common_4()
{
int bret = 0;
#define str(s) # s
#define xstr(s) str(s)
#define debug(s, t) printf("x" # s "= %d, x" # t "= %s", x ## s, x ## t)
#define INCFILE(n) vers ## n
#define glue(a, b) a ## b
#define xglue(a, b) glue(a, b)
#define HIGHLOW "hello"
#define LOW LOW ", world"
// debug(1, 2);
// fputs(str(strncmp("abc\0d", "abc", \4) // this goes away
// == 0) str(: @\n), s);
// #include xstr(INCFILE(2).h)
// glue(HIGH, LOW);
// xglue(HIGH, LOW)
// results in
// printf("x" "1" "= %d, x" "2" "= %s", x1, x2);
// fputs("strncmp(\"abc\\0d\", \"abc\", \\4) == 0" ": @\n", s);
// #include "vers2.h" (after macro replacement, before file access)
// "hello";
// "hello" ", world"
bret += PRINT_MACRO_RESULTS(debug(1, 2);,printf("x" "1" "= %d, x" "2" "= %s", x1, x2););
bret += print_macro
(
std::string("fputs(str(strncmp(\"abc\\0d\", \"abc\", '\\4') /* this goes away */== 0) str(: @\\n), s);"),
PRINT_EXPECTED(fputs("strncmp(\"abc\\0d\", \"abc\", '\\4') == 0" ": @\n", s);),
PRINT_EXPANSION(fputs(str(strncmp("abc\0d", "abc", '\4') /* this goes away */== 0) str(: @\n), s);)
);
bret += PRINT_MACRO_RESULTS(xstr(INCFILE(2).h),"vers2.h");
bret += PRINT_MACRO_RESULTS(glue(HIGH, LOW);,"hello";);
#if __cplusplus <= 199711L
bret += print_macro
(
PRINT_MACRO(xglue(HIGH, LOW)),
std::string("\"hello\" \", world\""),
PRINT_EXPANSION(xglue(HIGH, LOW))
);
#else
bret += PRINT_MACRO_RESULTS(xglue(HIGH, LOW),"hello" ", world");
#endif
#undef str
#undef xstr
#undef debug
#undef INCFILE
#undef glue
#undef xglue
#undef HIGHLOW
#undef LOW
return bret;
}
int print_macros_common_2()
{
int bret = 0;
#define hash_hash # ## #
#define mkstr(a) # a
#define in_between(a) mkstr(a)
#define join(c, d) in_between(c hash_hash d)
// char p[] = join(x, y);
// equivalent to
// char p[] = "x ## y";
bret += PRINT_MACRO_RESULTS(char p[] = join(x, y);,char p[] = "x ## y";);
#undef hash_hash
#undef mkstr
#undef in_between
#undef join
return bret;
}
#if BOOST_PP_VARIADICS && __cplusplus > 199711L
int print_macros_common_3()
{
int bret = 0;
#define p() int
#define q(x) x
#define r(x,y) x ## y
#define str(x) # x
// p() i[q()] = { q(1), r(2,3), r(4,), r(,5), r(,) };
// char c[2][6] = { str(hello), str() };
// results in
// int i[] = { 1, 23, 4, 5, };
// char c[2][6] = { "hello", "" };
bret += print_macro
(
PRINT_MACRO(p() i[q()] = { q(1), r(2,3), r(4,), r(,5), r(,) };),
PRINT_EXPECTED(int i[] = { 1, 23, 4, 5, };),
PRINT_EXPANSION(p() i[q()] = { q(1), r(2,3), r(4,), r(,5), r(,) };)
);
bret += print_macro
(
PRINT_MACRO(char c[2][6] = { str(hello), str() };),
PRINT_EXPECTED(char c[2][6] = { "hello", "" };),
PRINT_EXPANSION(char c[2][6] = { str(hello), str() };)
);
#undef p
#undef q
#undef r
#undef str
bret += print_macros_common_4();
#define t(x,y,z) x ## y ## z
// int j[] = { t(1,2,3), t(,4,5), t(6,,7), t(8,9,), t(10,,), t(,11,), t(,,12), t(,,) };
// results in
// int j[] = { 123, 45, 67, 89, 10, 11, 12, };
bret += print_macro
(
PRINT_MACRO(int j[] = { t(1,2,3), t(,4,5), t(6,,7), t(8,9,), t(10,,), t(,11,), t(,,12), t(,,) };),
PRINT_EXPECTED(int j[] = { 123, 45, 67, 89, 10, 11, 12, };),
PRINT_EXPANSION(int j[] = { t(1,2,3), t(,4,5), t(6,,7), t(8,9,), t(10,,), t(,11,), t(,,12), t(,,) };)
);
#undef t
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define showlist(...) puts(#__VA_ARGS__)
#define report(test, ...) ((test) ? puts(#test) : printf(__VA_ARGS__))
// debug("Flag");
// debug("X = %d\n", x);
// showlist(The first, second, and third items.);
// report(x>y, "x is %d but y is %d", x, y);
// results in
// fprintf(stderr, "Flag");
// fprintf(stderr, "X = %d\n", x);
// puts("The first, second, and third items.");
// ((x>y) ? puts("x>y") : printf("x is %d but y is %d", x, y));
#define save_stderr stderr
#undef stderr
bret += PRINT_MACRO_RESULTS(debug("Flag");,fprintf(stderr, "Flag"););
bret += PRINT_MACRO_RESULTS(debug("X = %d\n", x);,fprintf(stderr, "X = %d\n", x););
#define stderr save_stderr
bret += PRINT_MACRO_RESULTS(showlist(The first, second, and third items.);,puts("The first, second, and third items."););
bret += PRINT_MACRO_RESULTS(report(x>y, "x is %d but y is %d", x, y);,((x>y) ? puts("x>y") : printf("x is %d but y is %d", x, y)););
#undef debug
#undef showlist
#undef report
return bret;
}
#endif
int print_macros()
{
int bret = 0;
print_separator();
std::cout << "__cplusplus = " << __cplusplus;
print_separator();
#define OBJ_LIKE (1-1)
#define OBJ_LIKE /* white space */ (1-1) /* other */
#define FTN_LIKE(a) ( a )
#define FTN_LIKE( a )( /* note the white space */ a /* other stuff on this line */ )
#if !BOOST_PP_VARIADICS
bret += print_macros_common_1();
bret += print_macros_common_4();
#elif __cplusplus <= 199711L
bret += print_macros_common_2();
bret += print_macros_common_1();
bret += print_macros_common_4();
#elif __cplusplus <= 201703L
bret += print_macros_common_2();
bret += print_macros_common_1();
bret += print_macros_common_3();
#else
bret += print_macros_common_c20();
bret += print_macros_common_2();
bret += print_macros_common_1();
bret += print_macros_common_3();
#endif
print_separator();
return bret;
}
int main()
{
return (print_macros());
}
|