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]
Other format: [Raw text]

Re: PATCH: -save-temps still produces different .s and .o


Alexandre Oliva wrote:

It fails to generate a directory entry in the following case:

./xgcc -B./ -E < /dev/null |
./xgcc -B./ -E -fpreprocessed -fworking-dir -

I agree it's not all that important, but it seems reasonable to me,
and it worked before.  The fix, again, is to not add the pointless
pp_dir_change callback, and emit a file change callback instead.  Is
there any reason for you to not want it to be done the way it was
before?

Because that adds a spurius line_map entry, which complicates/confuses things. Not only a spurious line_map entry, but a bogus one, since it contained a dangling pointer to an alloca's string. (That is obviously fixable by copying the string, so it's less important than that it complicates/constraints the line_maps invariants.)

To do what you want, we should have a directory directive do nothing
except call set_src_pwd.  Then if -E is set, we (maybe later) emit a
directory directive.  Since we don't want to former to create a
line_maps entry or emit a duplicate directory directive, we must
suppress re-reading the directory directive.  I.e. don't back up.

As a bonus, we can remove handing of the working_directory flag in
cpplib.

I'm going to start a bootstrap and test now.
--
	--Per Bothner
per@bothner.com   http://per.bothner.com/

Index: ChangeLog
===================================================================
RCS file: /cvs/gcc/gcc/gcc/ChangeLog,v
retrieving revision 2.2457
diff -u -p -r2.2457 ChangeLog
--- ChangeLog	24 Jan 2004 15:31:04 -0000	2.2457
+++ ChangeLog	25 Jan 2004 02:41:14 -0000
@@ -1,3 +1,29 @@
+2004-01-24  Per Bothner  <per@bothner.com>
+
+[Needs to be revised.]
+	Partially revert/redo 2003-10-01 change; fix -fworking-directory.
+	* c-ppoutput.c (pp_dir_change):  New function.
+	* c-common.h (pp_dir_change):  New declaration.
+	* cppinit.c (cpp_find_main_file, cpp_push_main_file):  Merge back to
+	(cpp_read_main_file):  as before 10-01.  Call _cpp_stack_file.
+	To handle -fworking_directory, just call dir_change callback,
+	which calls pp_dir_change if flag_preprocess_only.
+	* cpplib.h.h:  Update declarations to match.
+	* c-opts.c (c_common_post_options):  Maybe set dir_change callback.
+	(finish_options):  Don't call cpp_find_main_file here.	Hence remove
+	unneeded parameter and result. Do LC_RENAME for <built-in>.
+	(c_common_post_options):  Call cpp_read_main_file here instead.
+	(c_common_init):  Update accordingly.
+	(push_command_line_include):  Don't cpp_push_main_file.
+	Do LC_RENAME rather than LC_LEASE to get back to main file.
+	Compared to pre-10-01 version, inline cpp_rename_to_main_file .
+	(c_common_parse_file):  Call cpp_read_main_file for subsequent main
+	files, but call finish_options for all files.
+	* fix-header.c (read_scan_file):  Call cpp_read_main_file instead of
+	cpp_find_main_file + cpp_push_main_file.
+	* c-lex.c (fe_file_change):  Don't set main_input_filename here.
+	* opts.c (handle_options):  Only set main_input_filename first time.
+
 2004-01-24  Jan Hubicka  <jh@suse.cz>
 
 	* emit-rtl.c (change_address, adjust_address_1, offset_address,
Index: c-common.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-common.h,v
retrieving revision 1.217
diff -u -p -r1.217 c-common.h
--- c-common.h	21 Jan 2004 20:39:51 -0000	1.217
+++ c-common.h	25 Jan 2004 02:41:15 -0000
@@ -1340,5 +1340,6 @@ extern void objc_mark_locals_volatile (v
 extern void init_pp_output (FILE *);
 extern void preprocess_file (cpp_reader *);
 extern void pp_file_change (const struct line_map *);
+extern void pp_dir_change (cpp_reader *, const char *);
 
 #endif /* ! GCC_C_COMMON_H */
Index: c-lex.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-lex.c,v
retrieving revision 1.215
diff -u -p -r1.215 c-lex.c
--- c-lex.c	2 Oct 2003 07:03:41 -0000	1.215
+++ c-lex.c	25 Jan 2004 02:41:15 -0000
@@ -69,7 +69,6 @@ static tree lex_charconst (const cpp_tok
 static void update_header_times (const char *);
 static int dump_one_header (splay_tree_node, void *);
 static void cb_line_change (cpp_reader *, const cpp_token *, int);
-static void cb_dir_change (cpp_reader *, const char *);
 static void cb_ident (cpp_reader *, unsigned int, const cpp_string *);
 static void cb_def_pragma (cpp_reader *, unsigned int);
 static void cb_define (cpp_reader *, unsigned int, cpp_hashnode *);
@@ -96,7 +95,6 @@ init_c_lex (void)
   cb = cpp_get_callbacks (parse_in);
 
   cb->line_change = cb_line_change;
-  cb->dir_change = cb_dir_change;
   cb->ident = cb_ident;
   cb->def_pragma = cb_def_pragma;
   cb->valid_pch = c_common_valid_pch;
@@ -202,13 +200,6 @@ cb_line_change (cpp_reader *pfile ATTRIB
   input_line = SOURCE_LINE (map, token->line);
 }
 
-static void
-cb_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
-{
-  if (! set_src_pwd (dir))
-    warning ("too late for # directive to set debug directory");
-}
-
 void
 fe_file_change (const struct line_map *new_map)
 {
@@ -222,9 +213,7 @@ fe_file_change (const struct line_map *n
     {
       /* Don't stack the main buffer on the input stack;
 	 we already did in compile_file.  */
-      if (map == NULL)
-	main_input_filename = new_map->to_file;
-      else
+      if (map != NULL)
 	{
           int included_at = SOURCE_LINE (new_map - 1, new_map->from_line - 1);
 
Index: c-opts.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-opts.c,v
retrieving revision 1.98
diff -u -p -r1.98 c-opts.c
--- c-opts.c	21 Jan 2004 20:39:51 -0000	1.98
+++ c-opts.c	25 Jan 2004 02:41:15 -0000
@@ -108,7 +108,8 @@ static void sanitize_cpp_opts (void);
 static void add_prefixed_path (const char *, size_t);
 static void push_command_line_include (void);
 static void cb_file_change (cpp_reader *, const struct line_map *);
-static bool finish_options (const char *);
+static void cb_dir_change (cpp_reader *, const char *);
+static void finish_options (void);
 
 #ifndef STDC_0_IN_SYSTEM_HEADERS
 #define STDC_0_IN_SYSTEM_HEADERS 0
@@ -1049,8 +1050,10 @@ c_common_handle_option (size_t scode, co
 
 /* Post-switch processing.  */
 bool
-c_common_post_options (const char **pfilename ATTRIBUTE_UNUSED)
+c_common_post_options (const char **pfilename)
 {
+  struct cpp_callbacks *cb;
+
   /* Canonicalize the input and output filenames.  */
   if (in_fnames == NULL)
     {
@@ -1138,7 +1141,9 @@ c_common_post_options (const char **pfil
       input_line = 0;
     }
 
-  cpp_get_callbacks (parse_in)->file_change = cb_file_change;
+  cb = cpp_get_callbacks (parse_in);
+  cb->file_change = cb_file_change;
+  cb->dir_change = cb_dir_change;
   cpp_post_options (parse_in);
 
   saved_lineno = input_line;
@@ -1148,6 +1153,14 @@ c_common_post_options (const char **pfil
      immediately.  */
   errorcount += cpp_errors (parse_in);
 
+  *pfilename = this_input_filename
+    = cpp_read_main_file (parse_in, in_fnames[0]);
+  if (this_input_filename == NULL)
+    return true;
+
+  if (flag_preprocess_only && flag_working_directory)
+    pp_dir_change (parse_in, get_src_pwd ());
+
   return flag_preprocess_only;
 }
 
@@ -1172,8 +1185,8 @@ c_common_init (void)
 
   if (flag_preprocess_only)
     {
-      if (finish_options (in_fnames[0]))
-	preprocess_file (parse_in);
+      finish_options ();
+      preprocess_file (parse_in);
       return false;
     }
 
@@ -1207,10 +1220,12 @@ c_common_parse_file (int set_yydebug ATT
 
 	  /* Reset cpplib's macros and start a new file.  */
 	  cpp_undef_all (parse_in);
+	  main_input_filename = this_input_filename
+	    = cpp_read_main_file (parse_in, in_fnames[file_index]);
+	  if (this_input_filename == NULL)
+	    break;
 	}
-
-      if (! finish_options(in_fnames[file_index]))
-	break;
+      finish_options ();
       if (file_index == 0)
 	pch_init();
       c_parse_file ();
@@ -1351,8 +1366,6 @@ sanitize_cpp_opts (void)
      actually output the current directory?  */
   if (flag_working_directory == -1)
     flag_working_directory = (debug_info_level != DINFO_LEVEL_NONE);
-  cpp_opts->working_directory
-    = flag_preprocess_only && flag_working_directory;
 }
 
 /* Add include path with a prefix at the front of its name.  */
@@ -1375,21 +1388,15 @@ add_prefixed_path (const char *suffix, s
   add_path (path, chain, 0);
 }
 
-/* Handle -D, -U, -A, -imacros, and the first -include.  
-   TIF is the input file to which we will return after processing all
-   the includes.  Returns true on success.  */
-static bool
-finish_options (const char *tif)
+/* Handle -D, -U, -A, -imacros, and the first -include.  */
+static void
+finish_options (void)
 {
-  this_input_filename = tif;
-  if (! cpp_find_main_file (parse_in, this_input_filename))
-    return false;
-
   if (!cpp_opts->preprocessed)
     {
       size_t i;
 
-      cpp_change_file (parse_in, LC_ENTER, _("<built-in>"));
+      cpp_change_file (parse_in, LC_RENAME, _("<built-in>"));
       cpp_init_builtins (parse_in, flag_hosted);
       c_cpp_builtins (parse_in);
 
@@ -1439,7 +1446,6 @@ finish_options (const char *tif)
 
   include_cursor = 0;
   push_command_line_include ();
-  return true;
 }
 
 /* Give CPP the next file given by -include, if any.  */
@@ -1458,12 +1464,15 @@ push_command_line_include (void)
   if (include_cursor == deferred_count)
     {
       include_cursor++;
-      /* Restore the line map from <command line>.  */
-      if (! cpp_opts->preprocessed)
-	cpp_change_file (parse_in, LC_LEAVE, NULL);
       /* -Wunused-macros should only warn about macros defined hereafter.  */
       cpp_opts->warn_unused_macros = warn_unused_macros;
-      cpp_push_main_file (parse_in);
+      /* Restore the line map from <command line>.  */
+      if (! cpp_opts->preprocessed)
+	cpp_change_file (parse_in, LC_RENAME, main_input_filename);
+
+      /* Set this here so the client can change the option if it wishes,
+	 and after stacking the main file so we don't trace the main file.  */
+      line_table.trace_includes = cpp_opts->print_include_names;
     }
 }
 
@@ -1479,6 +1488,13 @@ cb_file_change (cpp_reader *pfile ATTRIB
 
   if (new_map == 0 || (new_map->reason == LC_LEAVE && MAIN_FILE_P (new_map)))
     push_command_line_include ();
+}
+
+void
+cb_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
+{
+  if (! set_src_pwd (dir))
+    warning ("too late for # directive to set debug directory");
 }
 
 /* Set the C 89 standard (with 1994 amendments if C94, without GNU
Index: c-ppoutput.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-ppoutput.c,v
retrieving revision 1.11
diff -u -p -r1.11 c-ppoutput.c
--- c-ppoutput.c	2 Oct 2003 07:03:41 -0000	1.11
+++ c-ppoutput.c	25 Jan 2004 02:41:16 -0000
@@ -334,6 +334,22 @@ cb_include (cpp_reader *pfile ATTRIBUTE_
   print.line++;
 }
 
+/* Callback called when -fworking-director and -E to emit working
+   diretory in cpp output file. */
+
+void
+pp_dir_change (cpp_reader *pfile ATTRIBUTE_UNUSED, const char *dir)
+{
+  size_t to_file_len = strlen (dir);
+  unsigned char *to_file_quoted = alloca (to_file_len * 4 + 1);
+  unsigned char *p;
+
+  /* cpp_quote_string does not nul-terminate, so we have to do it ourselves. */
+  p = cpp_quote_string (to_file_quoted, (unsigned char *) dir, to_file_len);
+  *p = '\0';
+  fprintf (print.outf, "# 1 \"%s//\"\n", to_file_quoted);
+}
+
 /* The file name, line number or system header flags have changed, as
    described in MAP.  From this point on, the old print.map might be
    pointing to freed memory, and so must not be dereferenced.  */
Index: cppinit.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cppinit.c,v
retrieving revision 1.300
diff -u -p -r1.300 cppinit.c
--- cppinit.c	21 Jan 2004 20:39:52 -0000	1.300
+++ cppinit.c	25 Jan 2004 02:41:16 -0000
@@ -453,9 +453,10 @@ cpp_post_options (cpp_reader *pfile)
 }
 
 /* Setup for processing input from the file named FNAME, or stdin if
-   it is the empty string.  Returns true if the file was found.  */
-bool
-cpp_find_main_file (cpp_reader *pfile, const char *fname)
+   it is the empty string.  Return the original filename
+   on success (e.g. foo.i->foo.c), or NULL on failure.  */
+const char *
+cpp_read_main_file (cpp_reader *pfile, const char *fname)
 {
   if (CPP_OPTION (pfile, deps.style) != DEPS_NONE)
     {
@@ -471,37 +472,17 @@ cpp_find_main_file (cpp_reader *pfile, c
   if (_cpp_find_failed (pfile->main_file))
     return false;
 
-  if (CPP_OPTION (pfile, working_directory))
-    {
-      const char *dir = getpwd ();
-      char *dir_with_slashes = alloca (strlen (dir) + 3);
-
-      memcpy (dir_with_slashes, dir, strlen (dir));
-      memcpy (dir_with_slashes + strlen (dir), "//", 3);
-
-      if (pfile->cb.dir_change)
-	pfile->cb.dir_change (pfile, dir);
-    }
-  return true;
-}
-
-/* This function reads the file, but does not start preprocessing.
-   This will generate at least one file change callback, and possibly
-   a line change callback.  */
-void
-cpp_push_main_file (cpp_reader *pfile)
-{
   _cpp_stack_file (pfile, pfile->main_file, false);
+  pfile->line++;
 
   /* For foo.i, read the original filename foo.c now, for the benefit
      of the front ends.  */
   if (CPP_OPTION (pfile, preprocessed))
-    read_original_filename (pfile);
-
-  /* Set this here so the client can change the option if it wishes,
-     and after stacking the main file so we don't trace the main
-     file.  */
-  pfile->line_table->trace_includes = CPP_OPTION (pfile, print_include_names);
+    {
+      read_original_filename (pfile);
+      fname = pfile->map->to_file;
+    }
+  return fname;
 }
 
 /* For preprocessed files, if the first tokens are of the form # NUM.
@@ -580,12 +561,6 @@ read_original_directory (cpp_reader *pfi
 
       pfile->cb.dir_change (pfile, debugdir);
     }      
-
-  /* We want to process the fake line changes as regular changes, to
-     get them output.  */
-  _cpp_backup_tokens (pfile, 3);
-
-  CPP_OPTION (pfile, working_directory) = false;
 }
 
 /* This is called at the end of preprocessing.  It pops the last
Index: cpplib.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cpplib.h,v
retrieving revision 1.273
diff -u -p -r1.273 cpplib.h
--- cpplib.h	21 Jan 2004 20:39:53 -0000	1.273
+++ cpplib.h	25 Jan 2004 02:41:16 -0000
@@ -373,11 +373,6 @@ struct cpp_options
 
   /* Nonzero means __STDC__ should have the value 0 in system headers.  */
   unsigned char stdc_0_in_system_headers;
-
-  /* Nonzero means output a directory line marker right after the
-     initial file name line marker, and before a duplicate initial
-     line marker.  */
-  bool working_directory;
 };
 
 /* Call backs to cpplib client.  */
@@ -536,14 +531,12 @@ extern const struct line_maps *cpp_get_l
 extern cpp_callbacks *cpp_get_callbacks (cpp_reader *);
 extern void cpp_set_callbacks (cpp_reader *, cpp_callbacks *);
 
-/* This function finds the main file, but does not start reading it.
-   Returns true iff the file was found.  */
-extern bool cpp_find_main_file (cpp_reader *, const char *);
-
-/* This function reads the file, but does not start preprocessing.
-   This will generate at least one file change callback, and possibly
-   a line change callback.  */
-extern void cpp_push_main_file (cpp_reader *);
+/* This function reads the file, but does not start preprocessing.  It
+   returns the name of the original file; this is the same as the
+   input file, except for preprocessed input.  This will generate at
+   least one file change callback, and possibly a line change callback
+   too.  If there was an error opening the file, it returns NULL. */
+extern const char *cpp_read_main_file (cpp_reader *, const char *);
 
 /* Set up built-ins like __FILE__.  */
 extern void cpp_init_builtins (cpp_reader *, int);
Index: fix-header.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/fix-header.c,v
retrieving revision 1.106
diff -u -p -r1.106 fix-header.c
--- fix-header.c	21 Jan 2004 20:39:54 -0000	1.106
+++ fix-header.c	25 Jan 2004 02:41:17 -0000
@@ -606,9 +606,8 @@ read_scan_file (char *in_fname, int argc
   options->inhibit_errors = 1;
   cpp_post_options (scan_in);
 
-  if (!cpp_find_main_file (scan_in, in_fname))
+  if (!cpp_read_main_file (scan_in, in_fname))
     exit (FATAL_EXIT_CODE);
-  cpp_push_main_file (scan_in);
 
   cpp_change_file (scan_in, LC_RENAME, "<built-in>");
   cpp_init_builtins (scan_in, true);
Index: opts.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/opts.c,v
retrieving revision 1.51
diff -u -p -r1.51 opts.c
--- opts.c	1 Jan 2004 13:58:57 -0000	1.51
+++ opts.c	25 Jan 2004 02:41:17 -0000
@@ -448,7 +448,8 @@ handle_options (unsigned int argc, const
       /* Interpret "-" or a non-switch as a file name.  */
       if (opt[0] != '-' || opt[1] == '\0')
 	{
-	  main_input_filename = opt;
+	  if (main_input_filename == NULL)
+	    main_input_filename = opt;
 	  add_input_filename (opt);
 	  n = 1;
 	  continue;

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