gdb: remove callback in macro expand functions

I started to look into changing the callbacks in macroexp.h to use
gdb::function_view.  However, I noticed that the passed lookup function
was always `standard_macro_lookup`, which looks up a macro in a
`macro_scope` object.  Since that doesn't look like a very useful
abstraction, it would be simpler to just pass the scope around and have
the various functions call standard_macro_lookup themselves.  This is
what this patch does.

gdb/ChangeLog:

	* macroexp.h (macro_lookup_ftype): Remove.
	(macro_expand, macro_expand_once, macro_expand_next): Remove
	lookup function parameters, add scope parameter.
	* macroexp.c (scan, substitute_args, expand, maybe_expand,
	macro_expand, macro_expand_once, macro_expand_next): Likewise.
	* macroscope.h (standard_macro_lookup): Change parameter type
	to macro_scope.
	* macroscope.c (standard_macro_lookup): Likewise.
	* c-exp.y (lex_one_token): Update.
	* macrocmd.c (macro_expand_command): Likewise.
	(macro_expand_once_command): Likewise.

Change-Id: Id2431b1489359e1b0274dc2b81e5ea5d225d730c
This commit is contained in:
Simon Marchi 2020-07-02 20:38:25 -04:00 committed by Simon Marchi
parent 889d527eb4
commit 211d5b1c18
7 changed files with 69 additions and 88 deletions

View File

@ -1,3 +1,17 @@
2020-07-02 Simon Marchi <simon.marchi@efficios.com>
* macroexp.h (macro_lookup_ftype): Remove.
(macro_expand, macro_expand_once, macro_expand_next): Remove
lookup function parameters, add scope parameter.
* macroexp.c (scan, substitute_args, expand, maybe_expand,
macro_expand, macro_expand_once, macro_expand_next): Likewise.
* macroscope.h (standard_macro_lookup): Change parameter type
to macro_scope.
* macroscope.c (standard_macro_lookup): Likewise.
* c-exp.y (lex_one_token): Update.
* macrocmd.c (macro_expand_command): Likewise.
(macro_expand_once_command): Likewise.
2020-07-02 Simon Marchi <simon.marchi@polymtl.ca> 2020-07-02 Simon Marchi <simon.marchi@polymtl.ca>
* inf-loop.c (inferior_event_handler): Remove client_data param. * inf-loop.c (inferior_event_handler): Remove client_data param.

View File

