History

Fixes include the double quotes bug Added push_macro, pop_macro support Fix pack bug, when output with-E will pack the bug

jiang committed 12 years ago

Commit 6c820761 parent 5b52a446 files · +248 −111Browse files

Unified · Split

6 files
  1. libtcc.c +5 −1
  2. tcc.h +10 −2
  3. tccgen.c +3 −3
  4. tccpp.c +203 −99
  5. tcctok.h +2 −0
  6. tests/tcctest.c +25 −6
libtcc.c+5−1
@@ -868,6 +868,7 @@ LIBTCCAPI void tcc_undefine_symbol(TCCState *s1, const char *sym) · 867 unmodified lines
868static void tcc_cleanup(void)868static void tcc_cleanup(void)
869{869{
870 int i, n;870 int i, n;
871 CSym *def;
871 if (NULL == tcc_state)872 if (NULL == tcc_state)
872 return;873 return;
873 tcc_state = NULL;874 tcc_state = NULL;
@@ -877,8 +878,11 @@ static void tcc_cleanup(void) · 3 unmodified lines
877 878
878 /* free tokens */879 /* free tokens */
879 n = tok_ident - TOK_IDENT;880 n = tok_ident - TOK_IDENT;
880 for(i = 0; i < n; i++)881 for(i = 0; i < n; i++){
882 def = &table_ident[i]->sym_define;
883 tcc_free(def->data);
881 tcc_free(table_ident[i]);884 tcc_free(table_ident[i]);
885 }
882 tcc_free(table_ident);886 tcc_free(table_ident);
883 887
884 /* free sym_pools */888 /* free sym_pools */
tcc.h+10−2
@@ -305,15 +305,22 @@ · 304 unmodified lines
305#define VSTACK_SIZE 256305#define VSTACK_SIZE 256
306#define STRING_MAX_SIZE 1024306#define STRING_MAX_SIZE 1024
307#define PACK_STACK_SIZE 8307#define PACK_STACK_SIZE 8
308#define MACRO_STACK_SIZE 4
308 309
309#define TOK_HASH_SIZE 8192 /* must be a power of two */310#define TOK_HASH_SIZE 8192 /* must be a power of two */
310#define TOK_ALLOC_INCR 512 /* must be a power of two */311#define TOK_ALLOC_INCR 512 /* must be a power of two */
311#define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */312#define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
312 313
314typedef struct CSym {
315 int off;
316 int size;/* size in *sym */
317 struct Sym **data; /* if non NULL, data has been malloced */
318} CSym;
319
313/* token symbol management */320/* token symbol management */
314typedef struct TokenSym {321typedef struct TokenSym {
315 struct TokenSym *hash_next;322 struct TokenSym *hash_next;
316 struct Sym *sym_define; /* direct pointer to define */323 struct CSym sym_define; /* direct pointer to define */
317 struct Sym *sym_label; /* direct pointer to label */324 struct Sym *sym_label; /* direct pointer to label */
318 struct Sym *sym_struct; /* direct pointer to structure */325 struct Sym *sym_struct; /* direct pointer to structure */
319 struct Sym *sym_identifier; /* direct pointer to identifier */326 struct Sym *sym_identifier; /* direct pointer to identifier */
@@ -1129,7 +1136,8 @@ ST_DATA TokenSym **table_ident; · 809 unmodified lines
1129 token. line feed is also1136 token. line feed is also
1130 returned at eof */1137 returned at eof */
1131#define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */1138#define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
1132#define PARSE_FLAG_SPACES 0x0010 /* next() returns space tokens (for -E) */1139#define PARSE_FLAG_SPACES 0x0010 /* next() returns space tokens (for -E) */
1140#define PARSE_FLAG_PACK 0x0020 /* #pragma pack */
1133 1141
1134ST_FUNC TokenSym *tok_alloc(const char *str, int len);1142ST_FUNC TokenSym *tok_alloc(const char *str, int len);
1135ST_FUNC char *get_tok_str(int v, CValue *cv);1143ST_FUNC char *get_tok_str(int v, CValue *cv);
tccgen.c+3−3
@@ -5244,13 +5244,13 @@ static void init_putz(CType *t, Section *sec, unsigned long c, int size) · 5243 unmodified lines
5244#ifndef TCC_TARGET_X86_645244#ifndef TCC_TARGET_X86_64
5245 vpush_global_sym(&func_old_type, TOK_memset);5245 vpush_global_sym(&func_old_type, TOK_memset);
5246 vseti(VT_LOCAL, c);5246 vseti(VT_LOCAL, c);
5247#ifdef TCC_TARGET_ARM5247# ifdef TCC_TARGET_ARM
5248 vpushs(size);5248 vpushs(size);
5249 vpushi(0);5249 vpushi(0);
5250#else5250# else
5251 vpushi(0);5251 vpushi(0);
5252 vpushs(size);5252 vpushs(size);
5253#endif5253# endif
5254 gfunc_call(3);5254 gfunc_call(3);
5255#else5255#else
5256 vseti(VT_LOCAL, c);5256 vseti(VT_LOCAL, c);
tccpp.c+203−99
@@ -233,7 +233,10 @@ static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len) · 232 unmodified lines
233 ts = tcc_malloc(sizeof(TokenSym) + len);233 ts = tcc_malloc(sizeof(TokenSym) + len);
234 table_ident[i] = ts;234 table_ident[i] = ts;
235 ts->tok = tok_ident++;235 ts->tok = tok_ident++;
236 ts->sym_define = NULL;236 ts->sym_define.data = tcc_malloc(sizeof(Sym**));
237 ts->sym_define.off = 0;
238 ts->sym_define.data[0] = NULL;
239 ts->sym_define.size = 1;
237 ts->sym_label = NULL;240 ts->sym_label = NULL;
238 ts->sym_struct = NULL;241 ts->sym_struct = NULL;
239 ts->sym_identifier = NULL;242 ts->sym_identifier = NULL;
@@ -1052,52 +1055,62 @@ static int macro_is_equal(const int *a, const int *b) · 812 unmodified lines
1052ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg)1055ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg)
1053{1056{
1054 Sym *s;1057 Sym *s;
1055 1058 CSym *def;
1056 s = define_find(v);1059 s = define_find(v);
1057 if (s && !macro_is_equal(s->d, str))1060 if (s && !macro_is_equal(s->d, str))
1058 tcc_warning("%s redefined", get_tok_str(v, NULL));1061 tcc_warning("%s redefined", get_tok_str(v, NULL));
1059
1060 s = sym_push2(&define_stack, v, macro_type, 0);1062 s = sym_push2(&define_stack, v, macro_type, 0);
1061 s->d = str;1063 s->d = str;
1062 s->next = first_arg;1064 s->next = first_arg;
1063 table_ident[v - TOK_IDENT]->sym_define = s;1065 def = &table_ident[v - TOK_IDENT]->sym_define;
1066 def->data[def->off] = s;
1064}1067}
1065 1068
1066/* undefined a define symbol. Its name is just set to zero */1069/* undefined a define symbol. Its name is just set to zero */
1067ST_FUNC void define_undef(Sym *s)1070ST_FUNC void define_undef(Sym *s)
1068{1071{
1069 int v;1072 int v;
1070 v = s->v;1073 CSym *def;
1071 if (v >= TOK_IDENT && v < tok_ident)1074 v = s->v - TOK_IDENT;
1072 table_ident[v - TOK_IDENT]->sym_define = NULL;1075 if ((unsigned)v < (unsigned)(tok_ident - TOK_IDENT)){
1073 s->v = 0;1076 def = &table_ident[v]->sym_define;
1077 def->data[def->off] = NULL;
1078 }
1074}1079}
1075 1080
1076ST_INLN Sym *define_find(int v)1081ST_INLN Sym *define_find(int v)
1077{1082{
1083 CSym *def;
1078 v -= TOK_IDENT;1084 v -= TOK_IDENT;
1079 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))1085 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1080 return NULL;1086 return NULL;
1081 return table_ident[v]->sym_define;1087 def = &table_ident[v]->sym_define;
1088 return def->data[def->off];
1082}1089}
1083 1090
1084/* free define stack until top reaches 'b' */1091/* free define stack until top reaches 'b' */
1085ST_FUNC void free_defines(Sym *b)1092ST_FUNC void free_defines(Sym *b)
1086{1093{
1087 Sym *top, *top1;1094 Sym *top, *tmp;
1088 int v;1095 int v;
1096 CSym *def;
1089 1097
1090 top = define_stack;1098 top = define_stack;
1091 while (top != b) {1099 while (top != b) {
1092 top1 = top->prev;1100 tmp = top->prev;
1093 /* do not free args or predefined defines */1101 /* do not free args or predefined defines */
1094 if (top->d)1102 if (top->d)
1095 tok_str_free(top->d);1103 tok_str_free(top->d);
1096 v = top->v;1104 v = top->v - TOK_IDENT;
1097 if (v >= TOK_IDENT && v < tok_ident)1105 if ((unsigned)v < (unsigned)(tok_ident - TOK_IDENT)){
1098 table_ident[v - TOK_IDENT]->sym_define = NULL;1106 def = &table_ident[v]->sym_define;
1107 if(def->off)
1108 def->off = 0;
1109 if(def->data[0])
1110 def->data[0] = NULL;
1111 }
1099 sym_free(top);1112 sym_free(top);
1100 top = top1;1113 top = tmp;
1101 }1114 }
1102 define_stack = b;1115 define_stack = b;
1103}1116}
@@ -1338,66 +1351,18 @@ static inline void add_cached_include(TCCState *s1, const char *filename, int if · 234 unmodified lines
1338 s1->cached_includes_hash[h] = s1->nb_cached_includes;1351 s1->cached_includes_hash[h] = s1->nb_cached_includes;
1339}1352}
1340 1353
1341static void pragma_parse(TCCState *s1)
1342{
1343 int val;
1344
1345 next();
1346 if (tok == TOK_pack) {
1347 /*
1348 This may be:
1349 #pragma pack(1) // set
1350 #pragma pack() // reset to default
1351 #pragma pack(push,1) // push & set
1352 #pragma pack(pop) // restore previous
1353 */
1354 next();
1355 skip('(');
1356 if (tok == TOK_ASM_pop) {
1357 next();
1358 if (s1->pack_stack_ptr <= s1->pack_stack) {
1359 stk_error:
1360 tcc_error("out of pack stack");
1361 }
1362 s1->pack_stack_ptr--;
1363 } else {
1364 val = 0;
1365 if (tok != ')') {
1366 if (tok == TOK_ASM_push) {
1367 next();
1368 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
1369 goto stk_error;
1370 s1->pack_stack_ptr++;
1371 skip(',');
1372 }
1373 if (tok != TOK_CINT) {
1374 pack_error:
1375 tcc_error("invalid pack pragma");
1376 }
1377 val = tokc.i;
1378 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
1379 goto pack_error;
1380 next();
1381 }
1382 *s1->pack_stack_ptr = val;
1383 skip(')');
1384 }
1385 }
1386}
1387
1388/* is_bof is true if first non space token at beginning of file */1354/* is_bof is true if first non space token at beginning of file */
1389ST_FUNC void preprocess(int is_bof)1355ST_FUNC void preprocess(int is_bof)
1390{1356{
1391 TCCState *s1 = tcc_state;1357 TCCState *s1 = tcc_state;
1392 int i, c, n, saved_parse_flags;1358 int i, c, n, saved_parse_flags;
1393 char buf[1024], *q;1359 uint8_t buf[1024], *p;
1394 Sym *s;1360 Sym *s;
1395 1361
1396 saved_parse_flags = parse_flags;1362 saved_parse_flags = parse_flags;
1397 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM | 1363 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM | PARSE_FLAG_LINEFEED;
1398 PARSE_FLAG_LINEFEED;
1399 next_nomacro();1364 next_nomacro();
1400 redo:1365redo:
1401 switch(tok) {1366 switch(tok) {
1402 case TOK_DEFINE:1367 case TOK_DEFINE:
1403 next_nomacro();1368 next_nomacro();
@@ -1420,19 +1385,21 @@ ST_FUNC void preprocess(int is_bof) · 16 unmodified lines
1420 goto read_name;1385 goto read_name;
1421 } else if (ch == '\"') {1386 } else if (ch == '\"') {
1422 c = ch;1387 c = ch;
1423 read_name:1388read_name:
1424 inp();1389 inp();
1425 q = buf;1390 p = buf;
1426 while (ch != c && ch != '\n' && ch != CH_EOF) {1391 while (ch != c && ch != '\n' && ch != CH_EOF) {
1427 if ((q - buf) < sizeof(buf) - 1)1392 if ((p - buf) < sizeof(buf) - 1)
1428 *q++ = ch;1393 *p++ = ch;
1429 if (ch == '\\') {1394 if (ch == '\\') {
1430 if (handle_stray_noerror() == 0)1395 if (handle_stray_noerror() == 0)
1431 --q;1396 --p;
1432 } else1397 } else
1433 inp();1398 inp();
1434 }1399 }
1435 *q = '\0';1400 if (ch != c)
1401 goto include_syntax;
1402 *p = '\0';
1436 minp();1403 minp();
1437#if 01404#if 0
1438 /* eat all spaces and comments after include */1405 /* eat all spaces and comments after include */
@@ -1470,6 +1437,8 @@ ST_FUNC void preprocess(int is_bof) · 31 unmodified lines
1470 c = '>';1437 c = '>';
1471 }1438 }
1472 }1439 }
1440 if(!buf[0])
1441 tcc_error(" empty filename in #include");
1473 1442
1474 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)1443 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
1475 tcc_error("#include recursion too deep");1444 tcc_error("#include recursion too deep");
@@ -1536,8 +1505,7 @@ include_trynext: · 60 unmodified lines
1536 printf("%s: including %s\n", file->prev->filename, file->filename);1505 printf("%s: including %s\n", file->prev->filename, file->filename);
1537#endif1506#endif
1538 /* update target deps */1507 /* update target deps */
1539 dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps,1508 dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps, tcc_strdup(buf1));
1540 tcc_strdup(buf1));
1541 /* push current file in stack */1509 /* push current file in stack */
1542 ++s1->include_stack_ptr;1510 ++s1->include_stack_ptr;
1543 /* add include file debug info */1511 /* add include file debug info */
@@ -1570,7 +1538,7 @@ include_done: · 26 unmodified lines
1570 file->ifndef_macro = tok;1538 file->ifndef_macro = tok;
1571 }1539 }
1572 }1540 }
1573 c = (define_find(tok) != 0) ^ c;1541 c = !!define_find(tok) ^ c;
1574 do_if:1542 do_if:
1575 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)1543 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
1576 tcc_error("memory full (ifdef)");1544 tcc_error("memory full (ifdef)");
@@ -1594,12 +1562,12 @@ include_done: · 17 unmodified lines
1594 goto skip;1562 goto skip;
1595 c = expr_preprocess();1563 c = expr_preprocess();
1596 s1->ifdef_stack_ptr[-1] = c;1564 s1->ifdef_stack_ptr[-1] = c;
1597 test_else:1565test_else:
1598 if (s1->ifdef_stack_ptr == file->ifdef_stack_ptr + 1)1566 if (s1->ifdef_stack_ptr == file->ifdef_stack_ptr + 1)
1599 file->ifndef_macro = 0;1567 file->ifndef_macro = 0;
1600 test_skip:1568test_skip:
1601 if (!(c & 1)) {1569 if (!(c & 1)) {
1602 skip:1570skip:
1603 preprocess_skip();1571 preprocess_skip();
1604 is_bof = 0;1572 is_bof = 0;
1605 goto redo;1573 goto redo;
@@ -1617,11 +1585,11 @@ include_done: · 11 unmodified lines
1617 /* need to set to zero to avoid false matches if another1585 /* need to set to zero to avoid false matches if another
1618 #ifndef at middle of file */1586 #ifndef at middle of file */
1619 file->ifndef_macro = 0;1587 file->ifndef_macro = 0;
1620 while (tok != TOK_LINEFEED)
1621 next_nomacro();
1622 tok_flags |= TOK_FLAG_ENDIF;1588 tok_flags |= TOK_FLAG_ENDIF;
1623 goto the_end;
1624 }1589 }
1590 next_nomacro();
1591 if (tok != TOK_LINEFEED)
1592 tcc_warning("Ignoring: %s", get_tok_str(tok, &tokc));
1625 break;1593 break;
1626 case TOK_LINE:1594 case TOK_LINE:
1627 next();1595 next();
@@ -1632,8 +1600,7 @@ include_done: · 4 unmodified lines
1632 if (tok != TOK_LINEFEED) {1600 if (tok != TOK_LINEFEED) {
1633 if (tok != TOK_STR)1601 if (tok != TOK_STR)
1634 tcc_error("#line");1602 tcc_error("#line");
1635 pstrcpy(file->filename, sizeof(file->filename), 1603 pstrcpy(file->filename, sizeof(file->filename), (char *)tokc.cstr->data);
1636 (char *)tokc.cstr->data);
1637 }1604 }
1638 break;1605 break;
1639 case TOK_ERROR:1606 case TOK_ERROR:
@@ -1641,24 +1608,161 @@ include_done: · 1 unmodified line
1641 c = tok;1608 c = tok;
1642 ch = file->buf_ptr[0];1609 ch = file->buf_ptr[0];
1643 skip_spaces();1610 skip_spaces();
1644 q = buf;1611 p = buf;
1645 while (ch != '\n' && ch != CH_EOF) {1612 while (ch != '\n' && ch != CH_EOF) {
1646 if ((q - buf) < sizeof(buf) - 1)1613 if ((p - buf) < sizeof(buf) - 1)
1647 *q++ = ch;1614 *p++ = ch;
1648 if (ch == '\\') {1615 if (ch == '\\') {
1649 if (handle_stray_noerror() == 0)1616 if (handle_stray_noerror() == 0)
1650 --q;1617 --p;
1651 } else1618 } else
1652 inp();1619 inp();
1653 }1620 }
1654 *q = '\0';1621 *p = '\0';
1655 if (c == TOK_ERROR)1622 if (c == TOK_ERROR)
1656 tcc_error("#error %s", buf);1623 tcc_error("#error %s", buf);
1657 else1624 else
1658 tcc_warning("#warning %s", buf);1625 tcc_warning("#warning %s", buf);
1659 break;1626 break;
1660 case TOK_PRAGMA:1627 case TOK_PRAGMA:
1661 pragma_parse(s1);1628 next();
1629 if (tok == TOK_pack && parse_flags & PARSE_FLAG_PACK) {
1630 /*
1631 This may be:
1632 #pragma pack(1) // set
1633 #pragma pack() // reset to default
1634 #pragma pack(push,1) // push & set
1635 #pragma pack(pop) // restore previous
1636 */
1637 next();
1638 skip('(');
1639 if (tok == TOK_ASM_pop) {
1640 next();
1641 if (s1->pack_stack_ptr <= s1->pack_stack) {
1642stk_error:
1643 tcc_error("out of pack stack");
1644 }
1645 s1->pack_stack_ptr--;
1646 } else {
1647 int val = 0;
1648 if (tok != ')') {
1649 if (tok == TOK_ASM_push) {
1650 next();
1651 s1->pack_stack_ptr++;
1652 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE)
1653 goto stk_error;
1654 skip(',');
1655 }
1656 if (tok != TOK_CINT) {
1657pack_error:
1658 tcc_error("invalid pack pragma");
1659 }
1660 val = tokc.i;
1661 if (val < 1 || val > 16)
1662 goto pack_error;
1663 if (val < 1 || val > 16)
1664 tcc_error("Value must be greater than 1 is less than or equal to 16");
1665 if ((val & (val - 1)) != 0)
1666 tcc_error("Value must be a power of 2 curtain");
1667 next();
1668 }
1669 *s1->pack_stack_ptr = val;
1670 skip(')');
1671 }
1672 }else if (tok == TOK_PUSH_MACRO || tok == TOK_POP_MACRO) {
1673 TokenSym *ts;
1674 CSym *def;
1675 uint8_t *p1;
1676 int len, t;
1677 t = tok;
1678 ch = file->buf_ptr[0];
1679 skip_spaces();
1680 if (ch != '(')
1681 goto macro_xxx_syntax;
1682 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1683 inp();
1684 skip_spaces();
1685 if (ch == '\"'){
1686 inp();
1687 p = buf;
1688 while (ch != '\"' && ch != '\n' && ch != CH_EOF) {
1689 if ((p - buf) < sizeof(buf) - 1)
1690 *p++ = ch;
1691 if (ch == CH_EOB) {
1692 --p;
1693 handle_stray();
1694 }else
1695 inp();
1696 }
1697 if(ch != '\"')
1698 goto macro_xxx_syntax;
1699 *p = '\0';
1700 minp();
1701 next();
1702 }else{
1703 /* computed #pragma macro_xxx for #define xxx */
1704 next();
1705 buf[0] = '\0';
1706 while (tok != ')') {
1707 if (tok != TOK_STR) {
1708 macro_xxx_syntax:
1709 tcc_error("'macro_xxx' expects (\"NAME\")");
1710 }
1711 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
1712 next();
1713 }
1714 }
1715 skip (')');
1716 if(!buf[0])
1717 tcc_error(" empty string in #pragma");
1718 /* find TokenSym */
1719 p = buf;
1720 while (is_space(*p))
1721 p++;
1722 p1 = p;
1723 for(;;){
1724 if (!isidnum_table[p[0] - CH_EOF])
1725 break;
1726 ++p;
1727 }
1728 len = p - p1;
1729 while (is_space(*p))
1730 p++;
1731 if(!p) //'\0'
1732 tcc_error("unrecognized string: %s", buf);
1733 ts = tok_alloc(p1, len);
1734 if(ts){
1735 def = &ts->sym_define;
1736 if(t == TOK_PUSH_MACRO){
1737 void *tmp = def->data[def->off];
1738 if(tmp){
1739 def->off++;
1740 if(def->off >= def->size){
1741 int size = def->size;
1742 size *= 2;
1743 if (size >= MACRO_STACK_SIZE)
1744 tcc_error("stack full");
1745 def->data = tcc_realloc(def->data, size*sizeof(Sym**));
1746 def->size = size;
1747 }
1748 def->data[def->off] = tmp;
1749 }
1750 }else{
1751 if(def->off){
1752 --def->off;
1753 }else{
1754 tcc_warning("stack empty");
1755 }
1756 }
1757 }
1758 }else{
1759 fputs("#pragma ", s1->ppfp);
1760 while (tok != TOK_LINEFEED){
1761 fputs(get_tok_str(tok, &tokc), s1->ppfp);
1762 next();
1763 }
1764 goto the_end;
1765 }
1662 break;1766 break;
1663 default:1767 default:
1664 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_PPNUM) {1768 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_PPNUM) {
@@ -1678,7 +1782,7 @@ include_done: · 13 unmodified lines
1678 /* ignore other preprocess commands or #! for C scripts */1782 /* ignore other preprocess commands or #! for C scripts */
1679 while (tok != TOK_LINEFEED)1783 while (tok != TOK_LINEFEED)
1680 next_nomacro();1784 next_nomacro();
1681 the_end:1785the_end:
1682 parse_flags = saved_parse_flags;1786 parse_flags = saved_parse_flags;
1683}1787}
1684 1788
@@ -3030,12 +3134,13 @@ ST_FUNC int tcc_preprocess(TCCState *s1) · 1345 unmodified lines
3030 ch = file->buf_ptr[0];3134 ch = file->buf_ptr[0];
3031 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;3135 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
3032 parse_flags = PARSE_FLAG_ASM_COMMENTS | PARSE_FLAG_PREPROCESS |3136 parse_flags = PARSE_FLAG_ASM_COMMENTS | PARSE_FLAG_PREPROCESS |
3033 PARSE_FLAG_LINEFEED | PARSE_FLAG_SPACES;3137 PARSE_FLAG_LINEFEED | PARSE_FLAG_SPACES;
3034 token_seen = 0;3138 token_seen = 0;
3035 line_ref = 0;3139 line_ref = 0;
3036 file_ref = NULL;3140 file_ref = NULL;
3037 iptr = s1->include_stack_ptr;3141 iptr = s1->include_stack_ptr;
3038 3142 tok = TOK_LINEFEED; /* print line */
3143 goto print_line;
3039 for (;;) {3144 for (;;) {
3040 next();3145 next();
3041 if (tok == TOK_EOF) {3146 if (tok == TOK_EOF) {
@@ -3043,11 +3148,11 @@ ST_FUNC int tcc_preprocess(TCCState *s1) · 1 unmodified line
3043 } else if (file != file_ref) {3148 } else if (file != file_ref) {
3044 goto print_line;3149 goto print_line;
3045 } else if (tok == TOK_LINEFEED) {3150 } else if (tok == TOK_LINEFEED) {
3046 if (!token_seen)3151 if (token_seen)
3047 continue;3152 continue;
3048 ++line_ref;3153 ++line_ref;
3049 token_seen = 0;3154 token_seen = 1;
3050 } else if (!token_seen) {3155 } else if (token_seen) {
3051 d = file->line_num - line_ref;3156 d = file->line_num - line_ref;
3052 if (file != file_ref || d < 0 || d >= 8) {3157 if (file != file_ref || d < 0 || d >= 8) {
3053print_line:3158print_line:
@@ -3055,8 +3160,7 @@ print_line: · 1 unmodified line
3055 s = iptr_new > iptr ? " 1"3160 s = iptr_new > iptr ? " 1"
3056 : iptr_new < iptr ? " 2"3161 : iptr_new < iptr ? " 2"
3057 : iptr_new > s1->include_stack ? " 3"3162 : iptr_new > s1->include_stack ? " 3"
3058 : ""3163 : "";
3059 ;
3060 iptr = iptr_new;3164 iptr = iptr_new;
3061 fprintf(s1->ppfp, "# %d \"%s\"%s\n", file->line_num, file->filename, s);3165 fprintf(s1->ppfp, "# %d \"%s\"%s\n", file->line_num, file->filename, s);
3062 } else {3166 } else {
@@ -3064,8 +3168,8 @@ print_line: · 1 unmodified line
3064 fputs("\n", s1->ppfp), --d;3168 fputs("\n", s1->ppfp), --d;
3065 }3169 }
3066 line_ref = (file_ref = file)->line_num;3170 line_ref = (file_ref = file)->line_num;
3067 token_seen = tok != TOK_LINEFEED;3171 token_seen = tok == TOK_LINEFEED;
3068 if (!token_seen)3172 if (token_seen)
3069 continue;3173 continue;
3070 }3174 }
3071 fputs(get_tok_str(tok, &tokc), s1->ppfp);3175 fputs(get_tok_str(tok, &tokc), s1->ppfp);
tcctok.h+2−0
@@ -138,6 +138,8 @@ · 137 unmodified lines
138 138
139/* pragma */139/* pragma */
140 DEF(TOK_pack, "pack")140 DEF(TOK_pack, "pack")
141 DEF(TOK_PUSH_MACRO, "push_macro")
142 DEF(TOK_POP_MACRO, "pop_macro")
141#if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_X86_64)143#if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_X86_64)
142 /* already defined for assembler */144 /* already defined for assembler */
143 DEF(TOK_ASM_push, "push")145 DEF(TOK_ASM_push, "push")
tests/tcctest.c+25−6
@@ -235,7 +235,7 @@ void intdiv_test(void) · 234 unmodified lines
235 235
236void macro_test(void)236void macro_test(void)
237{237{
238 printf("macro:\n"); 238 printf("macro:\n");
239 pf("N=%d\n", N);239 pf("N=%d\n", N);
240 printf("aaa=%d\n", AAA);240 printf("aaa=%d\n", AAA);
241 241
@@ -379,6 +379,23 @@ comment · 137 unmodified lines
379 /* And again when the name and parenthes are separated by a379 /* And again when the name and parenthes are separated by a
380 comment. */380 comment. */
381 TEST2 /* the comment */ ();381 TEST2 /* the comment */ ();
382 /* macro_push and macro_pop test */
383 #define MACRO_TEST "macro_test1\n"
384 #pragma push_macro("MACRO_TEST")
385 #undef MACRO_TEST
386 #define MACRO_TEST "macro_test2\n"
387 printf(MACRO_TEST);
388 #pragma pop_macro("MACRO_TEST")
389 printf(MACRO_TEST);
390/* gcc does not support
391 #define MACRO_TEST_MACRO "MACRO_TEST"
392 #pragma push_macro(MACRO_TEST_MACRO)
393 #undef MACRO_TEST
394 #define MACRO_TEST "macro_test3\n"
395 printf(MACRO_TEST);
396 #pragma pop_macro(MACRO_TEST_MACRO)
397 printf(MACRO_TEST);
398*/
382}399}
383 400
384 401
@@ -2167,14 +2184,15 @@ void whitespace_test(void) · 1782 unmodified lines
2167{2184{
2168 char *str;2185 char *str;
2169 2186
2170 #if 12187
2188#if 1
2171 pri\2189 pri\
2172ntf("whitspace:\n"); 2190ntf("whitspace:\n");
2173#endif2191#endif
2174 pf("N=%d\n", 2);2192 pf("N=%d\n", 2);
2175 2193
2176#ifdef CORRECT_CR_HANDLING2194#ifdef CORRECT_CR_HANDLING
2177 pri\2195 pri\
2178ntf("aaa=%d\n", 3);2196ntf("aaa=%d\n", 3);
2179#endif2197#endif
2180 2198
@@ -2186,11 +2204,12 @@ ntf("min=%d\n", 4); · 5 unmodified lines
2186 printf("len1=%d\n", strlen("2204 printf("len1=%d\n", strlen("
2187"));2205"));
2188#ifdef CORRECT_CR_HANDLING2206#ifdef CORRECT_CR_HANDLING
2189 str = "2207 str = "
2190";2208";
2191 printf("len1=%d str[0]=%d\n", strlen(str), str[0]);2209 printf("len1=%d str[0]=%d\n", strlen(str), str[0]);
2192#endif2210#endif
2193 printf("len1=%d\n", strlen(" a2211 printf("len1=%d\n", strlen("
2212a
2194"));2213"));
2195#endif /* ACCEPT_CR_IN_STRINGS */2214#endif /* ACCEPT_CR_IN_STRINGS */
2196}2215}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t