public inbox for archer-commits@sourceware.org
help / color / mirror / Atom feed
* [SCM]  archer-keiths-expr-cumulative: Fixed shadowing problems
@ 2009-04-21 15:09 swagiaal
  0 siblings, 0 replies; only message in thread
From: swagiaal @ 2009-04-21 15:09 UTC (permalink / raw)
  To: archer-commits

The branch, archer-keiths-expr-cumulative has been updated
       via  f95071be91b43bb9b86137d058a3e0482c75abdb (commit)
       via  c5cbee3a3e14e1c662903c5259379821000ee4cb (commit)
      from  6c26e0eb10ea4cf9c0b84d1e3bf4de4819841c32 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email.

- Log -----------------------------------------------------------------
commit f95071be91b43bb9b86137d058a3e0482c75abdb
Author: Sami Wagiaalla <swagiaal@redhat.com>
Date:   Thu Apr 16 16:10:16 2009 -0400

    Fixed shadowing problems
    
    2009-04-16  Sami Wagiaalla  <swagiaal@redhat.com>
    
    	* symtab.c (lookup_symbol_aux_local): Perform namespace lookup
    	at every block level.
    	Check for member variables at function level.
    	* cp-namespace.c (cp_lookup_symbol_nonlocal): implemented incremental
    	lookup.
    	(lookup_namespace_scope): Added declaration_only argument.
    	(cp_lookup_symbol_namespace_incremental): Ditto.
    	(cp_lookup_symbol_namespace): Implemented declaration_only lookup.

commit c5cbee3a3e14e1c662903c5259379821000ee4cb
Author: Sami Wagiaalla <swagiaal@redhat.com>
Date:   Tue Mar 31 12:14:14 2009 -0400

    Added test for variable shadowing.
    
    +2009-03-31  Sami Wagiaalla  <swagiaal@redhat.com>
    +
    +	* gdb.cp/shadowing.exp: New test.
    +	* gdb.cp/shadowing.cc: New test program.
    +

-----------------------------------------------------------------------

Summary of changes:
 gdb/cp-namespace.c                 |   75 ++++++++++++++++++--------
 gdb/cp-support.h                   |   15 +++--
 gdb/symtab.c                       |  105 ++++++++++++++++++++----------------
 gdb/testsuite/gdb.cp/shadowing.cc  |   45 +++++++++++++++
 gdb/testsuite/gdb.cp/shadowing.exp |   85 +++++++++++++++++++++++++++++
 gdb/valops.c                       |    2 +-
 6 files changed, 252 insertions(+), 75 deletions(-)
 create mode 100644 gdb/testsuite/gdb.cp/shadowing.cc
 create mode 100644 gdb/testsuite/gdb.cp/shadowing.exp

First 500 lines of diff:
diff --git a/gdb/cp-namespace.c b/gdb/cp-namespace.c
index eace9c4..b914d59 100644
--- a/gdb/cp-namespace.c
+++ b/gdb/cp-namespace.c
@@ -270,9 +270,23 @@ cp_lookup_symbol_nonlocal (const char *name,
 			   const struct block *block,
 			   const domain_enum domain)
 {
+  struct symbol* sym = NULL;
+  const struct block *global_block = block_global_block (block);
+  const char* scope = block_scope(block);
+    /* Check if either no block is specified or it's a global block.  */
+
+  while (block != global_block)
+    while (block != global_block)
+      {
+      sym = lookup_namespace_scope(name, linkage_name, block,
+            domain, scope, 0, 0);
+
+      if (sym != NULL)
+        return sym;
+
+      block = BLOCK_SUPERBLOCK (block);
+    }
 
-  struct symbol* sym = lookup_namespace_scope(name, linkage_name, block,
-      domain, block_scope(block), 0);
 
   if (sym != NULL)
     return sym;
@@ -301,7 +315,8 @@ lookup_namespace_scope (const char *name,
 			const struct block *block,
 			const domain_enum domain,
 			const char *scope,
-			int scope_len)
+			int scope_len,
+                        int declaration_only)
 {
   char *namespace;
 
@@ -320,7 +335,7 @@ lookup_namespace_scope (const char *name,
 	}
       new_scope_len += cp_find_first_component (scope + new_scope_len);
       sym = lookup_namespace_scope (name, linkage_name, block,
-				    domain, scope, new_scope_len);
+				    domain, scope, new_scope_len, declaration_only);
       if (sym != NULL)
 	return sym;
     }
