tccpp: fix issues, add tests

* fix some macro expansion issues
* add some pp tests in tests/pp
* improved tcc -E output for better diff'ability
* remove -dD feature (quirky code, exotic feature,
  didn't work well)

Based partially on ideas / researches from PipCet

Some issues remain with VA_ARGS macros (if used in a
rather tricky way).

Also, to keep it simple, the pp doesn't automtically
add any extra spaces to separate tokens which otherwise
would form wrong tokens if re-read from tcc -E output
(such as '+' '=')  GCC does that, other compilers don't.

 * cleanups
  - #line 01 "file" / # 01 "file" processing
  - #pragma comment(lib,"foo")
  - tcc -E: forward some pragmas to output (pack, comment(lib))
  - fix macro parameter list parsing mess from
    a3fc543459
    a715d7143d
    (some coffee might help, next time ;)
  - introduce TOK_PPSTR - to have character constants as
    written in the file (similar to TOK_PPNUM)
  - allow '\' appear in macros
  - new functions begin/end_macro to:
      - fix switching macro levels during expansion
      - allow unget_tok to unget more than one tok
  - slight speedup by using bitflags in isidnum_table

Also:
  - x86_64.c : fix decl after statements
  - i386-gen,c : fix a vstack leak with VLA on windows
  - configure/Makefile : build on windows (MSYS) was broken
  - tcc_warning: fflush stderr to keep output order (win32)
This commit is contained in:
grischka
2015-05-09 14:29:39 +02:00
parent 70a6c4601e
commit 30df3189b1
49 changed files with 1060 additions and 988 deletions

View File

@ -19,7 +19,7 @@ TESTS = \
test3 \
abitest \
vla_test-run \
moretests
tests2-dir pp-dir
BTESTS = test1b test3b btest
@ -36,7 +36,7 @@ ifneq ($(ARCH),i386)
TESTS := $(filter-out $(BTESTS),$(TESTS))
endif
ifdef CONFIG_WIN32
TESTS := w32-prep $(filter-out $(BTESTS),$(TESTS))
TESTS := $(filter-out $(BTESTS),$(TESTS))
endif
ifeq ($(TARGETOS),Darwin)
TESTS := $(filter-out hello-exe test3 $(BTESTS),$(TESTS))
@ -60,9 +60,9 @@ ifeq ($(TARGETOS),Darwin)
endif
# run local version of tcc with local libraries and includes
TCCFLAGS = -B$(TOP) -I$(TOP) -I$(top_srcdir) -I$(top_srcdir)/include
TCCFLAGS = -B$(TOP) -I$(TOP) -I$(top_srcdir) -I$(top_srcdir)/include -L$(TOP)
ifdef CONFIG_WIN32
TCCFLAGS = -B$(top_srcdir)/win32 -I$(top_srcdir) -I$(top_srcdir)/include -I$(TOP) -L$(TOP)
TCCFLAGS = -B$(top_srcdir)/win32 -I$(top_srcdir) -I$(top_srcdir)/include -L$(TOP)
endif
XTCCFLAGS = -B$(TOP) -B$(top_srcdir)/win32 -I$(TOP) -I$(top_srcdir) -I$(top_srcdir)/include
@ -97,17 +97,14 @@ hello-run: ../examples/ex1.c
libtest: libtcc_test$(EXESUF) $(LIBTCC1)
@echo ------------ $@ ------------
./libtcc_test$(EXESUF) lib_path=..
./libtcc_test$(EXESUF) $(TCCFLAGS)
libtcc_test$(EXESUF): libtcc_test.c $(top_builddir)/$(LIBTCC)
$(CC) -o $@ $^ $(CPPFLAGS) $(CFLAGS) $(NATIVE_DEFINES) $(LIBS) $(LINK_LIBTCC) $(LDFLAGS) -I$(top_srcdir)
moretests:
%-dir:
@echo ------------ $@ ------------
$(MAKE) -C tests2
w32-prep:
cp ../libtcc1.a ../lib
$(MAKE) -k -C $*
# test.ref - generate using cc
test.ref: tcctest.c
@ -223,9 +220,8 @@ endif
abitest: $(ABITESTS)
@echo ------------ $@ ------------
./abitest-cc$(EXESUF) lib_path=.. include="$(top_srcdir)/include"
if [ "$(CONFIG_arm_eabi)" != "yes" ]; then \
./abitest-tcc$(EXESUF) lib_path=.. include="$(top_srcdir)/include"; fi
./abitest-cc$(EXESUF) $(TCCFLAGS)
if [ "$(CONFIG_arm_eabi)" != "yes" ]; then ./abitest-tcc$(EXESUF) $(TCCFLAGS); fi
vla_test$(EXESUF): vla_test.c
$(TCC) -o $@ $^ $(CPPFLAGS) $(CFLAGS)
@ -253,5 +249,4 @@ clean:
$(MAKE) -C tests2 $@
rm -vf *~ *.o *.a *.bin *.i *.ref *.out *.out? *.out?b *.cc \
*-cc *-tcc *.exe \
hello libtcc_test vla_test tcctest[1234] ex? tcc_g tcclib.h \
../lib/libtcc1.a
hello libtcc_test vla_test tcctest[1234] ex? tcc_g

View File

@ -13,8 +13,24 @@
#define LONG_DOUBLE_LITERAL(x) x ## L
#endif
static const char *tccdir = NULL;
static const char *include_dir = NULL;
static int g_argc;
static char **g_argv;
static void set_options(TCCState *s, int argc, char **argv)
{
int i;
for (i = 1; i < argc; ++i) {
char *a = argv[i];
if (a[0] == '-') {
if (a[1] == 'B')
tcc_set_lib_path(s, a+2);
else if (a[1] == 'I')
tcc_add_include_path(s, a+2);
else if (a[1] == 'L')
tcc_add_library_path(s, a+2);
}
}
}
typedef int (*callback_type) (void*);
@ -29,12 +45,9 @@ static int run_callback(const char *src, callback_type callback) {
s = tcc_new();
if (!s)
return -1;
if (tccdir)
tcc_set_lib_path(s, tccdir);
if (include_dir) {
if (tcc_add_include_path(s, include_dir) == -1)
return -1;
}
set_options(s, g_argc, g_argv);
if (tcc_set_output_type(s, TCC_OUTPUT_MEMORY) == -1)
return -1;
if (tcc_compile_string(s, src) == -1)
@ -611,13 +624,11 @@ int main(int argc, char **argv) {
/* if tcclib.h and libtcc1.a are not installed, where can we find them */
for (i = 1; i < argc; ++i) {
if (!memcmp(argv[i], "lib_path=",9))
tccdir = argv[i] + 9;
else if (!memcmp(argv[i], "run_test=", 9))
if (!memcmp(argv[i], "run_test=", 9))
testname = argv[i] + 9;
else if (!memcmp(argv[i], "include=", 8))
include_dir = argv[i] + 8;
}
}
g_argv = argv, g_argc = argc;
RUN_TEST(ret_int_test);
RUN_TEST(ret_longlong_test);

View File

@ -16,7 +16,7 @@ int add(int a, int b)
}
char my_program[] =
"#include <stdio.h> // printf()\n"
"#include <tcclib.h>\n" /* include the "Simple libc header for TCC" */
"extern int add(int a, int b);\n"
"int fib(int n)\n"
"{\n"
@ -37,6 +37,7 @@ char my_program[] =
int main(int argc, char **argv)
{
TCCState *s;
int i;
int (*func)(int);
s = tcc_new();
@ -46,8 +47,17 @@ int main(int argc, char **argv)
}
/* if tcclib.h and libtcc1.a are not installed, where can we find them */
if (argc == 2 && !memcmp(argv[1], "lib_path=",9))
tcc_set_lib_path(s, argv[1]+9);
for (i = 1; i < argc; ++i) {
char *a = argv[i];
if (a[0] == '-') {
if (a[1] == 'B')
tcc_set_lib_path(s, a+2);
else if (a[1] == 'I')
tcc_add_include_path(s, a+2);
else if (a[1] == 'L')
tcc_add_library_path(s, a+2);
}
}
/* MUST BE CALLED before any compilation */
tcc_set_output_type(s, TCC_OUTPUT_MEMORY);

6
tests/pp/01.c Normal file
View File

@ -0,0 +1,6 @@
#define hash_hash # ## #
#define mkstr(a) # a
#define in_between(a) mkstr(a)
#define join(c, d) in_between(c hash_hash d)
char p[] = join(x, y);
// char p[] = "x ## y";

1
tests/pp/01.expect Normal file
View File

@ -0,0 +1 @@
char p[] = "x ## y";

28
tests/pp/02.c Normal file
View File

@ -0,0 +1,28 @@
#define x 3
#define f(a) f(x * (a))
#undef x
#define x 2
#define g f
#define z z[0]
#define h g(~
#define m(a) a(w)
#define w 0,1
#define t(a) a
#define p() int
#define q(x) x
#define r(x,y) x ## y
#define str(x) # x
f(y+1) + f(f(z)) % t(t(g)(0) + t)(1);
g(x+(3,4)-w) | h 5) & m
(f)^m(m);
char c[2][6] = { str(hello), str() };
/*
* 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);
* char c[2][6] = { "hello", "" };
*/
#define L21 f(y+1) + f(f(z)) % t(t(g)(0) + t)(1);
#define L22 g(x+(3,4)-w) | h 5) & m\
(f)^m(m);
L21
L22

