This is the mail archive of the gdb-patches@sources.redhat.com mailing list for the GDB 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]

[rfa] annotate blocks with C++ namespace information


Here's the first of my namespace patches.  It attempts to annotate
blocks with namespace-related information.  It adds a
language-specific member to struct block, which can contain
information about the using directives that are active in that block
and about the namespace that is in scope in that block.  I don't
populate these members for every C++ block: to find all the using
directives in scope, you should add together all the using directives
for the block in question and for its superblocks; to find the
namespace in scope, you should travel up the superblocks until there
is a block saying what the current namespace is.

The only using directives that this adds are the ones corresponding to
anonymous namespaces.  (Which are the most important ones, for various
reasons.)  The reason for that is that you can't figure out that other
using directives exist without compiler support, and I don't have
access to a compiler generating appropriate debug info.

This information is calculated in two different ways.  If dwarf2read.c
notices that the file in question has namespace info, it will keep
track of the current namespace (using the variables
processing_has_namespace_info and processing_current_namespace added
to buildsym.h), and will set the using directives itself.  In that
case, buildsym.c will take the scope directly from
processing_current_namespace.  If the file doesn't have namespace
info, however, then buildsym.c will look at functions' demangled names
to figure out what the current namespace is, and will also scan the
names of symbols to see if any of them hint at the presence of
anonymous namespaces.  (So this will work for all debug formats except
for mdebugread.c, and it won't actually break anything even in that
case.)

What this file doesn't do is actually use the generated information in
any way.  That will come in my next namespace patch, along with
testsuite tests.  I figured this patch was long enough as is; also, I
still have one last bit of lookup_symbol cleanup to do before I can
use the information in question.

I've tested this as follows: I've run it through the test suite, both
with a stock GCC 3.1 and with a version of GCC 3.2 modified to
generate debug info for namespaces.  (All on i686-pc-linux-gnu with
DWARF 2.)  There were no regressions; good thing, since it's not
supposed to change GDB's output at all!  I also made sure that the
versions of these functions that I'm submitting here were identical to
the versions of the functions on my branch, wherever possible; that
branch has a much wider range of functionality (and testsuite tests),
which actually do exercise the functionality that this patch adds.

How does it look?  I guess I need approval both from the symtab side
and from the C++ side.

David Carlton
carlton at math dot stanford dot edu

2003-02-24  David Carlton  <carlton at math dot stanford dot edu>

	* Makefile.in (block.o): Depend on gdb_obstack_h and
	cp_support_h.
	(buildsym.o): Depend on cp_support_h.
	(cp-support.o): Depend on gdb_string_h, demangle_h, gdb_assert_h,
	gdb_obstack_h, symtab_h, and symfile_h.
	* jv-lang.c (get_java_class_symtab): Set BLOCK_NAMESPACE.
	* dwarf2read.c (process_die): Set processing_has_namespace_info,
	processing_current_namespace.
	(read_namespace): Update processing_current_namespace; check for
	anonymous namespaces.
	(dwarf2_name): New.
	(dwarf2_extension): New.
	* cp-support.h: Update copyright, contributors.
	Add inclusion guards.
	Add opaque declaration for struct obstack.
	Add declarations for cp_find_first_component,
	cp_entire_prefix_len, cp_add_using, cp_copy_usings,
	cp_is_anonymous.
	(struct using_direct): New.
	* cp-support.c: Update copyright, contributors.
	Include gdb_assert.h, gdb_obstack.h, symtab.h, symfile.h.
	(cp_find_first_component): New.
	(cp_entire_prefix_len, cp_add_using, cp_copy_usings)
	(cp_is_anonymous, xstrndup): Ditto.
	* buildsym.h: New variables processing_has_namespace_info and
	processing_current_namespace.
	Declare add_using_directive.
	* buildsym.c: Include cp-support.h.
	New variable using_list.
	(add_symbol_to_list): Check for anonymous namespaces.
	(scan_for_anonymous_namespaces): New.
	(add_using_directive): New.
	(finish_block): Set block's scope.
	(start_symtab): Initialize processing_has_namespace_info and
	using_list.
	(end_symtab): Put the using list in the block.
	* block.h: Add opaque declarations for structs
	block_namespace_info, using_direct, and obstack.
	Add declarations for block_set_scope and block_set_using.
	(struct block): Add 'language_specific' member.
	(BLOCK_NAMESPACE): New.
	* block.c: Include gdb_obstack.h and cp-support.h.
	(struct block_namespace_info): New.
	(block_set_scope, block_set_using, block_initialize_namespace):
	Ditto.

