RFA: More initialization reworking

Neil Booth neil@daikokuya.co.uk
Tue Mar 4 23:00:00 GMT 2003


Short-term goal is to get cpp_read_main_file into the post_options hook
rather than the init hook.  It is what determines the original file name
in the case of preprocessed input, and so pushing it earlier in the
initialization dance will mean that debug initialization can go earlier
too.  If debug initialization is earlier, then the remaining command
line options, like -D, get easier to move into the front ends.

This patch combines the current two calls to cpp_read_main_file into
one, ready for moving to a different hook.  It also does minor
cleanups of other issues.

This patch is borderline my CPP remit, so I'm asking for approval.

Thanks,

Neil.

	* Makefile.in (c-ppoutput.o): Update.
	* c-common.h (init_pp_output): New.
	(preprocess_file): Update.
	* c-lex.c (header_time, body_time, file_info_tree): Group in a struct.
	(init_c_lex): Move mbchar initialization to cpplib.  Don't initialize
	statistics here.
	(get_fileinfo, update_header_times, dump_time_statistics):
	Update accordingly.
	(cb_file_change): Conditionalize call to update_header_times.
	* c-opts.c (c_common_init): Call init_pp_output if preprocessing.
	Make call to cpp_read_main_file common to whether preprocessing
	or not.
	* c-ppoutput.c: Include c-pragma.h.
	(setup_callbacks): Register builtins.  Update prototype.
	(init_pp_output): New.
	(preprocess_file): No longer setup callbacks or call
	cpp_read_main_file.
	* cpphash.h (_cpp_init_mbchar): New.
	* cppinit.c (init_library): Call _cpp_init_mbchar.
	* cpplex.c (_cpp_init_mbchar): New.

============================================================
Index: gcc/Makefile.in
--- gcc/Makefile.in	4 Mar 2003 21:48:45 -0000	1.1007
+++ gcc/Makefile.in	4 Mar 2003 22:42:39 -0000
@@ -1235,7 +1235,7 @@ c-lex.o : c-lex.c $(CONFIG_H) $(SYSTEM_H
     c-pragma.h input.h intl.h flags.h toplev.h output.h \
     mbchar.h $(CPPLIB_H) $(EXPR_H) $(TM_P_H)
 c-ppoutput.o : c-ppoutput.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) \
-    c-common.h $(TREE_H) $(CPPLIB_H) cpphash.h $(TM_P_H)
+    c-common.h $(TREE_H) $(CPPLIB_H) cpphash.h $(TM_P_H) c-pragma.h
 c-objc-common.o : c-objc-common.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(TREE_H) \
     $(C_TREE_H) $(RTL_H) insn-config.h integrate.h $(EXPR_H) $(C_TREE_H) \
     flags.h toplev.h tree-inline.h diagnostic.h integrate.h $(VARRAY_H) \
============================================================
Index: gcc/c-common.h
--- gcc/c-common.h	4 Mar 2003 07:00:38 -0000	1.168
+++ gcc/c-common.h	4 Mar 2003 22:42:40 -0000
@@ -1292,8 +1292,9 @@ extern void c_common_read_pch			PARAMS (
 							 int fd,
 							 const char *orig));
 extern void c_common_write_pch			PARAMS ((void));
-extern void preprocess_file			PARAMS ((cpp_reader *,
-							 const char *,
-							 FILE *));
+
+/* In c-ppoutput.c  */
+extern void init_pp_output			PARAMS ((FILE *));
+extern void preprocess_file			PARAMS ((cpp_reader *));
 
 #endif /* ! GCC_C_COMMON_H */
============================================================
Index: gcc/c-lex.c
--- gcc/c-lex.c	4 Mar 2003 07:00:38 -0000	1.196
+++ gcc/c-lex.c	4 Mar 2003 22:42:41 -0000
@@ -54,8 +54,11 @@ static const struct line_map *map;
 static unsigned int src_lineno;
 
 /* We may keep statistics about how long which files took to compile.  */
-static int header_time, body_time;
-static splay_tree file_info_tree;
+static struct
+{
+  int header_time, body_time;
+  splay_tree file_info_tree;
+} stats;
 
 /* File used for outputting assembler code.  */
 extern FILE *asm_out_file;
@@ -93,32 +96,15 @@ static void cb_define		PARAMS ((cpp_read
 static void cb_undef		PARAMS ((cpp_reader *, unsigned int,
 					 cpp_hashnode *));
 
+/* Setup CPP callbacks that are appropriate for compiling.  */
 void
 init_c_lex ()
 {
   struct cpp_callbacks *cb;
-  struct c_fileinfo *toplevel;
-
-  /* Set up filename timing.  Must happen before cpp_read_main_file.  */
-  file_info_tree = splay_tree_new ((splay_tree_compare_fn)strcmp,
-				   0,
-				   (splay_tree_delete_value_fn)free);
-  toplevel = get_fileinfo ("<top level>");
-  if (flag_detailed_statistics)
-    {
-      header_time = 0;
-      body_time = get_run_time ();
-      toplevel->time = body_time;
-    }
-  
-#ifdef MULTIBYTE_CHARS
-  /* Change to the native locale for multibyte conversions.  */
-  setlocale (LC_CTYPE, "");
-  GET_ENVIRONMENT (literal_codeset, "LANG");
-#endif
 
   cb = cpp_get_callbacks (parse_in);
 
+  cb->register_builtins = cb_register_builtins;
   cb->line_change = cb_line_change;
   cb->ident = cb_ident;
   cb->file_change = cb_file_change;
@@ -167,7 +153,7 @@ get_fileinfo (name)
   splay_tree_node n;
   struct c_fileinfo *fi;
 
-  n = splay_tree_lookup (file_info_tree, (splay_tree_key) name);
+  n = splay_tree_lookup (stats.file_info_tree, (splay_tree_key) name);
   if (n)
     return (struct c_fileinfo *) n->value;
 
@@ -175,7 +161,7 @@ get_fileinfo (name)
   fi->time = 0;
   fi->interface_only = 0;
   fi->interface_unknown = 1;
-  splay_tree_insert (file_info_tree, (splay_tree_key) name,
+  splay_tree_insert (stats.file_info_tree, (splay_tree_key) name,
 		     (splay_tree_value) fi);
   return fi;
 }
@@ -186,14 +172,19 @@ update_header_times (name)
 {
   /* Changing files again.  This means currently collected time
      is charged against header time, and body time starts back at 0.  */
-  if (flag_detailed_statistics)
-    {
-      int this_time = get_run_time ();
-      struct c_fileinfo *file = get_fileinfo (name);
-      header_time += this_time - body_time;
-      file->time += this_time - body_time;
-      body_time = this_time;
-    }
+  int this_time;
+  struct c_fileinfo *file;
+
+  if (!stats.file_info_tree)
+    stats.file_info_tree = splay_tree_new ((splay_tree_compare_fn) strcmp,
+					   0,
+					   (splay_tree_delete_value_fn) free);
+
+  this_time = get_run_time ();
+  stats.header_time += this_time - stats.body_time;
+  file = get_fileinfo (name);
+  file->time += this_time - stats.body_time;
+  stats.body_time = this_time;
 }
 
 static int
@@ -209,18 +200,24 @@ dump_one_header (n, dummy)
 void
 dump_time_statistics ()
 {
-  struct c_fileinfo *file = get_fileinfo (input_filename);
-  int this_time = get_run_time ();
-  file->time += this_time - body_time;
+  struct c_fileinfo *file;
+  int this_time;
+
+  if (!stats.file_info_tree)
+    return;
+
+  this_time = get_run_time ();
+  file = get_fileinfo (input_filename);
+  file->time += this_time - stats.body_time;
 
   fprintf (stderr, "\n******\n");
-  print_time ("header files (total)", header_time);
-  print_time ("main file (total)", this_time - body_time);
+  print_time ("header files (total)", stats.header_time);
+  print_time ("main file (total)", this_time - stats.body_time);
   fprintf (stderr, "ratio = %g : 1\n",
-	   (double)header_time / (double)(this_time - body_time));
+	   stats.header_time / (double) (this_time - stats.body_time));
   fprintf (stderr, "\n******\n");
 
-  splay_tree_foreach (file_info_tree, dump_one_header, 0);
+  splay_tree_foreach (stats.file_info_tree, dump_one_header, 0);
 }
 
 static void
@@ -296,7 +293,6 @@ cb_file_change (pfile, new_map)
       (*debug_hooks->end_source_file) (to_line);
     }
 
-  update_header_times (new_map->to_file);
   in_system_header = new_map->sysp != 0;
   input_filename = new_map->to_file;
   lineno = to_line;
@@ -304,6 +300,9 @@ cb_file_change (pfile, new_map)
 
   /* Hook for C++.  */
   extract_interface_info ();
+
+  if (flag_detailed_statistics)
+    update_header_times (new_map->to_file);
 }
 
 static void
