Bug 60741 - -Wmaybe-uninitialized wrong location
Summary: -Wmaybe-uninitialized wrong location
Status: RESOLVED DUPLICATE of bug 81714
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.8.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: Wuninitialized
  Show dependency treegraph
 
Reported: 2014-04-02 14:17 UTC by Ulya
Modified: 2021-04-15 17:12 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2014-04-03 00:00:00


Attachments
wmaybe_uninitialized_strange_behavior.c (176 bytes, text/x-csrc)
2014-04-02 14:17 UTC, Ulya
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Ulya 2014-04-02 14:17:27 UTC
Created attachment 32521 [details]
wmaybe_uninitialized_strange_behavior.c

Given this code:

    enum A { A1, A2 };
    enum B { B1 };
    static inline int fa (enum A a) {
        int n;
        switch (a) {
            case A1: n = 1; break;
            case A2: n = 2; break;
        }
        return n;
    }
    static inline int fb (enum B b) {
        int n;
        switch (b) {
            case B1: n = 1; break;
        }
        return n;
    }
    int main (int argc, char **) {
        return fa((A)argc) + fb((B)argc);
    }

$ g++ -O1 -Wall wmaybe_uninitialized_strange_behavior.c
wmaybe_uninitialized_strange_behavior.c: In function ‘int main(int, char**)’:
wmaybe_uninitialized_strange_behavior.c:27:36: warning: ‘n’ may be used uninitialized in this function [-Wmaybe-uninitialized]
     return fa((A)argc) + fb((B)argc);
                                    ^

1) gcc generates warning for 'fa' function, but not for 'fb' function.
2) The message points to the end of expression, that contains 'fa' call (not to 'fa' declaration, or at least to 'fa' call). The message mentions name of a variable, that is used in 'fa' declaration. So we're lucky that the same name doesn't appear in the calling function (main).
3) This all happens with -O1 or higher, with -O0 there's no warnings at all!
Comment 1 Manuel López-Ibáñez 2014-04-02 15:22:45 UTC
We don't warn for fb(), because PR18501.

So the only original bug here is the location when warning. The problem is the locations that are propagated when transforming expressions. We reach:

  # n_4 = PHI <n_5(D)(3), [test.cc : 6:17] 1(6), [test.cc : 7:17] 2(4)>
<L3>:
  [test.cc : 20:32] # RANGE [-2147483648, 2147483647] NONZERO 0x00000000000000007
  _2 = n_4 + 1;

When instead we should have a location for n_4 that is different than the location for '+'.

In any case, pointing to the closing parenthesis is very confusing. At worst it should point to the '+'.
Comment 2 Richard Biener 2014-04-03 10:14:01 UTC
(In reply to Manuel López-Ibáñez from comment #1)
> We don't warn for fb(), because PR18501.
> 
> So the only original bug here is the location when warning. The problem is
> the locations that are propagated when transforming expressions. We reach:
> 
>   # n_4 = PHI <n_5(D)(3), [test.cc : 6:17] 1(6), [test.cc : 7:17] 2(4)>
> <L3>:
>   [test.cc : 20:32] # RANGE [-2147483648, 2147483647] NONZERO
> 0x00000000000000007
>   _2 = n_4 + 1;
> 
> When instead we should have a location for n_4 that is different than the
> location for '+'.

The location of n_4 depends on the edge you are coming from (thus n_4
itself doesn't have a location). The warning code picks the location of an actual
use of n_4 in that case, here that of

  _2 = n_4 + 1;

this points to the plus in main, but appearantly slightly wrong.  At the
point where we emit the warning we can't really do better, but we could at
least also hint where 'n' was declared and/or inlined from.

> In any case, pointing to the closing parenthesis is very confusing. At worst
> it should point to the '+'.
Comment 3 Manuel López-Ibáñez 2015-02-23 17:56:35 UTC
Fix blocks/depends list.
Comment 4 Martin Sebor 2021-04-15 17:12:20 UTC
The poor location in C++ is also tracked in pr81714.  Let me keep that bug open since it has a simpler test case and resolve this one as its dupe.

(The C front end does point to the + expression.)

*** This bug has been marked as a duplicate of bug 81714 ***