5
tests/pp/02.expect Normal file
View File

@ -0,0 +1,5 @@
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);
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);

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

@ -0,0 +1,15 @@
#define str(s) # s
#define xstr(s) str(s)
#define debug(s, t) printf("x" # s "= %d, x" # t "= %s", \
x ## s, x ## t)
#define INCFILE(n) vers ## n
#define glue(a, b) a ## b
#define xglue(a, b) glue(a, b)
#define HIGHLOW "hello"
#define LOW LOW ", world"
debug(1, 2);
fputs(str(strncmp("abc\0d", "abc", '\4') // this goes away
== 0) str(: @\n), s);
\#include xstr(INCFILE(2).h)
glue(HIGH, LOW);
xglue(HIGH, LOW)

5
tests/pp/03.expect Normal file
View File

@ -0,0 +1,5 @@
printf("x" "1" "= %d, x" "2" "= %s", x1, x2);
fputs("strncmp(\"abc\\0d\", \"abc\", '\\4') == 0" ": @\n", s);
\#include "vers2.h"
"hello";
"hello" ", world"

4
tests/pp/04.c Normal file
View File

@ -0,0 +1,4 @@
#define foobar 1
#define C(x,y) x##y
#define D(x) (C(x,bar))
D(foo)

1
tests/pp/04.expect Normal file
View File

