Patch to improve cpplib's C++ support

Jason Merrill jason@cygnus.com
Tue Jul 20 03:06:00 GMT 1999


Here's a revised patch.  Can I put this in?

Tue Jul 20 02:52:01 1999  Jason Merrill  <jason@yorick.cygnus.com>

	* toplev.c (documented_lang_options): Add -fpreprocessed.
	* cpplib.h (struct cpp_buffer): Add preprocessed.
	* cppinit.c (cpp_handle_option): Handle -fpreprocessed.
	(cpp_start_read): Don't expand macros or emit an initial #line
	directive if -fpreprocessed.

Tue Jul 20 16:16:09 1999  Michael Tiemann  <tiemann@holodeck.cygnus.com>

	* cpplib.h (struct cpp_buffer): Added manual_pop for
	better C++ tokenization.
	* cpplib.c (cpp_get_token): Return CPP_EOF if manual_pop.
	Also, support C++ tokenization for ->*, .*, <?, and >? operators.
	* c-common.c (cpp_token): Make non-static.

Index: cppinit.c
===================================================================
RCS file: /egcs/carton/cvsfiles/egcs/gcc/cppinit.c,v
retrieving revision 1.14
diff -c -p -r1.14 cppinit.c
*** cppinit.c	1999/05/10 15:24:34	1.14
--- cppinit.c	1999/07/20 09:54:30
*************** cpp_start_read (pfile, fname)
*** 961,966 ****
--- 961,971 ----
        cpp_message (pfile, -1, "End of search list.\n");
      }
  
+   /* Don't bother trying to do macro expansion if we've already done
+      preprocessing.  */
+   if (opts->preprocessed)
+     pfile->no_macro_expand++;
+ 
    /* Open the main input file.
       We do this in nonblocking mode so we don't get stuck here if
       someone clever has asked cpp to process /dev/rmt0;
*************** cpp_start_read (pfile, fname)
*** 988,994 ****
    ih_fake->limit = 0;
    if (!finclude (pfile, f, ih_fake))
      return 0;
!   output_line_command (pfile, same_file);
    pfile->only_seen_white = 2;
  
    /* The -imacros files can be scanned now, but the -include files
--- 993,1000 ----
    ih_fake->limit = 0;
    if (!finclude (pfile, f, ih_fake))
      return 0;
!   if (! opts->preprocessed)
!     output_line_command (pfile, same_file);
    pfile->only_seen_white = 2;
  
    /* The -imacros files can be scanned now, but the -include files
*************** cpp_handle_option (pfile, argc, argv)
*** 1155,1160 ****
--- 1161,1170 ----
  	  user_label_prefix = "_";
  	else if (!strcmp (argv[i], "-fno-leading-underscore"))
  	  user_label_prefix = "";
+ 	else if (!strcmp (argv[i], "-fpreprocessed"))
+ 	  opts->preprocessed = 1;
+ 	else if (!strcmp (argv[i], "-fno-preprocessed"))
+ 	  opts->preprocessed = 0;
  	break;
  
        case 'I':			/* Add directory to path for includes.  */
Index: cpplib.c
===================================================================
RCS file: /egcs/carton/cvsfiles/egcs/gcc/cpplib.c,v
retrieving revision 1.82
diff -c -p -r1.82 cpplib.c
*** cpplib.c	1999/06/07 10:35:26	1.82
--- cpplib.c	1999/07/20 09:54:30
*************** cpp_get_token (pfile)
*** 2029,2035 ****
      handle_eof:
        if (CPP_BUFFER (pfile)->seen_eof)
  	{
! 	  if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) == CPP_NULL_BUFFER (pfile))
  	    return CPP_EOF;
  
  	  cpp_pop_buffer (pfile);
--- 2029,2038 ----
      handle_eof:
        if (CPP_BUFFER (pfile)->seen_eof)
  	{
! 	  if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) == CPP_NULL_BUFFER (pfile)
! 	      /* If we've been reading from redirected input, the
! 		 frontend will pop the buffer.  */
! 	      || CPP_BUFFER (pfile)->manual_pop)
  	    return CPP_EOF;
  
  	  cpp_pop_buffer (pfile);
*************** cpp_get_token (pfile)
*** 2172,2179 ****
  	  c2 = PEEKC ();
  	  if (c2 == '-' && opts->chill)
  	    goto comment;  /* Chill style comment */
! 	  if (c2 == '-' || c2 == '=' || c2 == '>')
  	    goto op2;
  	  goto randomchar;
  
  	case '<':
--- 2175,2199 ----
  	  c2 = PEEKC ();
  	  if (c2 == '-' && opts->chill)
  	    goto comment;  /* Chill style comment */
! 	  if (c2 == '-' || c2 == '=')
  	    goto op2;
