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]

cpplib misc adjustments / speedups


This is a bunch of minor adjustments to cpplib and related code.  None
of it is terribly earth-shattering by itself, but it works, and I want
to get it off my laptop's hard disk before I go on vacation.

Highlights:

 * Performance tweakage: no-output mode disables the macro expander
   except in directives, and we compute the identifier hash function
   while we're scanning it.

 * files.c now properly detects redundant #includes in several cases
   where it wasn't.  (Also mainly a performance issue.)

 * Dependency generation interface: cpplib now exports a function to
   get at the deps buffer.  mkdeps.c now supports being told the VPATH,
   which it uses to shorten dependency strings.

 * Experimental new "makedepend" utility.  Not ready for prime time.

Bootstrapped i686-linux.

zw

libcpp:
        * Makefile.am: Add makedepend.
        * Makefile.in, aclocal.m4: Regenerate.
        * charset.c: Insert a space to avoid a warning.
        * directives.c: Include mkdeps.h.
        (_cpp_handle_directive): Reenable macro expander if appropriate.
        (undefine_macros): Inline body of _cpp_free_definition for speed.
        Do not call undef callback or _cpp_warn_if_unused_macro.
        (cpp_get_deps): New interface.
        * files.c (search_cache): Add pfile argument.  Check for file
        that would be found by "" or <> search here...
        (_cpp_find_file): ...not here.  Correct recorded start_dir of
        files found by directory-of-current-file search that would be
        found by "" or <> search.
        * init.c (cpp_add_dependency_target): Delete.
        * internal.h (struct lexer_state): Add discarding_output flag.
        * lex.c (lex_identifier): Compute hash function while scanning.
        * macro.c (cpp_scan_nooutput): Disable macro expansion outside
        directives.
        * makedepend.c: New file.
        * mkdeps.c (struct deps): Add vpath vector.
        (apply_vpath, deps_add_vpath): New function.
        (deps_free): Free vpath vector.
        (deps_add_dep, deps_add_target): Use apply_vpath.
        * symtab.c (calc_hash): Use HT_HASHSTEP and HT_FINISH.
        (ht_lookup_with_hash): New function.
        * cpplib.h, mkdeps.h: Update prototypes.
        * symtab.h: Update prototypes.
        (HT_HASHSTEP, HT_FINISH): New macros.

gcc:
        * Makefile.in (MKDEPS_H): New shorthand.
        (c-opts.o): Update dependencies.
        * c-opts.c: Include mkdeps.h.
        (handle_deferred_opts): Use cpp_get_deps and deps_add_target,
        not cpp_add_dependency_target.

===================================================================
Index: gcc/Makefile.in
--- gcc/Makefile.in	4 Jun 2004 16:10:17 -0000	1.1292
+++ gcc/Makefile.in	5 Jun 2004 20:42:59 -0000
@@ -714,6 +714,7 @@ SYSTEM_H = system.h hwint.h $(srcdir)/..
 PREDICT_H = predict.h predict.def
 CPPLIB_H = $(srcdir)/../libcpp/include/cpplib.h \
 	$(srcdir)/../libcpp/include/line-map.h
+MKDEPS_H = $(srcdir)/../libcpp/include/mkdeps.h
 SYMTAB_H = $(srcdir)/../libcpp/include/symtab.h
 TREE_DUMP_H = tree-dump.h $(SPLAY_TREE_H)
 TREE_GIMPLE_H = tree-gimple.h tree-iterator.h
@@ -1416,7 +1417,7 @@ c-pretty-print.o : c-pretty-print.c $(C_
 c-opts.o : c-opts.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H)		\
         $(TREE_H) c-pragma.h flags.h toplev.h langhooks.h		\
         tree-inline.h $(DIAGNOSTIC_H) intl.h debug.h $(C_COMMON_H)	\
-        opts.h options.h $(PARAMS_H)
+        opts.h options.h $(PARAMS_H) $(MKDEPS_H)
 	$(CC) -c $(ALL_CFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) \
 		$< $(OUTPUT_OPTION) @TARGET_SYSTEM_ROOT_DEFINE@
 