@ -0,0 +1 @@
(1)

7
tests/pp/05.c Normal file
View File

@ -0,0 +1,7 @@
#define t(x,y,z) x ## y ## z
#define xxx(s) int s[] = { t(1,2,3), t(,4,5), t(6,,7), t(8,9,), \
t(10,,), t(,11,), t(,,12), t(,,) };
int j[] = { t(1,2,3), t(,4,5), t(6,,7), t(8,9,),
t(10,,), t(,11,), t(,,12), t(,,) };
xxx(j)

3
tests/pp/05.expect Normal file
View File

@ -0,0 +1,3 @@
int j[] = { 123, 45, 67, 89,
10, 11, 12, };
int j[] = { 123, 45, 67, 89, 10, 11, 12, };

5
tests/pp/06.c Normal file
View File

@ -0,0 +1,5 @@
#define X(a,b, \
c,d) \
foo
X(1,2,3,4)

1
tests/pp/06.expect Normal file
View File

@ -0,0 +1 @@
foo

4
tests/pp/07.c Normal file
View File

@ -0,0 +1,4 @@
#define a() YES
#define b() a
b()
b()()

2
tests/pp/07.expect Normal file
View File

@ -0,0 +1,2 @@
a
YES

4
tests/pp/08.c Normal file
View File

@ -0,0 +1,4 @@
// test macro expansion in arguments
#define s_pos s_s.s_pos
#define foo(x) (x)
foo(hej.s_pos)

1
tests/pp/08.expect Normal file
View File

@ -0,0 +1 @@
(hej.s_s.s_pos)

4
tests/pp/09.c Normal file
View File

@ -0,0 +1,4 @@
#define C(a,b,c) a##b##c
#define N(x,y) C(x,_,y)
#define A_O aaaaoooo
N(A,O)

1
tests/pp/09.expect Normal file
View File

@ -0,0 +1 @@
aaaaoooo

10
tests/pp/10.c Normal file
View File

@ -0,0 +1,10 @@
#define f(x) x
#define g(x) f(x) f(x
#define i(x) g(x)) g(x
#define h(x) i(x))) i(x
#define k(x) i(x))) i(x))))
f(x)
g(x))
i(x)))
h(x))))
k(x))))

