On Jul 16, 2003, Alexandre Oliva <[EMAIL PROTECTED]> wrote:

> On Jul 14, 2003, Alexandre Oliva <[EMAIL PROTECTED]> wrote:
>> I don't see anything clear about it.  -f to me is an option to the
>> compiler.  This one isn't.  It's an option to the preprocessor.  I'm
>> currently thinking -Pwd would be the best choice, since -P has to do
>> with printing line markers, but If you care so much about -fpwd, I'll
>> go with that.

> Ping?

No response, so I assume -Pwd is a reasonable option.  Here's a
patch, bootstrapped on i686-pc-linux-gnu.  Ok to install?

Index: gcc/ChangeLog
from  Alexandre Oliva  <[EMAIL PROTECTED]>

	* c.opt: Introduce -Pwd.
	* gcc.c (cpp_unique_options): Pass it.
	* c-opts.c (c_common_handle_options): Set...
	* cpplib.h (struct cpp_options): ... current_directory option.
	(struct cpp_callbacks): Add dir_change.
	* cppinit.c (read_original_filename): Call...
	(read_original_directory): New.  Look for # 1 "directory//"
	and process it.
	(cpp_read_main_file): Call dir_change callback if current_directory
	option is set.
	* toplev.c (src_pwd): New static variable.
	(set_src_pwd, get_src_pwd): New functions.
	* toplev.h (get_src_pwd, set_src_pwd): Declare.
	* dbxout.c (dbxout_init): Call get_src_pwd() instead of getpwd().
	* dwarf2out.c (gen_compile_unit_die): Likewise.
	* dwarfout.c (output_compile_unit_die, dwarfout_init): Likewise.
	* c-lex.c (cb_dir_change): New.
	(init_c_lex): Set dir_change callback.
	* doc/cpp.texi, doc/invoke.texi: Add -Pwd.
	* doc/cppopts.texi: Document it.

