[cfe-dev] Fwd: -Wunreachable-code and templates

David Blaikie dblaikie at gmail.com
Wed Nov 30 09:48:23 PST 2011


> The idea I had to tackle this was a bit simpler, although it could certainly be flawed.

One issue here based on the basic idea you've outlined: we still would
be reporting on template instantiations, not the template itself. If
there is unreachable code that's just unreachable in all
instantiations of the template then we would actually report it once
for each instantiation rather than just once (& a lesser issue: we
wouldn't report it at all if there were no instantiations). Though
this is less of a problem than the issue of false positives.

$ cat unreachable2.cpp
int func1();
int func2();
template<bool b>
int func() {
  if (false) {
    return func1();
  }
  return func2();
}

int main() {
  return func<true>() + func<false>();
}
$ clang++ -Wunreachable-code unreachable2.cpp
unreachable2.cpp:6:12: warning: will never be executed [-Wunreachable-code]
    return func1();
           ^~~~~
unreachable2.cpp:12:10: note: in instantiation of function template
specialization 'func<true>' requested here
  return func<true>() + func<false>();
         ^
unreachable2.cpp:6:12: warning: will never be executed [-Wunreachable-code]
    return func1();
           ^~~~~
unreachable2.cpp:12:25: note: in instantiation of function template
specialization 'func<false>' requested here
  return func<true>() + func<false>();
                        ^
2 warnings generated.

>From a time perspective, this also means we're analyzing reachable
code for every template instantiation - should we ever find different
unreachable code for different instantiations (you mentioned some
differences between template patterns and template instantiations but
it's not immediately obvious to me if/when these differences could
produce different reachable code that we would want to diagnose on a
per-instantiation basis (unlike the original issue where we expressly
don't want to look per instantiation, basically))?

- David



More information about the cfe-dev mailing list