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

source code of clang-tools-extra/clang-tidy/cert/MutatingCopyCheck.cpp