1 | //===--- ComparePointerToMemberVirtualFunctionCheck.cpp - clang-tidy ------===// |
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 | |
9 | #include "ComparePointerToMemberVirtualFunctionCheck.h" |
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 | |
20 | using namespace clang::ast_matchers; |
21 | |
22 | namespace clang::tidy::bugprone { |
23 | |
24 | namespace { |
25 | |
26 | AST_MATCHER(CXXMethodDecl, isVirtual) { return Node.isVirtual(); } |
27 | |
28 | static const char *const 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 | |
35 | void ComparePointerToMemberVirtualFunctionCheck::registerMatchers( |
36 | MatchFinder *Finder) { |
37 | |
38 | auto DirectMemberVirtualFunctionPointer = unaryOperator( |
39 | allOf(hasOperatorName(Name: "&" ), |
40 | hasUnaryOperand(InnerMatcher: declRefExpr(to(InnerMatcher: cxxMethodDecl(isVirtual())))))); |
41 | auto IndirectMemberPointer = |
42 | ignoringImpCasts(InnerMatcher: declRefExpr().bind(ID: "indirect_member_pointer" )); |
43 | |
44 | Finder->addMatcher( |
45 | NodeMatch: binaryOperator( |
46 | allOf(hasAnyOperatorName("==" , "!=" ), |
47 | hasEitherOperand( |
48 | InnerMatcher: hasType(InnerMatcher: memberPointerType(pointee(functionType())))), |
49 | anyOf(hasEitherOperand(InnerMatcher: DirectMemberVirtualFunctionPointer), |
50 | hasEitherOperand(InnerMatcher: IndirectMemberPointer)), |
51 | unless(hasEitherOperand( |
52 | InnerMatcher: castExpr(hasCastKind(Kind: CK_NullToMemberPointer)))))) |
53 | .bind(ID: "binary_operator" ), |
54 | Action: this); |
55 | } |
56 | |
57 | void ComparePointerToMemberVirtualFunctionCheck::check( |
58 | const MatchFinder::MatchResult &Result) { |
59 | const auto *BO = Result.Nodes.getNodeAs<BinaryOperator>(ID: "binary_operator" ); |
60 | const auto *DRE = |
61 | Result.Nodes.getNodeAs<DeclRefExpr>(ID: "indirect_member_pointer" ); |
62 | |
63 | if (DRE == nullptr) { |
64 | // compare with pointer to member virtual function. |
65 | diag(Loc: BO->getOperatorLoc(), Description: 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(Elt: MD->getBeginLoc()); |
83 | Ret = StopVisit; |
84 | } |
85 | } |
86 | return Ret; |
87 | }; |
88 | |
89 | if (StopVisit != VisitSameSignatureVirtualMethods(RD)) { |
90 | RD->forallBases(BaseMatches: VisitSameSignatureVirtualMethods); |
91 | } |
92 | |
93 | if (!SameSignatureVirtualMethods.empty()) { |
94 | diag(Loc: BO->getOperatorLoc(), Description: ErrorMsg); |
95 | for (const auto Loc : SameSignatureVirtualMethods) |
96 | diag(Loc, Description: "potential member virtual function is declared here." , |
97 | Level: DiagnosticIDs::Note); |
98 | } |
99 | } |
100 | |
101 | } // namespace clang::tidy::bugprone |
102 | |