This is the mail archive of the gcc-patches@gcc.gnu.org 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]

Re: Comments with -fpreprocessed


Neil Booth wrote:
> > allow -C to indicate that they are. I.e.
> >       gcc -E -C foo.c > foo.i                 ;# generate with comments
> >       gcc -c -C foo.i                         ;# accept with comments
> > should do what you want. Does that make sense?
> 
> Not really.  I don't like that - it would involve a variety of
> kludges.
If I understand what you, you're saying you don't like it because
of the implementation issues? It doesn't seem particularly icky -
straight forward really. I attach a patch which does exactly that.
ok for mainline?

built & tested on i686-pc-linux-gnu.

Note that with this patch you can now say
	gcc -c -save-temps -C foo.c
and have comments in the saved .i file (Ok, so that was a fairly minor
part of the patch). I have still left in the after_stage3 check
in _cpp_lex_token, to catch if ever someone does manage to get a comment
through to there.

> at the moment - but that's when the code hasn't been through the
> preprocessor.  If they give us -C preprocessed code, we can't do
> anything with it - gcc won't accept it with or without -fpreprocessed.
good point.

> I think this is bogus - I don't think you can give an example where it
> doesn't behave exactly the same, now we've fixed the directive
I don't think it bogus, I think it defensive programming.

Anyway, what do you think of the attached?

nathan

-- 
Dr Nathan Sidwell   ::   http://www.codesourcery.com   ::   CodeSourcery LLC
         'But that's a lie.' - 'Yes it is. What's your point?'
nathan@codesourcery.com : http://www.cs.bris.ac.uk/~nathan/ : nathan@acm.org
2001-05-25  Nathan Sidwell  <nathan@codesourcery.com>

	* cpplex.c (_cpp_lex_token): Accept comments in preprocessed
	source when told to.
	* cpplib.c (end_directive): Adjust save_comments state
	restore.
	* cppmacro.c (_cpp_create_definition): Likewise.
	* cppinit.c (cpp_create_reader): Likewise.
	* invoke.texi (Preprocessor Options, -C): Document additional
	semantics.
	* gcc.c (cpp_options): Remove check for -C without -E.
	(default_compilers): Copy -C option for preprocessed source.

2001-05-25  Nathan Sidwell  <nathan@codesourcery.com>

	* lang-specs.h: Copy -C option for preprocessed source.
	
Index: cppinit.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cppinit.c,v
retrieving revision 1.147.2.8
diff -c -3 -p -r1.147.2.8 cppinit.c
*** cppinit.c	2001/05/10 00:11:03	1.147.2.8
--- cppinit.c	2001/05/25 12:26:46
*************** cpp_create_reader (lang)
*** 517,523 ****
    pfile->deps = deps_init ();
  
    /* Initialize lexer state.  */
!   pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
  
    /* Indicate date and time not yet calculated.  */
    pfile->date.type = CPP_EOF;
--- 517,524 ----
    pfile->deps = deps_init ();
  
    /* Initialize lexer state.  */
!   pfile->state.save_comments = (!CPP_OPTION (pfile, preprocessed)
! 				&& !CPP_OPTION (pfile, discard_comments));
  
    /* Indicate date and time not yet calculated.  */
    pfile->date.type = CPP_EOF;
Index: cpplex.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cpplex.c,v
retrieving revision 1.129.2.9
diff -c -3 -p -r1.129.2.9 cpplex.c
*** cpplex.c	2001/05/16 09:44:09	1.129.2.9
--- cpplex.c	2001/05/25 12:26:48
*************** _cpp_lex_token (pfile, result)
*** 993,999 ****
  	ACCEPT_CHAR (CPP_DIV_EQ);
        if (c != '/' && c != '*')
  	break;
!       if (buffer->from_stage3)
  	break;
        
        if (c == '*')
--- 993,1004 ----
  	ACCEPT_CHAR (CPP_DIV_EQ);
        if (c != '/' && c != '*')
  	break;
!       /* If it is a preprocessed file, then it can contain comments if
!          discard_comments is zero (-C was given). Otherwise comments
!          can exist, if it is before stage 3. */
!       if (CPP_OPTION (pfile, preprocessed)
! 	  ? CPP_OPTION (pfile, discard_comments)
! 	  : buffer->from_stage3)
  	break;
        
        if (c == '*')
Index: cpplib.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cpplib.c,v
retrieving revision 1.239.2.7
diff -c -3 -p -r1.239.2.7 cpplib.c
*** cpplib.c	2001/05/10 00:11:03	1.239.2.7
--- cpplib.c	2001/05/25 12:26:49
*************** end_directive (pfile, skip_line)
*** 257,263 ****
  
    /* Restore state.  */
    pfile->la_write = pfile->la_saved;
