/* Interface to C preprocessor macro tables for GDB.
- Copyright (C) 2002, 2007, 2008 Free Software Foundation, Inc.
+ Copyright (C) 2002-2021 Free Software Foundation, Inc.
Contributed by Red Hat, Inc.
This file is part of GDB.
#ifndef MACROTAB_H
#define MACROTAB_H
+#include "gdbsupport/function-view.h"
+
struct obstack;
+struct compunit_symtab;
+
+namespace gdb {
struct bcache;
+}
/* How do we represent a source location? I mean, how should we
represent them within GDB; the user wants to use all sorts of
number." This is analogous to the way GCC reports errors in
#include files:
- $ gcc -c base.c
- In file included from header2.h:8,
- from header1.h:3,
- from base.c:5:
- header3.h:1: parse error before ')' token
- $
+ $ gcc -c base.c
+ In file included from header2.h:8,
+ from header1.h:3,
+ from base.c:5:
+ header3.h:1: parse error before ')' token
+ $
GCC tells you exactly what path of #inclusions led you to the
problem. It gives you complete information, in a way that the
following would not:
- $ gcc -c base.c
- header3.h:1: parse error before ')' token
- $
+ $ gcc -c base.c
+ header3.h:1: parse error before ')' token
+ $
Converting all of GDB to use this is a big task, and I'm not really
suggesting it should be a priority. But this module's whole
/* A table of all the macro definitions for a given compilation unit. */
struct macro_table;
+/* The definition of a single macro. */
+struct macro_definition;
/* A source file that participated in a compilation unit --- either a
main file, or an #included file. If a file is #included more than
a part of. */
struct macro_table *table;
- /* A source file --- possibly a header file. */
+ /* A source file --- possibly a header file. This filename is relative to
+ the compilation directory (table->comp_dir), it exactly matches the
+ symtab->filename content. */
const char *filename;
/* The location we were #included from, or zero if we are the
xmalloc if OBSTACK is zero. Use BCACHE to store all macro names,
arguments, definitions, and anything else that might be the same
amongst compilation units in an executable file; if BCACHE is zero,
- don't cache these things.
+ don't cache these things. CUST is a pointer to the containing
+ compilation unit, or NULL if there isn't one.
Note that, if either OBSTACK or BCACHE are non-zero, then removing
information from the table may leak memory. Neither obstacks nor
the same source location (although 'gcc -DFOO -UFOO -DFOO=2' does
do that in GCC 4.1.2.). */
struct macro_table *new_macro_table (struct obstack *obstack,
- struct bcache *bcache);
+ gdb::bcache *bcache,
+ struct compunit_symtab *cust);
/* Free TABLE, and any macro definitions, source file structures,
The macro table makes its own copy of FILENAME; the caller is
responsible for freeing FILENAME when it is no longer needed. */
struct macro_source_file *macro_set_main (struct macro_table *table,
- const char *filename);
+ const char *filename);
/* Return the main source file of the macro table TABLE. */
struct macro_source_file *macro_main (struct macro_table *table);
+/* Mark the macro table TABLE so that macros defined in this table can
+ be redefined without error. Note that it invalid to call this if
+ TABLE is allocated on an obstack. */
+void macro_allow_redefinitions (struct macro_table *table);
+
/* Record a #inclusion.
Record in SOURCE's macro table that, at line number LINE in SOURCE,
The macro table makes its own copy of INCLUDED; the caller is
responsible for freeing INCLUDED when it is no longer needed. */
struct macro_source_file *macro_include (struct macro_source_file *source,
- int line,
- const char *included);
+ int line,
+ const char *included);
+/* Define any special macros, like __FILE__ or __LINE__. This should
+ be called once, on the main source file. */
+
+void macro_define_special (struct macro_table *table);
/* Find any source file structure for a file named NAME, either
included into SOURCE, or SOURCE itself. Return zero if we have
path. e.g., `stdio.h', not `/usr/include/stdio.h'. If NAME
appears more than once in the inclusion tree, return the
least-nested inclusion --- the one closest to the main source file. */
-struct macro_source_file *(macro_lookup_inclusion
- (struct macro_source_file *source,
- const char *name));
+struct macro_source_file *macro_lookup_inclusion
+ (struct macro_source_file *source,
+ const char *name);
/* Record an object-like #definition (i.e., one with no parameter list).
string is REPLACEMENT. This function makes copies of NAME and
REPLACEMENT; the caller is responsible for freeing them. */
void macro_define_object (struct macro_source_file *source, int line,
- const char *name, const char *replacement);
+ const char *name, const char *replacement);
/* Record an function-like #definition (i.e., one with a parameter list).
NAME, ARGV, and REPLACEMENT; the caller is responsible for freeing
them. */
void macro_define_function (struct macro_source_file *source, int line,
- const char *name, int argc, const char **argv,
- const char *replacement);
+ const char *name, int argc, const char **argv,
+ const char *replacement);
/* Record an #undefinition.
Record in SOURCE's macro table that, at line number LINE in SOURCE,
we removed the definition for the preprocessor symbol named NAME. */
void macro_undef (struct macro_source_file *source, int line,
- const char *name);
-
+ const char *name);
/* Different kinds of macro definitions. */
enum macro_kind
macro_function_like
};
+/* Different kinds of special macros. */
+
+enum macro_special_kind
+{
+ /* Ordinary. */
+ macro_ordinary,
+ /* The special macro __FILE__. */
+ macro_FILE,
+ /* The special macro __LINE__. */
+ macro_LINE
+};
/* A preprocessor symbol definition. */
struct macro_definition
struct macro_table *table;
/* What kind of macro it is. */
- enum macro_kind kind;
+ ENUM_BITFIELD (macro_kind) kind : 1;
/* If `kind' is `macro_function_like', the number of arguments it
takes, and their names. The names, and the array of pointers to
- them, are in the table's bcache, if it has one. */
- int argc;
+ them, are in the table's bcache, if it has one. If `kind' is
+ `macro_object_like', then this is actually a `macro_special_kind'
+ describing the macro. */
+ int argc : 30;
const char * const *argv;
- /* The replacement string (body) of the macro. This is in the
- table's bcache, if it has one. */
+ /* The replacement string (body) of the macro. For ordinary macros,
+ this is in the table's bcache, if it has one. For special macros
+ like __FILE__, this value is only valid until the next use of any
+ special macro definition; that is, it is reset each time any
+ special macro is looked up or iterated over. */
const char *replacement;
};
effect at the end of the file. The macro table owns the structure;
the caller need not free it. Return zero if NAME is not #defined
at that point. */
-struct macro_definition *(macro_lookup_definition
- (struct macro_source_file *source,
- int line, const char *name));
+struct macro_definition *macro_lookup_definition
+ (struct macro_source_file *source,
+ int line, const char *name);
/* Return the source location of the definition for NAME in scope at
number of the definition, and return a source file structure for
the file. Return zero if NAME has no definition in scope at that
point, and leave *DEFINITION_LINE unchanged. */
-struct macro_source_file *(macro_definition_location
- (struct macro_source_file *source,
- int line,
- const char *name,
- int *definition_line));
-
+struct macro_source_file *macro_definition_location
+ (struct macro_source_file *source,
+ int line,
+ const char *name,
+ int *definition_line);
+
+/* Prototype for a callback callable when walking a macro table. NAME
+ is the name of the macro, and DEFINITION is the definition. SOURCE
+ is the file at the start of the include path, and LINE is the line
+ number of the SOURCE file where the macro was defined. */
+typedef void (macro_callback_fn) (const char *name,
+ const struct macro_definition *definition,
+ struct macro_source_file *source,
+ int line);
+
+/* Call the callable FN for each macro in the macro table TABLE. */
+void macro_for_each (struct macro_table *table,
+ gdb::function_view<macro_callback_fn> fn);
+
+/* Call FN for each macro that is visible in a given scope. The scope
+ is represented by FILE and LINE. */
+void macro_for_each_in_scope (struct macro_source_file *file, int line,
+ gdb::function_view<macro_callback_fn> fn);
+
+/* Return FILE->filename with possibly prepended compilation directory name.
+ This is raw concatenation without the "set substitute-path" and gdb_realpath
+ applications done by symtab_to_fullname.
+
+ THis function ignores the "set filename-display" setting. Its default
+ setting is "relative" which is backward compatible but the former behavior
+ of macro filenames printing was "absolute". */
+extern std::string macro_source_fullname (struct macro_source_file *file);
#endif /* MACROTAB_H */