===================================================================
Index: gcc/c-opts.c
--- gcc/c-opts.c	22 May 2004 02:39:31 -0000	1.114
+++ gcc/c-opts.c	5 Jun 2004 20:42:59 -0000
@@ -37,6 +37,7 @@ Software Foundation, 59 Temple Place - S
 #include "debug.h"		/* For debug_hooks.  */
 #include "opts.h"
 #include "options.h"
+#include "mkdeps.h"
 
 #ifndef DOLLARS_IN_IDENTIFIERS
 # define DOLLARS_IN_IDENTIFIERS true
@@ -1322,13 +1323,22 @@ static void
 handle_deferred_opts (void)
 {
   size_t i;
+  struct deps *deps;
+
+  /* Avoid allocating the deps buffer if we don't need it.
+     (This flag may be true without there having been -MT or -MQ
+     options, but we'll still need the deps buffer.)  */
+  if (!deps_seen)
+    return;
+
+  deps = cpp_get_deps (parse_in);
 
   for (i = 0; i < deferred_count; i++)
     {
       struct deferred_opt *opt = &deferred_opts[i];
 
       if (opt->code == OPT_MT || opt->code == OPT_MQ)
-	cpp_add_dependency_target (parse_in, opt->arg, opt->code == OPT_MQ);
+	deps_add_target (deps, opt->arg, opt->code == OPT_MQ);
     }
 }
 
===================================================================
Index: libcpp/Makefile.am
--- libcpp/Makefile.am	25 May 2004 06:14:37 -0000	1.2
+++ libcpp/Makefile.am	5 Jun 2004 20:43:01 -0000
@@ -13,6 +13,9 @@ noinst_HEADERS = \
 	include/cpplib.h include/line-map.h include/mkdeps.h \
 	include/symtab.h internal.h system.h ucnid.h
 
+noinst_PROGRAMS = makedepend
+makedepend_LDADD = libcpp.a ../libiberty/libiberty.a
+
 XGETTEXT = @XGETTEXT@
 GMSGFMT = @GMSGFMT@
 MSGMERGE = msgmerge
===================================================================
Index: libcpp/charset.c
--- libcpp/charset.c	24 May 2004 10:50:44 -0000	1.1
+++ libcpp/charset.c	5 Jun 2004 20:43:02 -0000
@@ -1409,7 +1409,7 @@ _cpp_default_encoding (void)
      - the appropriate Unicode byte-order mark (FE FF) to recognize
        UTF16 and UCS4 (in both big-endian and little-endian flavors)
        and UTF8
-     - a "#i", "#d", "/*", "//", " #p" or "#p" (for #pragma) to
+     - a "#i", "#d", "/ *", "//", " #p" or "#p" (for #pragma) to
        distinguish ASCII and EBCDIC.
      - now we can parse something like "#pragma GCC encoding <xyz>
        on the first line, or even Emacs/VIM's mode line tags (there's
===================================================================
Index: libcpp/directives.c
--- libcpp/directives.c	24 May 2004 10:50:44 -0000	1.1
+++ libcpp/directives.c	5 Jun 2004 20:43:02 -0000
@@ -23,6 +23,7 @@ Foundation, 59 Temple Place - Suite 330,
 #include "system.h"
 #include "cpplib.h"
 #include "internal.h"
+#include "mkdeps.h"
 #include "obstack.h"
 
 /* Chained list of answers to an assertion.  */
@@ -336,8 +337,12 @@ _cpp_handle_directive (cpp_reader *pfile
   const directive *dir = 0;
   const cpp_token *dname;
   bool was_parsing_args = pfile->state.parsing_args;
+  bool was_discarding_output = pfile->state.discarding_output;
   int skip = 1;
 
+  if (was_discarding_output)
+    pfile->state.prevent_expansion = 0;
+
   if (was_parsing_args)
     {
       if (CPP_OPTION (pfile, pedantic))
@@ -432,6 +437,8 @@ _cpp_handle_directive (cpp_reader *pfile
       pfile->state.parsing_args = 2;
       pfile->state.prevent_expansion = 1;
     }
+  if (was_discarding_output)
+    pfile->state.prevent_expansion = 1;
   return skip;
 }
 
@@ -549,30 +556,13 @@ do_undef (cpp_reader *pfile)
 /* Undefine a single macro/assertion/whatever.  */
 
 static int
-undefine_macros (cpp_reader *pfile, cpp_hashnode *h,
+undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h,
 		 void *data_p ATTRIBUTE_UNUSED)
 {
-  switch (h->type)
-    {
-    case NT_VOID:
-      break;
-
-    case NT_MACRO:
-      if (pfile->cb.undef)
-        (*pfile->cb.undef) (pfile, pfile->directive_line, h);
-
-      if (CPP_OPTION (pfile, warn_unused_macros))
-        _cpp_warn_if_unused_macro (pfile, h, NULL);
-
-      /* And fall through....  */
-    case NT_ASSERTION:
-      _cpp_free_definition (h);
-      break;
-
-    default:
-      abort ();
-    }
-  h->flags &= ~NODE_POISONED;
+  /* Body of _cpp_free_definition inlined here for speed.
+     Macros and assertions no longer have anything to free.  */
+  h->type = NT_VOID;
+  h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED);
   return 1;
 }
 
@@ -1913,6 +1903,15 @@ cpp_set_callbacks (cpp_reader *pfile, cp
   pfile->cb = *cb;
 }
 
+/* The dependencies structure.  (Creates one if it hasn't already been.)  */
+struct deps *
+cpp_get_deps (cpp_reader *pfile)
+{
+  if (!pfile->deps)
+    pfile->deps = deps_init ();
+  return pfile->deps;
+}
+
 /* Push a new buffer on the buffer stack.  Returns the new buffer; it
    doesn't fail.  It does not generate a file change call back; that
    is the responsibility of the caller.  */
===================================================================
Index: libcpp/files.c
--- libcpp/files.c	24 May 2004 10:50:44 -0000	1.1
+++ libcpp/files.c	5 Jun 2004 20:43:02 -0000
@@ -158,7 +158,8 @@ static struct cpp_dir *search_path_head 
 				 int angle_brackets, enum include_type);
 static const char *dir_name_of_file (_cpp_file *file);
 static void open_file_failed (cpp_reader *pfile, _cpp_file *file);
-static struct file_hash_entry *search_cache (struct file_hash_entry *head,
+static struct file_hash_entry *search_cache (cpp_reader *pfile,
+					     struct file_hash_entry *head,
 					     const cpp_dir *start_dir);
 static _cpp_file *make_cpp_file (cpp_reader *, cpp_dir *, const char *fname);
 static cpp_dir *make_cpp_dir (cpp_reader *, const char *dir_name, int sysp);
@@ -406,7 +407,7 @@ _cpp_find_file (cpp_reader *pfile, const
 			      INSERT);
 
   /* First check the cache before we resort to memory allocation.  */
-  entry = search_cache (*hash_slot, start_dir);
+  entry = search_cache (pfile, *hash_slot, start_dir);
   if (entry)
     return entry->u.file;
 
@@ -435,17 +436,6 @@ _cpp_find_file (cpp_reader *pfile, const
 	    }
 	  break;
 	}
-
-      /* Only check the cache for the starting location (done above)
-	 and the quote and bracket chain heads because there are no
-	 other possible starting points for searches.  */
-      if (file->dir != pfile->bracket_include
-	  && file->dir != pfile->quote_include)
-	continue;
-
-      entry = search_cache (*hash_slot, file->dir);
-      if (entry)
-	break;
     }
 
   if (entry)
@@ -462,6 +452,33 @@ _cpp_find_file (cpp_reader *pfile, const
       pfile->all_files = file;
     }
 
+  /* If this file was found in the directory-of-the-current-file,
+     check whether that directory is reachable via one of the normal
+     search paths.  If so, we must record this entry as being
+     reachable that way, otherwise we will mistakenly reprocess this
+     file if it is included later from the normal search path.  */
+  if (file->dir && start_dir->next == pfile->quote_include)
+    {
+      cpp_dir *d;
+      cpp_dir *proper_start_dir = pfile->quote_include;
+
+      for (d = proper_start_dir;; d = d->next)
+	{
+	  if (d == pfile->bracket_include)
+	    proper_start_dir = d;
+	  if (d == 0)
+	    {
+	      proper_start_dir = 0;
+	      break;
+	    }
+	  /* file->dir->name will have a trailing slash.  */
+	  if (!strncmp (d->name, file->dir->name, file->dir->len - 1))
+	    break;
+	}
+      if (proper_start_dir)
+	start_dir = proper_start_dir;
+    }
+
   /* Store this new result in the hash table.  */
   entry = new_file_hash_entry (pfile);
   entry->next = *hash_slot;
