From bf425849f1dfa11f509e9ec251056d1e769b1ac2 Mon Sep 17 00:00:00 2001 From: Nathan Sidwell Date: Thu, 19 Nov 2020 04:43:13 -0800 Subject: [PATCH] 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. --- libcpp/directives.c | 6 +++--- libcpp/files.c | 29 ++++++++++++++--------------- libcpp/internal.h | 16 ++++++++-------- libcpp/lex.c | 4 ++-- libcpp/macro.c | 6 +++--- 5 files changed, 30 insertions(+), 31 deletions(-) diff --git a/libcpp/directives.c b/libcpp/directives.c index c4ecb9657e5..bffdc913adb 100644 --- a/libcpp/directives.c +++ b/libcpp/directives.c @@ -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 diff --git a/libcpp/files.c b/libcpp/files.c index b5d9f30297e..ba52d2bf3cf 100644 --- a/libcpp/files.c +++ b/libcpp/files.c @@ -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); diff --git a/libcpp/internal.h b/libcpp/internal.h index 4c1100bbdeb..e629cbca26a 100644 --- a/libcpp/internal.h +++ b/libcpp/internal.h @@ -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. */ diff --git a/libcpp/lex.c b/libcpp/lex.c index 2343ed5aa00..0f18daf6764 100644 --- a/libcpp/lex.c +++ b/libcpp/lex.c @@ -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. */ diff --git a/libcpp/macro.c b/libcpp/macro.c index ddcf3b4f63a..35a5e708519 100644 --- a/libcpp/macro.c +++ b/libcpp/macro.c @@ -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" -- 2.30.2