Fix for spurious warning

Mark Mitchell mmitchell@usa.net
Thu Sep 11 14:19:00 GMT 1997


On code like this:

    template <class T>
    struct S
    {
      struct R 
      {
	R();
	~R();
      };

      void foo()
      {
	R r;
	int i;
      }

      S();
      ~S();
    };

    void f()
    {
      S<int> si;
      si.foo();
    }

g++ -Wunused (or -Wall) gives:

    supernova% g++ -c -Wall test2.cpp
    test2.cpp: In method `void S<T>::foo()':
    test2.cpp:13: warning: unused variable `int i'
    test2.cpp:12: warning: unused variable `struct S<T>::R r'
    test2.cpp: In method `void S<int>::foo()':
    test2.cpp:13: warning: unused variable `int i'

The problem:
  
  o S<T>::R r is *not* unused.  It is initialized and destroyed, 
    just like si, and g++ is smart enough not to warn about that!

Below, I've got a simple fix, which changes this output to:

    supernova% test-g++ -c -Wunused test2.cpp
    test2.cpp: In method `void S<int>::foo()':
    test2.cpp:13: warning: unused variable `int i'

-- 
Mark Mitchell		mmitchell@usa.net
Stanford University	http://www.stanford.edu

Thu Sep 11 10:08:45 1997  Mark Mitchell  <mmitchell@usa.net>

	* pt.c (do_poplevel): Don't warn about unused local variables
	while processing_template_decl since we don't always know whether
	or not they will need constructing/destructing.

Index: pt.c
===================================================================
RCS file: /home/mitchell/Repository/egcs/gcc/cp/pt.c,v
retrieving revision 1.13
diff -c -p -r1.13 pt.c
*** pt.c	1997/09/11 21:12:17	1.13
--- pt.c	1997/09/11 21:12:24
*************** tree
*** 2423,2430 ****
--- 2423,2438 ----
  do_poplevel ()
  {
    tree t;
+   int saved_warn_unused;
  
+   if (processing_template_decl)
+     {
+       saved_warn_unused = warn_unused;
+       warn_unused = 0;
+     }
    expand_end_bindings (getdecls (), kept_level_p (), 1);
+   if (processing_template_decl)
+     warn_unused = saved_warn_unused;
    t = poplevel (kept_level_p (), 1, 0);
    pop_momentary ();
    return t;



More information about the Gcc mailing list