From a1c139063b9157c804fd76eedb6a8060726fe0d5 Mon Sep 17 00:00:00 2001 From: seyko Date: Sun, 1 May 2016 05:43:57 +0300 Subject: [PATCH] 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 --- tccpp.c | 53 +++++++++++++++++++++++++++++--- tests/pp/02.expect | 5 +-- tests/pp/14.c | 11 +++++++ tests/pp/14.expect | 8 +++++ tests/pp/15.c | 21 +++++++++++++ tests/pp/15.expect | 6 ++++ tests/pp/Makefile | 6 ++-- tests/tests2/84-hex-float.c | 12 ++++++++ tests/tests2/84-hex-float.expect | 1 + 9 files changed, 114 insertions(+), 9 deletions(-) create mode 100644 tests/pp/14.c create mode 100644 tests/pp/14.expect create mode 100644 tests/pp/15.c create mode 100644 tests/pp/15.expect create mode 100644 tests/tests2/84-hex-float.c create mode 100644 tests/tests2/84-hex-float.expect diff --git a/tccpp.c b/tccpp.c index 94f0b5f..cd50f1d 100644 --- a/tccpp.c +++ b/tccpp.c @@ -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); diff --git a/tests/pp/02.expect b/tests/pp/02.expect index 16f42ae..625c80b 100644 --- a/tests/pp/02.expect +++ b/tests/pp/02.expect @@ -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); diff --git a/tests/pp/14.c b/tests/pp/14.c new file mode 100644 index 0000000..8611479 --- /dev/null +++ b/tests/pp/14.c @@ -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); +} diff --git a/tests/pp/14.expect b/tests/pp/14.expect new file mode 100644 index 0000000..138f97b --- /dev/null +++ b/tests/pp/14.expect @@ -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; +} diff --git a/tests/pp/15.c b/tests/pp/15.c new file mode 100644 index 0000000..28a12bd --- /dev/null +++ b/tests/pp/15.c @@ -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; diff --git a/tests/pp/15.expect b/tests/pp/15.expect new file mode 100644 index 0000000..7ccee4a --- /dev/null +++ b/tests/pp/15.expect @@ -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; diff --git a/tests/pp/Makefile b/tests/pp/Makefile index 5694ebb..7f42a3e 100644 --- a/tests/pp/Makefile +++ b/tests/pp/Makefile @@ -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 diff --git a/tests/tests2/84-hex-float.c b/tests/tests2/84-hex-float.c new file mode 100644 index 0000000..0ef09bf --- /dev/null +++ b/tests/tests2/84-hex-float.c @@ -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); +} diff --git a/tests/tests2/84-hex-float.expect b/tests/tests2/84-hex-float.expect new file mode 100644 index 0000000..2175385 --- /dev/null +++ b/tests/tests2/84-hex-float.expect @@ -0,0 +1 @@ +n+1 = 15