!   pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
    pfile->state.in_directive = 0;
    pfile->state.angled_headers = 0;
    pfile->state.line_extension = 0;
--- 257,264 ----
  
    /* Restore state.  */
    pfile->la_write = pfile->la_saved;
!   pfile->state.save_comments = (!CPP_OPTION (pfile, preprocessed)
! 				&& !CPP_OPTION (pfile, discard_comments));
    pfile->state.in_directive = 0;
    pfile->state.angled_headers = 0;
    pfile->state.line_extension = 0;
Index: cppmacro.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cppmacro.c,v
retrieving revision 1.44.2.5
diff -c -3 -p -r1.44.2.5 cppmacro.c
*** cppmacro.c	2001/03/27 20:57:10	1.44.2.5
--- cppmacro.c	2001/05/25 12:26:50
*************** _cpp_create_definition (pfile, node)
*** 1400,1406 ****
      cpp_pedwarn (pfile, "ISO C requires whitespace after the macro name");
  
    /* Setting it here means we don't catch leading comments.  */
!   pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
  
    for (;;)
      {
--- 1400,1407 ----
      cpp_pedwarn (pfile, "ISO C requires whitespace after the macro name");
  
    /* Setting it here means we don't catch leading comments.  */
!   pfile->state.save_comments = (!CPP_OPTION (pfile, preprocessed)
! 				&& !CPP_OPTION (pfile, discard_comments));
  
    for (;;)
      {
Index: invoke.texi
===================================================================
RCS file: /cvs/gcc/egcs/gcc/invoke.texi,v
retrieving revision 1.273.2.24
diff -c -3 -p -r1.273.2.24 invoke.texi
*** invoke.texi	2001/05/25 01:22:09	1.273.2.24
--- invoke.texi	2001/05/25 12:26:55
*************** specified and output the results to stan
*** 3476,3483 ****
  specified output file.
  
  @item -C
! Tell the preprocessor not to discard comments.  Used with the
! @samp{-E} option.
  
  @item -P
  Tell the preprocessor not to generate @samp{#line} directives.
--- 3476,3487 ----
  specified output file.
  
  @item -C
! Tell the preprocessor or compiler proper that preprocessed source
! contains comments. When used with the @samp{-E} option, comments are
! preserved in the preprocessor output. When reading an already
! preprocessed file, tells the compiler that comments have been preserved
! (and so should be ignored). Preprocessor output traditionally does not
! contain comments.
  
  @item -P
  Tell the preprocessor not to generate @samp{#line} directives.
Index: gcc.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/gcc.c,v
retrieving revision 1.205.2.14
diff -c -3 -p -r1.205.2.14 gcc.c
*** gcc.c	2001/05/18 18:40:50	1.205.2.14
--- gcc.c	2001/05/25 12:26:57
*************** static const char *trad_capable_cpp =
*** 590,597 ****
  "%{traditional|ftraditional|traditional-cpp:trad}cpp0";
  
  static const char *cpp_options =
! "%{C:%{!E:%eGNU C does not support -C without using -E}}\
!  %{std*} %{nostdinc*}\
   %{C} %{v} %{I*} %{P} %{$} %I\
   %{MD:-M -MF %W{!o: %b.d}%W{o*:%.d%*}}\
   %{MMD:-MM -MF %W{!o: %b.d}%W{o*:%.d%*}}\
--- 590,596 ----
  "%{traditional|ftraditional|traditional-cpp:trad}cpp0";
  
  static const char *cpp_options =
! "%{std*} %{nostdinc*}\
   %{C} %{v} %{I*} %{P} %{$} %I\
   %{MD:-M -MF %W{!o: %b.d}%W{o*:%.d%*}}\
   %{MMD:-MM -MF %W{!o: %b.d}%W{o*:%.d%*}}\
*************** static struct compiler default_compilers
*** 750,760 ****
        %{!E:%{!M:%{!MM:\
  	  %{save-temps:%(trad_capable_cpp) -lang-c %{ansi:-std=c89}\
  		%(cpp_options) %b.i \n\
! 		    cc1 -fpreprocessed %b.i %(cc1_options)}\
  	  %{!save-temps:\
  	    %{traditional|ftraditional|traditional-cpp:\
  		tradcpp0 -lang-c %{ansi:-std=c89} %(cpp_options) %{!pipe:%g.i} |\n\
! 		    cc1 -fpreprocessed %{!pipe:%g.i} %(cc1_options)}\
  	    %{!traditional:%{!ftraditional:%{!traditional-cpp:\
  		cc1 -lang-c %{ansi:-std=c89} %(cpp_options) %(cc1_options)}}}}\
          %{!fsyntax-only:%(invoke_as)}}}}", 0},
--- 749,759 ----
        %{!E:%{!M:%{!MM:\
  	  %{save-temps:%(trad_capable_cpp) -lang-c %{ansi:-std=c89}\
  		%(cpp_options) %b.i \n\
! 		    cc1 -fpreprocessed %{C} %b.i %(cc1_options)}\
  	  %{!save-temps:\
  	    %{traditional|ftraditional|traditional-cpp:\
  		tradcpp0 -lang-c %{ansi:-std=c89} %(cpp_options) %{!pipe:%g.i} |\n\
! 		    cc1 -fpreprocessed %{C} %{!pipe:%g.i} %(cc1_options)}\
  	    %{!traditional:%{!ftraditional:%{!traditional-cpp:\
  		cc1 -lang-c %{ansi:-std=c89} %(cpp_options) %(cc1_options)}}}}\
          %{!fsyntax-only:%(invoke_as)}}}}", 0},
*************** static struct compiler default_compilers
*** 767,773 ****
      %(trad_capable_cpp) -lang-c %{ansi:-std=c89} %(cpp_options)", 0},
    {".i", "@cpp-output", 0},
    {"@cpp-output",
!    "%{!M:%{!MM:%{!E:cc1 -fpreprocessed %i %(cc1_options) %{!fsyntax-only:%(invoke_as)}}}}", 0},
    {".s", "@assembler", 0},
    {"@assembler",
     "%{!M:%{!MM:%{!E:%{!S:as %(asm_options) %i %A }}}}", 0},
--- 766,772 ----
      %(trad_capable_cpp) -lang-c %{ansi:-std=c89} %(cpp_options)", 0},
    {".i", "@cpp-output", 0},
    {"@cpp-output",
!    "%{!M:%{!MM:%{!E:cc1 -fpreprocessed %{C} %i %(cc1_options) %{!fsyntax-only:%(invoke_as)}}}}", 0},
    {".s", "@assembler", 0},
    {"@assembler",
     "%{!M:%{!MM:%{!E:%{!S:as %(asm_options) %i %A }}}}", 0},
Index: cp/lang-specs.h
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cp/lang-specs.h,v
retrieving revision 1.39.2.3
diff -c -3 -p -r1.39.2.3 lang-specs.h
*** lang-specs.h	2001/05/14 19:40:16	1.39.2.3
--- lang-specs.h	2001/05/25 12:26:57
*************** Boston, MA 02111-1307, USA.  */
*** 48,54 ****
  		    %{!fno-new-abi:-D__GXX_ABI_VERSION=100}\
  		    %{ansi:-D__STRICT_ANSI__ -trigraphs -$}\
  		    %(cpp_options) %b.ii \n}\
!       cc1plus %{save-temps:-fpreprocessed %b.ii}\
                %{!save-temps:%(cpp_options)\
  			    %{!no-gcc:-D__GNUG__=%v1} -D_GNU_SOURCE \
         			    %{!Wno-deprecated:-D__GXX_DEPRECATED}\
--- 48,54 ----
  		    %{!fno-new-abi:-D__GXX_ABI_VERSION=100}\
  		    %{ansi:-D__STRICT_ANSI__ -trigraphs -$}\
  		    %(cpp_options) %b.ii \n}\
!       cc1plus %{save-temps:-fpreprocessed %{C} %b.ii}\
                %{!save-temps:%(cpp_options)\
  			    %{!no-gcc:-D__GNUG__=%v1} -D_GNU_SOURCE \
         			    %{!Wno-deprecated:-D__GXX_DEPRECATED}\
*************** Boston, MA 02111-1307, USA.  */
*** 62,66 ****
    {".ii", "@c++-cpp-output", 0},
    {"@c++-cpp-output",
     "%{!M:%{!MM:%{!E:\
!     cc1plus -fpreprocessed %i %(cc1_options) %2 %{+e*}\
      %{!fsyntax-only:%(invoke_as)}}}}", 0},
--- 62,66 ----
    {".ii", "@c++-cpp-output", 0},
    {"@c++-cpp-output",
     "%{!M:%{!MM:%{!E:\
!     cc1plus -fpreprocessed %{C} %i %(cc1_options) %2 %{+e*}\
      %{!fsyntax-only:%(invoke_as)}}}}", 0},

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