Index: block.c
===================================================================
RCS file: /cvs/src/src/gdb/block.c,v
retrieving revision 1.2
diff -u -p -r1.2 block.c
--- block.c	20 Feb 2003 00:01:05 -0000	1.2
+++ block.c	22 Feb 2003 00:47:03 -0000
@@ -23,6 +23,21 @@
 #include "block.h"
 #include "symtab.h"
 #include "symfile.h"
+#include "gdb_obstack.h"
+#include "cp-support.h"
+
+/* This is used by struct block to store namespace-related info for
+   C++ files, namely using declarations and the current namespace in
+   scope.  */
+
+struct block_namespace_info
+{
+  const char *scope;
+  struct using_direct *using;
+};
+
+static void block_initialize_namespace (struct block *block,
+					struct obstack *obstack);
 
 /* Return Nonzero if block a is lexically nested within block b,
    or if a and b have the same pc range.
@@ -138,4 +153,49 @@ struct block *
 block_for_pc (register CORE_ADDR pc)
 {
   return block_for_pc_sect (pc, find_pc_mapped_section (pc));
+}
+
+/* Now come some functions designed to deal with C++ namespace
+   issues.  */
+
+/* Set BLOCK's scope member to SCOPE; if needed, allocate memory via
+   OBSTACK.  (It won't make a copy of SCOPE, however, so that already
+   has to be allocated correctly.)  */
+
+void
+block_set_scope (struct block *block, const char *scope,
+		 struct obstack *obstack)
+{
+  block_initialize_namespace (block, obstack);
+
+  BLOCK_NAMESPACE (block)->scope = scope;
+}
+
+/* Set BLOCK's using member to USING; if needed, allocate memory via
+   OBSTACK.  (It won't make a copy of USING, however, so that already
+   has to be allocated correctly.)  */
+
+void
+block_set_using (struct block *block,
+		 struct using_direct *using,
+		 struct obstack *obstack)
+{
+  block_initialize_namespace (block, obstack);
+
+  BLOCK_NAMESPACE (block)->using = using;
+}
+
+/* If BLOCK_NAMESPACE (block) is NULL, allocate it via OBSTACK and
+   ititialize its members to zero.  */
+
+static void
+block_initialize_namespace (struct block *block, struct obstack *obstack)
+{
+  if (BLOCK_NAMESPACE (block) == NULL)
+    {
+      BLOCK_NAMESPACE (block)
+	= obstack_alloc (obstack, sizeof (struct block_namespace_info));
+      BLOCK_NAMESPACE (block)->scope = NULL;
+      BLOCK_NAMESPACE (block)->using = NULL;
+    }
 }
Index: block.h
===================================================================
RCS file: /cvs/src/src/gdb/block.h,v
retrieving revision 1.2
diff -u -p -r1.2 block.h
--- block.h	20 Feb 2003 00:01:05 -0000	1.2
+++ block.h	22 Feb 2003 00:47:06 -0000
@@ -26,6 +26,9 @@
 
 struct symbol;
 struct symtab;
+struct block_namespace_info;
+struct using_direct;
+struct obstack;
 
 /* All of the name-scope contours of the program
    are represented by `struct block' objects.
@@ -74,6 +77,22 @@ struct block
 
   struct block *superblock;
 
+  /* Used for language-specific info.  */
+
+  union
+  {
+    struct
+    {
+      /* Contains information about namespace-related info relevant to
+	 this block: using directives and the current namespace
+	 scope.  */
+      
+      struct block_namespace_info *namespace;
+    }
+    cplus_specific;
+  }
+  language_specific;
+
   /* Version of GCC used to compile the function corresponding
      to this block, or 0 if not compiled with GCC.  When possible,
      GCC should be compatible with the native compiler, or if that
@@ -120,6 +139,7 @@ struct block
 #define BLOCK_FUNCTION(bl)	(bl)->function
 #define BLOCK_SUPERBLOCK(bl)	(bl)->superblock
 #define BLOCK_GCC_COMPILED(bl)	(bl)->gcc_compile_flag
+#define BLOCK_NAMESPACE(bl)   (bl)->language_specific.cplus_specific.namespace
 #define BLOCK_HASHTABLE(bl)	(bl)->hashtable
 
 /* For blocks without a hashtable (BLOCK_HASHTABLE (bl) == 0) only.  */
@@ -179,5 +199,12 @@ extern struct blockvector *blockvector_f
 extern struct block *block_for_pc (CORE_ADDR);
 
 extern struct block *block_for_pc_sect (CORE_ADDR, asection *);
+
+extern void block_set_scope (struct block *block, const char *scope,
+			     struct obstack *obstack);
+
+extern void block_set_using (struct block *block,
+			     struct using_direct *using,
+			     struct obstack *obstack);
 
 #endif /* BLOCK_H */
