-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[clang-tidy] Add check performance-lost-std-move #139525
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 31 commits
3abbce9
4a1a77b
f74066d
edd4800
5359c69
5bb6af6
592d79b
1710c2f
b4824ef
60e4aca
a27b1b3
6aacd41
71cd708
402ba55
fdf01b6
24357a2
b48425b
986d6aa
5fb15e1
4a1d653
0d612ec
6f55a1c
8963056
55c4d16
ee844fd
2fd53f6
54571f9
d28087b
b212770
aa46750
4ae1247
62e4951
e12c39c
97db45c
7b091cc
9816cdb
6b31812
e8b2d48
777eacd
67790b8
708317b
ed6d376
4f62ad9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,215 @@ | ||||
//===--- LostStdMoveCheck.cpp - clang-tidy --------------------------------===// | ||||
// | ||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||
// See https://llvm.org/LICENSE.txt for license information. | ||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||
// | ||||
//===----------------------------------------------------------------------===// | ||||
|
||||
#include "LostStdMoveCheck.h" | ||||
#include "clang/ASTMatchers/ASTMatchFinder.h" | ||||
#include "clang/Lex/Lexer.h" | ||||
|
||||
using namespace clang::ast_matchers; | ||||
|
||||
namespace clang::tidy::performance { | ||||
|
||||
template <typename Node> | ||||
void extractNodesByIdTo(ArrayRef<BoundNodes> Matches, StringRef ID, | ||||
llvm::SmallPtrSet<const Node *, 16> &Nodes) { | ||||
for (const BoundNodes &Match : Matches) | ||||
Nodes.insert(Match.getNodeAs<Node>(ID)); | ||||
} | ||||
|
||||
static llvm::SmallPtrSet<const DeclRefExpr *, 16> | ||||
allDeclRefExprsHonourLambda(const VarDecl &VarDecl, const Decl &Decl, | ||||
ASTContext &Context) { | ||||
auto Matches = match( | ||||
decl(forEachDescendant( | ||||
declRefExpr(to(varDecl(equalsNode(&VarDecl))), | ||||
unless(hasAncestor(lambdaExpr(hasAnyCapture(lambdaCapture( | ||||
capturesVar(varDecl(equalsNode(&VarDecl))))))))) | ||||
.bind("declRef"))), | ||||
Decl, Context); | ||||
llvm::SmallPtrSet<const DeclRefExpr *, 16> DeclRefs; | ||||
extractNodesByIdTo(Matches, "declRef", DeclRefs); | ||||
return DeclRefs; | ||||
} | ||||
|
||||
static llvm::SmallPtrSet<const VarDecl *, 16> | ||||
allVarDeclsExprs(const VarDecl &VarDecl, const Decl &Decl, | ||||
ASTContext &Context) { | ||||
auto Matches = match( | ||||
decl(forEachDescendant(declRefExpr( | ||||
to(varDecl(equalsNode(&VarDecl))), | ||||
hasParent(decl( | ||||
varDecl(hasType(qualType(referenceType()))).bind("varDecl"))), | ||||
unless(hasAncestor(lambdaExpr(hasAnyCapture( | ||||
lambdaCapture(capturesVar(varDecl(equalsNode(&VarDecl))))))))))), | ||||
Decl, Context); | ||||
llvm::SmallPtrSet<const class VarDecl *, 16> VarDecls; | ||||
extractNodesByIdTo(Matches, "varDecl", VarDecls); | ||||
return VarDecls; | ||||
} | ||||
|
||||
static const Expr * | ||||
getLastVarUsage(const llvm::SmallPtrSet<const DeclRefExpr *, 16> &Exprs) { | ||||
const Expr *LastExpr = nullptr; | ||||
for (const clang::DeclRefExpr *Expr : Exprs) { | ||||
if (!LastExpr) | ||||
LastExpr = Expr; | ||||
|
||||
if (LastExpr->getBeginLoc() < Expr->getBeginLoc()) | ||||
LastExpr = Expr; | ||||
} | ||||
|
||||
return LastExpr; | ||||
} | ||||
|
||||
AST_MATCHER(CXXRecordDecl, hasTrivialMoveConstructor) { | ||||
return Node.hasDefinition() && Node.hasTrivialMoveConstructor(); | ||||
} | ||||
|
||||
void LostStdMoveCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { | ||||
Options.store(Opts, "StrictMode", StrictMode); | ||||
} | ||||
|
||||
LostStdMoveCheck::LostStdMoveCheck(StringRef Name, ClangTidyContext *Context) | ||||
: ClangTidyCheck(Name, Context), | ||||
StrictMode(Options.get("StrictMode", true)) {} | ||||
|
||||
void LostStdMoveCheck::registerMatchers(MatchFinder *Finder) { | ||||
auto ReturnParent = | ||||
hasParent(expr(hasParent(cxxConstructExpr(hasParent(returnStmt()))))); | ||||
|
||||
auto OutermostExpr = expr(unless(hasParent(expr()))); | ||||
auto LeafStatement = stmt(OutermostExpr); | ||||
|
||||
Finder->addMatcher( | ||||
declRefExpr( | ||||
// not "return x;" | ||||
unless(ReturnParent), | ||||
|
||||
|
||||
unless(hasType(namedDecl(hasName("::std::string_view")))), | ||||
|
||||
// non-trivial type | ||||
hasType(hasCanonicalType(hasDeclaration(cxxRecordDecl()))), | ||||
|
||||
// non-trivial X(X&&) | ||||
unless(hasType(hasCanonicalType( | ||||
hasDeclaration(cxxRecordDecl(hasTrivialMoveConstructor()))))), | ||||
|
||||
// Not in a cycle | ||||
unless(hasAncestor(forStmt())), | ||||
|
||||
unless(hasAncestor(doStmt())), | ||||
|
||||
unless(hasAncestor(whileStmt())), | ||||
|
||||
// Not in a body of lambda | ||||
unless(hasAncestor(compoundStmt(hasAncestor(lambdaExpr())))), | ||||
|
||||
// only non-X& | ||||
unless(hasDeclaration( | ||||
varDecl(hasType(qualType(lValueReferenceType()))))), | ||||
|
||||
hasAncestor(LeafStatement.bind("leaf_statement")), | ||||
|
||||
hasDeclaration( | ||||
varDecl(hasAncestor(functionDecl().bind("func"))).bind("decl")), | ||||
|
||||
anyOf( | ||||
|
||||
// f(x) | ||||
hasParent(expr(hasParent(cxxConstructExpr())).bind("use_parent")), | ||||
|
||||
// f((x)) | ||||
hasParent(parenExpr(hasParent( | ||||
expr(hasParent(cxxConstructExpr())).bind("use_parent")))) | ||||
|
||||
) | ||||
|
||||
) | ||||
.bind("use"), | ||||
this); | ||||
} | ||||
|
||||
void LostStdMoveCheck::check(const MatchFinder::MatchResult &Result) { | ||||
const auto *MatchedDecl = Result.Nodes.getNodeAs<VarDecl>("decl"); | ||||
const auto *MatchedFunc = Result.Nodes.getNodeAs<FunctionDecl>("func"); | ||||
const auto *MatchedUse = Result.Nodes.getNodeAs<Expr>("use"); | ||||
const auto *MatchedUseCall = Result.Nodes.getNodeAs<CallExpr>("use_parent"); | ||||
const auto *MatchedLeafStatement = | ||||
Result.Nodes.getNodeAs<Stmt>("leaf_statement"); | ||||
|
||||
if (!MatchedDecl->hasLocalStorage()) | ||||
return; | ||||
|
||||
if (MatchedUseCall) { | ||||
return; | ||||
} | ||||
|
||||
if (StrictMode) { | ||||
llvm::SmallPtrSet<const VarDecl *, 16> AllVarDecls = | ||||
allVarDeclsExprs(*MatchedDecl, *MatchedFunc, *Result.Context); | ||||
if (!AllVarDecls.empty()) { | ||||
// x is referenced by local var, it may outlive the "use" | ||||
return; | ||||
} | ||||
} | ||||
|
||||
llvm::SmallPtrSet<const DeclRefExpr *, 16> AllReferences = | ||||
allDeclRefExprsHonourLambda(*MatchedDecl, *MatchedFunc, *Result.Context); | ||||
const Expr *LastUsage = getLastVarUsage(AllReferences); | ||||
|
||||
if (LastUsage && LastUsage->getBeginLoc() > MatchedUse->getBeginLoc()) { | ||||
// "use" is not the last reference to x | ||||
return; | ||||
} | ||||
|
||||
if (LastUsage && | ||||
LastUsage->getSourceRange() != MatchedUse->getSourceRange()) { | ||||
return; | ||||
} | ||||
|
||||
// Calculate X usage count in the statement | ||||
llvm::SmallPtrSet<const DeclRefExpr *, 16> DeclRefs; | ||||
|
||||
SmallVector<BoundNodes, 1> Matches = match( | ||||
findAll(declRefExpr( | ||||
|
||||
|
Same below.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,38 @@ | ||||||
//===--- LostStdMoveCheck.h - clang-tidy ------------------------*- C++ -*-===// | ||||||
|
//===--- LostStdMoveCheck.h - clang-tidy ------------------------*- C++ -*-===// | |
//===----------------------------------------------------------------------===// |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// http://clang.llvm.org/extra/clang-tidy/checks/performance/lost-std-move.html | |
/// https://clang.llvm.org/extra/clang-tidy/checks/performance/lost-std-move.html |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,47 @@ | ||||||||||
.. title:: clang-tidy - performance-lost-std-move | ||||||||||
|
||||||||||
performance-lost-std-move | ||||||||||
========================= | ||||||||||
|
||||||||||
Warns if copy constructor is used instead of ``std::move()`` and suggests a fix. | ||||||||||
It honours cycles, lambdas, and unspecified call order in compound expressions. | ||||||||||
|
||||||||||
.. code-block:: c++ | ||||||||||
|
||||||||||
void f(X); | ||||||||||
|
||||||||||
void g(X x) { | ||||||||||
f(x); // warning: Could be std::move() [performance-lost-std-move] | ||||||||||
} | ||||||||||
|
||||||||||
|
||||||||||
It finds the last local variable usage, and if it is a copy, emits a warning. | ||||||||||
The check is based on pure AST matching and doesn't take into account any | ||||||||||
data flow information. Thus, it doesn't catch assign-after-copy cases. | ||||||||||
|
||||||||||
Also it does notice variable references "behind the scenes": | ||||||||||
|
||||||||||
.. code-block:: c++ | ||||||||||
|
||||||||||
void f(X); | ||||||||||
|
||||||||||
void g(X x) { | ||||||||||
auto &y = x; | ||||||||||
f(x); // does not emit a warning | ||||||||||
y.f(); // because is still used | ||||||||||
} | ||||||||||
|
||||||||||
If you want to ignore assigns to reference variables, set ``StrictMode`` | ||||||||||
to ``false``. | ||||||||||
|
If you want to ignore assigns to reference variables, set ``StrictMode`` | |
to ``false``. | |
If you want to ignore assigns to reference variables, set :option:`StrictMode` | |
to `false`. |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please follow 80 characters limit.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// RUN: %check_clang_tidy %s performance-lost-std-move %t -- -config='{CheckOptions: {performance-lost-std-move.StrictMode: false}}' | ||
|
||
namespace std { | ||
|
||
template<typename T> | ||
class shared_ptr { | ||
public: | ||
T& operator*() { return reinterpret_cast<T&>(*this); } | ||
shared_ptr() {} | ||
shared_ptr(const shared_ptr<T>&) {} | ||
}; | ||
|
||
template<typename T> | ||
T&& move(T&) | ||
{ | ||
} | ||
|
||
} // namespace std | ||
|
||
int f(std::shared_ptr<int>); | ||
|
||
void f_copy_after_ref() | ||
{ | ||
std::shared_ptr<int> ptr; | ||
auto& ref = ptr; | ||
f(ptr); | ||
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: could be std::move() [performance-lost-std-move] | ||
// CHECK-FIXES: f(std::move(ptr)); | ||
*ref = 1; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.