Index: gcc/c-lex.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-lex.c,v
retrieving revision 1.210
diff -u -p -r1.210 c-lex.c
--- gcc/c-lex.c 29 Jul 2003 23:36:46 -0000 1.210
+++ gcc/c-lex.c 3 Aug 2003 12:10:36 -0000
@@ -72,6 +72,7 @@ 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 *);
@@ -98,6 +99,7 @@ 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;
@@ -198,6 +200,13 @@ cb_line_change (cpp_reader *pfile ATTRIB
 		int parsing_args ATTRIBUTE_UNUSED)
 {
   src_lineno = 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
Index: gcc/c-opts.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-opts.c,v
retrieving revision 1.84
diff -u -p -r1.84 c-opts.c
--- gcc/c-opts.c 1 Aug 2003 14:04:02 -0000 1.84
+++ gcc/c-opts.c 3 Aug 2003 12:10:37 -0000
@@ -212,6 +212,7 @@ c_common_init_options (unsigned int argc
   /* Reset to avoid warnings on internal definitions.  We set it just
      before passing on command-line options to cpplib.  */
   cpp_opts->warn_dollars = 0;
+  cpp_opts->current_directory = true;
 
   flag_const_strings = c_dialect_cxx ();
   flag_exceptions = c_dialect_cxx ();
@@ -333,6 +334,10 @@ c_common_handle_option (size_t scode, co
 
     case OPT_P:
       flag_no_line_commands = 1;
+      break;
+
+    case OPT_Pwd:
+      cpp_opts->current_directory = false;
       break;
 
     case OPT_U:
Index: gcc/c.opt
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c.opt,v
retrieving revision 1.13
diff -u -p -r1.13 c.opt
--- gcc/c.opt 1 Aug 2003 14:04:02 -0000 1.13
+++ gcc/c.opt 3 Aug 2003 12:10:37 -0000
@@ -141,6 +141,10 @@ P
 C ObjC C++ ObjC++
 Do not generate #line directives
 
+Pwd
+C ObjC C++ ObjC++
+Do not generate #line directives for the current working directory
+
 U
 C ObjC C++ ObjC++ Joined Separate
 -U<macro>	Undefine <macro>
Index: gcc/cppinit.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cppinit.c,v
retrieving revision 1.291
diff -u -p -r1.291 cppinit.c
--- gcc/cppinit.c 1 Aug 2003 14:04:02 -0000 1.291
+++ gcc/cppinit.c 3 Aug 2003 12:10:38 -0000
@@ -28,6 +28,7 @@ Foundation, 59 Temple Place - Suite 330,
 static void init_library (void);
 static void mark_named_operators (cpp_reader *);
 static void read_original_filename (cpp_reader *);
+static void read_original_directory (cpp_reader *);
 static void post_options (cpp_reader *);
 
 /* If we have designated initializers (GCC >2.7) these tables can be
@@ -470,6 +471,24 @@ cpp_read_main_file (cpp_reader *pfile, c
   if (CPP_OPTION (pfile, preprocessed))
     read_original_filename (pfile);
 
+  if (CPP_OPTION (pfile, current_directory))
+    {
+      const char *name = pfile->map->to_file;
+      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);
+      /* Emit file renames that will be recognized by
+	 read_directory_filename, since dir_change doesn't output
+	 anything.  */
+      _cpp_do_file_change (pfile, LC_RENAME, dir_with_slashes, 1, 0);
+      _cpp_do_file_change (pfile, LC_RENAME, name, 1, 0);
+    }
+
   return pfile->map->to_file;
 }
 
@@ -494,12 +513,67 @@ read_original_filename (cpp_reader *pfil
       if (token1->type == CPP_NUMBER)
 	{
 	  _cpp_handle_directive (pfile, token->flags & PREV_WHITE);
+	  read_original_directory (pfile);
 	  return;
 	}
     }
 
   /* Backup as if nothing happened.  */
   _cpp_backup_tokens (pfile, 1);
+}
+
+/* For preprocessed files, if the tokens following the first filename
+   line is of the form # <line> "/path/name//", handle the
+   directive so we know the original current directory.  */
+static void
+read_original_directory (cpp_reader *pfile)
+{
+  const cpp_token *hash, *token;
+
+  /* Lex ahead; if the first tokens are of the form # NUM, then
+     process the directive, otherwise back up.  */
+  hash = _cpp_lex_direct (pfile);
+  if (hash->type != CPP_HASH)
+    {
+      _cpp_backup_tokens (pfile, 1);
+      return;
+    }
+
+  token = _cpp_lex_direct (pfile);
+
+  if (token->type != CPP_NUMBER)
+    {
+      _cpp_backup_tokens (pfile, 2);
+      return;
+    }
+
+  token = _cpp_lex_direct (pfile);
+
+  if (token->type != CPP_STRING
+      || ! (token->val.str.len >= 5
+	    && token->val.str.text[token->val.str.len-2] == '/'
+	    && token->val.str.text[token->val.str.len-3] == '/'))
+    {
+      _cpp_backup_tokens (pfile, 3);
+      return;
+    }
+
+  if (pfile->cb.dir_change)
+    {
+      char *debugdir = alloca (token->val.str.len - 3);
+
+      memcpy (debugdir, (const char *) token->val.str.text + 1,
+	      token->val.str.len - 4);
+      debugdir[token->val.str.len - 4] = '\0';
+
+      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, current_directory) = false;
 }
 
 /* This is called at the end of preprocessing.  It pops the last
Index: gcc/cpplib.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cpplib.h,v
retrieving revision 1.263
diff -u -p -r1.263 cpplib.h
--- gcc/cpplib.h 1 Aug 2003 14:04:02 -0000 1.263
+++ gcc/cpplib.h 3 Aug 2003 12:10:38 -0000
@@ -370,6 +370,11 @@ 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 current_directory;
 };
 
 /* Call backs to cpplib client.  */
@@ -378,6 +383,7 @@ struct cpp_callbacks
   /* Called when a new line of preprocessed output is started.  */
   void (*line_change) (cpp_reader *, const cpp_token *, int);
   void (*file_change) (cpp_reader *, const struct line_map *);
+  void (*dir_change) (cpp_reader *, const char *);
   void (*include) (cpp_reader *, unsigned int, const unsigned char *,
 		   const char *, int);
   void (*define) (cpp_reader *, unsigned int, cpp_hashnode *);
