cpplib: More line-map related cleanup
Neil Booth
neil@daikokuya.demon.co.uk
Thu Aug 16 12:15:00 GMT 2001
This patch continues the cleanup that linemaps have made possible.
1) We no loner need a file name in the buffer structure; after all it's
more properly a property of the current line map.
2) cpp_pedwarn_with_file_and_line can die; there is no need for the
file name any more.
3) print_location can be simplified considerably.
It actually breaks something minor: we lose diagnostics that refer to
the command line macros and macro builtin locations as <cmdline> and
<builtin>. Fixing this is surprisingly tricky with the current way we
handle command line macros / -imacros / -include: i.e. after pushing
the main file. We basically "lose" the pseudo-lines they might be
accounted for on (I think we're also losing all line lines in
-include-d files too.)
I tried to fix it a couple of ways, but got bogged down so I thought
I'd get this out of the way first; I doubt it causes a regression.
There's a couple of ways I can fix it, both with their nasties, I'll
explain those and see what others prefer in a separate mail.
When this has bootstrapped and made check, I'll commit it. Mainline
is currently broken.
What do you think of making the cpp_error etc. diagnostic functions
not exported, Zack? I can't see much point in exporting them. Our
exported interface is a bit large IMO. And what happened to your
variadic function cleanup?
Neil.
* cpperror.c (print_location): Don't take a file name; use the
line map instead.
(_cpp_begin_message): Similarly.
(cpp_ice, cpp_fatal, cpp_error, cpp_error_with_line, cpp_warning,
cpp_warning_with_line, cpp_pedwarn, cpp_pedwarn_with_line): Update.
(cpp_pedwarn_with_file_and_line): Remove.
* cppfiles.c (stack_include_file): Update; set filename to stdin
here when appropriate.
* cpphash.h (struct cpp_buffer): Remove nominal_fname.
(_cpp_begin_message): Don't take a file name.
* cpplex.c: Add comment.
* cpplib.c: Don't include intl.h.
(run_directive, do_diagnostic): Update.
(do_line): Update to not use nominal_fname.
(cpp_push_buffer): Don't take a filename.
* cpplib.h (struct ht): Remove.
(cpp_push_buffer): Don't take a filename.
(cpp_pedwarn_with_file_and_line): Remove.
* cppmacro.c (struct cpp_macro): Remove file.
(builtin_macro): Update.
(_cpp_create_definition): Update.
* cppmain.c: Correct comment.
* fix-header.c (read_scan_file): Update.
Index: cpperror.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cpperror.c,v
retrieving revision 1.51
diff -u -p -r1.51 cpperror.c
--- cpperror.c 2001/08/11 07:33:38 1.51
+++ cpperror.c 2001/08/16 18:43:09
@@ -30,7 +30,6 @@ Foundation, 59 Temple Place - Suite 330,
#include "intl.h"
static void print_location PARAMS ((cpp_reader *,
- const char *,
const cpp_lexer_pos *));
/* Don't remove the blank before do, as otherwise the exgettext
@@ -39,9 +38,8 @@ static void print_location PARAMS ((cpp
do { vfprintf (stderr, _(msgid), ap); putc ('\n', stderr); } while (0)
static void
-print_location (pfile, filename, pos)
+print_location (pfile, pos)
cpp_reader *pfile;
- const char *filename;
const cpp_lexer_pos *pos;
{
cpp_buffer *buffer = pfile->buffer;
@@ -50,47 +48,28 @@ print_location (pfile, filename, pos)
fprintf (stderr, "%s: ", progname);
else
{
- unsigned int line, col = 0;
- enum cpp_buffer_type type = buffer->type;
+ unsigned int line, col;
+ const struct line_map *map;
- /* For _Pragma buffers, we want to print the location as
- "foo.c:5:8: _Pragma:", where foo.c is the containing buffer.
- For diagnostics relating to command line options, we want to
- print "<command line>:" with no line number. */
- if (type == BUF_CL_OPTION || type == BUF_BUILTIN)
- line = 0;
- else
- {
- const struct line_map *map;
-
- if (type == BUF_PRAGMA)
- buffer = buffer->prev;
-
- if (pos == 0)
- pos = cpp_get_line (pfile);
- map = lookup_line (&pfile->line_maps, pos->line);
- line = SOURCE_LINE (map, pos->line);
- if (filename == 0)
- filename = map->to_file;
-
- col = pos->col;
- if (col == 0)
- col = 1;
+ if (pos == 0)
+ pos = cpp_get_line (pfile);
+ map = lookup_line (&pfile->line_maps, pos->line);
+
+ print_containing_files (&pfile->line_maps, map);
+
+ line = SOURCE_LINE (map, pos->line);
+ col = pos->col;
+ if (col == 0)
+ col = 1;
- print_containing_files (&pfile->line_maps, map);
- }
-
- if (filename == 0)
- filename = buffer->nominal_fname;
-
if (line == 0)
- fprintf (stderr, "%s:", filename);
+ fprintf (stderr, "%s:", map->to_file);
else if (CPP_OPTION (pfile, show_column) == 0)
- fprintf (stderr, "%s:%u:", filename, line);
+ fprintf (stderr, "%s:%u:", map->to_file, line);
else
- fprintf (stderr, "%s:%u:%u:", filename, line, col);
+ fprintf (stderr, "%s:%u:%u:", map->to_file, line, col);
- if (type == BUF_PRAGMA)
+ if (buffer->type == BUF_PRAGMA)
fprintf (stderr, "_Pragma:");
fputc (' ', stderr);
}
@@ -101,10 +80,9 @@ print_location (pfile, filename, pos)
If it returns 0, this error has been suppressed. */
int
-_cpp_begin_message (pfile, code, file, pos)
+_cpp_begin_message (pfile, code, pos)
cpp_reader *pfile;
enum error_type code;
- const char *file;
const cpp_lexer_pos *pos;
{
int is_warning = 0;
@@ -149,7 +127,7 @@ _cpp_begin_message (pfile, code, file, p
break;
}
- print_location (pfile, file, pos);
+ print_location (pfile, pos);
if (is_warning)
fputs (_("warning: "), stderr);
@@ -177,7 +155,7 @@ cpp_ice VPARAMS ((cpp_reader *pfile, con
msgid = va_arg (ap, const char *);
#endif
- if (_cpp_begin_message (pfile, ICE, NULL, 0))
+ if (_cpp_begin_message (pfile, ICE, 0))
v_message (msgid, ap);
va_end(ap);
}
@@ -204,7 +182,7 @@ cpp_fatal VPARAMS ((cpp_reader *pfile, c
msgid = va_arg (ap, const char *);
#endif
- if (_cpp_begin_message (pfile, FATAL, NULL, 0))
+ if (_cpp_begin_message (pfile, FATAL, 0))
v_message (msgid, ap);
va_end(ap);
}
@@ -225,7 +203,7 @@ cpp_error VPARAMS ((cpp_reader * pfile,
msgid = va_arg (ap, const char *);
#endif
- if (_cpp_begin_message (pfile, ERROR, NULL, 0))
+ if (_cpp_begin_message (pfile, ERROR, 0))
v_message (msgid, ap);
va_end(ap);
}
@@ -254,7 +232,7 @@ cpp_error_with_line VPARAMS ((cpp_reader
pos.line = line;
pos.col = column;
- if (_cpp_begin_message (pfile, ERROR, NULL, &pos))
+ if (_cpp_begin_message (pfile, ERROR, &pos))
v_message (msgid, ap);
va_end(ap);
}
@@ -284,7 +262,7 @@ cpp_warning VPARAMS ((cpp_reader * pfile
msgid = va_arg (ap, const char *);
#endif
- if (_cpp_begin_message (pfile, WARNING, NULL, 0))
+ if (_cpp_begin_message (pfile, WARNING, 0))
v_message (msgid, ap);
va_end(ap);
}
@@ -313,7 +291,7 @@ cpp_warning_with_line VPARAMS ((cpp_read
pos.line = line;
pos.col = column;
- if (_cpp_begin_message (pfile, WARNING, NULL, &pos))
+ if (_cpp_begin_message (pfile, WARNING, &pos))
v_message (msgid, ap);
va_end(ap);
}
@@ -334,7 +312,7 @@ cpp_pedwarn VPARAMS ((cpp_reader * pfile
msgid = va_arg (ap, const char *);
#endif
- if (_cpp_begin_message (pfile, PEDWARN, NULL, 0))
+ if (_cpp_begin_message (pfile, PEDWARN, 0))
v_message (msgid, ap);
va_end(ap);
}
@@ -363,42 +341,7 @@ cpp_pedwarn_with_line VPARAMS ((cpp_read
pos.line = line;
pos.col = column;
- if (_cpp_begin_message (pfile, PEDWARN, NULL, &pos))
- v_message (msgid, ap);
- va_end(ap);
-}
-
-/* Report a warning (or an error if pedantic_errors)
- giving specified file name and line number, not current. */
-
-void
-cpp_pedwarn_with_file_and_line VPARAMS ((cpp_reader *pfile,
- const char *file, int line, int col,
- const char *msgid, ...))
-{
-#ifndef ANSI_PROTOTYPES
- cpp_reader *pfile;
- const char *file;
- int line;
- int col;
- const char *msgid;
-#endif
- va_list ap;
- cpp_lexer_pos pos;
-
- VA_START (ap, msgid);
-
-#ifndef ANSI_PROTOTYPES
- pfile = va_arg (ap, cpp_reader *);
- file = va_arg (ap, const char *);
- line = va_arg (ap, int);
- col = va_arg (ap, int);
- msgid = va_arg (ap, const char *);
-#endif
-
- pos.line = line;
- pos.col = col;
- if (_cpp_begin_message (pfile, PEDWARN, file, &pos))
+ if (_cpp_begin_message (pfile, PEDWARN, &pos))
v_message (msgid, ap);
va_end(ap);
}
Index: cppfiles.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cppfiles.c,v
retrieving revision 1.124
diff -u -p -r1.124 cppfiles.c
--- cppfiles.c 2001/08/11 07:33:38 1.124
+++ cppfiles.c 2001/08/16 18:43:11
@@ -287,6 +287,7 @@ stack_include_file (pfile, inc)
size_t len = 0;
cpp_buffer *fp;
int sysp, deps_sysp;
+ const char *filename;
/* We'll try removing deps_sysp after the release of 3.0. */
deps_sysp = pfile->system_include_depth != 0;
@@ -326,7 +327,7 @@ stack_include_file (pfile, inc)
}
/* Push a buffer. */
- fp = cpp_push_buffer (pfile, inc->buffer, len, BUF_FILE, inc->name, 0);
+ fp = cpp_push_buffer (pfile, inc->buffer, len, BUF_FILE, 0);
fp->inc = inc;
fp->inc->refcnt++;
@@ -336,7 +337,10 @@ stack_include_file (pfile, inc)
pfile->include_depth++;
/* Generate the call back. */
- _cpp_do_file_change (pfile, LC_ENTER, fp->nominal_fname, 1, sysp);
+ filename = inc->name;
+ if (*filename == '\0')
+ filename = _("<stdin>");
+ _cpp_do_file_change (pfile, LC_ENTER, filename, 1, sysp);
}
/* Read the file referenced by INC into the file cache.
Index: cpphash.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cpphash.h,v
retrieving revision 1.117
diff -u -p -r1.117 cpphash.h
--- cpphash.h 2001/08/11 12:37:17 1.117
+++ cpphash.h 2001/08/16 18:43:14
@@ -176,9 +176,6 @@ struct cpp_buffer
const unsigned char *buf; /* entire buffer */
- /* Filename specified with #line command. */
- const char *nominal_fname;
-
/* Pointer into the include table. Used for include_next and
to record control macros. */
struct include_file *inc;
@@ -375,7 +372,7 @@ extern unsigned char _cpp_trigraph_map[U
/* 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 char *, const cpp_lexer_pos *));
+ const cpp_lexer_pos *));
/* In cppmacro.c */
extern void _cpp_free_definition PARAMS ((cpp_hashnode *));
Index: cppinit.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cppinit.c,v
retrieving revision 1.170
diff -u -p -r1.170 cppinit.c
--- cppinit.c 2001/08/11 12:37:18 1.170
+++ cppinit.c 2001/08/16 18:43:21
@@ -939,6 +939,9 @@ cpp_start_read (pfile, fname)
if (!_cpp_read_file (pfile, fname))
return 0;
+ /* FIXME: we want to set up linemaps with _("<builtin>") and
+ _("<command line>") somewhere round here. Harder than it looks. */
+
/* If already preprocessed, don't install __LINE__, etc., and ignore
command line definitions and assertions. Handle -U's, -D's and
-A's in the order they were seen. */
Index: cpplex.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cpplex.c,v
retrieving revision 1.154
diff -u -p -r1.154 cpplex.c
--- cpplex.c 2001/08/11 12:37:18 1.154
+++ cpplex.c 2001/08/16 18:43:23
@@ -894,7 +894,7 @@ _cpp_lex_token (pfile, result)
in-progress directives and arguments have been taken care of.
Decrement the line to terminate an in-progress directive. */
if (pfile->state.in_directive)
- pfile->line--;
+ pfile->lexer_pos.output_line = pfile->line--;
else if (! pfile->state.parsing_args)
{
/* Non-empty files should end in a newline. Don't warn for
Index: cpplib.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cpplib.c,v
retrieving revision 1.267
diff -u -p -r1.267 cpplib.c
--- cpplib.c 2001/08/11 12:37:18 1.267
+++ cpplib.c 2001/08/16 18:43:38
@@ -24,7 +24,6 @@ Foundation, 59 Temple Place - Suite 330,
#include "cpplib.h"
#include "cpphash.h"
-#include "intl.h"
#include "obstack.h"
/* Chained list of answers to an assertion. */
@@ -403,7 +402,7 @@ run_directive (pfile, dir_no, type, buf,
{
cpp_buffer *buffer;
- buffer = cpp_push_buffer (pfile, (const U_CHAR *) buf, count, type, 0, 1);
+ buffer = cpp_push_buffer (pfile, (const U_CHAR *) buf, count, type, 1);
start_directive (pfile);
pfile->state.prevent_expansion++;
pfile->directive = &dtable[dir_no];
@@ -710,11 +709,11 @@ static void
do_line (pfile)
cpp_reader *pfile;
{
- cpp_buffer *buffer = pfile->buffer;
- enum lc_reason reason = LC_RENAME;
- unsigned long new_lineno;
- unsigned int cap, sysp = pfile->map->sysp;
cpp_token token;
+ const char *new_file = pfile->map->to_file;
+ unsigned long new_lineno;
+ unsigned int cap, new_sysp = pfile->map->sysp;
+ enum lc_reason reason = LC_RENAME;
/* C99 raised the minimum limit on #line numbers. */
cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
@@ -736,20 +735,20 @@ do_line (pfile)
cpp_get_token (pfile, &token);
if (token.type == CPP_STRING)
{
- buffer->nominal_fname = (const char *) token.val.str.text;
+ new_file = (const char *) token.val.str.text;
/* Only accept flags for the # 55 form. */
if (pfile->state.line_extension)
{
int flag;
- sysp = 0;
+ new_sysp = 0;
flag = read_flag (pfile, 0);
if (flag == 1)
{
reason = LC_ENTER;
/* Fake an include for cpp_included (). */
- _cpp_fake_include (pfile, buffer->nominal_fname);
+ _cpp_fake_include (pfile, new_file);
flag = read_flag (pfile, flag);
}
else if (flag == 2)
@@ -759,10 +758,10 @@ do_line (pfile)
}
if (flag == 3)
{
- sysp = 1;
+ new_sysp = 1;
flag = read_flag (pfile, flag);
if (flag == 4)
- sysp = 2;
+ new_sysp = 2;
}
}
check_eol (pfile);
@@ -775,8 +774,7 @@ do_line (pfile)
}
end_directive (pfile, 1);
- _cpp_do_file_change (pfile, reason, (const char *) buffer->nominal_fname,
- new_lineno, sysp);
+ _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
}
/* Arrange the file_change callback. pfile->line has changed to
@@ -809,7 +807,7 @@ do_diagnostic (pfile, code, print_dir)
enum error_type code;
int print_dir;
{
- if (_cpp_begin_message (pfile, code, NULL, 0))
+ if (_cpp_begin_message (pfile, code, 0))
{
if (print_dir)
fprintf (stderr, "#%s ", pfile->directive->name);
@@ -1737,23 +1735,15 @@ cpp_set_callbacks (pfile, cb)
doesn't fail. It does not generate a file change call back; that
is the responsibility of the caller. */
cpp_buffer *
-cpp_push_buffer (pfile, buffer, len, type, filename, return_at_eof)
+cpp_push_buffer (pfile, buffer, len, type, return_at_eof)
cpp_reader *pfile;
const U_CHAR *buffer;
size_t len;
enum cpp_buffer_type type;
- const char *filename;
int return_at_eof;
{
cpp_buffer *new = xobnew (&pfile->buffer_ob, cpp_buffer);
- if (type == BUF_BUILTIN)
- filename = _("<builtin>");
- else if (type == BUF_CL_OPTION)
- 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));
@@ -1768,10 +1758,6 @@ cpp_push_buffer (pfile, buffer, len, typ
options don't do trigraph and escaped newline processing. */
new->from_stage3 = type != BUF_FILE || CPP_OPTION (pfile, preprocessed);
- if (*filename == '\0')
- new->nominal_fname = _("<stdin>");
- else
- new->nominal_fname = filename;
new->type = type;
new->prev = pfile->buffer;
new->pfile = pfile;
Index: cpplib.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cpplib.h,v
retrieving revision 1.185
diff -u -p -r1.185 cpplib.h
--- cpplib.h 2001/08/11 07:33:38 1.185
+++ cpplib.h 2001/08/16 18:43:38
@@ -47,7 +47,6 @@ typedef struct cpp_callbacks cpp_callbac
struct answer;
struct file_name_map_list;
-struct ht;
/* The first two groups, apart from '=', can appear in preprocessor
expressions. This allows a lookup table to be implemented in
@@ -542,8 +541,7 @@ extern void cpp_unassert PARAMS ((cpp_re
extern cpp_buffer *cpp_push_buffer PARAMS ((cpp_reader *,
const unsigned char *, size_t,
- enum cpp_buffer_type,
- const char *, int));
+ enum cpp_buffer_type, int));
extern int cpp_defined PARAMS ((cpp_reader *, const unsigned char *, int));
/* N.B. The error-message-printer prototypes have not been nicely
@@ -570,8 +568,6 @@ extern void cpp_warning_with_line PARAMS
ATTRIBUTE_PRINTF_4;
extern void cpp_pedwarn_with_line PARAMS ((cpp_reader *, int, int, const char *msgid, ...))
ATTRIBUTE_PRINTF_4;
-extern void cpp_pedwarn_with_file_and_line PARAMS ((cpp_reader *, const char *, int, int, const char *msgid, ...))
- ATTRIBUTE_PRINTF_5;
extern void cpp_error_from_errno PARAMS ((cpp_reader *, const char *));
extern void cpp_notice_from_errno PARAMS ((cpp_reader *, const char *));
Index: cppmacro.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cppmacro.c,v
retrieving revision 1.61
diff -u -p -r1.61 cppmacro.c
--- cppmacro.c 2001/08/11 22:20:18 1.61
+++ cppmacro.c 2001/08/16 18:43:38
@@ -33,7 +33,6 @@ struct cpp_macro
{
cpp_hashnode **params; /* Parameters, if any. */
cpp_token *expansion; /* First token of replacement list. */
- const char *file; /* Defined in file name. */
unsigned int line; /* Starting line number. */
unsigned int count; /* Number of tokens in expansion. */
unsigned short paramc; /* Number of parameters. */
@@ -152,13 +151,13 @@ builtin_macro (pfile, token)
case BT_BASE_FILE:
{
const char *name;
- cpp_buffer *buffer = pfile->buffer;
+ const struct line_map *map = pfile->map;
if (node->value.builtin == BT_BASE_FILE)
- while (buffer->prev)
- buffer = buffer->prev;
+ while (! MAIN_FILE_P (map))
+ map = INCLUDED_FROM (&pfile->line_maps, map);
- name = buffer->nominal_fname;
+ name = map->to_file;
make_string_token (&pfile->ident_pool, token,
(const unsigned char *) name, strlen (name));
}
@@ -1372,7 +1371,6 @@ _cpp_create_definition (pfile, node)
macro = (cpp_macro *) _cpp_pool_alloc (&pfile->macro_pool,
sizeof (cpp_macro));
- macro->file = pfile->buffer->nominal_fname;
macro->line = pfile->directive_pos.line;
macro->params = 0;
macro->paramc = 0;
@@ -1476,9 +1474,7 @@ _cpp_create_definition (pfile, node)
"\"%s\" redefined", NODE_NAME (node));
if (node->type == NT_MACRO && !(node->flags & NODE_BUILTIN))
- cpp_pedwarn_with_file_and_line (pfile,
- node->value.macro->file,
- node->value.macro->line, 1,
+ cpp_pedwarn_with_line (pfile, node->value.macro->line, 1,
"this is the location of the previous definition");
}
_cpp_free_definition (node);
Index: cppmain.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cppmain.c,v
retrieving revision 1.75
diff -u -p -r1.75 cppmain.c
--- cppmain.c 2001/08/11 07:33:39 1.75
+++ cppmain.c 2001/08/16 18:43:39
@@ -419,8 +419,7 @@ cb_file_change (pfile, map)
print.map = map;
}
-/* Copy a #pragma directive to the preprocessed output. LINE is the
- line of the current source file, not the logical line. */
+/* Copy a #pragma directive to the preprocessed output. */
static void
cb_def_pragma (pfile, line)
cpp_reader *pfile;
Index: fix-header.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/fix-header.c,v
retrieving revision 1.69
diff -u -p -r1.69 fix-header.c
--- fix-header.c 2001/08/12 09:49:44 1.69
+++ fix-header.c 2001/08/16 18:43:58
@@ -658,7 +658,7 @@ read_scan_file (in_fname, argc, argv)
/* Scan the macro expansion of "getchar();". */
cpp_push_buffer (scan_in, getchar_call, sizeof(getchar_call) - 1,
- BUF_BUILTIN, in_fname, 1);
+ BUF_BUILTIN, 1);
for (;;)
{
cpp_token t;
More information about the Gcc-patches
mailing list