@@ -821,12 +838,40 @@ open_file_failed (cpp_reader *pfile, _cp
 /* Search in the chain beginning at HEAD for a file whose search path
    started at START_DIR != NULL.  */
 static struct file_hash_entry *
-search_cache (struct file_hash_entry *head, const cpp_dir *start_dir)
+search_cache (cpp_reader *pfile, struct file_hash_entry *head,
+	      const cpp_dir *start_dir)
 {
-  while (head && head->start_dir != start_dir)
-    head = head->next;
+  struct file_hash_entry *p;
+
+  /* Look for a file that was found from a search starting at the
+     given location.  */
+  for (p = head; p; p = p->next)
+    if (p->start_dir == start_dir)
+      return p;
+
+  /* If the given location is for a search of the directory containing
+     the current file, check for a match starting at the base of the
+     quoted include chain.  */
+  if (start_dir->next == pfile->quote_include)
+    {
+      start_dir = pfile->quote_include;
+      for (p = head; p; p = p->next)
+	if (p->start_dir == start_dir)
+	  return p;
+    }
+
+  /* If the given location is for a search from the base of the quoted
+     include chain, check for a match starting at the base of the
+     bracket include chain.  */
+  if (start_dir == pfile->quote_include)
+    {
+      start_dir = pfile->bracket_include;
+      for (p = head; p; p = p->next)
+	if (p->start_dir == start_dir)
+	  return p;
+    }
 
-  return head;
+  return 0;
 }
 
 /* Allocate a new _cpp_file structure.  */
===================================================================
Index: libcpp/init.c
--- libcpp/init.c	24 May 2004 10:50:44 -0000	1.1
+++ libcpp/init.c	5 Jun 2004 20:43:02 -0000
@@ -429,18 +429,6 @@ static void sanity_checks (cpp_reader *p
 # define sanity_checks(PFILE)
 #endif
 
-/* Add a dependency target.  Can be called any number of times before
-   cpp_read_main_file().  If no targets have been added before
-   cpp_read_main_file(), then the default target is used.  */
-void
-cpp_add_dependency_target (cpp_reader *pfile, const char *target, int quote)
-{
-  if (!pfile->deps)
-    pfile->deps = deps_init ();
-
-  deps_add_target (pfile->deps, target, quote);
-}
-
 /* This is called after options have been parsed, and partially
    processed.  */
 void
===================================================================
Index: libcpp/internal.h
--- libcpp/internal.h	24 May 2004 10:50:44 -0000	1.1
+++ libcpp/internal.h	5 Jun 2004 20:43:02 -0000
@@ -246,6 +246,10 @@ struct lexer_state
   /* Nonzero when parsing arguments to a function-like macro.  */
   unsigned char parsing_args;
 
+  /* Nonzero if prevent_expansion is true only because output is
+     being discarded.  */
+  unsigned char discarding_output;
+
   /* Nonzero to skip evaluating part of an expression.  */
   unsigned int skip_eval;
 };
===================================================================
Index: libcpp/lex.c
--- libcpp/lex.c	24 May 2004 10:50:44 -0000	1.1
+++ libcpp/lex.c	5 Jun 2004 20:43:02 -0000
@@ -470,22 +470,36 @@ static cpp_hashnode *
 lex_identifier (cpp_reader *pfile, const uchar *base)
 {
   cpp_hashnode *result;
-  const uchar *cur;
+  const uchar *cur, *limit;
+  unsigned int len;
+  unsigned int hash = HT_HASHSTEP (0, *base);
 
-  do
+  cur = pfile->buffer->cur;
+  for (;;)
     {
-      cur = pfile->buffer->cur;
-
       /* N.B. ISIDNUM does not include $.  */
       while (ISIDNUM (*cur))
-	cur++;
+	{
+	  hash = HT_HASHSTEP (hash, *cur);
+	  cur++;
+	}
 
       pfile->buffer->cur = cur;
+      if (!forms_identifier_p (pfile, false))
+	break;
+
+      limit = pfile->buffer->cur;
+      while (cur < limit)
+	{
+	  hash = HT_HASHSTEP (hash, *cur);
+	  cur++;
+	}
     }
-  while (forms_identifier_p (pfile, false));
+  len = cur - base;
+  hash = HT_HASHFINISH (hash, len);
 
   result = (cpp_hashnode *)
-    ht_lookup (pfile->hash_table, base, cur - base, HT_ALLOC);
+    ht_lookup_with_hash (pfile->hash_table, base, len, hash, HT_ALLOC);
 
   /* Rarely, identifiers require diagnostics when lexed.  */
   if (__builtin_expect ((result->flags & NODE_DIAGNOSTIC)
===================================================================
Index: libcpp/macro.c
--- libcpp/macro.c	24 May 2004 10:50:44 -0000	1.1
+++ libcpp/macro.c	5 Jun 2004 20:43:03 -0000
@@ -1142,12 +1142,18 @@ cpp_scan_nooutput (cpp_reader *pfile)
      transparently continuing with the including file.  */
   pfile->buffer->return_at_eof = true;
 
+  pfile->state.discarding_output++;
+  pfile->state.prevent_expansion++;
+
   if (CPP_OPTION (pfile, traditional))
     while (_cpp_read_logical_line_trad (pfile))
       ;
   else
     while (cpp_get_token (pfile)->type != CPP_EOF)
       ;
+
+  pfile->state.discarding_output--;
+  pfile->state.prevent_expansion--;
 }
 
 /* Step back one (or more) tokens.  Can only step mack more than 1 if
===================================================================
Index: libcpp/makedepend.c
--- libcpp/makedepend.c	1 Jan 1970 00:00:00 -0000
+++ libcpp/makedepend.c	5 Jun 2004 20:43:03 -0000
@@ -0,0 +1,205 @@
+/* Dependency generator utility.
+   Copyright (C) 2004 Free Software Foundation, Inc.
+   Contributed by Zack Weinberg, May 2004
+
+This program is free software; you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by the
+Free Software Foundation; either version 2, or (at your option) any
+later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+ In other words, you are welcome to use, share and improve this program.
+ You are forbidden to forbid anyone else to use, share and improve
+ what you give them.   Help stamp out software-hoarding!  */
+
+#include "config.h"
+#include "system.h"
+#include "line-map.h"
+#include "cpplib.h"
+#include "getopt.h"
+
+const char *progname;
+const char *vpath;
+
+static const char *output_file;
+static bool had_errors;
+
+/* Option lists, to give to cpplib before each input file.  */
+struct cmd_line_macro
+{
+  struct cmd_line_macro *next;
+  bool is_undef;
+  const char *macro;
+};
+
+static struct cmd_line_macro *cmd_line_macros;
+static cpp_dir *cmd_line_searchpath;
+
+static void
+add_clm (const char *macro, bool is_undef)
+{
+  struct cmd_line_macro *clm = xmalloc (sizeof (struct cmd_line_macro));
+  clm->next = cmd_line_macros;
+  clm->is_undef = is_undef;
+  clm->macro = macro;
+  cmd_line_macros = clm;
+}
+
+static void
+add_dir (char *name, bool sysp)
+{
+  cpp_dir *dir = xmalloc (sizeof (cpp_dir));
+  dir->next = cmd_line_searchpath;
+  dir->name = name;
+  dir->sysp = sysp;
+  dir->construct = 0;
+  dir->user_supplied_p = 1;
+  cmd_line_searchpath = dir;
+}
+
+/* Command line processing.  */
+
+static void ATTRIBUTE_NORETURN
+usage (int errcode)
+{
+  fprintf (stderr,
+"usage: %s [-vh] [-V vpath] [-Dname[=def]...] [-Uname] [-Idir...] [-o file] sources...\n",
+	   progname);
+  exit (errcode);
+}
+
+static int
+parse_options (int argc, char **argv)
+{
+  static const struct option longopts[] = {
+    { "--help", no_argument, 0, 'h' },
+    { 0, 0, 0, 0 }
+  };
+
+  for (;;)
+    switch (getopt_long (argc, argv, "hD:U:I:J:o:V:", longopts, 0))
+      {
+      case 'h': usage (0);
+      case 'D': add_clm (optarg, false); break;
+      case 'U': add_clm (optarg, true);  break;
+      case 'I': add_dir (optarg, false); break;
+      case 'J': add_dir (optarg, true);  break;
+      case 'o':
+	if (output_file)
+	  {
+	    fprintf (stderr, "%s: too many output files\n", progname);
+	    usage (2);
+	  }
+	output_file = optarg;
+	break;
+      case 'V':
+	if (vpath)
+	  {
+	    fprintf (stderr, "%s: too many vpaths\n", progname);
+	    usage (2);
+	  }
+	vpath = optarg;
+	break;
+      case '?':
+	usage (2);  /* getopt has issued the error message.  */
+
+      case -1: /* end of options */
+	if (optind == argc)
+	  {
+	    fprintf (stderr, "%s: no input files\n", progname);
+	    usage (2);
+	  }
+	return optind;
+	
+      default:
+	abort ();
+      }
+}
+
+/* Set up cpplib from command line options.  */
+static cpp_reader *
+reader_init (struct line_maps *line_table)
+{
+  cpp_reader *reader;
+  cpp_options *options;
+
+  linemap_init (line_table);
+  reader = cpp_create_reader (CLK_GNUC89, 0, line_table);
+
+  /* Ignore warnings and errors (we don't have access to system
+     headers).  Request dependency output.  */
+  options = cpp_get_options (reader);
+  options->inhibit_warnings = 1;
+  options->inhibit_errors = 1;
+  options->deps.style = DEPS_USER;
+
+  /* Further initialization.  */
+  cpp_post_options (reader);
+  cpp_init_iconv (reader);
+  cpp_set_include_chains (reader, cmd_line_searchpath, cmd_line_searchpath,
+			  false);
+  if (vpath)
+    {
+      struct deps *deps = cpp_get_deps (reader);
+      deps_add_vpath (deps, vpath);
+    }
+
+  return reader;
+}
+
+/* Process one input source file.  */
+static void
+process_file (const char *file)
+{
+  struct line_maps line_table;
+  cpp_reader *reader = reader_init (&line_table);
+
+  if (!cpp_read_main_file (reader, file))
+    had_errors = true;
+  else
+    {
+      struct cmd_line_macro *clm;
+
+      cpp_init_builtins (reader, true);
+      for (clm = cmd_line_macros; clm; clm = clm->next)
+	(clm->is_undef ? cpp_undef : cpp_define) (reader, clm->macro);
+
+      cpp_scan_nooutput (reader);
+      if (cpp_finish (reader, stdout))
+	had_errors = true;
+    }
+  cpp_destroy (reader);
+  linemap_free (&line_table);
+}
+
+/* Master control.  */
+
+int
+main(int argc, char **argv)
+{
+  int first_input, i;
+
+  progname = argv[0];
+  xmalloc_set_program_name (progname);
+
+  first_input = parse_options (argc, argv);
+  if (output_file)
+    if (!freopen (output_file, "w", stdout))
+      {
+	perror (output_file);
+	return 1;
+      }
+
+  for (i = first_input; i < argc; i++)
+    process_file (argv[i]);
+
+  return had_errors;
+}
===================================================================
Index: libcpp/mkdeps.c
--- libcpp/mkdeps.c	24 May 2004 10:50:44 -0000	1.1
+++ libcpp/mkdeps.c	5 Jun 2004 20:43:03 -0000
@@ -35,6 +35,11 @@ struct deps
   const char **depv;
   unsigned int ndeps;
   unsigned int deps_size;
+
+  const char **vpathv;
+  size_t *vpathlv;
+  unsigned int nvpaths;
+  unsigned int vpaths_size;
 };
 
 static const char *munge (const char *);
@@ -105,24 +110,48 @@ munge (const char *filename)
   return buffer;
 }
 
-/* Public routines.  */
-
-struct deps *
-deps_init (void)
+/* If T begins with any of the partial pathnames listed in d->vpathv,
+   then advance T to point beyond that pathname.  */
+static const char *
+apply_vpath (struct deps *d, const char *t)
 {
-  struct deps *d = xmalloc (sizeof (struct deps));
+  if (d->vpathv)
+    {
+      unsigned int i;
+      for (i = 0; i < d->nvpaths; i++)
+	{
+	  if (!strncmp (d->vpathv[i], t, d->vpathlv[i]))
+	    {
+	      const char *p = t + d->vpathlv[i];
+	      if (!IS_DIR_SEPARATOR (*p))
+		goto not_this_one;
+
+	      /* Do not simplify $(vpath)/../whatever.  ??? Might not
+		 be necessary. */
+	      if (p[1] == '.' && p[2] == '.' && IS_DIR_SEPARATOR (p[3]))
+		goto not_this_one;
+
+	      /* found a match */
+	      t = t + d->vpathlv[i] + 1;
+	      break;
+	    }
+	not_this_one:;
+	}
+    }
 
-  /* Allocate space for the vectors only if we need it.  */
+  /* Remove leading ./ in any case.  */
+  while (t[0] == '.' && IS_DIR_SEPARATOR (t[1]))
+    t += 2;
 
-  d->targetv = 0;
-  d->depv = 0;
+  return t;
+}
 
-  d->ntargets = 0;
-  d->targets_size = 0;
-  d->ndeps = 0;
-  d->deps_size = 0;
+/* Public routines.  */
 
-  return d;
+struct deps *
+deps_init (void)
+{
+  return xcalloc (sizeof (struct deps), 1);
 }
 
 void
@@ -144,6 +173,14 @@ deps_free (struct deps *d)
       free (d->depv);
     }
 
