[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:34:58 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?

I don’t really see this as a special case.  ‘x’ and ‘y’ could be constants defined in some other way in non-templated code.  A solution to that problem is to prune branches in the CFG, which is something we already do for many cases where the branch conditions are easy to resolve.

Unlike some other dataflow techniques where we wanted a warning to only apply for all template instantiations, warning for a particular template instantiation has goodness here, and seems no real different to me than handling other functions.  There are details to be handled here:  constant folding, reporting useful diagnostics that let’s someone understand that the recursion happens under a particular instantiation, only emitting one warning instead of N for each instantiation, and so on.  But these don’t seem like insurmountable problems.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20131009/3f581691/attachment.html>


More information about the cfe-commits mailing list