clang-tools 10.0.0

MutatingCopyCheck.cpp
Go to the documentation of this file.
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
23 void MutatingCopyCheck::registerMatchers(MatchFinder *Finder) {
24 if (!getLangOpts().CPlusPlus)
25 return;
26
27 const auto MemberExprOrSourceObject = anyOf(
28 memberExpr(), declRefExpr(to(decl(equalsBoundNode(SourceDeclName)))));
29
30 const auto IsPartOfSource =
31 allOf(unless(hasDescendant(expr(unless(MemberExprOrSourceObject)))),
32 MemberExprOrSourceObject);
33
34 const auto IsSourceMutatingAssignment =
35 expr(anyOf(binaryOperator(isAssignmentOperator(), hasLHS(IsPartOfSource))
36 .bind(MutatingOperatorName),
37 cxxOperatorCallExpr(isAssignmentOperator(),
38 hasArgument(0, IsPartOfSource))
39 .bind(MutatingOperatorName)));
40
41 const auto MemberExprOrSelf = anyOf(memberExpr(), cxxThisExpr());
42
43 const auto IsPartOfSelf = allOf(
44 unless(hasDescendant(expr(unless(MemberExprOrSelf)))), MemberExprOrSelf);
45
46 const auto IsSelfMutatingAssignment =
47 expr(anyOf(binaryOperator(isAssignmentOperator(), hasLHS(IsPartOfSelf)),
48 cxxOperatorCallExpr(isAssignmentOperator(),
49 hasArgument(0, IsPartOfSelf))));
50
51 const auto IsSelfMutatingMemberFunction =
52 functionDecl(hasBody(hasDescendant(IsSelfMutatingAssignment)));
53
54 const auto IsSourceMutatingMemberCall =
55 cxxMemberCallExpr(on(IsPartOfSource),
56 callee(IsSelfMutatingMemberFunction))
57 .bind(MutatingCallName);
58
59 const auto MutatesSource = allOf(
60 hasParameter(
61 0, parmVarDecl(hasType(lValueReferenceType())).bind(SourceDeclName)),
62 anyOf(forEachDescendant(IsSourceMutatingAssignment),
63 forEachDescendant(IsSourceMutatingMemberCall)));
64
65 Finder->addMatcher(cxxConstructorDecl(isCopyConstructor(), MutatesSource),
66 this);
67
68 Finder->addMatcher(cxxMethodDecl(isCopyAssignmentOperator(), MutatesSource),
69 this);
70 }
71
72 void MutatingCopyCheck::check(const MatchFinder::MatchResult &Result) {
73 if (const auto *MemberCall =
74 Result.Nodes.getNodeAs<CXXMemberCallExpr>(MutatingCallName))
75 diag(MemberCall->getBeginLoc(), "call mutates copied object");
76 else if (const auto *Assignment =
77 Result.Nodes.getNodeAs<Expr>(MutatingOperatorName))
78 diag(Assignment->getBeginLoc(), "mutating copied object");
79 }
80
81 } // namespace cert
82 } // namespace tidy
83 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:14
MutatingCopyCheck.h
clang::tidy::cert::MutatingCallName
static constexpr llvm::StringLiteral MutatingCallName
Definition: MutatingCopyCheck.cpp:21
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::cert::SourceDeclName
static constexpr llvm::StringLiteral SourceDeclName
Definition: MutatingCopyCheck.cpp:19
clang::tidy::cert::MutatingOperatorName
static constexpr llvm::StringLiteral MutatingOperatorName
Definition: MutatingCopyCheck.cpp:20