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

Richard Smith richard at metafoo.co.uk
Wed Nov 30 18:18:12 PST 2011


On Wed, November 30, 2011 09:30, Chandler Carruth wrote:
> On Wed, Nov 30, 2011 at 12:45 AM, Ted Kremenek <kremenek at apple.com> wrote:
>> The idea I had to tackle this was a bit simpler, although it could
>> certainly be flawed.  Looking at the results of -Wunreachable-code that you
>> mentioned, the reason we report this code as unreachable is because the
>> edges for the branch conditions in the CFG are intentionally pruned as being
>> trivially satisfiable/unsatisfiable.  Instead of just chopping off the edges
>> completely, we can retain all edges, but mark them as being unreachable for
>> various reasons, e.g. value dependent because of template instantiations or
>> expanded from a macro.
>
> I really like this idea for the reasons you mention.
>
> Also, Richard Smith and I were discussing based on David's initial queries
> in IRC how we might do this. Our specific idea was to retain just enough
> information in the instantiated AST to figure out the dependence of the
> templated expressions leading to that instantiation. He had a clever idea that
> would re-use the bits already in the Expr node to indicate different kinds of
> dependence to also indicate being instantiated *from* different kinds of
> dependence. I'll let him fill in the details, but essentially this (possibly
> combined with a side-table of more detailed information) seems like it would
> allow the AST to indicate exactly what template construct it came from, much
> the way source locations provide this information for macros.

It seems like we're no longer moving in this direction, but for posterity the
idea was to use the InstantiationDependent bit to indicate (as it currently
does) whether an Expr node is dependent, and if that bit is 0, use the
ValueDependent/TypeDependent bits to indicate if the node was instantiated
from a dependent node.

Injecting the right values into the latter bits is somewhat tricky to do
non-invasively. The best approach which Chandler and I found was to track in
TreeTransform whether we're visiting a dependent Expr node, and if any new
Expr nodes are created during such a visit, to set their Dependent bits to the
corresponding values.

Richard




More information about the cfe-dev mailing list