Index: buildsym.c
===================================================================
RCS file: /cvs/src/src/gdb/buildsym.c,v
retrieving revision 1.29
diff -u -p -r1.29 buildsym.c
--- buildsym.c	20 Feb 2003 17:17:23 -0000	1.29
+++ buildsym.c	22 Feb 2003 00:46:55 -0000
@@ -44,6 +44,7 @@
 #include "macrotab.h"
 #include "demangle.h"		/* Needed by SYMBOL_INIT_DEMANGLED_NAME.  */
 #include "block.h"
+#include "cp-support.h"
 /* Ask buildsym.h to define the vars it normally declares `extern'.  */
 #define	EXTERN
 /**/
@@ -63,8 +64,15 @@ static struct pending *free_pendings;
    otherwise empty symtab from being tossed.  */
 
 static int have_line_numbers;
+
+/* List of using directives that are active in the current file.  */
+
+static struct using_direct *using_list;
+
 
 static int compare_line_numbers (const void *ln1p, const void *ln2p);
+
+static void scan_for_anonymous_namespaces (struct symbol *symbol);
 
 
 /* Initial sizes of data structures.  These are realloc'd larger if
@@ -91,7 +99,10 @@ add_free_pendings (struct pending *list)}

 }
       
-/* Add a symbol to one of the lists of symbols.  */
+/* Add a symbol to one of the lists of symbols.  While we're at it, if
+   we're in the C++ case and don't have full namespace debugging info,
+   check to see if it references an anonymous namespace; if so, add an
+   appropriate using directive.  */
 
 void
 add_symbol_to_list (struct symbol *symbol, struct pending **listhead)
@@ -122,6 +133,61 @@ add_symbol_to_list (struct symbol *symbo
     }
 
   (*listhead)->symbol[(*listhead)->nsyms++] = symbol;
+
+  /* Check to see if we might need to look for a mention of anonymous
+     namespaces.  */
+  
+   if (SYMBOL_LANGUAGE (symbol) == language_cplus
+       && !processing_has_namespace_info
+       && SYMBOL_CPLUS_DEMANGLED_NAME (symbol) != NULL)
+     scan_for_anonymous_namespaces (symbol);
+}
+
+/* Check to see if a symbol is contained within an anonymous
+   namespace; if so, add an appropriate using directive.  */
+
+/* Optimize away strlen ("(anonymous namespace)").  */
+
+#define ANONYMOUS_NAMESPACE_LEN 21
+
+static void
+scan_for_anonymous_namespaces (struct symbol *symbol)
+{
+  const char *name = SYMBOL_CPLUS_DEMANGLED_NAME (symbol);
+  unsigned int previous_component;
+  unsigned int next_component;
+  const char *len;
+
+  /* Start with a quick-and-dirty check for mention of "(anonymous
+     namespace)".  */
+
+  if (!cp_is_anonymous (name))
+    return;
+
+  previous_component = 0;
+  next_component = cp_find_first_component (name + previous_component);
+
+  while (name[next_component] == ':')
+    {
+      if ((next_component - previous_component) == ANONYMOUS_NAMESPACE_LEN
+	  && strncmp (name + previous_component,
+		      "(anonymous namespace)",
+		      ANONYMOUS_NAMESPACE_LEN) == 0)
+	{
+	  /* We've found a component of the name that's an anonymous
+	     namespace.  So add symbols in it to the namespace given
+	     by the previous component if there is one, or to the
+	     global namespace if there isn't.  */
+	  add_using_directive (name,
+			       previous_component == 0
+			       ? 0 : previous_component - 2,
+			       next_component);
+	}
+      /* The "+ 2" is for the "::".  */
+      previous_component = next_component + 2;
+      next_component = (previous_component
+			+ cp_find_first_component (name + previous_component));
+    }
 }
 
 /* Find a symbol named NAME on a LIST.  NAME need not be
@@ -149,6 +215,34 @@ find_symbol_in_list (struct pending *lis
   return (NULL);
 }
 
+/* Add a using directive to using_list.  NAME is the start of a string
+   that should contain the namespaces we want to add as initial
+   substrings, OUTER_LENGTH is the end of the outer namespace, and
+   INNER_LENGTH is the end of the inner namespace.  If the using
+   directive in question has already been added, don't add it
+   twice.  */
+
+void
+add_using_directive (const char *name, unsigned int outer_length,
+		     unsigned int inner_length)
+{
+  struct using_direct *current;
+  struct using_direct *new;
+
+  /* Has it already been added?  */
+
+  for (current = using_list; current != NULL; current = current->next)
+    {
+      if ((strncmp (current->inner, name, inner_length) == 0)
+	  && (strlen (current->inner) == inner_length)
+	  && (strlen (current->outer) == outer_length))
+	return;
+    }
+
+  using_list = cp_add_using (name, inner_length, outer_length,
+			     using_list);
+}
+
 /* At end of reading syms, or in case of quit, really free as many
    `struct pending's as we can easily find. */
 
