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 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790
|
// This file implements the C preprocessor.
//
// The preprocessor takes a list of tokens as an input and returns a
// new list of tokens as an output.
//
// The preprocessing language is designed in such a way that that's
// guaranteed to stop even if there is a recursive macro.
// Informally speaking, a macro is applied only once for each token.
// That is, if a macro token T appears in a result of direct or
// indirect macro expansion of T, T won't be expanded any further.
// For example, if T is defined as U, and U is defined as T, then
// token T is expanded to U and then to T and the macro expansion
// stops at that point.
//
// To achieve the above behavior, we attach for each token a set of
// macro names from which the token is expanded. The set is called
// "hideset". Hideset is initially empty, and every time we expand a
// macro, the macro name is added to the resulting tokens' hidesets.
//
// The above macro expansion algorithm is explained in this document
// written by Dave Prossor, which is used as a basis for the
// standard's wording:
// https://github.com/rui314/chibicc/wiki/cpp.algo.pdf
#include "chibicc.h"
#define PREPROCESS_C "preprocess.c"
typedef struct MacroParam MacroParam;
struct MacroParam
{
MacroParam *next;
char *name;
};
typedef struct MacroArg MacroArg;
struct MacroArg
{
MacroArg *next;
char *name;
bool is_va_args;
Token *tok;
};
typedef Token *macro_handler_fn(Token *);
typedef struct Macro Macro;
struct Macro
{
char *name;
bool is_objlike; // Object-like or function-like or attribut-like
MacroParam *params;
char *va_args_name;
Token *body;
macro_handler_fn *handler;
};
// `#if` can be nested, so we use a stack to manage nested `#if`s.
typedef struct CondIncl CondIncl;
struct CondIncl
{
CondIncl *next;
enum
{
IN_THEN,
IN_ELIF,
IN_ELSE
} ctx;
Token *tok;
bool included;
};
typedef struct Hideset Hideset;
struct Hideset
{
Hideset *next;
char *name;
};
static HashMap macros;
static CondIncl *cond_incl;
static HashMap pragma_once;
static int include_next_idx;
//ISS-142
extern bool opt_E;
extern bool opt_fbuiltin;
extern Context *ctx;
static Token *preprocess2(Token *tok);
static Macro *find_macro(Token *tok);
static void join_adjacent_string_literals(Token *tok);
static Token *paste(Token *lhs, Token *rhs);
static bool file_exists_in_include_path(const char *filename);
//begin
static bool is_hash(Token *tok)
// [https://www.sigbus.info/n1570#6.10.3.4p3] tok->origin is checked here
// because "#" can appear in object-like macro, and after expansion of that
// macro, it's not a preprocessing directive even it resembles one.
// For example:
// 1. #define H #
// 2. #define I include
// 3.
// 4. H I <stdio.h>
// Line 4 produces these tokens:
// {#} {include} {<stdio.h>}
// which are not treated as preprocessing directive.
{
return tok->at_bol && equal(tok, "#") && !tok->origin;
}
// Some preprocessor directives such as #include allow extraneous
// tokens before newline. This function skips such tokens.
static Token *skip_line(Token *tok)
{
if (tok->at_bol)
return tok;
warn_tok(tok, "extra token");
while (!tok->at_bol)
tok = tok->next;
return tok;
}
static Token *copy_token(Token *tok)
{
Token *t = calloc(1, sizeof(Token));
if (t == NULL)
error("%s: %s:%d: error: in copy_token : t is null", PREPROCESS_C, __FILE__, __LINE__);
*t = *tok;
t->next = NULL;
return t;
}
static Token *new_eof(Token *tok)
{
Token *t = copy_token(tok);
t->at_bol = true;
t->kind = TK_EOF;
t->len = 0;
return t;
}
static Hideset *new_hideset(char *name)
{
Hideset *hs = calloc(1, sizeof(Hideset));
if (hs == NULL)
error("%s: %s:%d: error: in new_hideset : hs is null", PREPROCESS_C, __FILE__, __LINE__);
hs->name = name;
return hs;
}
static Hideset *hideset_union(Hideset *hs1, Hideset *hs2)
{
Hideset head = {};
Hideset *cur = &head;
for (; hs1; hs1 = hs1->next)
cur = cur->next = new_hideset(hs1->name);
cur->next = hs2;
return head.next;
}
static bool hideset_contains(Hideset *hs, char *s, int len)
{
for (; hs; hs = hs->next)
if (strlen(hs->name) == len && !strncmp(hs->name, s, len))
return true;
return false;
}
static Hideset *hideset_intersection(Hideset *hs1, Hideset *hs2)
{
Hideset head = {};
Hideset *cur = &head;
for (; hs1; hs1 = hs1->next)
if (hideset_contains(hs2, hs1->name, strlen(hs1->name)))
cur = cur->next = new_hideset(hs1->name);
return head.next;
}
static Token *add_hideset(Token *tok, Hideset *hs)
{
Token head = {};
Token *cur = &head;
for (; tok; tok = tok->next)
{
Token *t = copy_token(tok);
t->hideset = hideset_union(t->hideset, hs);
cur = cur->next = t;
}
return head.next;
}
// Append tok2 to the end of tok1.
static Token *append(Token *tok1, Token *tok2)
{
if (tok1->kind == TK_EOF)
return tok2;
Token head = {};
Token *cur = &head;
for (; tok1->kind != TK_EOF; tok1 = tok1->next)
cur = cur->next = copy_token(tok1);
cur->next = tok2;
return head.next;
}
static Token *skip_cond_incl2(Token *tok)
{
while (tok->kind != TK_EOF)
{
if (is_hash(tok) &&
(equal(tok->next, "if") || equal(tok->next, "ifdef") ||
equal(tok->next, "ifndef")))
{
tok = skip_cond_incl2(tok->next->next);
continue;
}
if (is_hash(tok) && equal(tok->next, "endif"))
return tok->next->next;
tok = tok->next;
}
return tok;
}
// Skip until next `#else`, `#elif` or `#endif`.
// Nested `#if` and `#endif` are skipped.
static Token *skip_cond_incl(Token *tok)
{
while (tok->kind != TK_EOF)
{
if (is_hash(tok) &&
(equal(tok->next, "if") || equal(tok->next, "ifdef") ||
equal(tok->next, "ifndef")))
{
tok = skip_cond_incl2(tok->next->next);
continue;
}
if (is_hash(tok) &&
(equal(tok->next, "elif") || equal(tok->next, "else") ||
equal(tok->next, "endif")))
break;
tok = tok->next;
}
return tok;
}
// Double-quote a given string and returns it.
static char *quote_string(char *str)
{
int bufsize = 3;
for (int i = 0; str[i]; i++)
{
if (str[i] == '\\' || str[i] == '"')
bufsize++;
bufsize++;
}
char *buf = calloc(1, bufsize);
if (buf == NULL)
error("%s: %s:%d: error: in quote_string : buf is null", PREPROCESS_C, __FILE__, __LINE__);
char *p = buf;
*p++ = '"';
for (int i = 0; str[i]; i++)
{
if (str[i] == '\\' || str[i] == '"')
*p++ = '\\';
*p++ = str[i];
}
*p++ = '"';
*p++ = '\0';
return buf;
}
static Token *new_str_token(char *str, Token *tmpl)
{
char *buf = quote_string(str);
return tokenize(new_file(tmpl->file->name, tmpl->file->file_no, buf));
}
// Copy all tokens until the next newline, terminate them with
// an EOF token and then returns them. This function is used to
// create a new list of tokens for `#if` arguments.
static Token *copy_line(Token **rest, Token *tok)
{
Token head = {};
Token *cur = &head;
for (; !tok->at_bol; tok = tok->next)
cur = cur->next = copy_token(tok);
cur->next = new_eof(tok);
*rest = tok;
return head.next;
}
static Token *new_num_token(int val, Token *tmpl)
{
char *buf = format("%d\n", val);
if (!buf)
error_tok(tmpl, "%s: in new_num_token : buf is null", PREPROCESS_C);
return tokenize(new_file(tmpl->file->name, tmpl->file->file_no, buf));
}
static Token *read_const_expr(Token **rest, Token *tok)
{
tok = copy_line(rest, tok);
Token head = {};
Token *cur = &head;
while (tok->kind != TK_EOF)
{
// "defined(foo)" or "defined foo" becomes "1" if macro "foo"
// is defined. Otherwise "0".
if (equal(tok, "defined") || equal(tok, "__has_attribute"))
{
Token *start = tok;
bool has_paren = consume(&tok, tok->next, "(");
if (tok->kind != TK_IDENT)
error_tok(start, "%s: in read_const_expr : macro name must be an identifier", PREPROCESS_C);
Macro *m = find_macro(tok);
tok = tok->next;
if (has_paren) {
SET_CTX(ctx);
tok = skip(tok, ")", ctx);
}
cur = cur->next = new_num_token(m ? 1 : 0, start);
continue;
} else if (equal(tok, "__has_include")) {
Token *start = tok;
bool has_paren = consume(&tok, tok->next, "(");
if (tok->kind != TK_STR && !equal(tok, "<"))
error_tok(start, "%s: in read_const_expr : __has_include expects a filename", PREPROCESS_C);
char filename[PATH_MAX];
if (tok->kind == TK_STR) {
// __has_include("header.h")
snprintf(filename, sizeof(filename), "%s", tok->str);
filename[tok->len] = '\0';
tok = tok->next;
} else {
// __has_include(<header.h>)
tok = tok->next;
const char *start_str = tok->str;
int len = 0;
while (!equal(tok, ">") && tok->kind != TK_EOF) {
len += tok->len;
tok = tok->next;
}
if (!equal(tok, ">"))
error_tok(start, "%s: in read_const_expr : expected closing > in __has_include", PREPROCESS_C);
snprintf(filename, sizeof(filename), "%.*s", len, start_str);
tok = tok->next; // consume '>'
}
if (has_paren) {
SET_CTX(ctx);
tok = skip(tok, ")", ctx);
}
bool found = file_exists_in_include_path(filename);
cur = cur->next = new_num_token(found ? 1 : 0, start);
continue;
}
cur = cur->next = tok;
tok = tok->next;
}
cur->next = tok;
return head.next;
}
// Read and evaluate a constant expression.
static long eval_const_expr(Token **rest, Token *tok)
{
Token *start = tok;
Token *expr = read_const_expr(rest, tok->next);
expr = preprocess2(expr);
if (expr->kind == TK_EOF)
error_tok(start, "%s: in eval_const_expr : no expression", PREPROCESS_C);
// [https://www.sigbus.info/n1570#6.10.1p4] The standard requires
// we replace remaining non-macro identifiers with "0" before
// evaluating a constant expression. For example, `#if foo` is
// equivalent to `#if 0` if foo is not defined.
for (Token *t = expr; t->kind != TK_EOF; t = t->next)
{
if (t->kind == TK_IDENT)
{
Token *next = t->next;
*t = *new_num_token(0, t);
t->next = next;
}
}
// Convert pp-numbers to regular numbers
convert_pp_tokens(expr);
if (expr->ty && is_flonum(expr->ty))
error_tok(expr, "%s: in eval_const_expr : floating constant in preprocessor expression", PREPROCESS_C);
Token *rest2;
long val = const_expr(&rest2, expr);
if (rest2->kind != TK_EOF)
error_tok(rest2, "%s: in eval_const_expr : extra token", PREPROCESS_C);
return val;
}
static CondIncl *push_cond_incl(Token *tok, bool included)
{
CondIncl *ci = calloc(1, sizeof(CondIncl));
if (ci == NULL)
error("%s: %s:%d: error: in push_cond_incl : ci is null", PREPROCESS_C, __FILE__, __LINE__);
ci->next = cond_incl;
ci->ctx = IN_THEN;
ci->tok = tok;
ci->included = included;
cond_incl = ci;
return ci;
}
static Macro *find_macro(Token *tok)
{
if (tok->kind != TK_IDENT)
return NULL;
return hashmap_get2(¯os, tok->loc, tok->len);
}
static Macro *add_macro(char *name, bool is_objlike, Token *body)
{
Macro *m = calloc(1, sizeof(Macro));
if (m == NULL)
error("%s: %s:%d: error: in add_macro : m is null", PREPROCESS_C, __FILE__, __LINE__);
m->name = name;
m->is_objlike = is_objlike;
m->body = body;
hashmap_put(¯os, name, m);
return m;
}
static MacroParam *read_macro_params(Token **rest, Token *tok, char **va_args_name)
{
MacroParam head = {};
MacroParam *cur = &head;
while (!equal(tok, ")"))
{
if (cur != &head) {
SET_CTX(ctx);
tok = skip(tok, ",", ctx);
}
if (equal(tok, "..."))
{
*va_args_name = "__VA_ARGS__";
SET_CTX(ctx);
*rest = skip(tok->next, ")", ctx);
return head.next;
}
if (tok->kind != TK_IDENT)
error_tok(tok, "%s in read_macro_params : expected an identifier", PREPROCESS_C);
if (equal(tok->next, "..."))
{
*va_args_name = strndup(tok->loc, tok->len);
SET_CTX(ctx);
*rest = skip(tok->next->next, ")", ctx);
return head.next;
}
MacroParam *m = calloc(1, sizeof(MacroParam));
if (m == NULL)
error("%s: %s:%d: error: in read_macro_params : m is null", PREPROCESS_C, __FILE__, __LINE__);
m->name = strndup(tok->loc, tok->len);
cur = cur->next = m;
tok = tok->next;
}
*rest = tok->next;
return head.next;
}
static void read_macro_definition(Token **rest, Token *tok)
{
Token head = {};
Token *cur = &head;
if (tok->kind != TK_IDENT)
error_tok(tok, "%s: in read_macro_definition : macro name must be an identifier", PREPROCESS_C);
char *name = strndup(tok->loc, tok->len);
tok = tok->next;
if (!tok->has_space && equal(tok, "("))
{
// Function-like macro
char *va_args_name = NULL;
MacroParam *params = read_macro_params(&tok, tok->next, &va_args_name);
//Macro *m = add_macro(name, false, copy_line(rest, tok));
while (!tok->at_bol) {
if (equal(tok, "##")) {
if (cur == &head)
error_tok(tok, "%s: in read_macro_definition : '##' cannot appear at start of replacement list", PREPROCESS_C);
if (tok->next->at_bol)
error_tok(tok, "%s : in read_macro_definition : '##' cannot appear at end of replacement list", PREPROCESS_C);
cur = cur->next = copy_token(tok); // ##
cur = cur->next = copy_token(tok->next); // rhs
tok = tok->next->next;
continue;
}
cur = cur->next = copy_token(tok);
tok = tok->next;
}
cur->next = new_eof(tok);
*rest = tok;
Macro *m = add_macro(name, false, head.next);
m->params = params;
m->va_args_name = va_args_name;
}
else
{
// Object-like macro
while (!tok->at_bol) {
if (equal(tok, "##")) {
if (cur == &head)
error_tok(tok, "%s: in read_macro_definition : '##' cannot appear at start of replacement list", PREPROCESS_C);
if (tok->next->at_bol)
error_tok(tok, "%s: in read_macro_definition : '##' cannot appear at end of replacement list", PREPROCESS_C);
*cur = *paste(cur, tok->next);
tok = tok->next->next;
continue;
}
cur = cur->next = copy_token(tok);
tok = tok->next;
}
cur->next = new_eof(tok);
*rest = tok;
add_macro(name, true, head.next);
//add_macro(name, true, copy_line(rest, tok));
}
}
static MacroArg *read_macro_arg_one(Token **rest, Token *tok, bool read_rest)
{
Token head = {};
Token *cur = &head;
int level = 0;
for (;;)
{
if (level == 0 && equal(tok, ")"))
break;
if (level == 0 && !read_rest && equal(tok, ","))
break;
if (tok->kind == TK_EOF)
error_tok(tok, "%s: in read_macro_arg_one : premature end of input", PREPROCESS_C);
if (equal(tok, "("))
level++;
else if (equal(tok, ")"))
level--;
cur = cur->next = copy_token(tok);
tok = tok->next;
}
cur->next = new_eof(tok);
MacroArg *arg = calloc(1, sizeof(MacroArg));
if (arg == NULL)
error("%s: %s:%d: error: in read_macro_arg_one : arg is null", PREPROCESS_C, __FILE__, __LINE__);
arg->tok = head.next;
*rest = tok;
return arg;
}
static MacroArg *
read_macro_args(Token **rest, Token *tok, MacroParam *params, char *va_args_name)
{
Token *start = tok;
tok = tok->next->next;
MacroArg head = {};
MacroArg *cur = &head;
MacroParam *pp = params;
for (; pp; pp = pp->next)
{
if (cur != &head) {
SET_CTX(ctx);
tok = skip(tok, ",", ctx);
}
cur = cur->next = read_macro_arg_one(&tok, tok, false);
cur->name = pp->name;
}
if (va_args_name)
{
MacroArg *arg;
if (equal(tok, ")"))
{
arg = calloc(1, sizeof(MacroArg));
if (arg == NULL)
error("%s: %s:%d: error: in read_macro_args : arg is null", PREPROCESS_C, __FILE__, __LINE__);
arg->tok = new_eof(tok);
}
else
{
if (pp != params) {
SET_CTX(ctx);
tok = skip(tok, ",", ctx);
}
arg = read_macro_arg_one(&tok, tok, true);
}
arg->name = va_args_name;
;
arg->is_va_args = true;
cur = cur->next = arg;
}
else if (pp)
{
error_tok(start, "%s: in read_macro_args : too many arguments", PREPROCESS_C);
}
SET_CTX(ctx);
skip(tok, ")",ctx);
*rest = tok;
return head.next;
}
static MacroArg *find_arg(MacroArg *args, Token *tok)
{
for (MacroArg *ap = args; ap; ap = ap->next)
if (tok->len == strlen(ap->name) && !strncmp(tok->loc, ap->name, tok->len))
return ap;
return NULL;
}
// Concatenates all tokens in `tok` and returns a new string.
static char *join_tokens(Token *tok, Token *end)
{
// Compute the length of the resulting token.
int len = 1;
for (Token *t = tok; t != end && t->kind != TK_EOF; t = t->next)
{
if (t != tok && t->has_space)
len++;
len += t->len;
}
char *buf = calloc(1, len);
if (buf == NULL)
error("%s: %s:%d: error: in join_tokens : buf is null", PREPROCESS_C, __FILE__, __LINE__);
// Copy token texts.
int pos = 0;
for (Token *t = tok; t != end && t->kind != TK_EOF; t = t->next)
{
if (t != tok && t->has_space)
buf[pos++] = ' ';
strncpy(buf + pos, t->loc, t->len);
pos += t->len;
}
buf[pos] = '\0';
return buf;
}
// Concatenates all tokens in `arg` and returns a new string token.
// This function is used for the stringizing operator (#).
static Token *stringize(Token *hash, Token *arg)
{
// Create a new string token. We need to set some value to its
// source location for error reporting function, so we use a macro
// name token as a template.
char *s = join_tokens(arg, NULL);
return new_str_token(s, hash);
}
// Concatenate two tokens to create a new token.
static Token *paste(Token *lhs, Token *rhs)
{
// Paste the two tokens.
char *buf = format("%.*s%.*s", lhs->len, lhs->loc, rhs->len, rhs->loc);
// Tokenize the resulting string.
Token *tok = tokenize(new_file(lhs->file->name, lhs->file->file_no, buf));
// if (tok->next->kind != TK_EOF)
// error_tok(lhs, "%s: in paste : pasting forms '%s', an invalid token", PREPROCESS_C, buf);
if (tok->kind == TK_EOF || tok->next->kind != TK_EOF) {
error_tok(lhs, "%s: in paste : invalid preprocessing token '%s' produced by pasting "
"'%.*s' and '%.*s'", PREPROCESS_C, buf, lhs->len, lhs->loc, rhs->len,
rhs->loc);
}
return tok;
}
static bool has_varargs(MacroArg *args)
{
for (MacroArg *ap = args; ap; ap = ap->next)
if (!strcmp(ap->name, "__VA_ARGS__"))
return ap->tok->kind != TK_EOF;
return false;
}
// Replace func-like macro parameters with given arguments.
static Token *subst(Macro *m, MacroArg *args)
{
Token head = {};
Token *cur = &head;
Token *tok = m->body;
while (tok->kind != TK_EOF)
{
// "#" followed by a parameter is replaced with stringized actuals.
if (equal(tok, "#"))
{
MacroArg *arg = find_arg(args, tok->next);
if (!arg)
error_tok(tok->next, "%s: in subst : '#' is not followed by a macro parameter", PREPROCESS_C);
cur = cur->next = stringize(tok, arg->tok);
tok = tok->next->next;
continue;
}
// [GNU] If __VA_ARG__ is empty, `,##__VA_ARGS__` is expanded
// to the empty token list. Otherwise, its expaned to `,` and
// __VA_ARGS__.
if (equal(tok, ",") && equal(tok->next, "##"))
{
MacroArg *arg = find_arg(args, tok->next->next);
if (arg && arg->is_va_args)
{
if (arg->tok->kind == TK_EOF)
{
tok = tok->next->next->next;
}
else
{
cur = cur->next = copy_token(tok);
tok = tok->next->next;
}
continue;
}
}
if (equal(tok, "##"))
{
MacroArg *arg = find_arg(args, tok->next);
if (arg)
{
if (arg->tok->kind != TK_EOF)
{
//*cur = *paste(cur, arg->tok);
if (cur == &head)
cur = cur->next = copy_token(arg->tok);
else
*cur = *paste(cur, arg->tok);
for (Token *t = arg->tok->next; t->kind != TK_EOF; t = t->next)
cur = cur->next = copy_token(t);
}
tok = tok->next->next;
continue;
}
*cur = *paste(cur, tok->next);
tok = tok->next->next;
continue;
}
MacroArg *arg = find_arg(args, tok);
if (arg && equal(tok->next, "##"))
{
Token *rhs = tok->next->next;
if (arg->tok->kind == TK_EOF)
{
MacroArg *arg2 = find_arg(args, rhs);
if (arg2)
{
for (Token *t = arg2->tok; t->kind != TK_EOF; t = t->next)
cur = cur->next = copy_token(t);
}
else
{
cur = cur->next = copy_token(rhs);
}
tok = rhs->next;
continue;
}
for (Token *t = arg->tok; t->kind != TK_EOF; t = t->next)
cur = cur->next = copy_token(t);
tok = tok->next;
continue;
}
// If __VA_ARG__ is empty, __VA_OPT__(x) is expanded to the
// empty token list. Otherwise, __VA_OPT__(x) is expanded to x.
if (equal(tok, "__VA_OPT__") && equal(tok->next, "("))
{
MacroArg *arg = read_macro_arg_one(&tok, tok->next->next, true);
if (has_varargs(args))
for (Token *t = arg->tok; t->kind != TK_EOF; t = t->next)
cur = cur->next = t;
SET_CTX(ctx);
tok = skip(tok, ")", ctx);
continue;
}
// Handle a macro token. Macro arguments are completely macro-expanded
// before they are substituted into a macro body.
if (arg)
{
Token *t = preprocess2(arg->tok);
// t->at_bol = tok->at_bol;
t->has_space = tok->has_space;
for (; t->kind != TK_EOF; t = t->next)
cur = cur->next = copy_token(t);
tok = tok->next;
continue;
}
// Handle a non-macro token.
cur = cur->next = copy_token(tok);
tok = tok->next;
continue;
}
cur->next = tok;
return head.next;
}
// If tok is a macro, expand it and return true.
// Otherwise, do nothing and return false.
static bool expand_macro(Token **rest, Token *tok)
{
if (hideset_contains(tok->hideset, tok->loc, tok->len))
return false;
Macro *m = find_macro(tok);
if (!m)
return false;
// Built-in dynamic macro application such as __LINE__
if (m->handler)
{
*rest = m->handler(tok);
(*rest)->next = tok->next;
return true;
}
// Object-like macro application
if (m->is_objlike)
{
Hideset *hs = hideset_union(tok->hideset, new_hideset(m->name));
Token *body = add_hideset(m->body, hs);
for (Token *t = body; t->kind != TK_EOF; t = t->next)
{
t->origin = tok;
}
*rest = append(body, tok->next);
//(*rest)->at_bol = tok->at_bol;
(*rest)->has_space = tok->has_space;
return true;
}
// If a funclike macro token is not followed by an argument list,
// treat it as a normal identifier.
if (!equal(tok->next, "("))
return false;
// Function-like macro application
Token *macro_token = tok;
MacroArg *args = read_macro_args(&tok, tok, m->params, m->va_args_name);
Token *rparen = tok;
// Tokens that consist a func-like macro invocation may have different
// hidesets, and if that's the case, it's not clear what the hideset
// for the new tokens should be. We take the interesection of the
// macro token and the closing parenthesis and use it as a new hideset
// as explained in the Dave Prossor's algorithm.
Hideset *hs = hideset_intersection(macro_token->hideset, rparen->hideset);
hs = hideset_union(hs, new_hideset(m->name));
if (m->body->kind != TK_EOF) { // If replacement list is not empty
Token *body = subst(m, args);
body = add_hideset(body, hs);
for (Token *t = body; t->kind != TK_EOF; t = t->next) {
t->origin = macro_token;
}
*rest = append(body, tok->next);
(*rest)->at_bol = macro_token->at_bol;
(*rest)->has_space = macro_token->has_space;
} else {
*rest = tok->next;
}
return true;
}
char *search_include_paths(char *filename)
{
if (filename[0] == '/')
return filename;
static HashMap cache;
char *cached = hashmap_get(&cache, filename);
if (cached)
return cached;
// Search a file from the include paths.
for (int i = 0; i < include_paths.len; i++)
{
char *path = format("%s/%s", include_paths.data[i], filename);
// printf("%s\n", path);
if (!file_exists(path))
continue;
hashmap_put(&cache, filename, path);
include_next_idx = i + 1;
return path;
}
return NULL;
}
static char *search_include_next(char *filename)
{
for (; include_next_idx < include_paths.len; include_next_idx++)
{
char *path = format("%s/%s", include_paths.data[include_next_idx], filename);
if (file_exists(path))
return path;
}
return NULL;
}
// Read an #include argument.
static char *read_include_filename(Token **rest, Token *tok, bool *is_dquote)
{
// Pattern 1: #include "foo.h"
if (tok->kind == TK_STR)
{
// A double-quoted filename for #include is a special kind of
// token, and we don't want to interpret any escape sequences in it.
// For example, "\f" in "C:\foo" is not a formfeed character but
// just two non-control characters, backslash and f.
// So we don't want to use token->str.
*is_dquote = true;
*rest = skip_line(tok->next);
return strndup(tok->loc + 1, tok->len - 2);
}
// Pattern 2: #include <foo.h>
if (equal(tok, "<"))
{
// Reconstruct a filename from a sequence of tokens between
// "<" and ">".
Token *start = tok;
// Find closing ">".
for (; !equal(tok, ">"); tok = tok->next)
if (tok->at_bol || tok->kind == TK_EOF) {
error_tok(tok, "%s: in read_include_filename : expected '>' %s", PREPROCESS_C, start->loc );
}
*is_dquote = false;
*rest = skip_line(tok->next);
return join_tokens(start->next, tok);
}
// Pattern 3: #include FOO
// In this case FOO must be macro-expanded to either
// a single string token or a sequence of "<" ... ">".
if (tok->kind == TK_IDENT)
{
Token *tok2 = preprocess2(copy_line(rest, tok));
return read_include_filename(&tok2, tok2, is_dquote);
}
error_tok(tok, "%s: in read_include_filename : expected a filename", PREPROCESS_C);
}
// Detect the following "include guard" pattern.
//
// #ifndef FOO_H
// #define FOO_H
// ...
// #endif
static char *detect_include_guard(Token *tok)
{
// Detect the first two lines.
if (!is_hash(tok) || !equal(tok->next, "ifndef"))
return NULL;
tok = tok->next->next;
if (tok->kind != TK_IDENT)
return NULL;
char *macro = strndup(tok->loc, tok->len);
tok = tok->next;
if (!is_hash(tok) || !equal(tok->next, "define") || !equal(tok->next->next, macro))
return NULL;
// Read until the end of the file.
while (tok->kind != TK_EOF)
{
if (!is_hash(tok))
{
tok = tok->next;
continue;
}
if (equal(tok->next, "endif") && tok->next->next->kind == TK_EOF)
return macro;
if (equal(tok, "if") || equal(tok, "ifdef") || equal(tok, "ifndef"))
tok = skip_cond_incl(tok->next);
else
tok = tok->next;
}
return NULL;
}
static Token *include_file(Token *tok, char *path, Token *filename_tok)
{
// Check for "#pragma once"
if (hashmap_get(&pragma_once, path))
return tok;
// If we read the same file before, and if the file was guarded
// by the usual #ifndef ... #endif pattern, we may be able to
// skip the file without opening it.
static HashMap include_guards;
char *guard_name = hashmap_get(&include_guards, path);
if (guard_name && hashmap_get(¯os, guard_name))
return tok;
Token *tok2 = tokenize_file(path);
if (!tok2)
error_tok(filename_tok, "%s: in include_file : %s: cannot open file: %s", PREPROCESS_C, path, strerror(errno));
guard_name = detect_include_guard(tok2);
if (guard_name)
hashmap_put(&include_guards, path, guard_name);
return append(tok2, tok);
}
static void print_macro(Macro *m) {
FILE *out = ofile ? ofile : stdout;
fprintf(out, "#define %s", m->name);
if (!m->is_objlike) {
fprintf(out, "(");
MacroParam *p = m->params;
bool first = true;
while (p) {
if (!first)
fprintf(out, ",");
fprintf(out, "%s", p->name);
first = false;
p = p->next;
}
if (m->va_args_name) {
if (!first)
fprintf(out, ",");
fprintf(out, "...");
}
fprintf(out, ")");
}
Token *tok = m->body;
if (tok && tok->kind != TK_EOF)
fprintf(out, " ");
for (; tok && tok->kind != TK_EOF; tok = tok->next) {
// Add space between tokens
fprintf(out, "%.*s", tok->len, tok->loc);
if (tok->next && tok->next->kind != TK_EOF)
fprintf(out, " ");
}
fprintf(out, "\n");
}
static Token *stdver_macro(Token *tok) {
if (opt_c99)
tok->val = 199901L;
else if (opt_c11)
tok->val = 201112L;
else if (opt_c17)
tok->val = 201710L;
else if (opt_c23)
tok->val = 202311L;
else if (opt_c89)
tok->val =0;
else
tok->val = 201112L;
tok->kind = TK_NUM;
tok->ty = ty_long;
return tok;
}
// Read #line arguments
static void read_line_marker(Token **rest, Token *tok)
{
Token *start = tok;
bool isReadLine = true;
tok = preprocess(copy_line(rest, tok), isReadLine);
// if (isDebug && f != NULL)
// print_debug_tokens(PREPROCESS_C, "read_line_marker", tok);
if (tok->kind != TK_NUM && tok->ty->kind != TY_INT)
error_tok(tok, "%s: in read_line_marker : invalid line marker", PREPROCESS_C);
// fix issue with negative number that cause Assembler less number than one
//start->file->line_delta = tok->val - start->line_no;
// fix from @fuhsnn Line control off by one
start->file->line_delta = tok->val - start->line_no - 1;
tok = tok->next;
if (tok->kind == TK_EOF)
return;
if (tok->kind != TK_STR)
error_tok(tok, "%s: in read_line_marker : filename expected", PREPROCESS_C);
start->file->display_name = tok->str;
}
// Visit all tokens in `tok` while evaluating preprocessing
// macros and directives.
static Token *preprocess2(Token *tok)
{
Token head = {};
Token *cur = &head;
while (tok->kind != TK_EOF)
{
// // // If it is a macro, expand it.
//if (expand_macro(&tok, tok))
if (tok->kind == TK_IDENT && !equal(tok, "__attribute__") && expand_macro(&tok, tok))
continue;
// Pass through if it is not a "#".
if (!is_hash(tok))
{
tok->line_delta = tok->file->line_delta;
tok->filename = tok->file->display_name;
cur = cur->next = tok;
tok = tok->next;
continue;
}
Token *start = tok;
tok = tok->next;
if (equal(tok, "include"))
{
bool is_dquote;
char *filename = read_include_filename(&tok, tok->next, &is_dquote);
if (isDebug)
printf("=====includes===%s\n", filename);
if (filename[0] != '/' && is_dquote)
{
char *path = format("%s/%s", dirname(strdup(start->file->name)), filename);
if (file_exists(path))
{
tok = include_file(tok, path, start->next->next);
continue;
}
}
char *path = search_include_paths(filename);
tok = include_file(tok, path ? path : filename, start->next->next);
continue;
}
if (equal(tok, "include_next"))
{
bool ignore;
char *filename = read_include_filename(&tok, tok->next, &ignore);
char *path = search_include_next(filename);
tok = include_file(tok, path ? path : filename, start->next->next);
continue;
}
if (equal(tok, "define"))
{
read_macro_definition(&tok, tok->next);
continue;
}
if (equal(tok, "undef"))
{
tok = tok->next;
if (tok->kind != TK_IDENT)
error_tok(tok, "%s: in preprocess2 : macro name must be an identifier", PREPROCESS_C);
Macro *m = find_macro(tok);
if (m)
undef_macro(m->name);
//undef_macro(strndup(tok->loc, tok->len));
tok = skip_line(tok->next);
continue;
}
if (equal(tok, "if"))
{
long val = eval_const_expr(&tok, tok);
push_cond_incl(start, val);
if (!val)
tok = skip_cond_incl(tok);
continue;
}
if (equal(tok, "ifdef"))
{
if (tok->next->kind != TK_IDENT)
error_tok(tok->next, "%s: in preprocess2 : no macro name given in #ifdef directive", PREPROCESS_C);
// bool defined = find_macro(tok->next);
// push_cond_incl(tok, defined);
Macro *defined = find_macro(tok->next);
push_cond_incl(tok, !!defined);
tok = skip_line(tok->next->next);
if (!defined)
tok = skip_cond_incl(tok);
continue;
}
if (equal(tok, "ifndef"))
{
//bool defined = find_macro(tok->next);
if (tok->next->kind != TK_IDENT)
error_tok(tok->next, "%s: in preprocess2 : no macro name given in #ifndef directive", PREPROCESS_C);
Macro *defined = find_macro(tok->next);
push_cond_incl(tok, !defined);
tok = skip_line(tok->next->next);
if (defined)
tok = skip_cond_incl(tok);
continue;
}
if (equal(tok, "elif"))
{
if (!cond_incl || cond_incl->ctx == IN_ELSE)
error_tok(start, "%s: in preprocess2 : stray #elif", PREPROCESS_C);
cond_incl->ctx = IN_ELIF;
if (!cond_incl->included && eval_const_expr(&tok, tok))
cond_incl->included = true;
else
tok = skip_cond_incl(tok);
continue;
}
if (equal(tok, "else"))
{
if (!cond_incl || cond_incl->ctx == IN_ELSE)
error_tok(start, "%s: in preprocess2 : stray #else", PREPROCESS_C);
cond_incl->ctx = IN_ELSE;
tok = skip_line(tok->next);
if (cond_incl->included)
tok = skip_cond_incl(tok);
continue;
}
if (equal(tok, "endif"))
{
if (!cond_incl)
error_tok(start, "%s: in preprocess2 : stray #endif", PREPROCESS_C);
cond_incl = cond_incl->next;
tok = skip_line(tok->next);
continue;
}
if (equal(tok, "line"))
{
read_line_marker(&tok, tok->next);
// tok = skip_line(tok->next->next->next);
continue;
}
if (tok->kind == TK_PP_NUM)
{
read_line_marker(&tok, tok);
continue;
}
if (equal(tok, "pragma") && equal(tok->next, "once"))
{
hashmap_put(&pragma_once, tok->file->name, (void *)1);
tok = skip_line(tok->next->next);
continue;
}
if (equal(tok, "pragma"))
{
do
{
tok = tok->next;
} while (!tok->at_bol);
continue;
}
if (equal(tok, "error"))
error_tok(tok, "%s: in preprocess2 : error", PREPROCESS_C);
// `#`-only line is legal. It's called a null directive.
if (tok->at_bol)
continue;
//from @fuhsnn warning management
if (equal(tok, "warning"))
{
warn_tok(tok, "warning");
do {
tok = tok->next;
}while (!tok->at_bol);
continue;
}
error_tok(tok, "%s: in preprocess2 : invalid preprocessor directive", PREPROCESS_C);
}
cur->next = tok;
return head.next;
}
void define_macro(char *name, char *buf)
{
if (strncmp(name, "__has_attribute", 15))
{
Token *tok = tokenize(new_file("<built-in>", 1, buf));
add_macro(name, true, tok);
}
}
void undef_macro(char *name)
{
hashmap_delete(¯os, name);
}
static Macro *add_builtin(char *name, macro_handler_fn *fn)
{
Macro *m = add_macro(name, true, NULL);
m->handler = fn;
return m;
}
static Token *file_macro(Token *tmpl)
{
while (tmpl->origin)
tmpl = tmpl->origin;
return new_str_token(tmpl->file->display_name, tmpl);
}
static Token *line_macro(Token *tmpl)
{
while (tmpl->origin)
tmpl = tmpl->origin;
int i = tmpl->line_no + tmpl->file->line_delta;
return new_num_token(i, tmpl);
}
// __COUNTER__ is expanded to serial values starting from 0.
static Token *counter_macro(Token *tmpl)
{
static int i = 0;
return new_num_token(i++, tmpl);
}
// __TIMESTAMP__ is expanded to a string describing the last
// modification time of the current file. E.g.
// "Fri Jul 24 01:32:50 2020"
static Token *timestamp_macro(Token *tmpl)
{
struct stat st;
if (stat(tmpl->file->name, &st) != 0)
return new_str_token("??? ??? ?? ??:??:?? ????", tmpl);
char buf[30];
ctime_r(&st.st_mtime, buf);
buf[24] = '\0';
return new_str_token(buf, tmpl);
}
static Token *builtin_atomics(Token *tmpl)
{
char *buf = "#include <\"stdatomic.h\"";
return new_str_token(buf, tmpl);
}
static Token *base_file_macro(Token *tmpl)
{
return new_str_token(base_file, tmpl);
}
// __DATE__ is expanded to the current date, e.g. "May 17 2020".
static char *format_date(struct tm *tm)
{
static char mon[][4] = {
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
};
return format("\"%s %2d %d\"", mon[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
}
// __TIME__ is expanded to the current time, e.g. "13:34:03".
static char *format_time(struct tm *tm)
{
return format("\"%02d:%02d:%02d\"", tm->tm_hour, tm->tm_min, tm->tm_sec);
}
void init_macros(void)
{
// Define predefined macros
define_macro("__VERSION__", "\"" VERSION "\"");
define_macro("__builtin_offsetof", "offsetof");
define_macro("_LP64", "1");
define_macro("__C99_MACRO_WITH_VA_ARGS", "1");
define_macro("__ELF__", "1");
define_macro("__LP64__", "1");
define_macro("__SIZEOF_DOUBLE__", "8");
define_macro("__SIZEOF_FLOAT__", "4");
define_macro("__UINTPTR_TYPE__", "unsigned long");
define_macro("__INT32_TYPE__", "int");
define_macro("__SIZEOF_INT__", "4");
define_macro("SIZEOF_INT", "4");
define_macro("__SIZEOF_LONG_DOUBLE__", "16");
define_macro("__SIZEOF_LONG_LONG__", "8");
define_macro("__SIZEOF_LONG__", "8");
define_macro("SIZEOF_LONG", "8");
define_macro("__SIZEOF_POINTER__", "8");
define_macro("__SIZEOF_PTRDIFF_T__", "8");
define_macro("__SIZEOF_SHORT__", "2");
define_macro("__SIZEOF_SIZE_T__", "8");
define_macro("__SIZEOF_WCHAR_T__", "4");
define_macro("__WCHAR_TYPE__", "int");
define_macro("__WINT_TYPE__", "unsigned int");
define_macro("__SIZE_TYPE__", "unsigned long");
define_macro("__PTRDIFF_TYPE__", "long int");
define_macro("__STDC_HOSTED__", "1");
define_macro("__STDC_NO_COMPLEX__", "1");
define_macro("__STDC_UTF_16__", "1");
define_macro("__STDC_UTF_32__", "1");
//define_macro("__STDC_VERSION__", "201112L");
define_macro("__STDC__", "1");
// define_macro("__STDC_IEC_559__" , "1");
// define_macro("__STDC_ISO_10646__" , "201706L");
// define_macro("__STDC_IEC_559_COMPLEX__" , "1");
// define_macro("__STDC_IEC_60559_COMPLEX__" , "201404L");
// define_macro("__STDC_IEC_60559_BFP__" , "201404L");
define_macro("__GNUC_STDC_INLINE__", "1");
define_macro("__USER_LABEL_PREFIX__", "");
define_macro("__alignof__", "_Alignof");
define_macro("__amd64", "1");
define_macro("__amd64__", "1");
define_macro("__CHIBICC__", "1");
define_macro("__chibicc__", "1");
define_macro("__const__", "const");
define_macro("__gnu_linux__", "1");
define_macro("__inline__", "inline");
define_macro("__linux", "1");
define_macro("__linux__", "1");
define_macro("__signed__", "signed");
define_macro("__typeof__", "typeof");
define_macro("__unix", "1");
define_macro("__unix__", "1");
define_macro("__volatile__", "volatile");
define_macro("__x86_64", "1");
define_macro("__x86_64__", "1");
define_macro("_GNU_SOURCE", "1");
define_macro("_DEFAULT_SOURCE", "1");
//define_macro("__INTEL_COMPILER", "1");
//define_macro("__GNUC__", "9");
define_macro("__GNUC__", "2");
define_macro("__GNUC_MINOR__", "1");
define_macro("__GNUC_PATCHLEVEL__ ", "1");
//define_macro("HAVE_ATTRIBUTE_PACKED", "1");
define_macro("linux", "1");
define_macro("unix", "1");
define_macro("HAVE_RECV", "1");
define_macro("HAVE_SEND", "1");
define_macro("HAVE_SYS_IO_H", "1");
define_macro("__extension__", "");
define_macro("HAVE_TEST_AND_SET", "1");
define_macro("HAVE_LONG_LONG_INT_64", "1");
define_macro("__ATOMIC_RELAXED", "0");
define_macro("__ATOMIC_CONSUME", "1");
define_macro("__ATOMIC_ACQUIRE", "2");
define_macro("__ATOMIC_RELEASE", "3");
define_macro("__ATOMIC_ACQ_REL", "4");
define_macro("__ATOMIC_SEQ_CST", "5");
define_macro("__builtin_choose_expr(cond, true_expr, false_expr)", "(cond ? true_expr : false_expr)");
define_macro("__SHRT_MAX__", "32767");
define_macro("__INT_MAX__", "2147483647");
define_macro("__LONG_MAX__", "9223372036854775807L");
define_macro("__LONG_LONG_MAX__", "9223372036854775807LL");
define_macro("__SCHAR_MAX__", "127");
define_macro("__WCHAR_MAX__", "2147483647");
define_macro("__CHAR_BIT__", "8");
define_macro("__SCHAR_MAX__", "127");
define_macro("__SHRT_MAX__", "32767");
define_macro("__INT_MAX__", "2147483647");
define_macro("__LONG_MAX__", "9223372036854775807L");
define_macro("__LONG_LONG_MAX__", "9223372036854775807LL");
define_macro("__SCHAR_WIDTH__", "8");
define_macro("__SHRT_WIDTH__", "16");
define_macro("__INT_WIDTH__", "32");
define_macro("__LONG_WIDTH__", "64");
define_macro("__LONG_LONG_WIDTH__", "64");
define_macro("__SCHAR_MIN__", "-128");
define_macro("__SHRT_MIN__", "-32768");
define_macro("__INT_MIN__", "-2147483648");
define_macro("__LONG_MIN__", "-9223372036854775808L");
define_macro("__LONG_LONG_MIN__", "-9223372036854775808LL");
define_macro("HAVE_ATTRIBUTE_PACKED", "1");
define_macro("linux", "1");
define_macro("unix", "1");
define_macro("__extension__", "");
define_macro("__has_attribute(x)", "1");
define_macro("__builtin_strlen", "strlen");
//define_macro("nonnull", "1");
//====fixing ISS-147 defining the two macros for the linux platform
define_macro("__ORDER_LITTLE_ENDIAN__", "1234");
define_macro("__ORDER_BIG_ENDIAN__", "4321");
define_macro("__BYTE_ORDER__", "__ORDER_LITTLE_ENDIAN__");
define_macro("USE_BUILTINS", "1");
define_macro("_Pragma(message) ", "");
add_builtin("__STDC_VERSION__", stdver_macro);
add_builtin("__FILE__", file_macro);
add_builtin("__LINE__", line_macro);
add_builtin("__COUNTER__", counter_macro);
add_builtin("__TIMESTAMP__", timestamp_macro);
add_builtin("__BASE_FILE__", base_file_macro);
add_builtin("__BUILTIN_ATOMICS__", builtin_atomics);
time_t now = time(NULL);
struct tm *tm = localtime(&now);
define_macro("__DATE__", format_date(tm));
define_macro("__TIME__", format_time(tm));
}
typedef enum
{
STR_NONE,
STR_UTF8,
STR_UTF16,
STR_UTF32,
STR_WIDE,
} StringKind;
static StringKind getStringKind(Token *tok)
{
if (!strcmp(tok->loc, "u8"))
return STR_UTF8;
switch (tok->loc[0])
{
case '"':
return STR_NONE;
case 'u':
return STR_UTF16;
case 'U':
return STR_UTF32;
case 'L':
return STR_WIDE;
}
unreachable();
}
// Concatenate adjacent string literals into a single string literal
// as per the C spec.
static void join_adjacent_string_literals(Token *tok)
{
// First pass: If regular string literals are adjacent to wide
// string literals, regular string literals are converted to a wide
// type before concatenation. In this pass, we do the conversion.
for (Token *tok1 = tok; tok1->kind != TK_EOF;)
{
if (tok1->kind != TK_STR || tok1->next->kind != TK_STR)
{
tok1 = tok1->next;
continue;
}
StringKind kind = getStringKind(tok1);
if (!tok1->ty){
error("%s: %s:%d: error: in join_adjacent_string_literals : tok1->ty is null", PREPROCESS_C, __FILE__, __LINE__);
}
Type *basety = tok1->ty->base;
for (Token *t = tok1->next; t->kind == TK_STR; t = t->next)
{
StringKind k = getStringKind(t);
if (kind == STR_NONE)
{
kind = k;
basety = t->ty->base;
}
else if (k != STR_NONE && kind != k)
{
error_tok(t, "%s: in join_adjacent_string_literals : unsupported non-standard concatenation of string literals", PREPROCESS_C);
}
}
if (basety->size > 1)
for (Token *t = tok1; t->kind == TK_STR; t = t->next)
if (t->ty->base->size == 1)
*t = *tokenize_string_literal(t, basety);
while (tok1->kind == TK_STR)
tok1 = tok1->next;
}
// Second pass: concatenate adjacent string literals.
for (Token *tok1 = tok; tok1->kind != TK_EOF;)
{
if (tok1->kind != TK_STR || tok1->next->kind != TK_STR)
{
tok1 = tok1->next;
continue;
}
if (!tok1->ty){
error("%s: %s:%d: error: in join_adjacent_string_literals : tok1->ty is null", PREPROCESS_C, __FILE__, __LINE__);
}
Token *tok2 = tok1->next;
while (tok2->kind == TK_STR)
tok2 = tok2->next;
int len = tok1->ty->array_len;
for (Token *t = tok1->next; t != tok2; t = t->next)
len = len + t->ty->array_len - 1;
char *buf = calloc(tok1->ty->base->size, len);
if (buf == NULL)
error("%s: %s:%d: error: in join_adjacent_string_literals : buf is null", PREPROCESS_C, __FILE__, __LINE__);
int i = 0;
for (Token *t = tok1; t != tok2; t = t->next)
{
memcpy(buf + i, t->str, t->ty->size);
i = i + t->ty->size - t->ty->base->size;
}
*tok1 = *copy_token(tok1);
tok1->ty = array_of(tok1->ty->base, len);
tok1->str = buf;
tok1->next = tok2;
tok1 = tok2;
}
}
// Entry point function of the preprocessor.
Token *preprocess(Token *tok, bool isReadLine)
{
tok = preprocess2(tok);
// to manage issue with macro used before its definition. gcc allows it
tok = preprocess3(tok);
if (cond_incl && !isReadLine)
error_tok(cond_incl->tok, "%s: in preprocess : unterminated conditional directive", PREPROCESS_C);
convert_pp_tokens(tok);
//ISS-142 temp fix
if (!opt_E) {
join_adjacent_string_literals(tok);
}
for (Token *t = tok; t; t = t->next)
t->line_no += abs(t->line_delta); // fixing issue with negative number that caused assembly issue
return tok;
}
// a temp fix to manage the fact that gcc allows that a macro was defined after their use. See issue124.c for more details
Token *preprocess3(Token *tok)
{
Token head = {};
Token *cur = &head;
// Token *start = tok;
while (tok->kind != TK_EOF)
{
Macro *m = find_macro(tok);
if (m != NULL && m->body->len == 0)
{
//if (expand_macro(&tok, tok))
if (tok->kind == TK_IDENT && !equal(tok, "__attribute__") && expand_macro(&tok, tok))
continue;
}
cur = cur->next = tok;
tok = tok->next;
}
cur->next = tok;
return head.next;
}
static bool file_exists_in_include_path(const char *filename) {
for (int i = 0; i < include_paths.len; i++) {
char path[PATH_MAX];
snprintf(path, sizeof(path), "%s/%s", include_paths.data[i], filename);
if (access(path, R_OK) == 0)
return true;
}
return false;
}
void print_all_macros(void) {
for (int i = 0; i < macros.capacity; i++) {
HashEntry *e = ¯os.buckets[i];
if (e->key) {
Macro *m = (Macro *)e->val;
print_macro(m);
}
}
}
|