================ @@ -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), + ---------------- EugeneZelenko wrote:
I don't think that such newlines improve code readability. Same in other places. 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