10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
23 void MutatingCopyCheck::registerMatchers(MatchFinder *Finder) {
24 const auto MemberExprOrSourceObject = anyOf(
26 declRefExpr(to(decl(equalsBoundNode(std::string(
SourceDeclName))))));
28 const auto IsPartOfSource =
29 allOf(unless(hasDescendant(expr(unless(MemberExprOrSourceObject)))),
30 MemberExprOrSourceObject);
32 const auto IsSourceMutatingAssignment = traverse(
33 ast_type_traits::TK_AsIs,
34 expr(anyOf(binaryOperator(isAssignmentOperator(), hasLHS(IsPartOfSource))
36 cxxOperatorCallExpr(isAssignmentOperator(),
37 hasArgument(0, IsPartOfSource))
40 const auto MemberExprOrSelf = anyOf(memberExpr(), cxxThisExpr());
42 const auto IsPartOfSelf = allOf(
43 unless(hasDescendant(expr(unless(MemberExprOrSelf)))), MemberExprOrSelf);
45 const auto IsSelfMutatingAssignment =
46 expr(anyOf(binaryOperator(isAssignmentOperator(), hasLHS(IsPartOfSelf)),
47 cxxOperatorCallExpr(isAssignmentOperator(),
48 hasArgument(0, IsPartOfSelf))));
50 const auto IsSelfMutatingMemberFunction =
51 functionDecl(hasBody(hasDescendant(IsSelfMutatingAssignment)));
53 const auto IsSourceMutatingMemberCall =
54 cxxMemberCallExpr(on(IsPartOfSource),
55 callee(IsSelfMutatingMemberFunction))
58 const auto MutatesSource = allOf(
60 0, parmVarDecl(hasType(lValueReferenceType())).bind(
SourceDeclName)),
61 anyOf(forEachDescendant(IsSourceMutatingAssignment),
62 forEachDescendant(IsSourceMutatingMemberCall)));
64 Finder->addMatcher(cxxConstructorDecl(isCopyConstructor(), MutatesSource),
67 Finder->addMatcher(cxxMethodDecl(isCopyAssignmentOperator(), MutatesSource),
71 void MutatingCopyCheck::check(
const MatchFinder::MatchResult &Result) {
72 if (
const auto *MemberCall =
74 diag(MemberCall->getBeginLoc(),
"call mutates copied object");
75 else if (
const auto *Assignment =
77 diag(Assignment->getBeginLoc(),
"mutating copied object");