[gnu.g++.bug] g++ -Wall produces incorrect 'defined but not used' warnings

donn@wasatch.utah.edu (Donn Seeley) (07/26/89)

This problem crops up in G++ 1.35.0 and doesn't appear to be fixed in
1.35.1:

% cat used.c
typedef int     (*PF) ();

static int      hash();

PF
f()
{
	PF              hp;

	hp = &::hash;
	return hp;
}

static int 
hash()
{
	return 10;
}
% g++ -Wall -v -S used.c
g++ version 1.35.0
 /usr/local/lib/gcc-cpp -+ -v -undef -D__GNU__ -D__GNUG__ -D__cplusplus -Dmc6800
0 -Dhp300 -Dhp9000 -Dunix -D__mc68000__ -D__hp300__ -D__hp9000__ -D__unix__ -Wal
l -D__HAVE_FPU__ used.c /tmp/cc011574.cpp
GNU CPP version 1.34
 /usr/local/lib/gcc-cc1plus /tmp/cc011574.cpp -quiet -dumpbase used.c -Wall -nor
eg -version -o used.s
GNU C++ version 1.35.0 (68k, MIT syntax) compiled by GNU C version 1.34.
used.c:16: warning: `int hash ()' defined but not used
% 

(In the user's actual example, 'f' is a method and there is a 'hash'
member in the class of 'f', hence the need for explicit scoping.  In
any case, if the :: scope operator is removed, the problem goes away.)

This seems to be the result of an oversight.  G++ handles scoped global
identifiers differently from ordinary bare identifiers; the latter are
marked used in the function hack_identifier, but the former don't get
marked at all.  GCC doesn't have this split and it doesn't have this
bug...  I copied the code to set TREE_USED into the action for scoped
globals in cplus-parse.y, and it appeared to fix the problem:

*** /tmp/,RCSt1011807	Wed Jul 26 00:29:40 1989
--- cplus-parse.y	Tue Jul 25 22:46:34 1989
***************
*** 1034,1039 ****
--- 1034,1045 ----
  		    }
  		  else if (TREE_CODE ($$) == CONST_DECL)
  		    $$ = DECL_INITIAL ($$);
+ 		  if (! TREE_USED ($$))
+ 		    {
+ 		      if (TREE_EXTERNAL ($$))
+ 			assemble_external ($$);
+ 		      TREE_USED ($$) = 1;
+ 		    }
  		}
  	| scoped_declarator identifier_or_opname  %prec HYPERUNARY
  		{ $$ = build_member_ref ($1, $2, 0); }

Donn Seeley    University of Utah CS Dept    donn@cs.utah.edu
40 46' 6"N 111 50' 34"W    (801) 581-5668    utah-cs!donn