[cfe-dev] new -Wuninitialized implementation in Clang

Ted Kremenek kremenek at apple.com
Thu Feb 3 22:06:05 PST 2011

On Feb 3, 2011, at 8:27 PM, Johannes Schaub (litb) wrote:

> Hmm, I would personally like to not initialize variables that I know are not 
> read from, as in your above code example. I personally regard it as bad 
> coding style to initialize variables with values not really used by the 
> code. 

That's a fair argument, although I would also argue that it is bad style to have variables that could potentially *never* be initialized.

> Would it be reasonable to have a flag that enables clang to try harder to 
> not warn in such cases? I haven't any clue on how much this slows down 
> clang's performance, but I think I'm willing to take a compile-time speed 
> drop equivalent to a -O2.

It's not just a matter of making the analysis smarter.  That inherently requires exponential time in the general case.  Nobody is willing to take that kind of compile-time hit.

Some control-dependencies can possibly be handled by doing some amount of abstract interpretation on boolean values, and then composing those with the uninitialized values dataflow analysis.  I'm not certain what kind of cost that would be in practice.  As an optimization, that kind of analysis could *possibly* be done as a secondary pass once a use of an uninitialized value is discovered.  That way the additional analysis cost is only paid when code uses this kind of coding idiom.

For example, suppose we have:

  int x;
  if (y)
   x = ...
  if (y)

The analysis believes that 'x' is used uninitialized because it thinks there is a false path where the first branch is 'false' and the second branch is 'true'.  This is easy to recover from the CFG since the analysis computes dataflow values for each basic block.  A reverse dataflow analysis from the use to the definition that accounts for the values of flags could then possibly prune out the paths that aren't possible, and thus show that the uninitialized use is infeasible.  Note that this would only work for *very simple* cases, but that might be enough to get the behavior people are expecting with a marginal analysis cost.

More information about the cfe-dev mailing list