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: Clean up input / output filenames


At present, cpplib is pretty inconsistent about what its input and
output filenames are, and how {stdin, stdout} are encoded.  As a result,
there was code scattered around cpplib checking file names for NULLs,
and ugliness in the option handling code checking for "-".  Moreover,
doing odd things like specifying the output filename with "-o" before
the input filename would break the option handling.

This puts the confusion to rest, with cpp_post_options guaranteed to
return non-null filenames for both input and output, with "" representing
{stdin, stdout}.  Then, cpp_start_read takes a file name to read, with the
same encoding, and does not reference the name in the options structure.

A bit further down the line, I think we move towards a scheme that Zack
suggested where we scrap cpp_start_read altogether, and have the library
client interpret non-options on the command line.  This is a step in
that direction at least.

Neil.

	* cppfiles.c: Update comments.
	(_cpp_read_file): Don't check for NULL filenames any more.
	* cppinit.c (cpp_start_read): Don't do canonicalization of
	in_fname and out_fname.  Use the passed file name exclusively.
	(_cpp_handle_options): Don't treat "-" as a command line option,
	but as a normal filename.
	(_cpp_post_options): Canonicalize in_fname and out_fname.
	* cppmain.c (printer_init): Don't check out_fname for NULL.
	* c-lex.c (orig_filename): Rename cpp_filename for clarity.
	(init_c_lex): Update, and use "" to represent stdin to CPP.
	(yyparse): Update.

Index: cppfiles.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cppfiles.c,v
retrieving revision 1.103
diff -u -p -r1.103 cppfiles.c
--- cppfiles.c	2001/02/19 19:50:21	1.103
+++ cppfiles.c	2001/02/20 23:45:11
@@ -174,7 +174,8 @@ _cpp_fake_include (pfile, fname)
    create one with a non-NULL value (regardless of success in opening
    the file).  If the file doesn't exist or is inaccessible, this
    entry is flagged so we don't attempt to open it again in the
-   future.  If the file isn't open, open it.
+   future.  If the file isn't open, open it.  The empty string is
+   interpreted as stdin.
 
    Returns an include_file structure with an open file descriptor on
    success, or NULL on failure.  */
@@ -755,18 +756,13 @@ _cpp_compare_file_date (pfile, f)
 
 
 /* Push an input buffer and load it up with the contents of FNAME.
-   If FNAME is "" or NULL, read standard input.  */
+   If FNAME is "", read standard input.  */
 int
 _cpp_read_file (pfile, fname)
      cpp_reader *pfile;
      const char *fname;
 {
-  struct include_file *f;
-
-  if (fname == NULL)
-    fname = "";
-
-  f = open_file (pfile, fname);
+  struct include_file *f = open_file (pfile, fname);
 
   if (f == NULL)
     {
Index: cppinit.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cppinit.c,v
retrieving revision 1.148
diff -u -p -r1.148 cppinit.c
--- cppinit.c	2001/02/20 22:52:11	1.148
+++ cppinit.c	2001/02/20 23:45:15
@@ -882,9 +882,8 @@ do_includes (pfile, p, scan)
 }
 
 /* This is called after options have been processed.  Setup for
-   processing input from the file named FNAME.  (Use standard input if
-   FNAME == NULL.)  Return 1 on success, 0 on failure.  */
-
+   processing input from the file named FNAME, or stdin if it is the
+   empty string.  Return 1 on success, 0 on failure.  */
 int
 cpp_start_read (pfile, fname)
      cpp_reader *pfile;
@@ -912,19 +911,9 @@ cpp_start_read (pfile, fname)
       fprintf (stderr, _("End of search list.\n"));
     }
 
-  if (CPP_OPTION (pfile, in_fname) == NULL
-      || *CPP_OPTION (pfile, in_fname) == 0)
-    {
-      CPP_OPTION (pfile, in_fname) = fname;
-      if (CPP_OPTION (pfile, in_fname) == NULL)
-	CPP_OPTION (pfile, in_fname) = "";
-    }
-  if (CPP_OPTION (pfile, out_fname) == NULL)
-    CPP_OPTION (pfile, out_fname) = "";
-
   if (CPP_OPTION (pfile, print_deps))
     /* Set the default target (if there is none already).  */
-    deps_add_default_target (pfile->deps, CPP_OPTION (pfile, in_fname));
+    deps_add_default_target (pfile->deps, fname);
 
   /* Open the main input file.  This must be done early, so we have a
      buffer to stand on.  */
@@ -1051,7 +1040,6 @@ new_pending_directive (pend, text, handl
 /* This is the list of all command line options, with the leading
    "-" removed.  It must be sorted in ASCII collating order.  */
 #define COMMAND_LINE_OPTIONS                                                  \
-  DEF_OPT("",                         0,      OPT_stdin_stdout)               \
   DEF_OPT("$",                        0,      OPT_dollar)                     \
   DEF_OPT("+",                        0,      OPT_plus)                       \
   DEF_OPT("-help",                    0,      OPT__help)                      \
@@ -1224,15 +1212,16 @@ cpp_handle_option (pfile, argc, argv)
   int i = 0;
   struct cpp_pending *pend = CPP_OPTION (pfile, pending);
 
-  if (argv[i][0] != '-')
+  /* Interpret "-" or a non-option as a file name.  */
+  if (argv[i][0] != '-' || argv[i][1] == '\0')
     {
-      if (CPP_OPTION (pfile, out_fname) != NULL)
-	cpp_fatal (pfile, "Too many arguments. Type %s --help for usage info",
-		   progname);
-      else if (CPP_OPTION (pfile, in_fname) != NULL)
+      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_OPTION (pfile, in_fname) = argv[i];
+	cpp_fatal (pfile, "Too many filenames. Type %s --help for usage info",
+		   progname);
     }
   else
     {
@@ -1415,21 +1404,13 @@ cpp_handle_option (pfile, argc, argv)
 	  CPP_OPTION (pfile, no_standard_cplusplus_includes) = 1;
 	  break;
 	case OPT_o:
-	  if (CPP_OPTION (pfile, out_fname) != NULL)
+	  if (CPP_OPTION (pfile, out_fname) == NULL)
+	    CPP_OPTION (pfile, out_fname) = arg;
+	  else
 	    {
 	      cpp_fatal (pfile, "Output filename specified twice");
 	      return argc;
 	    }
-	  CPP_OPTION (pfile, out_fname) = arg;
-	  if (!strcmp (CPP_OPTION (pfile, out_fname), "-"))
-	    CPP_OPTION (pfile, out_fname) = "";
-	  break;
-	case OPT_stdin_stdout:
-	  /* JF handle '-' as file name meaning stdin or stdout.  */
-	  if (CPP_OPTION (pfile, in_fname) == NULL)
-	    CPP_OPTION (pfile, in_fname) = "";
-	  else if (CPP_OPTION (pfile, out_fname) == NULL)
-	    CPP_OPTION (pfile, out_fname) = "";
 	  break;
 	case OPT_d:
 	  /* Args to -d specify what parts of macros to dump.
@@ -1693,6 +1674,16 @@ void
 cpp_post_options (pfile)
      cpp_reader *pfile;
 {
+  /* Canonicalize in_fname and out_fname.  We guarantee they are not
+     NULL, and that the empty string represents stdin / stdout.  */
+  if (CPP_OPTION (pfile, in_fname) == NULL
+      || !strcmp (CPP_OPTION (pfile, in_fname), "-"))
+    CPP_OPTION (pfile, in_fname) = "";
+
+  if (CPP_OPTION (pfile, out_fname) == NULL
+      || !strcmp (CPP_OPTION (pfile, out_fname), "-"))
+    CPP_OPTION (pfile, out_fname) = "";
+
   /* -Wtraditional is not useful in C++ mode.  */
   if (CPP_OPTION (pfile, cplusplus))
     CPP_OPTION (pfile, warn_traditional) = 0;
Index: cppmain.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cppmain.c,v
retrieving revision 1.61
diff -u -p -r1.61 cppmain.c
--- cppmain.c	2001/02/01 19:13:53	1.61
+++ cppmain.c	2001/02/20 23:45:17
@@ -271,9 +271,6 @@ printer_init (pfile)
   print.lineno = 0;
   print.printed = 0;
 
-  if (options->out_fname == NULL)
-    options->out_fname = "";
-  
   if (options->out_fname[0] == '\0')
     print.outf = stdout;
   else
Index: c-lex.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-lex.c,v
retrieving revision 1.130
diff -u -p -r1.130 c-lex.c
--- c-lex.c	2001/02/14 07:38:19	1.130
+++ c-lex.c	2001/02/20 23:45:23
@@ -53,8 +53,8 @@ Boston, MA 02111-1307, USA.  */
 #define GET_ENVIRONMENT(ENV_VALUE,ENV_NAME) ((ENV_VALUE) = getenv (ENV_NAME))
 #endif
 
-/* The original file name, before changing "-" to "stdin".  */
-static const char *orig_filename;
+/* The input filename as understood by CPP, where "" represents stdin.  */
+static const char *cpp_filename;
 
 /* We may keep statistics about how long which files took to compile.  */
 static int header_time, body_time;
@@ -102,8 +102,6 @@ init_c_lex (filename)
   struct cpp_callbacks *cb;
   struct c_fileinfo *toplevel;
 
-  orig_filename = filename;
-
   /* Set up filename timing.  Must happen before cpp_start_read.  */
   file_info_tree = splay_tree_new ((splay_tree_compare_fn)strcmp,
 				   0,
@@ -136,8 +134,11 @@ init_c_lex (filename)
       cb->undef = cb_undef;
     }
 
+
   if (filename == 0 || !strcmp (filename, "-"))
-    filename = "stdin";
+    filename = "stdin", cpp_filename = "";
+  else
+    cpp_filename = filename;
 
   /* Start it at 0.  */
   lineno = 0;
@@ -151,7 +152,7 @@ init_c_lex (filename)
 int
 yyparse()
 {
-  if (! cpp_start_read (parse_in, orig_filename))
+  if (! cpp_start_read (parse_in, cpp_filename))
     return 1;			/* cpplib has emitted an error.  */
 
   return yyparse_1();


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