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 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699
|
/*-
* Copyright (c) 1998, 2002-2008 Kiyoshi Matsui <kmatsui@t3.rim.or.jp>
* All rights reserved.
*
* Some parts of this code are derived from the public domain software
* DECUS cpp (1984,1985) written by Martin Minow.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* D I R E C T I V E . C
* P r o c e s s D i r e c t i v e L i n e s
*
* The routines to handle directives other than #include and #pragma
* are placed here.
*/
#if PREPROCESSED
#include "mcpp.H"
#else
#include "system.H"
#include "internal.H"
#endif
static int do_if( int hash, const char * directive_name);
/* #if, #elif, #ifdef, #ifndef */
static void sync_linenum( void);
/* Synchronize number of newlines */
static long do_line( void);
/* Process #line directive */
static int get_parm( void);
/* Get parameters of macro, its nargs, names, lengths */
static int get_repl( const char * macroname);
/* Get replacement text embedding parameter number */
static char * is_formal( const char * name, int conv);
/* If formal parameter, save the number */
static char * def_stringization( char * repl_cur);
/* Define stringization */
static char * mgtoken_save( const char * macroname);
/* Prefix DEF_MAGIC to macro name in repl-text */
static char * str_parm_scan( char * string_end);
/* Scan the parameter in quote */
static void do_undef( void);
/* Process #undef directive */
static void dump_repl( const DEFBUF * dp, FILE * fp, int gcc2_va);
/* Dump replacement text */
/*
* Generate (by hand-inspection) a set of unique values for each directive.
* MCPP won't compile if there are hash conflicts.
*/
#define L_if ('i' ^ (EOS << 1))
#define L_ifdef ('i' ^ ('d' << 1))
#define L_ifndef ('i' ^ ('n' << 1))
#define L_elif ('e' ^ ('i' << 1))
#define L_else ('e' ^ ('s' << 1))
#define L_endif ('e' ^ ('d' << 1))
#define L_define ('d' ^ ('f' << 1))
#define L_undef ('u' ^ ('d' << 1))
#define L_line ('l' ^ ('n' << 1))
#define L_include ('i' ^ ('c' << 1))
#if COMPILER == GNUC
#define L_include_next ('i' ^ ('c' << 1) ^ ('_' << 1))
#endif
#if SYSTEM == SYS_MAC
#define L_import ('i' ^ ('p' << 1))
#endif
#define L_error ('e' ^ ('r' << 1))
#define L_pragma ('p' ^ ('a' << 1))
static const char * const not_ident
= "Not an identifier \"%s\""; /* _E_ */
static const char * const no_arg = "No argument"; /* _E_ */
static const char * const excess
= "Excessive token sequence \"%s\""; /* _E_ _W1_ */
void directive( void)
/*
* Process #directive lines. Each directive have their own subroutines.
*/
{
const char * const many_nesting =
"More than %.0s%ld nesting of #if (#ifdef) sections%s"; /* _F_ _W4_ _W8_ */
const char * const not_in_section
= "Not in a #if (#ifdef) section in a source file"; /* _E_ _W1_ */
const char * const illeg_dir
= "Illegal #directive \"%s%.0ld%s\""; /* _E_ _W1_ _W8_ */
const char * const in_skipped = " (in skipped block)"; /* _W8_ */
FILEINFO * file;
int token_type;
int hash;
int c;
char * tp;
in_directive = TRUE;
if (keep_comments) {
mcpp_fputc( '\n', OUT); /* Possibly flush out comments */
newlines--;
}
c = skip_ws();
if (c == '\n') /* 'null' directive */
goto ret;
token_type = scan_token( c, (workp = work_buf, &workp), work_end);
if (in_asm && (token_type != NAM
|| (! str_eq( identifier, "asm")
&& ! str_eq( identifier, "endasm"))))
/* In #asm block, ignore #anything other than #asm or #endasm */
goto skip_line;
if (token_type != NAM) {
if (mcpp_mode == OLD_PREP && token_type == NUM) { /* # 123 [fname]*/
strcpy( identifier, "line");
} else {
if (compiling) {
if (option_flags.lang_asm) {
if (warn_level & 1)
cwarn( illeg_dir, work_buf, 0L, NULL);
} else {
cerror( illeg_dir, work_buf, 0L, NULL);
}
} else if (warn_level & 8) {
cwarn( illeg_dir, work_buf, 0L, in_skipped);
}
goto skip_line;
}
}
hash = (identifier[ 1] == EOS) ? identifier[ 0]
: (identifier[ 0] ^ (identifier[ 2] << 1));
if (strlen( identifier) > 7)
hash ^= (identifier[ 7] << 1);
/* hash is set to a unique value corresponding to the directive.*/
switch (hash) {
case L_if: tp = "if"; break;
case L_ifdef: tp = "ifdef"; break;
case L_ifndef: tp = "ifndef"; break;
case L_elif: tp = "elif"; break;
case L_else: tp = "else"; break;
case L_endif: tp = "endif"; break;
case L_define: tp = "define"; break;
case L_undef: tp = "undef"; break;
case L_line: tp = "line"; break;
case L_include: tp = "include"; break;
#if COMPILER == GNUC
case L_include_next: tp = "include_next"; break;
#endif
#if SYSTEM == SYS_MAC
case L_import: tp = "import"; break;
#endif
case L_error: tp = "error"; break;
case L_pragma: tp = "pragma"; break;
default: tp = NULL; break;
}
if (tp != NULL && ! str_eq( identifier, tp)) { /* Hash conflict*/
hash = 0; /* Unknown directive, will */
tp = NULL; /* be handled by do_old() */
}
if (! compiling) { /* Not compiling now */
switch (hash) {
case L_elif :
if (! standard) {
if (warn_level & 8)
do_old(); /* Unknown directive */
goto skip_line; /* Skip the line */
} /* Else fall through */
case L_else : /* Test the #if's nest, if 0, compile */
case L_endif: /* Un-nest #if */
break;
case L_if : /* These can't turn */
case L_ifdef: /* compilation on, but */
case L_ifndef : /* we must nest #if's.*/
if (&ifstack[ BLK_NEST] < ++ifptr)
goto if_nest_err;
if (standard && (warn_level & 8)
&& &ifstack[ std_limits.blk_nest + 1] == ifptr)
cwarn( many_nesting, NULL, (long) std_limits.blk_nest
, in_skipped);
ifptr->stat = 0; /* !WAS_COMPILING */
ifptr->ifline = src_line; /* Line at section start*/
goto skip_line;
default : /* Other directives */
if (tp == NULL && (warn_level & 8))
do_old(); /* Unknown directive ? */
goto skip_line; /* Skip the line */
}
}
macro_line = 0; /* Reset error flag */
file = infile; /* Remember the current file */
switch (hash) {
case L_if:
case L_ifdef:
case L_ifndef:
if (&ifstack[ BLK_NEST] < ++ifptr)
goto if_nest_err;
if (standard && (warn_level & 4) &&
&ifstack[ std_limits.blk_nest + 1] == ifptr)
cwarn( many_nesting, NULL , (long) std_limits.blk_nest, NULL);
ifptr->stat = WAS_COMPILING;
ifptr->ifline = src_line;
goto ifdo;
case L_elif:
if (! standard) {
do_old(); /* Unrecognized directive */
break;
}
if (ifptr == &ifstack[0])
goto nest_err;
if (ifptr == infile->initif) {
goto in_file_nest_err;
}
if (ifptr->stat & ELSE_SEEN)
goto else_seen_err;
if ((ifptr->stat & (WAS_COMPILING | TRUE_SEEN)) != WAS_COMPILING) {
compiling = FALSE; /* Done compiling stuff */
goto skip_line; /* Skip this group */
}
hash = L_if;
ifdo:
c = do_if( hash, tp);
if (mcpp_debug & IF) {
mcpp_fprintf( DBG
, "#if (#elif, #ifdef, #ifndef) evaluate to %s.\n"
, compiling ? "TRUE" : "FALSE");
mcpp_fprintf( DBG, "line %ld: %s", src_line, infile->buffer);
}
if (c == FALSE) { /* Error */
compiling = FALSE; /* Skip this group */
goto skip_line; /* Prevent an extra error message */
}
break;
case L_else:
if (ifptr == &ifstack[0])
goto nest_err;
if (ifptr == infile->initif) {
if (standard)
goto in_file_nest_err;
else if (warn_level & 1)
cwarn( not_in_section, NULL, 0L, NULL);
}
if (ifptr->stat & ELSE_SEEN)
goto else_seen_err;
ifptr->stat |= ELSE_SEEN;
ifptr->elseline = src_line;
if (ifptr->stat & WAS_COMPILING) {
if (compiling || (ifptr->stat & TRUE_SEEN) != 0)
compiling = FALSE;
else
compiling = TRUE;
}
if ((mcpp_debug & MACRO_CALL) && (ifptr->stat & WAS_COMPILING)) {
sync_linenum();
mcpp_fprintf( OUT, "/*else %ld:%c*/\n", src_line
, compiling ? 'T' : 'F'); /* Show that #else is seen */
}
break;
case L_endif:
if (ifptr == &ifstack[0])
goto nest_err;
if (ifptr <= infile->initif) {
if (standard)
goto in_file_nest_err;
else if (warn_level & 1)
cwarn( not_in_section, NULL, 0L, NULL);
}
if (! compiling && (ifptr->stat & WAS_COMPILING))
wrong_line = TRUE;
compiling = (ifptr->stat & WAS_COMPILING);
if ((mcpp_debug & MACRO_CALL) && compiling) {
sync_linenum();
mcpp_fprintf( OUT, "/*endif %ld*/\n", src_line);
/* Show that #if block has ended */
}
--ifptr;
break;
case L_define:
do_define( FALSE, 0);
break;
case L_undef:
do_undef();
break;
case L_line:
if ((c = do_line()) > 0) {
src_line = c;
sharp( NULL, 0); /* Putout the new line number and file name */
infile->line = --src_line; /* Next line number is 'src_line' */
newlines = -1;
} else { /* Error already diagnosed by do_line() */
skip_nl();
}
break;
case L_include:
in_include = TRUE;
if (do_include( FALSE) == TRUE && file != infile)
newlines = -1; /* File has been included. Clear blank lines */
in_include = FALSE;
break;
case L_error:
if (! standard) {
do_old(); /* Unrecognized directive */
break;
}
cerror( infile->buffer, NULL, 0L, NULL); /* _E_ */
break;
case L_pragma:
if (! standard) {
do_old(); /* Unrecognized directive */
break;
}
do_pragma();
newlines = -1; /* Do not putout excessive '\n' */
break;
default: /* Non-Standard or unknown directives */
do_old();
break;
}
switch (hash) {
case L_if :
case L_elif :
case L_define :
case L_line :
goto skip_line; /* To prevent duplicate error message */
#if COMPILER == GNUC
case L_include_next :
if (file != infile) /* File has been included */
newlines = -1;
#endif
#if SYSTEM == SYS_MAC
case L_import :
if (file != infile) /* File has been included */
newlines = -1;
#endif
case L_error :
if (standard)
goto skip_line;
/* Else fall through */
case L_include :
case L_pragma :
if (standard)
break; /* Already read over the line */
/* Else fall through */
default : /* L_else, L_endif, L_undef, etc. */
if (mcpp_mode == OLD_PREP) {
/*
* Ignore the rest of the #directive line so you can write
* #if foo
* #endif foo
*/
;
} else if (skip_ws() != '\n') {
#if COMPILER == GNUC
if (standard && hash != L_endif)
#else
if (standard)
#endif
cerror( excess, infile->bptr-1, 0L, NULL);
else if (warn_level & 1)
cwarn( excess, infile->bptr-1, 0L, NULL);
}
skip_nl();
}
goto ret;
in_file_nest_err:
cerror( not_in_section, NULL, 0L, NULL);
goto skip_line;
nest_err:
cerror( "Not in a #if (#ifdef) section", NULL, 0L, NULL); /* _E_ */
goto skip_line;
else_seen_err:
cerror( "Already seen #else at line %.0s%ld" /* _E_ */
, NULL, ifptr->elseline, NULL);
skip_line:
skip_nl(); /* Ignore rest of line */
goto ret;
if_nest_err:
cfatal( many_nesting, NULL, (long) BLK_NEST, NULL);
ret:
in_directive = FALSE;
keep_comments = option_flags.c && compiling && !no_output;
keep_spaces = option_flags.k && compiling;
/* keep_spaces is on for #define line even if no_output is TRUE */
if (! wrong_line)
newlines++;
}
static int do_if( int hash, const char * directive_name)
/*
* Process an #if (#elif), #ifdef or #ifndef. The latter two are straight-
* forward, while #if needs a subroutine to evaluate the expression.
* do_if() is called only if compiling is TRUE. If false, compilation is
* always supressed, so we don't need to evaluate anything. This supresses
* unnecessary warnings.
*/
{
int c;
int found;
DEFBUF * defp;
if ((c = skip_ws()) == '\n') {
unget_ch();
cerror( no_arg, NULL, 0L, NULL);
return FALSE;
}
if (mcpp_debug & MACRO_CALL) {
sync_linenum();
mcpp_fprintf( OUT, "/*%s %ld*/", directive_name, src_line);
}
if (hash == L_if) { /* #if or #elif */
unget_ch();
found = (eval_if() != 0L); /* Evaluate expression */
if (mcpp_debug & MACRO_CALL)
in_if = FALSE; /* 'in_if' is dynamically set in eval_lex() */
hash = L_ifdef; /* #if is now like #ifdef */
} else { /* #ifdef or #ifndef */
if (scan_token( c, (workp = work_buf, &workp), work_end) != NAM) {
cerror( not_ident, work_buf, 0L, NULL);
return FALSE; /* Next token is not an identifier */
}
found = ((defp = look_id( identifier)) != NULL); /* Look in table*/
if (mcpp_debug & MACRO_CALL) {
if (found)
mcpp_fprintf( OUT, "/*%s*/", defp->name);
}
}
if (found == (hash == L_ifdef)) {
compiling = TRUE;
ifptr->stat |= TRUE_SEEN;
} else {
compiling = FALSE;
}
if (mcpp_debug & MACRO_CALL) {
mcpp_fprintf( OUT, "/*i %c*/\n", compiling ? 'T' : 'F');
/* Report wheather the directive is evaluated TRUE or FALSE */
}
return TRUE;
}
static void sync_linenum( void)
/*
* Put out newlines or #line line to synchronize line number with the
* annotations about #if, #elif, #ifdef, #ifndef, #else or #endif on -K option.
*/
{
if (wrong_line || newlines > 10) {
sharp( NULL, 0);
} else {
while (newlines-- > 0)
mcpp_fputc('\n', OUT);
}
newlines = -1;
}
static long do_line( void)
/*
* Parse the line to update the line number and "filename" field for the next
* input line.
* Values returned are as follows:
* -1: syntax error or out-of-range error (diagnosed by do_line(),
* eval_num()).
* [1,32767]: legal line number for C90, [1,2147483647] for C99.
* Line number [32768,2147483647] in C90 mode is only warned (not an error).
* do_line() always absorbs the line (except the <newline>).
*/
{
const char * const not_digits
= "Line number \"%s\" isn't a decimal digits sequence"; /* _E_ _W1_ */
const char * const out_of_range
= "Line number \"%s\" is out of range of [1,%ld]"; /* _E_ _W1_ */
int token_type;
VAL_SIGN * valp;
char * save;
int c;
if ((c = skip_ws()) == '\n') {
cerror( no_arg, NULL, 0L, NULL);
unget_ch(); /* Push back <newline> */
return -1L; /* Line number is not changed */
}
if (standard) {
token_type = get_unexpandable( c, FALSE);
if (macro_line == MACRO_ERROR) /* Unterminated macro */
return -1L; /* already diagnosed. */
if (token_type == NO_TOKEN) /* Macro expanded to 0 token */
goto no_num;
if (token_type != NUM)
goto illeg_num;
} else if (scan_token( c, (workp = work_buf, &workp), work_end) != NUM) {
goto illeg_num;
}
for (workp = work_buf; *workp != EOS; workp++) {
if (! isdigit( *workp & UCHARMAX)) {
if (standard) {
cerror( not_digits, work_buf, 0L, NULL);
return -1L;
} else if (warn_level & 1) {
cwarn( not_digits, work_buf, 0L, NULL);
}
}
}
valp = eval_num( work_buf); /* Evaluate number */
if (valp->sign == VAL_ERROR) { /* Error diagnosed by eval_num()*/
return -1;
} else if (standard
&& (std_limits.line_num < valp->val || valp->val <= 0L)) {
if (valp->val < LINE99LIMIT && valp->val > 0L) {
if (warn_level & 1)
cwarn( out_of_range, work_buf, std_limits.line_num, NULL);
} else {
cerror( out_of_range, work_buf, std_limits.line_num, NULL);
return -1L;
}
}
if (standard) {
token_type = get_unexpandable( skip_ws(), FALSE);
if (macro_line == MACRO_ERROR)
return -1L;
if (token_type != STR) {
if (token_type == NO_TOKEN) { /* Filename is absent */
return (long) valp->val;
} else { /* Expanded macro should be a quoted string */
goto not_fname;
}
}
} else {
if ((c = skip_ws()) == '\n') {
unget_ch();
return (long) valp->val;
}
if (scan_token( c, (workp = work_buf, &workp), work_end) != STR)
goto not_fname;
}
#if COMPILER == GNUC
if (memcmp( workp - 3, "//", 2) == 0) { /* "/cur-dir//" */
save = infile->filename; /* Do not change the file name */
} else
#endif
{
*(workp - 1) = EOS; /* Ignore right '"' */
save = save_string( &work_buf[ 1]); /* Ignore left '"' */
}
if (standard) {
if (get_unexpandable( skip_ws(), FALSE) != NO_TOKEN) {
cerror( excess, work_buf, 0L, NULL);
free( save);
return -1L;
}
} else if (mcpp_mode == OLD_PREP) {
skip_nl();
unget_ch();
} else if ((c = skip_ws()) == '\n') {
unget_ch();
} else {
if (warn_level & 1) {
scan_token( c, (workp = work_buf, &workp), work_end);
cwarn( excess, work_buf, 0, NULL);
}
skip_nl();
unget_ch();
}
if (infile->filename)
free( infile->filename);
infile->filename = save; /* New file name */
/* Note that this does not change infile->real_fname */
return (long) valp->val; /* New line number */
no_num:
cerror( "No line number", NULL, 0L, NULL); /* _E_ */
return -1L;
illeg_num:
cerror( "Not a line number \"%s\"", work_buf, 0L, NULL); /* _E_ */
return -1L;
not_fname:
cerror( "Not a file name \"%s\"", work_buf, 0L, NULL); /* _E_ */
return -1L;
}
/*
* M a c r o D e f i n i t i o n s
*/
/*
* look_id() Looks for the name in the defined symbol table. Returns a
* pointer to the definition if found, or NULL if not present.
* install_macro() Installs the definition. Updates the symbol table.
* undefine() Deletes the definition from the symbol table.
*/
/*
* Global work_buf[] are used to store #define parameter lists and
* parms[].name point to them.
* 'nargs' contains the actual number of parameters stored.
*/
typedef struct {
char * name; /* -> Start of each parameter */
size_t len; /* Length of parameter name */
} PARM;
static PARM parms[ NMACPARS];
static int nargs; /* Number of parameters */
static char * token_p; /* Pointer to the token scanned */
static char * repl_base; /* Base of buffer for repl-text */
static char * repl_end; /* End of buffer for repl-text */
static const char * const no_ident = "No identifier"; /* _E_ */
#if COMPILER == GNUC
static int gcc2_va_arg; /* GCC2-spec variadic macro */
#endif
DEFBUF * do_define(
int ignore_redef, /* Do not redefine */
int predefine /* Predefine compiler-specific name */
/*
* Note: The value of 'predefine' should be one of 0, DEF_NOARGS_PREDEF
* or DEF_NOARGS_PREDEF_OLD, the other values cause errors.
*/
)
/*
* Called from directive() when a #define is scanned or called from
* do_options() when a -D option is scanned. This module parses formal
* parameters by get_parm() and the replacement text by get_repl().
*
* There is some special case code to distinguish
* #define foo bar -- object-like macro
* from #define foo() bar -- function-like macro with no parameter
*
* Also, we make sure that
* #define foo foo
* expands to "foo" but doesn't put MCPP into an infinite loop.
*
* A warning is printed if you redefine a symbol with a non-identical
* text. I.e,
* #define foo 123
* #define foo 123
* is ok, but
* #define foo 123
* #define foo +123
* is not.
*
* The following subroutines are called from do_define():
* get_parm() parsing and remembering parameter names.
* get_repl() parsing and remembering replacement text.
*
* The following subroutines are called from get_repl():
* is_formal() is called when an identifier is scanned. It checks through
* the array of formal parameters. If a match is found, the
* identifier is replaced by a control byte which will be used
* to locate the parameter when the macro is expanded.
* def_stringization() is called when '#' operator is scanned. It surrounds
* the token to stringize with magic-codes.
*
* modes other than STD ignore difference of parameter names in macro
* redefinition.
*/
{
const char * const predef = "\"%s\" shouldn't be redefined"; /* _E_ */
char repl_list[ NMACWORK + IDMAX]; /* Replacement text */
char macroname[ IDMAX + 1]; /* Name of the macro defining */
DEFBUF * defp; /* -> Old definition */
DEFBUF ** prevp; /* -> Pointer to previous def in list */
int c;
int redefined; /* TRUE if redefined */
int dnargs = 0; /* defp->nargs */
int cmp; /* Result of name comparison */
size_t def_start, def_end; /* Column of macro definition */
repl_base = repl_list;
repl_end = & repl_list[ NMACWORK];
c = skip_ws();
if ((mcpp_debug & MACRO_CALL) && src_line) /* Start of definition */
def_start = infile->bptr - infile->buffer - 1;
if (c == '\n') {
cerror( no_ident, NULL, 0L, NULL);
unget_ch();
return NULL;
} else if (scan_token( c, (workp = work_buf, &workp), work_end) != NAM) {
cerror( not_ident, work_buf, 0L, NULL);
return NULL;
} else {
prevp = look_prev( identifier, &cmp);
/* Find place in the macro list to insert the definition */
defp = *prevp;
if (standard) {
if (cmp || defp->push) { /* Not known or 'pushed' macro */
if (str_eq( identifier, "defined")
|| ((stdc_val || cplus_val)
&& str_eq( identifier, "__VA_ARGS__"))) {
cerror(
"\"%s\" shouldn't be defined", identifier, 0L, NULL); /* _E_ */
return NULL;
}
redefined = FALSE; /* Quite new definition */
} else { /* It's known: */
if (ignore_redef)
return defp;
dnargs = (defp->nargs == DEF_NOARGS_STANDARD
|| defp->nargs == DEF_NOARGS_PREDEF
|| defp->nargs == DEF_NOARGS_PREDEF_OLD)
? DEF_NOARGS : defp->nargs;
if (dnargs <= DEF_NOARGS_DYNAMIC /* __FILE__ and such */
|| dnargs == DEF_PRAGMA /* _Pragma() pseudo-macro */
) {
cerror( predef, identifier, 0L, NULL);
return NULL;
} else {
redefined = TRUE; /* Remember this fact */
}
}
} else {
if (cmp) {
redefined = FALSE; /* Quite new definition */
} else { /* It's known: */
if (ignore_redef)
return defp;
dnargs = (defp->nargs == DEF_NOARGS_STANDARD
|| defp->nargs == DEF_NOARGS_PREDEF
|| defp->nargs == DEF_NOARGS_PREDEF_OLD)
? DEF_NOARGS : defp->nargs;
redefined = TRUE;
}
}
}
strcpy( macroname, identifier); /* Remember the name */
in_define = TRUE; /* Recognize '#', '##' */
if (get_parm() == FALSE) { /* Get parameter list */
in_define = FALSE;
return NULL; /* Syntax error */
}
if (get_repl( macroname) == FALSE) { /* Get replacement text */
in_define = FALSE;
return NULL; /* Syntax error */
}
if ((mcpp_debug & MACRO_CALL) && src_line) {
/* Remember location on source */
char * cp;
cp = infile->bptr - 1; /* Before '\n' */
while (char_type[ *cp & UCHARMAX] & HSP)
cp--; /* Trailing space */
cp++; /* Just after the last token */
def_end = cp - infile->buffer; /* End of definition */
}
in_define = FALSE;
if (redefined) {
if (dnargs != nargs || ! str_eq( defp->repl, repl_list)
|| (mcpp_mode == STD && ! str_eq( defp->parmnames, work_buf))
) { /* Warn if differently redefined */
if (warn_level & 1) {
cwarn(
"The macro is redefined", NULL, 0L, NULL); /* _W1_ */
if (! option_flags.no_source_line)
dump_a_def( " previously macro", defp, FALSE, TRUE
, fp_err);
}
} else { /* Identical redefinition */
return defp;
}
} /* Else new or re-definition*/
defp = install_macro( macroname, nargs, work_buf, repl_list, prevp, cmp
, predefine);
if ((mcpp_debug & MACRO_CALL) && src_line) {
/* Get location on source file */
LINE_COL s_line_col, e_line_col;
s_line_col.line = src_line;
s_line_col.col = def_start;
get_src_location( & s_line_col);
/* Convert to pre-line-splicing data */
e_line_col.line = src_line;
e_line_col.col = def_end;
get_src_location( & e_line_col);
/* Putout the macro definition information embedded in comment */
mcpp_fprintf( OUT, "/*m%s %ld:%d-%ld:%d*/\n", defp->name
, s_line_col.line, s_line_col.col
, e_line_col.line, e_line_col.col);
wrong_line = TRUE; /* Need #line later */
}
if (mcpp_mode == STD && cplus_val && id_operator( macroname)
&& (warn_level & 1))
/* These are operators, not identifiers, in C++98 */
cwarn( "\"%s\" is defined as macro", macroname /* _W1_ */
, 0L, NULL);
return defp;
}
static int get_parm( void)
/*
* Get parameters i.e. numbers into nargs, name into work_buf[], name-length
* into parms[].len. parms[].name point into work_buf.
* Return TRUE if the parameters are legal, else return FALSE.
* In STD mode preprocessor must remember the parameter names, only for
* checking the validity of macro redefinitions. This is required by the
* Standard (what an overhead !).
*/
{
const char * const many_parms
= "More than %.0s%ld parameters"; /* _E_ _W4_ */
const char * const illeg_parm
= "Illegal parameter \"%s\""; /* _E_ */
const char * const misplaced_ellip
= "\"...\" isn't the last parameter"; /* _E_ */
int token_type;
int c;
parms[ 0].name = workp = work_buf;
work_buf[ 0] = EOS;
#if COMPILER == GNUC
gcc2_va_arg = FALSE;
#endif
/* POST_STD mode */
insert_sep = NO_SEP; /* Clear the inserted token separator */
c = get_ch();
if (c == '(') { /* With arguments? */
nargs = 0; /* Init parms counter */
if (skip_ws() == ')')
return TRUE; /* Macro with 0 parm */
else
unget_ch();
do { /* Collect parameters */
if (nargs >= NMACPARS) {
cerror( many_parms, NULL, (long) NMACPARS, NULL);
return FALSE;
}
parms[ nargs].name = workp; /* Save its start */
if ((token_type = scan_token( c = skip_ws(), &workp, work_end))
!= NAM) {
if (c == '\n') {
break;
} else if (c == ',' || c == ')') {
cerror( "Empty parameter", NULL, 0L, NULL); /* _E_ */
return FALSE;
} else if (standard && (stdc_val || cplus_val)
&& token_type == OPE && openum == OP_ELL) {
/*
* Enable variable argument macro which is a feature of
* C99. We enable this even on C90 or C++ for GCC
* compatibility.
*/
if (skip_ws() != ')') {
cerror( misplaced_ellip, NULL, 0L, NULL);
return FALSE;
}
parms[ nargs++].len = 3;
nargs |= VA_ARGS;
goto ret;
} else {
cerror( illeg_parm, parms[ nargs].name, 0L, NULL);
return FALSE; /* Bad parameter syntax */
}
}
if (standard && (stdc_val || cplus_val)
&& str_eq( identifier, "__VA_ARGS__")) {
cerror( illeg_parm, parms[ nargs].name, 0L, NULL);
return FALSE;
/* __VA_ARGS__ should not be used as a parameter */
}
if (is_formal( parms[ nargs].name, FALSE)) {
cerror( "Duplicate parameter name \"%s\"" /* _E_ */
, parms[ nargs].name, 0L, NULL);
return FALSE;
}
parms[ nargs].len = (size_t) (workp - parms[ nargs].name);
/* Save length of param */
*workp++ = ',';
nargs++;
} while ((c = skip_ws()) == ','); /* Get another parameter*/
*--workp = EOS; /* Remove excessive ',' */
if (c != ')') { /* Must end at ) */
#if COMPILER == GNUC
/* Handle GCC2 variadic params like par... */
char * tp = workp;
if (mcpp_mode == STD
&&(token_type = scan_token( c, &workp, work_end)) == OPE
&& openum == OP_ELL) {
if ((c = skip_ws()) != ')') {
cerror( misplaced_ellip, NULL, 0L, NULL);
return FALSE;
}
*tp = EOS; /* Remove "..." */
nargs |= VA_ARGS;
gcc2_va_arg = TRUE;
goto ret;
}
#endif
unget_ch(); /* Push back '\n' */
cerror(
"Missing \",\" or \")\" in parameter list \"(%s\"" /* _E_ */
, work_buf, 0L, NULL);
return FALSE;
}
} else {
/*
* DEF_NOARGS is needed to distinguish between
* "#define foo" and "#define foo()".
*/
nargs = DEF_NOARGS; /* Object-like macro */
unget_ch();
}
ret:
#if NMACPARS > NMACPARS90MIN
if ((warn_level & 4) && (nargs & ~AVA_ARGS) > std_limits.n_mac_pars)
cwarn( many_parms, NULL , (long) std_limits.n_mac_pars, NULL);
#endif
return TRUE;
}
static int get_repl(
const char * macroname
)
/*
* Get replacement text i.e. names of formal parameters are converted to
* the magic numbers, and operators #, ## is converted to magic characters.
* Return TRUE if replacement list is legal, else return FALSE.
* Any token separator in the text is converted to a single space, no token
* sepatator is inserted by MCPP. Those are required by the Standard for
* stringizing of an argument by # operator.
* In POST_STD mode, inserts a space between any tokens in source (except a
* macro name and the next '(' in macro definition), hence presence or absence
* of token separator makes no difference.
*/
{
const char * const mixed_ops
= "Macro with mixing of ## and # operators isn't portable"; /* _W4_ */
const char * const multiple_cats
= "Macro with multiple ## operators isn't portable"; /* _W4_ */
char * prev_token = NULL; /* Preceding token */
char * prev_prev_token = NULL; /* Pre-preceding token */
int multi_cats = FALSE; /* Multiple ## operators*/
int c;
int token_type; /* Type of token */
char * temp;
char * repl_cur = repl_base; /* Pointer into repl-text buffer*/
*repl_cur = EOS;
token_p = NULL;
if (mcpp_mode == STD) {
c = get_ch();
unget_ch();
if (((char_type[ c] & SPA) == 0) && (nargs < 0) && (warn_level & 1))
cwarn( "No space between macro name \"%s\" and repl-text"/* _W1_ */
, macroname, 0L, NULL);
}
c = skip_ws(); /* Get to the body */
while (c != '\n') {
if (standard) {
prev_prev_token = prev_token;
prev_token = token_p;
}
token_p = repl_cur; /* Remember the pointer */
token_type = scan_token( c, &repl_cur, repl_end);
switch (token_type) {
case OPE: /* Operator or punctuator */
if (! standard)
break;
switch (openum) {
case OP_CAT: /* ## */
if (prev_token == NULL) {
cerror( "No token before ##" /* _E_ */
, NULL, 0L, NULL);
return FALSE;
} else if (*prev_token == CAT) {
cerror( "## after ##", NULL, 0L, NULL); /* _E_ */
return FALSE;
} else if (prev_prev_token && *prev_prev_token == CAT) {
multi_cats = TRUE;
} else if (prev_prev_token && *prev_prev_token == ST_QUOTE
&& (warn_level & 4)) { /* # parm ## */
cwarn( mixed_ops, NULL, 0L, NULL);
}
repl_cur = token_p;
*repl_cur++ = CAT; /* Convert to CAT */
break;
case OP_STR: /* # */
if (nargs < 0) /* In object-like macro */
break; /* '#' is an usual char */
if (prev_token && *prev_token == CAT
&& (warn_level & 4)) /* ## # */
cwarn( mixed_ops, NULL, 0L, NULL);
repl_cur = token_p; /* Overwrite on # */
if ((temp = def_stringization( repl_cur)) == NULL) {
return FALSE; /* Error */
} else {
repl_cur = temp;
}
break;
default: /* Any operator as it is */
break;
}
break;
case NAM:
/*
* Replace this name if it's a parm. Note that the macro name is a
* possible replacement token. We stuff DEF_MAGIC in front of the
* token which is treated as a LETTER by the token scanner and eaten
* by the macro expanding routine. This prevents the macro expander
* from looping if someone writes "#define foo foo".
*/
temp = is_formal( identifier, TRUE);
if (temp == NULL) { /* Not a parameter name */
if (! standard)
break;
if ((stdc_val || cplus_val)
&& str_eq( identifier, "__VA_ARGS__")) {
#if COMPILER == GNUC
if (gcc2_va_arg) {
cerror( "\"%s\" cannot be used in GCC2-spec variadic macro" /* _E_ */
, identifier, 0L, NULL);
return FALSE;
}
#endif
cerror( "\"%s\" without corresponding \"...\"" /* _E_ */
, identifier, 0L, NULL);
return FALSE;
}
if ((temp = mgtoken_save( macroname)) != NULL)
repl_cur = temp; /* Macro name */
} else { /* Parameter name */
repl_cur = temp;
#if COMPILER == GNUC
if (mcpp_mode == STD && (nargs & VA_ARGS)
&& *(repl_cur - 1) == (nargs & ~AVA_ARGS)) {
if (! str_eq( identifier, "__VA_ARGS__")
&& (warn_level & 2))
cwarn(
"GCC2-spec variadic macro is defined" /* _W2_ */
, NULL, 0L, NULL);
if (prev_token && *prev_token == CAT
&& prev_prev_token && *prev_prev_token == ',')
/* ", ## __VA_ARGS__" is sequence peculiar */
/* to GCC3-spec variadic macro. */
/* Or ", ## last_arg" is sequence peculiar */
/* to GCC2-spec variadic macro. */
nargs |= GVA_ARGS;
/* Mark as sequence peculiar to GCC */
/* This will be warned at expansion time */
}
#endif
}
break;
case STR: /* String in mac. body */
case CHR: /* Character constant */
if (mcpp_mode == OLD_PREP)
repl_cur = str_parm_scan( repl_cur);
break;
case SEP:
if (mcpp_mode == OLD_PREP && c == COM_SEP)
repl_cur--; /* Skip comment now */
break;
default: /* Any token as it is */
break;
}
if ((c = get_ch()) == ' ' || c == '\t') {
*repl_cur++ = ' '; /* Space */
while ((c = get_ch()) == ' ' || c == '\t')
; /* Skip excessive spaces */
}
}
while (repl_base < repl_cur
&& (*(repl_cur - 1) == ' ' || *(repl_cur - 1) == '\t'))
repl_cur--; /* Remove trailing spaces */
*repl_cur = EOS; /* Terminate work */
unget_ch(); /* For syntax check */
if (standard) {
if (token_p && *token_p == CAT) {
cerror( "No token after ##", NULL, 0L, NULL); /* _E_ */
return FALSE;
}
if (multi_cats && (warn_level & 4))
cwarn( multiple_cats, NULL, 0L, NULL);
if ((nargs & VA_ARGS) && stdc_ver < 199901L && (warn_level & 2))
/* Variable arg macro is the spec of C99, not C90 nor C++98 */
cwarn( "Variable argument macro is defined", /* _W2_ */
NULL, 0L, NULL);
}
return TRUE;
}
static char * is_formal(
const char * name,
int conv /* Convert to magic number? */
)
/*
* If the identifier is a formal parameter, save the MAC_PARM and formal
* offset, returning the advanced pointer into the replacement text.
* Else, return NULL.
*/
{
char * repl_cur;
const char * va_arg = "__VA_ARGS__";
PARM parm;
size_t len;
int i;
len = strlen( name);
for (i = 0; i < (nargs & ~AVA_ARGS); i++) { /* For each parameter */
parm = parms[ i];
if ((len == parm.len
/* Note: parms[].name are comma separated */
&& memcmp( name, parm.name, parm.len) == 0)
|| (standard && (nargs & VA_ARGS)
&& i == (nargs & ~AVA_ARGS) - 1 && conv
&& str_eq( name, va_arg))) { /* __VA_ARGS__ */
/* If it's known */
#if COMPILER == GNUC
if (gcc2_va_arg && str_eq( name, va_arg))
return NULL; /* GCC2 variadic macro */
#endif
if (conv) {
repl_cur = token_p; /* Overwrite on the name*/
*repl_cur++ = MAC_PARM; /* Save the signal */
*repl_cur++ = i + 1; /* Save the parm number */
return repl_cur; /* Return "gotcha" */
} else {
return parm.name; /* Duplicate parm name */
}
}
}
return NULL; /* Not a formal param */
}
static char * def_stringization( char * repl_cur)
/*
* Define token stringization.
* We store a magic cookie (which becomes surrouding " on expansion) preceding
* the parameter as an operand of # operator.
* Return the current pointer into replacement text if the token following #
* is a parameter name, else return NULL.
*/
{
int c;
char * temp;
*repl_cur++ = ST_QUOTE; /* Prefix */
if (char_type[ c = get_ch()] & HSP) { /* There is a space */
*repl_cur++ = ' ';
while (char_type[ c = get_ch()] & HSP) /* Skip excessive spaces*/
;
}
token_p = repl_cur; /* Remember the pointer */
if (scan_token( c, &repl_cur, repl_end) == NAM) {
if ((temp = is_formal( identifier, TRUE)) != NULL) {
repl_cur = temp;
return repl_cur;
}
}
cerror( "Not a formal parameter \"%s\"", token_p, 0L, NULL); /* _E_ */
return NULL;
}
static char * mgtoken_save( const char * macroname)
/*
* A magic cookie is inserted if the token is identical to the macro name,
* so the expansion doesn't recurse.
* Return the advanced pointer into the replacement text or NULL.
*/
{
char * repl_cur;
if (str_eq( macroname, identifier)) { /* Macro name in body */
repl_cur = token_p; /* Overwrite on token */
*repl_cur++ = DEF_MAGIC; /* Save magic marker */
repl_cur = stpcpy( repl_cur, identifier);
/* And save the token */
return repl_cur;
} else {
return NULL;
}
}
static char * str_parm_scan( char * string_end)
/*
* String parameter scan.
* This code -- if enabled -- recognizes a formal parameter in a string
* literal or in a character constant.
* #define foo(bar, v) printf("%bar\n", v)
* foo( d, i)
* expands to:
* printf("%d\n", i)
* str_parm_scan() return the advanced pointer into the replacement text.
* This has been superceded by # stringizing and string concatenation.
* This routine is called only in OLD_PREP mode.
*/
{
int delim;
int c;
char * tp;
char * wp; /* Pointer into the quoted literal */
delim = *token_p;
unget_string( ++token_p, NULL);
/* Pseudo-token-parsing in a string literal */
wp = token_p;
while ((c = get_ch()) != delim) {
token_p = wp;
if (scan_token( c, &wp, string_end) != NAM)
continue;
if ((tp = is_formal( token_p, TRUE)) != NULL)
wp = tp;
}
*wp++ = delim;
return wp;
}
static void do_undef( void)
/*
* Remove the symbol from the defined list.
* Called from directive().
*/
{
DEFBUF * defp;
int c;
if ((c = skip_ws()) == '\n') {
cerror( no_ident, NULL, 0L, NULL);
unget_ch();
return;
}
if (scan_token( c, (workp = work_buf, &workp), work_end) != NAM) {
cerror( not_ident, work_buf, 0L, NULL);
skip_nl();
unget_ch();
} else {
if ((defp = look_id( identifier)) == NULL) {
if (warn_level & 8)
cwarn( "\"%s\" wasn't defined" /* _W8_ */
, identifier, 0L, NULL);
} else if (standard && (defp->nargs <= DEF_NOARGS_STANDARD
/* Standard predef */
|| defp->nargs == DEF_PRAGMA)) {
/* _Pragma() pseudo-macro */
cerror( "\"%s\" shouldn't be undefined" /* _E_ */
, identifier, 0L, NULL);
} else if (standard) {
c = skip_ws();
unget_ch();
if (c != '\n') /* Trailing junk */
return;
else
undefine( identifier);
} else {
undefine( identifier);
}
}
}
/*
* C P P S y m b o l T a b l e s
*
* SBSIZE defines the number of hash-table slots for the symbol table.
* It must be a power of 2.
*/
/* Symbol table queue headers. */
static DEFBUF * symtab[ SBSIZE];
static long num_of_macro = 0;
#if MCPP_LIB
void init_directive( void)
/* Initialize static variables. */
{
num_of_macro = 0;
}
#endif
DEFBUF * look_id( const char * name)
/*
* Look for the identifier in the symbol table.
* If found, return the table pointer; Else return NULL.
*/
{
DEFBUF ** prevp;
int cmp;
prevp = look_prev( name, &cmp);
if (standard)
return ((cmp == 0 && (*prevp)->push == 0) ? *prevp : NULL);
else
return ((cmp == 0) ? *prevp : NULL);
}
DEFBUF ** look_prev(
const char * name, /* Name of the macro */
int * cmp /* Result of comparison */
)
/*
* Look for the place to insert the macro definition.
* Return a pointer to the previous member in the linked list.
*/
{
const char * np;
DEFBUF ** prevp;
DEFBUF * dp;
size_t s_name;
int hash;
for (hash = 0, np = name; *np != EOS; )
hash += *np++;
hash += s_name = (size_t)(np - name);
s_name++;
prevp = & symtab[ hash & SBMASK];
*cmp = -1; /* Initialize */
while ((dp = *prevp) != NULL) {
if ((*cmp = memcmp( dp->name, name, s_name)) >= 0)
break;
prevp = &dp->link;
}
return prevp;
}
DEFBUF * look_and_install(
const char * name, /* Name of the macro */
int numargs, /* The numbers of parms */
const char * parmnames, /* Names of parameters concatenated */
const char * repl /* Replacement text */
)
/*
* Look for the name and (re)define it.
* Returns a pointer to the definition block.
* Returns NULL if the symbol was Standard-predefined.
*/
{
DEFBUF ** prevp; /* Place to insert definition */
DEFBUF * defp; /* New definition block */
int cmp; /* Result of comparison of new name and old */
prevp = look_prev( name, &cmp);
defp = install_macro( name, numargs, parmnames, repl, prevp, cmp, 0);
return defp;
}
DEFBUF * install_macro(
const char * name, /* Name of the macro */
int numargs, /* The numbers of parms */
const char * parmnames, /* Names of parameters concatenated */
const char * repl, /* Replacement text */
DEFBUF ** prevp, /* The place to insert definition */
int cmp, /* Result of comparison of new name and old */
int predefine /* Predefined macro without leading '_' */
)
/*
* Enter this name in the lookup table.
* Returns a pointer to the definition block.
* Returns NULL if the symbol was Standard-predefined.
* Note that predefinedness can be specified by either of 'numargs' or
* 'predefine'.
*/
{
DEFBUF * dp;
DEFBUF * defp;
size_t s_name, s_parmnames, s_repl;
defp = *prevp; /* Old definition, if cmp == 0 */
if (cmp == 0 && defp->nargs < DEF_NOARGS - 1)
return NULL; /* Standard predefined */
if (parmnames == NULL || repl == NULL || (predefine && numargs > 0)
|| (predefine && predefine != DEF_NOARGS_PREDEF
&& predefine != DEF_NOARGS_PREDEF_OLD))
/* Shouldn't happen */
cfatal( "Bug: Illegal macro installation of \"%s\"" /* _F_ */
, name, 0L, NULL); /* Use "" instead of NULL */
s_name = strlen( name);
if (mcpp_mode == STD)
s_parmnames = strlen( parmnames) + 1;
else
s_parmnames = 0;
s_repl = strlen( repl) + 1;
dp = (DEFBUF *)
xmalloc( sizeof (DEFBUF) + s_name + s_parmnames + s_repl);
if (cmp || (standard && (*prevp)->push)) { /* New definition */
dp->link = defp; /* Insert to linked list */
*prevp = dp;
} else { /* Redefinition */
dp->link = defp->link; /* Replace old def with new */
*prevp = dp;
free( defp);
}
dp->nargs = predefine ? predefine : numargs;
if (standard) {
dp->push = 0;
dp->parmnames = (char *)dp + sizeof (DEFBUF) + s_name;
dp->repl = dp->parmnames + s_parmnames;
if (mcpp_mode == STD)
memcpy( dp->parmnames, parmnames, s_parmnames);
} else {
dp->repl = (char *)dp + sizeof (DEFBUF) + s_name;
}
memcpy( dp->name, name, s_name + 1);
memcpy( dp->repl, repl, s_repl);
/* Remember where the macro is defined */
dp->fname = cur_fullname; /* Full-path-list of current file */
dp->mline = src_line;
if (standard && cmp && ++num_of_macro == std_limits.n_macro + 1
&& std_limits.n_macro && (warn_level & 4))
/* '&& std_limits.n_macro' to avoid warning before initialization */
cwarn( "More than %.0s%ld macros defined" /* _W4_ */
, NULL , std_limits.n_macro, NULL);
return dp;
}
int undefine(
const char * name /* Name of the macro */
)
/*
* Delete the macro definition from the symbol table.
* Returns TRUE, if deleted;
* Else returns FALSE (when the macro was not defined or was Standard
* predefined).
*/
{
DEFBUF ** prevp; /* Preceding definition in list */
DEFBUF * dp; /* Definition to delete */
int cmp; /* 0 if defined, else not defined */
prevp = look_prev( name, &cmp);
dp = *prevp; /* Definition to delete */
if (cmp || dp->nargs <= DEF_NOARGS_STANDARD)
return FALSE; /* Not defined or Standard predefined */
if (standard && dp->push)
return FALSE; /* 'Pushed' macro */
*prevp = dp->link; /* Link the previous and the next */
if ((mcpp_debug & MACRO_CALL) && dp->mline) {
/* Notice this directive unless the macro is predefined */
mcpp_fprintf( OUT, "/*undef %ld*//*%s*/\n", src_line, dp->name);
wrong_line = TRUE;
}
free( dp); /* Delete the definition */
if (standard)
num_of_macro--;
return TRUE;
}
static void dump_repl(
const DEFBUF * dp,
FILE * fp,
int gcc2_va
)
/*
* Dump replacement text.
*/
{
int numargs = dp->nargs;
char * cp1;
size_t i;
int c;
const char * cp;
for (cp = dp->repl; (c = *cp++ & UCHARMAX) != EOS; ) {
switch (c) {
case MAC_PARM: /* Parameter */
c = (*cp++ & UCHARMAX) - 1;
if (standard) {
PARM parm = parms[ c];
if ((numargs & VA_ARGS) && c == (numargs & ~AVA_ARGS) - 1) {
mcpp_fputs( gcc2_va ? parm.name : "__VA_ARGS__"
, FP2DEST( fp));
/* gcc2_va is possible only in STD mode */
} else {
if (mcpp_mode == STD) {
for (i = 0, cp1 = parm.name; i < parm.len; i++)
mcpp_fputc( *cp1++, FP2DEST( fp));
} else {
mcpp_fputc( 'a' + c % 26, FP2DEST( fp));
if (c > 26)
mcpp_fputc( '0' + c / 26, FP2DEST( fp));
}
}
} else {
mcpp_fputc( 'a' + c % 26, FP2DEST( fp));
if (c > 26)
mcpp_fputc( '0' + c / 26, FP2DEST( fp));
}
break;
case DEF_MAGIC:
if (! standard)
mcpp_fputc( c, FP2DEST( fp));
/* Else skip */
break;
case CAT:
if (standard)
mcpp_fputs( "##", FP2DEST( fp));
else
mcpp_fputc( c, FP2DEST( fp));
break;
case ST_QUOTE:
if (standard)
mcpp_fputs( "#", FP2DEST( fp));
else
mcpp_fputc( c, FP2DEST( fp));
break;
case COM_SEP:
/*
* Though TOK_SEP coincides to COM_SEP, this cannot appear in
* Standard mode.
*/
if (mcpp_mode == OLD_PREP)
mcpp_fputs( "/**/", FP2DEST( fp));
break;
default:
mcpp_fputc( c, FP2DEST( fp));
break;
}
}
}
/*
* If the compiler is so-called "one-pass" compiler, compiler-predefined
* macros are commented out to avoid redefinition.
*/
#if ONE_PASS
#define CAN_REDEF DEF_NOARGS
#else
#define CAN_REDEF DEF_NOARGS_PREDEF
#endif
void dump_a_def(
const char * why,
const DEFBUF * dp,
int newdef, /* TRUE if parmnames are currently in parms[] */
int comment, /* Show location of the definition in comment */
FILE * fp
)
/*
* Dump a macro definition.
*/
{
char * cp, * cp1;
int numargs = dp->nargs & ~AVA_ARGS;
int commented; /* To be commented out */
int gcc2_va = FALSE; /* GCC2-spec variadic */
int i;
if (standard && numargs == DEF_PRAGMA) /* _Pragma pseudo-macro */
return;
if ((numargs < CAN_REDEF) || (standard && dp->push))
commented = TRUE;
else
commented = FALSE;
if (! comment && commented) /* For -dM option */
return;
if (why)
mcpp_fprintf( FP2DEST( fp), "%s \"%s\" defined as: ", why, dp->name);
mcpp_fprintf( FP2DEST( fp), "%s#define %s", commented ? "/* " : "",
dp->name); /* Macro name */
if (numargs >= 0) { /* Parameter list */
if (mcpp_mode == STD) {
const char * appendix = null;
if (! newdef) {
/* Make parms[] for dump_repl() */
for (i = 0, cp = dp->parmnames; i < numargs;
i++, cp = cp1 + 1) {
if ((cp1 = strchr( cp, ',')) == NULL) /* The last arg */
parms[ i].len = strlen( cp);
else
parms[ i].len = (size_t) (cp1 - cp);
parms[ i].name = cp;
}
}
#if COMPILER == GNUC
if ((dp->nargs & VA_ARGS)
&& memcmp( parms[ numargs - 1].name, "...", 3) != 0) {
appendix = "..."; /* Append ... so as to become 'args...' */
gcc2_va = TRUE;
}
#endif
mcpp_fprintf( FP2DEST( fp), "(%s%s)", dp->parmnames, appendix);
} else {
if (newdef) {
mcpp_fprintf( FP2DEST( fp), "(%s)", parms[ 0].name);
} else if (numargs == 0) {
mcpp_fputs( "()", FP2DEST( fp));
} else {
/* Print parameter list automatically made as: */
/* a, b, c, ..., a0, b0, c0, ..., a1, b1, c1, ... */
mcpp_fputc( '(', FP2DEST( fp));
for (i = 0; i < numargs; i++) { /* Make parameter list */
mcpp_fputc( 'a' + i % 26, FP2DEST( fp));
if (i >= 26)
mcpp_fputc( '0' + i / 26, FP2DEST( fp));
if (i + 1 < numargs)
mcpp_fputc( ',', FP2DEST( fp));
}
mcpp_fputc( ')', FP2DEST( fp));
}
}
}
if (*dp->repl) {
mcpp_fputc( ' ', FP2DEST( fp));
dump_repl( dp, fp, gcc2_va); /* Replacement text */
}
if (commented)
/* Standard predefined or one-pass-compiler-predefined */
mcpp_fputs( " */", FP2DEST( fp));
if (comment) /* Not -dM option */
mcpp_fprintf( FP2DEST( fp), " \t/* %s:%ld\t*/", dp->fname, dp->mline);
mcpp_fputc( '\n', FP2DEST( fp));
}
void dump_def(
int comment, /* Location of definition in comment */
int K_opt /* -K option is specified */
)
/*
* Dump all the current macro definitions to output stream.
*/
{
DEFBUF * dp;
DEFBUF ** symp;
sharp( NULL, 0); /* Report the current source file & line */
if (comment)
mcpp_fputs( "/* Currently defined macros. */\n", OUT);
for (symp = symtab; symp < &symtab[ SBSIZE]; symp++) {
if ((dp = *symp) != NULL) {
do {
if (K_opt)
mcpp_fprintf( OUT, "/*m%s*/\n", dp->name);
else
dump_a_def( NULL, dp, FALSE, comment, fp_out);
} while ((dp = dp->link) != NULL);
}
}
wrong_line = TRUE; /* Line number is out of sync */
}
#if MCPP_LIB
void clear_symtable( void)
/*
* Free all the macro definitions.
*/
{
DEFBUF * next;
DEFBUF * dp;
DEFBUF ** symp;
for (symp = symtab; symp < &symtab[ SBSIZE]; symp++) {
for (next = *symp; next != NULL; ) {
dp = next;
next = dp->link;
free( dp); /* Free the symbol */
}
*symp = NULL;
}
}
#endif
|