+2001-09-14 Neil Booth <neil@daikokuya.demon.co.uk>
+
+ * cpperror.c (print_location): Take line and column, for
+ default positioning use the previously lexed token.
+ (_cpp_begin_message): Take line and column.
+ (cpp_ice, cpp_fatal, cpp_error, cpp_error_with_line, cpp_warning,
+ cpp_warning_with_line, cpp_pedwarn, cpp_pedwarn_with_line): Update.
+ * cpphash.h (_cpp_begin_message): Update prototype.
+ * cppinit.c (push_include): Don't set output line.
+ * cpplex.c (_cpp_lex_token): Callback for start of new output lines.
+ * cpplib.c (do_diagnostic, _cpp_pop_buffer): Update.
+ (do_pragma): Kludge for front ends. Don't expand macros at all.
+ * cpplib.h (cpp_lookahead, cpp_token_with_pos, cpp_get_line): Remove.
+ (struct cpp_token): Remove output_line.
+ (struct cpp_callbacks): New member line_change.
+ * cppmacro.c (builtin_macro, paste_all_tokens, replace_args,
+ cpp_get_token): Preserve BOL flag.
+ (cpp_get_line): Remove.
+ (_cpp_backup_tokens): Remove useless abort().
+ * cppmain.c (cb_line_change): New.
+ (scan_translation_unit): Don't worry about starting new lines here.
+ * scan-decls.c (scan_decls): Update.
+ * c-lex.c (c_lex, init_c_lex): Update.
+ (cb_line_change, src_lineno): New.
+
Fri Sep 14 13:54:50 EDT 2001 John Wehle (john@feith.com)
* tree.c (append_random_chars): Generate the random
/* The current line map. */
static const struct line_map *map;
+/* The line used to refresh the lineno global variable after each token. */
+static unsigned int src_lineno;
+
/* We may keep statistics about how long which files took to compile. */
static int header_time, body_time;
static splay_tree file_info_tree;
static tree lex_charconst PARAMS ((const cpp_token *));
static void update_header_times PARAMS ((const char *));
static int dump_one_header PARAMS ((splay_tree_node, void *));
+static void cb_line_change PARAMS ((cpp_reader *, const cpp_token *, int));
static void cb_ident PARAMS ((cpp_reader *, unsigned int,
const cpp_string *));
static void cb_file_change PARAMS ((cpp_reader *, const struct line_map *));
cb = cpp_get_callbacks (parse_in);
+ cb->line_change = cb_line_change;
cb->ident = cb_ident;
cb->file_change = cb_file_change;
cb->def_pragma = cb_def_pragma;
#endif
}
+/* Called at the start of every non-empty line. TOKEN is the first
+ lexed token on the line. Used for diagnostic line numbers. */
+static void
+cb_line_change (pfile, token, parsing_args)
+ cpp_reader *pfile ATTRIBUTE_UNUSED;
+ const cpp_token *token;
+ int parsing_args ATTRIBUTE_UNUSED;
+{
+ src_lineno = SOURCE_LINE (map, token->line);
+}
+
static void
cb_file_change (pfile, new_map)
cpp_reader *pfile ATTRIBUTE_UNUSED;
/* The C++ front end does horrible things with the current line
number. To ensure an accurate line number, we must reset it
every time we return a token. */
- lineno = SOURCE_LINE (map, cpp_get_line (parse_in)->line);
+ lineno = src_lineno;
*value = NULL_TREE;
type = tok.type;
#include "cpphash.h"
#include "intl.h"
-static void print_location PARAMS ((cpp_reader *,
- const cpp_lexer_pos *));
+static void print_location PARAMS ((cpp_reader *, unsigned int, unsigned int));
/* Don't remove the blank before do, as otherwise the exgettext
script will mistake this as a function definition */
do { vfprintf (stderr, _(msgid), ap); putc ('\n', stderr); } while (0)
static void
-print_location (pfile, pos)
+print_location (pfile, line, col)
cpp_reader *pfile;
- const cpp_lexer_pos *pos;
+ unsigned int line, col;
{
cpp_buffer *buffer = pfile->buffer;
fprintf (stderr, "%s: ", progname);
else
{
- unsigned int line, col;
const struct line_map *map;
- if (pos == 0)
- pos = cpp_get_line (pfile);
- map = lookup_line (&pfile->line_maps, pos->line);
+ if (line == 0)
+ {
+ line = pfile->cur_token[-1].line;
+ col = pfile->cur_token[-1].col;
+ }
+ map = lookup_line (&pfile->line_maps, line);
print_containing_files (&pfile->line_maps, map);
- line = SOURCE_LINE (map, pos->line);
- col = pos->col;
+ line = SOURCE_LINE (map, line);
if (col == 0)
col = 1;
}
/* Set up for an error message: print the file and line, bump the error
- counter, etc.
- If it returns 0, this error has been suppressed. */
+ counter, etc. LINE is the logical line number; zero means to print
+ at the location of the previously lexed token, which tends to be the
+ correct place by default. Returns 0 if the error has been suppressed. */
int
-_cpp_begin_message (pfile, code, pos)
+_cpp_begin_message (pfile, code, line, column)
cpp_reader *pfile;
enum error_type code;
- const cpp_lexer_pos *pos;
+ unsigned int line, column;
{
int is_warning = 0;
break;
}
- print_location (pfile, pos);
+ print_location (pfile, line, column);
if (is_warning)
fputs (_("warning: "), stderr);
VA_FIXEDARG (ap, cpp_reader *, pfile);
VA_FIXEDARG (ap, const char *, msgid);
- if (_cpp_begin_message (pfile, ICE, 0))
+ if (_cpp_begin_message (pfile, ICE, 0, 0))
v_message (msgid, ap);
VA_CLOSE (ap);
VA_FIXEDARG (ap, cpp_reader *, pfile);
VA_FIXEDARG (ap, const char *, msgid);
- if (_cpp_begin_message (pfile, FATAL, 0))
+ if (_cpp_begin_message (pfile, FATAL, 0, 0))
v_message (msgid, ap);
VA_CLOSE (ap);
VA_FIXEDARG (ap, cpp_reader *, pfile);
VA_FIXEDARG (ap, const char *, msgid);
- if (_cpp_begin_message (pfile, ERROR, 0))
+ if (_cpp_begin_message (pfile, ERROR, 0, 0))
v_message (msgid, ap);
VA_CLOSE (ap);
cpp_error_with_line VPARAMS ((cpp_reader *pfile, int line, int column,
const char *msgid, ...))
{
- cpp_lexer_pos pos;
-
VA_OPEN (ap, msgid);
VA_FIXEDARG (ap, cpp_reader *, pfile);
VA_FIXEDARG (ap, int, line);
VA_FIXEDARG (ap, int, column);
VA_FIXEDARG (ap, const char *, msgid);
- pos.line = line;
- pos.col = column;
- if (_cpp_begin_message (pfile, ERROR, &pos))
+ if (_cpp_begin_message (pfile, ERROR, line, column))
v_message (msgid, ap);
VA_CLOSE (ap);
VA_FIXEDARG (ap, cpp_reader *, pfile);
VA_FIXEDARG (ap, const char *, msgid);
- if (_cpp_begin_message (pfile, WARNING, 0))
+ if (_cpp_begin_message (pfile, WARNING, 0, 0))
v_message (msgid, ap);
VA_CLOSE (ap);
cpp_warning_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
const char *msgid, ...))
{
- cpp_lexer_pos pos;
-
VA_OPEN (ap, msgid);
VA_FIXEDARG (ap, cpp_reader *, pfile);
VA_FIXEDARG (ap, int, line);
VA_FIXEDARG (ap, int, column);
VA_FIXEDARG (ap, const char *, msgid);
- pos.line = line;
- pos.col = column;
- if (_cpp_begin_message (pfile, WARNING, &pos))
+ if (_cpp_begin_message (pfile, WARNING, line, column))
v_message (msgid, ap);
VA_CLOSE (ap);
VA_FIXEDARG (ap, cpp_reader *, pfile);
VA_FIXEDARG (ap, const char *, msgid);
- if (_cpp_begin_message (pfile, PEDWARN, 0))
+ if (_cpp_begin_message (pfile, PEDWARN, 0, 0))
v_message (msgid, ap);
VA_CLOSE (ap);
cpp_pedwarn_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
const char *msgid, ...))
{
- cpp_lexer_pos pos;
-
VA_OPEN (ap, msgid);
VA_FIXEDARG (ap, cpp_reader *, pfile);
VA_FIXEDARG (ap, int, line);
VA_FIXEDARG (ap, int, column);
VA_FIXEDARG (ap, const char *, msgid);
- pos.line = line;
- pos.col = column;
- if (_cpp_begin_message (pfile, PEDWARN, &pos))
+ if (_cpp_begin_message (pfile, PEDWARN, line, column))
v_message (msgid, ap);
VA_CLOSE (ap);
/* In cpperror.c */
enum error_type { WARNING = 0, WARNING_SYSHDR, PEDWARN, ERROR, FATAL, ICE };
extern int _cpp_begin_message PARAMS ((cpp_reader *, enum error_type,
- const cpp_lexer_pos *));
+ unsigned int, unsigned int));
/* In cppmacro.c */
extern void _cpp_free_definition PARAMS ((cpp_hashnode *));
header.val.str.text = (const unsigned char *) p->arg;
header.val.str.len = strlen (p->arg);
/* Make the command line directive take up a line. */
- pfile->lexer_pos.line = pfile->lexer_pos.output_line = ++pfile->line;
+ pfile->lexer_pos.line = ++pfile->line;
return _cpp_execute_include (pfile, &header, IT_CMDLINE);
}
if (result->flags & BOL)
{
- pfile->lexer_pos.output_line = result->line;
/* Is this a directive. If _cpp_handle_directive returns
false, it is an assembler #. */
if (result->type == CPP_HASH
&& !pfile->state.parsing_args
&& _cpp_handle_directive (pfile, result->flags & PREV_WHITE))
continue;
+ if (pfile->cb.line_change && !pfile->state.skipping)
+ (*pfile->cb.line_change)(pfile, result, pfile->state.parsing_args);
}
/* We don't skip tokens in directives. */
enum error_type code;
int print_dir;
{
- if (_cpp_begin_message (pfile, code, 0))
+ if (_cpp_begin_message (pfile, code, 0, 0))
{
if (print_dir)
fprintf (stderr, "#%s ", pfile->directive->name);
}
}
- pfile->state.prevent_expansion--;
+ /* FIXME. This is an awful kludge to get the front ends to update
+ their notion of line number for diagnostic purposes. The line
+ number should be passed to the handler and they should do it
+ themselves. Stand-alone CPP must ignore us, otherwise it will
+ prefix the directive with spaces, hence the 1. Ugh. */
+ if (pfile->cb.line_change)
+ (*pfile->cb.line_change)(pfile, &tok, 1);
+
if (handler)
(*handler) (pfile);
else if (pfile->cb.def_pragma)
_cpp_backup_tokens (pfile, count);
(*pfile->cb.def_pragma) (pfile, pfile->directive_line);
}
+ pfile->state.prevent_expansion--;
}
static void
cpp_error_with_line (pfile, ifs->pos.line, ifs->pos.col,
"unterminated #%s", dtable[ifs->type].name);
- /* The output line can fall out of sync if we missed the final
- newline from the previous buffer, for example because of an
- unterminated comment. Similarly, skipping needs to be cleared in
- case of a missing #endif. */
- pfile->lexer_pos.output_line = pfile->line;
+ /* In case of a missing #endif. */
pfile->state.skipping = 0;
/* Update the reader's buffer before _cpp_do_file_change. */
typedef struct cpp_hashnode cpp_hashnode;
typedef struct cpp_macro cpp_macro;
typedef struct cpp_lexer_pos cpp_lexer_pos;
-typedef struct cpp_lookahead cpp_lookahead;
typedef struct cpp_callbacks cpp_callbacks;
struct answer;
struct cpp_lexer_pos
{
unsigned int line;
- unsigned int output_line;
unsigned short col;
};
-typedef struct cpp_token_with_pos cpp_token_with_pos;
-struct cpp_token_with_pos
-{
- cpp_token token;
- cpp_lexer_pos pos;
-};
-
-/* Token lookahead. */
-struct cpp_lookahead
-{
- struct cpp_lookahead *next;
- cpp_token_with_pos *tokens;
- cpp_lexer_pos pos;
- unsigned int cur, count, cap;
-};
-
/* A standalone character. We may want to make it unsigned for the
same reason we use unsigned char - to avoid signedness issues. */
typedef int cppchar_t;
/* Call backs. */
struct cpp_callbacks
{
- void (*file_change) PARAMS ((cpp_reader *, const struct line_map *));
- void (*include) PARAMS ((cpp_reader *, unsigned int,
- const unsigned char *, const cpp_token *));
- void (*define) PARAMS ((cpp_reader *, unsigned int, cpp_hashnode *));
- void (*undef) PARAMS ((cpp_reader *, unsigned int, cpp_hashnode *));
- void (*ident) PARAMS ((cpp_reader *, unsigned int, const cpp_string *));
- void (*def_pragma) PARAMS ((cpp_reader *, unsigned int));
+ /* Called when a new line of preprocessed output is started. */
+ void (*line_change) PARAMS ((cpp_reader *, const cpp_token *, int));
+ void (*file_change) PARAMS ((cpp_reader *, const struct line_map *));
+ void (*include) PARAMS ((cpp_reader *, unsigned int,
+ const unsigned char *, const cpp_token *));
+ void (*define) PARAMS ((cpp_reader *, unsigned int, cpp_hashnode *));
+ void (*undef) PARAMS ((cpp_reader *, unsigned int, cpp_hashnode *));
+ void (*ident) PARAMS ((cpp_reader *, unsigned int, const cpp_string *));
+ void (*def_pragma) PARAMS ((cpp_reader *, unsigned int));
};
#define CPP_FATAL_LIMIT 1000
extern enum cpp_ttype cpp_can_paste PARAMS ((cpp_reader *, const cpp_token *,
const cpp_token *, int *));
extern void cpp_get_token PARAMS ((cpp_reader *, cpp_token *));
-extern const cpp_lexer_pos *cpp_get_line PARAMS ((cpp_reader *));
extern const unsigned char *cpp_macro_definition PARAMS ((cpp_reader *,
const cpp_hashnode *));
extern void _cpp_backup_tokens PARAMS ((cpp_reader *, unsigned int));
cpp_reader *pfile;
cpp_token *token;
{
- unsigned char flags = ((token->flags & PREV_WHITE) | AVOID_LPASTE);
+ unsigned char flags = ((token->flags & (PREV_WHITE | BOL)) | AVOID_LPASTE);
cpp_hashnode *node = token->val.node;
switch (node->value.builtin)
token->flags = flags;
}
-/* Used by cpperror.c to obtain the correct line and column to report
- in a diagnostic. */
-const cpp_lexer_pos *
-cpp_get_line (pfile)
- cpp_reader *pfile;
-{
- if (pfile->context->prev == NULL)
- {
- pfile->lexer_pos.line = pfile->cur_token[-1].line;
- pfile->lexer_pos.col = pfile->cur_token[-1].col;
- }
-
- return &pfile->lexer_pos;
-}
-
static void
lock_pools (pfile)
cpp_reader *pfile;
/* The pasted token has the PREV_WHITE flag of the LHS, is no longer
PASTE_LEFT, and is subject to macro expansion. */
- lhs->flags &= ~(PREV_WHITE | PASTE_LEFT | NO_EXPAND);
- lhs->flags |= orig_flags & (PREV_WHITE | AVOID_LPASTE);
+ lhs->flags &= ~(PREV_WHITE | BOL | PASTE_LEFT | NO_EXPAND);
+ lhs->flags |= orig_flags & (PREV_WHITE | BOL | AVOID_LPASTE);
}
/* Reads the unexpanded tokens of a macro argument into ARG. VAR_ARGS
memcpy (dest, from, count * sizeof (cpp_token));
/* The first token gets PREV_WHITE of the CPP_MACRO_ARG. */
- dest->flags &= ~PREV_WHITE;
- dest->flags |= src->flags & PREV_WHITE;
+ dest->flags &= ~(PREV_WHITE | BOL);
+ dest->flags |= src->flags & (PREV_WHITE | BOL);
dest->flags |= AVOID_LPASTE;
/* The last token gets the PASTE_LEFT of the CPP_MACRO_ARG. */
else if (enter_macro_context (pfile, node))
{
/* Pass AVOID_LPASTE and our PREV_WHITE to next token. */
- pfile->buffer->saved_flags = ((token->flags & PREV_WHITE)
+ pfile->buffer->saved_flags = ((token->flags & (PREV_WHITE | BOL))
| AVOID_LPASTE);
continue;
}
pfile->cur_token--;
if (pfile->cur_token == pfile->cur_run->base)
{
- if (pfile->cur_run == NULL)
- abort ();
pfile->cur_run = pfile->cur_run->prev;
pfile->cur_token = pfile->cur_run->limit;
}
/* 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,
{
cpp_callbacks *cb = cpp_get_callbacks (pfile);
+ cb->line_change = cb_line_change;
if (! options->no_output)
{
cb->ident = cb_ident;
scan_translation_unit (pfile)
cpp_reader *pfile;
{
- unsigned int index, line;
+ unsigned int index;
cpp_token tokens[2], *token;
for (index = 0;; index = 1 - index)
if (token->type == CPP_EOF)
break;
- line = cpp_get_line (pfile)->output_line;
- if (print.line != line)
- {
- unsigned int col = cpp_get_line (pfile)->col;
-
- /* Supply enough whitespace to put this token in its original
- column. Don't bother trying to reconstruct tabs; we can't
- get it right in general, and nothing ought to care. (Yes,
- some things do care; the fault lies with them.) */
- maybe_print_line (print.map, line);
- if (col > 1)
- {
- if (token->flags & PREV_WHITE)
- col--;
- while (--col)
- putc (' ', print.outf);
- }
- }
- else if ((token->flags & (PREV_WHITE | AVOID_LPASTE))
- == AVOID_LPASTE
- && cpp_avoid_paste (pfile, &tokens[1 - index], token))
+ if ((token->flags & (PREV_WHITE | AVOID_LPASTE | BOL)) == AVOID_LPASTE
+ && cpp_avoid_paste (pfile, &tokens[1 - index], token))
token->flags |= PREV_WHITE;
/* Special case '# <directive name>': insert a space between
the # and the token. This will prevent it from being
token->flags |= PREV_WHITE;
cpp_output_token (token, print.outf);
- print.printed = 1;
if (token->type == CPP_STRING || token->type == CPP_WSTRING
|| token->type == CPP_COMMENT)
check_multiline_token (&token->val.str);
}
}
-/* Callbacks. */
+/* Called when a line of output is started. TOKEN is the first token
+ of the line, and maybe 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;
+
+ /* 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)
|| token.type == CPP_ELLIPSIS)
have_arg_list = 1;
}
- recognized_function (&prev_id,
- cpp_get_line (pfile)->line,
+ recognized_function (&prev_id, token->line,
(saw_inline ? 'I'
: in_extern_C_brace || current_extern_C
? 'F' : 'f'), have_arg_list);