tcc -E: add one space in cases: tiny solution

replaces f5f82abc99

Also: fix tcc flags in Makefile, fix tcc -E
This commit is contained in:
grischka 2016-10-01 21:52:11 +02:00
parent afdbc5b815
commit 643a1b8848
3 changed files with 26 additions and 54 deletions

View File

@ -140,7 +140,7 @@ all: $(PROGS) $(TCCLIBS) $(TCCDOCS)
# Host Tiny C Compiler # Host Tiny C Compiler
tcc$(EXESUF): tcc.c $(LIBTCC) tcc$(EXESUF): tcc.c $(LIBTCC)
$(CC) -o $@ $^ $(LIBS) $(CFLAGS) $(LDFLAGS) $(LINK_LIBTCC) $(CC) -o $@ $^ $(LIBS) $(NATIVE_DEFINES) $(CFLAGS) $(LDFLAGS) $(LINK_LIBTCC)
# Cross Tiny C Compilers # Cross Tiny C Compilers
%-tcc$(EXESUF): tcc.c %-tcc$(EXESUF): tcc.c

6
tcc.c
View File

@ -331,7 +331,11 @@ int main(int argc, char **argv)
s->alacarte_link = 1; s->alacarte_link = 1;
} }
if (0 == ret) { if (s->output_type == TCC_OUTPUT_PREPROCESS) {
if (s->outfile)
fclose(s->ppfp);
} else if (0 == ret) {
if (s->do_bench) if (s->do_bench)
tcc_print_stats(s, getclock_us() - start_time); tcc_print_stats(s, getclock_us() - start_time);
if (s->output_type == TCC_OUTPUT_MEMORY) { if (s->output_type == TCC_OUTPUT_MEMORY) {

72
tccpp.c
View File

@ -3645,52 +3645,22 @@ static void pp_debug_builtins(TCCState *s1)
define_print(s1, v); define_print(s1, v);
} }
static int need_space(int prev_tok, int tok, const char *tokstr) /* Add a space between tokens a and b to avoid unwanted textual pasting */
static int pp_need_space(int a, int b)
{ {
const char *sp_chars = ""; return 'E' == a ? '+' == b || '-' == b
if ((prev_tok >= TOK_IDENT || prev_tok == TOK_PPNUM) && : '+' == a ? TOK_INC == b || '+' == b
(tok >= TOK_IDENT || tok == TOK_PPNUM)) : '-' == a ? TOK_DEC == b || '-' == b
return 1; : a >= TOK_IDENT ? b >= TOK_IDENT
switch (prev_tok) { : 0;
case '+': }
sp_chars = "+=";
break; /* maybe hex like 0x1e */
case '-': static int pp_check_he0xE(int t, const char *p)
sp_chars = "-=>"; {
break; if (t == TOK_PPNUM && toup(strchr(p, 0)[-1]) == 'E')
case '*': return 'E';
case '/': return t;
case '%':
case '^':
case '=':
case '!':
case TOK_A_SHL:
case TOK_A_SAR:
sp_chars = "=";
break;
case '&':
sp_chars = "&=";
break;
case '|':
sp_chars = "|=";
break;
case '<':
sp_chars = "<=";
break;
case '>':
sp_chars = ">=";
break;
case '.':
sp_chars = ".";
break;
case '#':
sp_chars = "#";
break;
case TOK_PPNUM:
sp_chars = "+-";
break;
}
return !!strchr(sp_chars, tokstr[0]);
} }
/* Preprocess the current file */ /* Preprocess the current file */
@ -3698,8 +3668,8 @@ ST_FUNC int tcc_preprocess(TCCState *s1)
{ {
BufferedFile **iptr; BufferedFile **iptr;
int token_seen, spcs, level; int token_seen, spcs, level;
const char *p;
Sym *define_start; Sym *define_start;
const char *tokstr;
preprocess_init(s1); preprocess_init(s1);
ch = file->buf_ptr[0]; ch = file->buf_ptr[0];
@ -3761,16 +3731,14 @@ ST_FUNC int tcc_preprocess(TCCState *s1)
pp_line(s1, file, 0); pp_line(s1, file, 0);
} else if (tok == TOK_LINEFEED) { } else if (tok == TOK_LINEFEED) {
++file->line_ref; ++file->line_ref;
} else {
spcs = pp_need_space(token_seen, tok);
} }
tokstr = get_tok_str(tok, &tokc);
if (!spcs && need_space(token_seen, tok, tokstr))
++spcs;
while (spcs) while (spcs)
fputs(" ", s1->ppfp), --spcs; fputs(" ", s1->ppfp), --spcs;
fputs(tokstr, s1->ppfp); fputs(p = get_tok_str(tok, &tokc), s1->ppfp);
token_seen = pp_check_he0xE(tok, p);;
token_seen = tok;
} }
/* reset define stack, but keep -D and built-ins */ /* reset define stack, but keep -D and built-ins */
free_defines(define_start); free_defines(define_start);