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]

cpplib: Digraphs (Part 1 of 2)


This patch ensures that cpplib only honours digraphs in the relevant
-std= settings.  Basically, -traditional and everything pre-1994 turns
them off.

Bootstrapping i386 Linux.

Neil.

	* cppinit.c: (cpp_reader_init): Allow digraphs by default.
	(handle_option): Set digraphs according to standard.

	* cpplex.c: (lex_line, can_paste): Honour digraphs in
	accordance with	the digraphs flag.

	* cpplib.h: (struct cpp_options): New option digraphs.

	* testsuite/gcc.dg/cpp/digraph1.c,
	testsuite/gcc.dg/cpp/digraph2.c,
	testsuite/gcc.dg/cpp/digraphs.c: New tests.

Index: cppinit.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cppinit.c,v
retrieving revision 1.92
diff -u -p -r1.92 cppinit.c
--- cppinit.c	2000/07/09 09:19:44	1.92
+++ cppinit.c	2000/07/09 10:49:22
@@ -418,6 +418,7 @@ cpp_reader_init (pfile)
   CPP_OPTION (pfile, cplusplus_comments) = 1;
   CPP_OPTION (pfile, warn_import) = 1;
   CPP_OPTION (pfile, warn_paste) = 1;
+  CPP_OPTION (pfile, digraphs) = 1;
   CPP_OPTION (pfile, discard_comments) = 1;
   CPP_OPTION (pfile, show_column) = 1;
   CPP_OPTION (pfile, tabstop) = 8;
@@ -1270,6 +1271,7 @@ handle_option (pfile, argc, argv)
 	  CPP_OPTION (pfile, traditional) = 1;
 	  CPP_OPTION (pfile, cplusplus_comments) = 0;
 	  CPP_OPTION (pfile, trigraphs) = 0;
+	  CPP_OPTION (pfile, digraphs) = 0;
 	  CPP_OPTION (pfile, warn_trigraphs) = 0;
 	  break;
 	case OPT_trigraphs:
@@ -1291,6 +1293,7 @@ handle_option (pfile, argc, argv)
 	  CPP_OPTION (pfile, cplusplus_comments) = 1;
 	  CPP_OPTION (pfile, c89) = 0;
 	  CPP_OPTION (pfile, c99) = 1;
+	  CPP_OPTION (pfile, digraphs) = 1;
 	  CPP_OPTION (pfile, objc) = 0;
 	  break;
 	case OPT_lang_c89:
@@ -1299,6 +1302,7 @@ handle_option (pfile, argc, argv)
 	  CPP_OPTION (pfile, c89) = 1;
 	  CPP_OPTION (pfile, c99) = 0;
 	  CPP_OPTION (pfile, objc) = 0;
+	  CPP_OPTION (pfile, digraphs) = 0;
 	  CPP_OPTION (pfile, trigraphs) = 1;
 	  new_pending_directive (pend, "__STRICT_ANSI__", cpp_define);
 	  break;
@@ -1308,6 +1312,7 @@ handle_option (pfile, argc, argv)
 	  CPP_OPTION (pfile, c89) = 0;
 	  CPP_OPTION (pfile, c99) = 0;
 	  CPP_OPTION (pfile, objc) = 0;
+	  CPP_OPTION (pfile, digraphs) = 1;
 	  new_pending_directive (pend, "__cplusplus", cpp_define);
 	  break;
 	case OPT_lang_objcplusplus:
@@ -1353,6 +1358,7 @@ handle_option (pfile, argc, argv)
 	  CPP_OPTION (pfile, c89) = 1;
 	  CPP_OPTION (pfile, c99) = 0;
 	  CPP_OPTION (pfile, objc) = 0;
+	  CPP_OPTION (pfile, digraphs) = 0;
 	  break;
 	case OPT_std_gnu9x:
 	case OPT_std_gnu99:
@@ -1360,6 +1366,7 @@ handle_option (pfile, argc, argv)
 	  CPP_OPTION (pfile, cplusplus_comments) = 1;
 	  CPP_OPTION (pfile, c89) = 0;
 	  CPP_OPTION (pfile, c99) = 1;
+	  CPP_OPTION (pfile, digraphs) = 1;
 	  CPP_OPTION (pfile, objc) = 0;
 	  new_pending_directive (CPP_OPTION (pfile, pending),
 				 "__STDC_VERSION__=199901L", cpp_define);
@@ -1375,6 +1382,7 @@ handle_option (pfile, argc, argv)
 	  CPP_OPTION (pfile, c89) = 1;
 	  CPP_OPTION (pfile, c99) = 0;
 	  CPP_OPTION (pfile, objc) = 0;
+	  CPP_OPTION (pfile, digraphs) = opt_code == OPT_std_iso9899_199409;
 	  CPP_OPTION (pfile, trigraphs) = 1;
 	  new_pending_directive (CPP_OPTION (pfile, pending),
 				 "__STRICT_ANSI__", cpp_define);
@@ -1388,6 +1396,7 @@ handle_option (pfile, argc, argv)
 	  CPP_OPTION (pfile, c89) = 0;
 	  CPP_OPTION (pfile, c99) = 1;
 	  CPP_OPTION (pfile, objc) = 0;
+	  CPP_OPTION (pfile, digraphs) = 1;
 	  CPP_OPTION (pfile, trigraphs) = 1;
 	  new_pending_directive (CPP_OPTION (pfile, pending),
 				 "__STRICT_ANSI__", cpp_define);
Index: cpplex.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cpplex.c,v
retrieving revision 1.66
diff -u -p -r1.66 cpplex.c
--- cpplex.c	2000/07/09 09:19:44	1.66
+++ cpplex.c	2000/07/09 10:49:26
@@ -24,7 +24,6 @@ Foundation, 59 Temple Place - Suite 330,
 
 Cleanups to do:-
 
-o Get use of digraphs in sync with the standard reqd on the command line.
 o -dM and with _cpp_dump_list: too many \n output.
 o Put a printer object in cpp_reader?
 o Check line numbers assigned to all errors.
@@ -1572,14 +1571,17 @@ lex_line (pfile, list)
 	      if (PREV_TOKEN_TYPE == CPP_COLON
 		  && CPP_OPTION (pfile, cplusplus))
 		BACKUP_TOKEN (CPP_SCOPE);
-	      /* Digraph: "<:" is a '['  */
-	      else if (PREV_TOKEN_TYPE == CPP_LESS)
-		BACKUP_DIGRAPH (CPP_OPEN_SQUARE);
-	      /* Digraph: "%:" is a '#'  */
-	      else if (PREV_TOKEN_TYPE == CPP_MOD)
+	      else if (CPP_OPTION (pfile, digraphs))
 		{
-		  (--cur_token)->flags |= DIGRAPH;
-		  goto make_hash;
+		  /* Digraph: "<:" is a '['  */
+		  if (PREV_TOKEN_TYPE == CPP_LESS)
+		    BACKUP_DIGRAPH (CPP_OPEN_SQUARE);
+		  /* Digraph: "%:" is a '#'  */
+		  else if (PREV_TOKEN_TYPE == CPP_MOD)
+		    {
+		      (--cur_token)->flags |= DIGRAPH;
+		      goto make_hash;
+		    }
 		}
 	    }
 	  cur_token++;
@@ -1623,12 +1625,15 @@ lex_line (pfile, list)
 		BACKUP_TOKEN (CPP_RSHIFT);
 	      else if (PREV_TOKEN_TYPE == CPP_MINUS)
 		BACKUP_TOKEN (CPP_DEREF);
-	      /* Digraph: ":>" is a ']'  */
-	      else if (PREV_TOKEN_TYPE == CPP_COLON)
-		BACKUP_DIGRAPH (CPP_CLOSE_SQUARE);
-	      /* Digraph: "%>" is a '}'  */
-	      else if (PREV_TOKEN_TYPE == CPP_MOD)
-		BACKUP_DIGRAPH (CPP_CLOSE_BRACE);
+	      else if (CPP_OPTION (pfile, digraphs))
+		{
+		  /* Digraph: ":>" is a ']'  */
+		  if (PREV_TOKEN_TYPE == CPP_COLON)
+		    BACKUP_DIGRAPH (CPP_CLOSE_SQUARE);
+		  /* Digraph: "%>" is a '}'  */
+		  else if (PREV_TOKEN_TYPE == CPP_MOD)
+		    BACKUP_DIGRAPH (CPP_CLOSE_BRACE);
+		}
 	    }
 	  cur_token++;
 	  break;
@@ -1652,7 +1657,8 @@ lex_line (pfile, list)
 	case '%':
 	  /* Digraph: "<%" is a '{'  */
 	  cur_token->type = CPP_MOD;
-	  if (IMMED_TOKEN () && PREV_TOKEN_TYPE == CPP_LESS)
+	  if (IMMED_TOKEN () && PREV_TOKEN_TYPE == CPP_LESS
+	      && CPP_OPTION (pfile, digraphs))
 	    BACKUP_DIGRAPH (CPP_OPEN_BRACE);
 	  cur_token++;
 	  break;
@@ -2421,10 +2427,13 @@ can_paste (pfile, token1, token2, digrap
       if (b == a) return CPP_LSHIFT;
       if (b == CPP_QUERY && cxx)	return CPP_MIN;
       if (b == CPP_LESS_EQ)	return CPP_LSHIFT_EQ;
-      if (b == CPP_COLON)
-	{*digraph = 1; return CPP_OPEN_SQUARE;} /* <: digraph */
-      if (b == CPP_MOD)
-	{*digraph = 1; return CPP_OPEN_BRACE;}	/* <% digraph */
+      if (CPP_OPTION (pfile, digraphs))
+	{
+	  if (b == CPP_COLON)
+	    {*digraph = 1; return CPP_OPEN_SQUARE;} /* <: digraph */
+	  if (b == CPP_MOD)
+	    {*digraph = 1; return CPP_OPEN_BRACE;}	/* <% digraph */
+	}
       break;
 
     case CPP_PLUS: if (b == a)	return CPP_PLUS_PLUS; break;
@@ -2437,15 +2446,18 @@ can_paste (pfile, token1, token2, digrap
       break;
     case CPP_COLON:
       if (b == a && cxx)	return CPP_SCOPE;
-      if (b == CPP_GREATER)
+      if (b == CPP_GREATER && CPP_OPTION (pfile, digraphs))
 	{*digraph = 1; return CPP_CLOSE_SQUARE;} /* :> digraph */
       break;
 
     case CPP_MOD:
-      if (b == CPP_GREATER)
-	{*digraph = 1; return CPP_CLOSE_BRACE;}  /* %> digraph */
-      if (b == CPP_COLON)
-	{*digraph = 1; return CPP_HASH;}         /* %: digraph */
+      if (CPP_OPTION (pfile, digraphs))
+	{
+	  if (b == CPP_GREATER)
+	    {*digraph = 1; return CPP_CLOSE_BRACE;}  /* %> digraph */
+	  if (b == CPP_COLON)
+	    {*digraph = 1; return CPP_HASH;}         /* %: digraph */
+	}
       break;
     case CPP_DEREF:
       if (b == CPP_MULT && cxx)	return CPP_DEREF_STAR;
Index: cpplib.h
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cpplib.h,v
retrieving revision 1.107
diff -u -p -r1.107 cpplib.h
--- cpplib.h	2000/07/08 19:00:39	1.107
+++ cpplib.h	2000/07/09 10:49:27
@@ -329,6 +329,9 @@ struct cpp_options
   /* Nonzero means process the ISO trigraph sequences.  */
   unsigned char trigraphs;
 
+  /* Nonzero means process the ISO digraph sequences.  */
+  unsigned char digraphs;
+
   /* Nonzero means print the names of included files rather than the
      preprocessed output.  1 means just the #include "...", 2 means
      #include <...> as well.  */
Index: testsuite/gcc.dg/cpp/digraph1.c
===================================================================
RCS file: digraph1.c
diff -N digraph1.c
--- /dev/null	Tue May  5 13:32:27 1998
+++ digraph1.c	Sun Jul  9 03:49:34 2000
@@ -0,0 +1,17 @@
+/* Copyright (C) 2000 Free Software Foundation, Inc.  */
+
+/* { dg-do compile } */
+/* { dg-options "-std=iso9899:199409" } */
+
+/* Just simple check that digraphs are on under c94, for both
+   preprocessor and compiler.  digraphs.c is the general test.  */
+
+%:define glue
+#ifndef glue
+#error glue not defined!
+#endif
+
+int main (int argc, char *argv<::>)
+<%
+  return 0;
+%>
Index: testsuite/gcc.dg/cpp/digraph2.c
===================================================================
RCS file: digraph2.c
diff -N digraph2.c
--- /dev/null	Tue May  5 13:32:27 1998
+++ digraph2.c	Sun Jul  9 03:49:34 2000
@@ -0,0 +1,17 @@
+/* Copyright (C) 2000 Free Software Foundation, Inc.  */
+
+/* { dg-do compile } */
+/* { dg-options "-std=c89" } */
+
+/* Just simple check that digraphs are not on in c89, for both
+   preprocessor and compiler.  digraphs.c is the general test.  */
+
+%:define glue
+#ifdef glue
+#error glue is defined!
+#endif
+
+int main (int argc, char *argv[:>) /*  */
+{
+  return 0;
+}
Index: testsuite/gcc.dg/cpp/digraphs.c
===================================================================
RCS file: digraphs.c
diff -N digraphs.c
--- /dev/null	Tue May  5 13:32:27 1998
+++ digraphs.c	Sun Jul  9 03:49:34 2000
@@ -0,0 +1,30 @@
+/* Copyright (C) 2000 Free Software Foundation, Inc.  */
+
+/* { dg-do run } */
+/* { dg-options "-std=c99" } */
+
+/* Fully test the 6 digraphs under c99 assumptions.  Four are pasted,
+   to check that digraph pasting works.  */
+
+extern int strcmp (const char *, const char *);
+extern void abort (void);
+#define err(str) do { puts(str); abort(); } while (0)
+
+%:define glue(x, y) x %:%: y	/* #define glue(x, y) x ## y. */
+#ifndef glue
+#error glue not defined!
+#endif
+%:define str(x) %:x		/* #define str(x) #x */
+
+int main (int argc, char *argv<::>) /* argv[] */
+glue (<, %) /* { */
+             /* di_str[] = */
+  const char di_str glue(<, :)glue(:, >) = str(%:%:<::><%%>%:);
+
+  /* Check the glue macro actually pastes, and that the spelling of
+     all digraphs is preserved.  */
+  if (glue(str, cmp) (di_str, "%:%:<::><%%>%:"))
+    err ("Digraph spelling not preserved!");
+
+  return 0;
+glue (%, >) /* } */

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