tccpp: cleanup #include_next

tcc_normalize_inc_dirs: normally no problem to be absolutly
gcc compatible as long as it can be done the tiny way.

This reverts to the state before recent related commits and
reimplements a (small) part of it to fix the reported problem.


Also: Revert "parsing "..." sequence"
c3975cf27c

	&& p[1] == '.'

is not a reliable way to lookahead
master
grischka 2015-11-20 12:05:55 +01:00
parent 54cf57ab1a
commit 0b3612631f
3 changed files with 46 additions and 127 deletions

View File

@ -841,7 +841,6 @@ 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;
@ -1509,96 +1508,6 @@ LIBTCCAPI int tcc_add_symbol(TCCState *s, const char *name, const void *val)
return 0; return 0;
} }
/* Windows stat* ( https://msdn.microsoft.com/en-us/library/14h5k7ff.aspx ):
* - st_gid, st_ino, st_uid: only valid on "unix" file systems (not FAT, NTFS, etc)
* - st_atime, st_ctime: not valid on FAT, valid on NTFS.
* - Other fields should be reasonably compatible (and S_ISDIR should work).
*
* BY_HANDLE_FILE_INFORMATION ( https://msdn.microsoft.com/en-us/library/windows/desktop/aa363788%28v=vs.85%29.aspx ):
* - File index (combined nFileIndexHigh and nFileIndexLow) _may_ change when the file is opened.
* - But on NTFS: it's guaranteed to be the same value until the file is deleted.
* - On windows server 2012 there's a 128b file id, and the 64b one via
* nFileIndex* is not guaranteed to be unique.
*
* - MS Docs suggest to that volume number with the file index could be used to
* check if two handles refer to the same file.
*/
#ifndef _WIN32
typedef struct stat file_info_t;
#else
typedef BY_HANDLE_FILE_INFORMATION file_info_t;
#endif
int get_file_info(const char *fname, file_info_t *out_info)
{
#ifndef _WIN32
return stat(fname, out_info);
#else
int rv = 1;
HANDLE h = CreateFile(fname, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (h != INVALID_HANDLE_VALUE) {
rv = !GetFileInformationByHandle(h, out_info);
CloseHandle(h);
}
return rv;
#endif
}
int is_dir(file_info_t *info)
{
#ifndef _WIN32
return S_ISDIR(info->st_mode);
#else
return (info->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ==
FILE_ATTRIBUTE_DIRECTORY;
#endif
}
int is_same_file(const file_info_t *fi1, const file_info_t *fi2)
{
#ifndef _WIN32
return fi1->st_dev == fi2->st_dev &&
fi1->st_ino == fi2->st_ino;
#else
return fi1->dwVolumeSerialNumber == fi2->dwVolumeSerialNumber &&
fi1->nFileIndexHigh == fi2->nFileIndexHigh &&
fi1->nFileIndexLow == fi2->nFileIndexLow;
#endif
}
static void
tcc_normalize_inc_dirs_aux(file_info_t *stats, size_t *pnum, char **path)
{
size_t i, num = *pnum;
if (get_file_info(*path, &stats[num]) || !is_dir(&stats[num]))
goto remove;
for (i = 0; i < num; i++)
if (is_same_file(&stats[i], &stats[num]))
goto remove;
*pnum = num + 1;
return;
remove:
tcc_free(*path);
*path = 0;
}
/* Remove non-existent and duplicate directories from include paths. */
ST_FUNC void tcc_normalize_inc_dirs(TCCState *s)
{
file_info_t *stats =
tcc_malloc(((size_t)s->nb_sysinclude_paths + s->nb_include_paths) *
sizeof(*stats));
size_t i, num = 0;
for (i = 0; i < s->nb_sysinclude_paths; i++)
tcc_normalize_inc_dirs_aux(stats, &num, &s->sysinclude_paths[i]);
for (i = 0; i < s->nb_include_paths; i++)
tcc_normalize_inc_dirs_aux(stats, &num, &s->include_paths[i]);
tcc_free(stats);
}
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;
@ -1664,7 +1573,6 @@ LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type)
} }
#endif #endif
tcc_normalize_inc_dirs(s);
if (s->output_type == TCC_OUTPUT_PREPROCESS) if (s->output_type == TCC_OUTPUT_PREPROCESS)
print_defines(); print_defines();

2
tcc.h
View File

@ -536,10 +536,10 @@ 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 */
int include_next_index; /* next search path */
char filename[1024]; /* filename */ char filename[1024]; /* filename */
unsigned char unget[4]; unsigned char unget[4];
unsigned char buffer[1]; /* extra size for CH_EOB char */ unsigned char buffer[1]; /* extra size for CH_EOB char */

79
tccpp.c
View File

