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

Arthur O'Dwyer arthur.j.odwyer at gmail.com
Wed Oct 9 15:56:36 PDT 2013


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>(); }

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




More information about the cfe-commits mailing list