This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
integrated -save-temps and the death of specs
- From: Aldy Hernandez <aldyh at redhat dot com>
- To: Zack Weinberg <zack at codesourcery dot com>
- Cc: gcc-patches at gcc dot gnu dot org, echristo at redhat dot com
- Date: Tue, 19 Mar 2002 18:06:45 +1100
- Subject: integrated -save-temps and the death of specs
hi zack.
i got very upset at specs files today and decided to start the painful
process towards getting rid of them altogether. eric sent me your
post on the -E mode for ./cc1, and here's a patch that will hopefully
start the ball rolling.
my plan after this patch is to:
- change the master specs to use "./cc1 -E" instead of cpp0
(and use -fpreprocessed on the output).
- implement target macros to be called with a string of
options, which will return additional options to pass to the
assembler, linker, etc etc. i'll provide memoizeable
functions to read options, so we don't reparse the command
options again and again.
- convert at least one port from specs to this new mechanism,
as an example.
this patch adds a -E option for ./cc1 to behave like the preprocessor.
there was a little code shuffling to get things initialized in the right
order. and quite a few variables and functions needed to be renamed
now that their visibility is expanded.
how does this look?
X2002-03-19 Aldy Hernandez <aldyh@redhat.com>
* c-lex.c (init_c_lex): Use default cb_include.
(cb_default_include): New.
(cb_ident): Handle flag_preprocess_only.
(cb_file_change): Same.
(cb_define): Same.
(cb_def_pragma): Same.
(cb_undef): Same.
(init_c_lex): Initialize cpp_options_1.
(init_c_lex): Initialize cpp_print.
(c_common_parse_file): Handle flag_preprocess_only.
* c-common.c (c_common_finish): Flush preprocessed output.
* Makefile.in (LIBCPP_OBJS): Add cppoutput.o.
(cppoutput.o): New dependency.
* cppoutput.c: New file with code mostly lifted from cppmain.c.
* cppmain.c (do_preprocessing): Rename scan_translation_unit to
cpp_scan_translation_unit.
Rename check_multiline_token to cpp_check_multiline_token.
Rename options to cpp_options.
Rename maybe_print_line to cpp_maybe_print_line.
Rename print_line to cpp_print_line.
(scan_translation_unit): Move to cppoutput.c.
(check_multiline_token): Same.
(maybe_print_line): Same.
(print_line): Same.
(cb_line_change): Same.
(cb_indent): Same.
(cb_define): Same.
(cb_undef): Same.
(cb_include): Same.
(cb_file_change): Same.
(cb_def_pragma): Same.
Rename uses of print to cpp_print.
(struct printer): Move to cpplib.h and rename to
cpp_printer.
(options): Move to cpplib.h and rename to cpp_options_1.
* cpplib.h: Add prototype for cpp_scan_translation_unit.
Add prototype for cpp_check_multiline_token.
Add prototype for cpp_maybe_print_line.
Add prototype for cpp_print_line.
Add prototypes for the callbacks (cpp_cb_default*).
* flags.h: Add flag_preprocess_only.
* toplev.c (display_help): Document -E.
Add flag_preprocess_only.
(independent_decode_option): Handle -E.
(compile_file): Return after preprocessing of
flag_preprocess_only.
(finalize): Close asm_out_file after we call
(*lang_hooks.finish)().
(lang_dependent_init): Call init_asm_output before
(*lang.hooks.init) when flag_preprocess_only.
(init_output_file): Rename to init_output_file.
(init_output_file): Do not output assembly header when in
preprocess mode.
Add .i suffix when preprocessing.
Index: c-lex.c
===================================================================
RCS file: /cvs/uberbaum/gcc/c-lex.c,v
retrieving revision 1.167
diff -c -p -r1.167 c-lex.c
*** c-lex.c 2002/03/17 20:41:34 1.167
--- c-lex.c 2002/03/19 06:54:54
*************** static void cb_ident PARAMS ((cpp_reade
*** 96,101 ****
--- 96,104 ----
const cpp_string *));
static void cb_file_change PARAMS ((cpp_reader *, const struct line_map *));
static void cb_def_pragma PARAMS ((cpp_reader *, unsigned int));
+ static void cb_include PARAMS ((cpp_reader *, unsigned int,
+ const unsigned char *,
+ const cpp_token *));
static void cb_define PARAMS ((cpp_reader *, unsigned int,
cpp_hashnode *));
static void cb_undef PARAMS ((cpp_reader *, unsigned int,
*************** init_c_lex (filename)
*** 132,138 ****
--- 135,144 ----
cb->ident = cb_ident;
cb->file_change = cb_file_change;
cb->def_pragma = cb_def_pragma;
+ cb->include = cb_include;
+ cpp_options_1 = cpp_get_options (parse_in);
+
/* Set the debug callbacks if we can use them. */
if (debug_info_level == DINFO_LEVEL_VERBOSE
&& (write_symbols == DWARF_DEBUG || write_symbols == DWARF2_DEBUG
*************** init_c_lex (filename)
*** 148,153 ****
--- 154,171 ----
if (filename == NULL || !strcmp (filename, "-"))
filename = "";
+ /* Initialize the printer structure. Setting cpp_print.line to
+ -1 here is a trick to guarantee that the first token of the
+ file will cause a linemarker to be output by
+ maybe_print_line. */
+ cpp_print.line = (unsigned int) -1;
+ cpp_print.printed = 0;
+ cpp_print.prev = 0;
+ cpp_print.map = 0;
+
+ /* Open the output now. */
+ cpp_print.outf = asm_out_file;
+
return cpp_read_main_file (parse_in, filename, ident_hash);
}
*************** c_common_parse_file ()
*** 162,168 ****
(*debug_hooks->start_source_file) (lineno, input_filename);
cpp_finish_options (parse_in);
! yyparse ();
}
struct c_fileinfo *
--- 180,189 ----
(*debug_hooks->start_source_file) (lineno, input_filename);
cpp_finish_options (parse_in);
! if (flag_preprocess_only)
! cpp_scan_translation_unit (parse_in);
! else
! yyparse ();
}
struct c_fileinfo *
*************** cb_ident (pfile, line, str)
*** 237,242 ****
--- 258,268 ----
unsigned int line ATTRIBUTE_UNUSED;
const cpp_string *str ATTRIBUTE_UNUSED;
{
+ if (flag_preprocess_only)
+ {
+ cpp_cb_default_ident (pfile, line, str);
+ return;
+ }
#ifdef ASM_OUTPUT_IDENT
if (! flag_no_ident)
{
*************** cb_line_change (pfile, token, parsing_ar
*** 255,263 ****
--- 281,306 ----
const cpp_token *token;
int parsing_args ATTRIBUTE_UNUSED;
{
+ if (flag_preprocess_only)
+ {
+ cpp_cb_default_line_change (pfile, token, parsing_args);
+ return;
+ }
+
src_lineno = SOURCE_LINE (map, token->line);
}
+ void
+ cb_include (pfile, line, dir, header)
+ cpp_reader *pfile;
+ unsigned int line;
+ const unsigned char *dir;
+ const cpp_token *header;
+ {
+ if (flag_preprocess_only)
+ cpp_cb_default_include (pfile, line, dir, header);
+ }
+
static void
cb_file_change (pfile, new_map)
cpp_reader *pfile ATTRIBUTE_UNUSED;
*************** cb_file_change (pfile, new_map)
*** 265,270 ****
--- 308,319 ----
{
unsigned int to_line = SOURCE_LINE (new_map, new_map->to_line);
+ if (flag_preprocess_only)
+ {
+ cpp_cb_default_file_change (pfile, new_map);
+ return;
+ }
+
if (new_map->reason == LC_ENTER)
{
/* Don't stack the main buffer on the input stack;
*************** cb_def_pragma (pfile, line)
*** 328,333 ****
--- 377,388 ----
cpp_reader *pfile;
unsigned int line;
{
+ if (flag_preprocess_only)
+ {
+ cpp_cb_default_def_pragma (pfile, line);
+ return;
+ }
+
/* Issue a warning message if we have been asked to do so. Ignore
unknown pragmas in system headers unless an explicit
-Wunknown-pragmas has been given. */
*************** cb_define (pfile, line, node)
*** 357,362 ****
--- 412,423 ----
unsigned int line;
cpp_hashnode *node;
{
+ if (flag_preprocess_only)
+ {
+ cpp_cb_default_define (pfile, line, node);
+ return;
+ }
+
(*debug_hooks->define) (SOURCE_LINE (map, line),
(const char *) cpp_macro_definition (pfile, node));
}
*************** cb_undef (pfile, line, node)
*** 368,373 ****
--- 429,440 ----
unsigned int line;
cpp_hashnode *node;
{
+ if (flag_preprocess_only)
+ {
+ cpp_cb_default_undef (pfile, line, node);
+ return;
+ }
+
(*debug_hooks->undef) (SOURCE_LINE (map, line),
(const char *) NODE_NAME (node));
}
Index: c-common.c
===================================================================
RCS file: /cvs/uberbaum/gcc/c-common.c,v
retrieving revision 1.298
diff -c -p -r1.298 c-common.c
*** c-common.c 2002/03/15 20:08:23 1.298
--- c-common.c 2002/03/19 06:54:55
*************** c_common_finish ()
*** 4123,4128 ****
--- 4123,4132 ----
{
cpp_finish (parse_in);
+ /* Flush any pending output. */
+ if (flag_preprocess_only && cpp_print.printed)
+ putc ('\n', cpp_print.outf);
+
/* For performance, avoid tearing down cpplib's internal structures.
Call cpp_errors () instead of cpp_destroy (). */
errorcount += cpp_errors (parse_in);
Index: Makefile.in
===================================================================
RCS file: /cvs/uberbaum/gcc/Makefile.in,v
retrieving revision 1.839
diff -c -p -r1.839 Makefile.in
*** Makefile.in 2002/03/15 07:09:36 1.839
--- Makefile.in 2002/03/19 06:54:56
*************** PREPROCESSOR_DEFINES = \
*** 1961,1967 ****
-DTOOL_INCLUDE_DIR=\"$(gcc_tooldir)/include\"
LIBCPP_OBJS = cpplib.o cpplex.o cppmacro.o cppexp.o cppfiles.o \
! cpphash.o cpperror.o cppinit.o cppdefault.o \
hashtable.o line-map.o mkdeps.o prefix.o version.o mbchar.o
LIBCPP_DEPS = $(CPPLIB_H) cpphash.h line-map.h hashtable.h intl.h \
--- 1961,1967 ----
-DTOOL_INCLUDE_DIR=\"$(gcc_tooldir)/include\"
LIBCPP_OBJS = cpplib.o cpplex.o cppmacro.o cppexp.o cppfiles.o \
! cpphash.o cpperror.o cppinit.o cppdefault.o cppoutput.o \
hashtable.o line-map.o mkdeps.o prefix.o version.o mbchar.o
LIBCPP_DEPS = $(CPPLIB_H) cpphash.h line-map.h hashtable.h intl.h \
*************** cpp0$(exeext): cppmain.o intl.o libcpp.a
*** 1979,1984 ****
--- 1979,1986 ----
intl.o libcpp.a $(LIBS)
cppmain.o: cppmain.c $(CONFIG_H) $(CPPLIB_H) intl.h $(SYSTEM_H)
+
+ cppoutput.o: cppoutput.c $(CONFIG_H) $(CPPLIB_H) intl.h $(SYSTEM_H)
cpperror.o: cpperror.c $(CONFIG_H) $(LIBCPP_DEPS)
cppexp.o: cppexp.c $(CONFIG_H) $(LIBCPP_DEPS)
Index: cppoutput.c
===================================================================
RCS file: cppoutput.c
diff -N cppoutput.c
*** /dev/null Tue May 5 13:32:27 1998
--- cppoutput.c Mon Mar 18 22:54:56 2002
***************
*** 0 ****
--- 1,289 ----
+ /* Integrated pre-processor common output routines.
+ Copyright (C) 1995, 1997, 1998, 1999, 2000, 2001, 2002
+ Free Software Foundation, Inc.
+ Written by Per Bothner, 1994-95.
+
+ This file is part of GCC.
+
+ GCC is free software; you can redistribute it and/or modify it under
+ the terms of the GNU General Public License as published by the Free
+ Software Foundation; either version 2, or (at your option) any later
+ version.
+
+ GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GCC; see the file COPYING. If not, write to the Free
+ Software Foundation, 59 Temple Place - Suite 330, Boston, MA
+ 02111-1307, USA. */
+
+ #include "config.h"
+ #include "system.h"
+ #include "cpplib.h"
+ #include "intl.h"
+
+ struct cpp_printer cpp_print;
+
+ cpp_options *cpp_options_1; /* Options of pfile. */
+
+ /* Adjust cpp_print.line for newlines embedded in tokens. */
+ void
+ cpp_check_multiline_token (str)
+ const cpp_string *str;
+ {
+ unsigned int i;
+
+ for (i = 0; i < str->len; i++)
+ if (str->text[i] == '\n')
+ cpp_print.line++;
+ }
+
+ /* Writes out the preprocessed file, handling spacing and paste
+ avoidance issues. */
+ void
+ cpp_scan_translation_unit (pfile)
+ cpp_reader *pfile;
+ {
+ bool avoid_paste = false;
+
+ cpp_print.source = NULL;
+ for (;;)
+ {
+ const cpp_token *token = cpp_get_token (pfile);
+
+ if (token->type == CPP_PADDING)
+ {
+ avoid_paste = true;
+ if (cpp_print.source == NULL
+ || (!(cpp_print.source->flags & PREV_WHITE)
+ && token->val.source == NULL))
+ cpp_print.source = token->val.source;
+ continue;
+ }
+
+ if (token->type == CPP_EOF)
+ break;
+
+ /* Subtle logic to output a space if and only if necessary. */
+ if (avoid_paste)
+ {
+ if (cpp_print.source == NULL)
+ cpp_print.source = token;
+ if (cpp_print.source->flags & PREV_WHITE
+ || (cpp_print.prev
+ && cpp_avoid_paste (pfile, cpp_print.prev, token))
+ || (cpp_print.prev == NULL && token->type == CPP_HASH))
+ putc (' ', cpp_print.outf);
+ }
+ else if (token->flags & PREV_WHITE)
+ putc (' ', cpp_print.outf);
+
+ avoid_paste = false;
+ cpp_print.source = NULL;
+ cpp_print.prev = token;
+ cpp_output_token (token, cpp_print.outf);
+
+ if (token->type == CPP_STRING || token->type == CPP_WSTRING
+ || token->type == CPP_COMMENT)
+ cpp_check_multiline_token (&token->val.str);
+ }
+ }
+
+ /* Output a line marker for logical line LINE. Special flags are "1"
+ or "2" indicating entering or leaving a file. */
+ void
+ cpp_print_line (map, line, special_flags)
+ const struct line_map *map;
+ unsigned int line;
+ const char *special_flags;
+ {
+ /* End any previous line of text. */
+ if (cpp_print.printed)
+ putc ('\n', cpp_print.outf);
+ cpp_print.printed = 0;
+
+ cpp_print.line = line;
+ if (! cpp_options_1->no_line_commands)
+ {
+ size_t to_file_len = strlen (map->to_file);
+ unsigned char *to_file_quoted = alloca (to_file_len * 4 + 1);
+ unsigned char *p;
+
+ /* cpp_quote_string does not nul-terminate, so we have to do it
+ ourselves. */
+ p = cpp_quote_string (to_file_quoted,
+ (unsigned char *)map->to_file, to_file_len);
+ *p = '\0';
+
+ fprintf (cpp_print.outf, "# %u \"%s\"%s",
+ SOURCE_LINE (map, cpp_print.line),
+ to_file_quoted, special_flags);
+
+ if (map->sysp == 2)
+ fputs (" 3 4", cpp_print.outf);
+ else if (map->sysp == 1)
+ fputs (" 3", cpp_print.outf);
+
+ putc ('\n', cpp_print.outf);
+ }
+ }
+
+ /* If the token read on logical line LINE needs to be output on a
+ different line to the current one, output the required newlines or
+ a line marker, and return 1. Otherwise return 0. */
+ void
+ cpp_maybe_print_line (map, line)
+ const struct line_map *map;
+ unsigned int line;
+ {
+ /* End the previous line of text. */
+ if (cpp_print.printed)
+ {
+ putc ('\n', cpp_print.outf);
+ cpp_print.line++;
+ cpp_print.printed = 0;
+ }
+
+ if (line >= cpp_print.line && line < cpp_print.line + 8)
+ {
+ while (line > cpp_print.line)
+ {
+ putc ('\n', cpp_print.outf);
+ cpp_print.line++;
+ }
+ }
+ else
+ cpp_print_line (map, line, "");
+ }
+
+ /* Called when a line of output is started. TOKEN is the first token
+ of the line, and at end of file will be CPP_EOF. */
+ void
+ cpp_cb_default_line_change (pfile, token, parsing_args)
+ cpp_reader *pfile ATTRIBUTE_UNUSED;
+ const cpp_token *token;
+ int parsing_args;
+ {
+ if (token->type == CPP_EOF || parsing_args)
+ return;
+
+ cpp_maybe_print_line (cpp_print.map, token->line);
+ cpp_print.printed = 1;
+ cpp_print.prev = 0;
+ cpp_print.source = 0;
+
+ /* Supply enough spaces to put this token in its original column,
+ one space per column greater than 2, since cpp_scan_translation_unit
+ will provide a space if PREV_WHITE. Don't bother trying to
+ reconstruct tabs; we can't get it right in general, and nothing
+ ought to care. Some things do care; the fault lies with them. */
+ if (token->col > 2)
+ {
+ unsigned int spaces = token->col - 2;
+
+ while (spaces--)
+ putc (' ', cpp_print.outf);
+ }
+ }
+
+ void
+ cpp_cb_default_ident (pfile, line, str)
+ cpp_reader *pfile ATTRIBUTE_UNUSED;
+ unsigned int line;
+ const cpp_string * str;
+ {
+ cpp_maybe_print_line (cpp_print.map, line);
+ fprintf (cpp_print.outf, "#ident \"%s\"\n", str->text);
+ cpp_print.line++;
+ }
+
+ void
+ cpp_cb_default_define (pfile, line, node)
+ cpp_reader *pfile;
+ unsigned int line;
+ cpp_hashnode *node;
+ {
+ cpp_maybe_print_line (cpp_print.map, line);
+ fputs ("#define ", cpp_print.outf);
+
+ /* -dD command line option. */
+ if (cpp_options_1->dump_macros == dump_definitions)
+ fputs ((const char *) cpp_macro_definition (pfile, node), cpp_print.outf);
+ else
+ fputs ((const char *) NODE_NAME (node), cpp_print.outf);
+
+ putc ('\n', cpp_print.outf);
+ cpp_print.line++;
+ }
+
+ void
+ cpp_cb_default_undef (pfile, line, node)
+ cpp_reader *pfile ATTRIBUTE_UNUSED;
+ unsigned int line;
+ cpp_hashnode *node;
+ {
+ cpp_maybe_print_line (cpp_print.map, line);
+ fprintf (cpp_print.outf, "#undef %s\n", NODE_NAME (node));
+ cpp_print.line++;
+ }
+
+ void
+ cpp_cb_default_include (pfile, line, dir, header)
+ cpp_reader *pfile;
+ unsigned int line;
+ const unsigned char *dir;
+ const cpp_token *header;
+ {
+ cpp_maybe_print_line (cpp_print.map, line);
+ fprintf (cpp_print.outf, "#%s %s\n", dir, cpp_token_as_text (pfile, header));
+ cpp_print.line++;
+ }
+
+ /* The file name, line number or system header flags have changed, as
+ described in MAP. From this point on, the old cpp_print.map might be
+ pointing to freed memory, and so must not be dereferenced. */
+
+ void
+ cpp_cb_default_file_change (pfile, map)
+ cpp_reader *pfile ATTRIBUTE_UNUSED;
+ const struct line_map *map;
+ {
+ const char *flags = "";
+
+ /* First time? */
+ if (cpp_print.map == NULL)
+ {
+ /* Avoid printing foo.i when the main file is foo.c. */
+ if (!cpp_options_1->preprocessed)
+ cpp_print_line (map, map->from_line, flags);
+ }
+ else
+ {
+ /* Bring current file to correct line when entering a new file. */
+ if (map->reason == LC_ENTER)
+ cpp_maybe_print_line (map - 1, map->from_line - 1);
+
+ if (map->reason == LC_ENTER)
+ flags = " 1";
+ else if (map->reason == LC_LEAVE)
+ flags = " 2";
+ cpp_print_line (map, map->from_line, flags);
+ }
+
+ cpp_print.map = map;
+ }
+
+ /* Copy a #pragma directive to the preprocessed output. */
+ void
+ cpp_cb_default_def_pragma (pfile, line)
+ cpp_reader *pfile;
+ unsigned int line;
+ {
+ cpp_maybe_print_line (cpp_print.map, line);
+ fputs ("#pragma ", cpp_print.outf);
+ cpp_output_line (pfile, cpp_print.outf);
+ cpp_print.line++;
+ }
Index: cppmain.c
===================================================================
RCS file: /cvs/uberbaum/gcc/cppmain.c,v
retrieving revision 1.92
diff -c -p -r1.92 cppmain.c
*** cppmain.c 2002/03/14 18:17:13 1.92
--- cppmain.c 2002/03/19 06:54:56
*************** Foundation, 59 Temple Place - Suite 330,
*** 26,73 ****
#include "cpplib.h"
#include "intl.h"
- /* Encapsulates state used to convert the stream of tokens coming from
- cpp_get_token back into a text file. */
- struct printer
- {
- FILE *outf; /* Stream to write to. */
- const struct line_map *map; /* Logical to physical line mappings. */
- const cpp_token *prev; /* Previous token. */
- const cpp_token *source; /* Source token for spacing. */
- unsigned int line; /* Line currently being written. */
- unsigned char printed; /* Nonzero if something output at line. */
- };
-
int main PARAMS ((int, char **));
static void general_init PARAMS ((const char *));
static void do_preprocessing PARAMS ((int, char **));
static void setup_callbacks PARAMS ((void));
/* General output routines. */
- static void scan_translation_unit PARAMS ((cpp_reader *));
- static void check_multiline_token PARAMS ((const cpp_string *));
static int dump_macro PARAMS ((cpp_reader *, cpp_hashnode *, void *));
- static void print_line PARAMS ((const struct line_map *, unsigned int,
- const char *));
- static void maybe_print_line PARAMS ((const struct line_map *, unsigned int));
-
- /* Callback routines for the parser. Most of these are active only
- in specific modes. */
- static void cb_line_change PARAMS ((cpp_reader *, const cpp_token *, int));
- static void cb_define PARAMS ((cpp_reader *, unsigned int, cpp_hashnode *));
- static void cb_undef PARAMS ((cpp_reader *, unsigned int, cpp_hashnode *));
- static void cb_include PARAMS ((cpp_reader *, unsigned int,
- const unsigned char *, const cpp_token *));
- static void cb_ident PARAMS ((cpp_reader *, unsigned int,
- const cpp_string *));
- static void cb_file_change PARAMS ((cpp_reader *, const struct line_map *));
- static void cb_def_pragma PARAMS ((cpp_reader *, unsigned int));
-
const char *progname; /* Needs to be global. */
static cpp_reader *pfile; /* An opaque handle. */
- static cpp_options *options; /* Options of pfile. */
- static struct printer print;
int
main (argc, argv)
--- 26,41 ----
*************** main (argc, argv)
*** 78,84 ****
/* Construct a reader with default language GNU C89. */
pfile = cpp_create_reader (CLK_GNUC89);
! options = cpp_get_options (pfile);
do_preprocessing (argc, argv);
--- 46,52 ----
/* Construct a reader with default language GNU C89. */
pfile = cpp_create_reader (CLK_GNUC89);
! cpp_options_1 = cpp_get_options (pfile);
do_preprocessing (argc, argv);
*************** do_preprocessing (argc, argv)
*** 130,187 ****
line, it will have set pfile->help_only to indicate this. Exit
successfully. [The library does not exit itself, because
e.g. cc1 needs to print its own --help message at this point.] */
! if (options->help_only)
return;
! /* Initialize the printer structure. Setting print.line to -1 here
! is a trick to guarantee that the first token of the file will
! cause a linemarker to be output by maybe_print_line. */
! print.line = (unsigned int) -1;
! print.printed = 0;
! print.prev = 0;
! print.map = 0;
/* Open the output now. We must do so even if no_output is on,
because there may be other output than from the actual
preprocessing (e.g. from -dM). */
! if (options->out_fname[0] == '\0')
! print.outf = stdout;
else
{
! print.outf = fopen (options->out_fname, "w");
! if (print.outf == NULL)
{
! cpp_notice_from_errno (pfile, options->out_fname);
return;
}
}
setup_callbacks ();
! if (cpp_read_main_file (pfile, options->in_fname, NULL))
{
cpp_finish_options (pfile);
/* A successful cpp_read_main_file guarantees that we can call
cpp_scan_nooutput or cpp_get_token next. */
! if (options->no_output)
cpp_scan_nooutput (pfile);
else
! scan_translation_unit (pfile);
/* -dM command line option. Should this be in cpp_finish? */
! if (options->dump_macros == dump_only)
cpp_forall_identifiers (pfile, dump_macro, NULL);
cpp_finish (pfile);
}
/* Flush any pending output. */
! if (print.printed)
! putc ('\n', print.outf);
! if (ferror (print.outf) || fclose (print.outf))
! cpp_notice_from_errno (pfile, options->out_fname);
}
/* Set up the callbacks as appropriate. */
--- 98,155 ----
line, it will have set pfile->help_only to indicate this. Exit
successfully. [The library does not exit itself, because
e.g. cc1 needs to print its own --help message at this point.] */
! if (cpp_options_1->help_only)
return;
! /* Initialize the printer structure. Setting cpp_print.line to -1
! here is a trick to guarantee that the first token of the file
! will cause a linemarker to be output by cpp_maybe_print_line. */
! cpp_print.line = (unsigned int) -1;
! cpp_print.printed = 0;
! cpp_print.prev = 0;
! cpp_print.map = 0;
/* Open the output now. We must do so even if no_output is on,
because there may be other output than from the actual
preprocessing (e.g. from -dM). */
! if (cpp_options_1->out_fname[0] == '\0')
! cpp_print.outf = stdout;
else
{
! cpp_print.outf = fopen (cpp_options_1->out_fname, "w");
! if (cpp_print.outf == NULL)
{
! cpp_notice_from_errno (pfile, cpp_options_1->out_fname);
return;
}
}
setup_callbacks ();
! if (cpp_read_main_file (pfile, cpp_options_1->in_fname, NULL))
{
cpp_finish_options (pfile);
/* A successful cpp_read_main_file guarantees that we can call
cpp_scan_nooutput or cpp_get_token next. */
! if (cpp_options_1->no_output)
cpp_scan_nooutput (pfile);
else
! cpp_scan_translation_unit (pfile);
/* -dM command line option. Should this be in cpp_finish? */
! if (cpp_options_1->dump_macros == dump_only)
cpp_forall_identifiers (pfile, dump_macro, NULL);
cpp_finish (pfile);
}
/* Flush any pending output. */
! if (cpp_print.printed)
! putc ('\n', cpp_print.outf);
! if (ferror (cpp_print.outf) || fclose (cpp_print.outf))
! cpp_notice_from_errno (pfile, cpp_options_1->out_fname);
}
/* Set up the callbacks as appropriate. */
*************** setup_callbacks ()
*** 190,476 ****
{
cpp_callbacks *cb = cpp_get_callbacks (pfile);
! if (! options->no_output)
{
! cb->line_change = cb_line_change;
/* Don't emit #pragma or #ident directives if we are processing
assembly language; the assembler may choke on them. */
! if (options->lang != CLK_ASM)
{
! cb->ident = cb_ident;
! cb->def_pragma = cb_def_pragma;
}
! if (! options->no_line_commands)
! cb->file_change = cb_file_change;
! }
!
! if (options->dump_includes)
! cb->include = cb_include;
!
! if (options->dump_macros == dump_names
! || options->dump_macros == dump_definitions)
! {
! cb->define = cb_define;
! cb->undef = cb_undef;
}
- }
! /* Writes out the preprocessed file, handling spacing and paste
! avoidance issues. */
! static void
! scan_translation_unit (pfile)
! cpp_reader *pfile;
! {
! bool avoid_paste = false;
! print.source = NULL;
! for (;;)
{
! const cpp_token *token = cpp_get_token (pfile);
!
! if (token->type == CPP_PADDING)
! {
! avoid_paste = true;
! if (print.source == NULL
! || (!(print.source->flags & PREV_WHITE)
! && token->val.source == NULL))
! print.source = token->val.source;
! continue;
! }
!
! if (token->type == CPP_EOF)
! break;
!
! /* Subtle logic to output a space if and only if necessary. */
! if (avoid_paste)
! {
! if (print.source == NULL)
! print.source = token;
! if (print.source->flags & PREV_WHITE
! || (print.prev && cpp_avoid_paste (pfile, print.prev, token))
! || (print.prev == NULL && token->type == CPP_HASH))
! putc (' ', print.outf);
! }
! else if (token->flags & PREV_WHITE)
! putc (' ', print.outf);
!
! avoid_paste = false;
! print.source = NULL;
! print.prev = token;
! cpp_output_token (token, print.outf);
!
! if (token->type == CPP_STRING || token->type == CPP_WSTRING
! || token->type == CPP_COMMENT)
! check_multiline_token (&token->val.str);
}
}
- /* Adjust print.line for newlines embedded in tokens. */
- static void
- check_multiline_token (str)
- const cpp_string *str;
- {
- unsigned int i;
-
- for (i = 0; i < str->len; i++)
- if (str->text[i] == '\n')
- print.line++;
- }
-
- /* If the token read on logical line LINE needs to be output on a
- different line to the current one, output the required newlines or
- a line marker, and return 1. Otherwise return 0. */
- static void
- maybe_print_line (map, line)
- const struct line_map *map;
- unsigned int line;
- {
- /* End the previous line of text. */
- if (print.printed)
- {
- putc ('\n', print.outf);
- print.line++;
- print.printed = 0;
- }
-
- if (line >= print.line && line < print.line + 8)
- {
- while (line > print.line)
- {
- putc ('\n', print.outf);
- print.line++;
- }
- }
- else
- print_line (map, line, "");
- }
-
- /* Output a line marker for logical line LINE. Special flags are "1"
- or "2" indicating entering or leaving a file. */
- static void
- print_line (map, line, special_flags)
- const struct line_map *map;
- unsigned int line;
- const char *special_flags;
- {
- /* End any previous line of text. */
- if (print.printed)
- putc ('\n', print.outf);
- print.printed = 0;
-
- print.line = line;
- if (! options->no_line_commands)
- {
- size_t to_file_len = strlen (map->to_file);
- unsigned char *to_file_quoted = alloca (to_file_len * 4 + 1);
- unsigned char *p;
-
- /* cpp_quote_string does not nul-terminate, so we have to do it
- ourselves. */
- p = cpp_quote_string (to_file_quoted,
- (unsigned char *)map->to_file, to_file_len);
- *p = '\0';
- fprintf (print.outf, "# %u \"%s\"%s",
- SOURCE_LINE (map, print.line), to_file_quoted, special_flags);
-
- if (map->sysp == 2)
- fputs (" 3 4", print.outf);
- else if (map->sysp == 1)
- fputs (" 3", print.outf);
-
- putc ('\n', print.outf);
- }
- }
-
- /* Called when a line of output is started. TOKEN is the first token
- of the line, and at end of file will be CPP_EOF. */
- static void
- cb_line_change (pfile, token, parsing_args)
- cpp_reader *pfile ATTRIBUTE_UNUSED;
- const cpp_token *token;
- int parsing_args;
- {
- if (token->type == CPP_EOF || parsing_args)
- return;
-
- maybe_print_line (print.map, token->line);
- print.printed = 1;
- print.prev = 0;
- print.source = 0;
-
- /* Supply enough spaces to put this token in its original column,
- one space per column greater than 2, since scan_translation_unit
- will provide a space if PREV_WHITE. Don't bother trying to
- reconstruct tabs; we can't get it right in general, and nothing
- ought to care. Some things do care; the fault lies with them. */
- if (token->col > 2)
- {
- unsigned int spaces = token->col - 2;
-
- while (spaces--)
- putc (' ', print.outf);
- }
- }
-
- static void
- cb_ident (pfile, line, str)
- cpp_reader *pfile ATTRIBUTE_UNUSED;
- unsigned int line;
- const cpp_string * str;
- {
- maybe_print_line (print.map, line);
- fprintf (print.outf, "#ident \"%s\"\n", str->text);
- print.line++;
- }
-
- static void
- cb_define (pfile, line, node)
- cpp_reader *pfile;
- unsigned int line;
- cpp_hashnode *node;
- {
- maybe_print_line (print.map, line);
- fputs ("#define ", print.outf);
-
- /* -dD command line option. */
- if (options->dump_macros == dump_definitions)
- fputs ((const char *) cpp_macro_definition (pfile, node), print.outf);
- else
- fputs ((const char *) NODE_NAME (node), print.outf);
-
- putc ('\n', print.outf);
- print.line++;
- }
-
- static void
- cb_undef (pfile, line, node)
- cpp_reader *pfile ATTRIBUTE_UNUSED;
- unsigned int line;
- cpp_hashnode *node;
- {
- maybe_print_line (print.map, line);
- fprintf (print.outf, "#undef %s\n", NODE_NAME (node));
- print.line++;
- }
-
- static void
- cb_include (pfile, line, dir, header)
- cpp_reader *pfile;
- unsigned int line;
- const unsigned char *dir;
- const cpp_token *header;
- {
- maybe_print_line (print.map, line);
- fprintf (print.outf, "#%s %s\n", dir, cpp_token_as_text (pfile, header));
- print.line++;
- }
-
- /* The file name, line number or system header flags have changed, as
- described in MAP. From this point on, the old print.map might be
- pointing to freed memory, and so must not be dereferenced. */
-
- static void
- cb_file_change (pfile, map)
- cpp_reader *pfile ATTRIBUTE_UNUSED;
- const struct line_map *map;
- {
- const char *flags = "";
-
- /* First time? */
- if (print.map == NULL)
- {
- /* Avoid printing foo.i when the main file is foo.c. */
- if (!options->preprocessed)
- print_line (map, map->from_line, flags);
- }
- else
- {
- /* Bring current file to correct line when entering a new file. */
- if (map->reason == LC_ENTER)
- maybe_print_line (map - 1, map->from_line - 1);
-
- if (map->reason == LC_ENTER)
- flags = " 1";
- else if (map->reason == LC_LEAVE)
- flags = " 2";
- print_line (map, map->from_line, flags);
- }
-
- print.map = map;
- }
-
- /* Copy a #pragma directive to the preprocessed output. */
- static void
- cb_def_pragma (pfile, line)
- cpp_reader *pfile;
- unsigned int line;
- {
- maybe_print_line (print.map, line);
- fputs ("#pragma ", print.outf);
- cpp_output_line (pfile, print.outf);
- print.line++;
- }
-
/* Dump out the hash table. */
static int
dump_macro (pfile, node, v)
--- 158,188 ----
{
cpp_callbacks *cb = cpp_get_callbacks (pfile);
! if (! cpp_options_1->no_output)
{
! cb->line_change = cpp_cb_default_line_change;
/* Don't emit #pragma or #ident directives if we are processing
assembly language; the assembler may choke on them. */
! if (cpp_options_1->lang != CLK_ASM)
{
! cb->ident = cpp_cb_default_ident;
! cb->def_pragma = cpp_cb_default_def_pragma;
}
! if (! cpp_options_1->no_line_commands)
! cb->file_change = cpp_cb_default_file_change;
}
! if (cpp_options_1->dump_includes)
! cb->include = cpp_cb_default_include;
! if (cpp_options_1->dump_macros == dump_names
! || cpp_options_1->dump_macros == dump_definitions)
{
! cb->define = cpp_cb_default_define;
! cb->undef = cpp_cb_default_undef;
}
}
/* Dump out the hash table. */
static int
dump_macro (pfile, node, v)
*************** dump_macro (pfile, node, v)
*** 480,489 ****
{
if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
{
! fputs ("#define ", print.outf);
! fputs ((const char *) cpp_macro_definition (pfile, node), print.outf);
! putc ('\n', print.outf);
! print.line++;
}
return 1;
--- 192,202 ----
{
if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
{
! fputs ("#define ", cpp_print.outf);
! fputs ((const char *) cpp_macro_definition (pfile, node),
! cpp_print.outf);
! putc ('\n', cpp_print.outf);
! cpp_print.line++;
}
return 1;
Index: cpplib.h
===================================================================
RCS file: /cvs/uberbaum/gcc/cpplib.h,v
retrieving revision 1.203
diff -c -p -r1.203 cpplib.h
*** cpplib.h 2002/03/14 18:17:12 1.203
--- cpplib.h 2002/03/19 06:54:56
*************** struct cpp_options
*** 338,343 ****
--- 338,346 ----
/* Nonzero means give all the error messages the ANSI standard requires. */
unsigned char pedantic;
+ /* Nonzero means we're only preprocessing, so don't compile. */
+ unsigned char preprocess;
+
/* Nonzero means we're looking at already preprocessed code, so don't
bother trying to do macro expansion and whatnot. */
unsigned char preprocessed;
*************** struct cpp_hashnode
*** 453,458 ****
--- 456,501 ----
enum builtin_type builtin; /* Code for a builtin macro. */
} value;
};
+
+ /* Encapsulates state used to convert the stream of tokens coming from
+ cpp_get_token back into a text file. */
+ struct cpp_printer
+ {
+ FILE *outf; /* Stream to write to. */
+ const struct line_map *map; /* Logical to physical line mappings. */
+ const cpp_token *prev; /* Previous token. */
+ const cpp_token *source; /* Source token for spacing. */
+ unsigned int line; /* Line currently being written. */
+ unsigned char printed; /* Nonzero if something output at line. */
+ };
+
+ extern cpp_options *cpp_options_1; /* Options of pfile. */
+
+ extern struct cpp_printer cpp_print;
+
+ extern void cpp_scan_translation_unit PARAMS ((cpp_reader *));
+ extern void cpp_check_multiline_token PARAMS ((const cpp_string *));
+ extern void cpp_maybe_print_line PARAMS ((const struct line_map *,
+ unsigned int));
+ extern void cpp_print_line PARAMS ((const struct line_map *, unsigned int,
+ const char *));
+
+ /* Callback routines for the parser. Most of these are active only
+ in specific modes. */
+ extern void cpp_cb_default_line_change PARAMS ((cpp_reader *,
+ const cpp_token *, int));
+ extern void cpp_cb_default_define PARAMS ((cpp_reader *, unsigned int,
+ cpp_hashnode *));
+ extern void cpp_cb_default_undef PARAMS ((cpp_reader *, unsigned int,
+ cpp_hashnode *));
+ extern void cpp_cb_default_include PARAMS ((cpp_reader *, unsigned int,
+ const unsigned char *,
+ const cpp_token *));
+ extern void cpp_cb_default_ident PARAMS ((cpp_reader *, unsigned int,
+ const cpp_string *));
+ extern void cpp_cb_default_file_change PARAMS ((cpp_reader *,
+ const struct line_map *));
+ extern void cpp_cb_default_def_pragma PARAMS ((cpp_reader *, unsigned int));
/* Call this first to get a handle to pass to other functions. */
extern cpp_reader *cpp_create_reader PARAMS ((enum c_lang));
Index: flags.h
===================================================================
RCS file: /cvs/uberbaum/gcc/flags.h,v
retrieving revision 1.80
diff -c -p -r1.80 flags.h
*** flags.h 2002/03/07 13:38:21 1.80
--- flags.h 2002/03/19 06:54:56
*************** extern int flag_strict_aliasing;
*** 523,528 ****
--- 523,531 ----
may cause large objects to be allocated dynamically. */
extern int flag_stack_check;
+ /* Nonzero if preprocessing only. */
+ extern int flag_preprocess_only;
+
/* Do the full regmove optimization pass. */
extern int flag_regmove;
Index: toplev.c
===================================================================
RCS file: /cvs/uberbaum/gcc/toplev.c,v
retrieving revision 1.597
diff -c -p -r1.597 toplev.c
*** toplev.c 2002/03/17 20:41:35 1.597
--- toplev.c 2002/03/19 06:54:57
*************** static void do_compile PARAMS ((void));
*** 104,110 ****
static void process_options PARAMS ((void));
static void lang_independent_init PARAMS ((void));
static int lang_dependent_init PARAMS ((const char *));
! static void init_asm_output PARAMS ((const char *));
static void finalize PARAMS ((void));
static void set_target_switch PARAMS ((const char *));
--- 104,110 ----
static void process_options PARAMS ((void));
static void lang_independent_init PARAMS ((void));
static int lang_dependent_init PARAMS ((const char *));
! static void init_output_file PARAMS ((const char *));
static void finalize PARAMS ((void));
static void set_target_switch PARAMS ((const char *));
*************** int flag_pack_struct = 0;
*** 793,798 ****
--- 793,801 ----
to be allocated dynamically. */
int flag_stack_check;
+ /* Nonzero if preprocessing only. */
+ int flag_preprocess_only;
+
/* When non-NULL, indicates that whenever space is allocated on the
stack, the resulting stack pointer must not pass this
address---that is, for stacks that grow downward, the stack pointer
*************** compile_file ()
*** 2118,2124 ****
what's left of the symbol table output. */
timevar_pop (TV_PARSE);
! if (flag_syntax_only)
return;
globals = getdecls ();
--- 2121,2127 ----
what's left of the symbol table output. */
timevar_pop (TV_PARSE);
! if (flag_syntax_only || flag_preprocess_only)
return;
globals = getdecls ();
*************** display_help ()
*** 3566,3571 ****
--- 3569,3575 ----
unsigned long i;
const char *lang;
+ printf (_(" -E Preprocess the input. Do not run the compiler.\n"));
printf (_(" -ffixed-<register> Mark <register> as being unavailable to the compiler\n"));
printf (_(" -fcall-used-<register> Mark <register> as being corrupted by function calls\n"));
printf (_(" -fcall-saved-<register> Mark <register> as being preserved across functions\n"));
*************** independent_decode_option (argc, argv)
*** 4179,4184 ****
--- 4183,4192 ----
default:
return 0;
+ case 'E':
+ flag_preprocess_only = 1;
+ break;
+
case 'O':
/* Already been treated in main (). Do nothing. */
break;
*************** print_switch_values (file, pos, max, ind
*** 4495,4501 ****
temporary file or bit bucket for us. NAME is the file specified on
the command line, possibly NULL. */
static void
! init_asm_output (name)
const char *name;
{
if (name == NULL && asm_file_name == 0)
--- 4503,4509 ----
temporary file or bit bucket for us. NAME is the file specified on
the command line, possibly NULL. */
static void
! init_output_file (name)
const char *name;
{
if (name == NULL && asm_file_name == 0)
*************** init_asm_output (name)
*** 4508,4514 ****
char *dumpname = (char *) xmalloc (len + 6);
memcpy (dumpname, dump_base_name, len + 1);
strip_off_ending (dumpname, len);
! strcat (dumpname, ".s");
asm_file_name = dumpname;
}
if (!strcmp (asm_file_name, "-"))
--- 4516,4525 ----
char *dumpname = (char *) xmalloc (len + 6);
memcpy (dumpname, dump_base_name, len + 1);
strip_off_ending (dumpname, len);
! if (flag_preprocess_only)
! strcat (dumpname, ".i");
! else
! strcat (dumpname, ".s");
asm_file_name = dumpname;
}
if (!strcmp (asm_file_name, "-"))
*************** init_asm_output (name)
*** 4524,4529 ****
--- 4535,4543 ----
_IOFBF, IO_BUFFER_SIZE);
#endif
+ if (flag_preprocess_only)
+ return;
+
if (!flag_syntax_only)
{
#ifdef ASM_FILE_START
*************** lang_dependent_init (name)
*** 5050,5055 ****
--- 5064,5075 ----
if (dump_base_name == 0)
dump_base_name = name ? name : "gccdump";
+ /* When preprocessing, initialize the output here because
+ cpp_read_main_line will need a file to start outputting line
+ information. */
+ if (flag_preprocess_only)
+ init_output_file (name);
+
/* Front-end initialization. This hook can assume that GC,
identifier hashes etc. are set up, but debug initialization is
not done yet. This routine must return the original filename
*************** lang_dependent_init (name)
*** 5061,5067 ****
/* Is this duplication necessary? */
name = ggc_strdup (name);
main_input_filename = input_filename = name;
! init_asm_output (name);
/* These create various _DECL nodes, so need to be called after the
front end is initialized. */
--- 5081,5088 ----
/* Is this duplication necessary? */
name = ggc_strdup (name);
main_input_filename = input_filename = name;
! if (!flag_preprocess_only)
! init_output_file (name);
/* These create various _DECL nodes, so need to be called after the
front end is initialized. */
*************** finalize ()
*** 5102,5119 ****
unlink (aux_info_file_name);
}
- /* Close non-debugging input and output files. Take special care to note
- whether fclose returns an error, since the pages might still be on the
- buffer chain while the file is open. */
-
- if (asm_out_file)
- {
- if (ferror (asm_out_file) != 0)
- fatal_io_error ("error writing to %s", asm_file_name);
- if (fclose (asm_out_file) != 0)
- fatal_io_error ("error closing %s", asm_file_name);
- }
-
/* Do whatever is necessary to finish printing the graphs. */
if (graph_dump_format != no_graph)
{
--- 5123,5128 ----
*************** finalize ()
*** 5144,5149 ****
--- 5153,5174 ----
/* Language-specific end of compilation actions. */
(*lang_hooks.finish) ();
+
+ /* Close non-debugging input and output files. Take special care to note
+ whether fclose returns an error, since the pages might still be on the
+ buffer chain while the file is open.
+
+ Close the output files after we run the language specific cleanups
+ becasue, in preprocessor mode (-E) we may have pending output
+ to be flushed. */
+
+ if (asm_out_file)
+ {
+ if (ferror (asm_out_file) != 0)
+ fatal_io_error ("error writing to %s", asm_file_name);
+ if (fclose (asm_out_file) != 0)
+ fatal_io_error ("error closing %s", asm_file_name);
+ }
}
/* Initialize the compiler, and compile the input file. */