@ -2632,8 +2632,7 @@ lex_one_token (struct parser_state *par_state, bool *is_quoted_name)
if (! scanning_macro_expansion ()) if (! scanning_macro_expansion ())
{ {
char *expanded = macro_expand_next (&pstate->lexptr, char *expanded = macro_expand_next (&pstate->lexptr,
standard_macro_lookup, *expression_macro_scope);
expression_macro_scope);
if (expanded) if (expanded)
scan_macro_expansion (expanded); scan_macro_expansion (expanded);

View File

@ -47,9 +47,6 @@ macro_inform_no_debuginfo (void)
static void static void
macro_expand_command (const char *exp, int from_tty) macro_expand_command (const char *exp, int from_tty)
{ {
gdb::unique_xmalloc_ptr<struct macro_scope> ms;
gdb::unique_xmalloc_ptr<char> expanded;
/* You know, when the user doesn't specify any expression, it would be /* You know, when the user doesn't specify any expression, it would be
really cool if this defaulted to the last expression evaluated. really cool if this defaulted to the last expression evaluated.
Then it would be easy to ask, "Hey, what did I just evaluate?" But Then it would be easy to ask, "Hey, what did I just evaluate?" But
@ -60,10 +57,12 @@ macro_expand_command (const char *exp, int from_tty)
" expression you\n" " expression you\n"
"want to expand.")); "want to expand."));
ms = default_macro_scope (); gdb::unique_xmalloc_ptr<macro_scope> ms = default_macro_scope ();
if (ms)
if (ms != nullptr)
{ {
expanded = macro_expand (exp, standard_macro_lookup, ms.get ()); gdb::unique_xmalloc_ptr<char> expanded = macro_expand (exp, *ms);
fputs_filtered ("expands to: ", gdb_stdout); fputs_filtered ("expands to: ", gdb_stdout);
fputs_filtered (expanded.get (), gdb_stdout); fputs_filtered (expanded.get (), gdb_stdout);
fputs_filtered ("\n", gdb_stdout); fputs_filtered ("\n", gdb_stdout);
@ -76,9 +75,6 @@ macro_expand_command (const char *exp, int from_tty)
static void static void
macro_expand_once_command (const char *exp, int from_tty) macro_expand_once_command (const char *exp, int from_tty)
{ {
gdb::unique_xmalloc_ptr<struct macro_scope> ms;
gdb::unique_xmalloc_ptr<char> expanded;
/* You know, when the user doesn't specify any expression, it would be /* You know, when the user doesn't specify any expression, it would be
really cool if this defaulted to the last expression evaluated. really cool if this defaulted to the last expression evaluated.
And it should set the once-expanded text as the new `last And it should set the once-expanded text as the new `last
@ -89,10 +85,12 @@ macro_expand_once_command (const char *exp, int from_tty)
" the expression\n" " the expression\n"
"you want to expand.")); "you want to expand."));
ms = default_macro_scope (); gdb::unique_xmalloc_ptr<macro_scope> ms = default_macro_scope ();
if (ms)
if (ms != nullptr)
{ {
expanded = macro_expand_once (exp, standard_macro_lookup, ms.get ()); gdb::unique_xmalloc_ptr<char> expanded = macro_expand_once (exp, *ms);
fputs_filtered ("expands to: ", gdb_stdout); fputs_filtered ("expands to: ", gdb_stdout);
fputs_filtered (expanded.get (), gdb_stdout); fputs_filtered (expanded.get (), gdb_stdout);
fputs_filtered ("\n", gdb_stdout); fputs_filtered ("\n", gdb_stdout);

View File

@ -21,6 +21,7 @@
#include "gdb_obstack.h" #include "gdb_obstack.h"
#include "macrotab.h" #include "macrotab.h"
#include "macroexp.h" #include "macroexp.h"
#include "macroscope.h"
#include "c-lang.h" #include "c-lang.h"
@ -877,9 +878,7 @@ gather_arguments (const char *name, struct macro_buffer *src, int nargs,
static void scan (struct macro_buffer *dest, static void scan (struct macro_buffer *dest,
struct macro_buffer *src, struct macro_buffer *src,
struct macro_name_list *no_loop, struct macro_name_list *no_loop,
macro_lookup_ftype *lookup_func, const macro_scope &scope);
void *lookup_baton);
/* A helper function for substitute_args. /* A helper function for substitute_args.
@ -959,8 +958,7 @@ substitute_args (struct macro_buffer *dest,
int is_varargs, const struct macro_buffer *va_arg_name, int is_varargs, const struct macro_buffer *va_arg_name,
const std::vector<struct macro_buffer> &argv, const std::vector<struct macro_buffer> &argv,
struct macro_name_list *no_loop, struct macro_name_list *no_loop,
macro_lookup_ftype *lookup_func, const macro_scope &scope)
void *lookup_baton)
{ {
/* The token we are currently considering. */ /* The token we are currently considering. */
struct macro_buffer tok; struct macro_buffer tok;
@ -1194,7 +1192,7 @@ substitute_args (struct macro_buffer *dest,
referring to the argument's text, not the argument referring to the argument's text, not the argument
itself. */ itself. */
struct macro_buffer arg_src (argv[arg].text, argv[arg].len); struct macro_buffer arg_src (argv[arg].text, argv[arg].len);
scan (dest, &arg_src, no_loop, lookup_func, lookup_baton); scan (dest, &arg_src, no_loop, scope);
substituted = 1; substituted = 1;
} }
@ -1224,8 +1222,7 @@ expand (const char *id,
struct macro_buffer *dest, struct macro_buffer *dest,
struct macro_buffer *src, struct macro_buffer *src,
struct macro_name_list *no_loop, struct macro_name_list *no_loop,
macro_lookup_ftype *lookup_func, const macro_scope &scope)
void *lookup_baton)
{ {
struct macro_name_list new_no_loop; struct macro_name_list new_no_loop;
@ -1243,7 +1240,7 @@ expand (const char *id,
struct macro_buffer replacement_list (def->replacement, struct macro_buffer replacement_list (def->replacement,
strlen (def->replacement)); strlen (def->replacement));
scan (dest, &replacement_list, &new_no_loop, lookup_func, lookup_baton); scan (dest, &replacement_list, &new_no_loop, scope);
return 1; return 1;
} }
else if (def->kind == macro_function_like) else if (def->kind == macro_function_like)
@ -1310,7 +1307,7 @@ expand (const char *id,
expand an argument until we see how it's being used. */ expand an argument until we see how it's being used. */
struct macro_buffer substituted (0); struct macro_buffer substituted (0);
substitute_args (&substituted, def, is_varargs, &va_arg_name, substitute_args (&substituted, def, is_varargs, &va_arg_name,
argv, no_loop, lookup_func, lookup_baton); argv, no_loop, scope);
/* Now `substituted' is the macro's replacement list, with all /* Now `substituted' is the macro's replacement list, with all
argument values substituted into it properly. Re-scan it for argument values substituted into it properly. Re-scan it for
@ -1323,7 +1320,7 @@ expand (const char *id,
`substituted's original text buffer after scanning it so we `substituted's original text buffer after scanning it so we
can free it. */ can free it. */
struct macro_buffer substituted_src (substituted.text, substituted.len); struct macro_buffer substituted_src (substituted.text, substituted.len);
scan (dest, &substituted_src, &new_no_loop, lookup_func, lookup_baton); scan (dest, &substituted_src, &new_no_loop, scope);
return 1; return 1;
} }
@ -1344,8 +1341,7 @@ maybe_expand (struct macro_buffer *dest,
struct macro_buffer *src_first, struct macro_buffer *src_first,
struct macro_buffer *src_rest, struct macro_buffer *src_rest,
struct macro_name_list *no_loop, struct macro_name_list *no_loop,
macro_lookup_ftype *lookup_func, const macro_scope &scope)
void *lookup_baton)
{ {
gdb_assert (src_first->shared); gdb_assert (src_first->shared);
gdb_assert (src_rest->shared); gdb_assert (src_rest->shared);
@ -1363,11 +1359,9 @@ maybe_expand (struct macro_buffer *dest,
if (! currently_rescanning (no_loop, id.c_str ())) if (! currently_rescanning (no_loop, id.c_str ()))
{ {
/* Does this identifier have a macro definition in scope? */ /* Does this identifier have a macro definition in scope? */
struct macro_definition *def = lookup_func (id.c_str (), macro_definition *def = standard_macro_lookup (id.c_str (), scope);
lookup_baton);
if (def && expand (id.c_str (), def, dest, src_rest, no_loop, if (def && expand (id.c_str (), def, dest, src_rest, no_loop, scope))
lookup_func, lookup_baton))
return 1; return 1;
} }
} }
@ -1385,8 +1379,7 @@ static void
scan (struct macro_buffer *dest, scan (struct macro_buffer *dest,
struct macro_buffer *src, struct macro_buffer *src,
struct macro_name_list *no_loop, struct macro_name_list *no_loop,
macro_lookup_ftype *lookup_func, const macro_scope &scope)
void *lookup_baton)
{ {
gdb_assert (src->shared); gdb_assert (src->shared);
gdb_assert (! dest->shared); gdb_assert (! dest->shared);
@ -1408,7 +1401,7 @@ scan (struct macro_buffer *dest,
dest->last_token = dest->len; dest->last_token = dest->len;
} }
if (! maybe_expand (dest, &tok, src, no_loop, lookup_func, lookup_baton)) if (! maybe_expand (dest, &tok, src, no_loop, scope))
/* We didn't end up expanding tok as a macro reference, so /* We didn't end up expanding tok as a macro reference, so
simply append it to dest. */ simply append it to dest. */
append_tokens_without_splicing (dest, &tok); append_tokens_without_splicing (dest, &tok);
@ -1425,16 +1418,14 @@ scan (struct macro_buffer *dest,
gdb::unique_xmalloc_ptr<char> gdb::unique_xmalloc_ptr<char>
macro_expand (const char *source, macro_expand (const char *source, const macro_scope &scope)
macro_lookup_ftype *lookup_func,
void *lookup_func_baton)
{ {
struct macro_buffer src (source, strlen (source)); struct macro_buffer src (source, strlen (source));
struct macro_buffer dest (0); struct macro_buffer dest (0);
dest.last_token = 0; dest.last_token = 0;
scan (&dest, &src, 0, lookup_func, lookup_func_baton); scan (&dest, &src, 0, scope);
dest.appendc ('\0'); dest.appendc ('\0');
@ -1443,18 +1434,14 @@ macro_expand (const char *source,
gdb::unique_xmalloc_ptr<char> gdb::unique_xmalloc_ptr<char>
macro_expand_once (const char *source, macro_expand_once (const char *source, const macro_scope &scope)
macro_lookup_ftype *lookup_func,
void *lookup_func_baton)
{ {
error (_("Expand-once not implemented yet.")); error (_("Expand-once not implemented yet."));
} }
char * char *
macro_expand_next (const char **lexptr, macro_expand_next (const char **lexptr, const macro_scope &scope)
macro_lookup_ftype *lookup_func,
void *lookup_baton)
{ {
struct macro_buffer tok; struct macro_buffer tok;
@ -1470,7 +1457,7 @@ macro_expand_next (const char **lexptr,
return 0; return 0;
/* If it's a macro invocation, expand it. */ /* If it's a macro invocation, expand it. */
if (maybe_expand (&dest, &tok, &src, 0, lookup_func, lookup_baton)) if (maybe_expand (&dest, &tok, &src, 0, scope))
{ {
/* It was a macro invocation! Package up the expansion as a /* It was a macro invocation! Package up the expansion as a
null-terminated string and return it. Set *lexptr to the null-terminated string and return it. Set *lexptr to the

View File

@ -21,38 +21,26 @@
#ifndef MACROEXP_H #ifndef MACROEXP_H
#define MACROEXP_H #define MACROEXP_H
/* A function for looking up preprocessor macro definitions. Return struct macro_scope;
the preprocessor definition of NAME in scope according to BATON, or
zero if NAME is not defined as a preprocessor macro.
The caller must not free or modify the definition returned. It is /* Expand any preprocessor macros in SOURCE (a null-terminated string), and
probably unwise for the caller to hold pointers to it for very return the expanded text.
long; it probably lives in some objfile's obstacks. */
typedef struct macro_definition *(macro_lookup_ftype) (const char *name,
void *baton);
Use SCOPE to find identifiers' preprocessor definitions.
/* Expand any preprocessor macros in SOURCE, and return the expanded The result is a null-terminated string. */
text. Use LOOKUP_FUNC and LOOKUP_FUNC_BATON to find identifiers'
preprocessor definitions. SOURCE is a null-terminated string. The
result is a null-terminated string, allocated using xmalloc; it is
the caller's responsibility to free it. */
gdb::unique_xmalloc_ptr<char> macro_expand (const char *source, gdb::unique_xmalloc_ptr<char> macro_expand (const char *source,
macro_lookup_ftype *lookup_func, const macro_scope &scope);
void *lookup_func_baton);
/* Expand all preprocessor macro references that appear explicitly in SOURCE
(a null-terminated string), but do not expand any new macro references
introduced by that first level of expansion.
/* Expand all preprocessor macro references that appear explicitly in Use SCOPE to find identifiers' preprocessor definitions.
SOURCE, but do not expand any new macro references introduced by
that first level of expansion. Use LOOKUP_FUNC and The result is a null-terminated string. */
LOOKUP_FUNC_BATON to find identifiers' preprocessor definitions.
SOURCE is a null-terminated string. The result is a
null-terminated string, allocated using xmalloc; it is the caller's
responsibility to free it. */
gdb::unique_xmalloc_ptr<char> macro_expand_once (const char *source, gdb::unique_xmalloc_ptr<char> macro_expand_once (const char *source,
macro_lookup_ftype *lookup_func, const macro_scope &scope);
void *lookup_func_baton);
/* If the null-terminated string pointed to by *LEXPTR begins with a /* If the null-terminated string pointed to by *LEXPTR begins with a
macro invocation, return the result of expanding that invocation as macro invocation, return the result of expanding that invocation as
@ -61,9 +49,9 @@ gdb::unique_xmalloc_ptr<char> macro_expand_once (const char *source,
contains no further macro invocations. contains no further macro invocations.
Otherwise, if *LEXPTR does not start with a macro invocation, Otherwise, if *LEXPTR does not start with a macro invocation,
return zero, and leave *LEXPTR unchanged. return nullptr, and leave *LEXPTR unchanged.
Use LOOKUP_FUNC and LOOKUP_BATON to find macro definitions. Use SCOPE to find macro definitions.
If this function returns a string, the caller is responsible for If this function returns a string, the caller is responsible for
freeing it, using xfree. freeing it, using xfree.
@ -80,9 +68,7 @@ gdb::unique_xmalloc_ptr<char> macro_expand_once (const char *source,
much have to do tokenization to find the end of the string that much have to do tokenization to find the end of the string that
needs to be macro-expanded. Our C/C++ tokenizer isn't really needs to be macro-expanded. Our C/C++ tokenizer isn't really
designed to be called by anything but the yacc parser engine. */ designed to be called by anything but the yacc parser engine. */
char *macro_expand_next (const char **lexptr, char *macro_expand_next (const char **lexptr, const macro_scope &scope);
macro_lookup_ftype *lookup_func,
void *lookup_baton);
/* Functions to classify characters according to cpp rules. */ /* Functions to classify characters according to cpp rules. */

View File

@ -140,15 +140,15 @@ default_macro_scope (void)
location given by BATON, which must be a pointer to a `struct location given by BATON, which must be a pointer to a `struct
macro_scope' structure. */ macro_scope' structure. */
struct macro_definition * struct macro_definition *
standard_macro_lookup (const char *name, void *baton) standard_macro_lookup (const char *name, const macro_scope &ms)
{ {
struct macro_scope *ms = (struct macro_scope *) baton;
struct macro_definition *result;
/* Give user-defined macros priority over all others. */ /* Give user-defined macros priority over all others. */
result = macro_lookup_definition (macro_main (macro_user_macros), -1, name); macro_definition *result
if (! result) = macro_lookup_definition (macro_main (macro_user_macros), -1, name);
result = macro_lookup_definition (ms->file, ms->line, name);
if (result == nullptr)
result = macro_lookup_definition (ms.file, ms.line, name);
return result; return result;
} }

View File

@ -56,12 +56,9 @@ gdb::unique_xmalloc_ptr<struct macro_scope> user_macro_scope (void);
the user macro scope. */ the user macro scope. */
gdb::unique_xmalloc_ptr<struct macro_scope> default_macro_scope (void); gdb::unique_xmalloc_ptr<struct macro_scope> default_macro_scope (void);
/* Look up the definition of the macro named NAME in scope at the source /* Look up the definition of the macro named NAME in scope at the source
location given by BATON, which must be a pointer to a `struct location given by MS. */
macro_scope' structure. This function is suitable for use as macro_definition *standard_macro_lookup (const char *name,
a macro_lookup_ftype function. */ const macro_scope &ms);
struct macro_definition *standard_macro_lookup (const char *name, void *baton);
#endif /* MACROSCOPE_H */ #endif /* MACROSCOPE_H */