1 |
On Fri, 14 Sep 2018 11:54:57 -0400 |
2 |
Richard Yao <ryao@g.o> wrote: |
3 |
|
4 |
> >> My read of this is that the warning occurs regardless of optimization level, but it could somehow be improved by optimization. |
5 |
> >> |
6 |
> >> As for the last, it is for uninitialized variable reads. However, I think you are misinterpreting the claim. The way that optimization level could affect warning generation would be if the warning generation came after optimization passes that could hide reads. That means that -O3 would prevent the warning. |
7 |
... |
8 |
> Either provide code examples that generate warnings in a way that demonstrates that I am incorrect |
9 |
|
10 |
To make code behave differently it needs substantial amount of code |
11 |
to provide you an example. You need to him O2<->O3 behaviour delta |
12 |
after all. But I will try (for a different warning, it should not matter |
13 |
much). |
14 |
|
15 |
Below is a reduced example of a larger C++ program. |
16 |
Many thanks to Ulya for providing recent example! |
17 |
|
18 |
In this example -O3 manages to inline/const-propagate deep enough |
19 |
and find out potential null-deref. -O2 was not able to do it. |
20 |
|
21 |
$ bash -x ./mk_.sh |
22 |
+ LANG=C |
23 |
+ g++-8.2.0 -O2 -c 1.cc -Wnull-dereference |
24 |
+ g++-8.2.0 -O3 -c 1.cc -Wnull-dereference |
25 |
1.cc: In function 'bool foo(std::vector<int>)': |
26 |
1.cc:3:22: warning: null pointer dereference [-Wnull-dereference] |
27 |
typename h = e - d >> *g; |
28 |
~~~~~~^~~~~ |
29 |
1.cc:3:22: warning: null pointer dereference [-Wnull-dereference] |
30 |
typename h = e - d >> *g; |
31 |
~~~~~~^~~~~ |
32 |
|
33 |
$ cat 1.cc |
34 |
#include <vector> |
35 |
template <typename a, typename b> a c(a d, a e, b f, int *g) { |
36 |
typename h = e - d >> *g; |
37 |
for (; h;) if (f(*d)) if (f(*d)) return d; |
38 |
return d; |
39 |
} |
40 |
template <typename i, typename b> i o(i d, i e, b f, int *g) { |
41 |
return c(d, e, f, g); |
42 |
} |
43 |
template <typename i, typename b> i oo(i d, i e, b f, int *g) { |
44 |
return c(d, e, f, g); |
45 |
} |
46 |
template <typename j, typename b> j k(j d, j e, b f, int *g) { |
47 |
return o(d, e, f, g); |
48 |
} |
49 |
template <typename j, typename b> j kk(j d, j e, b f, int *g) { |
50 |
return oo(d, e, f, g); |
51 |
} |
52 |
bool cmpp(int); |
53 |
bool foo(std::vector<int> l) { |
54 |
std::vector<int>::const_iterator ib, |
55 |
ie = l.end(), m, n = k(ib, ie, cmpp, 0) = kk(m, ie, cmpp, 0); |
56 |
return m == n; |
57 |
} |
58 |
|
59 |
-- |
60 |
|
61 |
Sergei |