This may sound like a silly warning, but it actually very useful in finding missing member functions in CRTP. Given the testcase template <typename D> struct C { void foo() { static_cast<D *>(this)->foo(); } }; struct D : C<D> { // this is missing: // void foo() {} }; void f(D *d) { d->foo(); } gcc is silent, but clang prints: test.cpp:2:14: warning: all paths through this function will call itself [-Winfinite-recursion] void foo() { static_cast<D *>(this)->foo(); }
I'd like this too -- I've run into the same thing many times.
Another nice example is PR87742 where it would be nice to tell the user that the operator * has infinite recursion.
Such a '-Winfinite-recursion' diagnostic would've been helpful for PR101204/PR103157 (meaning: prevented/flagged that issue early, via a GCC build-time '-Werror' diagnostic).
Patch: https://gcc.gnu.org/pipermail/gcc-patches/2021-November/583914.html
The master branch has been updated by Martin Sebor <msebor@gcc.gnu.org>: https://gcc.gnu.org/g:30ba058f77eedfaf7a0582f5d42aff949710bce4 commit r12-5483-g30ba058f77eedfaf7a0582f5d42aff949710bce4 Author: Martin Sebor <msebor@redhat.com> Date: Tue Nov 23 15:30:29 2021 -0700 Implement -Winfinite-recursion [PR88232]. Resolves: PR middle-end/88232 - Please implement -Winfinite-recursion gcc/ChangeLog: PR middle-end/88232 * Makefile.in (OBJS): Add gimple-warn-recursion.o. * common.opt: Add -Winfinite-recursion. * doc/invoke.texi (-Winfinite-recursion): Document. * passes.def (pass_warn_recursion): Schedule a new pass. * tree-pass.h (make_pass_warn_recursion): Declare. * gimple-warn-recursion.c: New file. gcc/c-family/ChangeLog: PR middle-end/88232 * c.opt: Add -Winfinite-recursion. gcc/testsuite/ChangeLog: PR middle-end/88232 * c-c++-common/attr-used-5.c: Suppress valid warning. * c-c++-common/attr-used-6.c: Same. * c-c++-common/attr-used-9.c: Same. * g++.dg/warn/Winfinite-recursion-2.C: New test. * g++.dg/warn/Winfinite-recursion-3.C: New test. * g++.dg/warn/Winfinite-recursion.C: New test. * gcc.dg/Winfinite-recursion-2.c: New test. * gcc.dg/Winfinite-recursion.c: New test.
Done for GCC 12.