This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


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

Re: C++: -Wshadow violates the ISO C++ scoping rules


> Date: Thu, 27 Jan 2000 17:43:08 +0100 (MET)
> From: Gerald Pfeifer <pfeifer@dbai.tuwien.ac.at>
> To: gcc-bugs@gcc.gnu.org

> The following snippet shows a bug in the -Wshadow as for as the ISO C++
> scoping rules are concerned:

>   int main() {
>     for(int i=1; i < 3; i++);
>     for(int i=1; i < 3; i++);
>     }

>   nunki% gcc -ansi -Wshadow y.cc
>   y.cc: In function `int main ()':
>   y.cc:3: warning: declaration of `i' shadows previous local

> This effectively renders -Wshadow unusable for large classes of C++ code.

This sounds like a bug in Per's code that handles old style scoping
for for statements.  The fix should be easy enough:

Can I put it in?

g++.mike/for3.C:
// Special g++ Options: -Wshadow

int
main(int i) {
  for(int i=1; i < 3; i++);	// WARNING - shadows parm
  for(int i=1; i < 3; i++);	// WARNING - shadows parm
  for(int j=1; j < 3; j++);
  for(int j=1; j < 3; j++);
}

Thu Jan 27 13:54:12 2000  Mike Stump  <mrs@wrs.com>

	* decl.c (pushdecl): Fix up shadow warnings with respect to implicit
	for scopes.

Index: decl.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cp/decl.c,v
retrieving revision 1.534
diff -c -p -r1.534 decl.c
*** decl.c	2000/01/17 20:18:39	1.534
--- decl.c	2000/01/27 21:50:49
*************** pushdecl (x)
*** 3933,3938 ****
--- 3933,3949 ----
  	    set_identifier_type_value_with_scope (name, NULL_TREE,
  						  current_binding_level);
  
+ 	  if (oldlocal)
+ 	    {
+ 	      tree d = oldlocal;
+ 	      while (oldlocal && DECL_DEAD_FOR_LOCAL (oldlocal))
+ 		{
+ 		  oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
+ 		}
+ 	      if (oldlocal == NULL_TREE)
+ 		oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
+ 	    }
+ 
  	  /* If this is an extern function declaration, see if we
  	     have a global definition or declaration for the function.  */
  	  if (oldlocal == NULL_TREE
*************** pushdecl (x)
*** 3959,3973 ****
  	      && TREE_PUBLIC (x))
  	    TREE_PUBLIC (name) = 1;
  
- 	  if (DECL_FROM_INLINE (x))
- 	    /* Inline decls shadow nothing.  */;
- 
  	  /* Warn if shadowing an argument at the top level of the body.  */
! 	  else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
! 		   && TREE_CODE (oldlocal) == PARM_DECL
! 		   /* Don't complain if it's from an enclosing function.  */
! 		   && DECL_CONTEXT (oldlocal) == current_function_decl
! 		   && TREE_CODE (x) != PARM_DECL)
  	    {
  	      /* Go to where the parms should be and see if we
  		 find them there.  */
--- 3970,3983 ----
  	      && TREE_PUBLIC (x))
  	    TREE_PUBLIC (name) = 1;
  
  	  /* Warn if shadowing an argument at the top level of the body.  */
! 	  if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
! 	      /* Inline decls shadow nothing.  */
! 	      && !DECL_FROM_INLINE (x)
! 	      && TREE_CODE (oldlocal) == PARM_DECL
! 	      /* Don't complain if it's from an enclosing function.  */
! 	      && DECL_CONTEXT (oldlocal) == current_function_decl
! 	      && TREE_CODE (x) != PARM_DECL)
  	    {
  	      /* Go to where the parms should be and see if we
  		 find them there.  */
*************** pushdecl (x)
*** 3979,3999 ****
  	      /* ARM $8.3 */
  	      if (b->parm_flag == 1)
  		cp_error ("declaration of `%#D' shadows a parameter", name);
- 	    }
- 	  else if (warn_shadow && oldlocal != NULL_TREE
- 		   && current_binding_level->is_for_scope
- 		   && !DECL_DEAD_FOR_LOCAL (oldlocal))
- 	    {
- 	      warning ("variable `%s' shadows local",
- 		       IDENTIFIER_POINTER (name));
- 	      cp_warning_at ("  this is the shadowed declaration", oldlocal);
  	    }
  	  /* Maybe warn if shadowing something else.  */
! 	  else if (warn_shadow && !DECL_EXTERNAL (x)
! 		   /* No shadow warnings for internally generated vars.  */
! 		   && ! DECL_ARTIFICIAL (x)
! 		   /* No shadow warnings for vars made for inlining.  */
! 		   && ! DECL_FROM_INLINE (x))
  	    {
  	      if (oldlocal != NULL_TREE && TREE_CODE (oldlocal) == PARM_DECL)
  		warning ("declaration of `%s' shadows a parameter",
--- 3989,4004 ----
  	      /* ARM $8.3 */
  	      if (b->parm_flag == 1)
  		cp_error ("declaration of `%#D' shadows a parameter", name);
  	    }
+ 
  	  /* Maybe warn if shadowing something else.  */
! 	  if (warn_shadow && !DECL_EXTERNAL (x)
! 	      /* Inline decls shadow nothing.  */
! 	      && !DECL_FROM_INLINE (x)
! 	      /* No shadow warnings for internally generated vars.  */
! 	      && ! DECL_ARTIFICIAL (x)
! 	      /* No shadow warnings for vars made for inlining.  */
! 	      && ! DECL_FROM_INLINE (x))
  	    {
  	      if (oldlocal != NULL_TREE && TREE_CODE (oldlocal) == PARM_DECL)
  		warning ("declaration of `%s' shadows a parameter",

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