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

Ted Kremenek kremenek at apple.com
Wed Oct 9 15:30:22 PDT 2013


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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20131009/e364d480/attachment.html>


More information about the cfe-commits mailing list