[clang-tools-extra] [clang-tidy] Add check performance-lost-std-move (PR #139525)
Congcong Cai via cfe-commits
cfe-commits at lists.llvm.org
Tue Sep 16 20:40:29 PDT 2025
================
@@ -0,0 +1,195 @@
+//===--- 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"),
----------------
HerrCai0907 wrote:
I am worry about the performance of this matcher because it will check all `DeclRefExpr`. Could we check the child of `CXXConstructExpr` instead.
Then for `parenExpr`, we can simply use ignoreParen
https://github.com/llvm/llvm-project/pull/139525
More information about the cfe-commits
mailing list