-Wunreachable-code and templates

<kremene> Date: Wed, 30 Nov 2011 18:33:39 -0800

From: David Blaikie
Subject: Re: [cfe-dev] -Wunreachable-code and templates
To: kremenek
Cc: cfe-dev Developers <cfe-dev@cs.uiuc.edu>
Message-ID:
       <CAENS6EuPHirDBA3pJC553_USeTdsOX24zSMpRaf21E8VAS734Q@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

> Realistically, unless we really want to be super clever here, the most
> immediate solution to making -Wunreachable-code useful in the presence of
> templates is to simply have it skip templates entirely.

In the spirit of improving the utility of the warning, I have gone and done
this in?r145520. ?If we come up with a better solution, we can certainly
change this. ?I believe the general solution, if one exists, will be very
complex.

Thanks Ted - I tend to agree once we got to those examples of
conditionally unreachable code that it appears to be a hard problem. I
did check gcc's behavior, but it seems they gave up on this warning
entirely around 4.3-4.4 (because their implementation was based on
their optimizations & fraught with many more problems than just
templates, by the sounds of it) so we're not missing any GCC parity
here by not checking templates.

Slightly related/minor tidyup - should the isType/ValueDependent
checks in tryEvaluate/tryEvaluateBool be removed (and possibly
replaced with assertions) as they don't seem to do anything - we
weren't visiting dependent contexts previously nor are we now.

- David

I think this is the only reasonable solution (having
-Wunreachable-code skip templates entirely).

Templates are used to generate code. Subjecting them to
-Wunreachable-code implies that all the potentially generated code has
to exist in all instantiations.

I would love to have templates that generate no code in some instantiations.

Templates are used to generate code. Subjecting them to
-Wunreachable-code implies that all the potentially generated code has
to exist in all instantiations.

Well that's true of -Wunreachable-code before Ted's changes today, but
it's possible that there are real problems that could be detected if
we were smart enough:

template<typename T>
void foo() {
  if (false)
    bar();
}

It'd be nice if we could diagnose that while not having false
positives related to the sort of scenarios you're describing (where
code is conditional on template parameters)