================ @@ -0,0 +1,180 @@ +//===--- 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 const Expr* getLastVarUsage(const VarDecl& Var, const Decl& Func, + ASTContext& Context) { + auto Exprs = allDeclRefExprsHonourLambda(Var, Func, Context); + + 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::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; ---------------- EugeneZelenko wrote:
Should be two lines as next statement. https://github.com/llvm/llvm-project/pull/139525 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits