15796c8dcSSimon Schubert /* Interface to C preprocessor macro expansion for GDB. 2*ef5ccd6cSJohn Marino Copyright (C) 2002-2013 Free Software Foundation, Inc. 35796c8dcSSimon Schubert Contributed by Red Hat, Inc. 45796c8dcSSimon Schubert 55796c8dcSSimon Schubert This file is part of GDB. 65796c8dcSSimon Schubert 75796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify 85796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by 95796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or 105796c8dcSSimon Schubert (at your option) any later version. 115796c8dcSSimon Schubert 125796c8dcSSimon Schubert This program is distributed in the hope that it will be useful, 135796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of 145796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 155796c8dcSSimon Schubert GNU General Public License for more details. 165796c8dcSSimon Schubert 175796c8dcSSimon Schubert You should have received a copy of the GNU General Public License 185796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */ 195796c8dcSSimon Schubert 205796c8dcSSimon Schubert 215796c8dcSSimon Schubert #ifndef MACROEXP_H 225796c8dcSSimon Schubert #define MACROEXP_H 235796c8dcSSimon Schubert 245796c8dcSSimon Schubert /* A function for looking up preprocessor macro definitions. Return 255796c8dcSSimon Schubert the preprocessor definition of NAME in scope according to BATON, or 265796c8dcSSimon Schubert zero if NAME is not defined as a preprocessor macro. 275796c8dcSSimon Schubert 285796c8dcSSimon Schubert The caller must not free or modify the definition returned. It is 295796c8dcSSimon Schubert probably unwise for the caller to hold pointers to it for very 305796c8dcSSimon Schubert long; it probably lives in some objfile's obstacks. */ 315796c8dcSSimon Schubert typedef struct macro_definition *(macro_lookup_ftype) (const char *name, 325796c8dcSSimon Schubert void *baton); 335796c8dcSSimon Schubert 345796c8dcSSimon Schubert 355796c8dcSSimon Schubert /* Expand any preprocessor macros in SOURCE, and return the expanded 365796c8dcSSimon Schubert text. Use LOOKUP_FUNC and LOOKUP_FUNC_BATON to find identifiers' 375796c8dcSSimon Schubert preprocessor definitions. SOURCE is a null-terminated string. The 385796c8dcSSimon Schubert result is a null-terminated string, allocated using xmalloc; it is 395796c8dcSSimon Schubert the caller's responsibility to free it. */ 405796c8dcSSimon Schubert char *macro_expand (const char *source, 415796c8dcSSimon Schubert macro_lookup_ftype *lookup_func, 425796c8dcSSimon Schubert void *lookup_func_baton); 435796c8dcSSimon Schubert 445796c8dcSSimon Schubert 455796c8dcSSimon Schubert /* Expand all preprocessor macro references that appear explicitly in 465796c8dcSSimon Schubert SOURCE, but do not expand any new macro references introduced by 475796c8dcSSimon Schubert that first level of expansion. Use LOOKUP_FUNC and 485796c8dcSSimon Schubert LOOKUP_FUNC_BATON to find identifiers' preprocessor definitions. 495796c8dcSSimon Schubert SOURCE is a null-terminated string. The result is a 505796c8dcSSimon Schubert null-terminated string, allocated using xmalloc; it is the caller's 515796c8dcSSimon Schubert responsibility to free it. */ 525796c8dcSSimon Schubert char *macro_expand_once (const char *source, 535796c8dcSSimon Schubert macro_lookup_ftype *lookup_func, 545796c8dcSSimon Schubert void *lookup_func_baton); 555796c8dcSSimon Schubert 565796c8dcSSimon Schubert 575796c8dcSSimon Schubert /* If the null-terminated string pointed to by *LEXPTR begins with a 585796c8dcSSimon Schubert macro invocation, return the result of expanding that invocation as 595796c8dcSSimon Schubert a null-terminated string, and set *LEXPTR to the next character 605796c8dcSSimon Schubert after the invocation. The result is completely expanded; it 615796c8dcSSimon Schubert contains no further macro invocations. 625796c8dcSSimon Schubert 635796c8dcSSimon Schubert Otherwise, if *LEXPTR does not start with a macro invocation, 645796c8dcSSimon Schubert return zero, and leave *LEXPTR unchanged. 655796c8dcSSimon Schubert 665796c8dcSSimon Schubert Use LOOKUP_FUNC and LOOKUP_BATON to find macro definitions. 675796c8dcSSimon Schubert 685796c8dcSSimon Schubert If this function returns a string, the caller is responsible for 695796c8dcSSimon Schubert freeing it, using xfree. 705796c8dcSSimon Schubert 715796c8dcSSimon Schubert We need this expand-one-token-at-a-time interface in order to 725796c8dcSSimon Schubert accomodate GDB's C expression parser, which may not consume the 735796c8dcSSimon Schubert entire string. When the user enters a command like 745796c8dcSSimon Schubert 755796c8dcSSimon Schubert (gdb) break *func+20 if x == 5 765796c8dcSSimon Schubert 775796c8dcSSimon Schubert the parser is expected to consume `func+20', and then stop when it 785796c8dcSSimon Schubert sees the "if". But of course, "if" appearing in a character string 795796c8dcSSimon Schubert or as part of a larger identifier doesn't count. So you pretty 805796c8dcSSimon Schubert much have to do tokenization to find the end of the string that 815796c8dcSSimon Schubert needs to be macro-expanded. Our C/C++ tokenizer isn't really 825796c8dcSSimon Schubert designed to be called by anything but the yacc parser engine. */ 835796c8dcSSimon Schubert char *macro_expand_next (char **lexptr, 845796c8dcSSimon Schubert macro_lookup_ftype *lookup_func, 855796c8dcSSimon Schubert void *lookup_baton); 865796c8dcSSimon Schubert 875796c8dcSSimon Schubert /* Functions to classify characters according to cpp rules. */ 885796c8dcSSimon Schubert 895796c8dcSSimon Schubert int macro_is_whitespace (int c); 905796c8dcSSimon Schubert int macro_is_identifier_nondigit (int c); 915796c8dcSSimon Schubert int macro_is_digit (int c); 925796c8dcSSimon Schubert 935796c8dcSSimon Schubert 94*ef5ccd6cSJohn Marino /* Stringify STR according to C rules and return an xmalloc'd pointer 95*ef5ccd6cSJohn Marino to the result. */ 96*ef5ccd6cSJohn Marino 97*ef5ccd6cSJohn Marino char *macro_stringify (const char *str); 98*ef5ccd6cSJohn Marino 995796c8dcSSimon Schubert #endif /* MACROEXP_H */ 100