Bug 44478 - -Wunused- but-set-variable is incredible noisy in kernel builds
Summary: -Wunused- but-set-variable is incredible noisy in kernel builds
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.6.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-06-09 09:40 UTC by Andi Kleen
Modified: 2015-12-16 13:57 UTC (History)
1 user (show)

See Also:
Host: x86_64-linux
Target: x86_64-linux
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andi Kleen 2010-06-09 09:40:50 UTC
When building a Linux kernel the new default of having -Wunused-but-set-variable is incredibly noisy in kernel build. I get hundreds of new warnings from that.

I looked at some of the circumstances and it's typically

int var = VALUE;

#ifdef SYMBOL
if (do something)
  var = other value
#endif

with SYMBOL being undefined in this build.

I don't think it's feasible to avoid the warning short of turning it off.
I suspect other code bases will have the same problem.
Can the warning be removed from -Wall please?
Comment 1 Paolo Carlini 2010-06-09 09:49:54 UTC
I'm sure you are right, but I don't understand your explanation: even when SYMBOL
is undefined, why no code actually uses (roughly speaking, reads) var? That's the point of the warning and your example doesn't seem to shed any special light on that.
Comment 2 Jakub Jelinek 2010-06-09 09:53:58 UTC
The warning is useful and can find (and already found) several real bugs e.g. in gcc itself.  Icc has similar warning.
If kernel has lots of useless code like that and doesn't wish to use this warning, it can add -Wno-unused-but-set-{variable,parameter} to CFLAGS it uses.

Note that in the snippet you mentioned -Wunused would warn always, no matter whether SYMBOL is defined or not.  When it is not defined, var is unused (also warned with 4.5 and earlier), when it is defined, var is only set but never used.

I guess what you really mean is that kernel has lots of snippets that set some variable to some value and then only conditionally (in #ifdef guarded code) uses it.
Comment 3 Andi Kleen 2010-06-09 10:13:31 UTC
Sorry my example was not very good.

Anyways this typically happens with #ifdefs, so the some ifdef path
is actually using the variable, it's just not enabled in the current build.
In theory the ifdefs could be put around the variables too, but
it would increase the ifdef frequency a lot.

Another case i've also seen is to use it as a dummy output variable of inline
assembler, where the assembler is in a macro and it's sometimes used
and sometimes not.
Comment 4 Jakub Jelinek 2010-06-09 10:52:35 UTC
We don't warn on
void foo (void)
{
  int dummy;
  asm ("" : "=r" (dummy));
}
- the use in the asm is considered as a use, not just set.
Comment 5 Andi Kleen 2010-06-09 11:08:40 UTC
Hmm yes there was another temporary and a inline inbetween

unsigned inlinefunc(void)
{
   unsigned var;
   asm(" ... " : "=r" (var));
   return var;
}

#define macro(x,y)
{ 
unsigned var = inlinefunc();
x = var;
y = var >> 16;
};

caller macro(x,y)      y is just a dummy
Comment 6 Jakub Jelinek 2010-06-09 11:36:44 UTC
Then it has nothing to do with the asm.
If the macro is widely used and very often sets a var that isn't used, all
you can do is add (void) cast to shut the warning up.
(void) (y = var >> 16);
in this case.
Comment 7 Marek Polacek 2015-12-16 13:57:36 UTC
Looks like there isn't anything to do.