[clang-tools-extra] [clang-tidy]add new check `bugprone-compare-pointer-to-member-virtual-function` (PR #66055)

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 13 02:11:34 PDT 2023


================
@@ -0,0 +1,44 @@
+.. title:: clang-tidy - bugprone-compare-pointer-to-member-virtual-function
+
+bugprone-compare-pointer-to-member-virtual-function
+===================================================
+
+Detects unspecified behavior about equality comparison between pointer to member virtual 
+function and anything other than null-pointer-constant.
+
+
----------------
PiotrZSL wrote:

Some information is missing here why this comparation is bad, here is some my suggestions:

"This detection mechanism aims to identify instances where equality comparisons involve pointers to member virtual functions and any entity other than a null-pointer constant. It is essential to highlight this concern within your code as it addresses potential pitfalls and risks associated with such comparisons.

In certain compilers, virtual function addresses are not conventional pointers but instead consist of offsets and indexes within a virtual function table (vtable). Consequently, these pointers may vary between base and derived classes, leading to unpredictable behavior when compared directly. This issue becomes particularly challenging when dealing with pointers to pure virtual functions, as they may not even have a valid address, further complicating comparisons.

Moreover, when comparing these pointers, it may inadvertently trigger "thunk" functions instead of the intended original functions, further compromising code reliability. Using this approach for comparing objects introduces a level of ambiguity and unpredictability, making it a less reliable choice.

Instead, it is recommended to utilize the typeid operator or other appropriate mechanisms for comparing objects to ensure robust and predictable behavior in your codebase. By heeding this detection and adopting a more reliable comparison method, you can mitigate potential issues related to unspecified behavior, especially when dealing with pointers to member virtual functions or pure virtual functions, thereby improving the overall stability and maintainability of your code."

Pickup whatever you want from it.

https://github.com/llvm/llvm-project/pull/66055


More information about the cfe-commits mailing list