| 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::tidy::cert { |
| 16 |
|
| 17 |
static constexpr llvm::StringLiteral SourceDeclName = "ChangedPVD"; |
| 18 |
static constexpr llvm::StringLiteral MutatingOperatorName = "MutatingOp"; |
| 19 |
static constexpr llvm::StringLiteral MutatingCallName = "MutatingCall"; |
| 20 |
|
| 21 |
void MutatingCopyCheck::registerMatchers(MatchFinder *Finder) { |
| 22 |
const auto MemberExprOrSourceObject = anyOf( |
| 23 |
memberExpr(), |
| 24 |
declRefExpr(to(decl(equalsBoundNode(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_AsIs, binaryOperation(hasOperatorName("="), hasLHS(IsPartOfSource)) |
| 32 |
.bind(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(IsPartOfSelf)); |
| 41 |
|
| 42 |
const auto IsSelfMutatingMemberFunction = |
| 43 |
functionDecl(hasBody(hasDescendant(IsSelfMutatingAssignment))); |
| 44 |
|
| 45 |
const auto IsSourceMutatingMemberCall = |
| 46 |
cxxMemberCallExpr(on(IsPartOfSource), |
| 47 |
callee(IsSelfMutatingMemberFunction)) |
| 48 |
.bind(MutatingCallName); |
| 49 |
|
| 50 |
const auto MutatesSource = allOf( |
| 51 |
hasParameter( |
| 52 |
0, parmVarDecl(hasType(lValueReferenceType())).bind(SourceDeclName)), |
| 53 |
anyOf(forEachDescendant(IsSourceMutatingAssignment), |
| 54 |
forEachDescendant(IsSourceMutatingMemberCall))); |
| 55 |
|
| 56 |
Finder->addMatcher(cxxConstructorDecl(isCopyConstructor(), MutatesSource), |
| 57 |
this); |
| 58 |
|
| 59 |
Finder->addMatcher(cxxMethodDecl(isCopyAssignmentOperator(), MutatesSource), |
| 60 |
this); |
| 61 |
} |
| 62 |
|
| 63 |
void MutatingCopyCheck::check(const MatchFinder::MatchResult &Result) { |
| 64 |
if (const auto *MemberCall = |
| 65 |
Result.Nodes.getNodeAs<CXXMemberCallExpr>(MutatingCallName)) |
| 66 |
diag(MemberCall->getBeginLoc(), "call mutates copied object"); |
| 67 |
else if (const auto *Assignment = |
| 68 |
Result.Nodes.getNodeAs<Expr>(MutatingOperatorName)) |
| 69 |
diag(Assignment->getBeginLoc(), "mutating copied object"); |
| 70 |
} |
| 71 |
|
| 72 |
} // namespace clang::tidy::cert |