gdb/macrotab.h - gdb

Data types defined

Macros defined

Source code

  1. /* Interface to C preprocessor macro tables for GDB.
  2.    Copyright (C) 2002-2015 Free Software Foundation, Inc.
  3.    Contributed by Red Hat, Inc.

  4.    This file is part of GDB.

  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 3 of the License, or
  8.    (at your option) any later version.

  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.

  13.    You should have received a copy of the GNU General Public License
  14.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  15. #ifndef MACROTAB_H
  16. #define MACROTAB_H

  17. struct obstack;
  18. struct bcache;
  19. struct compunit_symtab;

  20. /* How do we represent a source location?  I mean, how should we
  21.    represent them within GDB; the user wants to use all sorts of
  22.    ambiguous abbreviations, like "break 32" and "break foo.c:32"
  23.    ("foo.c" may have been #included into several compilation units),
  24.    but what do we disambiguate those things to?

  25.    - Answer 1: "Filename and line number."  (Or column number, if
  26.    you're picky.)  That's not quite good enough.  For example, the
  27.    same source file can be #included into several different
  28.    compilation units --- which #inclusion do you mean?

  29.    - Answer 2: "Compilation unit, filename, and line number."  This is
  30.    a pretty good answer; GDB's `struct symtab_and_line' basically
  31.    embodies this representation.  But it's still ambiguous; what if a
  32.    given compilation unit #includes the same file twice --- how can I
  33.    set a breakpoint on line 12 of the fifth #inclusion of "foo.c"?

  34.    - Answer 3: "Compilation unit, chain of #inclusions, and line
  35.    number."  This is analogous to the way GCC reports errors in
  36.    #include files:

  37.         $ gcc -c base.c
  38.         In file included from header2.h:8,
  39.                          from header1.h:3,
  40.                          from base.c:5:
  41.         header3.h:1: parse error before ')' token
  42.         $

  43.    GCC tells you exactly what path of #inclusions led you to the
  44.    problem.  It gives you complete information, in a way that the
  45.    following would not:

  46.         $ gcc -c base.c
  47.         header3.h:1: parse error before ')' token
  48.         $

  49.    Converting all of GDB to use this is a big task, and I'm not really
  50.    suggesting it should be a priority.  But this module's whole
  51.    purpose is to maintain structures describing the macro expansion
  52.    process, so I think it's appropriate for us to take a little care
  53.    to do that in a complete fashion.

  54.    In this interface, the first line of a file is numbered 1, not 0.
  55.    This is the same convention the rest of GDB uses.  */


  56. /* A table of all the macro definitions for a given compilation unit.  */
  57. struct macro_table;

  58. /* The definition of a single macro.  */
  59. struct macro_definition;

  60. /* A source file that participated in a compilation unit --- either a
  61.    main file, or an #included file.  If a file is #included more than
  62.    once, the presence of the `included_from' and `included_at_line'
  63.    members means that we need to make one instance of this structure
  64.    for each #inclusion.  Taken as a group, these structures form a
  65.    tree mapping the #inclusions that contributed to the compilation
  66.    unit, with the main source file as its root.

  67.    Beware --- not every source file mentioned in a compilation unit's
  68.    symtab structures will appear in the #inclusion tree!  As of Oct
  69.    2002, GCC does record the effect of #line directives in the source
  70.    line info, but not in macro info.  This means that GDB's symtabs
  71.    (built from the former, among other things) may mention filenames
  72.    that the #inclusion tree (built from the latter) doesn't have any
  73.    record of.  See macroscope.c:sal_macro_scope for how to accomodate
  74.    this.

  75.    It's worth noting that libcpp has a simpler way of representing all
  76.    this, which we should consider switching to.  It might even be
  77.    suitable for ordinary non-macro line number info.

  78.    Suppose you take your main source file, and after each line
  79.    containing an #include directive you insert the text of the
  80.    #included file.  The result is a big file that pretty much
  81.    corresponds to the full text the compiler's going to see.  There's
  82.    a one-to-one correspondence between lines in the big file and
  83.    per-inclusion lines in the source files.  (Obviously, #include
  84.    directives that are #if'd out don't count.  And you'll need to
  85.    append a newline to any file that doesn't end in one, to avoid
  86.    splicing the last #included line with the next line of the
  87.    #including file.)

  88.    Libcpp calls line numbers in this big imaginary file "logical line
  89.    numbers", and has a data structure called a "line map" that can map
  90.    logical line numbers onto actual source filenames and line numbers,
  91.    and also tell you the chain of #inclusions responsible for any
  92.    particular logical line number.  Basically, this means you can pass
  93.    around a single line number and some kind of "compilation unit"
  94.    object and you get nice, unambiguous source code locations that
  95.    distinguish between multiple #inclusions of the same file, etc.

  96.    Pretty neat, huh?  */

  97. struct macro_source_file
  98. {

  99.   /* The macro table for the compilation unit this source location is
  100.      a part of.  */
  101.   struct macro_table *table;

  102.   /* A source file --- possibly a header file.  This filename is relative to
  103.      the compilation directory (table->comp_dir), it exactly matches the
  104.      symtab->filename content.  */
  105.   const char *filename;

  106.   /* The location we were #included from, or zero if we are the
  107.      compilation unit's main source file.  */
  108.   struct macro_source_file *included_by;

  109.   /* If `included_from' is non-zero, the line number in that source
  110.      file at which we were included.  */
  111.   int included_at_line;

  112.   /* Head of a linked list of the source files #included by this file;
  113.      our children in the #inclusion tree.  This list is sorted by its
  114.      elements' `included_at_line' values, which are unique.  (The
  115.      macro splay tree's ordering function needs this property.)  */
  116.   struct macro_source_file *includes;

  117.   /* The next file #included by our `included_from' file; our sibling
  118.      in the #inclusion tree.  */
  119.   struct macro_source_file *next_included;
  120. };


  121. /* Create a new, empty macro table.  Allocate it in OBSTACK, or use
  122.    xmalloc if OBSTACK is zero.  Use BCACHE to store all macro names,
  123.    arguments, definitions, and anything else that might be the same
  124.    amongst compilation units in an executable file; if BCACHE is zero,
  125.    don't cache these things.  CUST is a pointer to the containing
  126.    compilation unit, or NULL if there isn't one.

  127.    Note that, if either OBSTACK or BCACHE are non-zero, then removing
  128.    information from the table may leak memory.  Neither obstacks nor
  129.    bcaches really allow you to remove information, so although we can
  130.    update the data structure to record the change, we can't free the
  131.    old data.  At the moment, since we only provide obstacks and
  132.    bcaches for macro tables for symtabs, this isn't a problem; only
  133.    odd debugging information makes a definition and then deletes it at
  134.    the same source location (although 'gcc -DFOO -UFOO -DFOO=2' does
  135.    do that in GCC 4.1.2.).  */
  136. struct macro_table *new_macro_table (struct obstack *obstack,
  137.                                      struct bcache *bcache,
  138.                                      struct compunit_symtab *cust);


  139. /* Free TABLE, and any macro definitions, source file structures,
  140.    etc. it owns.  This will raise an internal error if TABLE was
  141.    allocated on an obstack, or if it uses a bcache.  */
  142. void free_macro_table (struct macro_table *table);


  143. /* Set FILENAME as the main source file of TABLE.  Return a source
  144.    file structure describing that file; if we record the #definition
  145.    of macros, or the #inclusion of other files into FILENAME, we'll
  146.    use that source file structure to indicate the context.

  147.    The "main source file" is the one that was given to the compiler;
  148.    all other source files that contributed to the compilation unit are
  149.    #included, directly or indirectly, from this one.

  150.    The macro table makes its own copy of FILENAME; the caller is
  151.    responsible for freeing FILENAME when it is no longer needed.  */
  152. struct macro_source_file *macro_set_main (struct macro_table *table,
  153.                                           const char *filename);


  154. /* Return the main source file of the macro table TABLE.  */
  155. struct macro_source_file *macro_main (struct macro_table *table);

  156. /* Mark the macro table TABLE so that macros defined in this table can
  157.    be redefined without error.  Note that it invalid to call this if
  158.    TABLE is allocated on an obstack.  */
  159. void macro_allow_redefinitions (struct macro_table *table);


  160. /* Record a #inclusion.
  161.    Record in SOURCE's macro table that, at line number LINE in SOURCE,
  162.    we #included the file INCLUDED.  Return a source file structure we
  163.    can use for symbols #defined or files #included into that.  If we've
  164.    already created a source file structure for this #inclusion, return
  165.    the same structure we created last time.

  166.    The first line of the source file has a line number of 1, not 0.

  167.    The macro table makes its own copy of INCLUDED; the caller is
  168.    responsible for freeing INCLUDED when it is no longer needed.  */
  169. struct macro_source_file *macro_include (struct macro_source_file *source,
  170.                                          int line,
  171.                                          const char *included);

  172. /* Define any special macros, like __FILE__ or __LINE__.  This should
  173.    be called once, on the main source file.  */

  174. void macro_define_special (struct macro_table *table);

  175. /* Find any source file structure for a file named NAME, either
  176.    included into SOURCE, or SOURCE itself.  Return zero if we have
  177.    none.  NAME is only the final portion of the filename, not the full
  178.    path.  e.g., `stdio.h', not `/usr/include/stdio.h'.  If NAME
  179.    appears more than once in the inclusion tree, return the
  180.    least-nested inclusion --- the one closest to the main source file.  */
  181. struct macro_source_file *(macro_lookup_inclusion
  182.                            (struct macro_source_file *source,
  183.                             const char *name));


  184. /* Record an object-like #definition (i.e., one with no parameter list).
  185.    Record in SOURCE's macro table that, at line number LINE in SOURCE,
  186.    we #defined a preprocessor symbol named NAME, whose replacement
  187.    string is REPLACEMENT.  This function makes copies of NAME and
  188.    REPLACEMENT; the caller is responsible for freeing them.  */
  189. void macro_define_object (struct macro_source_file *source, int line,
  190.                           const char *name, const char *replacement);


  191. /* Record an function-like #definition (i.e., one with a parameter list).

  192.    Record in SOURCE's macro table that, at line number LINE in SOURCE,
  193.    we #defined a preprocessor symbol named NAME, with ARGC arguments
  194.    whose names are given in ARGV, whose replacement string is REPLACEMENT.  If
  195.    the macro takes a variable number of arguments, then ARGC should be
  196.    one greater than the number of named arguments, and ARGV[ARGC-1]
  197.    should be the string "...".  This function makes its own copies of
  198.    NAME, ARGV, and REPLACEMENT; the caller is responsible for freeing
  199.    them.  */
  200. void macro_define_function (struct macro_source_file *source, int line,
  201.                             const char *name, int argc, const char **argv,
  202.                             const char *replacement);


  203. /* Record an #undefinition.
  204.    Record in SOURCE's macro table that, at line number LINE in SOURCE,
  205.    we removed the definition for the preprocessor symbol named NAME.  */
  206. void macro_undef (struct macro_source_file *source, int line,
  207.                   const char *name);

  208. /* Different kinds of macro definitions.  */
  209. enum macro_kind
  210. {
  211.   macro_object_like,
  212.   macro_function_like
  213. };

  214. /* Different kinds of special macros.  */

  215. enum macro_special_kind
  216. {
  217.   /* Ordinary.  */
  218.   macro_ordinary,
  219.   /* The special macro __FILE__.  */
  220.   macro_FILE,
  221.   /* The special macro __LINE__.  */
  222.   macro_LINE
  223. };

  224. /* A preprocessor symbol definition.  */
  225. struct macro_definition
  226. {
  227.   /* The table this definition lives in.  */
  228.   struct macro_table *table;

  229.   /* What kind of macro it is.  */
  230.   ENUM_BITFIELD (macro_kind) kind : 1;

  231.   /* If `kind' is `macro_function_like', the number of arguments it
  232.      takes, and their names.  The names, and the array of pointers to
  233.      them, are in the table's bcache, if it has one.  If `kind' is
  234.      `macro_object_like', then this is actually a `macro_special_kind'
  235.      describing the macro.  */
  236.   int argc : 30;
  237.   const char * const *argv;

  238.   /* The replacement string (body) of the macro.  For ordinary macros,
  239.      this is in the table's bcache, if it has one.  For special macros
  240.      like __FILE__, this value is only valid until the next use of any
  241.      special macro definition; that is, it is reset each time any
  242.      special macro is looked up or iterated over.  */
  243.   const char *replacement;
  244. };


  245. /* Return a pointer to the macro definition for NAME in scope at line
  246.    number LINE of SOURCE.  If LINE is -1, return the definition in
  247.    effect at the end of the file.  The macro table owns the structure;
  248.    the caller need not free it.  Return zero if NAME is not #defined
  249.    at that point.  */
  250. struct macro_definition *(macro_lookup_definition
  251.                           (struct macro_source_file *source,
  252.                            int line, const char *name));


  253. /* Return the source location of the definition for NAME in scope at
  254.    line number LINE of SOURCE.  Set *DEFINITION_LINE to the line
  255.    number of the definition, and return a source file structure for
  256.    the file.  Return zero if NAME has no definition in scope at that
  257.    point, and leave *DEFINITION_LINE unchanged.  */
  258. struct macro_source_file *(macro_definition_location
  259.                            (struct macro_source_file *source,
  260.                             int line,
  261.                             const char *name,
  262.                             int *definition_line));

  263. /* Callback function when walking a macro table.  NAME is the name of
  264.    the macro, and DEFINITION is the definition.  SOURCE is the file at the
  265.    start of the include path, and LINE is the line number of the SOURCE file
  266.    where the macro was defined.  USER_DATA is an arbitrary pointer which is
  267.    passed by the caller to macro_for_each or macro_for_each_in_scope.  */
  268. typedef void (*macro_callback_fn) (const char *name,
  269.                                    const struct macro_definition *definition,
  270.                                    struct macro_source_file *source,
  271.                                    int line,
  272.                                    void *user_data);

  273. /* Call the function FN for each macro in the macro table TABLE.
  274.    USER_DATA is passed, untranslated, to FN.  */
  275. void macro_for_each (struct macro_table *table, macro_callback_fn fn,
  276.                      void *user_data);

  277. /* Call the function FN for each macro that is visible in a given
  278.    scope.  The scope is represented by FILE and LINE.  USER_DATA is
  279.    passed, untranslated, to FN.  */
  280. void macro_for_each_in_scope (struct macro_source_file *file, int line,
  281.                               macro_callback_fn fn,
  282.                               void *user_data);

  283. /* Return FILE->filename with possibly prepended compilation directory name.
  284.    This is raw concatenation without the "set substitute-path" and gdb_realpath
  285.    applications done by symtab_to_fullname.  Returned string must be freed by
  286.    xfree.

  287.    THis function ignores the "set filename-display" setting.  Its default
  288.    setting is "relative" which is backward compatible but the former behavior
  289.    of macro filenames printing was "absolute".  */
  290. extern char *macro_source_fullname (struct macro_source_file *file);

  291. #endif /* MACROTAB_H */