Commit 3caee4a8 by Zack Weinberg Committed by Zack Weinberg

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
parent 641be6fe
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.
Mon Apr 26 15:27:33 1999 Mark Mitchell <mark@codesourcery.com>
* toplev.c (compile_file): Move call to check_global_declarations
......
......@@ -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);
}
}
......@@ -58,3 +58,4 @@ extern MACRODEF create_definition PARAMS ((U_CHAR *, U_CHAR *,
extern int compare_defs PARAMS ((cpp_reader *, DEFINITION *,
DEFINITION *));
extern void macroexpand PARAMS ((cpp_reader *, HASHNODE *));
extern void dump_definition PARAMS ((cpp_reader *, MACRODEF));
......@@ -1094,6 +1094,25 @@ cpp_finish (pfile)
}
}
}
if (opts->dump_macros == dump_only)
{
int i;
HASHNODE *h;
MACRODEF m;
for (i = HASHSIZE; --i >= 0;)
{
for (h = pfile->hashtab[i]; h; h = h->next)
if (h->type == T_MACRO)
{
m.defn = h->value.defn;
m.symnam = h->name;
m.symlen = h->length;
dump_definition (pfile, m);
CPP_PUTC (pfile, '\n');
}
}
}
}
/* Handle one command-line option in (argc, argv).
......
......@@ -94,6 +94,7 @@ main (argc, argv)
}
cpp_finish (&parse_in);
fwrite (parse_in.token_buffer, 1, CPP_WRITTEN (&parse_in), stdout);
if (parse_in.errors)
exit (FATAL_EXIT_CODE);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment