cpplib: More switch cleanup

Neil Booth neil@daikokuya.co.uk
Sun Aug 11 00:31:00 GMT 2002


This gets the front ends handling input / output filenames, -remap
and -o.

I also broke out the c99 settings into a subroutine.  This showed up
a bug where currently -std=gnu99 doesn't clear flag_iso (seems to
have been around for a long time; a similar one existed I think for
-std=gnu89).

My attempt to capture bad standards with OPT_std_bad only works about
10% of the time, because of the nature of the binary search lookup,
so I'm removing it.

I'm about to apply this; it has bootstrapped and regtested.

Neil.

	* c-common.h (enum c_language_kind): Emphasize that clk_c is 0.
	* c-opts.c (parse_option): Rename find_opt.
	(set_std_c99): New function.
	(COMMAND_LINE_OPTIONS): Handle -remap and -o.  Remove OPT_std_bad.
	(missing_arg): Remove OPT_std_bad.  Handle -o.
	(c_common_decode_option): Handle input and output file names,
	-o and -remap.	Clean up -std= handling.
	* cppinit.c (COMMAND_LINE_OPTIONS): Remove OPT_o and OPT_remap.
	(cpp_handle_option): Similarly.  Don't handle filenames.

Index: c-common.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-common.h,v
retrieving revision 1.149
diff -u -p -r1.149 c-common.h
--- c-common.h	10 Aug 2002 02:18:26 -0000	1.149
+++ c-common.h	10 Aug 2002 21:53:19 -0000
@@ -238,8 +238,7 @@ extern GTY(()) tree c_global_trees[CTI_M
 
 typedef enum c_language_kind
 {
-  clk_c,           /* A dialect of C: K&R C, ANSI/ISO C89, C2000,
-		       etc.  */
+  clk_c = 0,      /* A dialect of C: K&R C, ANSI/ISO C89, C2000, etc.  */
   clk_cplusplus   /* ANSI/ISO C++ */
 }
 c_language_kind;
Index: c-opts.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-opts.c,v
retrieving revision 1.5
diff -u -p -r1.5 c-opts.c
--- c-opts.c	10 Aug 2002 20:58:43 -0000	1.5
+++ c-opts.c	10 Aug 2002 21:53:20 -0000
@@ -34,7 +34,7 @@ Software Foundation, 59 Temple Place - S
 static cpp_options *cpp_opts;
 
 static void missing_arg PARAMS ((size_t));
-static size_t parse_option PARAMS ((const char *, int));
+static size_t find_opt PARAMS ((const char *, int));
 static void set_Wimplicit PARAMS ((int));
 static void complain_wrong_lang PARAMS ((size_t));
 static void write_langs PARAMS ((char *, int));
@@ -42,6 +42,7 @@ static void print_help PARAMS ((void));
 static void handle_OPT_d PARAMS ((const char *));
 static void set_std_cxx98 PARAMS ((int));
 static void set_std_c89 PARAMS ((int, int));
+static void set_std_c99 PARAMS ((int));
 
 #define CL_C_ONLY	(1 << 0) /* Only C.  */
 #define CL_OBJC_ONLY	(1 << 1) /* Only ObjC.  */
@@ -216,10 +217,11 @@ static void set_std_c89 PARAMS ((int, in
   OPT("lang-objc",              CL_ALL,   OPT_lang_objc)		     \
   OPT("nostdinc",               CL_ALL,   OPT_nostdinc)			     \
   OPT("nostdinc++",             CL_ALL,   OPT_nostdincplusplus)		     \
+  OPT("o",			CL_ALL | CL_ARG, OPT_o)                      \
   OPT("pedantic",		CL_ALL,   OPT_pedantic)			     \
   OPT("pedantic-errors",	CL_ALL,   OPT_pedantic_errors)		     \
   OPT("print-objc-runtime-info", CL_OBJC, OPT_print_objc_runtime_info)	     \
-  OPT("std=",			CL_ALL | CL_JOINED, OPT_std_bad)	     \
+  OPT("remap",			CL_ALL,   OPT_remap)                         \
   OPT("std=c++98",		CL_CXX,	  OPT_std_cplusplus98)		     \
   OPT("std=c89",		CL_C,     OPT_std_c89)			     \
   OPT("std=c99",		CL_C,     OPT_std_c99)			     \
@@ -294,7 +296,6 @@ missing_arg (opt_index)
     case OPT_fname_mangling:
     case OPT_ftabstop:
     case OPT_ftemplate_depth:
-    case OPT_std_bad:
     default:
       error ("missing argument to \"-%s\"", cl_options[opt_index].opt_text);
       break;
@@ -302,6 +303,10 @@ missing_arg (opt_index)
     case OPT_fconstant_string_class:
       error ("no class name specified with -fconstant-string-class=");
       break;
+
+    case OPT_o:
+      error ("missing filename after \"-%s\"", cl_options[opt_index].opt_text);
+      break;
     }
 }
 
@@ -314,7 +319,7 @@ missing_arg (opt_index)
    and -pedantic-errors.  Also, some options are only accepted by some
    languages.  */
 static size_t
-parse_option (input, lang_flag)
+find_opt (input, lang_flag)
      const char *input;
      int lang_flag;
 {
@@ -435,32 +440,32 @@ c_common_decode_option (argc, argv)
      int argc;
      char **argv;
 {
+  static int lang_flags[] = {CL_C_ONLY, CL_C, CL_CXX_ONLY, CL_CXX};
   size_t opt_index;
   const char *opt, *arg = 0;
   char *dup = 0;
   bool on = true;
-  int result, lang_flag;
+  int result;
   const struct cl_option *option;
   enum opt_code code;
 
-  result = cpp_handle_option (parse_in, argc, argv);
   opt = argv[0];
 
-  /* Until handling CPP stuff, ignore non-switches.  */
+  /* Interpret "-" or a non-switch as a file name.  */
   if (opt[0] != '-' || opt[1] == '\0')
-    return result;
-
-  switch (c_language)
     {
-    case clk_c:			lang_flag = (flag_objc
-					     ? CL_C
-					     : CL_C_ONLY);
-				break;
-    case clk_cplusplus:		lang_flag = (flag_objc
-					     ? CL_CXX
-					     : CL_CXX_ONLY);
-				break;
-    default:			abort ();
+      if (!cpp_opts->in_fname)
+	cpp_opts->in_fname = opt;
+      else if (!cpp_opts->out_fname)
+	cpp_opts->out_fname = opt;
+      else
+	{
+	  error ("too many filenames given.  Type %s --help for usage",
+		 progname);
+	  return argc;
+	}
+
+      return 1;
     }
 
   /* Drop the "no-" from negative switches.  */
@@ -477,8 +482,10 @@ c_common_decode_option (argc, argv)
       on = false;
     }
 
+  result = cpp_handle_option (parse_in, argc, argv);
+
   /* Skip over '-'.  */
-  opt_index = parse_option (opt + 1, lang_flag);
+  opt_index = find_opt (opt + 1, lang_flags[(c_language << 1) + flag_objc]);
   if (opt_index == N_OPTS)
     goto done;
 
@@ -1102,6 +1109,16 @@ c_common_decode_option (argc, argv)
       cpp_opts->no_standard_cplusplus_includes = 1;
       break;
 
+    case OPT_o:
+      if (!cpp_opts->out_fname)
+	cpp_opts->out_fname = arg;
+      else
+	{
+	  error ("output filename specified twice");
+	  result = argc;
+	}
+      break;
+
       /* We need to handle the -pedantic switches here, rather than in
 	 c_common_post_options, so that a subsequent -Wno-endif-labels
 	 is not overridden.  */
@@ -1117,29 +1134,19 @@ c_common_decode_option (argc, argv)
       print_struct_values = 1;
       break;
 
-    case OPT_std_bad:
-      error ("unknown standard \"%s\"", arg);
+    case OPT_remap:
+      cpp_opts->remap = 1;
       break;
 
-      /* Language standards.  We currently recognize:
-	 -std=iso9899:1990	same as -ansi
-	 -std=iso9899:199409	ISO C as modified in amend. 1
-	 -std=iso9899:1999	ISO C 99
-	 -std=c89		same as -std=iso9899:1990
-	 -std=c99		same as -std=iso9899:1999
-	 -std=gnu89		default, iso9899:1990 + gnu extensions
-	 -std=gnu99		iso9899:1999 + gnu extensions
-      */
-
     case OPT_std_cplusplus98:
     case OPT_std_gnuplusplus98:
-      set_std_cxx98 (code == OPT_std_cplusplus98);
+      set_std_cxx98 (code == OPT_std_cplusplus98 /* ISO */);
       break;
 
-    case OPT_std_iso9899_199409:
     case OPT_std_c89:
     case OPT_std_iso9899_1990:
-      set_std_c89 (code == OPT_std_iso9899_199409, true);
+    case OPT_std_iso9899_199409:
+      set_std_c89 (code == OPT_std_iso9899_199409 /* c94 */, true /* ISO */);
       break;
 
     case OPT_std_gnu89:
@@ -1150,25 +1157,12 @@ c_common_decode_option (argc, argv)
     case OPT_std_c9x:
     case OPT_std_iso9899_1999:
     case OPT_std_iso9899_199x:
-      cpp_set_lang (parse_in, CLK_STDC99);
-      flag_writable_strings = 0;
-      flag_no_asm = 1;
-      flag_no_nonansi_builtin = 1;
-      flag_noniso_default_format_attributes = 0;
-      flag_isoc99 = 1;
-      flag_isoc94 = 1;
-      flag_iso = 1;
+      set_std_c99 (true /* ISO */);
       break;
 
     case OPT_std_gnu99:
     case OPT_std_gnu9x:
-      cpp_set_lang (parse_in, CLK_GNUC99);
-      flag_writable_strings = 0;
-      flag_no_asm = 0;
-      flag_no_nonansi_builtin = 0;
-      flag_noniso_default_format_attributes = 1;
-      flag_isoc99 = 1;
-      flag_isoc94 = 1;
+      set_std_c99 (false /* ISO */);
       break;
 
     case OPT_trigraphs:
@@ -1259,6 +1253,21 @@ set_std_c89 (c94, iso)
   flag_noniso_default_format_attributes = !iso;
   flag_isoc94 = c94;
   flag_isoc99 = 0;
+  flag_writable_strings = 0;
+}
+
+/* Set the C 99 standard (without GNU extensions if ISO).  */
+static void
+set_std_c99 (iso)
+     int iso;
+{
+  cpp_set_lang (parse_in, iso ? CLK_STDC99: CLK_GNUC99);
+  flag_no_asm = iso;
+  flag_no_nonansi_builtin = iso;
+  flag_noniso_default_format_attributes = !iso;
+  flag_iso = iso;
+  flag_isoc99 = 1;
+  flag_isoc94 = 1;
   flag_writable_strings = 0;
 }
 
Index: cppinit.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cppinit.c,v
retrieving revision 1.253
diff -u -p -r1.253 cppinit.c
--- cppinit.c	10 Aug 2002 20:58:43 -0000	1.253
+++ cppinit.c	10 Aug 2002 21:53:20 -0000
@@ -1151,9 +1151,7 @@ new_pending_directive (pend, text, handl
   DEF_OPT("iprefix",                  no_pth, OPT_iprefix)                    \
   DEF_OPT("isystem",                  no_dir, OPT_isystem)                    \
   DEF_OPT("iwithprefix",              no_dir, OPT_iwithprefix)                \
-  DEF_OPT("iwithprefixbefore",        no_dir, OPT_iwithprefixbefore)          \
-  DEF_OPT("o",                        no_fil, OPT_o)                          \
-  DEF_OPT("remap",                    0,      OPT_remap)
+  DEF_OPT("iwithprefixbefore",        no_dir, OPT_iwithprefixbefore)
 
 #define DEF_OPT(text, msg, code) code,
 enum opt_code
@@ -1256,19 +1254,6 @@ cpp_handle_option (pfile, argc, argv)
   int i = 0;
   struct cpp_pending *pend = CPP_OPTION (pfile, pending);
 
-  /* Interpret "-" or a non-option as a file name.  */
-  if (argv[i][0] != '-' || argv[i][1] == '\0')
-    {
-      if (CPP_OPTION (pfile, in_fname) == NULL)
-	CPP_OPTION (pfile, in_fname) = argv[i];
-      else if (CPP_OPTION (pfile, out_fname) == NULL)
-	CPP_OPTION (pfile, out_fname) = argv[i];
-      else
-	cpp_error (pfile, DL_ERROR,
-		   "too many filenames. Type %s --help for usage info",
-		   progname);
-    }
-  else
     {
       enum opt_code opt_code;
       int opt_index;
@@ -1303,21 +1288,9 @@ cpp_handle_option (pfile, argc, argv)
 	case OPT_D:
 	  new_pending_directive (pend, arg, cpp_define);
 	  break;
-	case OPT_remap:
-	  CPP_OPTION (pfile, remap) = 1;
-	  break;
 	case OPT_iprefix:
 	  CPP_OPTION (pfile, include_prefix) = arg;
 	  CPP_OPTION (pfile, include_prefix_len) = strlen (arg);
-	  break;
-	case OPT_o:
-	  if (CPP_OPTION (pfile, out_fname) == NULL)
-	    CPP_OPTION (pfile, out_fname) = arg;
-	  else
-	    {
-	      cpp_error (pfile, DL_ERROR, "output filename specified twice");
-	      return argc;
-	    }
 	  break;
 
 	case OPT_MG:



More information about the Gcc-patches mailing list