This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [CPP] -M vs -include

Richard Henderson wrote:-

> The following test case:
>   % echo > x.h
>   % echo > z.h
>   % echo '#include "z.h"' > z.c
>   % gcc -M -include x.h z.c
> produced
>   z.o: z.c x.h
> instead of the expected
>   z.o: z.c x.h z.h
> This caused the dependencies created by glibc to be wrong, which
> leads to extremely irritating non-recompilation errors.
> The following patch appears to cure the problem.  Neil, can you
> comment on why you added this in the first place?

cpp_scan_nooutput's semantics are to return when the current buffer
(and anything it includes) have been scanned; this is why it sets
return_at_eof.  This is what we need for -imacros; with your suggested
patch -imacros would not stop after the first -imacros file; it would
continue its scan and only get its CPP_EOF at the end of the main file.

The bug is that cpp_preprocess_file in cppmain.c is assuming, in calling
cpp_scan_nooutput the way it does, that the current buffer is the
main file, and so that when the current buffer is finished, the whole
job is done.  In the presence of -include, that is not true, as
_cpp_maybe_push_include_file has put the first -included file on top
of the main file and made it the current one.

Easily fixed by forcing cpp_preprocess_file to loop until it really has
done all the buffers.  I seem to recall removing this loop thinking it
didn't achieve anything.  Heh.

The loop condition sucks; I'll do something better for 3.4.

Thanks for finding this issue.  I'll apply the patch after a bootstrap


	* cppmain.c (cpp_preprocess_file): Loop to pop any -included

Index: cppmain.c
RCS file: /cvs/gcc/gcc/gcc/cppmain.c,v
retrieving revision 1.104
diff -u -p -r1.104 cppmain.c
--- cppmain.c	12 Aug 2002 17:21:52 -0000	1.104
+++ cppmain.c	7 Nov 2002 23:35:42 -0000
@@ -78,7 +78,13 @@ cpp_preprocess_file (pfile, in_fname, ou
       /* A successful cpp_read_main_file guarantees that we can call
 	 cpp_scan_nooutput or cpp_get_token next.  */
       if (options->no_output)
-	cpp_scan_nooutput (pfile);
+	{
+	  /* Loop in case of -included buffers.  */
+	  do
+	    cpp_scan_nooutput (pfile);
+	  while (pfile->buffer->prev
+		 || pfile->buffer->cur < pfile->buffer->rlimit);
+	}
       else if (options->traditional)
 	scan_translation_unit_trad (pfile);

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]