+ 	  if (c2 == '>')
+ 	    {
+ 	      if (opts->cplusplus && PEEKN (1) == '*')
+ 		{
+ 		  /* In C++, there's a ->* operator.  */
+ 		op3:
+ 		  token = CPP_OTHER;
+ 		  pfile->only_seen_white = 0;
+ 		  CPP_RESERVE (pfile, 4);
+ 		  CPP_PUTC_Q (pfile, c);
+ 		  CPP_PUTC_Q (pfile, GETC ());
+ 		  CPP_PUTC_Q (pfile, GETC ());
+ 		  CPP_NUL_TERMINATE_Q (pfile);
+ 		  return token;
+ 		}
+ 	      goto op2;
+ 	    }
  	  goto randomchar;
  
  	case '<':
*************** cpp_get_token (pfile)
*** 2219,2225 ****
  	  c2 = PEEKC ();
  	  if (c2 == '=')
  	    goto op2;
! 	  if (c2 != c)
  	    goto randomchar;
  	  FORWARD(1);
  	  CPP_RESERVE (pfile, 4);
--- 2239,2246 ----
  	  c2 = PEEKC ();
  	  if (c2 == '=')
  	    goto op2;
! 	  /* GNU C++ supports MIN and MAX operators <? and >?.  */
! 	  if (c2 != c && (!opts->cplusplus || c2 != '?'))
  	    goto randomchar;
  	  FORWARD(1);
  	  CPP_RESERVE (pfile, 4);
*************** cpp_get_token (pfile)
*** 2241,2246 ****
--- 2262,2272 ----
  	      c = GETC ();
  	      goto number;
  	    }
+ 
+ 	  /* In C++ there's a .* operator.  */
+ 	  if (opts->cplusplus && c2 == '*')
+ 	    goto op2;
+ 
  	  if (c2 == '.' && PEEKN(1) == '.')
  	    {
  	      CPP_RESERVE(pfile, 4);
*************** parse_name (pfile, c)
*** 2549,2555 ****
  /* Parse a string starting with C.  A single quoted string is treated
     like a double -- some programs (e.g., troff) are perverse this way.
     (However, a single quoted string is not allowed to extend over
!    multiple lines.  */
  static void
  parse_string (pfile, c)
       cpp_reader *pfile;
--- 2575,2581 ----
  /* Parse a string starting with C.  A single quoted string is treated
     like a double -- some programs (e.g., troff) are perverse this way.
     (However, a single quoted string is not allowed to extend over
!    multiple lines.)  */
  static void
  parse_string (pfile, c)
       cpp_reader *pfile;
Index: cpplib.h
===================================================================
RCS file: /egcs/carton/cvsfiles/egcs/gcc/cpplib.h,v
retrieving revision 1.38
diff -c -p -r1.38 cpplib.h
*** cpplib.h	1999/05/10 15:24:35	1.38
--- cpplib.h	1999/07/20 09:54:30
*************** struct cpp_buffer
*** 136,141 ****
--- 136,145 ----
       escapes are used only in macro buffers, and backslash-newline is removed
       from macro expansion text in collect_expansion and/or macarg.  */
    char has_escapes;
+ 
+   /* Used by the C++ frontend to implement redirected input (such as for
+      default argument and/or template parsing).  */
+   char manual_pop;
  };
  
  struct file_name_map_list;
*************** struct cpp_options {
*** 453,458 ****
--- 457,466 ----
    
    /* Nonzero means give all the error messages the ANSI standard requires.  */
    char pedantic;
+ 
+   /* Nonzero means we're looking at already preprocessed code, so don't
+      bother trying to do macro expansion and whatnot.  */
+   char preprocessed;
  
    char done_initializing;
  
Index: c-common.c
===================================================================
RCS file: /egcs/carton/cvsfiles/egcs/gcc/c-common.c,v
retrieving revision 1.58
diff -c -p -r1.58 c-common.c
*** c-common.c	1999/07/06 12:34:41	1.58
--- c-common.c	1999/07/20 09:54:30
*************** Boston, MA 02111-1307, USA.  */
*** 34,40 ****
  #include "cpplib.h"
  cpp_reader  parse_in;
  cpp_options parse_options;
! static enum cpp_token cpp_token;
  #endif
  
  #ifndef WCHAR_TYPE_SIZE
--- 34,40 ----
  #include "cpplib.h"
  cpp_reader  parse_in;
  cpp_options parse_options;
! enum cpp_token cpp_token;
  #endif
  
  #ifndef WCHAR_TYPE_SIZE
Index: toplev.c
===================================================================
RCS file: /egcs/carton/cvsfiles/egcs/gcc/toplev.c,v
retrieving revision 1.187
diff -c -p -r1.187 toplev.c
*** toplev.c	1999/07/09 22:48:57	1.187
--- toplev.c	1999/07/20 09:54:30
*************** documented_lang_options[] =
*** 1028,1033 ****
--- 1028,1035 ----
    { "-fno-cond-mismatch", "" },
    { "-fdollars-in-identifiers", "Allow the use of $ inside identifiers" },
    { "-fno-dollars-in-identifiers", "" },
+   { "-fpreprocessed", "" },
+   { "-fno-preprocessed", "" },
    { "-fshort-double", "Use the same size for double as for float" },
    { "-fno-short-double", "" },
    { "-fshort-enums", "Use the smallest fitting integer to hold enums"},


More information about the Gcc-patches mailing list