preprocessor: main-file cleanup

In preparing module patch 7 I realized there was a cleanup I could
make to simplify it.  This is that cleanup.  Also, when doing the
cleanup I noticed some macros had been turned into inline functions,
but not renamed to the preprocessors internal namespace
(_cpp_$INTERNAL rather than cpp_$USER).  Thus, this renames those
functions, deletes an internal field of the file structure, and
determines whether we're in the main file by comparing to
pfile->main_file, the _cpp_file of the main file.

	libcpp/
	* internal.h (cpp_in_system_header): Rename to ...
	(_cpp_in_system_header): ... here.
	(cpp_in_primary_file): Rename to ...
	(_cpp_in_main_source_file): ... here.  Compare main_file equality
	and check main_search value.
	* lex.c (maybe_va_opt_error, _cpp_lex_direct): Adjust for rename.
	* macro.c (_cpp_builtin_macro_text): Likewise.
	(replace_args): Likewise.
	* directives.c (do_include_next): Likewise.
	(do_pragma_once, do_pragma_system_header): Likewise.
	* files.c (struct _cpp_file): Delete main_file field.
	(pch_open): Check pfile->main_file equality.
	(make_cpp_file): Drop cpp_reader parm, don't set main_file.
	(_cpp_find_file): Adjust.
	(_cpp_stack_file): Check pfile->main_file equality.
	(struct report_missing_guard_data): Add cpp_reader field.
	(report_missing_guard): Check pfile->main_file equality.
	(_cpp_report_missing_guards): Adjust.
This commit is contained in:
Nathan Sidwell 2020-11-19 04:43:13 -08:00
parent d84ba819fe
commit bf425849f1
5 changed files with 30 additions and 31 deletions

View File

@ -877,7 +877,7 @@ do_include_next (cpp_reader *pfile)
/* If this is the primary source file, warn and use the normal
search logic. */
if (cpp_in_primary_file (pfile))
if (_cpp_in_main_source_file (pfile))
{
cpp_error (pfile, CPP_DL_WARNING,
"#include_next in primary source file");
@ -1546,7 +1546,7 @@ do_pragma (cpp_reader *pfile)
static void
do_pragma_once (cpp_reader *pfile)
{
if (cpp_in_primary_file (pfile))
if (_cpp_in_main_source_file (pfile))
cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
check_eol (pfile, false);
@ -1708,7 +1708,7 @@ do_pragma_poison (cpp_reader *pfile)
static void
do_pragma_system_header (cpp_reader *pfile)
{
if (cpp_in_primary_file (pfile))
if (_cpp_in_main_source_file (pfile))
cpp_error (pfile, CPP_DL_WARNING,
"#pragma system_header ignored outside include file");
else

View File

@ -103,9 +103,6 @@ struct _cpp_file
/* If read() failed before. */
bool dont_read : 1;
/* If this file is the main file. */
bool main_file : 1;
/* If BUFFER above contains the true contents of the file. */
bool buffer_valid : 1;
@ -186,7 +183,7 @@ static void open_file_failed (cpp_reader *pfile, _cpp_file *file, int,
location_t);
static struct cpp_file_hash_entry *search_cache (struct cpp_file_hash_entry *head,
const cpp_dir *start_dir);
static _cpp_file *make_cpp_file (cpp_reader *, cpp_dir *, const char *fname);
static _cpp_file *make_cpp_file (cpp_dir *, const char *fname);
static void destroy_cpp_file (_cpp_file *);
static cpp_dir *make_cpp_dir (cpp_reader *, const char *dir_name, int sysp);
static void allocate_file_hash_entries (cpp_reader *pfile);
@ -299,7 +296,7 @@ pch_open_file (cpp_reader *pfile, _cpp_file *file, bool *invalid_pch)
for (_cpp_file *f = pfile->all_files; f; f = f->next_file)
if (f->implicit_preinclude)
continue;
else if (f->main_file)
else if (pfile->main_file == f)
break;
else
return false;
@ -528,7 +525,7 @@ _cpp_find_file (cpp_reader *pfile, const char *fname, cpp_dir *start_dir,
if (entry)
return entry->u.file;
_cpp_file *file = make_cpp_file (pfile, start_dir, fname);
_cpp_file *file = make_cpp_file (start_dir, fname);
file->implicit_preinclude
= (kind == _cpp_FFK_PRE_INCLUDE
|| (pfile->buffer && pfile->buffer->file->implicit_preinclude));
@ -865,7 +862,7 @@ has_unique_contents (cpp_reader *pfile, _cpp_file *file, bool import,
{
/* We already have a buffer but it is not valid, because
the file is still stacked. Make a new one. */
ref_file = make_cpp_file (pfile, f->dir, f->name);
ref_file = make_cpp_file (f->dir, f->name);
ref_file->path = f->path;
}
else
@ -951,7 +948,8 @@ _cpp_stack_file (cpp_reader *pfile, _cpp_file *file, include_type type,
if (CPP_OPTION (pfile, deps.style) > (sysp != 0)
&& !file->stack_count
&& file->path[0]
&& !(file->main_file && CPP_OPTION (pfile, deps.ignore_main_file)))
&& !(pfile->main_file == file
&& CPP_OPTION (pfile, deps.ignore_main_file)))
deps_add_dep (pfile->deps, file->path);
/* Clear buffer_valid since _cpp_clean_line messes it up. */
@ -1187,12 +1185,9 @@ search_cache (struct cpp_file_hash_entry *head, const cpp_dir *start_dir)
/* Allocate a new _cpp_file structure. */
static _cpp_file *
make_cpp_file (cpp_reader *pfile, cpp_dir *dir, const char *fname)
make_cpp_file (cpp_dir *dir, const char *fname)
{
_cpp_file *file;
file = XCNEW (_cpp_file);
file->main_file = !pfile->buffer;
_cpp_file *file = XCNEW (_cpp_file);
file->fd = -1;
file->dir = dir;
file->name = xstrdup (fname);
@ -1452,6 +1447,7 @@ cpp_change_file (cpp_reader *pfile, enum lc_reason reason,
struct report_missing_guard_data
{
cpp_reader *pfile;
const char **paths;
size_t count;
};
@ -1470,8 +1466,10 @@ report_missing_guard (void **slot, void *d)
_cpp_file *file = entry->u.file;
/* We don't want MI guard advice for the main file. */
if (!file->once_only && file->cmacro == NULL
&& file->stack_count == 1 && !file->main_file)
if (!file->once_only
&& file->cmacro == NULL
&& file->stack_count == 1
&& data->pfile->main_file != file)
{
if (data->paths == NULL)
{
@ -1501,6 +1499,7 @@ _cpp_report_missing_guards (cpp_reader *pfile)
{
struct report_missing_guard_data data;
data.pfile = pfile;
data.paths = NULL;
data.count = htab_elements (pfile->file_hash);
htab_traverse (pfile->file_hash, report_missing_guard, &data);

View File

@ -628,22 +628,22 @@ typedef unsigned char uchar;
#define UC (const uchar *) /* Intended use: UC"string" */
/* Macros. */
/* Accessors. */
static inline int cpp_in_system_header (cpp_reader *);
static inline int
cpp_in_system_header (cpp_reader *pfile)
inline int
_cpp_in_system_header (cpp_reader *pfile)
{
return pfile->buffer ? pfile->buffer->sysp : 0;
}
#define CPP_PEDANTIC(PF) CPP_OPTION (PF, cpp_pedantic)
#define CPP_WTRADITIONAL(PF) CPP_OPTION (PF, cpp_warn_traditional)
static inline int cpp_in_primary_file (cpp_reader *);
static inline int
cpp_in_primary_file (cpp_reader *pfile)
/* Return true if we're in the main file (unless it's considered to be
an include file in its own right. */
inline int
_cpp_in_main_source_file (cpp_reader *pfile)
{
return pfile->line_table->depth == 1;
return pfile->buffer->file == pfile->main_file;
}
/* True if NODE is a macro for the purposes of ifdef, defined etc. */

View File

@ -1370,7 +1370,7 @@ maybe_va_opt_error (cpp_reader *pfile)
{
/* __VA_OPT__ should not be accepted at all, but allow it in
system headers. */
if (!cpp_in_system_header (pfile))
if (!_cpp_in_system_header (pfile))
cpp_error (pfile, CPP_DL_PEDWARN,
"__VA_OPT__ is not available until C++20");
}
@ -3093,7 +3093,7 @@ _cpp_lex_direct (cpp_reader *pfile)
else if (c == '/' && ! CPP_OPTION (pfile, traditional))
{
/* Don't warn for system headers. */
if (cpp_in_system_header (pfile))
if (_cpp_in_system_header (pfile))
;
/* Warn about comments if pedantically GNUC89, and not
in system headers. */

View File

@ -588,7 +588,7 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node,
(c) we are not in strictly conforming mode, then it has the
value 0. (b) and (c) are already checked in cpp_init_builtins. */
case BT_STDC:
if (cpp_in_system_header (pfile))
if (_cpp_in_system_header (pfile))
number = 0;
else
number = 1;
@ -2217,7 +2217,7 @@ replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
= (const cpp_token **) tokens_buff_last_token_ptr (buff);
}
else if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, c99)
&& ! macro->syshdr && ! cpp_in_system_header (pfile))
&& ! macro->syshdr && ! _cpp_in_system_header (pfile))
{
if (CPP_OPTION (pfile, cplusplus))
cpp_pedwarning (pfile, CPP_W_PEDANTIC,
@ -2236,7 +2236,7 @@ replace_args (cpp_reader *pfile, cpp_hashnode *node, cpp_macro *macro,
}
else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
&& ! CPP_OPTION (pfile, cplusplus)
&& ! macro->syshdr && ! cpp_in_system_header (pfile))
&& ! macro->syshdr && ! _cpp_in_system_header (pfile))
cpp_warning (pfile, CPP_W_C90_C99_COMPAT,
"invoking macro %s argument %d: "
"empty macro arguments are undefined"