@ -1578,13 +1578,14 @@ ST_FUNC void preprocess(int is_bof)
case TOK_INCLUDE: case TOK_INCLUDE:
case TOK_INCLUDE_NEXT: case TOK_INCLUDE_NEXT:
ch = file->buf_ptr[0]; ch = file->buf_ptr[0];
skip_spaces(); /* XXX: incorrect if comments : use next_nomacro with a special mode */ /* XXX: incorrect if comments : use next_nomacro with a special mode */
c = 0; skip_spaces();
if (ch == '<') if (ch == '<') {
c = '>'; c = '>';
if (ch == '\"') goto read_name;
} else if (ch == '\"') {
c = ch; c = ch;
if (c) { read_name:
inp(); inp();
q = buf; q = buf;
while (ch != c && ch != '\n' && ch != CH_EOF) { while (ch != c && ch != '\n' && ch != CH_EOF) {
@ -1598,6 +1599,12 @@ ST_FUNC void preprocess(int is_bof)
} }
*q = '\0'; *q = '\0';
minp(); minp();
#if 0
/* eat all spaces and comments after include */
/* XXX: slightly incorrect */
while (ch1 != '\n' && ch1 != CH_EOF)
inp();
#endif
} else { } else {
/* computed #include : either we have only strings or /* computed #include : either we have only strings or
we have anything enclosed in '<>' */ we have anything enclosed in '<>' */
@ -1631,25 +1638,22 @@ 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 */
i = -2; *s1->include_stack_ptr = file;
if (tok == TOK_INCLUDE_NEXT) i = tok == TOK_INCLUDE_NEXT ? file->include_next_index : 0;
i = file->inc_path_index + 1; n = 2 + s1->nb_include_paths + s1->nb_sysinclude_paths;
n = s1->nb_include_paths + s1->nb_sysinclude_paths;
for (; i < n; ++i) { for (; i < n; ++i) {
char buf1[sizeof file->filename]; char buf1[sizeof file->filename];
CachedInclude *e; CachedInclude *e;
const char *path; const char *path;
if (i == -2) { if (i == 0) {
/* check absolute include path */ /* check absolute include path */
if (!IS_ABSPATH(buf)) if (!IS_ABSPATH(buf))
continue; continue;
buf1[0] = 0; buf1[0] = 0;
i = n - 1; /* force end loop */
} else if (i == -1) { } else if (i == 1) {
/* search in current dir if "header.h" */ /* search in current dir if "header.h" */
if (c != '\"') if (c != '\"')
continue; continue;
@ -1658,40 +1662,45 @@ ST_FUNC void preprocess(int is_bof)
} else { } else {
/* search in all the include paths */ /* search in all the include paths */
if (i < s1->nb_include_paths) int j = i - 2, k = j - s1->nb_include_paths;
path = s1->include_paths[i]; path = k < 0 ? s1->include_paths[j] : s1->sysinclude_paths[k];
else
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), "/");
} }
pstrcat(buf1, sizeof(buf1), buf); pstrcat(buf1, sizeof(buf1), buf);
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)) {
break; /* no need to parse the include */ /* no need to parse the include because the 'ifndef macro'
is defined */
#ifdef INC_DEBUG
printf("%s: skipping cached %s\n", file->filename, buf1);
#endif
goto include_done;
}
if (tcc_open(s1, buf1) < 0) if (tcc_open(s1, buf1) < 0)
continue; continue;
file->inc_path_index = i; file->include_next_index = i + 1;
*(s1->include_stack_ptr++) = file->prev; #ifdef INC_DEBUG
printf("%s: including %s\n", file->prev->filename, file->filename);
#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];
break; goto the_end;
} }
if (i >= n) tcc_error("include file '%s' not found", buf); tcc_error("include file '%s' not found", buf);
goto the_end; include_done:
break;
case TOK_IFNDEF: case TOK_IFNDEF:
c = 1; c = 1;
goto do_ifdef; goto do_ifdef;
@ -1706,9 +1715,9 @@ ST_FUNC void preprocess(int is_bof)
tcc_error("invalid argument for '#if%sdef'", c ? "n" : ""); tcc_error("invalid argument for '#if%sdef'", c ? "n" : "");
if (is_bof) { if (is_bof) {
if (c) { if (c) {
#ifdef INC_DEBUG #ifdef INC_DEBUG
printf("#ifndef %s\n", get_tok_str(tok, NULL)); printf("#ifndef %s\n", get_tok_str(tok, NULL));
#endif #endif
file->ifndef_macro = tok; file->ifndef_macro = tok;
} }
} }
@ -2538,8 +2547,10 @@ maybe_newline:
cstr_reset(&tokcstr); cstr_reset(&tokcstr);
cstr_ccat(&tokcstr, '.'); cstr_ccat(&tokcstr, '.');
goto parse_num; goto parse_num;
} else if ((c == '.') && (p[1] == '.')){ } else if (c == '.') {
PEEKC(c, p); PEEKC(c, p);
if (c != '.')
expect("'.'");
PEEKC(c, p); PEEKC(c, p);
tok = TOK_DOTS; tok = TOK_DOTS;
} else { } else {