public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [patch 3/3 optional] Template Lookup
@ 2010-07-19 15:29 sami wagiaalla
  2010-08-18 22:15 ` Tom Tromey
  0 siblings, 1 reply; 4+ messages in thread
From: sami wagiaalla @ 2010-07-19 15:29 UTC (permalink / raw)
  To: gdb-patches

[-- Attachment #1: Type: text/plain, Size: 1027 bytes --]

This patch improves the output provided by patch 2 in the series. It 
makes the meta symbol created for templates aware of its instanced 
within scope. So if the symbol table contained:

foo<int>(int)
foo<char>(char)
Bar<int>
Bar<char>

and the user did:

(gdb) print foo
or
(gdb) ptype Bar

Symbol represents a template and cannot be evaluated.
Did you mean:
    foo<int>(int)
    foo<char>(char)
Address of symbol "foo" is unknown.

and

Symbol represents a template and cannot be evaluated.
Did you mean:
    Bar<int>
    Bar<char>
type = Template Symbol

respectively. The output needs a bit of cleanup. This is achieved 
through a bit of coordination between symbol evaluation and type 
printing. Also the instance suggested will come only from the current 
scope. If this template had instance in other blocks or files they will 
not be presented here.

I don't think completing the work on this patch would provide 
significant improvement on user experience but I posted it as a 
demonstration, to get other opinions.


[-- Attachment #2: template_lookup_3.patch --]
[-- Type: text/plain, Size: 6603 bytes --]

Template meta var now knows about its instances.

2010-07-16  Sami Wagiaalla  <swagiaal@redhat.com>

	* symtab.h (symbol_add_template_instance): New function prototype.
	(symbol_get_template_instances): New function prototype.
	Created struct symbol_list_element.
	(instances): Add an instance of symbol_list_element to
	general_symbol_info
	* symtab.c (symbol_add_template_instance): New function.
	(symbol_get_template_instances): New function.
	* findvar.c (read_var_value): Print list of candidates when
	attempting to
	evaluate a LOC_TEMPLATE.
	* dwarf2read.c (add_template_variable): Find template variable and
	add instance reference to it.

diff --git a/gdb/dwarf2read.c b/gdb/dwarf2read.c
index 6659968..2c074ef 100644
--- a/gdb/dwarf2read.c
+++ b/gdb/dwarf2read.c
@@ -8641,7 +8641,7 @@ add_template_variable (struct symbol *symbol, struct pending **listhead,
                        struct objfile *objfile)
 {
   const struct pending *iterator = *listhead;
-  struct symbol *template_sym;
+  struct symbol *template_sym = NULL;
   struct symbol *iterator_sym;
   struct type *type;
   char *name = SYMBOL_NATURAL_NAME (symbol);
@@ -8672,7 +8672,7 @@ add_template_variable (struct symbol *symbol, struct pending **listhead,
 
   /* Has a template symbol for this symbol been added already ?  */
   for (iterator = *(listhead);
-       iterator != NULL;
+       iterator != NULL && template_sym == NULL;
        iterator = iterator->next)
     {
       for (i = iterator->nsyms - 1; i >= 0; --i)
@@ -8682,28 +8682,32 @@ add_template_variable (struct symbol *symbol, struct pending **listhead,
 	  if (TYPE_CODE (SYMBOL_TYPE (iterator_sym)) == TYPE_CODE_TEMPLATE
 	      && strcmp (SYMBOL_SEARCH_NAME(iterator_sym), search_name) == 0)
 	    {
-	      do_cleanups (all_cleanups);
-	      return;
+	      template_sym = iterator_sym;
+	      break;
 	    }
         }
     }
 
-  /* Add a new template symbol.  */
-  template_sym = (struct symbol *) obstack_alloc (&objfile->objfile_obstack,
-                                         sizeof (struct symbol));
-  OBJSTAT (objfile, n_syms++);
-  memset (template_sym, 0, sizeof (struct symbol));
+  if (template_sym == NULL)
+    {
+      /* Add a new template symbol.  */
+      template_sym = (struct symbol *) obstack_alloc (&objfile->objfile_obstack,
+					     sizeof (struct symbol));
+      OBJSTAT (objfile, n_syms++);
+      memset (template_sym, 0, sizeof (struct symbol));
 
-  SYMBOL_SET_NAMES (template_sym, search_name, strlen (search_name), 0, objfile);
-  type = alloc_type (objfile);
-  TYPE_CODE (type) = TYPE_CODE_TEMPLATE;
-  SYMBOL_TYPE (template_sym) = type;
+      SYMBOL_SET_NAMES (template_sym, search_name, strlen (search_name), 0, objfile);
+      type = alloc_type (objfile);
+      TYPE_CODE (type) = TYPE_CODE_TEMPLATE;
+      SYMBOL_TYPE (template_sym) = type;
 
-  SYMBOL_DOMAIN (template_sym) = VAR_DOMAIN;
-  SYMBOL_CLASS (template_sym) = LOC_TEMPLATE;
+      SYMBOL_DOMAIN (template_sym) = VAR_DOMAIN;
+      SYMBOL_CLASS (template_sym) = LOC_TEMPLATE;
 
-  add_symbol_to_list (template_sym, listhead);
+      add_symbol_to_list (template_sym, listhead);
+    }
 
+  symbol_add_template_instance (template_sym, symbol, objfile);
   do_cleanups (all_cleanups);
 
 }
diff --git a/gdb/findvar.c b/gdb/findvar.c
index f4749b7..2e6de94 100644
--- a/gdb/findvar.c
+++ b/gdb/findvar.c
@@ -568,9 +568,19 @@ read_var_value (struct symbol *var, struct frame_info *frame)
       return v;
 
     case LOC_TEMPLATE:
-      error (_("Symbol represents a template and cannot be evaluated."));
-      break;
+      {
+	struct symbol_list_element* instance = symbol_get_template_instances (var);
+
+	printf_filtered ("Symbol represents a template and cannot be evaluated.\n");
+	printf_filtered ("Did you mean:\n");
 
+	while (instance != NULL)
+	  {
+	    printf_filtered ("  %s\n", SYMBOL_NATURAL_NAME (instance->sym));
+	    instance = instance->next;
+	  }
+	return NULL;
+      }
     default:
       error (_("Cannot look up value of a botched symbol."));
       break;
diff --git a/gdb/symtab.c b/gdb/symtab.c
index 70feb62..1240b09 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -343,6 +343,28 @@ gdb_mangle_name (struct type *type, int method_id, int signature_id)
 /* Initialize the cplus_specific structure.  'cplus_specific' should
    only be allocated for use with C++ symbols.  */
 
+void
+symbol_add_template_instance (struct symbol* sym,
+                              const struct symbol* instance,
+                              struct objfile *objfile)
+{
+  struct symbol_list_element* new_instance =
+      OBSTACK_ZALLOC (&objfile->objfile_obstack,
+		      struct symbol_list_element);
+
+  new_instance->sym = instance;
+  new_instance->next = sym->ginfo.value.instances;
+  sym->ginfo.value.instances = new_instance;
+}
+
+/* Return the list of known instances of this template symbol.  */
+
+struct symbol_list_element *
+symbol_get_template_instances (struct symbol* sym)
+{
+  return sym->ginfo.value.instances;
+}
+
 static void
 symbol_init_cplus_specific (struct general_symbol_info *gsymbol,
                             struct objfile *objfile)
diff --git a/gdb/symtab.h b/gdb/symtab.h
index 67dddeb..3f98916 100644
--- a/gdb/symtab.h
+++ b/gdb/symtab.h
@@ -75,6 +75,15 @@ struct program_space;
 
    --chastain 2003-08-21  */
 
+/* A likend list of symbols used to keep references to
+   instances of a template.  */
+
+struct symbol_list_element
+{
+  struct symbol_list_element *next;
+  const struct symbol *sym;
+};
+
 /* Struct for storing C++ specific information.  Allocated when needed.  */
 
 struct cplus_specific
@@ -126,6 +135,10 @@ struct general_symbol_info
     /* for opaque typedef struct chain */
 
     struct symbol *chain;
+
+    /* If this is a value of a template symbol this is a pointer to the
+       list of instances of that variable.  */
+    struct symbol_list_element *instances;
   }
   value;
 
@@ -197,6 +210,14 @@ extern CORE_ADDR symbol_overlayed_address (CORE_ADDR, struct obj_section *);
 #define SYMBOL_SECTION(symbol)		(symbol)->ginfo.section
 #define SYMBOL_OBJ_SECTION(symbol)	(symbol)->ginfo.obj_section
 
+extern void
+symbol_add_template_instance (struct symbol* sym,
+                              const struct symbol* instance,
+                              struct objfile *objfile);
+
+extern struct symbol_list_element*
+symbol_get_template_instances (struct symbol* sym);
+
 /* Initializes the language dependent portion of a symbol
    depending upon the language for the symbol. */
 #define SYMBOL_INIT_LANGUAGE_SPECIFIC(symbol,language) \


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2010-09-23 14:48 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-07-19 15:29 [patch 3/3 optional] Template Lookup sami wagiaalla
2010-08-18 22:15 ` Tom Tromey
2010-08-30 14:59   ` sami wagiaalla
2010-09-23 18:43     ` [patch 3/4 " sami wagiaalla

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).