From e9a2e208dd763bf71b0cc9db8526ef8f47ee289e Mon Sep 17 00:00:00 2001 From: Nathan Sidwell Date: Mon, 2 Nov 2020 08:29:58 -0800 Subject: cpplib: Macro use location and comparison Our macro use hook passes a location, but doesn't recieve it from the using location. This patch adds the extra location_t parameter and passes it though. A second cleanup is breaking out the macro comparison code from the redefinition warning. That;ll turn out useful for modules. Finally, there's a filename comparison needed for the location optimization of rewinding from line 2 (occurs during the emission of builtin macros). libcpp/ * internal.h (_cpp_notify_macro_use): Add location parm. (_cpp_maybe_notify_macro_use): Likewise. * directives.c (_cpp_do_file_change): Check we've not changed file when optimizing a rewind. (do_ifdef): Pass location to _cpp_maybe_notify_macro_use. (do_ifndef): Likewise. Delete obsolete comment about powerpc. * expr.c (parse_defined): Pass location to _cpp_maybe_notify_macro_use. * macro.c (enter_macro_context): Likewise. (warn_of_redefinition): Break out helper function. Call it. (compare_macros): New function broken out of warn_of_redefinition. (_cpp_new_macro): Zero all fields. (_cpp_notify_macro_use): Add location parameter. --- libcpp/directives.c | 9 +++------ libcpp/expr.c | 2 +- libcpp/internal.h | 8 +++++--- libcpp/macro.c | 26 +++++++++++++++++++++----- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/libcpp/directives.c b/libcpp/directives.c index f597187..d7b59aa 100644 --- a/libcpp/directives.c +++ b/libcpp/directives.c @@ -1134,6 +1134,7 @@ _cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason, preprocessed source. */ line_map_ordinary *last = LINEMAPS_LAST_ORDINARY_MAP (pfile->line_table); if (!ORDINARY_MAP_STARTING_LINE_NUMBER (last) + && 0 == filename_cmp (to_file, ORDINARY_MAP_FILE_NAME (last)) && SOURCE_LINE (last, pfile->line_table->highest_line) == 2) { ord_map = last; @@ -1981,7 +1982,7 @@ do_ifdef (cpp_reader *pfile) { skip = !_cpp_defined_macro_p (node); _cpp_mark_macro_used (node); - _cpp_maybe_notify_macro_use (pfile, node); + _cpp_maybe_notify_macro_use (pfile, node, pfile->directive_line); if (pfile->cb.used) pfile->cb.used (pfile, pfile->directive_line, node); check_eol (pfile, false); @@ -2004,13 +2005,9 @@ do_ifndef (cpp_reader *pfile) if (node) { - /* Do not treat conditional macros as being defined. This is due to - the powerpc port using conditional macros for 'vector', 'bool', - and 'pixel' to act as conditional keywords. This messes up tests - like #ifndef bool. */ skip = _cpp_defined_macro_p (node); _cpp_mark_macro_used (node); - _cpp_maybe_notify_macro_use (pfile, node); + _cpp_maybe_notify_macro_use (pfile, node, pfile->directive_line); if (pfile->cb.used) pfile->cb.used (pfile, pfile->directive_line, node); check_eol (pfile, false); diff --git a/libcpp/expr.c b/libcpp/expr.c index 2ae9be0..e01a47a 100644 --- a/libcpp/expr.c +++ b/libcpp/expr.c @@ -1070,7 +1070,7 @@ parse_defined (cpp_reader *pfile) "this use of \"defined\" may not be portable"); _cpp_mark_macro_used (node); - _cpp_maybe_notify_macro_use (pfile, node); + _cpp_maybe_notify_macro_use (pfile, node, token->src_loc); /* A possible controlling macro of the form #if !defined (). _cpp_parse_expr checks there was no other junk on the line. */ diff --git a/libcpp/internal.h b/libcpp/internal.h index b1a2a99..4759961 100644 --- a/libcpp/internal.h +++ b/libcpp/internal.h @@ -649,11 +649,13 @@ inline bool _cpp_defined_macro_p (cpp_hashnode *node) } /* In macro.c */ -extern void _cpp_notify_macro_use (cpp_reader *pfile, cpp_hashnode *node); -inline void _cpp_maybe_notify_macro_use (cpp_reader *pfile, cpp_hashnode *node) +extern void _cpp_notify_macro_use (cpp_reader *pfile, cpp_hashnode *node, + location_t loc); +inline void _cpp_maybe_notify_macro_use (cpp_reader *pfile, cpp_hashnode *node, + location_t loc) { if (!(node->flags & NODE_USED)) - _cpp_notify_macro_use (pfile, node); + _cpp_notify_macro_use (pfile, node, loc); } extern cpp_macro *_cpp_new_macro (cpp_reader *, cpp_macro_kind, void *); extern void _cpp_free_definition (cpp_hashnode *); diff --git a/libcpp/macro.c b/libcpp/macro.c index 0874028..e304f67 100644 --- a/libcpp/macro.c +++ b/libcpp/macro.c @@ -340,6 +340,8 @@ static cpp_macro *create_iso_definition (cpp_reader *); static cpp_macro *lex_expansion_token (cpp_reader *, cpp_macro *); static bool warn_of_redefinition (cpp_reader *, cpp_hashnode *, const cpp_macro *); +static bool compare_macros (const cpp_macro *, const cpp_macro *); + static bool parse_params (cpp_reader *, unsigned *, bool *); static void check_trad_stringification (cpp_reader *, const cpp_macro *, const cpp_string *); @@ -1433,7 +1435,7 @@ enter_macro_context (cpp_reader *pfile, cpp_hashnode *node, /* Laziness can only affect the expansion tokens of the macro, not its fun-likeness or parameters. */ - _cpp_maybe_notify_macro_use (pfile, node); + _cpp_maybe_notify_macro_use (pfile, node, location); if (pfile->cb.used) pfile->cb.used (pfile, location, node); @@ -3111,6 +3113,14 @@ warn_of_redefinition (cpp_reader *pfile, cpp_hashnode *node, macro1->lazy = 0; } + return compare_macros (macro1, macro2); +} + +/* Return TRUE if MACRO1 and MACRO2 differ. */ + +static bool +compare_macros (const cpp_macro *macro1, const cpp_macro *macro2) +{ /* Redefinition of a macro is allowed if and only if the old and new definitions are the same. (6.10.3 paragraph 2). */ @@ -3579,6 +3589,10 @@ _cpp_new_macro (cpp_reader *pfile, cpp_macro_kind kind, void *placement) { cpp_macro *macro = (cpp_macro *) placement; + /* Zero init all the fields. This'll tell the compiler know all the + following inits are writing a virgin object. */ + memset (macro, 0, offsetof (cpp_macro, exp)); + macro->line = pfile->directive_line; macro->parm.params = 0; macro->lazy = 0; @@ -3665,10 +3679,12 @@ cpp_define_lazily (cpp_reader *pfile, cpp_hashnode *node, unsigned num) } /* Notify the use of NODE in a macro-aware context (i.e. expanding it, - or testing its existance). Also applies any lazy definition. */ + or testing its existance). Also applies any lazy definition. + Return FALSE if the macro isn't really there. */ extern void -_cpp_notify_macro_use (cpp_reader *pfile, cpp_hashnode *node) +_cpp_notify_macro_use (cpp_reader *pfile, cpp_hashnode *node, + location_t loc) { node->flags |= NODE_USED; switch (node->type) @@ -3686,12 +3702,12 @@ _cpp_notify_macro_use (cpp_reader *pfile, cpp_hashnode *node) case NT_BUILTIN_MACRO: if (pfile->cb.used_define) - pfile->cb.used_define (pfile, pfile->directive_line, node); + pfile->cb.used_define (pfile, loc, node); break; case NT_VOID: if (pfile->cb.used_undef) - pfile->cb.used_undef (pfile, pfile->directive_line, node); + pfile->cb.used_undef (pfile, loc, node); break; default: -- cgit v1.1