This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
cpplib: Return endless CPP_EOFs at end of translation unit
- To: gcc-patches at gcc dot gnu dot org
- Subject: cpplib: Return endless CPP_EOFs at end of translation unit
- From: Neil Booth <neil at daikokuya dot demon dot co dot uk>
- Date: Tue, 7 Aug 2001 19:33:54 +0100
- Cc: Gerald Pfeifer <pfeifer at dbai dot tuwien dot ac dot at>
With my recent changes to cpplib, it was returning CPP_EOF at the end
of the translation unit, and popping the final buffer at the same
time. This meant any subsequent calls to cpp_get_token would segfault
since the buffer stack was empty.
Requiring clients to only call cpp_get_token the exact number of times
is a bit onerous, so this patch doesn't pop the final buffer.
fix-header was calling cpp_get_token even after receiving a CPP_EOF,
for example.
I'll bootstrap this on x86 Linux and commit it if it passes. Gerald,
please confirm this solves your bootstrap issue (and sorry for causing
it).
Neil.
* cppinit.c (cpp_finish): Pop the final buffer without comment.
* cpplex.c (_cpp_lex_token): Don't pop the final buffer; and
take care to avoid multiple no-newline at EOF warnings in that
case.
Index: cppinit.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cppinit.c,v
retrieving revision 1.168
diff -u -p -r1.168 cppinit.c
--- cppinit.c 2001/08/05 17:31:22 1.168
+++ cppinit.c 2001/08/07 18:28:02
@@ -1010,12 +1010,13 @@ void
cpp_finish (pfile)
cpp_reader *pfile;
{
- if (CPP_BUFFER (pfile))
- {
- cpp_ice (pfile, "buffers still stacked in cpp_finish");
- while (CPP_BUFFER (pfile))
- _cpp_pop_buffer (pfile);
- }
+ /* cpplex.c leaves the final buffer on the stack. This it so that
+ it returns an unending stream of CPP_EOFs to the client. If we
+ popped the buffer, we'd derefence a NULL buffer pointer and
+ segfault. It's nice to allow the client to do worry-free excess
+ cpp_get_token calls. */
+ while (pfile->buffer)
+ _cpp_pop_buffer (pfile);
/* Don't write the deps file if preprocessing has failed. */
if (CPP_OPTION (pfile, print_deps) && pfile->errors == 0)
Index: cpplex.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cpplex.c,v
retrieving revision 1.152
diff -u -p -r1.152 cpplex.c
--- cpplex.c 2001/08/05 17:31:23 1.152
+++ cpplex.c 2001/08/07 18:28:12
@@ -897,21 +897,26 @@ _cpp_lex_token (pfile, result)
pfile->line--;
else if (! pfile->state.parsing_args)
{
- unsigned char ret = pfile->buffer->return_at_eof;
-
/* Non-empty files should end in a newline. Don't warn for
command line and _Pragma buffers. */
if (pfile->lexer_pos.col != 0)
{
- /* Account for the missing \n. */
+ /* Account for the missing \n, prevent multiple warnings. */
pfile->line++;
+ pfile->lexer_pos.col = 0;
if (!buffer->from_stage3)
cpp_pedwarn (pfile, "no newline at end of file");
}
+
+ /* Don't pop the last file. */
+ if (buffer->prev)
+ {
+ unsigned char stop = buffer->return_at_eof;
- _cpp_pop_buffer (pfile);
- if (pfile->buffer && !ret)
- goto next_token;
+ _cpp_pop_buffer (pfile);
+ if (!stop)
+ goto next_token;
+ }
}
result->type = CPP_EOF;
return;