aboutsummaryrefslogtreecommitdiff
path: root/clang-tools-extra/clang-tidy/bugprone/ComparePointerToMemberVirtualFunctionCheck.cpp
blob: 1cbf1e22a33a7bedb977b2ba38626fc1696b657f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//===--- 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/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/DiagnosticIDs.h"
#include "llvm/ADT/SmallVector.h"

using namespace clang::ast_matchers;

namespace clang::tidy::bugprone {

namespace {

AST_MATCHER(CXXMethodDecl, isVirtual) { return Node.isVirtual(); }

static constexpr llvm::StringLiteral ErrorMsg =
    "comparing a pointer to member virtual function with other pointer is "
    "unspecified behavior, only compare it with a null-pointer constant for "
    "equality.";

} // namespace

void ComparePointerToMemberVirtualFunctionCheck::registerMatchers(
    MatchFinder *Finder) {

  auto DirectMemberVirtualFunctionPointer = unaryOperator(
      allOf(hasOperatorName("&"),
            hasUnaryOperand(declRefExpr(to(cxxMethodDecl(isVirtual()))))));
  auto IndirectMemberPointer =
      ignoringImpCasts(declRefExpr().bind("indirect_member_pointer"));

  Finder->addMatcher(
      binaryOperator(
          allOf(hasAnyOperatorName("==", "!="),
                hasEitherOperand(
                    hasType(memberPointerType(pointee(functionType())))),
                anyOf(hasEitherOperand(DirectMemberVirtualFunctionPointer),
                      hasEitherOperand(IndirectMemberPointer)),
                unless(hasEitherOperand(
                    castExpr(hasCastKind(CK_NullToMemberPointer))))))
          .bind("binary_operator"),
      this);
}

void ComparePointerToMemberVirtualFunctionCheck::check(
    const MatchFinder::MatchResult &Result) {
  const auto *BO = Result.Nodes.getNodeAs<BinaryOperator>("binary_operator");
  const auto *DRE =
      Result.Nodes.getNodeAs<DeclRefExpr>("indirect_member_pointer");

  if (DRE == nullptr) {
    // compare with pointer to member virtual function.
    diag(BO->getOperatorLoc(), ErrorMsg);
    return;
  }
  // compare with variable which type is pointer to member function.
  llvm::SmallVector<SourceLocation, 12U> SameSignatureVirtualMethods{};
  const auto *MPT = cast<MemberPointerType>(DRE->getType().getCanonicalType());
  const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
  if (RD == nullptr)
    return;

  constexpr bool StopVisit = false;

  auto VisitSameSignatureVirtualMethods =
      [&](const CXXRecordDecl *CurrentRecordDecl) -> bool {
    bool Ret = !StopVisit;
    for (const auto *MD : CurrentRecordDecl->methods()) {
      if (MD->isVirtual() && MD->getType() == MPT->getPointeeType()) {
        SameSignatureVirtualMethods.push_back(MD->getBeginLoc());
        Ret = StopVisit;
      }
    }
    return Ret;
  };

  if (StopVisit != VisitSameSignatureVirtualMethods(RD)) {
    RD->forallBases(VisitSameSignatureVirtualMethods);
  }

  if (!SameSignatureVirtualMethods.empty()) {
    diag(BO->getOperatorLoc(), ErrorMsg);
    for (const auto Loc : SameSignatureVirtualMethods)
      diag(Loc, "potential member virtual function is declared here.",
           DiagnosticIDs::Note);
  }
}

} // namespace clang::tidy::bugprone