On Fri, 14 Sep 2018 11:54:57 -0400
Richard Yao <r...@gentoo.org> wrote:

> >> My read of this is that the warning occurs regardless of optimization 
> >> level, but it could somehow be improved by optimization.
> >> 
> >> 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.
...
> Either provide code examples that generate warnings in a way that 
> demonstrates that I am incorrect

To make code behave differently it needs substantial amount of code
to provide you an example. You need to him O2<->O3 behaviour delta
after all. But I will try (for a different warning, it should not matter
much).

Below is a reduced example of a larger C++ program.
Many thanks to Ulya for providing recent example!

In this example -O3 manages to inline/const-propagate deep enough
and find out potential null-deref. -O2 was not able to do it.

$ bash -x ./mk_.sh
+ LANG=C
+ g++-8.2.0 -O2 -c 1.cc -Wnull-dereference
+ g++-8.2.0 -O3 -c 1.cc -Wnull-dereference
1.cc: In function 'bool foo(std::vector<int>)':
1.cc:3:22: warning: null pointer dereference [-Wnull-dereference]
   typename h = e - d >> *g;
                ~~~~~~^~~~~
1.cc:3:22: warning: null pointer dereference [-Wnull-dereference]
   typename h = e - d >> *g;
                ~~~~~~^~~~~

$ cat 1.cc
#include <vector>
template <typename a, typename b> a c(a d, a e, b f, int *g) {
  typename h = e - d >> *g;
  for (; h;) if (f(*d)) if (f(*d)) return d;
  return d;
}
template <typename i, typename b> i o(i d, i e, b f, int *g) {
  return c(d, e, f, g);
}
template <typename i, typename b> i oo(i d, i e, b f, int *g) {
  return c(d, e, f, g);
}
template <typename j, typename b> j k(j d, j e, b f, int *g) {
  return o(d, e, f, g);
}
template <typename j, typename b> j kk(j d, j e, b f, int *g) {
  return oo(d, e, f, g);
}
bool cmpp(int);
bool foo(std::vector<int> l) {
  std::vector<int>::const_iterator ib,
      ie = l.end(), m, n = k(ib, ie, cmpp, 0) = kk(m, ie, cmpp, 0);
  return m == n;
}

-- 

  Sergei

Reply via email to