cppinit.c (cpp_read_main_file): Split into two functions: Distribute _cpp_stack_file call over the two functions.

* cppinit.c (cpp_read_main_file):  Split into two functions:
	Distribute _cpp_stack_file call over the two functions.
	(cpp_find_main_file):  New function.
	Don't call _cpp_do_file_change even if working_directory flag set.
	(cpp_push_main_file):  New function.
	* cppfiles.c (_cpp_find_failed):  New helper function.
	(find_file):  Made non-static and renamed to _cpp_find_file.
	(_cpp_stack_file):  No longer needed.  But note the following.
	(stack_file):  Made non-static and renamed to _cpp_stack_file.
	* fix-header.c (cpp_read_main_file):  Replace cpp_read_main_file
	call with calls to cpp_find_main_file and cpp_push_main_file.
	(search_path_head):  If there is no current buffer, use main_file.
	* cpphash.h:  Update function declarations.
	* cpplib.h:  Update function declarations.

From-SVN: r72014
This commit is contained in:
Per Bothner 2003-10-02 07:23:27 +00:00 committed by Per Bothner
parent a506c55cb1
commit 4dc299fbbe
6 changed files with 83 additions and 63 deletions

View File

@ -9,6 +9,21 @@
* fix-header.c (cb_file_change): Likewise.
* c-ppoutput.c (pp_file_change): Likewise.
* cppinit.c (cpp_read_main_file): Split into two functions:
Distribute _cpp_stack_file call over the two functions.
(cpp_find_main_file): New function.
Don't call _cpp_do_file_change even if working_directory flag set.
(cpp_push_main_file): New function.
* cppfiles.c (_cpp_find_failed): New helper function.
(find_file): Made non-static and renamed to _cpp_find_file.
(_cpp_stack_file): No longer needed. But note the following.
(stack_file): Made non-static and renamed to _cpp_stack_file.
* fix-header.c (cpp_read_main_file): Replace cpp_read_main_file
call with calls to cpp_find_main_file and cpp_push_main_file.
(search_path_head): If there is no current buffer, use main_file.
* cpphash.h: Update function declarations.
* cpplib.h: Update function declarations.
* cpplex.c (_cpp_get_fresh_line): Revert my no-longer-needed
08-28 change, since we're never called with a NULL buffer.
(_cpp_lex_direct): Likewise.

View File

