>From e3f4b4e3c115274b61efafa6bab7171c7d504352 Mon Sep 17 00:00:00 2001 From: Daniel Holden Date: Tue, 14 Apr 2015 18:21:33 +0100 Subject: [PATCH] Fixed Dollars in Identifiers Patch Added tests for dollars in identifiers flag and also fixed issue with compile flags not being ready at the time of preprocessor construction. Added fix for syntax conflict with asm where identifier with $ as first character has different meaning. --- tccasm.c | 3 +- tccpp.c | 18 +++++++----- tests/Makefile | 2 +- tests/tests2/76_dollars_in_identifiers.c | 41 +++++++++++++++++++++++++++ tests/tests2/76_dollars_in_identifiers.expect | 14 +++++++++ tests/tests2/Makefile | 12 +++++--- 6 files changed, 77 insertions(+), 13 deletions(-) create mode 100644 tests/tests2/76_dollars_in_identifiers.c create mode 100644 tests/tests2/76_dollars_in_identifiers.expect diff --git a/tccasm.c b/tccasm.c index d9c929c..25277fe 100644 --- a/tccasm.c +++ b/tccasm.c @@ -747,7 +747,7 @@ static int tcc_assemble_internal(TCCState *s1, int do_preprocess) ch = file->buf_ptr[0]; tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF; - parse_flags = PARSE_FLAG_ASM_COMMENTS; + parse_flags |= PARSE_FLAG_ASM_COMMENTS; if (do_preprocess) parse_flags |= PARSE_FLAG_PREPROCESS; next(); @@ -853,6 +853,7 @@ static void tcc_assemble_inline(TCCState *s1, char *str, int len) memcpy(file->buffer, str, len); macro_ptr = NULL; + parse_flags |= PARSE_FLAG_ASM_FILE; tcc_assemble_internal(s1, 0); tcc_close(); diff --git a/tccpp.c b/tccpp.c index 111ea2b..d9300da 100644 --- a/tccpp.c +++ b/tccpp.c @@ -2287,9 +2287,11 @@ maybe_newline: } break; - /* treat $ as allowed char in indentifier */ - case '$': if (!tcc_state->dollars_in_identifiers) goto parse_simple; - + /* dollar is allowed to start identifiers when not parsing asm */ + case '$': + if (!tcc_state->dollars_in_identifiers + || (parse_flags & PARSE_FLAG_ASM_FILE)) goto parse_simple; + case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': case 'h': case 'i': case 'j': case 'k': case 'l': @@ -2312,7 +2314,8 @@ maybe_newline: p++; for(;;) { c = *p; - if (!isidnum_table[c-CH_EOF]) + if (!isidnum_table[c-CH_EOF] + && (tcc_state->dollars_in_identifiers ? c != '$' : 1)) break; h = TOK_HASH_FUNC(h, c); p++; @@ -2347,7 +2350,8 @@ maybe_newline: p--; PEEKC(c, p); parse_ident_slow: - while (isidnum_table[c-CH_EOF]) { + while (isidnum_table[c-CH_EOF] + || (tcc_state->dollars_in_identifiers ? c == '$' : 0)) { cstr_ccat(&tokcstr, c); PEEKC(c, p); } @@ -3176,9 +3180,9 @@ ST_FUNC void preprocess_new(void) const char *p, *r; /* init isid table */ + for(i=CH_EOF;i<256;i++) - isidnum_table[i-CH_EOF] = (isid(i) || isnum(i) || - (tcc_state->dollars_in_identifiers ? i == '$' : 0)); + isidnum_table[i-CH_EOF] = isid(i) || isnum(i); /* add all tokens */ if (table_ident) { diff --git a/tests/Makefile b/tests/Makefile index bc68de0..98ccb1e 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -199,7 +199,7 @@ ex%: $(top_srcdir)/examples/ex%.c # tiny assembler testing asmtest.ref: asmtest.S - $(CC) -Wa,-W -o asmtest.ref.o -c asmtest.S + $(CC) -m32 -Wa,-W -o asmtest.ref.o -c asmtest.S objdump -D asmtest.ref.o > asmtest.ref asmtest: asmtest.ref diff --git a/tests/tests2/76_dollars_in_identifiers.c b/tests/tests2/76_dollars_in_identifiers.c new file mode 100644 index 0000000..c5fcf99 --- /dev/null +++ b/tests/tests2/76_dollars_in_identifiers.c @@ -0,0 +1,41 @@ +#include + +#define $(x) x +#define $fred 10 +#define joe$ 20 +#define hen$y 30 + +#define $10(x) x*10 +#define _$10(x) x/10 + +int main() +{ + printf("fred=%d\n", $fred); + printf("joe=%d\n", joe$); + printf("henry=%d\n", hen$y); + + printf("fred2=%d\n", $($fred)); + printf("joe2=%d\n", $(joe$)); + printf("henry2=%d\n", $(hen$y)); + + printf("fred10=%d\n", $10($fred)); + printf("joe_10=%d\n", _$10(joe$)); + + int $ = 10; + int a100$ = 100; + int a$$ = 1000; + int a$c$b = 2121; + int $100 = 10000; + const char *$$$ = "money"; + + printf("local=%d\n", $); + printf("a100$=%d\n", a100$); + printf("a$$=%d\n", a$$); + printf("a$c$b=%d\n", a$c$b); + printf("$100=%d\n", $100); + printf("$$$=%s", $$$); + + return 0; +} + +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ diff --git a/tests/tests2/76_dollars_in_identifiers.expect b/tests/tests2/76_dollars_in_identifiers.expect new file mode 100644 index 0000000..4a20a52 --- /dev/null +++ b/tests/tests2/76_dollars_in_identifiers.expect @@ -0,0 +1,14 @@ +fred=10 +joe=20 +henry=30 +fred2=10 +joe2=20 +henry2=30 +fred10=100 +joe_10=2 +local=10 +a100$=100 +a$$=1000 +a$c$b=2121 +$100=10000 +$$$=money diff --git a/tests/tests2/Makefile b/tests/tests2/Makefile index 35ab3c1..a5a18fb 100644 --- a/tests/tests2/Makefile +++ b/tests/tests2/Makefile @@ -94,8 +94,8 @@ TESTS = \ 72_long_long_constant.test \ 73_arm64.test \ 74_nocode_wanted.test \ - 75_array_in_struct_init.test - + 75_array_in_struct_init.test \ + 76_dollars_in_identifiers.test # 34_array_assignment.test -- array assignment is not in C standard @@ -121,15 +121,19 @@ ARGS = 31_args.test : ARGS = arg1 arg2 arg3 arg4 arg5 46_grep.test : ARGS = '[^* ]*[:a:d: ]+\:\*-/: $$' 46_grep.c +# Some tests might need different flags +FLAGS = +76_dollars_in_identifiers.test : FLAGS = -fdollars-in-identifiers + all test: $(filter-out $(SKIP),$(TESTS)) %.test: %.c @echo Test: $*... - @$(TCC) -run $< $(ARGS) 2>&1 | grep -v 'warning: soft float ABI currently not supported: default to softfp' >$*.output || true + @$(TCC) -run $(FLAGS) $< $(ARGS) 2>&1 | grep -v 'warning: soft float ABI currently not supported: default to softfp' >$*.output || true @diff -Nbu $*.expect $*.output && rm -f $*.output - @($(TCC) $< -o $*.exe && ./$*.exe $(ARGS)) 2>&1 | grep -v 'warning: soft float ABI currently not supported: default to softfp' >$*.output2 || true + @($(TCC) $(FLAGS) $< -o $*.exe && ./$*.exe $(ARGS)) 2>&1 | grep -v 'warning: soft float ABI currently not supported: default to softfp' >$*.output2 || true @diff -Nbu $*.expect $*.output2 && rm -f $*.output2 $*.exe clean: -- 2.3.0