History

Insert spaces between certain tokens when tcc is invoked with -E.

Insert a space when it is required to prevent mistokenisation of the output, and also in a few cases where it is not strictly required, imitating GCC's behaviour.

Edmund Grimley Evans committed 10 years ago

Commit f5f82ab1 parent 75243f74 files · +78 −3Browse files

Unified · Split

4 files
  1. TODO +0 −2
  2. tccpp.c +53 −1
  3. tests/pp/15.c +5 −0
  4. tests/pp/15.expect +20 −0
TODO+0−2
@@ -31,7 +31,6 @@ Bugs: · 30 unmodified lines
3131 - make libtcc fully reentrant (except for the compilation stage itself).
3232 - struct/union/enum definitions in nested scopes (see also Debian bug #770657)
3333 - __STDC_IEC_559__: float f(void) { static float x = 0.0 / 0.0; return x; }
34−- preprocessor: #define Y(x) Z(x) {newline} #define X Y {newline} X(X(1))
3534
3635 Portability:
3736
@@ -94,7 +93,6 @@ Not critical: · 56 unmodified lines
9493 - handle void (__attribute__() *ptr)()
9594 - VLAs are implemented in a way that is not compatible with signals:
9695 http://lists.gnu.org/archive/html/tinycc-devel/2015-11/msg00018.html
97−- output with -E should include spaces: #define n 0xe {newline} n+1
9896
9997 Fixed (probably):
10098
tccpp.c+53−1
@@ -3644,12 +3644,61 @@ static void pp_debug_builtins(TCCState *s1) · 3643 unmodified lines
36443644 define_print(s1, v);
36453645 }
36463646
3647+static int need_space(int prev_tok, int tok, const char *tokstr)
3648+{
3649+ const char *sp_chars = "";
3650+ if ((prev_tok >= TOK_IDENT || prev_tok == TOK_PPNUM) &&
3651+ (tok >= TOK_IDENT || tok == TOK_PPNUM))
3652+ return 1;
3653+ switch (prev_tok) {
3654+ case '+':
3655+ sp_chars = "+=";
3656+ break;
3657+ case '-':
3658+ sp_chars = "-=>";
3659+ break;
3660+ case '*':
3661+ case '/':
3662+ case '%':
3663+ case '^':
3664+ case '=':
3665+ case '!':
3666+ case TOK_A_SHL:
3667+ case TOK_A_SAR:
3668+ sp_chars = "=";
3669+ break;
3670+ case '&':
3671+ sp_chars = "&=";
3672+ break;
3673+ case '|':
3674+ sp_chars = "|=";
3675+ break;
3676+ case '<':
3677+ sp_chars = "<=";
3678+ break;
3679+ case '>':
3680+ sp_chars = ">=";
3681+ break;
3682+ case '.':
3683+ sp_chars = ".";
3684+ break;
3685+ case '#':
3686+ sp_chars = "#";
3687+ break;
3688+ case TOK_PPNUM:
3689+ sp_chars = "+-";
3690+ break;
3691+ }
3692+ return !!strchr(sp_chars, tokstr[0]);
3693+}
3694+
36473695 /* Preprocess the current file */
36483696 ST_FUNC int tcc_preprocess(TCCState *s1)
36493697 {
36503698 BufferedFile **iptr;
36513699 int token_seen, spcs, level;
36523700 Sym *define_start;
3701+ const char *tokstr;
36533702
36543703 preprocess_init(s1);
36553704 ch = file->buf_ptr[0];
@@ -3707,9 +3756,12 @@ ST_FUNC int tcc_preprocess(TCCState *s1) · 51 unmodified lines
37073756 ++file->line_ref;
37083757 }
37093758
3759+ tokstr = get_tok_str(tok, &tokc);
3760+ if (!spcs && need_space(token_seen, tok, tokstr))
3761+ ++spcs;
37103762 while (spcs)
37113763 fputs(" ", s1->ppfp), --spcs;
3712− fputs(get_tok_str(tok, &tokc), s1->ppfp);
3764+ fputs(tokstr, s1->ppfp);
37133765
37143766 token_seen = tok;
37153767 }
tests/pp/15.c+5−0
@@ -19,3 +19,8 @@ return A + B; · 18 unmodified lines
1919 #define B1 C1+2
2020 #define C1 A1+3
2121 return A1 + B1;
22+
23+#define i() x
24+#define n() 1
25+i()i()n()n()i()
26+i()+i()-n()+n()-
tests/pp/15.expect+20−0
@@ -1,6 +1,26 @@
1+
2+
13 Z(1)
24 Z(Z(1))
35 Z(Z(Z(Z(Z(1)))))
6+
7+
8+
49 return A + B;
10+
11+
12+
13+
14+
15+
516 return A+1 + B+1;
17+
18+
19+
20+
621 return A1+3 +2 +1 + B1+1 +3 +2;
22+
23+
24+
25+x x 1 1 x
26+x+x-1 +1 -

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