+  if (d->vpathv)
+    {
+      for (i = 0; i < d->nvpaths; i++)
+	free ((void *) d->vpathv[i]);
+      free (d->vpathv);
+      free (d->vpathlv);
+    }
+
   free (d);
 }
 
@@ -159,6 +196,7 @@ deps_add_target (struct deps *d, const c
 			     d->targets_size * sizeof (const char *));
     }
 
+  t = apply_vpath (d, t);
   if (quote)
     t = munge (t);  /* Also makes permanent copy.  */
   else
@@ -202,7 +240,7 @@ deps_add_default_target (struct deps *d,
 void
 deps_add_dep (struct deps *d, const char *t)
 {
-  t = munge (t);  /* Also makes permanent copy.  */
+  t = munge (apply_vpath (d, t));  /* Also makes permanent copy.  */
 
   if (d->ndeps == d->deps_size)
     {
@@ -213,6 +251,36 @@ deps_add_dep (struct deps *d, const char
 }
 
 void
+deps_add_vpath (struct deps *d, const char *vpath)
+{
+  const char *elem, *p;
+  char *copy;
+  size_t len;
+
+  for (elem = vpath; *elem; elem = p)
+    {
+      for (p = elem; *p && *p != ':'; p++);
+      len = p - elem;
+      copy = xmalloc (len + 1);
+      memcpy (copy, elem, len);
+      copy[len] = '\0';
+      if (*p == ':')
+	p++;
+
+      if (d->nvpaths == d->vpaths_size)
+	{
+	  d->vpaths_size = d->vpaths_size * 2 + 8;
+	  d->vpathv = xrealloc (d->vpathv,
+				d->vpaths_size * sizeof (const char *));
+	  d->vpathlv = xrealloc (d->vpathlv, d->vpaths_size * sizeof (size_t));
+	}
+      d->vpathv[d->nvpaths] = copy;
+      d->vpathlv[d->nvpaths] = len;
+      d->nvpaths++;
+    }
+}
+
+void
 deps_write (const struct deps *d, FILE *fp, unsigned int colmax)
 {
   unsigned int size, i, column;
===================================================================
Index: libcpp/symtab.c
--- libcpp/symtab.c	30 May 2004 00:49:02 -0000	1.2
+++ libcpp/symtab.c	5 Jun 2004 20:43:03 -0000
@@ -41,13 +41,11 @@ calc_hash (const unsigned char *str, siz
 {
   size_t n = len;
   unsigned int r = 0;
-#define HASHSTEP(r, c) ((r) * 67 + ((c) - 113));
 
   while (n--)
-    r = HASHSTEP (r, *str++);
+    r = HT_HASHSTEP (r, *str++);
 
-  return r + len;
-#undef HASHSTEP
+  return HT_HASHFINISH (r, len);
 }
 
 /* Initialize an identifier hashtable.  */
@@ -96,7 +94,15 @@ hashnode
 ht_lookup (hash_table *table, const unsigned char *str, size_t len,
 	   enum ht_lookup_option insert)
 {
-  unsigned int hash = calc_hash (str, len);
+  return ht_lookup_with_hash (table, str, len, calc_hash (str, len),
+			      insert);
+}
+
+hashnode
+ht_lookup_with_hash (hash_table *table, const unsigned char *str,
+		     size_t len, unsigned int hash,
+		     enum ht_lookup_option insert)
+{
   unsigned int hash2;
   unsigned int index;
   size_t sizemask;
===================================================================
Index: libcpp/include/cpplib.h
--- libcpp/include/cpplib.h	24 May 2004 10:50:45 -0000	1.1
+++ libcpp/include/cpplib.h	5 Jun 2004 20:43:03 -0000
@@ -538,23 +538,18 @@ extern cpp_reader *cpp_create_reader (en
    command line options).  */
 extern void cpp_set_lang (cpp_reader *, enum c_lang);
 
-/* Add a dependency TARGET.  Quote it for "make" if QUOTE.  Can be
-   called any number of times before cpp_read_main_file().  If no
-   targets have been added before cpp_read_main_file(), then the
-   default target is used.  */
-extern void cpp_add_dependency_target (cpp_reader *, const char *, int);
-
 /* Set the include paths.  */
 extern void cpp_set_include_chains (cpp_reader *, cpp_dir *, cpp_dir *, int);
 
-/* Call these to get pointers to the options and callback structures
-   for a given reader.  These pointers are good until you call
-   cpp_finish on that reader.  You can either edit the callbacks
+/* Call these to get pointers to the options, callback, and deps
+   structures for a given reader.  These pointers are good until you
+   call cpp_finish on that reader.  You can either edit the callbacks
    through the pointer returned from cpp_get_callbacks, or set them
    with cpp_set_callbacks.  */
 extern cpp_options *cpp_get_options (cpp_reader *);
 extern cpp_callbacks *cpp_get_callbacks (cpp_reader *);
 extern void cpp_set_callbacks (cpp_reader *, cpp_callbacks *);
+extern struct deps *cpp_get_deps (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
===================================================================
Index: libcpp/include/mkdeps.h
--- libcpp/include/mkdeps.h	24 May 2004 10:50:45 -0000	1.1
+++ libcpp/include/mkdeps.h	5 Jun 2004 20:43:03 -0000
@@ -34,6 +34,13 @@ extern struct deps *deps_init (void);
 /* Destroy a deps buffer.  */
 extern void deps_free (struct deps *);
 
+/* Add a set of "vpath" directories. The second argument is a colon-
+   separated list of pathnames, like you would set Make's VPATH
+   variable to.  If a dependency or target name begins with any of
+   these pathnames (and the next path element is not "..") that
+   pathname is stripped off.  */
+extern void deps_add_vpath (struct deps *, const char *);
+
 /* Add a target (appears on left side of the colon) to the deps list.  Takes
    a boolean indicating whether to quote the target for MAKE.  */
 extern void deps_add_target (struct deps *, const char *, int);
===================================================================
Index: libcpp/include/symtab.h
--- libcpp/include/symtab.h	30 May 2004 00:49:02 -0000	1.2
+++ libcpp/include/symtab.h	5 Jun 2004 20:43:03 -0000
@@ -71,6 +71,11 @@ extern void ht_destroy (hash_table *);
 
 extern hashnode ht_lookup (hash_table *, const unsigned char *,
 			   size_t, enum ht_lookup_option);
+extern hashnode ht_lookup_with_hash (hash_table *, const unsigned char *,
+                                     size_t, unsigned int,
+                                     enum ht_lookup_option);
+#define HT_HASHSTEP(r, c) ((r) * 67 + ((c) - 113));
+#define HT_HASHFINISH(r, len) ((r) + (len))
 
 /* For all nodes in TABLE, make a callback.  The callback takes
    TABLE->PFILE, the node, and a PTR, and the callback sequence stops


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