clang-tools 22.0.0git
ComparePointerToMemberVirtualFunctionCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "clang/AST/ASTContext.h"
11#include "clang/AST/DeclCXX.h"
12#include "clang/AST/OperationKinds.h"
13#include "clang/AST/Type.h"
14#include "clang/ASTMatchers/ASTMatchFinder.h"
15#include "clang/ASTMatchers/ASTMatchers.h"
16#include "clang/ASTMatchers/ASTMatchersMacros.h"
17#include "clang/Basic/DiagnosticIDs.h"
18#include "llvm/ADT/SmallVector.h"
19
20using namespace clang::ast_matchers;
21
22namespace clang::tidy::bugprone {
23
24namespace {
25
26AST_MATCHER(CXXMethodDecl, isVirtual) { return Node.isVirtual(); }
27
28static constexpr llvm::StringLiteral ErrorMsg =
29 "comparing a pointer to member virtual function with other pointer is "
30 "unspecified behavior, only compare it with a null-pointer constant for "
31 "equality.";
32
33} // namespace
34
36 MatchFinder *Finder) {
37
38 auto DirectMemberVirtualFunctionPointer = unaryOperator(
39 allOf(hasOperatorName("&"),
40 hasUnaryOperand(declRefExpr(to(cxxMethodDecl(isVirtual()))))));
41 auto IndirectMemberPointer =
42 ignoringImpCasts(declRefExpr().bind("indirect_member_pointer"));
43
44 Finder->addMatcher(
45 binaryOperator(
46 allOf(hasAnyOperatorName("==", "!="),
47 hasEitherOperand(
48 hasType(memberPointerType(pointee(functionType())))),
49 anyOf(hasEitherOperand(DirectMemberVirtualFunctionPointer),
50 hasEitherOperand(IndirectMemberPointer)),
51 unless(hasEitherOperand(
52 castExpr(hasCastKind(CK_NullToMemberPointer))))))
53 .bind("binary_operator"),
54 this);
55}
56
58 const MatchFinder::MatchResult &Result) {
59 const auto *BO = Result.Nodes.getNodeAs<BinaryOperator>("binary_operator");
60 const auto *DRE =
61 Result.Nodes.getNodeAs<DeclRefExpr>("indirect_member_pointer");
62
63 if (DRE == nullptr) {
64 // compare with pointer to member virtual function.
65 diag(BO->getOperatorLoc(), ErrorMsg);
66 return;
67 }
68 // compare with variable which type is pointer to member function.
69 llvm::SmallVector<SourceLocation, 12U> SameSignatureVirtualMethods{};
70 const auto *MPT = cast<MemberPointerType>(DRE->getType().getCanonicalType());
71 const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl();
72 if (RD == nullptr)
73 return;
74
75 constexpr bool StopVisit = false;
76
77 auto VisitSameSignatureVirtualMethods =
78 [&](const CXXRecordDecl *CurrentRecordDecl) -> bool {
79 bool Ret = !StopVisit;
80 for (const auto *MD : CurrentRecordDecl->methods()) {
81 if (MD->isVirtual() && MD->getType() == MPT->getPointeeType()) {
82 SameSignatureVirtualMethods.push_back(MD->getBeginLoc());
83 Ret = StopVisit;
84 }
85 }
86 return Ret;
87 };
88
89 if (StopVisit != VisitSameSignatureVirtualMethods(RD)) {
90 RD->forallBases(VisitSameSignatureVirtualMethods);
91 }
92
93 if (!SameSignatureVirtualMethods.empty()) {
94 diag(BO->getOperatorLoc(), ErrorMsg);
95 for (const auto Loc : SameSignatureVirtualMethods)
96 diag(Loc, "potential member virtual function is declared here.",
97 DiagnosticIDs::Note);
98 }
99}
100
101} // namespace clang::tidy::bugprone
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
AST_MATCHER(BinaryOperator, isRelationalOperator)