Commit ad2a084d by Neil Booth Committed by Neil Booth

c-lex.c: Move main_input_filename handling to FC_ENTER.

        * c-lex.c: Move main_input_filename handling to FC_ENTER. Clean up.
        * cpperror.c (print_containing_files): Get right line number.
        (print_location): Output column of 1 if 0.
        * cppfiles.c (stack_include_file): cpp_push_buffer handles
        the callback.
        * cpphash.h (_cpp_do_file_change): No longer external.
        * cpplib.c (do_file_change): Now local to cpplib.c.
        (do_line): Fake a buffer stack for preprocessed files.
        (cpp_push_buffer): Create a file_change callback.  Handle faked
        buffers.
        (cpp_pop_buffer): Similarly.
        * cpplib.h: BUF_FAKE: New buffer type.
        * cppmain.c: Update to handle correct file renaming where a
        #line is the first line of the main file, and produce only
        the renamed file, not the original file, as output.

From-SVN: r38319
parent 3b67042a
2000-12-17 Neil Booth <neil@daikokuya.demon.co.uk>
* c-lex.c: Move main_input_filename handling to FC_ENTER. Clean up.
* cpperror.c (print_containing_files): Get right line number.
(print_location): Output column of 1 if 0.
* cppfiles.c (stack_include_file): cpp_push_buffer handles
the callback.
* cpphash.h (_cpp_do_file_change): No longer external.
* cpplib.c (do_file_change): Now local to cpplib.c.
(do_line): Fake a buffer stack for preprocessed files.
(cpp_push_buffer): Create a file_change callback. Handle faked
buffers.
(cpp_pop_buffer): Similarly.
* cpplib.h: BUF_FAKE: New buffer type.
* cppmain.c: Update to handle correct file renaming where a
#line is the first line of the main file, and produce only
the renamed file, not the original file, as output.
2000-12-17 Michael Hayes <m.hayes@elec.canterbury.ac.nz> 2000-12-17 Michael Hayes <m.hayes@elec.canterbury.ac.nz>
* config/c4x/c4x.md: Remove redundant @s from output patterns. * config/c4x/c4x.md: Remove redundant @s from output patterns.
......
...@@ -234,14 +234,10 @@ cb_change_file (pfile, fc) ...@@ -234,14 +234,10 @@ cb_change_file (pfile, fc)
cpp_reader *pfile ATTRIBUTE_UNUSED; cpp_reader *pfile ATTRIBUTE_UNUSED;
const cpp_file_change *fc; const cpp_file_change *fc;
{ {
if (fc->from.filename == 0)
main_input_filename = fc->to.filename;
in_system_header = fc->sysp;
/* Do the actions implied by the preceding numbers. */ /* Do the actions implied by the preceding numbers. */
if (fc->reason == FC_ENTER) if (fc->reason == FC_ENTER)
{ {
/* FIXME. Don't stack the main buffer on the input stack. */ /* Don't stack the main buffer on the input stack. */
if (fc->from.filename) if (fc->from.filename)
{ {
lineno = lex_lineno; lineno = lex_lineno;
...@@ -258,6 +254,8 @@ cb_change_file (pfile, fc) ...@@ -258,6 +254,8 @@ cb_change_file (pfile, fc)
} }
#endif #endif
} }
else
main_input_filename = fc->to.filename;
} }
else if (fc->reason == FC_LEAVE) else if (fc->reason == FC_LEAVE)
{ {
...@@ -288,11 +286,9 @@ cb_change_file (pfile, fc) ...@@ -288,11 +286,9 @@ cb_change_file (pfile, fc)
else else
error ("leaving more files than we entered"); error ("leaving more files than we entered");
} }
else if (fc->reason == FC_RENAME)
input_filename = fc->to.filename;
update_header_times (fc->to.filename); update_header_times (fc->to.filename);
in_system_header = fc->sysp;
input_filename = fc->to.filename; input_filename = fc->to.filename;
lex_lineno = fc->to.lineno; lex_lineno = fc->to.lineno;
......
...@@ -69,7 +69,7 @@ print_containing_files (ip) ...@@ -69,7 +69,7 @@ print_containing_files (ip)
The trailing comma is at the beginning of this message, The trailing comma is at the beginning of this message,
and the trailing colon is not translated. */ and the trailing colon is not translated. */
fprintf (stderr, _(",\n from %s:%u"), fprintf (stderr, _(",\n from %s:%u"),
ip->nominal_fname, CPP_BUF_LINE (ip) - 1); ip->nominal_fname, CPP_BUF_LINE (ip));
} }
fputs (":\n", stderr); fputs (":\n", stderr);
} }
...@@ -111,6 +111,9 @@ print_location (pfile, filename, pos) ...@@ -111,6 +111,9 @@ print_location (pfile, filename, pos)
col = pos->col; col = pos->col;
} }
if (col == 0)
col = 1;
/* Don't repeat the include stack unnecessarily. */ /* Don't repeat the include stack unnecessarily. */
if (buffer->prev && ! buffer->include_stack_listed) if (buffer->prev && ! buffer->include_stack_listed)
{ {
......
...@@ -245,16 +245,8 @@ stack_include_file (pfile, inc) ...@@ -245,16 +245,8 @@ stack_include_file (pfile, inc)
cpp_reader *pfile; cpp_reader *pfile;
struct include_file *inc; struct include_file *inc;
{ {
const char *filename = 0;
unsigned int lineno = 0;
cpp_buffer *fp; cpp_buffer *fp;
if (pfile->buffer)
{
filename = pfile->buffer->nominal_fname;
lineno = pfile->buffer->lineno;
}
/* Not in cache? */ /* Not in cache? */
if (! inc->buffer) if (! inc->buffer)
read_include_file (pfile, inc); read_include_file (pfile, inc);
...@@ -268,7 +260,6 @@ stack_include_file (pfile, inc) ...@@ -268,7 +260,6 @@ stack_include_file (pfile, inc)
fp->rlimit += inc->st.st_size; fp->rlimit += inc->st.st_size;
fp->cur = fp->buf; fp->cur = fp->buf;
fp->line_base = fp->buf; fp->line_base = fp->buf;
fp->lineno = 0; /* For _cpp_do_file_change. */
fp->inc->refcnt++; fp->inc->refcnt++;
if (inc->foundhere) if (inc->foundhere)
fp->sysp = inc->foundhere->sysp; fp->sysp = inc->foundhere->sysp;
...@@ -282,10 +273,6 @@ stack_include_file (pfile, inc) ...@@ -282,10 +273,6 @@ stack_include_file (pfile, inc)
pfile->mi_state = MI_OUTSIDE; pfile->mi_state = MI_OUTSIDE;
pfile->mi_cmacro = 0; pfile->mi_cmacro = 0;
pfile->include_depth++; pfile->include_depth++;
_cpp_do_file_change (pfile, FC_ENTER, filename, lineno);
fp->lineno = 1;
} }
/* Read the file referenced by INC into the file cache. /* Read the file referenced by INC into the file cache.
......
...@@ -229,8 +229,6 @@ extern void _cpp_do__Pragma PARAMS ((cpp_reader *)); ...@@ -229,8 +229,6 @@ extern void _cpp_do__Pragma PARAMS ((cpp_reader *));
extern void _cpp_init_stacks PARAMS ((cpp_reader *)); extern void _cpp_init_stacks PARAMS ((cpp_reader *));
extern void _cpp_cleanup_stacks PARAMS ((cpp_reader *)); extern void _cpp_cleanup_stacks PARAMS ((cpp_reader *));
extern void _cpp_init_internal_pragmas PARAMS ((cpp_reader *)); extern void _cpp_init_internal_pragmas PARAMS ((cpp_reader *));
extern void _cpp_do_file_change PARAMS ((cpp_reader *, enum cpp_fc_reason,
const char *, unsigned int));
/* Utility routines and macros. */ /* Utility routines and macros. */
#define DSC(str) (const U_CHAR *)str, sizeof str - 1 #define DSC(str) (const U_CHAR *)str, sizeof str - 1
......
...@@ -108,6 +108,8 @@ static cpp_hashnode *parse_assertion PARAMS ((cpp_reader *, struct answer **, ...@@ -108,6 +108,8 @@ static cpp_hashnode *parse_assertion PARAMS ((cpp_reader *, struct answer **,
static struct answer ** find_answer PARAMS ((cpp_hashnode *, static struct answer ** find_answer PARAMS ((cpp_hashnode *,
const struct answer *)); const struct answer *));
static void handle_assertion PARAMS ((cpp_reader *, const char *, int)); static void handle_assertion PARAMS ((cpp_reader *, const char *, int));
static void do_file_change PARAMS ((cpp_reader *, enum cpp_fc_reason,
const char *, unsigned int));
/* This is the table of directive handlers. It is ordered by /* This is the table of directive handlers. It is ordered by
frequency of occurrence; the numbers at the end are directive frequency of occurrence; the numbers at the end are directive
...@@ -721,7 +723,6 @@ do_line (pfile) ...@@ -721,7 +723,6 @@ do_line (pfile)
fname[len] = '\0'; fname[len] = '\0';
_cpp_simplify_pathname (fname); _cpp_simplify_pathname (fname);
buffer->nominal_fname = fname;
if (! pfile->state.line_extension) if (! pfile->state.line_extension)
check_eol (pfile); check_eol (pfile);
...@@ -748,8 +749,36 @@ do_line (pfile) ...@@ -748,8 +749,36 @@ do_line (pfile)
sysp = 2, read_flag (pfile, flag); sysp = 2, read_flag (pfile, flag);
} }
if (reason == FC_ENTER)
{
cpp_push_buffer (pfile, 0, 0, BUF_FAKE, fname);
buffer = pfile->buffer;
}
else if (reason == FC_LEAVE)
{
if (buffer->type != BUF_FAKE)
cpp_warning (pfile, "file \"%s\" left but not entered",
buffer->nominal_fname);
else
{
cpp_pop_buffer (pfile);
buffer = pfile->buffer;
if (strcmp (buffer->nominal_fname, fname))
cpp_warning (pfile, "expected to return to file \"%s\"",
buffer->nominal_fname);
if (buffer->lineno + 1 != new_lineno)
cpp_warning (pfile, "expected to return to line number %u",
buffer->lineno + 1);
if (buffer->sysp != sysp)
cpp_warning (pfile, "header flags for \"%s\" have changed",
buffer->nominal_fname);
}
}
cpp_make_system_header (pfile, sysp, sysp == 2); cpp_make_system_header (pfile, sysp, sysp == 2);
} }
buffer->nominal_fname = fname;
} }
else if (token.type != CPP_EOF) else if (token.type != CPP_EOF)
{ {
...@@ -760,13 +789,19 @@ do_line (pfile) ...@@ -760,13 +789,19 @@ do_line (pfile)
/* Our line number is incremented after the directive is processed. */ /* Our line number is incremented after the directive is processed. */
buffer->lineno = new_lineno - 1; buffer->lineno = new_lineno - 1;
_cpp_do_file_change (pfile, reason, filename, lineno);
if (reason == FC_RENAME)
{
/* Special case for file "foo.i" with "# 1 foo.c" on first line. */
if (! buffer->prev && pfile->directive_pos.line == 1)
filename = 0;
do_file_change (pfile, reason, filename, lineno);
}
} }
/* Arrange the file_change callback. The assumption is that the /* Arrange the file_change callback. */
current buffer's lineno is one less than the next line. */ static void
void do_file_change (pfile, reason, from_file, from_lineno)
_cpp_do_file_change (pfile, reason, from_file, from_lineno)
cpp_reader *pfile; cpp_reader *pfile;
enum cpp_fc_reason reason; enum cpp_fc_reason reason;
const char *from_file; const char *from_file;
...@@ -1622,9 +1657,7 @@ cpp_define (pfile, str) ...@@ -1622,9 +1657,7 @@ cpp_define (pfile, str)
run_directive (pfile, T_DEFINE, BUF_CL_OPTION, buf, count); run_directive (pfile, T_DEFINE, BUF_CL_OPTION, buf, count);
} }
/* Slight variant of the above for use by initialize_builtins, which (a) /* Slight variant of the above for use by initialize_builtins. */
knows how to set up the buffer itself, (b) needs a different "filename"
tag. */
void void
_cpp_define_builtin (pfile, str) _cpp_define_builtin (pfile, str)
cpp_reader *pfile; cpp_reader *pfile;
...@@ -1698,35 +1731,63 @@ cpp_push_buffer (pfile, buffer, len, type, filename) ...@@ -1698,35 +1731,63 @@ cpp_push_buffer (pfile, buffer, len, type, filename)
{ {
cpp_buffer *new = xobnew (pfile->buffer_ob, cpp_buffer); cpp_buffer *new = xobnew (pfile->buffer_ob, cpp_buffer);
/* Clears, amongst other things, if_stack and mi_cmacro. */ if (type == BUF_FAKE)
memset (new, 0, sizeof (cpp_buffer)); {
/* A copy of the current buffer, just with a new name and type. */
switch (type) memcpy (new, pfile->buffer, sizeof (cpp_buffer));
new->type = BUF_FAKE;
}
else
{ {
case BUF_FILE: new->nominal_fname = filename; break; if (type == BUF_BUILTIN)
case BUF_BUILTIN: new->nominal_fname = _("<builtin>"); break; filename = _("<builtin>");
case BUF_CL_OPTION: new->nominal_fname = _("<command line>"); break; else if (type == BUF_CL_OPTION)
case BUF_PRAGMA: new->nominal_fname = _("<_Pragma>"); break; filename = _("<command line>");
else if (type == BUF_PRAGMA)
filename = "<_Pragma>";
/* Clears, amongst other things, if_stack and mi_cmacro. */
memset (new, 0, sizeof (cpp_buffer));
new->line_base = new->buf = new->cur = buffer;
new->rlimit = buffer + len;
/* No read ahead or extra char initially. */
new->read_ahead = EOF;
new->extra_char = EOF;
/* Preprocessed files, builtins, _Pragma and command line
options don't do trigraph and escaped newline processing. */
new->from_stage3 = type != BUF_FILE || CPP_OPTION (pfile, preprocessed);
pfile->lexer_pos.output_line = 1;
} }
new->nominal_fname = filename;
new->type = type; new->type = type;
new->line_base = new->buf = new->cur = buffer;
new->rlimit = buffer + len;
new->prev = pfile->buffer; new->prev = pfile->buffer;
new->pfile = pfile; new->pfile = pfile;
new->include_stack_listed = 0;
/* No read ahead or extra char initially. */
new->read_ahead = EOF;
new->extra_char = EOF;
/* Preprocessed files, builtins, _Pragma and command line options
don't do trigraph and escaped newline processing. */
new->from_stage3 = type != BUF_FILE || CPP_OPTION (pfile, preprocessed);
pfile->state.next_bol = 1; pfile->state.next_bol = 1;
pfile->buffer_stack_depth++; pfile->buffer_stack_depth++;
pfile->lexer_pos.output_line = 1;
pfile->buffer = new; pfile->buffer = new;
if (type == BUF_FILE || type == BUF_FAKE)
{
const char *filename = 0;
unsigned int lineno = 0;
if (new->prev)
{
filename = new->prev->nominal_fname;
lineno = new->prev->lineno;
}
new->lineno = 0;
do_file_change (pfile, FC_ENTER, filename, lineno);
}
new->lineno = 1;
return new; return new;
} }
...@@ -1734,31 +1795,44 @@ cpp_buffer * ...@@ -1734,31 +1795,44 @@ cpp_buffer *
cpp_pop_buffer (pfile) cpp_pop_buffer (pfile)
cpp_reader *pfile; cpp_reader *pfile;
{ {
cpp_buffer *buffer = pfile->buffer; cpp_buffer *buffer;
const char *filename = buffer->nominal_fname; struct if_stack *ifs;
unsigned int lineno = buffer->lineno; int in_do_line = pfile->directive == &dtable[T_LINE];
struct if_stack *ifs = buffer->if_stack;
int file_buffer_p = buffer->type == BUF_FILE;
/* Walk back up the conditional stack till we reach its level at do
entry to this file, issuing error messages. */ {
for (ifs = buffer->if_stack; ifs; ifs = ifs->next) buffer = pfile->buffer;
cpp_error_with_line (pfile, ifs->pos.line, ifs->pos.col, /* Walk back up the conditional stack till we reach its level at
"unterminated #%s", dtable[ifs->type].name); entry to this file, issuing error messages. */
for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
cpp_error_with_line (pfile, ifs->pos.line, ifs->pos.col,
"unterminated #%s", dtable[ifs->type].name);
if (buffer->type == BUF_FAKE)
{
if (!in_do_line)
cpp_warning (pfile, "file \"%s\" entered but not left",
buffer->nominal_fname);
if (file_buffer_p) buffer->prev->cur = buffer->cur;
_cpp_pop_file_buffer (pfile, buffer); }
else if (buffer->type == BUF_FILE)
_cpp_pop_file_buffer (pfile, buffer);
pfile->buffer = buffer->prev; pfile->buffer = buffer->prev;
obstack_free (pfile->buffer_ob, buffer); pfile->buffer_stack_depth--;
pfile->buffer_stack_depth--;
if (pfile->buffer && file_buffer_p) if ((buffer->type == BUF_FILE || buffer->type == BUF_FAKE)
{ && pfile->buffer)
_cpp_do_file_change (pfile, FC_LEAVE, filename, lineno); {
pfile->buffer->include_stack_listed = 0; do_file_change (pfile, FC_LEAVE, buffer->nominal_fname,
buffer->lineno);
pfile->buffer->include_stack_listed = 0;
}
} }
while (pfile->buffer && pfile->buffer->type == BUF_FAKE && !in_do_line);
obstack_free (pfile->buffer_ob, buffer);
return pfile->buffer; return pfile->buffer;
} }
......
...@@ -633,9 +633,10 @@ struct cpp_reader ...@@ -633,9 +633,10 @@ struct cpp_reader
/* Name under which this program was invoked. */ /* Name under which this program was invoked. */
extern const char *progname; extern const char *progname;
/* Where does this buffer come from? A file, a builtin macro, a /* Where does this buffer come from? A faked include, a source file,
command-line option, or a _Pragma operator. */ a builtin macro, a command-line option, or a _Pragma operator. */
enum cpp_buffer_type {BUF_FILE, BUF_BUILTIN, BUF_CL_OPTION, BUF_PRAGMA}; enum cpp_buffer_type {BUF_FAKE, BUF_FILE, BUF_BUILTIN,
BUF_CL_OPTION, BUF_PRAGMA};
/* The structure of a node in the hash table. The hash table has /* The structure of a node in the hash table. The hash table has
entries for all identifiers: either macros defined by #define entries for all identifiers: either macros defined by #define
......
...@@ -263,7 +263,10 @@ maybe_print_line (line) ...@@ -263,7 +263,10 @@ maybe_print_line (line)
if (print.no_line_dirs) if (print.no_line_dirs)
return; return;
if (line >= print.lineno && line < print.lineno + 8) /* 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.lineno)
{ {
...@@ -358,7 +361,6 @@ cb_change_file (pfile, fc) ...@@ -358,7 +361,6 @@ cb_change_file (pfile, fc)
if (fc->reason == FC_ENTER && fc->from.filename) if (fc->reason == FC_ENTER && fc->from.filename)
maybe_print_line (fc->from.lineno); maybe_print_line (fc->from.lineno);
print.lineno = fc->to.lineno;
print.last_fname = fc->to.filename; print.last_fname = fc->to.filename;
if (fc->externc) if (fc->externc)
print.syshdr_flags = " 3 4"; print.syshdr_flags = " 3 4";
...@@ -367,14 +369,18 @@ cb_change_file (pfile, fc) ...@@ -367,14 +369,18 @@ cb_change_file (pfile, fc)
else else
print.syshdr_flags = ""; print.syshdr_flags = "";
switch (fc->reason) if (print.lineno)
{ {
case FC_ENTER : flags = fc->from.filename ? " 1": ""; break; print.lineno = fc->to.lineno;
case FC_LEAVE : flags = " 2"; break; switch (fc->reason)
case FC_RENAME: flags = ""; break; {
} case FC_ENTER : flags = " 1"; break;
case FC_LEAVE : flags = " 2"; break;
case FC_RENAME: flags = ""; break;
}
print_line (flags); print_line (flags);
}
} }
static void static void
......
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