[cfe-dev] Warning when calling virtual functions from constructor/desctructor?
Aaron Ballman via cfe-dev
cfe-dev at lists.llvm.org
Thu Dec 13 07:21:29 PST 2018
On Thu, Dec 13, 2018 at 10:08 AM Arnaud Bienner via cfe-dev
<cfe-dev at lists.llvm.org> wrote:
>
> 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?
The static analyzer currently has alpha.cplusplus.VirtualCall that
should catch this sort of issue. Given the concerns about false
positives, perhaps that check could be improved enough to pull it out
of alpha status instead of working on a check within clang itself?
~Aaron
>
> Best regards,
> Arnaud
>
> [1]: https://github.com/llvm-mirror/clang/commit/528a1d17a3ff8361fdd4a1a379adf3f64ec68e00
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
More information about the cfe-dev
mailing list