forked from Mirrors/tinycc
normalize inc dirs, symplify include_next
include dirs are prepared as in gcc - for each duplicate path keep just the first one - remove each include_path that exists in sysinclude_paths include_next streamlined by introducing inc_path_index in the BufferedFilemaster
parent
45bc505968
commit
a6276b7a78
80
libtcc.c
80
libtcc.c
|
@ -841,6 +841,7 @@ ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen)
|
||||||
normalize_slashes(bf->filename);
|
normalize_slashes(bf->filename);
|
||||||
#endif
|
#endif
|
||||||
bf->line_num = 1;
|
bf->line_num = 1;
|
||||||
|
bf->inc_path_index = -2;
|
||||||
bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
|
bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
|
||||||
bf->fd = -1;
|
bf->fd = -1;
|
||||||
bf->prev = file;
|
bf->prev = file;
|
||||||
|
@ -1508,6 +1509,84 @@ LIBTCCAPI int tcc_add_symbol(TCCState *s, const char *name, const void *val)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
ST_FUNC void tcc_mormalize_inc_dirs(TCCState *s)
|
||||||
|
{
|
||||||
|
/* check a preprocessor include dirs and remove not
|
||||||
|
existing dirs and duplicates:
|
||||||
|
- for each duplicate path keep just the first one
|
||||||
|
- remove each include_path that exists in sysinclude_paths
|
||||||
|
*/
|
||||||
|
struct stat sysinc_stats[50]; // we don't use VLA in tcc code
|
||||||
|
struct stat inc_stats[50];
|
||||||
|
int num_sysinc = s->nb_sysinclude_paths;
|
||||||
|
int num_inc = s->nb_include_paths;
|
||||||
|
char** pp;
|
||||||
|
int i, j, r;
|
||||||
|
|
||||||
|
if ((num_sysinc > 50) || (num_inc > 50)) {
|
||||||
|
tcc_warning("fix a max number of the inc dirs");
|
||||||
|
if (num_sysinc > 50) num_sysinc = 50;
|
||||||
|
if (num_inc > 50) num_inc = 50;
|
||||||
|
}
|
||||||
|
|
||||||
|
pp = s->sysinclude_paths;
|
||||||
|
for (i=0; i < num_sysinc; i++) {
|
||||||
|
struct stat *st = &sysinc_stats [i];
|
||||||
|
r = stat( pp [i], st);
|
||||||
|
if (r || !S_ISDIR(st->st_mode)) {
|
||||||
|
tcc_free( pp[i] );
|
||||||
|
pp[i] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pp = s->include_paths;
|
||||||
|
for (i=0; i < num_inc; i++) {
|
||||||
|
struct stat *st = &inc_stats [i];
|
||||||
|
r = stat( pp [i], st);
|
||||||
|
if (r || !S_ISDIR(st->st_mode)) {
|
||||||
|
tcc_free( pp[i] );
|
||||||
|
pp[i] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i=0; i < num_inc; i++) {
|
||||||
|
struct stat *st1 = &inc_stats [i];
|
||||||
|
for (j=i+1; j < num_inc; j++) {
|
||||||
|
struct stat *st2 = &inc_stats [j];
|
||||||
|
if ((st1->st_dev == st2->st_dev) && (st1->st_ino == st2->st_ino)) {
|
||||||
|
pp = &s->include_paths[j];
|
||||||
|
if (*pp) {
|
||||||
|
tcc_free( *pp );
|
||||||
|
*pp = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (j=0; i < num_sysinc; i++) {
|
||||||
|
struct stat *st2 = &sysinc_stats [j];
|
||||||
|
if ((st1->st_dev == st2->st_dev) && (st1->st_ino == st2->st_ino)) {
|
||||||
|
pp = &s->include_paths[i];
|
||||||
|
if (*pp) {
|
||||||
|
tcc_free( *pp );
|
||||||
|
*pp = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (i=0; i < num_sysinc; i++) {
|
||||||
|
struct stat *st1 = &sysinc_stats [i];
|
||||||
|
for (j=i+1; j < num_sysinc; j++) {
|
||||||
|
struct stat *st2 = &sysinc_stats [j];
|
||||||
|
if ((st1->st_dev == st2->st_dev) && (st1->st_ino == st2->st_ino)) {
|
||||||
|
pp = &s->sysinclude_paths[j];
|
||||||
|
if (*pp) {
|
||||||
|
tcc_free( *pp );
|
||||||
|
*pp = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type)
|
LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type)
|
||||||
{
|
{
|
||||||
s->output_type = output_type;
|
s->output_type = output_type;
|
||||||
|
@ -1573,6 +1652,7 @@ LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
tcc_mormalize_inc_dirs(s);
|
||||||
if (s->output_type == TCC_OUTPUT_PREPROCESS)
|
if (s->output_type == TCC_OUTPUT_PREPROCESS)
|
||||||
print_defines();
|
print_defines();
|
||||||
|
|
||||||
|
|
1
tcc.h
1
tcc.h
|
@ -501,6 +501,7 @@ typedef struct BufferedFile {
|
||||||
struct BufferedFile *prev;
|
struct BufferedFile *prev;
|
||||||
int line_num; /* current line number - here to simplify code */
|
int line_num; /* current line number - here to simplify code */
|
||||||
int line_ref; /* tcc -E: last printed line */
|
int line_ref; /* tcc -E: last printed line */
|
||||||
|
int inc_path_index;
|
||||||
int ifndef_macro; /* #ifndef macro / #endif search */
|
int ifndef_macro; /* #ifndef macro / #endif search */
|
||||||
int ifndef_macro_saved; /* saved ifndef_macro */
|
int ifndef_macro_saved; /* saved ifndef_macro */
|
||||||
int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */
|
int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */
|
||||||
|
|
87
tccpp.c
87
tccpp.c
|
@ -1544,7 +1544,6 @@ ST_FUNC void preprocess(int is_bof)
|
||||||
int i, c, n, saved_parse_flags;
|
int i, c, n, saved_parse_flags;
|
||||||
char buf[1024], *q;
|
char buf[1024], *q;
|
||||||
Sym *s;
|
Sym *s;
|
||||||
char *p3 = 0;
|
|
||||||
|
|
||||||
saved_parse_flags = parse_flags;
|
saved_parse_flags = parse_flags;
|
||||||
parse_flags = PARSE_FLAG_PREPROCESS
|
parse_flags = PARSE_FLAG_PREPROCESS
|
||||||
|
@ -1631,19 +1630,15 @@ ST_FUNC void preprocess(int is_bof)
|
||||||
|
|
||||||
if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
|
if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
|
||||||
tcc_error("#include recursion too deep");
|
tcc_error("#include recursion too deep");
|
||||||
/* store current file in stack, but increment stack later below */
|
|
||||||
*s1->include_stack_ptr = file;
|
|
||||||
|
|
||||||
#ifdef INC_DEBUG
|
i = -2;
|
||||||
if (tok == TOK_INCLUDE_NEXT)
|
if (tok == TOK_INCLUDE_NEXT)
|
||||||
printf("%s (1) include_next file %s\n", file->filename, buf);
|
i = file->inc_path_index + 1;
|
||||||
#endif
|
|
||||||
|
|
||||||
n = s1->nb_include_paths + s1->nb_sysinclude_paths;
|
n = s1->nb_include_paths + s1->nb_sysinclude_paths;
|
||||||
for (i = -2; i < n; ++i) {
|
for (; i < n; ++i) {
|
||||||
char buf1[sizeof file->filename];
|
char buf1[sizeof file->filename];
|
||||||
CachedInclude *e;
|
CachedInclude *e;
|
||||||
BufferedFile **f;
|
|
||||||
const char *path;
|
const char *path;
|
||||||
|
|
||||||
if (i == -2) {
|
if (i == -2) {
|
||||||
|
@ -1666,86 +1661,36 @@ ST_FUNC void preprocess(int is_bof)
|
||||||
path = s1->include_paths[i];
|
path = s1->include_paths[i];
|
||||||
else
|
else
|
||||||
path = s1->sysinclude_paths[i - s1->nb_include_paths];
|
path = s1->sysinclude_paths[i - s1->nb_include_paths];
|
||||||
|
if (path == 0) continue;
|
||||||
pstrcpy(buf1, sizeof(buf1), path);
|
pstrcpy(buf1, sizeof(buf1), path);
|
||||||
pstrcat(buf1, sizeof(buf1), "/");
|
pstrcat(buf1, sizeof(buf1), "/");
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef INC_DEBUG
|
|
||||||
if (tok == TOK_INCLUDE_NEXT)
|
|
||||||
printf("%s (2) include_next path <%s> name <%s>\n", file->filename, buf1, buf);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
pstrcat(buf1, sizeof(buf1), buf);
|
pstrcat(buf1, sizeof(buf1), buf);
|
||||||
|
|
||||||
if (tok == TOK_INCLUDE_NEXT) {
|
|
||||||
char *p1 = buf1;
|
|
||||||
if ((p1[0] == '.') && IS_DIRSEP(p1[1])) p1 += 2;
|
|
||||||
|
|
||||||
if (p3) {
|
|
||||||
if (0 == PATHCMP(p1, p3)) {
|
|
||||||
#ifdef INC_DEBUG
|
|
||||||
printf("%s (3) include_next skipping <%s>\n", file->filename, p1);
|
|
||||||
#endif
|
|
||||||
goto include_trynext;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
for (f = s1->include_stack_ptr; f >= s1->include_stack; --f) {
|
|
||||||
char *p2 = (*f)->filename;
|
|
||||||
if ((p2[0] == '.') && IS_DIRSEP(p2[1])) p2 += 2;
|
|
||||||
if (0 == PATHCMP(p2, p1)) {
|
|
||||||
p3 = p2;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#ifdef INC_DEBUG
|
|
||||||
printf("%s: (4) include_next skipping <%s> (p3=%p)\n", file->filename, buf1, p3);
|
|
||||||
#endif
|
|
||||||
goto include_trynext;
|
|
||||||
}
|
|
||||||
#ifdef INC_DEBUG
|
|
||||||
printf("%s (5) include_next considering <%s>\n", file->filename, buf1);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
e = search_cached_include(s1, buf1);
|
e = search_cached_include(s1, buf1);
|
||||||
if (e && (define_find(e->ifndef_macro) || e->ifndef_macro == TOK_once)) {
|
if (e && (define_find(e->ifndef_macro) || e->ifndef_macro == TOK_once))
|
||||||
/* no need to parse the include because the 'ifndef macro'
|
break; /* no need to parse the include */
|
||||||
is defined */
|
|
||||||
|
|
||||||
#ifdef INC_DEBUG
|
if (tcc_open(s1, buf1) < 0)
|
||||||
printf("%s: skipping cached <%s>\n", file->filename, buf1);
|
|
||||||
#endif
|
|
||||||
goto include_done;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tcc_open(s1, buf1) < 0) {
|
|
||||||
#ifdef INC_DEBUG
|
|
||||||
printf("%s: include open failed <%s>\n", file->filename, buf1);
|
|
||||||
#endif
|
|
||||||
include_trynext:
|
|
||||||
continue;
|
continue;
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef INC_DEBUG
|
file->inc_path_index = i;
|
||||||
printf("%s: including <%s>\n", file->prev->filename, file->filename);
|
*(s1->include_stack_ptr++) = file->prev;
|
||||||
#endif
|
|
||||||
|
|
||||||
/* update target deps */
|
/* update target deps */
|
||||||
dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps,
|
dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps,
|
||||||
tcc_strdup(buf1));
|
tcc_strdup(buf1));
|
||||||
/* push current file in stack */
|
|
||||||
++s1->include_stack_ptr;
|
|
||||||
/* add include file debug info */
|
/* add include file debug info */
|
||||||
if (s1->do_debug)
|
if (s1->do_debug)
|
||||||
put_stabs(file->filename, N_BINCL, 0, 0, 0);
|
put_stabs(file->filename, N_BINCL, 0, 0, 0);
|
||||||
|
|
||||||
tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
|
tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
|
||||||
ch = file->buf_ptr[0];
|
ch = file->buf_ptr[0];
|
||||||
goto the_end;
|
break;
|
||||||
}
|
}
|
||||||
tcc_error("include file '%s' not found", buf);
|
if (i >= n) tcc_error("include file '%s' not found", buf);
|
||||||
include_done:
|
goto the_end;
|
||||||
break;
|
|
||||||
case TOK_IFNDEF:
|
case TOK_IFNDEF:
|
||||||
c = 1;
|
c = 1;
|
||||||
goto do_ifdef;
|
goto do_ifdef;
|
||||||
|
|
Loading…
Reference in New Issue