Bug 41952 - patch to improve Wuninitialized
patch to improve Wuninitialized
Product: gcc
Classification: Unclassified
Component: middle-end
: P3 enhancement
: 4.6.0
Assigned To: Not yet assigned to anyone
: diagnostic, missed-optimization, patch
Depends on:
Blocks: Wuninitialized 4.6
  Show dependency treegraph
Reported: 2009-11-05 18:17 UTC by davidxl
Modified: 2010-04-29 20:40 UTC (History)
2 users (show)

See Also:
Known to work:
Known to fail:
Last reconfirmed: 2009-11-06 10:01:05

proposed patch (2.04 KB, patch)
2009-11-05 18:18 UTC, davidxl
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description davidxl 2009-11-05 18:17:01 UTC
Compiling the following example with -Wuninitialized, 4.4 gcc will emit false warning on uninitialized variable. In fact the definition is guarded by a condition which is always true (coming from the assignment operator) for locals. The solution is to fold the predicate (see proposed patch).

On 4.5, due to vsym related changes, the false positive warning issue is masked (but false negative one occurs -- see a different PR). While this patch can be used to fix the 4.4 issue, it is also applicable to 4.5 as a very safe optimization. Is it ok to get it in at this stage?


struct ExtentsBase {
 ExtentsBase() : startx_(), endx_() { }
 ExtentsBase(const ExtentsBase &b) {
  *this = b;

 const ExtentsBase & operator=(const ExtentsBase &b) {
  if (this != &b || WORK_AROUND_GCC_4_4_0_BROKENNESS) {
    startx_ = b.startx_;
  return *this;

 int startx_;
  int endx_;

int f(const ExtentsBase &e1) {
 ExtentsBase my_extents = e1;
 return my_extents.startx_;


self2.cc: In function 'int f(const ExtentsBase&)':
self2.cc:25: warning: 'my_extents' may be used uninitialized in this function
Comment 1 davidxl 2009-11-05 18:18:56 UTC
Created attachment 18975 [details]
proposed patch

Bootstrapped and regression tested on x86-64 linux with gcc 4.5
Comment 2 Richard Biener 2009-11-06 10:01:05 UTC
Patches should go to gcc-patches, you need to add the testcase and you
should use auto_var_in_fn_p instead of !is_global_var.
Comment 3 davidxl 2009-11-06 18:11:51 UTC
(In reply to comment #2)
> Patches should go to gcc-patches, you need to add the testcase and you
> should use auto_var_in_fn_p instead of !is_global_var.

auto_var_in_fn_p uses DECL_CONTEXT --- is it guaranteed to always point to the enclosing functions even for inner block scoped locals (it seems so now).

Comment 4 Richard Biener 2009-11-06 19:06:11 UTC
Yes, the predicate is implemented correctly.
Comment 5 H.J. Lu 2010-04-29 20:40:19 UTC
Fixed by revision 158567: