[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
Wed Oct 9 15:51:13 PDT 2013


On Wed, Oct 9, 2013 at 3:30 PM, Ted Kremenek <kremenek at apple.com> wrote:

> On Oct 9, 2013, at 1:03 PM, Richard Trieu <rtrieu at google.com> wrote:
>
> Here is the code in question, template parameters fixed now:
>
> 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>();
>
> calculate_value<1,1>() causes the instantiation of sum<0>(), even though
> it is protected from being called by the conditional "if (x != y)".
>  sum<0>() is the only self-recursive function here.  Are you saying we
> should warn on that even though the programmer added the proper check to
> prevent it from being called?
>
>
> 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.
>

I don't think that helps. sum<0> is still used, so we still instantiate it,
and when we check its definition we would diagnose the infinite recursion.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20131009/738f452d/attachment.html>


More information about the cfe-commits mailing list