1 //===--- MutatingCopyCheck.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 "MutatingCopyCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 
13 using namespace clang::ast_matchers;
14 
15 namespace clang {
16 namespace tidy {
17 namespace cert {
18 
19 static constexpr llvm::StringLiteral SourceDeclName = "ChangedPVD";
20 static constexpr llvm::StringLiteral MutatingOperatorName = "MutatingOp";
21 static constexpr llvm::StringLiteral MutatingCallName = "MutatingCall";
22 
registerMatchers(MatchFinder * Finder)23 void MutatingCopyCheck::registerMatchers(MatchFinder *Finder) {
24   const auto MemberExprOrSourceObject = anyOf(
25       memberExpr(),
26       declRefExpr(to(decl(equalsBoundNode(std::string(SourceDeclName))))));
27 
28   const auto IsPartOfSource =
29       allOf(unless(hasDescendant(expr(unless(MemberExprOrSourceObject)))),
30             MemberExprOrSourceObject);
31 
32   const auto IsSourceMutatingAssignment = traverse(
33       ast_type_traits::TK_AsIs,
34       expr(anyOf(binaryOperator(isAssignmentOperator(), hasLHS(IsPartOfSource))
35                      .bind(MutatingOperatorName),
36                  cxxOperatorCallExpr(isAssignmentOperator(),
37                                      hasArgument(0, IsPartOfSource))
38                      .bind(MutatingOperatorName))));
39 
40   const auto MemberExprOrSelf = anyOf(memberExpr(), cxxThisExpr());
41 
42   const auto IsPartOfSelf = allOf(
43       unless(hasDescendant(expr(unless(MemberExprOrSelf)))), MemberExprOrSelf);
44 
45   const auto IsSelfMutatingAssignment =
46       expr(anyOf(binaryOperator(isAssignmentOperator(), hasLHS(IsPartOfSelf)),
47                  cxxOperatorCallExpr(isAssignmentOperator(),
48                                      hasArgument(0, IsPartOfSelf))));
49 
50   const auto IsSelfMutatingMemberFunction =
51       functionDecl(hasBody(hasDescendant(IsSelfMutatingAssignment)));
52 
53   const auto IsSourceMutatingMemberCall =
54       cxxMemberCallExpr(on(IsPartOfSource),
55                         callee(IsSelfMutatingMemberFunction))
56           .bind(MutatingCallName);
57 
58   const auto MutatesSource = allOf(
59       hasParameter(
60           0, parmVarDecl(hasType(lValueReferenceType())).bind(SourceDeclName)),
61       anyOf(forEachDescendant(IsSourceMutatingAssignment),
62             forEachDescendant(IsSourceMutatingMemberCall)));
63 
64   Finder->addMatcher(cxxConstructorDecl(isCopyConstructor(), MutatesSource),
65                      this);
66 
67   Finder->addMatcher(cxxMethodDecl(isCopyAssignmentOperator(), MutatesSource),
68                      this);
69 }
70 
check(const MatchFinder::MatchResult & Result)71 void MutatingCopyCheck::check(const MatchFinder::MatchResult &Result) {
72   if (const auto *MemberCall =
73           Result.Nodes.getNodeAs<CXXMemberCallExpr>(MutatingCallName))
74     diag(MemberCall->getBeginLoc(), "call mutates copied object");
75   else if (const auto *Assignment =
76                Result.Nodes.getNodeAs<Expr>(MutatingOperatorName))
77     diag(Assignment->getBeginLoc(), "mutating copied object");
78 }
79 
80 } // namespace cert
81 } // namespace tidy
82 } // namespace clang
83