Index: gcc/dbxout.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/dbxout.c,v
retrieving revision 1.156
diff -u -p -r1.156 dbxout.c
--- gcc/dbxout.c 19 Jul 2003 14:47:02 -0000 1.156
+++ gcc/dbxout.c 3 Aug 2003 12:10:40 -0000
@@ -469,7 +469,8 @@ dbxout_init (const char *input_file_name
   /* Put the current working directory in an N_SO symbol.  */
   if (use_gnu_debug_info_extensions)
     {
-      if (!cwd && (cwd = getpwd ()) && (!*cwd || cwd[strlen (cwd) - 1] != '/'))
+      if (!cwd && (cwd = get_src_pwd ())
+	  && (!*cwd || cwd[strlen (cwd) - 1] != '/'))
 	cwd = concat (cwd, FILE_NAME_JOINER, NULL);
       if (cwd)
 	{
Index: gcc/dwarf2out.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/dwarf2out.c,v
retrieving revision 1.447
diff -u -p -r1.447 dwarf2out.c
--- gcc/dwarf2out.c 1 Aug 2003 21:51:13 -0000 1.447
+++ gcc/dwarf2out.c 3 Aug 2003 12:10:47 -0000
@@ -9506,7 +9506,7 @@ add_name_attribute (dw_die_ref die, cons
 static void
 add_comp_dir_attribute (dw_die_ref die)
 {
-  const char *wd = getpwd ();
+  const char *wd = get_src_pwd ();
   if (wd != NULL)
     add_AT_string (die, DW_AT_comp_dir, wd);
 }
Index: gcc/dwarfout.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/dwarfout.c,v
retrieving revision 1.131
diff -u -p -r1.131 dwarfout.c
--- gcc/dwarfout.c 25 Jul 2003 09:52:24 -0000 1.131
+++ gcc/dwarfout.c 3 Aug 2003 12:10:54 -0000
@@ -4043,7 +4043,7 @@ output_compile_unit_die (void *arg)
     stmt_list_attribute (LINE_BEGIN_LABEL);
 
   {
-    const char *wd = getpwd ();
+    const char *wd = get_src_pwd ();
     if (wd)
       comp_dir_attribute (wd);
   }
@@ -6114,7 +6114,7 @@ dwarfout_init (const char *main_input_fi
 	  ASM_OUTPUT_PUSH_SECTION (asm_out_file, DEBUG_SFNAMES_SECTION);
 	  ASM_OUTPUT_LABEL (asm_out_file, SFNAMES_BEGIN_LABEL);
 	  {
-	    const char *pwd = getpwd ();
+	    const char *pwd = get_src_pwd ();
 	    char *dirname;
 
 	    if (!pwd)
Index: gcc/gcc.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/gcc.c,v
retrieving revision 1.390
diff -u -p -r1.390 gcc.c
--- gcc/gcc.c 31 Jul 2003 23:36:43 -0000 1.390
+++ gcc/gcc.c 3 Aug 2003 12:10:57 -0000
@@ -740,7 +740,8 @@ static const char *trad_capable_cpp =
    file that happens to exist is up-to-date.  */
 static const char *cpp_unique_options =
 "%{C|CC:%{!E:%eGCC does not support -C or -CC without -E}}\
- %{!Q:-quiet} %{nostdinc*} %{C} %{CC} %{v} %{I*} %{P} %I\
+ %{!Q:-quiet} %{nostdinc*} %{C} %{CC} %{v} %{I*} %I\
+ %{P} %{Pwd} %{!Pwd:%{!g*:-Pwd}}\
  %{MD:-MD %{!o:%b.d}%{o*:%.d%*}}\
  %{MMD:-MMD %{!o:%b.d}%{o*:%.d%*}}\
  %{M} %{MM} %{MF*} %{MG} %{MP} %{MQ*} %{MT*}\
Index: gcc/toplev.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/toplev.c,v
retrieving revision 1.814
diff -u -p -r1.814 toplev.c
--- gcc/toplev.c 25 Jul 2003 09:52:26 -0000 1.814
+++ gcc/toplev.c 3 Aug 2003 12:11:02 -0000
@@ -1219,6 +1219,41 @@ FILE *aux_info_file;
 FILE *rtl_dump_file = NULL;
 FILE *cgraph_dump_file = NULL;
 
+/* The current working directory of a translation.  It's generally the
+   directory from which compilation was initiated, but a preprocessed
+   file may specify the original directory in which it was
+   created.  */
+
+static const char *src_pwd;
+
+/* Initialize src_pwd with the given string, and return true.  If it
+   was already initialized, return false.  As a special case, it may
+   be called with a NULL argument to test whether src_pwd has NOT been
+   initialized yet.  */
+
+bool
+set_src_pwd (const char *pwd)
+{
+  if (src_pwd)
+    return false;
+
+  src_pwd = xstrdup (pwd);
+  return true;
+}
+
+/* Return the directory from which the translation unit was initiated,
+   in case set_src_pwd() was not called before to assign it a
+   different value.  */
+
+const char *
+get_src_pwd (void)
+{
+  if (! src_pwd)
+    src_pwd = getpwd ();
+
+   return src_pwd;
+}
+
 /* Set up a default flag_random_seed and local_tick, unless the user
    already specified one.  */
 
@@ -1254,7 +1289,6 @@ randomize (void)
   else if (!local_tick)
     local_tick = -1;
 }
-
 
 /* Decode the string P as an integral parameter.
    If the string is indeed an integer return its numeric value else
Index: gcc/toplev.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/toplev.h,v
retrieving revision 1.110
diff -u -p -r1.110 toplev.h
--- gcc/toplev.h 25 Jul 2003 09:52:26 -0000 1.110
+++ gcc/toplev.h 3 Aug 2003 12:11:02 -0000
@@ -155,4 +155,10 @@ extern bool fast_math_flags_set_p	(void)
 extern int exact_log2_wide             (unsigned HOST_WIDE_INT);
 extern int floor_log2_wide             (unsigned HOST_WIDE_INT);
 
+/* Functions used to get and set GCC's notion of in what directory
+   compilation was started.  */
+
+extern const char *get_src_pwd	       (void);
+extern bool set_src_pwd		       (const char *);
+
 #endif /* ! GCC_TOPLEV_H */
Index: gcc/doc/cpp.texi
===================================================================
RCS file: /cvs/gcc/gcc/gcc/doc/cpp.texi,v
retrieving revision 1.63
diff -u -p -r1.63 cpp.texi
--- gcc/doc/cpp.texi 1 Aug 2003 21:53:21 -0000 1.63
+++ gcc/doc/cpp.texi 3 Aug 2003 12:11:05 -0000
@@ -4133,7 +4133,9 @@ without notice.
 cpp [EMAIL PROTECTED]@[EMAIL PROTECTED]@dots{}] [EMAIL PROTECTED]@var{macro}]
     [EMAIL PROTECTED]@[EMAIL PROTECTED] [EMAIL PROTECTED]@[EMAIL PROTECTED]
     [EMAIL PROTECTED]|@option{-MM}] [EMAIL PROTECTED] [EMAIL PROTECTED] @var{filename}]
-    [EMAIL PROTECTED] [EMAIL PROTECTED] @[EMAIL PROTECTED] [EMAIL PROTECTED] @[EMAIL PROTECTED]
+    [EMAIL PROTECTED] [EMAIL PROTECTED] @[EMAIL PROTECTED]
+    [EMAIL PROTECTED] @[EMAIL PROTECTED]
+    [EMAIL PROTECTED] [EMAIL PROTECTED] 
     [EMAIL PROTECTED] @var{language}] [EMAIL PROTECTED]@var{standard}]
     @var{infile} @var{outfile}
 
Index: gcc/doc/cppopts.texi
===================================================================
RCS file: /cvs/gcc/gcc/gcc/doc/cppopts.texi,v
retrieving revision 1.25
diff -u -p -r1.25 cppopts.texi
--- gcc/doc/cppopts.texi 5 Jul 2003 00:23:53 -0000 1.25
+++ gcc/doc/cppopts.texi 3 Aug 2003 12:11:06 -0000
@@ -1,4 +1,4 @@
[EMAIL PROTECTED] Copyright (c) 1999, 2000, 2001, 2002
[EMAIL PROTECTED] Copyright (c) 1999, 2000, 2001, 2002, 2003
 @c Free Software Foundation, Inc.
 @c This is part of the CPP and GCC manuals.
 @c For copying conditions, see the file gcc.texi.
@@ -582,6 +582,17 @@ linemarkers.
 @ifset cppmanual
 @xref{Preprocessor Output}.
 @end ifset
+
[EMAIL PROTECTED] -Pwd
[EMAIL PROTECTED] Pwd
+Inhibit generation of linemarkers in the output from the preprocessor
+that tell the compiler the current working directory at the time of
+preprocessing.  If this option is not present, and any @samp{-g}
+option is encountered in the command line, the preprocessor will emit,
+after the initial linemarker, a second linemarker with the current
+working directory followed by two slashes.  GCC will use this
+directory, when it's present, as the directory emitted as the current
+working directory in some debugging information formats.
 
 @item -C
 @opindex C
Index: gcc/doc/invoke.texi
===================================================================
RCS file: /cvs/gcc/gcc/gcc/doc/invoke.texi,v
retrieving revision 1.327
diff -u -p -r1.327 invoke.texi
--- gcc/doc/invoke.texi 1 Aug 2003 21:53:22 -0000 1.327
+++ gcc/doc/invoke.texi 3 Aug 2003 12:11:16 -0000
@@ -301,7 +301,7 @@ in the following sections.
 -include @var{file}  -imacros @var{file} @gol
 -iprefix @var{file}  -iwithprefix @var{dir} @gol
 -iwithprefixbefore @var{dir}  -isystem @var{dir} @gol
--M  -MM  -MF  -MG  -MP  -MQ  -MT  -nostdinc  -P  -remap @gol
+-M  -MM  -MF  -MG  -MP  -MQ  -MT  -nostdinc  -P  -Pwd  -remap @gol
 -trigraphs  -undef  [EMAIL PROTECTED]  -Wp,@var{option} @gol
 -Xpreprocessor @var{option}}
 
-- 
Alexandre Oliva   Enjoy Guarana', see http://www.ic.unicamp.br/~oliva/
Red Hat GCC Developer                 [EMAIL PROTECTED], gcc.gnu.org}
CS PhD student at IC-Unicamp        [EMAIL PROTECTED], gnu.org}
Free Software Evangelist                Professional serial bug killer
__ 
distcc mailing list            http://distcc.samba.org/
To unsubscribe or change options: 
http://lists.samba.org/cgi-bin/mailman/listinfo/distcc

Reply via email to