diff options
author | Zack Weinberg <zack@rabi.columbia.edu> | 1999-04-26 16:41:02 +0000 |
---|---|---|
committer | Zack Weinberg <zack@gcc.gnu.org> | 1999-04-26 16:41:02 +0000 |
commit | 3caee4a8ee44a285efab9a80cf765c05f3209eb5 (patch) | |
tree | 5c575c1ae52f10bc1eebdcf45ba407212d863336 /gcc/cpphash.c | |
parent | 641be6fea8860669fb0535babeab8cfbe17b8413 (diff) | |
download | gcc-3caee4a8ee44a285efab9a80cf765c05f3209eb5.zip gcc-3caee4a8ee44a285efab9a80cf765c05f3209eb5.tar.gz gcc-3caee4a8ee44a285efab9a80cf765c05f3209eb5.tar.bz2 |
cpphash.c (dump_definition): New function.
1999-04-26 19:16 -0400 Zack Weinberg <zack@rabi.columbia.edu>
* cpphash.c (dump_definition): New function.
* cpphash.h: Prototype it.
* cpplib.c (handle_directive): Don't output anything here.
Streamline.
(pass_thru_directive): Take a length, not a pointer to the
end. All callers changed.
(do_define): Handle -dD, -dN, -g3 entirely here. Streamline.
(do_include): Handle -dI here.
(do_ident): Correct to match cccp.
(do_pragma): Copy the pragma through here.
(do_assert, do_unassert): Tidy.
* cppinit.c (cpp_finish): If -dM was specified, walk the macro
hash table and call dump_definition on all the entries.
* cppmain.c: cpp_finish may produce output.
From-SVN: r26659
Diffstat (limited to 'gcc/cpphash.c')
-rw-r--r-- | gcc/cpphash.c | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/gcc/cpphash.c b/gcc/cpphash.c index 6e601a0..1d8a104 100644 --- a/gcc/cpphash.c +++ b/gcc/cpphash.c @@ -1636,3 +1636,100 @@ comp_def_part (first, beg1, len1, beg2, len2, last) } return (beg1 != end1) || (beg2 != end2); } + +/* Dump the definition of macro MACRO on stdout. The format is suitable + to be read back in again. */ + +void +dump_definition (pfile, macro) + cpp_reader *pfile; + MACRODEF macro; +{ + DEFINITION *defn = macro.defn; + + CPP_RESERVE (pfile, macro.symlen + sizeof "#define "); + CPP_PUTS_Q (pfile, "#define ", sizeof "#define " -1); + CPP_PUTS_Q (pfile, macro.symnam, macro.symlen); + + if (defn->nargs == -1) + { + CPP_PUTC_Q (pfile, ' '); + + /* The first and last two characters of a macro expansion are + always "\r "; this needs to be trimmed out. + So we need length-4 chars of space, plus one for the NUL. */ + CPP_RESERVE (pfile, defn->length - 4 + 1); + CPP_PUTS_Q (pfile, defn->expansion + 2, defn->length - 4); + CPP_NUL_TERMINATE_Q (pfile); + } + else + { + struct reflist *r; + unsigned char *argnames = xstrdup (defn->args.argnames); + unsigned char **argv = alloca (defn->nargs * sizeof(char *)); + int *argl = alloca (defn->nargs * sizeof(int)); + unsigned char *x; + int i; + + /* First extract the argument list. */ + x = argnames; + i = defn->nargs; + while (i--) + { + argv[i] = x; + while (*x != ',' && *x != '\0') x++; + argl[i] = x - argv[i]; + if (*x == ',') + { + *x = '\0'; + x += 2; /* skip the space after the comma */ + } + } + + /* Now print out the argument list. */ + CPP_PUTC_Q (pfile, '('); + for (i = 0; i < defn->nargs; i++) + { + CPP_RESERVE (pfile, argl[i] + 2); + CPP_PUTS_Q (pfile, argv[i], argl[i]); + if (i < defn->nargs-1) + CPP_PUTS_Q (pfile, ", ", 2); + } + + if (defn->rest_args) + CPP_PUTS (pfile, "...) ", 5); + else + CPP_PUTS (pfile, ") ", 2); + + /* Now the definition. */ + x = defn->expansion; + for (r = defn->pattern; r; r = r->next) + { + i = r->nchars; + if (*x == '\r') x += 2, i -= 2; + /* i chars for macro text, plus the length of the macro + argument name, plus one for a stringify marker, plus two for + each concatenation marker. */ + CPP_RESERVE (pfile, + i + argl[r->argno] + r->stringify + + (r->raw_before + r->raw_after) * 2); + + if (i > 0) CPP_PUTS_Q (pfile, x, i); + if (r->raw_before) + CPP_PUTS_Q (pfile, "##", 2); + if (r->stringify) + CPP_PUTC_Q (pfile, '#'); + CPP_PUTS_Q (pfile, argv[r->argno], argl[r->argno]); + if (r->raw_after && !(r->next && r->next->nchars == 0 + && r->next->raw_before)) + CPP_PUTS_Q (pfile, "##", 2); + + x += i; + } + + i = defn->length - (x - defn->expansion) - 2; + if (*x == '\r') x += 2, i -= 2; + if (i > 0) CPP_PUTS (pfile, x, i); + CPP_NUL_TERMINATE (pfile); + } +} |