1*b725ae77Skettenis /* Interface to C preprocessor macro tables for GDB. 2*b725ae77Skettenis Copyright 2002 Free Software Foundation, Inc. 3*b725ae77Skettenis Contributed by Red Hat, Inc. 4*b725ae77Skettenis 5*b725ae77Skettenis This file is part of GDB. 6*b725ae77Skettenis 7*b725ae77Skettenis This program is free software; you can redistribute it and/or modify 8*b725ae77Skettenis it under the terms of the GNU General Public License as published by 9*b725ae77Skettenis the Free Software Foundation; either version 2 of the License, or 10*b725ae77Skettenis (at your option) any later version. 11*b725ae77Skettenis 12*b725ae77Skettenis This program is distributed in the hope that it will be useful, 13*b725ae77Skettenis but WITHOUT ANY WARRANTY; without even the implied warranty of 14*b725ae77Skettenis MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15*b725ae77Skettenis GNU General Public License for more details. 16*b725ae77Skettenis 17*b725ae77Skettenis You should have received a copy of the GNU General Public License 18*b725ae77Skettenis along with this program; if not, write to the Free Software 19*b725ae77Skettenis Foundation, Inc., 59 Temple Place - Suite 330, 20*b725ae77Skettenis Boston, MA 02111-1307, USA. */ 21*b725ae77Skettenis 22*b725ae77Skettenis #ifndef MACROTAB_H 23*b725ae77Skettenis #define MACROTAB_H 24*b725ae77Skettenis 25*b725ae77Skettenis struct obstack; 26*b725ae77Skettenis struct bcache; 27*b725ae77Skettenis 28*b725ae77Skettenis /* How do we represent a source location? I mean, how should we 29*b725ae77Skettenis represent them within GDB; the user wants to use all sorts of 30*b725ae77Skettenis ambiguous abbreviations, like "break 32" and "break foo.c:32" 31*b725ae77Skettenis ("foo.c" may have been #included into several compilation units), 32*b725ae77Skettenis but what do we disambiguate those things to? 33*b725ae77Skettenis 34*b725ae77Skettenis - Answer 1: "Filename and line number." (Or column number, if 35*b725ae77Skettenis you're picky.) That's not quite good enough. For example, the 36*b725ae77Skettenis same source file can be #included into several different 37*b725ae77Skettenis compilation units --- which #inclusion do you mean? 38*b725ae77Skettenis 39*b725ae77Skettenis - Answer 2: "Compilation unit, filename, and line number." This is 40*b725ae77Skettenis a pretty good answer; GDB's `struct symtab_and_line' basically 41*b725ae77Skettenis embodies this representation. But it's still ambiguous; what if a 42*b725ae77Skettenis given compilation unit #includes the same file twice --- how can I 43*b725ae77Skettenis set a breakpoint on line 12 of the fifth #inclusion of "foo.c"? 44*b725ae77Skettenis 45*b725ae77Skettenis - Answer 3: "Compilation unit, chain of #inclusions, and line 46*b725ae77Skettenis number." This is analogous to the way GCC reports errors in 47*b725ae77Skettenis #include files: 48*b725ae77Skettenis 49*b725ae77Skettenis $ gcc -c base.c 50*b725ae77Skettenis In file included from header2.h:8, 51*b725ae77Skettenis from header1.h:3, 52*b725ae77Skettenis from base.c:5: 53*b725ae77Skettenis header3.h:1: parse error before ')' token 54*b725ae77Skettenis $ 55*b725ae77Skettenis 56*b725ae77Skettenis GCC tells you exactly what path of #inclusions led you to the 57*b725ae77Skettenis problem. It gives you complete information, in a way that the 58*b725ae77Skettenis following would not: 59*b725ae77Skettenis 60*b725ae77Skettenis $ gcc -c base.c 61*b725ae77Skettenis header3.h:1: parse error before ')' token 62*b725ae77Skettenis $ 63*b725ae77Skettenis 64*b725ae77Skettenis Converting all of GDB to use this is a big task, and I'm not really 65*b725ae77Skettenis suggesting it should be a priority. But this module's whole 66*b725ae77Skettenis purpose is to maintain structures describing the macro expansion 67*b725ae77Skettenis process, so I think it's appropriate for us to take a little care 68*b725ae77Skettenis to do that in a complete fashion. 69*b725ae77Skettenis 70*b725ae77Skettenis In this interface, the first line of a file is numbered 1, not 0. 71*b725ae77Skettenis This is the same convention the rest of GDB uses. */ 72*b725ae77Skettenis 73*b725ae77Skettenis 74*b725ae77Skettenis /* A table of all the macro definitions for a given compilation unit. */ 75*b725ae77Skettenis struct macro_table; 76*b725ae77Skettenis 77*b725ae77Skettenis 78*b725ae77Skettenis /* A source file that participated in a compilation unit --- either a 79*b725ae77Skettenis main file, or an #included file. If a file is #included more than 80*b725ae77Skettenis once, the presence of the `included_from' and `included_at_line' 81*b725ae77Skettenis members means that we need to make one instance of this structure 82*b725ae77Skettenis for each #inclusion. Taken as a group, these structures form a 83*b725ae77Skettenis tree mapping the #inclusions that contributed to the compilation 84*b725ae77Skettenis unit, with the main source file as its root. 85*b725ae77Skettenis 86*b725ae77Skettenis Beware --- not every source file mentioned in a compilation unit's 87*b725ae77Skettenis symtab structures will appear in the #inclusion tree! As of Oct 88*b725ae77Skettenis 2002, GCC does record the effect of #line directives in the source 89*b725ae77Skettenis line info, but not in macro info. This means that GDB's symtabs 90*b725ae77Skettenis (built from the former, among other things) may mention filenames 91*b725ae77Skettenis that the #inclusion tree (built from the latter) doesn't have any 92*b725ae77Skettenis record of. See macroscope.c:sal_macro_scope for how to accomodate 93*b725ae77Skettenis this. 94*b725ae77Skettenis 95*b725ae77Skettenis It's worth noting that libcpp has a simpler way of representing all 96*b725ae77Skettenis this, which we should consider switching to. It might even be 97*b725ae77Skettenis suitable for ordinary non-macro line number info. 98*b725ae77Skettenis 99*b725ae77Skettenis Suppose you take your main source file, and after each line 100*b725ae77Skettenis containing an #include directive you insert the text of the 101*b725ae77Skettenis #included file. The result is a big file that pretty much 102*b725ae77Skettenis corresponds to the full text the compiler's going to see. There's 103*b725ae77Skettenis a one-to-one correspondence between lines in the big file and 104*b725ae77Skettenis per-inclusion lines in the source files. (Obviously, #include 105*b725ae77Skettenis directives that are #if'd out don't count. And you'll need to 106*b725ae77Skettenis append a newline to any file that doesn't end in one, to avoid 107*b725ae77Skettenis splicing the last #included line with the next line of the 108*b725ae77Skettenis #including file.) 109*b725ae77Skettenis 110*b725ae77Skettenis Libcpp calls line numbers in this big imaginary file "logical line 111*b725ae77Skettenis numbers", and has a data structure called a "line map" that can map 112*b725ae77Skettenis logical line numbers onto actual source filenames and line numbers, 113*b725ae77Skettenis and also tell you the chain of #inclusions responsible for any 114*b725ae77Skettenis particular logical line number. Basically, this means you can pass 115*b725ae77Skettenis around a single line number and some kind of "compilation unit" 116*b725ae77Skettenis object and you get nice, unambiguous source code locations that 117*b725ae77Skettenis distinguish between multiple #inclusions of the same file, etc. 118*b725ae77Skettenis 119*b725ae77Skettenis Pretty neat, huh? */ 120*b725ae77Skettenis 121*b725ae77Skettenis struct macro_source_file 122*b725ae77Skettenis { 123*b725ae77Skettenis 124*b725ae77Skettenis /* The macro table for the compilation unit this source location is 125*b725ae77Skettenis a part of. */ 126*b725ae77Skettenis struct macro_table *table; 127*b725ae77Skettenis 128*b725ae77Skettenis /* A source file --- possibly a header file. */ 129*b725ae77Skettenis const char *filename; 130*b725ae77Skettenis 131*b725ae77Skettenis /* The location we were #included from, or zero if we are the 132*b725ae77Skettenis compilation unit's main source file. */ 133*b725ae77Skettenis struct macro_source_file *included_by; 134*b725ae77Skettenis 135*b725ae77Skettenis /* If `included_from' is non-zero, the line number in that source 136*b725ae77Skettenis file at which we were included. */ 137*b725ae77Skettenis int included_at_line; 138*b725ae77Skettenis 139*b725ae77Skettenis /* Head of a linked list of the source files #included by this file; 140*b725ae77Skettenis our children in the #inclusion tree. This list is sorted by its 141*b725ae77Skettenis elements' `included_at_line' values, which are unique. (The 142*b725ae77Skettenis macro splay tree's ordering function needs this property.) */ 143*b725ae77Skettenis struct macro_source_file *includes; 144*b725ae77Skettenis 145*b725ae77Skettenis /* The next file #included by our `included_from' file; our sibling 146*b725ae77Skettenis in the #inclusion tree. */ 147*b725ae77Skettenis struct macro_source_file *next_included; 148*b725ae77Skettenis }; 149*b725ae77Skettenis 150*b725ae77Skettenis 151*b725ae77Skettenis /* Create a new, empty macro table. Allocate it in OBSTACK, or use 152*b725ae77Skettenis xmalloc if OBSTACK is zero. Use BCACHE to store all macro names, 153*b725ae77Skettenis arguments, definitions, and anything else that might be the same 154*b725ae77Skettenis amongst compilation units in an executable file; if BCACHE is zero, 155*b725ae77Skettenis don't cache these things. 156*b725ae77Skettenis 157*b725ae77Skettenis Note that, if either OBSTACK or BCACHE are non-zero, then you 158*b725ae77Skettenis should only ever add information the macro table --- you should 159*b725ae77Skettenis never remove things from it. You'll get an error if you try. At 160*b725ae77Skettenis the moment, since we only provide obstacks and bcaches for macro 161*b725ae77Skettenis tables for symtabs, this restriction makes a nice sanity check. 162*b725ae77Skettenis Obstacks and bcaches are pretty much grow-only structures anyway. 163*b725ae77Skettenis However, if we find that it's occasionally useful to delete things 164*b725ae77Skettenis even from the symtab's tables, and the storage leak isn't a 165*b725ae77Skettenis problem, this restriction could be lifted. */ 166*b725ae77Skettenis struct macro_table *new_macro_table (struct obstack *obstack, 167*b725ae77Skettenis struct bcache *bcache); 168*b725ae77Skettenis 169*b725ae77Skettenis 170*b725ae77Skettenis /* Free TABLE, and any macro definitions, source file structures, 171*b725ae77Skettenis etc. it owns. This will raise an internal error if TABLE was 172*b725ae77Skettenis allocated on an obstack, or if it uses a bcache. */ 173*b725ae77Skettenis void free_macro_table (struct macro_table *table); 174*b725ae77Skettenis 175*b725ae77Skettenis 176*b725ae77Skettenis /* Set FILENAME as the main source file of TABLE. Return a source 177*b725ae77Skettenis file structure describing that file; if we record the #definition 178*b725ae77Skettenis of macros, or the #inclusion of other files into FILENAME, we'll 179*b725ae77Skettenis use that source file structure to indicate the context. 180*b725ae77Skettenis 181*b725ae77Skettenis The "main source file" is the one that was given to the compiler; 182*b725ae77Skettenis all other source files that contributed to the compilation unit are 183*b725ae77Skettenis #included, directly or indirectly, from this one. 184*b725ae77Skettenis 185*b725ae77Skettenis The macro table makes its own copy of FILENAME; the caller is 186*b725ae77Skettenis responsible for freeing FILENAME when it is no longer needed. */ 187*b725ae77Skettenis struct macro_source_file *macro_set_main (struct macro_table *table, 188*b725ae77Skettenis const char *filename); 189*b725ae77Skettenis 190*b725ae77Skettenis 191*b725ae77Skettenis /* Return the main source file of the macro table TABLE. */ 192*b725ae77Skettenis struct macro_source_file *macro_main (struct macro_table *table); 193*b725ae77Skettenis 194*b725ae77Skettenis 195*b725ae77Skettenis /* Record a #inclusion. 196*b725ae77Skettenis Record in SOURCE's macro table that, at line number LINE in SOURCE, 197*b725ae77Skettenis we #included the file INCLUDED. Return a source file structure we 198*b725ae77Skettenis can use for symbols #defined or files #included into that. If we've 199*b725ae77Skettenis already created a source file structure for this #inclusion, return 200*b725ae77Skettenis the same structure we created last time. 201*b725ae77Skettenis 202*b725ae77Skettenis The first line of the source file has a line number of 1, not 0. 203*b725ae77Skettenis 204*b725ae77Skettenis The macro table makes its own copy of INCLUDED; the caller is 205*b725ae77Skettenis responsible for freeing INCLUDED when it is no longer needed. */ 206*b725ae77Skettenis struct macro_source_file *macro_include (struct macro_source_file *source, 207*b725ae77Skettenis int line, 208*b725ae77Skettenis const char *included); 209*b725ae77Skettenis 210*b725ae77Skettenis 211*b725ae77Skettenis /* Find any source file structure for a file named NAME, either 212*b725ae77Skettenis included into SOURCE, or SOURCE itself. Return zero if we have 213*b725ae77Skettenis none. NAME is only the final portion of the filename, not the full 214*b725ae77Skettenis path. e.g., `stdio.h', not `/usr/include/stdio.h'. If NAME 215*b725ae77Skettenis appears more than once in the inclusion tree, return the 216*b725ae77Skettenis least-nested inclusion --- the one closest to the main source file. */ 217*b725ae77Skettenis struct macro_source_file *(macro_lookup_inclusion 218*b725ae77Skettenis (struct macro_source_file *source, 219*b725ae77Skettenis const char *name)); 220*b725ae77Skettenis 221*b725ae77Skettenis 222*b725ae77Skettenis /* Record an object-like #definition (i.e., one with no parameter list). 223*b725ae77Skettenis Record in SOURCE's macro table that, at line number LINE in SOURCE, 224*b725ae77Skettenis we #defined a preprocessor symbol named NAME, whose replacement 225*b725ae77Skettenis string is REPLACEMENT. This function makes copies of NAME and 226*b725ae77Skettenis REPLACEMENT; the caller is responsible for freeing them. */ 227*b725ae77Skettenis void macro_define_object (struct macro_source_file *source, int line, 228*b725ae77Skettenis const char *name, const char *replacement); 229*b725ae77Skettenis 230*b725ae77Skettenis 231*b725ae77Skettenis /* Record an function-like #definition (i.e., one with a parameter list). 232*b725ae77Skettenis 233*b725ae77Skettenis Record in SOURCE's macro table that, at line number LINE in SOURCE, 234*b725ae77Skettenis we #defined a preprocessor symbol named NAME, with ARGC arguments 235*b725ae77Skettenis whose names are given in ARGV, whose replacement string is REPLACEMENT. If 236*b725ae77Skettenis the macro takes a variable number of arguments, then ARGC should be 237*b725ae77Skettenis one greater than the number of named arguments, and ARGV[ARGC-1] 238*b725ae77Skettenis should be the string "...". This function makes its own copies of 239*b725ae77Skettenis NAME, ARGV, and REPLACEMENT; the caller is responsible for freeing 240*b725ae77Skettenis them. */ 241*b725ae77Skettenis void macro_define_function (struct macro_source_file *source, int line, 242*b725ae77Skettenis const char *name, int argc, const char **argv, 243*b725ae77Skettenis const char *replacement); 244*b725ae77Skettenis 245*b725ae77Skettenis 246*b725ae77Skettenis /* Record an #undefinition. 247*b725ae77Skettenis Record in SOURCE's macro table that, at line number LINE in SOURCE, 248*b725ae77Skettenis we removed the definition for the preprocessor symbol named NAME. */ 249*b725ae77Skettenis void macro_undef (struct macro_source_file *source, int line, 250*b725ae77Skettenis const char *name); 251*b725ae77Skettenis 252*b725ae77Skettenis 253*b725ae77Skettenis /* Different kinds of macro definitions. */ 254*b725ae77Skettenis enum macro_kind 255*b725ae77Skettenis { 256*b725ae77Skettenis macro_object_like, 257*b725ae77Skettenis macro_function_like 258*b725ae77Skettenis }; 259*b725ae77Skettenis 260*b725ae77Skettenis 261*b725ae77Skettenis /* A preprocessor symbol definition. */ 262*b725ae77Skettenis struct macro_definition 263*b725ae77Skettenis { 264*b725ae77Skettenis /* The table this definition lives in. */ 265*b725ae77Skettenis struct macro_table *table; 266*b725ae77Skettenis 267*b725ae77Skettenis /* What kind of macro it is. */ 268*b725ae77Skettenis enum macro_kind kind; 269*b725ae77Skettenis 270*b725ae77Skettenis /* If `kind' is `macro_function_like', the number of arguments it 271*b725ae77Skettenis takes, and their names. The names, and the array of pointers to 272*b725ae77Skettenis them, are in the table's bcache, if it has one. */ 273*b725ae77Skettenis int argc; 274*b725ae77Skettenis const char * const *argv; 275*b725ae77Skettenis 276*b725ae77Skettenis /* The replacement string (body) of the macro. This is in the 277*b725ae77Skettenis table's bcache, if it has one. */ 278*b725ae77Skettenis const char *replacement; 279*b725ae77Skettenis }; 280*b725ae77Skettenis 281*b725ae77Skettenis 282*b725ae77Skettenis /* Return a pointer to the macro definition for NAME in scope at line 283*b725ae77Skettenis number LINE of SOURCE. If LINE is -1, return the definition in 284*b725ae77Skettenis effect at the end of the file. The macro table owns the structure; 285*b725ae77Skettenis the caller need not free it. Return zero if NAME is not #defined 286*b725ae77Skettenis at that point. */ 287*b725ae77Skettenis struct macro_definition *(macro_lookup_definition 288*b725ae77Skettenis (struct macro_source_file *source, 289*b725ae77Skettenis int line, const char *name)); 290*b725ae77Skettenis 291*b725ae77Skettenis 292*b725ae77Skettenis /* Return the source location of the definition for NAME in scope at 293*b725ae77Skettenis line number LINE of SOURCE. Set *DEFINITION_LINE to the line 294*b725ae77Skettenis number of the definition, and return a source file structure for 295*b725ae77Skettenis the file. Return zero if NAME has no definition in scope at that 296*b725ae77Skettenis point, and leave *DEFINITION_LINE unchanged. */ 297*b725ae77Skettenis struct macro_source_file *(macro_definition_location 298*b725ae77Skettenis (struct macro_source_file *source, 299*b725ae77Skettenis int line, 300*b725ae77Skettenis const char *name, 301*b725ae77Skettenis int *definition_line)); 302*b725ae77Skettenis 303*b725ae77Skettenis 304*b725ae77Skettenis #endif /* MACROTAB_H */ 305