============================================================
Index: gcc/c-opts.c
--- gcc/c-opts.c	4 Mar 2003 07:00:38 -0000	1.31
+++ gcc/c-opts.c	4 Mar 2003 22:42:41 -0000
@@ -1512,10 +1512,6 @@ c_common_init (filename)
   cpp_opts->wchar_precision = TYPE_PRECISION (wchar_type_node);
   cpp_opts->unsigned_wchar = TREE_UNSIGNED (wchar_type_node);
 
-  /* Register preprocessor built-ins before calls to
-     cpp_main_file.  */
-  cpp_get_callbacks (parse_in)->register_builtins = cb_register_builtins;
-
   if (flag_preprocess_only)
     {
       /* Open the output now.  We must do so even if flag_no_output is
@@ -1527,22 +1523,30 @@ c_common_init (filename)
 	out_stream = fopen (out_fname, "w");
 
       if (out_stream == NULL)
-	fatal_io_error ("opening output file %s", out_fname);
-      else
-	/* Preprocess the input file to out_stream.  */
-	preprocess_file (parse_in, in_fname, out_stream);
+	{
+	  fatal_io_error ("opening output file %s", out_fname);
+	  return NULL;
+	}
 
-      /* Exit quickly in toplev.c.  */
-      return NULL;
+      init_pp_output (out_stream);
     }
+  else
+    init_c_lex ();
 
-  init_c_lex ();
-
-  /* Start it at 0.  */
+  /* Yuk.  WTF is this?  I do know ObjC relies on it somewhere.  */
   lineno = 0;
 
   /* NOTE: we use in_fname here, not the one supplied.  */
   filename = cpp_read_main_file (parse_in, in_fname, ident_hash);
+
+  if (flag_preprocess_only)
+    {
+      /* Preprocess the input file.  */
+      preprocess_file (parse_in);
+
+      /* Exit quickly in toplev.c.  */
+      return NULL;
+    }
 
   /* Has to wait until now so that cpplib has its hash table.  */
   init_pragma ();
============================================================
Index: gcc/c-ppoutput.c
--- gcc/c-ppoutput.c	4 Mar 2003 07:00:38 -0000	1.1
+++ gcc/c-ppoutput.c	4 Mar 2003 22:42:41 -0000
@@ -24,7 +24,8 @@ Foundation, 59 Temple Place - Suite 330,
 #include "cpplib.h"
 #include "cpphash.h"
 #include "tree.h"
-#include "c-common.h"
+#include "c-common.h"		/* For flags.  */
+#include "c-pragma.h"		/* For parse_in.  */
 
 /* Encapsulates state used to convert a stream of tokens into a text
    file.  */
@@ -38,7 +39,7 @@ static struct
   unsigned char printed;	/* Nonzero if something output at line.  */
 } print;
 
-static void setup_callbacks PARAMS ((cpp_reader *));
+static void setup_callbacks PARAMS ((void));
 
 /* General output routines.  */
 static void scan_translation_unit PARAMS ((cpp_reader *));
