tcc -dt -run ... : simpler is better

* -dt now with lowercase t

* test snippets now separated by real preprocessor statements
  which is valid C also for other compilers

    #if defined test_xxx
       < test snippet x >
    #elif defined test_yyy
       < test snippet y >
    #elif ...
    #endif

* simpler implementation, behaves like -run if no 'test_...' macros
  are seen, works with -E too

* for demonstration I combined some of the small tests for errors
  and warnings (56..63,74) in "60_errors_and_warnings.c"

Also:
* libtcc.c:
  put tcc_preprocess() and tcc_assemble() under the setjmp clause
  to let them return to caller after errors.  This is for -dt -E.
* tccgen.c:
  - get rid of save/restore_parse_state(), macro_ptr is saved
    by begin_macro anyway, now line_num too.
  - use expr_eq for parsing _Generic's controlling_type
  - set nocode_wanted with const_wanted. too, This is to keep
    VT_JMP on vtop when parsing preprocessor expressions.
* tccpp.c: tcc -E: suppress trailing whitespace from lines with
  comments (that -E removes) such as
       NO_GOTPLT_ENTRY,\t    /* never generate ... */
This commit is contained in:
grischka
2017-07-20 22:21:27 +02:00
parent ba2b25e4ea
commit 0cc24d0e84
31 changed files with 323 additions and 446 deletions

View File

@@ -543,111 +543,4 @@ ST_FUNC void gen_makedeps(TCCState *s, const char *target, const char *filename)
fclose(depout);
}
#ifdef TCC_IS_NATIVE
/* -------------------------------------------------------------- */
/* run test snippets from file */
static char *readfile(const char *fname)
{
char *buf;
int fsize;
FILE *fi;
fi = fopen(fname, "rb");
if (!fi)
return NULL;
fseek(fi, 0, SEEK_END);
fsize = ftell(fi);
fseek(fi, 0, SEEK_SET);
buf = tcc_malloc(fsize + 1);
fread(buf, fsize, 1, fi);
fclose(fi);
buf[fsize] = 0;
return buf;
}
static int run_prog(const char *prog, int ac, char **av)
{
TCCState *s;
int (*func)(int, char**);
int ret = -10000;
s = tcc_new();
tcc_parse_args(s, &ac, &av, 1);
tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
if (tcc_compile_string(s, prog) == -1)
goto done;
if (tcc_relocate(s, TCC_RELOCATE_AUTO) < 0)
goto done;
func = tcc_get_symbol(s, "main");
if (!func)
goto done;
ret = func(ac, av);
done:
tcc_delete(s);
return ret;
}
static char *trimback(char *a, char *e)
{
while (e > a && (unsigned char)e[-1] <= ' ')
--e;
*e = 0;;
return a;
}
ST_FUNC int tcc_tool_test(TCCState *s, int argc, char **argv)
{
const char *fname;
char *buf, *p, *a, *b, *e, tmp[100];
int r = 0, c, n;
const char sep[] = "/*-* test";
n = s->do_test - argc;
if (!n)
return 0;
fname = argv[0], argv -= n, argc += n;
buf = readfile(fname);
if (NULL == buf)
return -1;
p = strstr(buf, sep);
if (!p) {
tcc_free(buf);
return -1;
}
while (*p) {
a = p, p = strchr(p, '\n');
if (NULL == p)
break;
*p++ = 0;
b = p, p = strstr(p, sep);
if (NULL == p)
p = strchr(b, 0);
c = *p, *p = 0;
trimback(a, b);
if (r)
printf("\n");
printf("%s\n", a);
fflush(stdout);
e = a += sizeof sep - 5;
while (*e && *e != ':')
++e;
if (!*e || e - a > 32)
e = a + 4;
n = snprintf(tmp, sizeof tmp, "#line 1 \"%.*s\"\n", (int)(e - a), a);
if (b - buf >= n)
b = memcpy(b - n, tmp, n);
n = run_prog(b, argc, argv);
if (n != -10000)
printf("returns %d\n", n);
*p = c, ++r;
}
tcc_free(buf);
exit(0);
}
#endif /* TCC_IS_NATIVE */
/* -------------------------------------------------------------- */