@@ -280,6 +374,7 @@ finish_block (struct symbol *symbol, str
   BLOCK_END (block) = end;
   /* Superblock filled in when containing block is made */
   BLOCK_SUPERBLOCK (block) = NULL;
+  BLOCK_NAMESPACE (block) = NULL;
 
   BLOCK_GCC_COMPILED (block) = processing_gcc_compilation;
 
@@ -368,6 +463,41 @@ finish_block (struct symbol *symbol, str
 		}
 	    }
 	}
+
+      /* If we're in the C++ case, record the namespace that the
+	 function was defined in.  Make sure that the name was
+	 originally mangled: if not, there certainly isn't any
+	 namespace information to worry about!  */
+      if (SYMBOL_LANGUAGE (symbol) == language_cplus
+	  && SYMBOL_CPLUS_DEMANGLED_NAME (symbol) != NULL)
+	{
+	  if (processing_has_namespace_info)
+	    {
+	      block_set_scope
+		(block, obsavestring (processing_current_namespace,
+				      strlen (processing_current_namespace),
+				      &objfile->symbol_obstack),
+		 &objfile->symbol_obstack);
+	    }
+	  else
+	    {
+	      /* Try to figure out the appropriate namespace from the
+		 demangled name.  */
+
+	      /* FIXME: carlton/2003-02-21: If the function in
+		 question is a method of a class, the name will
+		 actually include the name of the class as well.  This
+		 should be harmless, but is a little unfortunate.  */
+
+	      const char *name = SYMBOL_CPLUS_DEMANGLED_NAME (symbol);
+	      unsigned int prefix_len = cp_entire_prefix_len (name);
+
+	      block_set_scope (block,
+			       obsavestring (name, prefix_len,
+					     &objfile->symbol_obstack),
+			       &objfile->symbol_obstack);
+	    }
+	}
     }
   else
     {
@@ -799,6 +929,8 @@ start_symtab (char *name, char *dirname,
   global_symbols = NULL;
   within_function = 0;
   have_line_numbers = 0;
+  processing_has_namespace_info = 0;
+  using_list = NULL;
 
   /* Context stack is initially empty.  Allocate first one with room
      for 10 levels; reuse it forever afterward.  */
@@ -931,6 +1063,14 @@ end_symtab (CORE_ADDR end_addr, struct o
       finish_block (0, &global_symbols, 0, last_source_start_addr, end_addr,
 		    objfile);
       blockvector = make_blockvector (objfile);
+      if (using_list != NULL)
+	{
+	  block_set_using (BLOCKVECTOR_BLOCK (blockvector, STATIC_BLOCK),
+			   cp_copy_usings (using_list,
+					   &objfile->symbol_obstack),
+			   &objfile->symbol_obstack);
+	  using_list = NULL;
+	}
     }
 
 #ifndef PROCESS_LINENUMBER_HOOK
Index: buildsym.h
===================================================================
RCS file: /cvs/src/src/gdb/buildsym.h,v
retrieving revision 1.11
diff -u -p -r1.11 buildsym.h
--- buildsym.h	20 Feb 2003 00:01:05 -0000	1.11
+++ buildsym.h	22 Feb 2003 00:46:52 -0000
@@ -85,6 +85,18 @@ EXTERN unsigned char processing_gcc_comp
 
 EXTERN unsigned char processing_acc_compilation;
 
+/* When set, the file that we're processing seems to have debugging
+   info for C++ namespaces, so buildsym.c shouldn't try to guess
+   namespace info itself.  */
+
+EXTERN unsigned char processing_has_namespace_info;
+
+/* If processing_has_namespace_info is nonzero, this string should
+   contain the name of the current namespace.  The string is
+   temporary; copy it if you need it.  */
+
+EXTERN const char *processing_current_namespace;
+
 /* Count symbols as they are processed, for error messages.  */
 
 EXTERN unsigned int symnum;
@@ -228,6 +240,9 @@ extern void add_symbol_to_list (struct s
 
 extern struct symbol *find_symbol_in_list (struct pending *list,
 					   char *name, int length);
+
+extern void add_using_directive (const char *name, unsigned int outer_length,
+				 unsigned int inner_length);
 
 extern void finish_block (struct symbol *symbol,
 			  struct pending **listhead,
Index: cp-support.c
===================================================================
RCS file: /cvs/src/src/gdb/cp-support.c,v
retrieving revision 1.1
diff -u -p -r1.1 cp-support.c
--- cp-support.c	14 Sep 2002 02:09:39 -0000	1.1
+++ cp-support.c	22 Feb 2003 00:46:29 -0000
@@ -1,7 +1,7 @@
 /* Helper routines for C++ support in GDB.
-   Copyright 2002 Free Software Foundation, Inc.
+   Copyright 2002, 2003 Free Software Foundation, Inc.
 
-   Contributed by MontaVista Software.
+   Contributed by MontaVista Software and Stanford University.
 
    This file is part of GDB.
 
@@ -24,6 +24,46 @@
 #include "cp-support.h"
 #include "gdb_string.h"
 #include "demangle.h"
+#include "gdb_assert.h"
+#include "gdb_obstack.h"
+#include "symtab.h"
+#include "symfile.h"
+
+static char *xstrndup (const char *string, size_t len);
+
+/* Here are some random pieces of trivia to keep in mind while trying
+   to take apart demangled names:
+
+   - Names can contain function arguments or templates, so the process
+     has to be, to some extent recursive: maybe keep track of your
+     depth based on encountering <> and ().
+
+   - Parentheses don't just have to happen at the end of a name: they
+     can occur even if the name in question isn't a function, because
+     a template argument might be a type that's a function.
+
+   - Conversely, even if you're trying to deal with a function, its
+     demangled name might not end with ')': it could be a const or
+     volatile class method, in which case it ends with "const" or
+     "volatile".
+
+   - Parentheses are also used in anonymous namespaces: a variable
+     'foo' in an anonymous namespace gets demangled as "(anonymous
+     namespace)::foo".
+
+   - And operator names can contain parentheses or angle brackets.
+     Fortunately, I _think_ that operator names can only occur in a
+     fairly restrictive set of locations (in particular, they have be
+     at depth 0, don't they?).  */
+
+/* NOTE: carlton/2003-02-21: Daniel Jacobowitz came up with an example
+   where operator names don't occur at depth 0.  Sigh.  (It involved a
+   template argument that was a pointer: I hadn't realized that was
+   possible.)  Handling such edge cases does not seem like a
+   high-priority problem to me.  */
+
+/* FIXME: carlton/2003-02-21: Do all the functions here handle all the
+   above considerations correctly?  */
 
 /* Find the last component of the demangled C++ name NAME.  NAME
    must be a method name including arguments, in order to correctly
@@ -138,4 +178,196 @@ method_name_from_physname (const char *p
 
   xfree (demangled_name);
   return ret;
+}
+
+/* This returns the length of first component of NAME, which should be
+   the demangled name of a C++ variable/function/method/etc.
+   Specifically, it returns the index of the first colon forming the
+   boundary of the first component: so, given 'A::foo' or 'A::B::foo'
+   it returns the 1, and given 'foo', it returns 0.  */
+
+/* Well, that's what it should do when called externally, but to make
+   the recursion easier, it also stops if it reaches an unexpected ')'
+   or '>'.  */
+
+/* Let's optimize away calls to strlen("operator").  */
+
+#define LENGTH_OF_OPERATOR 8
+
+unsigned int
+cp_find_first_component (const char *name)
+{
+  /* Names like 'operator<<' screw up the recursion, so let's
+     special-case them.  I _hope_ they can only occur at the start of
+     a component.  */
+
+  unsigned int index = 0;
+
+  if (strncmp (name, "operator", LENGTH_OF_OPERATOR) == 0)
+    {
+      index += LENGTH_OF_OPERATOR;
+      switch (name[index])
+	{
+	case '<':
+	  if (name[index + 1] == '<')
+	    index += 2;
+	  else
+	    index += 1;
+	  break;
+	case '>':
+	case '-':
+	  if (name[index + 1] == '>')
+	    index += 2;
+	  else
+	    index += 1;
+	  break;
+	case '(':
+	  index += 2;
+	  break;
+	default:
+	  index += 1;
+	  break;
+	}
+    }
+
+  for (;; ++index)
+    {
+      switch (name[index])
+	{
+	case '<':
+	  /* Template; eat it up.  The calls to cp_first_component
+	     should only return (I hope!) when they reach the '>'
+	     terminating the component or a '::' between two
+	     components.  (Hence the '+ 2'.)  */
+	  index += 1;
+	  for (index += cp_find_first_component (name + index);
+	       name[index] != '>';
+	       index += cp_find_first_component (name + index))
+	    {
+	      gdb_assert (name[index] == ':');
+	      index += 2;
+	    }
+	  break;
+	case '(':
+	  /* Similar comment as to '<'.  */
+	  index += 1;
+	  for (index += cp_find_first_component (name + index);
+	       name[index] != ')';
+	       index += cp_find_first_component (name + index))
+	    {
+	      gdb_assert (name[index] == ':');
+	      index += 2;
+	    }
+	  break;
+	case '>':
+	case ')':
+	case '\0':
+	case ':':
+	  return index;
+	default:
+	  break;
+	}
+    }
+}
+
+/* If NAME is the fully-qualified name of a C++
+   function/variable/method/etc., this returns the length of its
+   entire prefix: all of the namespaces and classes that make up its
+   name.  Given 'A::foo', it returns 1, given 'A::B::foo', it returns
+   4, given 'foo', it returns 0.  */
+
+unsigned int cp_entire_prefix_len (const char *name)
+{
+  unsigned int current_len = cp_find_first_component (name);
+  unsigned int previous_len = 0;
+
+  while (name[current_len] != '\0')
+    {
+      gdb_assert (name[current_len] == ':');
+      previous_len = current_len;
+      /* Skip the '::'.  */
+      current_len += 2;
+      current_len += cp_find_first_component (name + current_len);
+    }
+
+  return previous_len;
+}
+
+/* Create a new struct using direct whose inner namespace is the
+   initial substring of NAME of leng INNER_LEN and whose outer
+   namespace is the initial substring of NAME of length OUTER_LENGTH.
+   Set its next member in the linked list to NEXT; allocate all memory
+   using xmalloc.  It copies the strings, so NAME can be a temporary
+   string.  */
+
+struct using_direct *
+cp_add_using (const char *name,
+	      unsigned int inner_len,
+	      unsigned int outer_len,
+	      struct using_direct *next)
+{
+  struct using_direct *retval;
+
+  gdb_assert (outer_len < inner_len);
+
+  retval = xmalloc (sizeof (struct using_direct));
+  retval->inner = xstrndup (name, inner_len);
+  retval->outer = xstrndup (name, outer_len);
+  retval->next = next;
+
+  return retval;
+}
+
+/* Make a copy of the using directives in the list pointed to by
+   USING, using OBSTACK to allocate memory.  Free all memory pointed
+   to by USING via xfree.  */
+
+extern struct using_direct *
+cp_copy_usings (struct using_direct *using,
+		struct obstack *obstack)
+{
+  if (using == NULL)
+    {
+      return NULL;
+    }
+  else
+    {
+      struct using_direct *retval
+	= obstack_alloc (obstack, sizeof (struct using_direct));
+      retval->inner = obsavestring (using->inner, strlen (using->inner),
+				    obstack);
+      retval->outer = obsavestring (using->outer, strlen (using->outer),
+				    obstack);
+      retval->next = cp_copy_usings (using->next, obstack);
+
+      xfree (using->inner);
+      xfree (using->outer);
+      xfree (using);
+
+      return retval;
+    }
+}
+
+/* Test whether or not NAMESPACE looks like it mentions an anonymous
+   namespace; return nonzero if so.  */
+
+int
+cp_is_anonymous (const char *namespace)
+{
+  return (strstr (namespace, "(anonymous namespace)")
+	  != NULL);
+}
+
+/* Create a zero-terminated copy of the initial substring of STRING of
+   length LEN.  Allocate memory via xmalloc.  */
+
+static char *
+xstrndup (const char *string, size_t len)
+{
+  char *retval = xmalloc (len + 1);
+
+  strncpy (retval, string, len);
+  retval[len] = '\0';
+
+  return retval;
 }
Index: cp-support.h
===================================================================
RCS file: /cvs/src/src/gdb/cp-support.h,v
retrieving revision 1.1
diff -u -p -r1.1 cp-support.h
--- cp-support.h	14 Sep 2002 02:09:39 -0000	1.1
+++ cp-support.h	22 Feb 2003 00:46:45 -0000
@@ -1,7 +1,7 @@
 /* Helper routines for C++ support in GDB.
-   Copyright 2002 Free Software Foundation, Inc.
+   Copyright 2002, 2003 Free Software Foundation, Inc.
 
-   Contributed by MontaVista Software.
+   Contributed by MontaVista Software and Stanford University.
 
    This file is part of GDB.
 
@@ -20,6 +20,42 @@
    Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.  */
 
+#ifndef CP_SUPPORT_H
+#define CP_SUPPORT_H
+
+/* Opaque declarations.  */
+
+struct obstack;
+
+/* This struct is designed to store data from using directives.  It
+   says that names from namespace INNER should be visible within
+   namespace OUTER.  OUTER should always be a strict initial substring
+   of INNER.  These form a linked list; NEXT is the next element of
+   the list.  */
+
+struct using_direct
+{
+  char *inner;
+  char *outer;
+  struct using_direct *next;
+};
+
 extern char *class_name_from_physname (const char *physname);
 
 extern char *method_name_from_physname (const char *physname);
+
+extern unsigned int cp_find_first_component (const char *name);
+
+extern unsigned int cp_entire_prefix_len (const char *name);
+
+extern struct using_direct *cp_add_using (const char *name,
+					  unsigned int inner_len,
+					  unsigned int outer_len,
+					  struct using_direct *next);
+
+extern struct using_direct *cp_copy_usings (struct using_direct *using,
+					    struct obstack *obstack);
+
+extern int cp_is_anonymous (const char *namespace);
+
+#endif /* CP_SUPPORT_H */
Index: dwarf2read.c
===================================================================
RCS file: /cvs/src/src/gdb/dwarf2read.c,v
retrieving revision 1.87
diff -u -p -r1.87 dwarf2read.c
--- dwarf2read.c	21 Feb 2003 15:24:17 -0000	1.87
+++ dwarf2read.c	22 Feb 2003 00:48:06 -0000
@@ -853,6 +853,10 @@ static void process_die (struct die_info
 
 static char *dwarf2_linkage_name (struct die_info *);
 
+static char *dwarf2_name (struct die_info *die);
+
+static struct die_info *dwarf2_extension (struct die_info *die);
+
 static char *dwarf_tag_name (unsigned int);
 
 static char *dwarf_attr_name (unsigned int);
@@ -1753,6 +1757,11 @@ process_die (struct die_info *die, struc
     case DW_TAG_common_inclusion:
       break;
     case DW_TAG_namespace:
+      if (!processing_has_namespace_info)
+	{
+	  processing_has_namespace_info = 1;
+	  processing_current_namespace = "";
+	}
       read_namespace (die, objfile, cu_header);
       break;
     case DW_TAG_imported_declaration:
@@ -1763,6 +1772,11 @@ process_die (struct die_info *die, struc
 	 shouldn't in the C++ case, but conceivably could in the
 	 Fortran case, so we'll have to replace this gdb_assert if
 	 Fortran compilers start generating that info.  */
+      if (!processing_has_namespace_info)
+	{
+	  processing_has_namespace_info = 1;
+	  processing_current_namespace = "";
+	}
       gdb_assert (!die->has_children);
       break;
     default:
@@ -3157,13 +3171,59 @@ read_common_block (struct die_info *die,
 
 /* Read a C++ namespace.  */
 
-/* FIXME: carlton/2002-10-16: For now, we don't actually do anything
-   useful with the namespace data: we just process its children.  */
-
 static void
 read_namespace (struct die_info *die, struct objfile *objfile,
 		const struct comp_unit_head *cu_header)
 {
+  const char *previous_namespace = processing_current_namespace;
+  const char *name = NULL;
+  int is_anonymous;
+  struct die_info *current_die;
+
+  /* Loop through the extensions until we find a name.  */
+
+  for (current_die = die;
+       current_die != NULL;
+       current_die = dwarf2_extension (die))
+    {
+      name = dwarf2_name (current_die);
+      if (name != NULL)
+	break;
+    }
+
+  /* Is it an anonymous namespace?  */
+
+  is_anonymous = (name == NULL);
+  if (is_anonymous)
+    name = "(anonymous namespace)";
+
+  /* Now build the name of the current namespace.  */
+
+  if (previous_namespace[0] == '\0')
+    {
+      processing_current_namespace = name;
+    }
+  else
+    {
+      /* We need temp_name around because processing_current_namespace
+	 is a const char *.  */
+      char *temp_name = alloca (strlen (previous_namespace)
+				+ 2 + strlen(name) + 1);
+      strcpy (temp_name, previous_namespace);
+      strcat (temp_name, "::");
+      strcat (temp_name, name);
+
+      processing_current_namespace = temp_name;
+    }
+
+  /* If it's an anonymous namespace that we're seeing for the first
+     time, add a using directive.  */
+
+  if (is_anonymous && dwarf_attr (die, DW_AT_extension) == NULL)
+    add_using_directive (processing_current_namespace,
+			 strlen (previous_namespace),
+			 strlen (processing_current_namespace));
+
   if (die->has_children)
     {
       struct die_info *child_die = die->next;
@@ -3174,6 +3234,8 @@ read_namespace (struct die_info *die, st
 	  child_die = sibling_die (child_die);
 	}
     }
+
+  processing_current_namespace = previous_namespace;
 }
 
 /* Extract all information from a DW_TAG_pointer_type DIE and add to
@@ -5638,6 +5700,43 @@ dwarf2_linkage_name (struct die_info *di
   if (attr && DW_STRING (attr))
     return DW_STRING (attr);
   return NULL;
+}
+
+/* Get name of a die, return NULL if not found.  */
+
+static char *
+dwarf2_name (struct die_info *die)
+{
+  struct attribute *attr;
+
+  attr = dwarf_attr (die, DW_AT_name);
+  if (attr && DW_STRING (attr))
+    return DW_STRING (attr);
+  return NULL;
+}
+
+/* Return the die that this die in an extension of, or NULL if there
+   is none.  */
+
+static struct die_info *
+dwarf2_extension (struct die_info *die)
+{
+  struct attribute *attr;
+  struct die_info *extension_die;
+  unsigned int ref;
+
+  attr = dwarf_attr (die, DW_AT_extension);
+  if (attr == NULL)
+    return NULL;
+
+  ref = dwarf2_get_ref_die_offset (attr);
+  extension_die = follow_die_ref (ref);
+  if (!extension_die)
+    {
+      error ("Dwarf Error: Cannot find referent at offset %d.", ref);
+    }
+
+  return extension_die;
 }
 
 /* Convert a DIE tag into its string name.  */
Index: jv-lang.c
===================================================================
RCS file: /cvs/src/src/gdb/jv-lang.c,v
retrieving revision 1.14
diff -u -p -r1.14 jv-lang.c
--- jv-lang.c	20 Feb 2003 00:01:05 -0000	1.14
+++ jv-lang.c	22 Feb 2003 00:46:09 -0000
@@ -118,6 +118,7 @@ get_java_class_symtab (void)
       BLOCK_END (bl) = 0;
       BLOCK_FUNCTION (bl) = NULL;
       BLOCK_SUPERBLOCK (bl) = NULL;
+      BLOCK_NAMESPACE (bl) = NULL;
       BLOCK_GCC_COMPILED (bl) = 0;
       BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK) = bl;
 
Index: Makefile.in
===================================================================
RCS file: /cvs/src/src/gdb/Makefile.in,v
retrieving revision 1.334
diff -u -p -r1.334 Makefile.in
--- Makefile.in	20 Feb 2003 03:12:46 -0000	1.334
+++ Makefile.in	22 Feb 2003 00:46:04 -0000
@@ -1529,7 +1529,8 @@ ax-gdb.o: ax-gdb.c $(defs_h) $(symtab_h)
 	$(target_h) $(ax_h) $(ax_gdb_h) $(gdb_string_h) $(block_h)
 ax-general.o: ax-general.c $(defs_h) $(ax_h) $(value_h) $(gdb_string_h)
 bcache.o: bcache.c $(defs_h) $(gdb_obstack_h) $(bcache_h) $(gdb_string_h)
-block.o: block.c $(defs_h) $(block_h) $(symtab_h) $(symfile_h)
+block.o: block.c $(defs_h) $(block_h) $(symtab_h) $(symfile_h) \
+	$(gdb_obstack_h) $(cp_support_h)
 blockframe.o: blockframe.c $(defs_h) $(symtab_h) $(bfd_h) $(symfile_h) \
 	$(objfiles_h) $(frame_h) $(gdbcore_h) $(value_h) $(target_h) \
 	$(inferior_h) $(annotate_h) $(regcache_h) $(gdb_assert_h) \
@@ -1545,7 +1546,7 @@ buildsym.o: buildsym.c $(defs_h) $(bfd_h
 	$(symfile_h) $(objfiles_h) $(gdbtypes_h) $(gdb_assert_h) \
 	$(complaints_h)	$(gdb_string_h) $(expression_h) $(language_h) \
 	$(bcache_h) $(filenames_h) $(macrotab_h) $(demangle_h) $(buildsym_h) \
-	$(stabsread_h) $(block_h)
+	$(stabsread_h) $(block_h) $(cp_support_h)
 builtin-regs.o: builtin-regs.c $(defs_h) $(builtin_regs_h) $(gdbtypes_h) \
 	$(gdb_string_h) $(gdb_assert_h)
 c-lang.o: c-lang.c $(defs_h) $(symtab_h) $(gdbtypes_h) $(expression_h) \
@@ -1589,7 +1590,9 @@ corelow.o: corelow.c $(defs_h) $(gdb_str
 	$(symtab_h) $(command_h) $(bfd_h) $(target_h) $(gdbcore_h) \
 	$(gdbthread_h) $(regcache_h) $(symfile_h) $(readline_h)
 cp-abi.o: cp-abi.c $(defs_h) $(value_h) $(cp_abi_h) $(gdb_string_h)
-cp-support.o: cp-support.c $(defs_h) $(cp_support_h)
+cp-support.o: cp-support.c $(defs_h) $(cp_support_h) $(gdb_string_h) \
+	$(demangle_h) $(gdb_assert_h) $(gdb_obstack_h) $(symtab_h) \
+	$(symfile_h)
 cp-valprint.o: cp-valprint.c $(defs_h) $(gdb_obstack_h) $(symtab_h) \
 	$(gdbtypes_h) $(expression_h) $(value_h) $(command_h) $(gdbcmd_h) \
 	$(demangle_h) $(annotate_h) $(gdb_string_h) $(c_lang_h) $(target_h) \


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