5
tests/pp/10.expect Normal file
View File

@ -0,0 +1,5 @@
x
x x
x x x x
x x x x x x x x
x x x x x x x x))))

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

@ -0,0 +1,31 @@
#define D1(s, ...) s
#define D2(s, ...) s D1(__VA_ARGS__)
#define D3(s, ...) s D2(__VA_ARGS__)
#define D4(s, ...) s D3(__VA_ARGS__)
D1(a)
D2(a, b)
D3(a, b, c)
D4(a, b, c, d)
x D4(a, b, c, d) y
x D4(a, b, c) y
x D4(a, b) y
x D4(a) y
x D4() y
#define GNU_COMMA(X,Y...) X,## Y
x GNU_COMMA(A,B,C) y
x GNU_COMMA(A,B) y
x GNU_COMMA(A) y
x GNU_COMMA() y
#define __sun_attr___noreturn__ __attribute__((__noreturn__))
#define ___sun_attr_inner(__a) __sun_attr_##__a
#define __sun_attr__(__a) ___sun_attr_inner __a
#define __NORETURN __sun_attr__((__noreturn__))
__NORETURN
#define X(...)
#define Y(...) 1 __VA_ARGS__ 2
Y(X X() ())

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

@ -0,0 +1,15 @@
a
a b
a b c
a b c d
x a b c d y
x a b c y
x a b y
x a y
x y
x A,B,C y
x A,B y
x A y
x y
__attribute__((__noreturn__))
1 2

35
tests/pp/Makefile Normal file
View File

@ -0,0 +1,35 @@
#
# credits: 01..13.c from the pcc cpp-tests suite
#
TCC = ../../tcc
TESTS = $(patsubst %.c,%.test,$(wildcard *.c))
all test : $(TESTS)
%.test: %.c %.expect
@echo PPTest $* ...
@$(TCC) -E -P $< >$*.output 2>&1 ; \
diff -Nu -b -B -I "^#" $(EXTRA_DIFF_OPTS) $*.expect $*.output \
&& rm -f $*.output
# automatically generate .expect files with gcc:
%.expect :
gcc -E -P $*.c >$*.expect 2>&1
# tell make not to delete
.PRECIOUS: %.expect
clean:
rm -vf *.output
# 02.test : EXTRA_DIFF_OPTS = -w
# 03.test : EXTRA_DIFF_OPTS = -w
# 04.test : EXTRA_DIFF_OPTS = -w
# 10.test : EXTRA_DIFF_OPTS = -w
# diff options:
# -b ighore space changes
# -w ighore all whitespace
# -B ignore blank lines
# -I <RE> ignore lines matching RE

View File

@ -316,6 +316,7 @@ void macro_test(void)
printf("__LINE__=%d __FILE__=%s\n",
__LINE__, __FILE__);
#if 0
#line 200
printf("__LINE__=%d __FILE__=%s\n",
__LINE__, __FILE__);
@ -323,6 +324,7 @@ void macro_test(void)
printf("__LINE__=%d __FILE__=%s\n",
__LINE__, __FILE__);
#line 227 "tcctest.c"
#endif
/* not strictly preprocessor, but we test it there */
#ifdef C99_MACROS

View File

@ -1,2 +0,0 @@
#define paste(A,B) ##A B
paste(x,y)

View File

@ -1 +0,0 @@
65_macro_concat_start.c:1: error: '##' invalid at start of macro

View File

@ -1,2 +0,0 @@
#define paste(A,B) A B##
paste(x,y)

View File

@ -1 +0,0 @@
66_macro_concat_end.c:2: error: '##' invalid at end of macro

View File

@ -1,9 +0,0 @@
#include <stdio.h>
#define hexCh(c (c >= 10 ? 'a' + c - 10 : '0' + c)
int main(void)
{
int c = 0xa;
printf("hex: %c\n", hexCh(c));
return 0;
}

View File

@ -1 +0,0 @@
68_macro_param_list_err_1.c:2: error: '(' may not appear in parameter list

View File

@ -1,9 +0,0 @@
#include <stdio.h>
#define hexCh(c/3) (c >= 10 ? 'a' + c - 10 : '0' + c)
int main(void)
{
int c = 0xa;
printf("hex: %c\n", hexCh(c));
return 0;
}

