diff options
author | neil <neil@138bc75d-0d04-0410-961f-82ee72b054a4> | 2001-08-05 17:31:25 +0000 |
---|---|---|
committer | neil <neil@138bc75d-0d04-0410-961f-82ee72b054a4> | 2001-08-05 17:31:25 +0000 |
commit | 1ea7ed2119257b80d0327b5c3d979cebd87aa894 (patch) | |
tree | 37a35ef3c394621edd2c84f886dcc1310e94c38d /gcc/cppmain.c | |
parent | 9c0e6d90f7b28384e458f3844e0eb12adce16452 (diff) | |
download | gcc-1ea7ed2119257b80d0327b5c3d979cebd87aa894.tar.gz |
PR preprocessor/3081
* c-lex.c (map): New.
(cb_file_change): Update map and use it.
(cb_def_pragma, cb_define, cb_undef): Use map and line.
(c_lex): Update to use map.
* cpperror.c (print_location): Move to using logical line numbers.
* cppfiles.c (stack_include_file): Update for new _cpp_do_file_change.
(cpp_make_system_header): Similarly.
(_cpp_execute_include): Stop line numbering hacks. Store the
line we will return to.
* cpphash.h (CPP_BUF_LINE): Remove.
(struct cpp_buffer): Remove lineno and pseudo_newlines.
Add map and return_to_line.
(_cpp_do_file_change): Update.
* cppinit.c (cpp_start_read): Update line kludge.
* cpplex.c (handle_newline): Don't update lineno and pseudo_newlines.
(trigraph_ok): Use logical line numbers for diagnostics.
(skip_block_comment): Likewise.
(skip_whitespace): Likewise.
(skip_line_comment): Use pfile->line instead.
(_cpp_lex_token): Update to use logical line numbering exclusively.
Handle BOL locally. Accept new lines in directives, but keep
pfile->line decremented. Diagnostics use logical lines. Update
directive handling.
* cpplib.c (SEEN_EOL): New.
(skip_rest_of_line, check_eol): Use it.
(end_directive): Increase line number when accepting the newline
at the end of a directive.
(run_directive): Simplify.
(do_line): Bad LC_LEAVEs become LC_RENAMEs. Update.
(_cpp_do_file_change): Update to take buffer line number as an
argument, and store the current map in the cpp_reader. Remove
line number kludges.
(_cpp_do__Pragma): Restore output position after a _Pragma.
(cpp_push_buffer): Don't set output line or lineno.
(_cpp_pop_buffer): Transfer more info from a faked buffer.
Remove line kludge. Set output_line.
* cppmacro.c (builtin_macro): Update handling of __LINE__.
(parse_arg): Use logical lines.
(save_lookahead_token): Save EOFs too now.
* cppmain.c (struct printer): Fix comments.
(printer_init): Simplify, let caller do errors.
(scan_translation_unit, check_multiline_token, dump_macro): Update.
(maybe_print_line): Simplify.
(print_line): Don't print a linemarker if -P.
(cb_define, cb_undef, cb_def_pragma, cb_ident, cb_include): Update.
(cb_file_change): Simplify.
* line-map.h (LAST_SOURCE_LINE): Fix.
(CURRENT_LINE_MAP): New.
* gcc.dg/cpp/19951025-1.c: Revert.
* gcc.dg/cpp/directiv.c: We no longer process directives that
interrupt macro arguments.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@44650 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'gcc/cppmain.c')
-rw-r--r-- | gcc/cppmain.c | 152 |
1 files changed, 71 insertions, 81 deletions
diff --git a/gcc/cppmain.c b/gcc/cppmain.c index 63026102600..86d245ab9f1 100644 --- a/gcc/cppmain.c +++ b/gcc/cppmain.c @@ -30,12 +30,12 @@ Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. cpp_get_token back into a text file. */ struct printer { - FILE *outf; /* stream to write to. */ - const char *last_fname; /* previous file name. */ - const char *syshdr_flags; /* system header flags, if any. */ - unsigned int lineno; /* line currently being written. */ - unsigned char printed; /* nonzero if something output at lineno. */ - struct line_map *map; /* logical to physical line mappings. */ + FILE *outf; /* Stream to write to. */ + const char *filename; /* Name of current file. */ + const char *syshdr_flags; /* System header flags, if any. */ + unsigned int line; /* Line currently being written. */ + unsigned char printed; /* Nonzero if something output at line. */ + struct line_map *map; /* Logical to physical line mappings. */ }; int main PARAMS ((int, char **)); @@ -46,10 +46,10 @@ static void setup_callbacks PARAMS ((void)); /* General output routines. */ static void scan_translation_unit PARAMS ((cpp_reader *)); static void check_multiline_token PARAMS ((cpp_string *)); -static int printer_init PARAMS ((cpp_reader *)); +static void printer_init PARAMS ((void)); static int dump_macro PARAMS ((cpp_reader *, cpp_hashnode *, void *)); -static void print_line PARAMS ((const char *)); +static void print_line PARAMS ((unsigned int, const char *)); static void maybe_print_line PARAMS ((unsigned int)); /* Callback routines for the parser. Most of these are active only @@ -144,8 +144,12 @@ do_preprocessing (argc, argv) /* 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 (printer_init (pfile)) - return; + printer_init (); + if (print.outf == NULL) + { + cpp_notice_from_errno (pfile, options->out_fname); + return; + } setup_callbacks (); @@ -216,7 +220,7 @@ scan_translation_unit (pfile) break; line = cpp_get_line (pfile)->output_line; - if (print.lineno != line) + if (print.line != line) { unsigned int col = cpp_get_line (pfile)->col; @@ -253,7 +257,7 @@ scan_translation_unit (pfile) } } -/* Adjust print.lineno for newlines embedded in tokens. */ +/* Adjust print.line for newlines embedded in tokens. */ static void check_multiline_token (str) cpp_string *str; @@ -262,85 +266,69 @@ check_multiline_token (str) for (i = 0; i < str->len; i++) if (str->text[i] == '\n') - print.lineno++; + print.line++; } /* Initialize a cpp_printer structure. As a side effect, open the - output file. */ -static int -printer_init (pfile) - cpp_reader *pfile; + output file. If print.outf is NULL an error occurred. */ +static void +printer_init () { - print.last_fname = 0; - print.lineno = 0; + /* Setting print.line to -1 here guarantees 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.map = 0; if (options->out_fname[0] == '\0') print.outf = stdout; else - { - print.outf = fopen (options->out_fname, "w"); - if (! print.outf) - { - cpp_notice_from_errno (pfile, options->out_fname); - return 1; - } - } - - return 0; + print.outf = fopen (options->out_fname, "w"); } -/* Newline-terminate any output line currently in progress. If - appropriate, write the current line number to the output, or pad - with newlines so the output line matches the current 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 (line) unsigned int line; { - /* End the previous line of text (probably only needed until we get - multi-line tokens fixed). */ + /* End the previous line of text. */ if (print.printed) { putc ('\n', print.outf); - print.lineno++; + print.line++; print.printed = 0; } - if (options->no_line_commands) + if (line >= print.line && line < print.line + 8) { - print.lineno = line; - return; - } - - /* print.lineno is zero if this is the first token of the file. We - handle this specially, so that a first line of "# 1 "foo.c" in - file foo.i outputs just the foo.c line, and not a foo.i line. */ - if (line >= print.lineno && line < print.lineno + 8 && print.lineno) - { - while (line > print.lineno) + while (line > print.line) { putc ('\n', print.outf); - print.lineno++; + print.line++; } } else - { - print.lineno = line; - print_line (""); - } + print_line (line, ""); } static void -print_line (special_flags) - const char *special_flags; +print_line (line, special_flags) + unsigned int line; + const char *special_flags; { /* End any previous line of text. */ if (print.printed) putc ('\n', print.outf); print.printed = 0; - fprintf (print.outf, "# %u \"%s\"%s%s\n", - print.lineno, print.last_fname, special_flags, print.syshdr_flags); + print.line = line; + if (! options->no_line_commands) + fprintf (print.outf, "# %u \"%s\"%s%s\n", + SOURCE_LINE (print.map, print.line), + print.filename, special_flags, print.syshdr_flags); } /* Callbacks. */ @@ -348,21 +336,21 @@ print_line (special_flags) static void cb_ident (pfile, line, str) cpp_reader *pfile ATTRIBUTE_UNUSED; - unsigned int line ATTRIBUTE_UNUSED; + unsigned int line; const cpp_string * str; { - maybe_print_line (cpp_get_line (pfile)->output_line); + maybe_print_line (line); fprintf (print.outf, "#ident \"%s\"\n", str->text); - print.lineno++; + print.line++; } static void cb_define (pfile, line, node) cpp_reader *pfile; - unsigned int line ATTRIBUTE_UNUSED; + unsigned int line; cpp_hashnode *node; { - maybe_print_line (cpp_get_line (pfile)->output_line); + maybe_print_line (line); fputs ("#define ", print.outf); /* -dD command line option. */ @@ -372,30 +360,30 @@ cb_define (pfile, line, node) fputs ((const char *) NODE_NAME (node), print.outf); putc ('\n', print.outf); - print.lineno++; + print.line++; } static void cb_undef (pfile, line, node) - cpp_reader *pfile; - unsigned int line ATTRIBUTE_UNUSED; + cpp_reader *pfile ATTRIBUTE_UNUSED; + unsigned int line; cpp_hashnode *node; { - maybe_print_line (cpp_get_line (pfile)->output_line); + maybe_print_line (line); fprintf (print.outf, "#undef %s\n", NODE_NAME (node)); - print.lineno++; + print.line++; } static void cb_include (pfile, line, dir, header) - cpp_reader *pfile ATTRIBUTE_UNUSED; - unsigned int line ATTRIBUTE_UNUSED; + cpp_reader *pfile; + unsigned int line; const unsigned char *dir; const cpp_token *header; { - maybe_print_line (cpp_get_line (pfile)->output_line); + maybe_print_line (line); fprintf (print.outf, "#%s %s\n", dir, cpp_token_as_text (pfile, header)); - print.lineno++; + print.line++; } static void @@ -403,12 +391,16 @@ cb_file_change (pfile, fc) cpp_reader *pfile ATTRIBUTE_UNUSED; const cpp_file_change *fc; { - /* Bring current file to correct line (except first file). */ - if (fc->reason == LC_ENTER && !MAIN_FILE_P (fc->map)) - maybe_print_line (SOURCE_LINE (fc->map - 1, fc->line - 1)); + bool first_time = print.map == NULL; + + /* Bring current file to correct line. We handle the first file + change callback specially, so that a first line of "# 1 "foo.c" + in file foo.i outputs just the foo.c line, and not a foo.i line. */ + if (fc->reason == LC_ENTER && !first_time) + maybe_print_line (fc->line - 1); print.map = fc->map; - print.last_fname = fc->map->to_file; + print.filename = fc->map->to_file; if (fc->externc) print.syshdr_flags = " 3 4"; else if (fc->sysp) @@ -416,18 +408,16 @@ cb_file_change (pfile, fc) else print.syshdr_flags = ""; - if (print.lineno) + if (!first_time) { const char *flags = ""; - print.lineno = SOURCE_LINE (fc->map, fc->line); if (fc->reason == LC_ENTER) flags = " 1"; else if (fc->reason == LC_LEAVE) flags = " 2"; - if (! options->no_line_commands) - print_line (flags); + print_line (fc->line, flags); } } @@ -436,12 +426,12 @@ cb_file_change (pfile, fc) static void cb_def_pragma (pfile, line) cpp_reader *pfile; - unsigned int line ATTRIBUTE_UNUSED; + unsigned int line; { - maybe_print_line (cpp_get_line (pfile)->output_line); + maybe_print_line (line); fputs ("#pragma ", print.outf); cpp_output_line (pfile, print.outf); - print.lineno++; + print.line++; } /* Dump out the hash table. */ @@ -456,7 +446,7 @@ dump_macro (pfile, node, v) fputs ("#define ", print.outf); fputs ((const char *) cpp_macro_definition (pfile, node), print.outf); putc ('\n', print.outf); - print.lineno++; + print.line++; } return 1; |