@ -54,7 +54,6 @@ Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/* This structure represents a file searched for by CPP, whether it
exists or not. An instance may be pointed to by more than one
file_hash_entry; at present no reference count is kept. */
typedef struct _cpp_file _cpp_file;
struct _cpp_file
{
/* Filename as given to #include or command line switch. */
@ -157,11 +156,8 @@ struct file_hash_entry
static bool open_file (_cpp_file *file);
static bool pch_open_file (cpp_reader *pfile, _cpp_file *file);
static bool find_file_in_dir (cpp_reader *pfile, _cpp_file *file);
static _cpp_file *find_file (cpp_reader *, const char *fname,
cpp_dir *start_dir, bool fake);
static bool read_file_guts (cpp_reader *pfile, _cpp_file *file);
static bool read_file (cpp_reader *pfile, _cpp_file *file);
static bool stack_file (cpp_reader *, _cpp_file *file, bool import);
static bool should_stack_file (cpp_reader *, _cpp_file *file, bool import);
static struct cpp_dir *search_path_head (cpp_reader *, const char *fname,
int angle_brackets, enum include_type);
@ -330,6 +326,12 @@ find_file_in_dir (cpp_reader *pfile, _cpp_file *file)
return false;
}
bool
_cpp_find_failed (_cpp_file *file)
{
return file->err_no != 0;
}
/* Given a filename FNAME search for such a file in the include path
starting from START_DIR. If FNAME is the empty string it is
interpreted as STDIN if START_DIR is PFILE->no_seach_path.
@ -344,8 +346,8 @@ find_file_in_dir (cpp_reader *pfile, _cpp_file *file)
had previously been closed. To open it again pass the return value
to open_file().
*/
static _cpp_file *
find_file (cpp_reader *pfile, const char *fname, cpp_dir *start_dir, bool fake)
_cpp_file *
_cpp_find_file (cpp_reader *pfile, const char *fname, cpp_dir *start_dir, bool fake)
{
struct file_hash_entry *entry, **hash_slot;
_cpp_file *file;
@ -594,8 +596,8 @@ should_stack_file (cpp_reader *pfile, _cpp_file *file, bool import)
stack if possible. IMPORT is true if this stacking attempt is
because of a #import directive. Returns true if a buffer is
stacked. */
static bool
stack_file (cpp_reader *pfile, _cpp_file *file, bool import)
bool
_cpp_stack_file (cpp_reader *pfile, _cpp_file *file, bool import)
{
cpp_buffer *buffer;
int sysp;
@ -619,8 +621,7 @@ stack_file (cpp_reader *pfile, _cpp_file *file, bool import)
/* Stack the buffer. */
buffer = cpp_push_buffer (pfile, file->buffer, file->st.st_size,
CPP_OPTION (pfile, preprocessed),
! pfile->buffer);
CPP_OPTION (pfile, preprocessed));
buffer->file = file;
/* Initialize controlling macro state. */
@ -654,7 +655,8 @@ search_path_head (cpp_reader *pfile, const char *fname, int angle_brackets,
if (IS_ABSOLUTE_PATH (fname))
return &pfile->no_search_path;
file = pfile->buffer->file;
/* pfile->buffer is NULL when processing an -include command-line flag. */
file = pfile->buffer == NULL ? pfile->main_file : pfile->buffer->file;
/* For #include_next, skip in the search path past the dir in which
the current file was found, but if it was found via an absolute
@ -698,16 +700,6 @@ dir_name_of_file (_cpp_file *file)
return file->dir_name;
}
/* Push an input buffer with the contents of FNAME, the empty string
for standard input. Return true if a buffer was stacked. */
bool
_cpp_stack_file (cpp_reader *pfile, const char *fname)
{
struct cpp_dir *dir = &pfile->no_search_path;
return stack_file (pfile, find_file (pfile, fname, dir, false), false);
}
/* Handles #include-family directives (distinguished by TYPE),
including HEADER, and the command line -imacros and -include.
Returns true if a buffer was stacked. */
@ -721,7 +713,7 @@ _cpp_stack_include (cpp_reader *pfile, const char *fname, int angle_brackets,
if (!dir)
return false;
return stack_file (pfile, find_file (pfile, fname, dir, false),
return _cpp_stack_file (pfile, _cpp_find_file (pfile, fname, dir, false),
type == IT_IMPORT);
}
@ -881,7 +873,7 @@ _cpp_cleanup_files (cpp_reader *pfile)
void
_cpp_fake_include (cpp_reader *pfile, const char *fname)
{
find_file (pfile, fname, pfile->buffer->file->dir, true);
_cpp_find_file (pfile, fname, pfile->buffer->file->dir, true);
}
/* Not everyone who wants to set system-header-ness on a buffer can
@ -963,7 +955,7 @@ _cpp_compare_file_date (cpp_reader *pfile, const char *fname,
if (!dir)
return -1;
file = find_file (pfile, fname, dir, false);
file = _cpp_find_file (pfile, fname, dir, false);
if (file->err_no)
return -1;

View File

@ -307,11 +307,6 @@ struct cpp_buffer
include files has been calculated and stored in "dir" below. */
unsigned char search_cached;
/* At EOF, a buffer is automatically popped. If RETURN_AT_EOF is
true, a CPP_EOF token is then returned. Otherwise, the next
token from the enclosing buffer is returned. */
bool return_at_eof;
/* The directory of the this buffer's file. Its NAME member is not
allocated, so we don't need to worry about freeing it. */
struct cpp_dir dir;
@ -362,6 +357,8 @@ struct cpp_reader
/* Chain of all hashed _cpp_file instances. */
struct _cpp_file *all_files;
struct _cpp_file *main_file;
/* File and directory hash table. */
struct htab *file_hash;
struct file_hash_entry *file_hash_entries;
@ -515,9 +512,13 @@ extern void _cpp_init_hashtable (cpp_reader *, hash_table *);
extern void _cpp_destroy_hashtable (cpp_reader *);
/* In cppfiles.c */
typedef struct _cpp_file _cpp_file;
extern _cpp_file *_cpp_find_file (cpp_reader *, const char *fname,
cpp_dir *start_dir, bool fake);
extern bool _cpp_find_failed (_cpp_file *);
extern void _cpp_mark_file_once_only (cpp_reader *, struct _cpp_file *);
extern void _cpp_fake_include (cpp_reader *, const char *);
extern bool _cpp_stack_file (cpp_reader *, const char *);
extern bool _cpp_stack_file (cpp_reader *, _cpp_file*, bool);
extern bool _cpp_stack_include (cpp_reader *, const char *, int,
enum include_type);
extern int _cpp_compare_file_date (cpp_reader *, const char *, int);

View File

@ -448,11 +448,10 @@ cpp_post_options (cpp_reader *pfile)
mark_named_operators (pfile);
}
/* Setup for processing input from the file named FNAME,
or stdin if it is the empty string. Return the original filename
on success (e.g. foo.i->foo.c), or NULL on failure. */
const char *
cpp_read_main_file (cpp_reader *pfile, const char *fname)
/* Setup for processing input from the file named FNAME, or stdin if
it is the empty string. Returns true if the file was found. */
bool
cpp_find_main_file (cpp_reader *pfile, const char *fname)
{
if (CPP_OPTION (pfile, deps.style) != DEPS_NONE)
{
@ -463,22 +462,13 @@ cpp_read_main_file (cpp_reader *pfile, const char *fname)
deps_add_default_target (pfile->deps, fname);
}
if (!_cpp_stack_file (pfile, fname))
return NULL;
/* Set this here so the client can change the option if it wishes,
and after stacking the main file so we don't trace the main
file. */
pfile->line_maps.trace_includes = CPP_OPTION (pfile, print_include_names);
/* For foo.i, read the original filename foo.c now, for the benefit
of the front ends. */
if (CPP_OPTION (pfile, preprocessed))
read_original_filename (pfile);
pfile->main_file
= _cpp_find_file (pfile, fname, &pfile->no_search_path, false);
if (_cpp_find_failed (pfile->main_file))
return false;
if (CPP_OPTION (pfile, working_directory))
{
const char *name = pfile->map->to_file;
const char *dir = getpwd ();
char *dir_with_slashes = alloca (strlen (dir) + 3);
@ -487,14 +477,27 @@ cpp_read_main_file (cpp_reader *pfile, const char *fname)
if (pfile->cb.dir_change)
pfile->cb.dir_change (pfile, dir);
/* Emit file renames that will be recognized by
read_directory_filename, since dir_change doesn't output
anything. */
_cpp_do_file_change (pfile, LC_RENAME, dir_with_slashes, 1, 0);
_cpp_do_file_change (pfile, LC_RENAME, name, 1, 0);
}
return true;
}
return pfile->map->to_file;
/* This function reads the file, but does not start preprocessing.
This will generate at least one file change callback, and possibly
a line change callback. */
void
cpp_push_main_file (cpp_reader *pfile)
{
_cpp_stack_file (pfile, pfile->main_file, false);
/* For foo.i, read the original filename foo.c now, for the benefit
of the front ends. */
if (CPP_OPTION (pfile, preprocessed))
read_original_filename (pfile);
/* Set this here so the client can change the option if it wishes,
and after stacking the main file so we don't trace the main
file. */
pfile->line_maps.trace_includes = CPP_OPTION (pfile, print_include_names);
}
/* For preprocessed files, if the first tokens are of the form # NUM.

View File

@ -382,7 +382,13 @@ struct cpp_callbacks
{
/* Called when a new line of preprocessed output is started. */
void (*line_change) (cpp_reader *, const cpp_token *, int);
/* Called when switching to/from a new file.
The line_map is for the new file. It is NULL if there is no new file.
(In C this happens when done with <built-in>+<command line> and also
when done with a main file.) This can be used for resource cleanup. */
void (*file_change) (cpp_reader *, const struct line_map *);
void (*dir_change) (cpp_reader *, const char *);
void (*include) (cpp_reader *, unsigned int, const unsigned char *,
const char *, int);
@ -526,12 +532,14 @@ extern const struct line_maps *cpp_get_line_maps (cpp_reader *);
extern cpp_callbacks *cpp_get_callbacks (cpp_reader *);
extern void cpp_set_callbacks (cpp_reader *, cpp_callbacks *);
/* This function reads the file, but does not start preprocessing. It
returns the name of the original file; this is the same as the
input file, except for preprocessed input. This will generate at
least one file change callback, and possibly a line change callback
too. If there was an error opening the file, it returns NULL. */
extern const char *cpp_read_main_file (cpp_reader *, const char *);
/* This function finds the main file, but does not start reading it.
Returns true iff the file was found. */
extern bool cpp_find_main_file (cpp_reader *, const char *);
/* This function reads the file, but does not start preprocessing.
This will generate at least one file change callback, and possibly
a line change callback. */
extern void cpp_push_main_file (cpp_reader *);
/* Set up built-ins like __FILE__. */
extern void cpp_init_builtins (cpp_reader *, int);
@ -590,7 +598,7 @@ extern void cpp_unassert (cpp_reader *, const char *);
extern void cpp_undef_all (cpp_reader *);
extern cpp_buffer *cpp_push_buffer (cpp_reader *, const unsigned char *,
size_t, int, int);
size_t, int);
extern int cpp_defined (cpp_reader *, const unsigned char *, int);
/* A preprocessing number. Code assumes that any unused high bits of

View File

@ -604,8 +604,9 @@ read_scan_file (char *in_fname, int argc, char **argv)
options->inhibit_errors = 1;
cpp_post_options (scan_in);
if (! cpp_read_main_file (scan_in, in_fname))
if (!cpp_find_main_file (scan_in, in_fname))
exit (FATAL_EXIT_CODE);
cpp_push_main_file (scan_in);
cpp_change_file (scan_in, LC_RENAME, "<built-in>");
cpp_init_builtins (scan_in, true);
@ -669,7 +670,7 @@ read_scan_file (char *in_fname, int argc, char **argv)
/* Scan the macro expansion of "getchar();". */
cpp_push_buffer (scan_in, getchar_call, sizeof(getchar_call) - 1,
/* from_stage3 */ true, 1);
/* from_stage3 */ true);
for (;;)
{
const cpp_token *t = cpp_get_token (scan_in);