View File

@ -1 +0,0 @@
69_macro_param_list_err_2.c:2: error: '/' may not appear in parameter list

View File

@ -13,5 +13,7 @@ int main()
printf("Error: 2147483647 < 0\n");
return 2;
}
else
printf("long long constant test ok.\n");
return 0;
}

View File

@ -0,0 +1 @@
long long constant test ok.

View File

@ -19,89 +19,9 @@ endif
TCC = $(TOP)/tcc $(TCCFLAGS)
TESTS = \
00_assignment.test \
01_comment.test \
02_printf.test \
03_struct.test \
04_for.test \
05_array.test \
06_case.test \
07_function.test \
08_while.test \
09_do_while.test \
10_pointer.test \
11_precedence.test \
12_hashdefine.test \
13_integer_literals.test \
14_if.test \
15_recursion.test \
16_nesting.test \
17_enum.test \
18_include.test \
19_pointer_arithmetic.test \
20_pointer_comparison.test \
21_char_array.test \
22_floating_point.test \
23_type_coercion.test \
24_math_library.test \
25_quicksort.test \
26_character_constants.test \
27_sizeof.test \
28_strings.test \
29_array_address.test \
30_hanoi.test \
31_args.test \
32_led.test \
33_ternary_op.test \
34_array_assignment.test \
35_sizeof.test \
36_array_initialisers.test \
37_sprintf.test \
38_multiple_array_index.test \
39_typedef.test \
40_stdio.test \
41_hashif.test \
42_function_pointer.test \
43_void_param.test \
44_scoped_declarations.test \
45_empty_for.test \
46_grep.test \
47_switch_return.test \
48_nested_break.test \
49_bracket_evaluation.test \
50_logical_second_arg.test \
51_static.test \
52_unnamed_enum.test \
54_goto.test \
55_lshift_type.test \
56_btype_excess-1.test \
57_btype_excess-2.test \
58_function_redefinition.test \
59_function_array.test \
60_enum_redefinition.test \
61_undefined_enum.test \
62_enumerator_redefinition.test \
63_local_enumerator_redefinition.test \
64_macro_nesting.test \
65_macro_concat_start.test \
66_macro_concat_end.test \
67_macro_concat.test \
68_macro_param_list_err_1.test \
69_macro_param_list_err_2.test \
70_floating_point_literals.test \
71_macro_empty_arg.test \
72_long_long_constant.test \
73_arm64.test \
74_nocode_wanted.test \
75_array_in_struct_init.test \
76_dollars_in_identifiers.test \
77_push_pop_macro.test \
78_vla_label.test \
79_vla_continue.test
TESTS = $(patsubst %.c,%.test,$(wildcard *.c))
# 34_array_assignment.test -- array assignment is not in C standard
SKIP = 34_array_assignment.test
# some tests do not pass on all platforms, remove them for now
@ -128,16 +48,29 @@ ARGS =
FLAGS =
76_dollars_in_identifiers.test : FLAGS = -fdollars-in-identifiers
# Filter some always-warning
FILTER =
ifeq (-$(findstring arm,$(ARCH))-,-arm-)
FILTER = 2>&1 | grep -v 'warning: soft float ABI currently not supported'
endif
all test: $(filter-out $(SKIP),$(TESTS))
%.test: %.c
%.test: %.c %.expect
@echo Test: $*...
@$(TCC) -run $(FLAGS) $< $(ARGS) 2>&1 | grep -v 'warning: soft float ABI currently not supported: default to softfp' >$*.output || true
# test -run
@$(TCC) $(FLAGS) -run $< $(ARGS) $(FILTER) >$*.output 2>&1 || true
@diff -Nbu $*.expect $*.output && rm -f $*.output
# test exe (disabled for speed)
# @($(TCC) $(FLAGS) $< -o $*.exe && ./$*.exe $(ARGS)) $(FiLTER) >$*.output2 2>&1 ; \
# diff -Nbu $*.expect $*.output2 && rm -f $*.output2 $*.exe
@($(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
# automatically generate .expect files with gcc:
%.expect :
(gcc $*.c -o a.exe && ./a.exe $(ARGS)) >$*.expect 2>&1; rm -f a.exe
# tell make not to delete
.PRECIOUS: %.expect
clean:
rm -vf fred.txt *.output* *.exe
rm -vf fred.txt *.output a.exe