@@ -331,8 +346,8 @@ lookup_namespace_scope (const char *name,
   namespace = alloca (scope_len + 1);
   strncpy (namespace, scope, scope_len);
   namespace[scope_len] = '\0';
-  return cp_lookup_symbol_namespace_incremental (namespace, name, linkage_name,
-				     block, domain);
+  return cp_lookup_symbol_namespace (namespace, name, linkage_name,
+				     block, domain, declaration_only);
 }
 
 /* Searches the for the given NAME in the given NAMESPACE, using import 
@@ -342,7 +357,8 @@ cp_lookup_symbol_namespace_incremental (const char *namespace,
                                         const char *name,
                                         const char *linkage_name,
                                         const struct block *block,
-                                        const domain_enum domain)
+                                        const domain_enum domain,
+                                        int declaration_only)
 {
   struct symbol *sym;
   const struct block *global_block = block_global_block (block);
@@ -354,7 +370,7 @@ cp_lookup_symbol_namespace_incremental (const char *namespace,
 
   while (block != global_block)
     {
-      sym = cp_lookup_symbol_namespace (namespace, name, linkage_name, block, domain);
+      sym = cp_lookup_symbol_namespace (namespace, name, linkage_name, block, domain, declaration_only);
 
       if (sym != NULL)
         return sym;
@@ -377,7 +393,8 @@ cp_lookup_symbol_namespace (const char *namespace,
 			    const char *name,
 			    const char *linkage_name,
 			    const struct block *block,
-			    const domain_enum domain)
+			    const domain_enum domain,
+			    int declaration_only)
 {
   const struct using_direct *current;
   struct symbol *sym = NULL;
@@ -402,16 +419,28 @@ cp_lookup_symbol_namespace (const char *namespace,
 	     imported namespace.  */
 	  if (strcmp ("", current->declaration) != 0)
 	    {
-	    if(strcmp (name, current->declaration) == 0){
-	      sym = cp_lookup_symbol_namespace (current->inner,
-	                                        name,
-	                                        linkage_name,
-	                                        block,
-	                                        domain);
+	    if (strcmp (name, current->declaration) == 0)
+	      {
+	        sym = cp_lookup_symbol_namespace (current->inner,
+	                                          name,
+	                                          linkage_name,
+	                                          block,
+	                                          domain,
+	                                          declaration_only);
+	      }
 	    }
 	   
-	    }
-	  else if(strcmp (name, current->alias) == 0)
+	  if (declaration_only)
+            {
+              if (sym)
+                {
+                  return sym;
+                } else {
+                  continue;
+                }
+            }
+
+	  if (strcmp (name, current->alias) == 0)
 	    /* If the import is creating an alias and the alias matches the
 	       sought name. Pass current->inner as the NAME to direct the
 	       search towards the aliased namespace */
@@ -420,15 +449,17 @@ cp_lookup_symbol_namespace (const char *namespace,
 	                                      current->inner,
 	                                      linkage_name,
 	                                      block,
-	                                      domain);
-	  }else if(strcmp ("", current->alias) == 0){
+	                                      domain,
+                                              declaration_only);
+	    } else if (strcmp ("", current->alias) == 0){
 	    /* If this import statement creates no alias, pass current->inner as
 	       NAMESPACE to direct the search towards the imported namespace. */
 	      sym = cp_lookup_symbol_namespace (current->inner,
 		                                name,
 		                                linkage_name,
 		                                block,
-		                                domain);
+		                                domain,
+                                                declaration_only);
 	    }
 
 	  if (sym != NULL){
@@ -441,7 +472,7 @@ cp_lookup_symbol_namespace (const char *namespace,
      that are still applicable; so let's see if we've got a match
      using the current namespace.  */
   
-  if (namespace[0] == '\0')
+  if (namespace[0] == '\0' && !declaration_only)
     {
       sym = lookup_symbol_file (name, linkage_name,
                                      block, domain, 
@@ -546,7 +577,7 @@ cp_lookup_nested_type (struct type *parent_type,
 							 nested_name,
 							 NULL,
 							 block,
-							 VAR_DOMAIN);
+							 VAR_DOMAIN,0);
 	if (sym == NULL || SYMBOL_CLASS (sym) != LOC_TYPEDEF)
 	  return NULL;
 	else
diff --git a/gdb/cp-support.h b/gdb/cp-support.h
index 2ff53ff..68dd9a0 100644
--- a/gdb/cp-support.h
+++ b/gdb/cp-support.h
@@ -129,19 +129,22 @@ extern struct symbol *lookup_namespace_scope (const char *name,
                                               const struct block *block,
                                               const domain_enum domain,
                                               const char *scope,
-                                              int scope_len);
+                                              int scope_len,
+                                              int declaration_only);
 
 extern struct symbol *cp_lookup_symbol_namespace_incremental (const char *namespace,
                                                               const char *name,
                                                               const char *linkage_name,
                                                               const struct block *block,
-                                                              const domain_enum domain);
+                                                              const domain_enum domain,
+                                                              int declaration_only);
 
 extern struct symbol *cp_lookup_symbol_namespace (const char *namespace,
-						  const char *name,
-						  const char *linkage_name,
-						  const struct block *block,
-						  const domain_enum domain);
+                                                  const char *name,
+                                                  const char *linkage_name,
+                                                  const struct block *block,
+                                                  const domain_enum domain,
+                                                  int declaration_only);
 
 extern struct type *cp_lookup_nested_type (struct type *parent_type,
 					   const char *nested_name,
diff --git a/gdb/symtab.c b/gdb/symtab.c
index b67b5d4..cad6615 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -96,7 +96,9 @@ static
 struct symbol *lookup_symbol_aux_local (const char *name,
 					const char *linkage_name,
 					const struct block *block,
-					const domain_enum domain);
+					const domain_enum domain,
+					enum language language,
+					int *is_a_field_of_this);
 
 static
 struct symbol *lookup_symbol_aux_symtabs (int block_index,
@@ -1300,56 +1302,18 @@ lookup_symbol_aux (const char *name, const char *linkage_name,
   /* Search specified block and its superiors.  Don't search
      STATIC_BLOCK or GLOBAL_BLOCK.  */
 
-  sym = lookup_symbol_aux_local (name, linkage_name, block, domain);
+  sym = lookup_symbol_aux_local (name, linkage_name, block, domain, language, is_a_field_of_this);
   if (sym != NULL)
     return sym;
 
-  /* If requested to do so by the caller and if appropriate for LANGUAGE,
-     check to see if NAME is a field of `this'. */
-
-  langdef = language_def (language);
-
-  if (langdef->la_name_of_this != NULL && is_a_field_of_this != NULL
-      && block != NULL)
-    {
-      struct symbol *sym = NULL;
-      const struct block *function_block = block;
-
-      /* 'this' is only defined in the function's block, so find the
-	 enclosing function block.  */
-      for (; function_block && !BLOCK_FUNCTION (function_block);
-	   function_block = BLOCK_SUPERBLOCK (function_block));
-
-      if (function_block && !dict_empty (BLOCK_DICT (function_block)))
-	sym = lookup_block_symbol (function_block, langdef->la_name_of_this,
-				   NULL, VAR_DOMAIN);
-      if (sym)
-	{
-	  struct type *t = sym->type;
-
-	  /* I'm not really sure that type of this can ever
-	     be typedefed; just be safe.  */
-	  CHECK_TYPEDEF (t);
-	  if (TYPE_CODE (t) == TYPE_CODE_PTR
-	      || TYPE_CODE (t) == TYPE_CODE_REF)
-	    t = TYPE_TARGET_TYPE (t);
-
-	  if (TYPE_CODE (t) != TYPE_CODE_STRUCT
-	      && TYPE_CODE (t) != TYPE_CODE_UNION)
-	    error (_("Internal error: `%s' is not an aggregate"),
-		   langdef->la_name_of_this);
-
-	  if (check_field (t, name))
-	    {
-	      *is_a_field_of_this = 1;
-	      return NULL;
-	    }
-	}
-    }
+  /* this symbol was found to be a member variable
+     do not perform the global search. */
+  if (is_a_field_of_this && *is_a_field_of_this)
+    return NULL;
 
   /* Now do whatever is appropriate for LANGUAGE to look
      up static and global variables.  */
-
+  langdef = language_def (language);
   sym = langdef->la_lookup_symbol_nonlocal (name, linkage_name, block, domain);
   if (sym != NULL)
     return sym;
@@ -1377,11 +1341,17 @@ lookup_symbol_aux (const char *name, const char *linkage_name,
 static struct symbol *
 lookup_symbol_aux_local (const char *name, const char *linkage_name,
 			 const struct block *block,
-			 const domain_enum domain)
+			 const domain_enum domain,
+			 enum language language,
+			 int *is_a_field_of_this)
 {
   struct symbol *sym;
   const struct block *global_block = block_global_block (block);
   const struct block *block_iterator = block;
+  const struct language_defn *langdef;
+
+  langdef = language_def (language);
+
 
   /* Check if either no block is specified or it's a global block.  */
 
@@ -1390,10 +1360,53 @@ lookup_symbol_aux_local (const char *name, const char *linkage_name,
 
   while (block_iterator != global_block)
     {
+
       sym = lookup_symbol_aux_block (name, linkage_name, block_iterator, domain);
+
       if (sym != NULL)
 	return sym;
     
+      if (language == language_cplus )
+        {
+          sym = lookup_namespace_scope(name, linkage_name, block,domain, block_scope(block_iterator), 0, 1);
+
+          if (sym != NULL)
+            return sym;
+        }
+
+      if (langdef->la_name_of_this != NULL && is_a_field_of_this != NULL && BLOCK_FUNCTION (block_iterator))
+        {
+          if (!dict_empty (BLOCK_DICT (block_iterator)))
+            {
+              sym = lookup_block_symbol (block_iterator, langdef->la_name_of_this,
+                                             NULL, VAR_DOMAIN);
+
+
+              if (sym)
+                {
+                  struct type *t = sym->type;
+
+                  /* I'm not really sure that type of this can ever
+                     be typedefed; just be safe.  */
+                  CHECK_TYPEDEF (t);
+                  if (TYPE_CODE (t) == TYPE_CODE_PTR
+                      || TYPE_CODE (t) == TYPE_CODE_REF)
+                      t = TYPE_TARGET_TYPE (t);
+
+                  if (TYPE_CODE (t) != TYPE_CODE_STRUCT
+                      && TYPE_CODE (t) != TYPE_CODE_UNION)
+                    error (_("Internal error: `%s' is not an aggregate"),
+                        langdef->la_name_of_this);
+
+                  if (check_field (t, name))
+                    {
+                      *is_a_field_of_this = 1;
+                      return NULL;
+                    }
+                }
+            }
+        }
+
       block_iterator = BLOCK_SUPERBLOCK (block_iterator);
     }
 
diff --git a/gdb/testsuite/gdb.cp/shadowing.cc b/gdb/testsuite/gdb.cp/shadowing.cc
new file mode 100644
index 0000000..1651510
--- /dev/null
+++ b/gdb/testsuite/gdb.cp/shadowing.cc
@@ -0,0 +1,45 @@
+namespace A
+{
+  int x = 11;
+}
+
+int x = 22;
+int y = 0;
+
+class B
+{
+public:
+  int x;
+
+  int
+  func()
+  {
+    x = 33;
+    y++; // marker1
+
+      {
+        int x = 44;
+        y++; // marker2
+
+          {
+            int x = 55;
+            y++; // marker3
+
+              {
+                using namespace A;
+                y++; // marker4
+
+                using A::x;
+                y++; // marker5
+              }
+          }
+      }
+  }
+};
+
+int
+main()
+{
+  B theB;
+  return theB.func();
+}
diff --git a/gdb/testsuite/gdb.cp/shadowing.exp b/gdb/testsuite/gdb.cp/shadowing.exp
new file mode 100644
index 0000000..fcbf680
--- /dev/null
+++ b/gdb/testsuite/gdb.cp/shadowing.exp
@@ -0,0 +1,85 @@
+# Copyright 2008 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 3 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, see <http://www.gnu.org/licenses/>.
+
+if $tracelevel then {
+    strace $tracelevel
+}
+
+set prms_id 0
+set bug_id 0
+
+set testfile shadowing
+set srcfile ${testfile}.cc
+set binfile ${objdir}/${subdir}/${testfile}
+if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug c++}] != "" } {
+    untested "Couldn't compile test program"
+    return -1
+}
+
+# Get things started.
+
+gdb_exit
+gdb_start
+gdb_reinitialize_dir $srcdir/$subdir
+gdb_load ${binfile}
+
+if ![runto_main] then {
+    perror "couldn't run to breakpoint main"
+    continue
+}
+
+############################################
+# Test printing of class variable is not shadowed
+# by global variable
+
+gdb_breakpoint [gdb_get_line_number "marker1"]
+gdb_continue_to_breakpoint "marker1"
+
+gdb_test "print x" "= 33" "Print class x shadowing global x"
+
+
+############################################
+# Test printing local variable is not shadowed
+# by class variable
+
+gdb_breakpoint [gdb_get_line_number "marker2"]
+gdb_continue_to_breakpoint "marker2"
+
+gdb_test "print x" "= 44" "Print local x shadowing class x"
+
+############################################
+# Test inner scope x is printed not outer scope
+
+gdb_breakpoint [gdb_get_line_number "marker3"]
+gdb_continue_to_breakpoint "marker3"
+
+gdb_test "print x" "= 55" "Print inner scope x"
+
+############################################
+# Test printing local variable is not shadowed
+# by namespace variable
+
+gdb_breakpoint [gdb_get_line_number "marker4"]
+gdb_continue_to_breakpoint "marker4"
+
+gdb_test "print x" "= 55" "Print local x not namespace x"
+
+############################################
+# Test imported namespace element is printed
+
+gdb_breakpoint [gdb_get_line_number "marker5"]
+gdb_continue_to_breakpoint "marker5"
+
+gdb_test "print x" "= 11" "Print imported namespace x"
diff --git a/gdb/valops.c b/gdb/valops.c
index 1ec7647..87c3676 100644
--- a/gdb/valops.c
+++ b/gdb/valops.c
@@ -2847,7 +2847,7 @@ value_maybe_namespace_elt (const struct type *curtype,
 
   sym = cp_lookup_symbol_namespace_incremental (namespace_name, name, NULL,
 				    get_selected_block (0), 
-				    VAR_DOMAIN);


hooks/post-receive
--
Repository for Project Archer.


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2009-04-21 15:09 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-04-21 15:09 [SCM] archer-keiths-expr-cumulative: Fixed shadowing problems swagiaal

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).