This is the mail archive of the gcc@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]

-Wshadow vs 'struct stat stat' et al


Under (for example) gcc/g++ 2.95.2, the common idiom
   
   % cat shadow.cc
   struct  status
           {
           int x, y, z;
           };
   
   // prototype
   void foo();
   
   void foo(struct status &status)
   {
   }

triggers a warning from -Wshadow:

   % /usr/local/share2/bin/g++ -Wshadow -c shadow.cc
   shadow.cc: In function `void foo(status &)':
   shadow.cc:10: warning: declaration of `status' shadows global declaration

This isn't what I expected from the description in gcc.info-2,

   `-Wshadow'
        Warn whenever a local variable shadows another local variable.

It seems to me that we really have two distinct semantic situations here:
#1: variable name matches (earlier-declared) variable name
    = this is what the -Wshadow description says it's looking-out-for
#2: "variable name matches (earlier-declared) struct/class/enum name
    = what the above example program is doing.

It seems to me that
(a) #2 is much less likely to happen by accident than #1, so if #2 occurs,
    it's much more likely to be intentional,
(b) #2 is not really covered by the (#1) -Wshadow description, and
(c) #2 is an at-least-mildly-common C/C++ idiom, as witness the common
    Unix incantation (even in system header files) 'struct stat stat'

I think it might be worthwhile to split off #2 into a (new) separate
warning option from (maybe call the #2 option -Wname-shadows-type or
suchlike?), so it could be enabled/disabled independently of -Wshadow.

Comments?

-- 
-- Jonathan Thornburg <jthorn@galileo.thp.univie.ac.at>
   http://www.thp.univie.ac.at/~jthorn/home.html
   Universitaet Wien (Vienna, Austria) / Institut fuer Theoretische Physik
   "[[after learning NT]] I looked back at UNIX with the feeling I'd
    been demoted from a backhoe to a leaf-blower." -- Thomas Scoville

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