Commit 4dc299fb by Per Bothner Committed by Per Bothner

cppinit.c (cpp_read_main_file): Split into two functions: Distribute…

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
parent a506c55c
...@@ -9,6 +9,21 @@ ...@@ -9,6 +9,21 @@
* fix-header.c (cb_file_change): Likewise. * fix-header.c (cb_file_change): Likewise.
* c-ppoutput.c (pp_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 * cpplex.c (_cpp_get_fresh_line): Revert my no-longer-needed
08-28 change, since we're never called with a NULL buffer. 08-28 change, since we're never called with a NULL buffer.
(_cpp_lex_direct): Likewise. (_cpp_lex_direct): Likewise.
......
...@@ -54,7 +54,6 @@ Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ ...@@ -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 /* This structure represents a file searched for by CPP, whether it
exists or not. An instance may be pointed to by more than one exists or not. An instance may be pointed to by more than one
file_hash_entry; at present no reference count is kept. */ file_hash_entry; at present no reference count is kept. */
typedef struct _cpp_file _cpp_file;
struct _cpp_file struct _cpp_file
{ {
/* Filename as given to #include or command line switch. */ /* Filename as given to #include or command line switch. */
...@@ -157,11 +156,8 @@ struct file_hash_entry ...@@ -157,11 +156,8 @@ struct file_hash_entry
static bool open_file (_cpp_file *file); static bool open_file (_cpp_file *file);
static bool pch_open_file (cpp_reader *pfile, _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 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_guts (cpp_reader *pfile, _cpp_file *file);
static bool read_file (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 bool should_stack_file (cpp_reader *, _cpp_file *file, bool import);
static struct cpp_dir *search_path_head (cpp_reader *, const char *fname, static struct cpp_dir *search_path_head (cpp_reader *, const char *fname,
int angle_brackets, enum include_type); int angle_brackets, enum include_type);
...@@ -330,6 +326,12 @@ find_file_in_dir (cpp_reader *pfile, _cpp_file *file) ...@@ -330,6 +326,12 @@ find_file_in_dir (cpp_reader *pfile, _cpp_file *file)
return false; 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 /* 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 starting from START_DIR. If FNAME is the empty string it is
interpreted as STDIN if START_DIR is PFILE->no_seach_path. 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) ...@@ -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 had previously been closed. To open it again pass the return value
to open_file(). to open_file().
*/ */
static _cpp_file * _cpp_file *
find_file (cpp_reader *pfile, const char *fname, cpp_dir *start_dir, bool fake) _cpp_find_file (cpp_reader *pfile, const char *fname, cpp_dir *start_dir, bool fake)
{ {
struct file_hash_entry *entry, **hash_slot; struct file_hash_entry *entry, **hash_slot;
_cpp_file *file; _cpp_file *file;
...@@ -594,8 +596,8 @@ should_stack_file (cpp_reader *pfile, _cpp_file *file, bool import) ...@@ -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 stack if possible. IMPORT is true if this stacking attempt is
because of a #import directive. Returns true if a buffer is because of a #import directive. Returns true if a buffer is
stacked. */ stacked. */
static bool bool
stack_file (cpp_reader *pfile, _cpp_file *file, bool import) _cpp_stack_file (cpp_reader *pfile, _cpp_file *file, bool import)
{ {
cpp_buffer *buffer; cpp_buffer *buffer;
int sysp; int sysp;
...@@ -619,8 +621,7 @@ stack_file (cpp_reader *pfile, _cpp_file *file, bool import) ...@@ -619,8 +621,7 @@ stack_file (cpp_reader *pfile, _cpp_file *file, bool import)
/* Stack the buffer. */ /* Stack the buffer. */
buffer = cpp_push_buffer (pfile, file->buffer, file->st.st_size, buffer = cpp_push_buffer (pfile, file->buffer, file->st.st_size,
CPP_OPTION (pfile, preprocessed), CPP_OPTION (pfile, preprocessed));
! pfile->buffer);
buffer->file = file; buffer->file = file;
/* Initialize controlling macro state. */ /* Initialize controlling macro state. */
...@@ -654,7 +655,8 @@ search_path_head (cpp_reader *pfile, const char *fname, int angle_brackets, ...@@ -654,7 +655,8 @@ search_path_head (cpp_reader *pfile, const char *fname, int angle_brackets,
if (IS_ABSOLUTE_PATH (fname)) if (IS_ABSOLUTE_PATH (fname))
return &pfile->no_search_path; 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 /* 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 the current file was found, but if it was found via an absolute
...@@ -698,16 +700,6 @@ dir_name_of_file (_cpp_file *file) ...@@ -698,16 +700,6 @@ dir_name_of_file (_cpp_file *file)
return file->dir_name; 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), /* Handles #include-family directives (distinguished by TYPE),
including HEADER, and the command line -imacros and -include. including HEADER, and the command line -imacros and -include.
Returns true if a buffer was stacked. */ Returns true if a buffer was stacked. */
...@@ -721,7 +713,7 @@ _cpp_stack_include (cpp_reader *pfile, const char *fname, int angle_brackets, ...@@ -721,7 +713,7 @@ _cpp_stack_include (cpp_reader *pfile, const char *fname, int angle_brackets,
if (!dir) if (!dir)
return false; 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); type == IT_IMPORT);
} }
...@@ -881,7 +873,7 @@ _cpp_cleanup_files (cpp_reader *pfile) ...@@ -881,7 +873,7 @@ _cpp_cleanup_files (cpp_reader *pfile)
void void
_cpp_fake_include (cpp_reader *pfile, const char *fname) _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 /* 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, ...@@ -963,7 +955,7 @@ _cpp_compare_file_date (cpp_reader *pfile, const char *fname,
if (!dir) if (!dir)
return -1; return -1;
file = find_file (pfile, fname, dir, false); file = _cpp_find_file (pfile, fname, dir, false);
if (file->err_no) if (file->err_no)
return -1; return -1;
......
...@@ -307,11 +307,6 @@ struct cpp_buffer ...@@ -307,11 +307,6 @@ struct cpp_buffer
include files has been calculated and stored in "dir" below. */ include files has been calculated and stored in "dir" below. */
unsigned char search_cached; 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 /* The directory of the this buffer's file. Its NAME member is not
allocated, so we don't need to worry about freeing it. */ allocated, so we don't need to worry about freeing it. */
struct cpp_dir dir; struct cpp_dir dir;
...@@ -362,6 +357,8 @@ struct cpp_reader ...@@ -362,6 +357,8 @@ struct cpp_reader
/* Chain of all hashed _cpp_file instances. */ /* Chain of all hashed _cpp_file instances. */
struct _cpp_file *all_files; struct _cpp_file *all_files;
struct _cpp_file *main_file;
/* File and directory hash table. */ /* File and directory hash table. */
struct htab *file_hash; struct htab *file_hash;
struct file_hash_entry *file_hash_entries; struct file_hash_entry *file_hash_entries;
...@@ -515,9 +512,13 @@ extern void _cpp_init_hashtable (cpp_reader *, hash_table *); ...@@ -515,9 +512,13 @@ extern void _cpp_init_hashtable (cpp_reader *, hash_table *);
extern void _cpp_destroy_hashtable (cpp_reader *); extern void _cpp_destroy_hashtable (cpp_reader *);
/* In cppfiles.c */ /* 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_mark_file_once_only (cpp_reader *, struct _cpp_file *);
extern void _cpp_fake_include (cpp_reader *, const char *); 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, extern bool _cpp_stack_include (cpp_reader *, const char *, int,
enum include_type); enum include_type);
extern int _cpp_compare_file_date (cpp_reader *, const char *, int); extern int _cpp_compare_file_date (cpp_reader *, const char *, int);
......
...@@ -448,11 +448,10 @@ cpp_post_options (cpp_reader *pfile) ...@@ -448,11 +448,10 @@ cpp_post_options (cpp_reader *pfile)
mark_named_operators (pfile); mark_named_operators (pfile);
} }
/* Setup for processing input from the file named FNAME, /* Setup for processing input from the file named FNAME, or stdin if
or stdin if it is the empty string. Return the original filename it is the empty string. Returns true if the file was found. */
on success (e.g. foo.i->foo.c), or NULL on failure. */ bool
const char * cpp_find_main_file (cpp_reader *pfile, const char *fname)
cpp_read_main_file (cpp_reader *pfile, const char *fname)
{ {
if (CPP_OPTION (pfile, deps.style) != DEPS_NONE) if (CPP_OPTION (pfile, deps.style) != DEPS_NONE)
{ {
...@@ -463,22 +462,13 @@ cpp_read_main_file (cpp_reader *pfile, const char *fname) ...@@ -463,22 +462,13 @@ cpp_read_main_file (cpp_reader *pfile, const char *fname)
deps_add_default_target (pfile->deps, fname); deps_add_default_target (pfile->deps, fname);
} }
if (!_cpp_stack_file (pfile, fname)) pfile->main_file
return NULL; = _cpp_find_file (pfile, fname, &pfile->no_search_path, false);
if (_cpp_find_failed (pfile->main_file))
/* Set this here so the client can change the option if it wishes, return false;
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);
if (CPP_OPTION (pfile, working_directory)) if (CPP_OPTION (pfile, working_directory))
{ {
const char *name = pfile->map->to_file;
const char *dir = getpwd (); const char *dir = getpwd ();
char *dir_with_slashes = alloca (strlen (dir) + 3); char *dir_with_slashes = alloca (strlen (dir) + 3);
...@@ -487,14 +477,27 @@ cpp_read_main_file (cpp_reader *pfile, const char *fname) ...@@ -487,14 +477,27 @@ cpp_read_main_file (cpp_reader *pfile, const char *fname)
if (pfile->cb.dir_change) if (pfile->cb.dir_change)
pfile->cb.dir_change (pfile, dir); 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. /* For preprocessed files, if the first tokens are of the form # NUM.
......
...@@ -382,7 +382,13 @@ struct cpp_callbacks ...@@ -382,7 +382,13 @@ struct cpp_callbacks
{ {
/* Called when a new line of preprocessed output is started. */ /* Called when a new line of preprocessed output is started. */
void (*line_change) (cpp_reader *, const cpp_token *, int); 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 (*file_change) (cpp_reader *, const struct line_map *);
void (*dir_change) (cpp_reader *, const char *); void (*dir_change) (cpp_reader *, const char *);
void (*include) (cpp_reader *, unsigned int, const unsigned char *, void (*include) (cpp_reader *, unsigned int, const unsigned char *,
const char *, int); const char *, int);
...@@ -526,12 +532,14 @@ extern const struct line_maps *cpp_get_line_maps (cpp_reader *); ...@@ -526,12 +532,14 @@ extern const struct line_maps *cpp_get_line_maps (cpp_reader *);
extern cpp_callbacks *cpp_get_callbacks (cpp_reader *); extern cpp_callbacks *cpp_get_callbacks (cpp_reader *);
extern void cpp_set_callbacks (cpp_reader *, cpp_callbacks *); extern void cpp_set_callbacks (cpp_reader *, cpp_callbacks *);
/* This function reads the file, but does not start preprocessing. It /* This function finds the main file, but does not start reading it.
returns the name of the original file; this is the same as the Returns true iff the file was found. */
input file, except for preprocessed input. This will generate at extern bool cpp_find_main_file (cpp_reader *, const char *);
least one file change callback, and possibly a line change callback
too. If there was an error opening the file, it returns NULL. */ /* This function reads the file, but does not start preprocessing.
extern const char *cpp_read_main_file (cpp_reader *, const char *); 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__. */ /* Set up built-ins like __FILE__. */
extern void cpp_init_builtins (cpp_reader *, int); extern void cpp_init_builtins (cpp_reader *, int);
...@@ -590,7 +598,7 @@ extern void cpp_unassert (cpp_reader *, const char *); ...@@ -590,7 +598,7 @@ extern void cpp_unassert (cpp_reader *, const char *);
extern void cpp_undef_all (cpp_reader *); extern void cpp_undef_all (cpp_reader *);
extern cpp_buffer *cpp_push_buffer (cpp_reader *, const unsigned char *, 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); extern int cpp_defined (cpp_reader *, const unsigned char *, int);
/* A preprocessing number. Code assumes that any unused high bits of /* A preprocessing number. Code assumes that any unused high bits of
......
...@@ -604,8 +604,9 @@ read_scan_file (char *in_fname, int argc, char **argv) ...@@ -604,8 +604,9 @@ read_scan_file (char *in_fname, int argc, char **argv)
options->inhibit_errors = 1; options->inhibit_errors = 1;
cpp_post_options (scan_in); 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); exit (FATAL_EXIT_CODE);
cpp_push_main_file (scan_in);
cpp_change_file (scan_in, LC_RENAME, "<built-in>"); cpp_change_file (scan_in, LC_RENAME, "<built-in>");
cpp_init_builtins (scan_in, true); cpp_init_builtins (scan_in, true);
...@@ -669,7 +670,7 @@ read_scan_file (char *in_fname, int argc, char **argv) ...@@ -669,7 +670,7 @@ read_scan_file (char *in_fname, int argc, char **argv)
/* Scan the macro expansion of "getchar();". */ /* Scan the macro expansion of "getchar();". */
cpp_push_buffer (scan_in, getchar_call, sizeof(getchar_call) - 1, cpp_push_buffer (scan_in, getchar_call, sizeof(getchar_call) - 1,
/* from_stage3 */ true, 1); /* from_stage3 */ true);
for (;;) for (;;)
{ {
const cpp_token *t = cpp_get_token (scan_in); const cpp_token *t = cpp_get_token (scan_in);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment