[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
Tue Sep 12 01:59:48 PDT 2023


================
@@ -0,0 +1,89 @@
+//===--- ComparePointerToMemberVirtualFunctionCheck.cpp - clang-tidy ------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "ComparePointerToMemberVirtualFunctionCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/ASTTypeTraits.h"
+#include "clang/AST/DeclCXX.h"
+#include "clang/AST/OperationKinds.h"
+#include "clang/AST/Type.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/ASTMatchers/ASTMatchersMacros.h"
+#include "clang/Basic/Diagnostic.h"
+#include "clang/Basic/DiagnosticIDs.h"
+#include "llvm/ADT/SmallVector.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::bugprone {
+
+AST_MATCHER(CXXMethodDecl, isVirtual) { return Node.isVirtual(); }
+
+void ComparePointerToMemberVirtualFunctionCheck::registerMatchers(
+    MatchFinder *Finder) {
+
+  auto DirectMemberPointer = unaryOperator(
+      allOf(hasOperatorName("&"),
+            hasUnaryOperand(declRefExpr(to(cxxMethodDecl(isVirtual()))))));
+  auto IndirectMemberPointer = ignoringImpCasts(declRefExpr());
+
+  auto BinaryOperatorMatcher = [](auto &&Matcher) {
+    return binaryOperator(allOf(hasAnyOperatorName("==", "!="),
+                                hasEitherOperand(hasType(memberPointerType(
+                                    pointee(functionType())))),
+                                hasEitherOperand(Matcher)),
+                          unless(hasEitherOperand(
+                              castExpr(hasCastKind(CK_NullToMemberPointer)))));
+  };
+
+  Finder->addMatcher(
+      BinaryOperatorMatcher(DirectMemberPointer).bind("direct_member_pointer"),
+      this);
+
+  Finder->addMatcher(BinaryOperatorMatcher(IndirectMemberPointer)
+                         .bind("indirect_member_pointer"),
+                     this);
+}
+
+static const char *const ErrorMsg =
+    "A pointer to member virtual function shall only be tested for equality "
+    "with null-pointer-constant.";
+
+static const Expr *removeImplicitCast(const Expr *E) {
+  while (const auto *ICE = dyn_cast<ImplicitCastExpr>(E))
+    E = ICE->getSubExpr();
+  return E;
+}
+
+void ComparePointerToMemberVirtualFunctionCheck::check(
+    const MatchFinder::MatchResult &Result) {
+  if (const auto *DirectCompare =
+          Result.Nodes.getNodeAs<BinaryOperator>("direct_member_pointer")) {
+    diag(DirectCompare->getOperatorLoc(), ErrorMsg);
+  } else if (const auto *IndirectCompare =
----------------
PiotrZSL wrote:

no need for else, just put return....

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


More information about the cfe-commits mailing list