From 0b3612631f49ba0c008635aaa46ecf8be2eefcf7 Mon Sep 17 00:00:00 2001 From: grischka Date: Fri, 20 Nov 2015 12:05:55 +0100 Subject: [PATCH] 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" c3975cf27c792d493e76a2058a1eaf588324053e && p[1] == '.' is not a reliable way to lookahead --- libtcc.c | 92 -------------------------------------------------------- tcc.h | 2 +- tccpp.c | 79 +++++++++++++++++++++++++++--------------------- 3 files changed, 46 insertions(+), 127 deletions(-) diff --git a/libtcc.c b/libtcc.c index 4745018c..f51774fb 100644 --- a/libtcc.c +++ b/libtcc.c @@ -841,7 +841,6 @@ ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen) normalize_slashes(bf->filename); #endif bf->line_num = 1; - bf->inc_path_index = -2; bf->ifdef_stack_ptr = s1->ifdef_stack_ptr; bf->fd = -1; bf->prev = file; @@ -1509,96 +1508,6 @@ LIBTCCAPI int tcc_add_symbol(TCCState *s, const char *name, const void *val) 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) { s->output_type = output_type; @@ -1664,7 +1573,6 @@ LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type) } #endif - tcc_normalize_inc_dirs(s); if (s->output_type == TCC_OUTPUT_PREPROCESS) print_defines(); diff --git a/tcc.h b/tcc.h index 06b80fec..05e39657 100644 --- a/tcc.h +++ b/tcc.h @@ -536,10 +536,10 @@ typedef struct BufferedFile { struct BufferedFile *prev; int line_num; /* current line number - here to simplify code */ int line_ref; /* tcc -E: last printed line */ - int inc_path_index; int ifndef_macro; /* #ifndef macro / #endif search */ int ifndef_macro_saved; /* saved ifndef_macro */ int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */ + int include_next_index; /* next search path */ char filename[1024]; /* filename */ unsigned char unget[4]; unsigned char buffer[1]; /* extra size for CH_EOB char */ diff --git a/tccpp.c b/tccpp.c index d2e0e930..57e1b2ee 100644 --- a/tccpp.c +++ b/tccpp.c @@ -1578,13 +1578,14 @@ ST_FUNC void preprocess(int is_bof) case TOK_INCLUDE: case TOK_INCLUDE_NEXT: ch = file->buf_ptr[0]; - skip_spaces(); /* XXX: incorrect if comments : use next_nomacro with a special mode */ - c = 0; - if (ch == '<') + /* XXX: incorrect if comments : use next_nomacro with a special mode */ + skip_spaces(); + if (ch == '<') { c = '>'; - if (ch == '\"') + goto read_name; + } else if (ch == '\"') { c = ch; - if (c) { + read_name: inp(); q = buf; while (ch != c && ch != '\n' && ch != CH_EOF) { @@ -1598,6 +1599,12 @@ ST_FUNC void preprocess(int is_bof) } *q = '\0'; minp(); +#if 0 + /* eat all spaces and comments after include */ + /* XXX: slightly incorrect */ + while (ch1 != '\n' && ch1 != CH_EOF) + inp(); +#endif } else { /* computed #include : either we have only strings or 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) tcc_error("#include recursion too deep"); - - i = -2; - if (tok == TOK_INCLUDE_NEXT) - i = file->inc_path_index + 1; - - n = s1->nb_include_paths + s1->nb_sysinclude_paths; + /* store current file in stack, but increment stack later below */ + *s1->include_stack_ptr = file; + i = tok == TOK_INCLUDE_NEXT ? file->include_next_index : 0; + n = 2 + s1->nb_include_paths + s1->nb_sysinclude_paths; for (; i < n; ++i) { char buf1[sizeof file->filename]; CachedInclude *e; const char *path; - if (i == -2) { + if (i == 0) { /* check absolute include path */ if (!IS_ABSPATH(buf)) continue; buf1[0] = 0; - i = n - 1; /* force end loop */ - } else if (i == -1) { + } else if (i == 1) { /* search in current dir if "header.h" */ if (c != '\"') continue; @@ -1658,40 +1662,45 @@ ST_FUNC void preprocess(int is_bof) } else { /* search in all the include paths */ - if (i < s1->nb_include_paths) - path = s1->include_paths[i]; - else - path = s1->sysinclude_paths[i - s1->nb_include_paths]; - if (path == 0) continue; + int j = i - 2, k = j - s1->nb_include_paths; + path = k < 0 ? s1->include_paths[j] : s1->sysinclude_paths[k]; pstrcpy(buf1, sizeof(buf1), path); pstrcat(buf1, sizeof(buf1), "/"); } pstrcat(buf1, sizeof(buf1), buf); e = search_cached_include(s1, buf1); - if (e && (define_find(e->ifndef_macro) || e->ifndef_macro == TOK_once)) - break; /* no need to parse the include */ + if (e && (define_find(e->ifndef_macro) || e->ifndef_macro == TOK_once)) { + /* 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) continue; - file->inc_path_index = i; - *(s1->include_stack_ptr++) = file->prev; - + file->include_next_index = i + 1; +#ifdef INC_DEBUG + printf("%s: including %s\n", file->prev->filename, file->filename); +#endif /* update 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 */ if (s1->do_debug) put_stabs(file->filename, N_BINCL, 0, 0, 0); - tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL; ch = file->buf_ptr[0]; - break; + goto the_end; } - if (i >= n) tcc_error("include file '%s' not found", buf); - goto the_end; + tcc_error("include file '%s' not found", buf); +include_done: + break; case TOK_IFNDEF: c = 1; goto do_ifdef; @@ -1706,9 +1715,9 @@ ST_FUNC void preprocess(int is_bof) tcc_error("invalid argument for '#if%sdef'", c ? "n" : ""); if (is_bof) { if (c) { - #ifdef INC_DEBUG - printf("#ifndef %s\n", get_tok_str(tok, NULL)); - #endif +#ifdef INC_DEBUG + printf("#ifndef %s\n", get_tok_str(tok, NULL)); +#endif file->ifndef_macro = tok; } } @@ -2538,8 +2547,10 @@ maybe_newline: cstr_reset(&tokcstr); cstr_ccat(&tokcstr, '.'); goto parse_num; - } else if ((c == '.') && (p[1] == '.')){ + } else if (c == '.') { PEEKC(c, p); + if (c != '.') + expect("'.'"); PEEKC(c, p); tok = TOK_DOTS; } else {