* [rfa] annotate blocks with C++ namespace information
@ 2003-02-22 1:47 David Carlton
2003-03-11 17:11 ` Daniel Jacobowitz
0 siblings, 1 reply; 15+ messages in thread
From: David Carlton @ 2003-02-22 1:47 UTC (permalink / raw)
To: gdb-patches; +Cc: Elena Zannoni, Daniel Jacobowitz, Jim Blandy
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@math.stanford.edu
2003-02-24 David Carlton <carlton@math.stanford.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;
+
\f
static int compare_line_numbers (const void *ln1p, const void *ln2p);
+
+static void scan_for_anonymous_namespaces (struct symbol *symbol);
\f
/* 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) \
^ permalink raw reply [flat|nested] 15+ messages in thread* Re: [rfa] annotate blocks with C++ namespace information 2003-02-22 1:47 [rfa] annotate blocks with C++ namespace information David Carlton @ 2003-03-11 17:11 ` Daniel Jacobowitz 2003-03-11 21:14 ` David Carlton 2003-03-17 22:33 ` David Carlton 0 siblings, 2 replies; 15+ messages in thread From: Daniel Jacobowitz @ 2003-03-11 17:11 UTC (permalink / raw) To: David Carlton; +Cc: gdb-patches, Elena Zannoni, Jim Blandy On Fri, Feb 21, 2003 at 05:47:35PM -0800, David Carlton wrote: > 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. Well, here's comments from the C++ side. More precisely, I guess, comments from the nit-picking pedantic side... > --- 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'. */ Blank line :) > + /* 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); Use NULL for zero pointers, please. > +/* 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); > +} > + Just checking, but right now this adds namespaces like "foo::(anonymous namespace)" to the using list of "foo"? And eventually, it will add things like "foo::bar" to the using list of "foo"? > + { > + /* 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); > + } > + } Yes, that is a bit unfortunate. It may cause us issues down the line; but we'll deal with it when it comes up. > 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. Two nits: - How about adding "Namespace supported contributed by Stanford University" instead? The file wasn't originally contributed by Stanford. - This may be obvious, but it implies that you have an employer disclaimer from Stanford in addition to a personal assignment, if Stanford is contributing code. I'd just like to double-check that that's accurate. I don't have access to the assignments data. > +/* Here are some random pieces of trivia to keep in mind while trying > + to take apart demangled names: You're adding cp_find_first_component, which seems to me to duplicate logic from find_last_component among other places. I think we have either three or four subtly different copies of this logic now. Is it really necessary? It's not precisely the same (you're going in the other direction) but it would be really nice to condense this. > + - 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". However, in a demangled method-name-with-arguments the rightmost ) is the end of the arguments list. Right? I know we're already using that assumption. > +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) I think that handling operators correctly would be simpler than I thought previously. All we should have to do is, when we hit a '<', check if the preceding word is "operator". It's still not entirely trivial (there might be a space after operator, or not; there must be something indicating word-break or beginning-of-string before operator) but it's pretty simple. If you're not interested in trying this that's OK. I can look at it later once this is used. We should probably expose this function via maint (perhaps "maint cplus first_component"?) so that we can unit-test it. Could you add that to this patch? Creating the new command menu is pretty easy; see MIPS for an example how. There may be better examples. > +/* 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; > } Please put this in utils.c rather than in a C++ support file. The rest looks good to me; let's see what the symtabs/dwarf2 people have to say. -- Daniel Jacobowitz MontaVista Software Debian GNU/Linux Developer ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [rfa] annotate blocks with C++ namespace information 2003-03-11 17:11 ` Daniel Jacobowitz @ 2003-03-11 21:14 ` David Carlton 2003-03-11 21:23 ` Daniel Jacobowitz 2003-03-17 22:33 ` David Carlton 1 sibling, 1 reply; 15+ messages in thread From: David Carlton @ 2003-03-11 21:14 UTC (permalink / raw) To: Daniel Jacobowitz; +Cc: gdb-patches, Elena Zannoni, Jim Blandy On Tue, 11 Mar 2003 12:11:33 -0500, Daniel Jacobowitz <drow@mvista.com> said: > On Fri, Feb 21, 2003 at 05:47:35PM -0800, David Carlton wrote: >> --- 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'. */ > Blank line :) You weren't joking about the 'nit-picking pedantic'. :-) >> + /* 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); > Use NULL for zero pointers, please. They're ints, not pointers. You might be remembering an earlier version of this where I used pointers in this loop; I decided that it was cleaner for cp_find_first_component to return an int instead of a const char *. >> +/* 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); >> +} >> + > Just checking, but right now this adds namespaces like "foo::(anonymous > namespace)" to the using list of "foo"? And eventually, it will add > things like "foo::bar" to the using list of "foo"? Right. >> + { >> + /* 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); >> + } >> + } > Yes, that is a bit unfortunate. It may cause us issues down the line; > but we'll deal with it when it comes up. Yeah, there's some thinking to be done on this matter down the road; we'll want to specify some of this behavior more precisely once we really tackle the nested classes issue. For now, though, it works fine: about all that can happen is that class members might get detected in search for namespace members (once the relevant code has been added to lookup_symbol_aux), which is innocuous enough: they'll already have gotten detected by the is_a_field_of_this test anyways. (Which, of course, opens up the tantalizing possibility of eventually reorganizing all of the namespace/class code to make the is_a_field_of_this test unnecessary, but that's for far in the future.) >> 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. > Two nits: > - How about adding "Namespace supported contributed by Stanford > University" instead? The file wasn't originally contributed by > Stanford. I don't really care one way or another; I just figure that, since I typed most of the characters in that file (at least in the version on the branch), I might as well get some credit. :-) Honestly, though, I'm happy just to leave it as "Contributed by MontaVista Software." > - This may be obvious, but it implies that you have an employer > disclaimer from Stanford in addition to a personal assignment, if > Stanford is contributing code. I'd just like to double-check that > that's accurate. I don't have access to the assignments data. My situation is, I am learning (because I get asked this question not infrequently), a bit unusual: no, I don't have an employer disclaimer from Stanford, but that's okay since the FSF and I agree that I don't need one. Stanford's employment contract for faculty members makes it quite clear that Stanford doesn't own the copyright for software that I write (with some exceptions that clearly don't apply to me), even if I write it using Stanford's computers. (Similarly, Stanford doesn't own the copyright to articles or books that I write.) I'm just mentioning Stanford out of politeness. >> +/* Here are some random pieces of trivia to keep in mind while trying >> + to take apart demangled names: > You're adding cp_find_first_component, which seems to me to duplicate > logic from find_last_component among other places. I think we have > either three or four subtly different copies of this logic now. Is it > really necessary? It's not precisely the same (you're going in the > other direction) but it would be really nice to condense this. Yes; it should be easy enough to rewrite, say, find_last_component using cp_find_first_component. The only reason why I didn't do that (other than laziness) was that then I'd want to go figure out a situation where find_last_component actually gets called, to make sure I didn't make a boneheaded mistake while doing so, and I didn't feel like doing that. But I'll definitely add a FIXME comment about that? >> + - 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". > However, in a demangled method-name-with-arguments the rightmost ) is > the end of the arguments list. Right? I know we're already using that > assumption. Right. >> +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) > I think that handling operators correctly would be simpler than I > thought previously. All we should have to do is, when we hit a '<', > check if the preceding word is "operator". It's still not entirely > trivial (there might be a space after operator, or not; there must be > something indicating word-break or beginning-of-string before operator) > but it's pretty simple. There's also operator-> to worry about. And, now that you mention it, I'm not handling the possibility of whitespace between 'operator' and the actual operator. The function isn't entirely bullet-proof when given user input (you could construct malformed user input containing single colons that would cause it problems, I think); I should probably add a comment saying it's intended for internal use only, at least for now. (Though the similar function in linespec.c has the same flaw.) Do any demanglers put in spaces after 'operator'? I hope not... > If you're not interested in trying this that's OK. I can look at it > later once this is used. We should probably expose this function via > maint (perhaps "maint cplus first_component"?) so that we can unit-test > it. Could you add that to this patch? Creating the new command menu > is pretty easy; see MIPS for an example how. There may be better > examples. Okay, will do. >> +/* 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; >> } > Please put this in utils.c rather than in a C++ support file. Sure, that makes sense. > The rest looks good to me; let's see what the symtabs/dwarf2 people > have to say. Thanks! David Carlton carlton@math.stanford.edu ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [rfa] annotate blocks with C++ namespace information 2003-03-11 21:14 ` David Carlton @ 2003-03-11 21:23 ` Daniel Jacobowitz 2003-03-11 22:43 ` David Carlton 0 siblings, 1 reply; 15+ messages in thread From: Daniel Jacobowitz @ 2003-03-11 21:23 UTC (permalink / raw) To: gdb-patches On Tue, Mar 11, 2003 at 01:14:16PM -0800, David Carlton wrote: > On Tue, 11 Mar 2003 12:11:33 -0500, Daniel Jacobowitz <drow@mvista.com> said: > > On Fri, Feb 21, 2003 at 05:47:35PM -0800, David Carlton wrote: > > >> --- 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'. */ > > > Blank line :) > > You weren't joking about the 'nit-picking pedantic'. :-) > > >> + /* 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); > > > Use NULL for zero pointers, please. > > They're ints, not pointers. You might be remembering an earlier > version of this where I used pointers in this loop; I decided that it > was cleaner for cp_find_first_component to return an int instead of a > const char *. Oh, right. > I don't really care one way or another; I just figure that, since I > typed most of the characters in that file (at least in the version on > the branch), I might as well get some credit. :-) Honestly, though, > I'm happy just to leave it as "Contributed by MontaVista Software." That wasn't one of the options :) You should certainly ave your name in there. > > - This may be obvious, but it implies that you have an employer > > disclaimer from Stanford in addition to a personal assignment, if > > Stanford is contributing code. I'd just like to double-check that > > that's accurate. I don't have access to the assignments data. > > My situation is, I am learning (because I get asked this question not > infrequently), a bit unusual: no, I don't have an employer disclaimer > from Stanford, but that's okay since the FSF and I agree that I don't > need one. Stanford's employment contract for faculty members makes it > quite clear that Stanford doesn't own the copyright for software that > I write (with some exceptions that clearly don't apply to me), even if > I write it using Stanford's computers. (Similarly, Stanford doesn't > own the copyright to articles or books that I write.) I'm just > mentioning Stanford out of politeness. In this case Stanford is not contributing the code; contribution is a copyright related action, and they never had the copyright. Please say "Namespace support contributed by David Carlton" instead of "Contributed by Stanford University". > >> +/* Here are some random pieces of trivia to keep in mind while trying > >> + to take apart demangled names: > > > You're adding cp_find_first_component, which seems to me to duplicate > > logic from find_last_component among other places. I think we have > > either three or four subtly different copies of this logic now. Is it > > really necessary? It's not precisely the same (you're going in the > > other direction) but it would be really nice to condense this. > > Yes; it should be easy enough to rewrite, say, find_last_component > using cp_find_first_component. The only reason why I didn't do that > (other than laziness) was that then I'd want to go figure out a > situation where find_last_component actually gets called, to make sure > I didn't make a boneheaded mistake while doing so, and I didn't feel > like doing that. But I'll definitely add a FIXME comment about that? OK. find_last_component gets called for dealing with stub methods, if I recall correctly why I wrote it. > >> +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) > > > I think that handling operators correctly would be simpler than I > > thought previously. All we should have to do is, when we hit a '<', > > check if the preceding word is "operator". It's still not entirely > > trivial (there might be a space after operator, or not; there must be > > something indicating word-break or beginning-of-string before operator) > > but it's pretty simple. > > There's also operator-> to worry about. And, now that you mention it, > I'm not handling the possibility of whitespace between 'operator' and > the actual operator. The function isn't entirely bullet-proof when > given user input (you could construct malformed user input containing > single colons that would cause it problems, I think); I should > probably add a comment saying it's intended for internal use only, at > least for now. (Though the similar function in linespec.c has the > same flaw.) Do any demanglers put in spaces after 'operator'? I hope > not... I thought one of them did, but I might have been mistaken. -- Daniel Jacobowitz MontaVista Software Debian GNU/Linux Developer ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [rfa] annotate blocks with C++ namespace information 2003-03-11 21:23 ` Daniel Jacobowitz @ 2003-03-11 22:43 ` David Carlton 2003-03-11 22:59 ` David Carlton 0 siblings, 1 reply; 15+ messages in thread From: David Carlton @ 2003-03-11 22:43 UTC (permalink / raw) To: Daniel Jacobowitz; +Cc: gdb-patches On Tue, 11 Mar 2003 16:23:13 -0500, Daniel Jacobowitz <drow@mvista.com> said: > On Tue, Mar 11, 2003 at 01:14:16PM -0800, David Carlton wrote: > In this case Stanford is not contributing the code; contribution is a > copyright related action, and they never had the copyright. Fair enough. >> Yes; it should be easy enough to rewrite, say, find_last_component >> using cp_find_first_component. The only reason why I didn't do >> that (other than laziness) was that then I'd want to go figure out >> a situation where find_last_component actually gets called, to make >> sure I didn't make a boneheaded mistake while doing so, and I >> didn't feel like doing that. But I'll definitely add a FIXME >> comment about that? > OK. find_last_component gets called for dealing with stub methods, if > I recall correctly why I wrote it. Yes, that's right. And all I know about them is that I just spent an afternoon investigating a bug dealing with them, didn't succeed in fixing the bug, and felt dirty afterwards. >> Do any demanglers put in spaces after 'operator'? I hope not... > I thought one of them did, but I might have been mistaken. You could easily be right: I haven't done a lot of GCC v2 checking. I'll look into that, and, if so, add a guard for a possible extra space in cp_find_first_component. (Otherwise, the gdb_assert in cp_find_first_component could leave people unable to load C++ files with certain constructs mixing templates and operators.) Not that there's any justification for having the different demanglers producing different output, but that's another issue entirely... David Carlton carlton@math.stanford.edu ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [rfa] annotate blocks with C++ namespace information 2003-03-11 22:43 ` David Carlton @ 2003-03-11 22:59 ` David Carlton 2003-03-11 23:01 ` Daniel Jacobowitz 0 siblings, 1 reply; 15+ messages in thread From: David Carlton @ 2003-03-11 22:59 UTC (permalink / raw) To: Daniel Jacobowitz; +Cc: gdb-patches On 11 Mar 2003 14:43:19 -0800, David Carlton <carlton@math.stanford.edu> said: > On Tue, 11 Mar 2003 16:23:13 -0500, Daniel Jacobowitz <drow@mvista.com> said: >> On Tue, Mar 11, 2003 at 01:14:16PM -0800, David Carlton wrote: >>> Do any demanglers put in spaces after 'operator'? I hope not... >> I thought one of them did, but I might have been mistaken. > You could easily be right: I haven't done a lot of GCC v2 checking. > I'll look into that, and, if so, add a guard for a possible extra > space in cp_find_first_component. Yeah, I'll add it: with gcc 2.95.3 -gdwarf-2, I get: .ascii "operator ->\0" # DW_AT_name .byte 0x1 # DW_AT_decl_file .byte 0x3 # DW_AT_decl_line .ascii "__rf__1C\0" # DW_AT_MIPS_linkage_name and while the demangler doesn't put in a space when demangling __rf__1C, there's still that space in the DW_AT_name. And while I think that most internal uses of operator names within GDB will come via the demangler, it's probably not wise to bet that they all do. David Carlton carlton@math.stanford.edu ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [rfa] annotate blocks with C++ namespace information 2003-03-11 22:59 ` David Carlton @ 2003-03-11 23:01 ` Daniel Jacobowitz 0 siblings, 0 replies; 15+ messages in thread From: Daniel Jacobowitz @ 2003-03-11 23:01 UTC (permalink / raw) To: David Carlton; +Cc: gdb-patches On Tue, Mar 11, 2003 at 02:58:56PM -0800, David Carlton wrote: > On 11 Mar 2003 14:43:19 -0800, David Carlton <carlton@math.stanford.edu> said: > > On Tue, 11 Mar 2003 16:23:13 -0500, Daniel Jacobowitz <drow@mvista.com> said: > >> On Tue, Mar 11, 2003 at 01:14:16PM -0800, David Carlton wrote: > > >>> Do any demanglers put in spaces after 'operator'? I hope not... > > >> I thought one of them did, but I might have been mistaken. > > > You could easily be right: I haven't done a lot of GCC v2 checking. > > I'll look into that, and, if so, add a guard for a possible extra > > space in cp_find_first_component. > > Yeah, I'll add it: with gcc 2.95.3 -gdwarf-2, I get: > > .ascii "operator ->\0" # DW_AT_name > .byte 0x1 # DW_AT_decl_file > .byte 0x3 # DW_AT_decl_line > .ascii "__rf__1C\0" # DW_AT_MIPS_linkage_name > > and while the demangler doesn't put in a space when demangling > __rf__1C, there's still that space in the DW_AT_name. And while I > think that most internal uses of operator names within GDB will come > via the demangler, it's probably not wise to bet that they all do. Good. Especially since, going forward, more of them will not come via the demangler. I'm just waiting for better namespace handling to settle in before I get back to that. -- Daniel Jacobowitz MontaVista Software Debian GNU/Linux Developer ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [rfa] annotate blocks with C++ namespace information 2003-03-11 17:11 ` Daniel Jacobowitz 2003-03-11 21:14 ` David Carlton @ 2003-03-17 22:33 ` David Carlton 2003-04-14 19:22 ` Elena Zannoni 1 sibling, 1 reply; 15+ messages in thread From: David Carlton @ 2003-03-17 22:33 UTC (permalink / raw) To: gdb-patches; +Cc: Daniel Jacobowitz, Elena Zannoni, Jim Blandy Just for reference, here's a slightly updated version of my namespace patch, following Daniel's suggestions. The only real change is that it adds a new command "maint cplus first_component" and a new file gdb.c++/maint.exp to test it. David Carlton carlton@math.stanford.edu 2003-03-17 David Carlton <carlton@math.stanford.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, symfile_h, and gdbcmd_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 function. (dwarf2_extension): Ditto. * 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 struct. * cp-support.c: Update copyright, contributors. Include gdb_assert.h, gdb_obstack.h, symtab.h, symfile.h, gdbcmd.h, ctype.h. New variable maint_cplus_cmd_list. (cp_find_first_component): New function. (cp_entire_prefix_len, cp_add_using, cp_copy_usings) (cp_is_anonymous, maint_cplus_command, first_component_command) (_initialize_cp_support): 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 function. (add_using_directive): Ditto. (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 macro. * block.c: Include gdb_obstack.h and cp-support.h. (struct block_namespace_info): New struct. (block_set_scope): New function. (block_set_using, block_initialize_namespace): Ditto. 2003-03-17 David Carlton <carlton@math.stanford.edu> * gdb.c++/maint.exp: New file. Index: Makefile.in =================================================================== RCS file: /cvs/src/src/gdb/Makefile.in,v retrieving revision 1.341 diff -u -p -r1.341 Makefile.in --- Makefile.in 5 Mar 2003 18:07:15 -0000 1.341 +++ Makefile.in 17 Mar 2003 21:50:37 -0000 @@ -1535,7 +1535,8 @@ ax-gdb.o: ax-gdb.c $(defs_h) $(symtab_h) $(regcache_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) \ @@ -1551,7 +1552,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) \ @@ -1596,7 +1597,9 @@ corelow.o: corelow.c $(defs_h) $(gdb_str $(gdbthread_h) $(regcache_h) $(symfile_h) $(readline_h) cp-abi.o: cp-abi.c $(defs_h) $(value_h) $(cp_abi_h) $(command_h) \ $(gdbcmd_h) $(ui_out_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) $(gdbcmd_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: jv-lang.c =================================================================== RCS file: /cvs/src/src/gdb/jv-lang.c,v retrieving revision 1.15 diff -u -p -r1.15 jv-lang.c --- jv-lang.c 25 Feb 2003 21:36:18 -0000 1.15 +++ jv-lang.c 17 Mar 2003 21:52:04 -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: dwarf2read.c =================================================================== RCS file: /cvs/src/src/gdb/dwarf2read.c,v retrieving revision 1.88 diff -u -p -r1.88 dwarf2read.c --- dwarf2read.c 25 Feb 2003 21:36:17 -0000 1.88 +++ dwarf2read.c 17 Mar 2003 21:51:42 -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: 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 17 Mar 2003 21:51:29 -0000 @@ -1,7 +1,8 @@ /* Helper routines for C++ support in GDB. - Copyright 2002 Free Software Foundation, Inc. + Copyright 2002, 2003 Free Software Foundation, Inc. Contributed by MontaVista Software. + Namespace support contributed by David Carlton. This file is part of GDB. @@ -20,6 +21,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: 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 17 Mar 2003 21:51:22 -0000 @@ -1,7 +1,8 @@ /* Helper routines for C++ support in GDB. - Copyright 2002 Free Software Foundation, Inc. + Copyright 2002, 2003 Free Software Foundation, Inc. Contributed by MontaVista Software. + Namespace support contributed by David Carlton. This file is part of GDB. @@ -21,9 +22,59 @@ Boston, MA 02111-1307, USA. */ #include "defs.h" +#include <ctype.h> #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" +#include "gdbcmd.h" + +/* The list of "maint cplus" commands. */ + +static struct cmd_list_element *maint_cplus_cmd_list = NULL; + +/* The actual commands. */ + +static void maint_cplus_command (char *arg, int from_tty); +static void first_component_command (char *arg, int from_tty); + +/* 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-03-13: We have several functions here with + overlapping functionality; can we combine them? Also, do they + 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 +189,229 @@ 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 '>'. */ + +/* NOTE: carlton/2003-03-13: This function is currently only intended + for internal use: it's probably not entirely safe when called on + user-generated input, because some of the 'index += 2' lines might + go past the end of malformed input. */ + +/* 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; + while (isspace(name[index])) + ++index; + 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 = savestring (name, inner_len); + retval->outer = savestring (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); +} + +/* Don't allow just "maintenance cplus". */ + +static void +maint_cplus_command (char *arg, int from_tty) +{ + printf_unfiltered ("\"maintenance cplus\" must be followed by the name of a command.\n"); + help_list (maint_cplus_cmd_list, "maintenance cplus ", -1, gdb_stdout); +} + +/* This is a front end for cp_find_first_component, for unit testing. + Be careful when using it: see the NOTE above + cp_find_first_component. */ + +static void +first_component_command (char *arg, int from_tty) +{ + int len = cp_find_first_component (arg); + char *prefix = alloca (len + 1); + + memcpy (prefix, arg, len); + prefix[len] = '\0'; + + printf_unfiltered ("%s\n", prefix); +} + +void +_initialize_cp_support (void) +{ + add_prefix_cmd ("cplus", class_maintenance, maint_cplus_command, + "C++ maintenance commands.", &maint_cplus_cmd_list, + "maintenance cplus ", 0, &maintenancelist); + add_alias_cmd ("cp", "cplus", class_maintenance, 1, &maintenancelist); + + add_cmd ("first_component", class_maintenance, first_component_command, + "Print the first class/namespace component of NAME.", + &maint_cplus_cmd_list); + } 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 17 Mar 2003 21:51:06 -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: buildsym.c =================================================================== RCS file: /cvs/src/src/gdb/buildsym.c,v retrieving revision 1.31 diff -u -p -r1.31 buildsym.c --- buildsym.c 25 Feb 2003 21:36:17 -0000 1.31 +++ buildsym.c 17 Mar 2003 21:51:00 -0000 @@ -44,6 +44,8 @@ #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 +65,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; + \f static int compare_line_numbers (const void *ln1p, const void *ln2p); + +static void scan_for_anonymous_namespaces (struct symbol *symbol); \f /* Initial sizes of data structures. These are realloc'd larger if @@ -91,7 +100,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 +134,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 +216,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 +375,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; @@ -372,6 +468,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 { @@ -803,6 +934,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. */ @@ -935,6 +1068,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: 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 17 Mar 2003 21:50:53 -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: 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 17 Mar 2003 21:50:44 -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; + } } --- /dev/null Thu Apr 11 07:25:15 2002 +++ maint.exp Mon Mar 17 13:33:14 2003 @@ -0,0 +1,79 @@ +# Copyright 2003 Free Software Foundation Inc. + +# 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 of the License, 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, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +# Please email any bugs, comments, and/or additions to this file to: +# bug-gdb@prep.ai.mit.edu + + +# This file tests C++-specific maintenance commands and help on those. + +# Currently, no source file is used. + +if $tracelevel then { + strace $tracelevel + } + +# Test the help messages. + +proc test_help {} { + gdb_test "help maintenance cplus" "C\\+\\+ maintenance commands.\r\n\r\nList of maintenance cplus subcommands:\r\n\r\nmaintenance cplus first_component -- Print the first class/namespace component of NAME\r\n\r\nType \"help maintenance cplus\" followed by maintenance cplus subcommand name for full documentation.\r\nCommand name abbreviations are allowed if unambiguous." + + gdb_test "help maint cp" "C\\+\\+ maintenance commands.\r\n\r\nList of maintenance cplus subcommands:\r\n\r\nmaintenance cplus first_component -- Print the first class/namespace component of NAME\r\n\r\nType \"help maintenance cplus\" followed by maintenance cplus subcommand name for full documentation.\r\nCommand name abbreviations are allowed if unambiguous." + + gdb_test "maint cp" "\"maintenance cplus\" must be followed by the name of a command.\r\nList of maintenance cplus subcommands:\r\n\r\nmaintenance cplus first_component -- Print the first class/namespace component of NAME\r\n\r\nType \"help maintenance cplus\" followed by maintenance cplus subcommand name for full documentation.\r\nCommand name abbreviations are allowed if unambiguous." + + gdb_test "help maint cp first_component" "Print the first class/namespace component of NAME." +} + +# This is used when NAME should contain only a single component. Be +# careful to make sure that parentheses get escaped properly. +proc test_single_component {name} { + set matchname [string_to_regexp "$name"] + gdb_test "maint cp first_component $name" "$matchname" +} + +proc test_first_component {} { + test_single_component "foo" + test_single_component "operator<<" + test_single_component "operator>>" + test_single_component "operator ->" + test_single_component "operator()" + test_single_component "operator>" + test_single_component "operator<" + test_single_component "operator ->" + test_single_component "operator ->" + + test_single_component "foo()" + test_single_component "foo(int)" + test_single_component "foo(X::Y)" + test_single_component "foo(X::Y, A::B)" + test_single_component "foo(std::basic_streambuf<wchar_t,std::char_traits<wchar_t> >)" + test_single_component "operator>(X::Y)" + + gdb_test "maint cp first_component foo::bar" "foo" + gdb_test "maint cp first_component foo::bar::baz" "foo" + gdb_test "maint cp first_component C<A>::bar" "C<A>" + gdb_test "maint cp first_component C<std::basic_streambuf<wchar_t,std::char_traits<wchar_t> > >::bar" "C<std::basic_streambuf<wchar_t,std::char_traits<wchar_t> > >" +} + +gdb_exit +gdb_start + +test_help +test_first_component + +gdb_exit +return 0 ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [rfa] annotate blocks with C++ namespace information 2003-03-17 22:33 ` David Carlton @ 2003-04-14 19:22 ` Elena Zannoni 2003-04-14 21:33 ` David Carlton 0 siblings, 1 reply; 15+ messages in thread From: Elena Zannoni @ 2003-04-14 19:22 UTC (permalink / raw) To: David Carlton; +Cc: gdb-patches, Daniel Jacobowitz, Elena Zannoni, Jim Blandy David Carlton writes: > Just for reference, here's a slightly updated version of my namespace > patch, following Daniel's suggestions. The only real change is that > it adds a new command "maint cplus first_component" and a new file > gdb.c++/maint.exp to test it. Ok, I got around to this finally. It is basically ok, except for the line between what is c++ and what is symbol table stuff. I think that more stuff can be pushed into cp-support.c. See below... > > David Carlton > carlton@math.stanford.edu > > 2003-03-17 David Carlton <carlton@math.stanford.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, symfile_h, and gdbcmd_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 function. > (dwarf2_extension): Ditto. > * 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 struct. > * cp-support.c: Update copyright, contributors. > Include gdb_assert.h, gdb_obstack.h, symtab.h, symfile.h, > gdbcmd.h, ctype.h. > New variable maint_cplus_cmd_list. > (cp_find_first_component): New function. > (cp_entire_prefix_len, cp_add_using, cp_copy_usings) > (cp_is_anonymous, maint_cplus_command, first_component_command) > (_initialize_cp_support): 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 function. > (add_using_directive): Ditto. > (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 macro. > * block.c: Include gdb_obstack.h and cp-support.h. > (struct block_namespace_info): New struct. > (block_set_scope): New function. > (block_set_using, block_initialize_namespace): Ditto. > > 2003-03-17 David Carlton <carlton@math.stanford.edu> > > * gdb.c++/maint.exp: New file. > > Index: Makefile.in > =================================================================== > RCS file: /cvs/src/src/gdb/Makefile.in,v > retrieving revision 1.341 > diff -u -p -r1.341 Makefile.in > --- Makefile.in 5 Mar 2003 18:07:15 -0000 1.341 > +++ Makefile.in 17 Mar 2003 21:50:37 -0000 > @@ -1535,7 +1535,8 @@ ax-gdb.o: ax-gdb.c $(defs_h) $(symtab_h) > $(regcache_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) \ > @@ -1551,7 +1552,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) \ > @@ -1596,7 +1597,9 @@ corelow.o: corelow.c $(defs_h) $(gdb_str > $(gdbthread_h) $(regcache_h) $(symfile_h) $(readline_h) > cp-abi.o: cp-abi.c $(defs_h) $(value_h) $(cp_abi_h) $(command_h) \ > $(gdbcmd_h) $(ui_out_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) $(gdbcmd_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: jv-lang.c > =================================================================== > RCS file: /cvs/src/src/gdb/jv-lang.c,v > retrieving revision 1.15 > diff -u -p -r1.15 jv-lang.c > --- jv-lang.c 25 Feb 2003 21:36:18 -0000 1.15 > +++ jv-lang.c 17 Mar 2003 21:52:04 -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: dwarf2read.c > =================================================================== > RCS file: /cvs/src/src/gdb/dwarf2read.c,v > retrieving revision 1.88 > diff -u -p -r1.88 dwarf2read.c > --- dwarf2read.c 25 Feb 2003 21:36:17 -0000 1.88 > +++ dwarf2read.c 17 Mar 2003 21:51:42 -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. */ dwarf2read.c changes OK. > 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 17 Mar 2003 21:51:29 -0000 > @@ -1,7 +1,8 @@ > /* Helper routines for C++ support in GDB. > - Copyright 2002 Free Software Foundation, Inc. > + Copyright 2002, 2003 Free Software Foundation, Inc. > > Contributed by MontaVista Software. > + Namespace support contributed by David Carlton. > > This file is part of GDB. > > @@ -20,6 +21,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: cp-support.c I am going to ignore this file, you and Daniel already settled it, right? > =================================================================== > 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 17 Mar 2003 21:51:22 -0000 > @@ -1,7 +1,8 @@ > /* Helper routines for C++ support in GDB. > - Copyright 2002 Free Software Foundation, Inc. > + Copyright 2002, 2003 Free Software Foundation, Inc. > > Contributed by MontaVista Software. > + Namespace support contributed by David Carlton. > > This file is part of GDB. > > @@ -21,9 +22,59 @@ > Boston, MA 02111-1307, USA. */ > > #include "defs.h" > +#include <ctype.h> > #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" > +#include "gdbcmd.h" > + > +/* The list of "maint cplus" commands. */ > + > +static struct cmd_list_element *maint_cplus_cmd_list = NULL; > + > +/* The actual commands. */ > + > +static void maint_cplus_command (char *arg, int from_tty); > +static void first_component_command (char *arg, int from_tty); > + > +/* 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-03-13: We have several functions here with > + overlapping functionality; can we combine them? Also, do they > + 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 +189,229 @@ 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 '>'. */ > + > +/* NOTE: carlton/2003-03-13: This function is currently only intended > + for internal use: it's probably not entirely safe when called on > + user-generated input, because some of the 'index += 2' lines might > + go past the end of malformed input. */ > + > +/* 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; > + while (isspace(name[index])) > + ++index; > + 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 = savestring (name, inner_len); > + retval->outer = savestring (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); > +} > + > +/* Don't allow just "maintenance cplus". */ > + > +static void > +maint_cplus_command (char *arg, int from_tty) > +{ > + printf_unfiltered ("\"maintenance cplus\" must be followed by the name of a command.\n"); > + help_list (maint_cplus_cmd_list, "maintenance cplus ", -1, gdb_stdout); > +} > + > +/* This is a front end for cp_find_first_component, for unit testing. > + Be careful when using it: see the NOTE above > + cp_find_first_component. */ > + > +static void > +first_component_command (char *arg, int from_tty) > +{ > + int len = cp_find_first_component (arg); > + char *prefix = alloca (len + 1); > + > + memcpy (prefix, arg, len); > + prefix[len] = '\0'; > + > + printf_unfiltered ("%s\n", prefix); > +} > + > +void > +_initialize_cp_support (void) > +{ > + add_prefix_cmd ("cplus", class_maintenance, maint_cplus_command, > + "C++ maintenance commands.", &maint_cplus_cmd_list, > + "maintenance cplus ", 0, &maintenancelist); > + add_alias_cmd ("cp", "cplus", class_maintenance, 1, &maintenancelist); > + > + add_cmd ("first_component", class_maintenance, first_component_command, > + "Print the first class/namespace component of NAME.", > + &maint_cplus_cmd_list); > + > } > 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 17 Mar 2003 21:51:06 -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: buildsym.c > =================================================================== > RCS file: /cvs/src/src/gdb/buildsym.c,v > retrieving revision 1.31 > diff -u -p -r1.31 buildsym.c > --- buildsym.c 25 Feb 2003 21:36:17 -0000 1.31 > +++ buildsym.c 17 Mar 2003 21:51:00 -0000 > @@ -44,6 +44,8 @@ > #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 +65,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; > + see below for some thoughts..... > \f > static int compare_line_numbers (const void *ln1p, const void *ln2p); > + > +static void scan_for_anonymous_namespaces (struct symbol *symbol); > \f > > /* Initial sizes of data structures. These are realloc'd larger if > @@ -91,7 +100,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 +134,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); > +} OK. > + > +/* 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)); > + } > } > Should this function be in cp-support.c? You could tighten the interface a bit, by passing in the name of the symbol only, instead of the symbol. This way it would definitely belong in cp-support.c (...dirty plan to delegate c++ stuff to Daniel in an inequivocable manner..). > /* Find a symbol named NAME on a LIST. NAME need not be > @@ -149,6 +216,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); > +} > + This too, could be in cp-support.c, it has no knowledge of symbols, etc. Hmm, but it touches using_list. Which opens the question of whether using_list should be in cp-support.c as well, so that the interface is just using-list, exported in cp-support.h. It would certainly make things cleaner. Not to mention that then you can check stuff in w/o waiting for me. The struct using_direct could also be made opaque, I think. > /* At end of reading syms, or in case of quit, really free as many > `struct pending's as we can easily find. */ > > @@ -280,6 +375,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; > > @@ -372,6 +468,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 > { > @@ -803,6 +934,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; > processing_has_namespace_info: there is one per symtab, yes? I mean, it is set to 0 only here, just want to make sure. > /* Context stack is initially empty. Allocate first one with room > for 10 levels; reuse it forever afterward. */ > @@ -935,6 +1068,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, Should this function be called something like cp_dup_usings? But you are freeing the original copy, right? Why do you need to copy it over? > + &objfile->symbol_obstack), > + &objfile->symbol_obstack); > + using_list = NULL; If you move using_list in cp-support.c you could even introduce a method that sets it to NULL, and another to check if it is null. Nothing else would be needed, all other manipulations would be done in cp-support.c. > + } > } > > #ifndef PROCESS_LINENUMBER_HOOK > 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 17 Mar 2003 21:50:53 -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: 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 17 Mar 2003 21:50:44 -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; > + } > } block.c changes OK. The below is not my to approve. > --- /dev/null Thu Apr 11 07:25:15 2002 > +++ maint.exp Mon Mar 17 13:33:14 2003 > @@ -0,0 +1,79 @@ > +# Copyright 2003 Free Software Foundation Inc. > + > +# 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 of the License, 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, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. > + > +# Please email any bugs, comments, and/or additions to this file to: > +# bug-gdb@prep.ai.mit.edu > + > + > +# This file tests C++-specific maintenance commands and help on those. > + > +# Currently, no source file is used. > + > +if $tracelevel then { > + strace $tracelevel > + } > + > +# Test the help messages. > + > +proc test_help {} { > + gdb_test "help maintenance cplus" "C\\+\\+ maintenance commands.\r\n\r\nList of maintenance cplus subcommands:\r\n\r\nmaintenance cplus first_component -- Print the first class/namespace component of NAME\r\n\r\nType \"help maintenance cplus\" followed by maintenance cplus subcommand name for full documentation.\r\nCommand name abbreviations are allowed if unambiguous." > + > + gdb_test "help maint cp" "C\\+\\+ maintenance commands.\r\n\r\nList of maintenance cplus subcommands:\r\n\r\nmaintenance cplus first_component -- Print the first class/namespace component of NAME\r\n\r\nType \"help maintenance cplus\" followed by maintenance cplus subcommand name for full documentation.\r\nCommand name abbreviations are allowed if unambiguous." > + > + gdb_test "maint cp" "\"maintenance cplus\" must be followed by the name of a command.\r\nList of maintenance cplus subcommands:\r\n\r\nmaintenance cplus first_component -- Print the first class/namespace component of NAME\r\n\r\nType \"help maintenance cplus\" followed by maintenance cplus subcommand name for full documentation.\r\nCommand name abbreviations are allowed if unambiguous." > + > + gdb_test "help maint cp first_component" "Print the first class/namespace component of NAME." > +} > + > +# This is used when NAME should contain only a single component. Be > +# careful to make sure that parentheses get escaped properly. > +proc test_single_component {name} { > + set matchname [string_to_regexp "$name"] > + gdb_test "maint cp first_component $name" "$matchname" > +} > + > +proc test_first_component {} { > + test_single_component "foo" > + test_single_component "operator<<" > + test_single_component "operator>>" > + test_single_component "operator ->" > + test_single_component "operator()" > + test_single_component "operator>" > + test_single_component "operator<" > + test_single_component "operator ->" > + test_single_component "operator ->" > + > + test_single_component "foo()" > + test_single_component "foo(int)" > + test_single_component "foo(X::Y)" > + test_single_component "foo(X::Y, A::B)" > + test_single_component "foo(std::basic_streambuf<wchar_t,std::char_traits<wchar_t> >)" > + test_single_component "operator>(X::Y)" > + > + gdb_test "maint cp first_component foo::bar" "foo" > + gdb_test "maint cp first_component foo::bar::baz" "foo" > + gdb_test "maint cp first_component C<A>::bar" "C<A>" > + gdb_test "maint cp first_component C<std::basic_streambuf<wchar_t,std::char_traits<wchar_t> > >::bar" "C<std::basic_streambuf<wchar_t,std::char_traits<wchar_t> > >" > +} > + > +gdb_exit > +gdb_start > + > +test_help > +test_first_component > + > +gdb_exit > +return 0 ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [rfa] annotate blocks with C++ namespace information 2003-04-14 19:22 ` Elena Zannoni @ 2003-04-14 21:33 ` David Carlton 2003-04-15 2:08 ` Daniel Jacobowitz 0 siblings, 1 reply; 15+ messages in thread From: David Carlton @ 2003-04-14 21:33 UTC (permalink / raw) To: Elena Zannoni; +Cc: gdb-patches, Daniel Jacobowitz, Jim Blandy On Mon, 14 Apr 2003 15:27:05 -0400, Elena Zannoni <ezannoni@redhat.com> said: > David Carlton writes: >> Just for reference, here's a slightly updated version of my namespace >> patch, following Daniel's suggestions. The only real change is that >> it adds a new command "maint cplus first_component" and a new file >> gdb.c++/maint.exp to test it. > Ok, I got around to this finally. It is basically ok, except for the > line between what is c++ and what is symbol table stuff. I think that > more stuff can be pushed into cp-support.c. See below... I have mixed feelings about your comments. My first reaction was the 'using_list' stuff more logically belongs in buildsym.c: it's about building a symtab, after all! So if the only reason to move it to cp-support is to shift the maintenance responsibilities (which is sensible enough, no need for you to look at changes that only affect C++ support), then I'd rather fix the maintenance process: make Daniel a symtab maintainer (he's certainly done enough work on symtabs), or at least allow him to approve C++-specific symtab changes. Having said that, I'm tentatively coming around to your point of view. After all, it's easy enough for me to say that everything related to building symtabs should be in buildsym.c, but if lots of different languages develop their own special needs for the symbol table, then buildsym.c will quickly degenerate into a mess of language-specific special cases. So maybe you're right. And, after all, cp-support.c is a lot smaller than buildsym.c, so it will be a while before it gets too bloated. Daniel, what do you think? A few other issues: > You could tighten the interface a bit, by passing in the name of the > symbol only, instead of the symbol. Yup, good idea. > The struct using_direct could also be made opaque, I think. It can't really be profitably made opaque: it probably looks that way from this patch, but that's only because this patch doesn't actually use struct using_direct, it just constructs them. My next patch will use them in symtab.c. > processing_has_namespace_info: there is one per symtab, yes? > I mean, it is set to 0 only here, just want to make sure. Right. >> + block_set_using (BLOCKVECTOR_BLOCK (blockvector, STATIC_BLOCK), >> + cp_copy_usings (using_list, > Should this function be called something like cp_dup_usings? But you > are freeing the original copy, right? Why do you need to copy it > over? The relevant memory-management issues are a mess. Basically, the list of using directives has to eventually be stored on the objfile's obstack. Unfortunately, that obstack isn't stashed anywhere that buildsym.c (or cp-support.c) can get access to it while constructing using_list. I thought about adding it as an extra argument to start_symtab, but it would have involved making changes to all of start_symtab's callers that I didn't feel entirely comfortable with. So I reluctantly settled on allocating it with xmalloc, and then copying it onto the obstack at a location where we actually know what the obstack is. Thanks a bunch for looking over this; I really appreciate it. I'll check it in once we agree about the proper location of using_list and friends. Actually, the more I think about it, the better cp-support looks as a location: many of the extern functions that I want to add to cp-support are only used by buildsym.c, so that's a good argument for putting those bits of buildsym.c in cp-support.c. David Carlton carlton@math.stanford.edu ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [rfa] annotate blocks with C++ namespace information 2003-04-14 21:33 ` David Carlton @ 2003-04-15 2:08 ` Daniel Jacobowitz 2003-04-15 2:26 ` Elena Zannoni 0 siblings, 1 reply; 15+ messages in thread From: Daniel Jacobowitz @ 2003-04-15 2:08 UTC (permalink / raw) To: David Carlton; +Cc: Elena Zannoni, gdb-patches, Jim Blandy On Mon, Apr 14, 2003 at 02:33:03PM -0700, David Carlton wrote: > On Mon, 14 Apr 2003 15:27:05 -0400, Elena Zannoni <ezannoni@redhat.com> said: > > David Carlton writes: > > >> Just for reference, here's a slightly updated version of my namespace > >> patch, following Daniel's suggestions. The only real change is that > >> it adds a new command "maint cplus first_component" and a new file > >> gdb.c++/maint.exp to test it. > > > Ok, I got around to this finally. It is basically ok, except for the > > line between what is c++ and what is symbol table stuff. I think that > > more stuff can be pushed into cp-support.c. See below... > > I have mixed feelings about your comments. My first reaction was the > 'using_list' stuff more logically belongs in buildsym.c: it's about > building a symtab, after all! So if the only reason to move it to > cp-support is to shift the maintenance responsibilities (which is > sensible enough, no need for you to look at changes that only affect > C++ support), then I'd rather fix the maintenance process: make Daniel > a symtab maintainer (he's certainly done enough work on symtabs), or > at least allow him to approve C++-specific symtab changes. > > Having said that, I'm tentatively coming around to your point of view. > After all, it's easy enough for me to say that everything related to > building symtabs should be in buildsym.c, but if lots of different > languages develop their own special needs for the symbol table, then > buildsym.c will quickly degenerate into a mess of language-specific > special cases. So maybe you're right. And, after all, cp-support.c > is a lot smaller than buildsym.c, so it will be a while before it gets > too bloated. > > Daniel, what do you think? I can see it either way - in symtab or in C++. Does it make sense to have cp-namespace.c for this, do you think? -- Daniel Jacobowitz MontaVista Software Debian GNU/Linux Developer ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [rfa] annotate blocks with C++ namespace information 2003-04-15 2:08 ` Daniel Jacobowitz @ 2003-04-15 2:26 ` Elena Zannoni 2003-04-15 3:07 ` David Carlton 2003-04-15 23:12 ` David Carlton 0 siblings, 2 replies; 15+ messages in thread From: Elena Zannoni @ 2003-04-15 2:26 UTC (permalink / raw) To: Daniel Jacobowitz; +Cc: David Carlton, Elena Zannoni, gdb-patches, Jim Blandy Daniel Jacobowitz writes: > On Mon, Apr 14, 2003 at 02:33:03PM -0700, David Carlton wrote: > > On Mon, 14 Apr 2003 15:27:05 -0400, Elena Zannoni <ezannoni@redhat.com> said: > > > David Carlton writes: > > > > >> Just for reference, here's a slightly updated version of my namespace > > >> patch, following Daniel's suggestions. The only real change is that > > >> it adds a new command "maint cplus first_component" and a new file > > >> gdb.c++/maint.exp to test it. > > > > > Ok, I got around to this finally. It is basically ok, except for the > > > line between what is c++ and what is symbol table stuff. I think that > > > more stuff can be pushed into cp-support.c. See below... > > > > I have mixed feelings about your comments. My first reaction was the > > 'using_list' stuff more logically belongs in buildsym.c: it's about > > building a symtab, after all! So if the only reason to move it to > > cp-support is to shift the maintenance responsibilities (which is > > sensible enough, no need for you to look at changes that only affect > > C++ support), then I'd rather fix the maintenance process: make Daniel > > a symtab maintainer (he's certainly done enough work on symtabs), or > > at least allow him to approve C++-specific symtab changes. > > > > Having said that, I'm tentatively coming around to your point of view. > > After all, it's easy enough for me to say that everything related to > > building symtabs should be in buildsym.c, but if lots of different > > languages develop their own special needs for the symbol table, then > > buildsym.c will quickly degenerate into a mess of language-specific > > special cases. So maybe you're right. And, after all, cp-support.c > > is a lot smaller than buildsym.c, so it will be a while before it gets > > too bloated. > > > > Daniel, what do you think? > > I can see it either way - in symtab or in C++. Does it make sense to > have cp-namespace.c for this, do you think? I see it more as building language specific structures, and letting symtabs have a pointer to those. I think the cp-namespace.c idea is a good compromise. elena > > > -- > Daniel Jacobowitz > MontaVista Software Debian GNU/Linux Developer ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [rfa] annotate blocks with C++ namespace information 2003-04-15 2:26 ` Elena Zannoni @ 2003-04-15 3:07 ` David Carlton 2003-04-15 23:12 ` David Carlton 1 sibling, 0 replies; 15+ messages in thread From: David Carlton @ 2003-04-15 3:07 UTC (permalink / raw) To: Elena Zannoni; +Cc: Daniel Jacobowitz, gdb-patches, Jim Blandy On Mon, 14 Apr 2003 22:30:11 -0400, Elena Zannoni <ezannoni@redhat.com> said: > Daniel Jacobowitz writes: >> I can see it either way - in symtab or in C++. Does it make sense >> to have cp-namespace.c for this, do you think? > I see it more as building language specific structures, and letting > symtabs have a pointer to those. I think the cp-namespace.c idea is > a good compromise. Okay, then that's what I'll do: I'll move the buildsym stuff into cp-namespace.c, along with whatever of the stuff I was planning to add to cp-support.c that's namespace specific. David Carlton carlton@math.stanford.edu ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [rfa] annotate blocks with C++ namespace information 2003-04-15 2:26 ` Elena Zannoni 2003-04-15 3:07 ` David Carlton @ 2003-04-15 23:12 ` David Carlton 2003-04-16 1:22 ` Elena Zannoni 1 sibling, 1 reply; 15+ messages in thread From: David Carlton @ 2003-04-15 23:12 UTC (permalink / raw) To: Elena Zannoni; +Cc: Daniel Jacobowitz, gdb-patches, Jim Blandy On Mon, 14 Apr 2003 22:30:11 -0400, Elena Zannoni <ezannoni@redhat.com> said: > I see it more as building language specific structures, and letting > symtabs have a pointer to those. I think the cp-namespace.c idea is a > good compromise. Excellent! Here's what I've committed: it's the same as before, except that a lot of what was added to buildsym.c is in cp-namespace.c, as is a lot of what was added to cp-support.c. No substantial changes otherwise. If I could get a final decision on the fate of lookup_symbol_aux_minsyms, I'd appreciate it, because that will affect the details of the next patch in this series. Which will probably take me a little while to prepare, anyways: I'm fairly busy for the rest of this week, so it might have to wait until next week. David Carlton carlton@math.stanford.edu 2003-04-15 David Carlton <carlton@math.stanford.edu> * Makefile.in (SFILES): Add cp-namespace.c. (COMMON_OBS): Add cp-namespace.o. (block.o): Depend on gdb_obstack_h and cp_support_h. (buildsym.o): Depend on cp_support_h. (cp-namespace.o): New. (cp-support.o): Depend on gdb_string_h, demangle_h, gdb_assert_h, gdb_obstack_h, symtab_h, symfile_h, and gdbcmd_h. (dwarf2read.o): Depend on cp_support_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 function. (dwarf2_extension): Ditto. * cp-support.h: Update copyright, contributors. Add inclusion guards. Add opaque declaration for structs obstack, block, symbol. (struct using_direct): New struct. Add declarations for cp_find_first_component, cp_entire_prefix_len, processing_has_namespace_info, processing_current_namespace, cp_is_anonymous, cp_add_using_directive, cp_initialize_namespace, cp_finalize_namespace, cp_set_block_scope, cp_scan_for_anonymous_namespaces. * cp-namespace.c: New file. * cp-support.c: Update copyright. Include ctype.h, gdb_assert.h, gdbcmd.h. New variable maint_cplus_cmd_list. (cp_find_first_component): New function. (cp_entire_prefix_len, maint_cplus_command) (first_component_command, _initialize_cp_support): Ditto. * buildsym.c: Include cp-support.h. New variable using_list. (add_symbol_to_list): Check for anonymous namespaces. (finish_block): Set block's scope. (start_symtab): Initialize C++ namespace support. (end_symtab): Finalize C++ namespace support. * 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 macro. * block.c: Include gdb_obstack.h and cp-support.h. (struct block_namespace_info): New struct. (block_set_scope): New function. (block_set_using, block_initialize_namespace): Ditto. 2003-03-17 David Carlton <carlton@math.stanford.edu> * gdb.c++/maint.exp: New file. Index: Makefile.in =================================================================== RCS file: /cvs/src/src/gdb/Makefile.in,v retrieving revision 1.363 diff -u -p -r1.363 Makefile.in --- Makefile.in 14 Apr 2003 18:42:26 -0000 1.363 +++ Makefile.in 15 Apr 2003 22:28:30 -0000 @@ -512,7 +512,7 @@ SFILES = ada-exp.y ada-lang.c ada-typepr c-exp.y c-lang.c c-typeprint.c c-valprint.c \ charset.c cli-out.c coffread.c coff-pe-read.c \ complaints.c completer.c corefile.c \ - cp-abi.c cp-support.c cp-valprint.c \ + cp-abi.c cp-support.c cp-namespace.c cp-valprint.c \ dbxread.c demangle.c disasm.c doublest.c \ dummy-frame.c dwarfread.c dwarf2expr.c dwarf2loc.c dwarf2read.c \ elfread.c environ.c eval.c event-loop.c event-top.c expprint.c \ @@ -864,6 +864,7 @@ COMMON_OBS = version.o blockframe.o brea frame.o frame-unwind.o doublest.o \ frame-base.o \ gnu-v2-abi.o gnu-v3-abi.o hpacc-abi.o cp-abi.o cp-support.o \ + cp-namespace.o \ reggroups.o OBS = $(COMMON_OBS) $(ANNOTATE_OBS) @@ -1551,7 +1552,8 @@ ax-gdb.o: ax-gdb.c $(defs_h) $(symtab_h) $(regcache_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) \ @@ -1567,7 +1569,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) \ @@ -1613,7 +1615,10 @@ corelow.o: corelow.c $(defs_h) $(gdb_str $(gdbthread_h) $(regcache_h) $(symfile_h) $(readline_h) cp-abi.o: cp-abi.c $(defs_h) $(value_h) $(cp_abi_h) $(command_h) \ $(gdbcmd_h) $(ui_out_h) $(gdb_string_h) -cp-support.o: cp-support.c $(defs_h) $(cp_support_h) +cp-namespace.o: cp-namespace.c $(defs_h) $(cp_support_h) $(gdb_obstack_h) \ + $(symtab_h) $(symfile_h) $(gdb_assert_h) $(block_h) +cp-support.o: cp-support.c $(defs_h) $(cp_support_h) $(gdb_string_h) \ + $(demangle_h) $(gdb_assert_h) $(gdbcmd_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) \ @@ -1666,7 +1671,7 @@ dwarf2read.o: dwarf2read.c $(defs_h) $(b $(symfile_h) $(objfiles_h) $(elf_dwarf2_h) $(buildsym_h) \ $(demangle_h) $(expression_h) $(filenames_h) $(macrotab_h) \ $(language_h) $(complaints_h) $(bcache_h) $(dwarf2expr_h) \ - $(dwarf2loc_h) $(gdb_string_h) $(gdb_assert_h) + $(dwarf2loc_h) $(cp_support_h) $(gdb_string_h) $(gdb_assert_h) dwarfread.o: dwarfread.c $(defs_h) $(symtab_h) $(gdbtypes_h) $(symfile_h) \ $(objfiles_h) $(elf_dwarf_h) $(buildsym_h) $(demangle_h) \ $(expression_h) $(language_h) $(complaints_h) $(gdb_string_h) 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 17 Mar 2003 21:50:44 -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 17 Mar 2003 21:50:53 -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.31 diff -u -p -r1.31 buildsym.c --- buildsym.c 25 Feb 2003 21:36:17 -0000 1.31 +++ buildsym.c 15 Apr 2003 22:42:21 -0000 @@ -44,6 +44,8 @@ #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 /**/ @@ -91,7 +93,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 +127,12 @@ 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) + cp_scan_for_anonymous_namespaces (symbol); } /* Find a symbol named NAME on a LIST. NAME need not be @@ -280,6 +291,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; @@ -372,6 +384,12 @@ finish_block (struct symbol *symbol, str } } } + + /* If we're in the C++ case, set the block's scope. */ + if (SYMBOL_LANGUAGE (symbol) == language_cplus) + { + cp_set_block_scope (symbol, block, &objfile->symbol_obstack); + } } else { @@ -814,6 +832,10 @@ start_symtab (char *name, char *dirname, } context_stack_depth = 0; + /* Set up support for C++ namespace support, in case we need it. */ + + cp_initialize_namespace (); + /* Initialize the list of sub source files with one entry for this file (the top-level source file). */ @@ -935,6 +957,8 @@ 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); + cp_finalize_namespace (BLOCKVECTOR_BLOCK (blockvector, STATIC_BLOCK), + &objfile->symbol_obstack); } #ifndef PROCESS_LINENUMBER_HOOK --- /dev/null Thu Apr 11 07:25:15 2002 +++ cp-namespace.c Tue Apr 15 15:39:59 2003 @@ -0,0 +1,266 @@ +/* Helper routines for C++ support in GDB. + Copyright 2003 Free Software Foundation, Inc. + + Contributed by David Carlton. + + This file is part of GDB. + + 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 of the License, 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, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#include "defs.h" +#include "cp-support.h" +#include "gdb_obstack.h" +#include "symtab.h" +#include "symfile.h" +#include "gdb_assert.h" +#include "block.h" + +/* When set, the file that we're processing seems to have debugging + info for C++ namespaces, so cp-namespace.c shouldn't try to guess + namespace info itself. */ + +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. */ + +const char *processing_current_namespace; + +/* List of using directives that are active in the current file. */ + +static struct using_direct *using_list; + +static struct using_direct *cp_add_using (const char *name, + unsigned int inner_len, + unsigned int outer_len, + struct using_direct *next); + +static struct using_direct *cp_copy_usings (struct using_direct *using, + struct obstack *obstack); + +/* Set up support for dealing with C++ namespace info in the current + symtab. */ + +void cp_initialize_namespace () +{ + processing_has_namespace_info = 0; + using_list = NULL; +} + +/* Add all the using directives we've gathered to the current symtab. + STATIC_BLOCK should be the symtab's static block; OBSTACK is used + for allocation. */ + +void +cp_finalize_namespace (struct block *static_block, + struct obstack *obstack) +{ + if (using_list != NULL) + { + block_set_using (static_block, + cp_copy_usings (using_list, obstack), + obstack); + using_list = NULL; + } +} + +/* Check to see if SYMBOL refers to an object contained within an + anonymous namespace; if so, add an appropriate using directive. */ + +/* Optimize away strlen ("(anonymous namespace)"). */ + +#define ANONYMOUS_NAMESPACE_LEN 21 + +void +cp_scan_for_anonymous_namespaces (const struct symbol *symbol) +{ + if (!processing_has_namespace_info + && SYMBOL_CPLUS_DEMANGLED_NAME (symbol) != NULL) + { + 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. */ + cp_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)); + } + } +} + +/* 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 +cp_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); +} + +/* Record the namespace that the function defined by SYMBOL was + defined in, if necessary. BLOCK is the associated block; use + OBSTACK for allocation. */ + +void +cp_set_block_scope (const struct symbol *symbol, + struct block *block, + struct obstack *obstack) +{ + /* Make sure that the name was originally mangled: if not, there + certainly isn't any namespace information to worry about! */ + + if (SYMBOL_CPLUS_DEMANGLED_NAME (symbol) != NULL) + { + if (processing_has_namespace_info) + { + block_set_scope + (block, obsavestring (processing_current_namespace, + strlen (processing_current_namespace), + obstack), + obstack); + } + else + { + /* Try to figure out the appropriate namespace from the + demangled name. */ + + /* FIXME: carlton/2003-04-15: 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, obstack), + obstack); + } + } +} + +/* 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 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. */ + +static 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 = savestring (name, inner_len); + retval->outer = savestring (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. */ + +static 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; + } +} 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 15 Apr 2003 23:05:07 -0000 @@ -1,5 +1,5 @@ /* Helper routines for C++ support in GDB. - Copyright 2002 Free Software Foundation, Inc. + Copyright 2002, 2003 Free Software Foundation, Inc. Contributed by MontaVista Software. @@ -21,9 +21,56 @@ Boston, MA 02111-1307, USA. */ #include "defs.h" +#include <ctype.h> #include "cp-support.h" #include "gdb_string.h" #include "demangle.h" +#include "gdb_assert.h" +#include "gdbcmd.h" + +/* The list of "maint cplus" commands. */ + +static struct cmd_list_element *maint_cplus_cmd_list = NULL; + +/* The actual commands. */ + +static void maint_cplus_command (char *arg, int from_tty); +static void first_component_command (char *arg, int from_tty); + +/* 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-03-13: We have several functions here with + overlapping functionality; can we combine them? Also, do they + 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 +185,164 @@ 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 '>'. */ + +/* NOTE: carlton/2003-03-13: This function is currently only intended + for internal use: it's probably not entirely safe when called on + user-generated input, because some of the 'index += 2' lines might + go past the end of malformed input. */ + +/* 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; + while (isspace(name[index])) + ++index; + 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; +} + +/* Don't allow just "maintenance cplus". */ + +static void +maint_cplus_command (char *arg, int from_tty) +{ + printf_unfiltered ("\"maintenance cplus\" must be followed by the name of a command.\n"); + help_list (maint_cplus_cmd_list, "maintenance cplus ", -1, gdb_stdout); +} + +/* This is a front end for cp_find_first_component, for unit testing. + Be careful when using it: see the NOTE above + cp_find_first_component. */ + +static void +first_component_command (char *arg, int from_tty) +{ + int len = cp_find_first_component (arg); + char *prefix = alloca (len + 1); + + memcpy (prefix, arg, len); + prefix[len] = '\0'; + + printf_unfiltered ("%s\n", prefix); +} + +void +_initialize_cp_support (void) +{ + add_prefix_cmd ("cplus", class_maintenance, maint_cplus_command, + "C++ maintenance commands.", &maint_cplus_cmd_list, + "maintenance cplus ", 0, &maintenancelist); + add_alias_cmd ("cp", "cplus", class_maintenance, 1, &maintenancelist); + + add_cmd ("first_component", class_maintenance, first_component_command, + "Print the first class/namespace component of NAME.", + &maint_cplus_cmd_list); + } 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 15 Apr 2003 22:42:02 -0000 @@ -1,7 +1,8 @@ /* Helper routines for C++ support in GDB. - Copyright 2002 Free Software Foundation, Inc. + Copyright 2002, 2003 Free Software Foundation, Inc. Contributed by MontaVista Software. + Namespace support contributed by David Carlton. This file is part of GDB. @@ -20,6 +21,61 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +#ifndef CP_SUPPORT_H +#define CP_SUPPORT_H + +/* Opaque declarations. */ + +struct obstack; +struct block; +struct symbol; + +/* 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; +}; + + +/* Functions from cp-support.c. */ + 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); + + +/* Functions/variables from cp-namespace.c. */ + +extern unsigned char processing_has_namespace_info; + +extern const char *processing_current_namespace; + +extern int cp_is_anonymous (const char *namespace); + +extern void cp_add_using_directive (const char *name, + unsigned int outer_length, + unsigned int inner_length); + +extern void cp_initialize_namespace (); + +extern void cp_finalize_namespace (struct block *static_block, + struct obstack *obstack); + +extern void cp_set_block_scope (const struct symbol *symbol, + struct block *block, + struct obstack *obstack); + +extern void cp_scan_for_anonymous_namespaces (const struct symbol *symbol); + +#endif /* CP_SUPPORT_H */ Index: dwarf2read.c =================================================================== RCS file: /cvs/src/src/gdb/dwarf2read.c,v retrieving revision 1.89 diff -u -p -r1.89 dwarf2read.c --- dwarf2read.c 13 Apr 2003 15:43:35 -0000 1.89 +++ dwarf2read.c 15 Apr 2003 22:42:42 -0000 @@ -43,6 +43,7 @@ #include "bcache.h" #include "dwarf2expr.h" #include "dwarf2loc.h" +#include "cp-support.h" #include <fcntl.h> #include "gdb_string.h" @@ -867,6 +868,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); @@ -1805,6 +1810,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: @@ -1815,6 +1825,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: @@ -3187,13 +3202,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) + cp_add_using_directive (processing_current_namespace, + strlen (previous_namespace), + strlen (processing_current_namespace)); + if (die->has_children) { struct die_info *child_die = die->next; @@ -3204,6 +3265,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 @@ -5668,6 +5731,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.15 diff -u -p -r1.15 jv-lang.c --- jv-lang.c 25 Feb 2003 21:36:18 -0000 1.15 +++ jv-lang.c 17 Mar 2003 21:52:04 -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; --- /dev/null Thu Apr 11 07:25:15 2002 +++ maint.exp Mon Mar 17 13:33:14 2003 @@ -0,0 +1,79 @@ +# Copyright 2003 Free Software Foundation Inc. + +# 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 of the License, 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, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +# Please email any bugs, comments, and/or additions to this file to: +# bug-gdb@prep.ai.mit.edu + + +# This file tests C++-specific maintenance commands and help on those. + +# Currently, no source file is used. + +if $tracelevel then { + strace $tracelevel + } + +# Test the help messages. + +proc test_help {} { + gdb_test "help maintenance cplus" "C\\+\\+ maintenance commands.\r\n\r\nList of maintenance cplus subcommands:\r\n\r\nmaintenance cplus first_component -- Print the first class/namespace component of NAME\r\n\r\nType \"help maintenance cplus\" followed by maintenance cplus subcommand name for full documentation.\r\nCommand name abbreviations are allowed if unambiguous." + + gdb_test "help maint cp" "C\\+\\+ maintenance commands.\r\n\r\nList of maintenance cplus subcommands:\r\n\r\nmaintenance cplus first_component -- Print the first class/namespace component of NAME\r\n\r\nType \"help maintenance cplus\" followed by maintenance cplus subcommand name for full documentation.\r\nCommand name abbreviations are allowed if unambiguous." + + gdb_test "maint cp" "\"maintenance cplus\" must be followed by the name of a command.\r\nList of maintenance cplus subcommands:\r\n\r\nmaintenance cplus first_component -- Print the first class/namespace component of NAME\r\n\r\nType \"help maintenance cplus\" followed by maintenance cplus subcommand name for full documentation.\r\nCommand name abbreviations are allowed if unambiguous." + + gdb_test "help maint cp first_component" "Print the first class/namespace component of NAME." +} + +# This is used when NAME should contain only a single component. Be +# careful to make sure that parentheses get escaped properly. +proc test_single_component {name} { + set matchname [string_to_regexp "$name"] + gdb_test "maint cp first_component $name" "$matchname" +} + +proc test_first_component {} { + test_single_component "foo" + test_single_component "operator<<" + test_single_component "operator>>" + test_single_component "operator ->" + test_single_component "operator()" + test_single_component "operator>" + test_single_component "operator<" + test_single_component "operator ->" + test_single_component "operator ->" + + test_single_component "foo()" + test_single_component "foo(int)" + test_single_component "foo(X::Y)" + test_single_component "foo(X::Y, A::B)" + test_single_component "foo(std::basic_streambuf<wchar_t,std::char_traits<wchar_t> >)" + test_single_component "operator>(X::Y)" + + gdb_test "maint cp first_component foo::bar" "foo" + gdb_test "maint cp first_component foo::bar::baz" "foo" + gdb_test "maint cp first_component C<A>::bar" "C<A>" + gdb_test "maint cp first_component C<std::basic_streambuf<wchar_t,std::char_traits<wchar_t> > >::bar" "C<std::basic_streambuf<wchar_t,std::char_traits<wchar_t> > >" +} + +gdb_exit +gdb_start + +test_help +test_first_component + +gdb_exit +return 0 ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [rfa] annotate blocks with C++ namespace information 2003-04-15 23:12 ` David Carlton @ 2003-04-16 1:22 ` Elena Zannoni 0 siblings, 0 replies; 15+ messages in thread From: Elena Zannoni @ 2003-04-16 1:22 UTC (permalink / raw) To: David Carlton; +Cc: Elena Zannoni, Daniel Jacobowitz, gdb-patches, Jim Blandy David Carlton writes: > On Mon, 14 Apr 2003 22:30:11 -0400, Elena Zannoni <ezannoni@redhat.com> said: > > > I see it more as building language specific structures, and letting > > symtabs have a pointer to those. I think the cp-namespace.c idea is a > > good compromise. > > Excellent! Here's what I've committed: it's the same as before, > except that a lot of what was added to buildsym.c is in > cp-namespace.c, as is a lot of what was added to cp-support.c. No > substantial changes otherwise. nice, thanks! > > If I could get a final decision on the fate of > lookup_symbol_aux_minsyms, I'd appreciate it, because that will affect > the details of the next patch in this series. Which will probably > take me a little while to prepare, anyways: I'm fairly busy for the > rest of this week, so it might have to wait until next week. *blink* *blink*..ahhh.. totally forgot, sorry. I'll make it next. elena > > David Carlton > carlton@math.stanford.edu > ^ permalink raw reply [flat|nested] 15+ messages in thread
end of thread, other threads:[~2003-04-16 1:22 UTC | newest] Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2003-02-22 1:47 [rfa] annotate blocks with C++ namespace information David Carlton 2003-03-11 17:11 ` Daniel Jacobowitz 2003-03-11 21:14 ` David Carlton 2003-03-11 21:23 ` Daniel Jacobowitz 2003-03-11 22:43 ` David Carlton 2003-03-11 22:59 ` David Carlton 2003-03-11 23:01 ` Daniel Jacobowitz 2003-03-17 22:33 ` David Carlton 2003-04-14 19:22 ` Elena Zannoni 2003-04-14 21:33 ` David Carlton 2003-04-15 2:08 ` Daniel Jacobowitz 2003-04-15 2:26 ` Elena Zannoni 2003-04-15 3:07 ` David Carlton 2003-04-15 23:12 ` David Carlton 2003-04-16 1:22 ` Elena Zannoni
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox