output space after TOK_PPNUM which followed by '+' or '-'

* correct -E output for the case ++ + ++ concatenation
        do this only for expanded from macro string
        and only when tcc_state->output_type == TCC_OUTPUT_PREPROCESS
This commit is contained in:
seyko 2016-05-01 05:43:57 +03:00
parent 256078933c
commit a1c139063b
9 changed files with 114 additions and 9 deletions

53
tccpp.c
View File

@ -48,6 +48,7 @@ static TokenSym *hash_ident[TOK_HASH_SIZE];
static char token_buf[STRING_MAX_SIZE + 1];
static CString cstr_buf;
static TokenString tokstr_buf;
static TokenString tokstr_buf2;
static unsigned char isidnum_table[256 - CH_EOF];
/* isidnum_table flags: */
#define IS_SPC 1
@ -3539,10 +3540,51 @@ ST_FUNC void next(void)
/* if reading from file, try to substitute macros */
s = define_find(tok);
if (s) {
Sym *nested_list = NULL;
tokstr_buf.len = 0;
nested_list = NULL;
macro_subst_tok(&tokstr_buf, &nested_list, s, 1);
if (tcc_state->output_type == TCC_OUTPUT_PREPROCESS)
{
int t = 0;
Sym *nested_list = NULL;
tokstr_buf2.len = 0;
macro_subst_tok(&tokstr_buf2, &nested_list, s, 1);
next_nomacro_spc();
if (tok >= TOK_IDENT) {
s = define_find(tok);
if (s) {
nested_list = NULL;
macro_subst_tok(&tokstr_buf2, &nested_list, s, 1);
} else
tok_str_add_tok(&tokstr_buf2);
} else
tok_str_add_tok(&tokstr_buf2);
{
const int *str = tokstr_buf2.str;
const int *str1 = tokstr_buf2.str + tokstr_buf2.len;
int tok = 0;
CValue cval;
tokstr_buf.len = 0;
while (str < str1) {
if (tok != TOK_LINENUM)
t = tok;
TOK_GET(&tok, &str, &cval);
if ((t == TOK_PPNUM) && ((tok == '+') || (tok == '-'))) {
tok_str_add(&tokstr_buf, ' ');
tok_str_add2(&tokstr_buf, tok, &cval);
} else
if ((t == TOK_INC || (t == TOK_DEC)) && ((tok == '+') || (tok == '-'))) {
tok_str_add(&tokstr_buf, tok);
tok_str_add(&tokstr_buf, ' ');
} else
tok_str_add2(&tokstr_buf, tok, &cval);
}
}
}
else {
Sym *nested_list = NULL;
tokstr_buf.len = 0;
macro_subst_tok(&tokstr_buf, &nested_list, s, 1);
}
tok_str_add(&tokstr_buf, 0);
begin_macro(&tokstr_buf, 2);
goto redo;
@ -3617,6 +3659,8 @@ ST_FUNC void preprocess_new(void)
cstr_realloc(&cstr_buf, STRING_MAX_SIZE);
tok_str_new(&tokstr_buf);
tok_str_realloc(&tokstr_buf, TOKSTR_MAX_SIZE);
tok_str_new(&tokstr_buf2);
tok_str_realloc(&tokstr_buf2, TOKSTR_MAX_SIZE);
tok_ident = TOK_IDENT;
p = tcc_keywords;
@ -3655,6 +3699,7 @@ ST_FUNC void preprocess_delete(void)
cstr_free(&tokcstr);
cstr_free(&cstr_buf);
tok_str_free(tokstr_buf.str);
tok_str_free(tokstr_buf2.str);
/* free allocators */
tal_delete(toksym_alloc);

View File

@ -1,5 +1,6 @@
f(2 * (y+1)) + f(2 * (f(2 * (z[0])))) % f(2 * (0)) + t(1);
f(2 * (2+(3,4)-0,1)) | f(2 * (~ 5)) & f(2 * (0,1))^m(0,1);
f(2 * (2 +(3,4)-0,1)) | f(2 * (~ 5)) & f(2 * (0,1))^m(0,1);
char c[2][6] = { "hello", "" };
f(2 * (y+1)) + f(2 * (f(2 * (z[0])))) % f(2 * (0)) + t(1);
f(2 * (2+(3,4)-0,1)) | f(2 * (~ 5)) & f(2 * (0,1))^m(0,1);
f(2 * (2 +(3,4)-0,1)) | f(2 * (~ 5)) & f(2 * (0,1))^m(0,1);

11
tests/pp/14.c Normal file
View File

@ -0,0 +1,11 @@
extern int printf(const char *format, ...);
#define P ++
#define n(x) x
int main(void)
{
int a = 0, b = -1;
int i1 = a P+P b;
printf("i1 = %d\n", i1);
return n(0x1e)n(-1);
}

8
tests/pp/14.expect Normal file
View File

@ -0,0 +1,8 @@
extern int printf(const char *format, ...);
int main(void)
{
int a = 0, b = -1;
int i1 = a +++ ++ b;
printf("i1 = %d\n", i1);
return 0x1e -1;
}

21
tests/pp/15.c Normal file
View File

@ -0,0 +1,21 @@
#define Y(x) Z(x)
#define X Y
X(1)
X(X(1))
X(X(X(X(X(1)))))
#define A B
#define B A
return A + B;
#undef A
#undef B
#define A B+1
#define B A
return A + B;
#define A1 B1+1
#define B1 C1+2
#define C1 A1+3
return A1 + B1;

6
tests/pp/15.expect Normal file
View File

@ -0,0 +1,6 @@
Z(1)
Z(Z(1))
Z(Z(Z(Z(Z(1)))))
return A + B;
return A+1 + B+1;
return A1+3 +2 +1 + B1+1 +3 +2;

View File

@ -6,17 +6,17 @@ TCC = ../../tcc
TESTS = $(patsubst %.c,%.test,$(wildcard *.c))
TESTS += $(patsubst %.S,%.test,$(wildcard *.S))
all test : $(TESTS)
all test : $(sort $(TESTS))
%.test: %.c %.expect
@echo PPTest $* ...
@$(TCC) -E -P $< >$*.output 2>&1 ; \
-@$(TCC) -E -P $< >$*.output 2>&1 ; \
diff -Nu -b -B -I "^#" $(EXTRA_DIFF_OPTS) $*.expect $*.output \
&& rm -f $*.output
%.test: %.S %.expect
@echo PPTest $* ...
@$(TCC) -E -P $< >$*.output 2>&1 ; \
-@$(TCC) -E -P $< >$*.output 2>&1 ; \
diff -Nu -b -B -I "^#" $(EXTRA_DIFF_OPTS) $*.expect $*.output \
&& rm -f $*.output

View File

@ -0,0 +1,12 @@
extern int printf(const char *format, ...);
#define ACPI_TYPE_INVALID 0x1E
#define NUM_NS_TYPES ACPI_TYPE_INVALID+1
int array[NUM_NS_TYPES];
#define n 0xe
int main()
{
printf("n+1 = %d\n", n+1);
// printf("n+1 = %d\n", 0xe+1);
}

View File

@ -0,0 +1 @@
n+1 = 15