[cfe-dev] Warning when calling virtual functions from constructor/desctructor?

Arnaud Bienner via cfe-dev cfe-dev at lists.llvm.org
Thu Dec 13 07:08:23 PST 2018


Hi all :)

I noticed that clang doesn't emit a warning in this case.
It does when calling a *pure* virtual function though (a warning for which
I added a diagnostic group recently [1]).
I think one difference is that calling pure virtual function from
constructor is undefined, while calling virtual function is defined (it
calls the base class function since the vtable isn't filled yet with
inherited virtual functions reimplementations).
So implementing a warning like this will likely trigger false positives.

However, I think in most cases it denotes a bug, since many IMHO C++
developers won't get that the function will not behave as they might expect.
Also, we can have a fixit to this, by suggesting the user to to specify the
class of the function e.g. write "B::f()" instead of "f()".

I'm fine (trying to) write a patch for this, but first I wanted to get
opinions about whether it's worth it.
I guess it might not be that much difficult to do for simple cases (when
calling virtual function directly). I expect it might be more difficult to
catch cases where we call a function that is not virtual, but which calls a
virtual function behind the scene.
Also, if you're worried about having too many false positives, I think we
could put this new warning behind Wextra.

What do you think?

Best regards,
Arnaud

[1]:
https://github.com/llvm-mirror/clang/commit/528a1d17a3ff8361fdd4a1a379adf3f64ec68e00
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20181213/954dbecb/attachment.html>


More information about the cfe-dev mailing list