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/ASTTypeTraits.h" |
12 | #include "clang/AST/DeclCXX.h" |
13 | #include "clang/AST/OperationKinds.h" |
14 | #include "clang/AST/Type.h" |
15 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
16 | #include "clang/ASTMatchers/ASTMatchers.h" |
17 | #include "clang/ASTMatchers/ASTMatchersMacros.h" |
18 | #include "clang/Basic/Diagnostic.h" |
19 | #include "clang/Basic/DiagnosticIDs.h" |
20 | #include "llvm/ADT/SmallVector.h" |
21 | |
22 | using namespace clang::ast_matchers; |
23 | |
24 | namespace clang::tidy::bugprone { |
25 | |
26 | namespace { |
27 | |
28 | AST_MATCHER(CXXMethodDecl, isVirtual) { return Node.isVirtual(); } |
29 | |
30 | static const char *const ErrorMsg = |
31 | "comparing a pointer to member virtual function with other pointer is " |
32 | "unspecified behavior, only compare it with a null-pointer constant for " |
33 | "equality." ; |
34 | |
35 | } // namespace |
36 | |
37 | void ComparePointerToMemberVirtualFunctionCheck::registerMatchers( |
38 | MatchFinder *Finder) { |
39 | |
40 | auto DirectMemberVirtualFunctionPointer = unaryOperator( |
41 | allOf(hasOperatorName(Name: "&" ), |
42 | hasUnaryOperand(InnerMatcher: declRefExpr(to(InnerMatcher: cxxMethodDecl(isVirtual())))))); |
43 | auto IndirectMemberPointer = |
44 | ignoringImpCasts(InnerMatcher: declRefExpr().bind(ID: "indirect_member_pointer" )); |
45 | |
46 | Finder->addMatcher( |
47 | NodeMatch: binaryOperator( |
48 | allOf(hasAnyOperatorName("==" , "!=" ), |
49 | hasEitherOperand( |
50 | InnerMatcher: hasType(InnerMatcher: memberPointerType(pointee(functionType())))), |
51 | anyOf(hasEitherOperand(InnerMatcher: DirectMemberVirtualFunctionPointer), |
52 | hasEitherOperand(InnerMatcher: IndirectMemberPointer)), |
53 | unless(hasEitherOperand( |
54 | InnerMatcher: castExpr(hasCastKind(Kind: CK_NullToMemberPointer)))))) |
55 | .bind(ID: "binary_operator" ), |
56 | Action: this); |
57 | } |
58 | |
59 | void ComparePointerToMemberVirtualFunctionCheck::check( |
60 | const MatchFinder::MatchResult &Result) { |
61 | const auto *BO = Result.Nodes.getNodeAs<BinaryOperator>(ID: "binary_operator" ); |
62 | const auto *DRE = |
63 | Result.Nodes.getNodeAs<DeclRefExpr>(ID: "indirect_member_pointer" ); |
64 | |
65 | if (DRE == nullptr) { |
66 | // compare with pointer to member virtual function. |
67 | diag(Loc: BO->getOperatorLoc(), Description: ErrorMsg); |
68 | return; |
69 | } |
70 | // compare with variable which type is pointer to member function. |
71 | llvm::SmallVector<SourceLocation, 12U> SameSignatureVirtualMethods{}; |
72 | const auto *MPT = cast<MemberPointerType>(DRE->getType().getCanonicalType()); |
73 | const Type *T = MPT->getClass(); |
74 | if (T == nullptr) |
75 | return; |
76 | const CXXRecordDecl *RD = T->getAsCXXRecordDecl(); |
77 | if (RD == nullptr) |
78 | return; |
79 | |
80 | constexpr bool StopVisit = false; |
81 | |
82 | auto VisitSameSignatureVirtualMethods = |
83 | [&](const CXXRecordDecl *CurrentRecordDecl) -> bool { |
84 | bool Ret = !StopVisit; |
85 | for (const auto *MD : CurrentRecordDecl->methods()) { |
86 | if (MD->isVirtual() && MD->getType() == MPT->getPointeeType()) { |
87 | SameSignatureVirtualMethods.push_back(Elt: MD->getBeginLoc()); |
88 | Ret = StopVisit; |
89 | } |
90 | } |
91 | return Ret; |
92 | }; |
93 | |
94 | if (StopVisit != VisitSameSignatureVirtualMethods(RD)) { |
95 | RD->forallBases(BaseMatches: VisitSameSignatureVirtualMethods); |
96 | } |
97 | |
98 | if (!SameSignatureVirtualMethods.empty()) { |
99 | diag(Loc: BO->getOperatorLoc(), Description: ErrorMsg); |
100 | for (const auto Loc : SameSignatureVirtualMethods) |
101 | diag(Loc, Description: "potential member virtual function is declared here." , |
102 | Level: DiagnosticIDs::Note); |
103 | } |
104 | } |
105 | |
106 | } // namespace clang::tidy::bugprone |
107 | |