[PATCH] Add new warning to Clang to detect when all code paths in a function has a call back to the function.

Richard Smith richard at metafoo.co.uk
Fri Oct 11 12:32:23 PDT 2013


On Wed, Oct 9, 2013 at 3:56 PM, Arthur O'Dwyer <arthur.j.odwyer at gmail.com>wrote:

> On Wed, Oct 9, 2013 at 3:30 PM, Ted Kremenek <kremenek at apple.com> wrote:
> >
> > template <int value> int sum() {
> >   return value + sum<value/2>();
> > }
> >
> > template<> int sum<1>() { return 1; }
> >
> > template<int x, int y> int calculate_value() {
> >   if (x != y)
> >     return sum<x - y>();
> >   else
> >     return 0;
> > }
> >
> > int value = calculate_value<1,1>();
> [...]
> > Technically this should get folded out by the CFG builder.  It probably
> > doesn’t handle it yet, but it could easily be taught to handle this
> specific
> > case.  The CFG builder already prunes out some trivially unreachable
> paths.
> > This seems like one of those cases, since ‘x’ and ‘y’ are template
> > parameters.
>
> FWIW, I would have thought that the implementation is *required* to
> instantiate sum<0> here, since it is implicitly instantiated by
> calculate_value<1,1> in this translation unit, and the implementation
> can't prove that sum<0> won't be required by an extern declaration in
> some other translation unit. I.e., I might link against a "foo.cc"
> containing
>
>     extern template int sum<0>(void);
>     void foo(bool b) { if (b) sum<0>(); }
>

You can't rely on an implicit instantiation to satisfy an explicit
instantiation declaration. If you use a specialization that is subject to
an explicit instantiation declaration, there must be an explicit
instantiation definition of that specialization somewhere in the program.


> However, I see that Clang (Apple's clang-425.0.28) actually already
> fails this test, so either it's permitted by the Standard or else
> Clang is already out of conformance in this area. I couldn't find any
> official language about "extern template", though.
>
> –Arthur
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20131011/fd7e2d2c/attachment.html>


More information about the cfe-commits mailing list