@@ -62,11 +63,8 @@ static void cb_ident	  PARAMS ((cpp_read
 static void cb_file_change PARAMS ((cpp_reader *, const struct line_map *));
 static void cb_def_pragma PARAMS ((cpp_reader *, unsigned int));
 
-/* Preprocess and output.  */
 void
-preprocess_file (pfile, in_fname, out_stream)
-     cpp_reader *pfile;
-     const char *in_fname;
+init_pp_output (out_stream)
      FILE *out_stream;
 {
   /* Initialize the print structure.  Setting print.line to -1 here is
@@ -78,30 +76,33 @@ preprocess_file (pfile, in_fname, out_st
   print.map = 0;
   print.outf = out_stream;
 
-  setup_callbacks (pfile);
+  setup_callbacks ();
+}
 
-  if (cpp_read_main_file (pfile, in_fname, NULL))
-    {
-      cpp_finish_options (pfile);
+/* Preprocess and output.  */
+void
+preprocess_file (pfile)
+     cpp_reader *pfile;
+{
+  cpp_finish_options (pfile);
 
-      /* A successful cpp_read_main_file guarantees that we can call
-	 cpp_scan_nooutput or cpp_get_token next.  */
-      if (flag_no_output)
-	{
-	  /* Scan -included buffers, then the main file.  */
-	  while (pfile->buffer->prev)
-	    cpp_scan_nooutput (pfile);
-	  cpp_scan_nooutput (pfile);
-	}
-      else if (cpp_get_options (pfile)->traditional)
-	scan_translation_unit_trad (pfile);
-      else
-	scan_translation_unit (pfile);
-
-      /* -dM command line option.  Should this be elsewhere?  */
-      if (flag_dump_macros == 'M')
-	cpp_forall_identifiers (pfile, dump_macro, NULL);
+  /* A successful cpp_read_main_file guarantees that we can call
+     cpp_scan_nooutput or cpp_get_token next.  */
+  if (flag_no_output)
+    {
+      /* Scan -included buffers, then the main file.  */
+      while (pfile->buffer->prev)
+	cpp_scan_nooutput (pfile);
+      cpp_scan_nooutput (pfile);
     }
+  else if (cpp_get_options (pfile)->traditional)
+    scan_translation_unit_trad (pfile);
+  else
+    scan_translation_unit (pfile);
+
+  /* -dM command line option.  Should this be elsewhere?  */
+  if (flag_dump_macros == 'M')
+    cpp_forall_identifiers (pfile, dump_macro, NULL);
 
   /* Flush any pending output.  */
   if (print.printed)
@@ -110,18 +111,19 @@ preprocess_file (pfile, in_fname, out_st
 
 /* Set up the callbacks as appropriate.  */
 static void
-setup_callbacks (pfile)
-     cpp_reader *pfile;
+setup_callbacks ()
 {
-  cpp_options *options = &pfile->opts;
-  cpp_callbacks *cb = cpp_get_callbacks (pfile);
+  cpp_callbacks *cb;
+
+  cb = cpp_get_callbacks (parse_in);
+  cb->register_builtins = cb_register_builtins;
 
   if (!flag_no_output)
     {
       cb->line_change = cb_line_change;
       /* Don't emit #pragma or #ident directives if we are processing
 	 assembly language; the assembler may choke on them.  */
-      if (options->lang != CLK_ASM)
+      if (cpp_get_options (parse_in)->lang != CLK_ASM)
 	{
 	  cb->ident      = cb_ident;
 	  cb->def_pragma = cb_def_pragma;
============================================================
Index: gcc/cpphash.h
--- gcc/cpphash.h	4 Mar 2003 07:00:38 -0000	1.178
+++ gcc/cpphash.h	4 Mar 2003 22:42:41 -0000
@@ -517,6 +517,7 @@ extern cpp_token *_cpp_lex_direct	PARAMS
 extern int _cpp_equiv_tokens		PARAMS ((const cpp_token *,
 						 const cpp_token *));
 extern void _cpp_init_tokenrun		PARAMS ((tokenrun *, unsigned int));
+extern void _cpp_init_mbchar		PARAMS ((void));
 
 /* In cppinit.c.  */
 extern void _cpp_maybe_push_include_file PARAMS ((cpp_reader *));
============================================================
Index: gcc/cppinit.c
--- gcc/cppinit.c	1 Mar 2003 14:31:12 -0000	1.268
+++ gcc/cppinit.c	4 Mar 2003 22:42:41 -0000
@@ -191,6 +191,8 @@ init_library ()
 	 we were compiled with a compiler that supports C99 designated
 	 initializers.  */
       init_trigraph_map ();
+
+      _cpp_init_mbchar ();
     }
 }
 
============================================================
Index: gcc/cpplex.c
--- gcc/cpplex.c	23 Jan 2003 19:51:16 -0000	1.218
+++ gcc/cpplex.c	4 Mar 2003 22:42:41 -0000
@@ -89,6 +89,16 @@ static tokenrun *next_tokenrun PARAMS ((
 static unsigned int hex_digit_value PARAMS ((unsigned int));
 static _cpp_buff *new_buff PARAMS ((size_t));
 
+/* Change to the native locale for multibyte conversions.  */
+void
+_cpp_init_mbchar ()
+{
+#ifdef MULTIBYTE_CHARS
+  setlocale (LC_CTYPE, "");
+  GET_ENVIRONMENT (literal_codeset, "LANG");
+#endif
+}
+
 /* Utility routine:
 
    Compares, the token TOKEN to the NUL-terminated string STRING.



More information about the Gcc-patches mailing list