From 519de4b69205a17df69250e92423d8bd482bbf99 Mon Sep 17 00:00:00 2001 From: Felix Berger Date: Fri, 16 Dec 2016 02:47:56 +0000 Subject: [PATCH] [clang-tidy] Do not move parameter if only DeclRefExpr occurs inside of a loop Summary: This fixes a bug where the performance-unnecessary-value-param check suggests a fix to move the parameter inside of a loop which could be invoked multiple times. Reviewers: sbenza, aaron.ballman, alexfh Subscribers: JDevlieghere, cfe-commits Differential Revision: https://reviews.llvm.org/D27187 llvm-svn: 289912 --- .../performance/UnnecessaryValueParamCheck.cpp | 13 +++++++++++++ .../performance-unnecessary-value-param.cpp | 9 +++++++++ 2 files changed, 22 insertions(+) diff --git a/clang-tools-extra/clang-tidy/performance/UnnecessaryValueParamCheck.cpp b/clang-tools-extra/clang-tidy/performance/UnnecessaryValueParamCheck.cpp index 5d746d6ab771..0b5838432295 100644 --- a/clang-tools-extra/clang-tidy/performance/UnnecessaryValueParamCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/UnnecessaryValueParamCheck.cpp @@ -47,6 +47,17 @@ bool isReferencedOutsideOfCallExpr(const FunctionDecl &Function, return !Matches.empty(); } +bool hasLoopStmtAncestor(const DeclRefExpr &DeclRef, const Stmt &Stmt, + ASTContext &Context) { + auto Matches = + match(findAll(declRefExpr( + equalsNode(&DeclRef), + unless(hasAncestor(stmt(anyOf(forStmt(), cxxForRangeStmt(), + whileStmt(), doStmt())))))), + Stmt, Context); + return Matches.empty(); +} + } // namespace UnnecessaryValueParamCheck::UnnecessaryValueParamCheck( @@ -105,6 +116,8 @@ void UnnecessaryValueParamCheck::check(const MatchFinder::MatchResult &Result) { if (!IsConstQualified) { auto CanonicalType = Param->getType().getCanonicalType(); if (AllDeclRefExprs.size() == 1 && + !hasLoopStmtAncestor(**AllDeclRefExprs.begin(), *Function->getBody(), + *Result.Context) && ((utils::type_traits::hasNonTrivialMoveConstructor(CanonicalType) && utils::decl_ref_expr::isCopyConstructorArgument( **AllDeclRefExprs.begin(), *Function->getBody(), diff --git a/clang-tools-extra/test/clang-tidy/performance-unnecessary-value-param.cpp b/clang-tools-extra/test/clang-tidy/performance-unnecessary-value-param.cpp index 5f7a4dd91d4e..18fcf0ffca2d 100644 --- a/clang-tools-extra/test/clang-tidy/performance-unnecessary-value-param.cpp +++ b/clang-tools-extra/test/clang-tidy/performance-unnecessary-value-param.cpp @@ -225,6 +225,15 @@ void PositiveMoveOnCopyAssignment(ExpensiveMovableType E) { // CHECK-FIXES: F = std::move(E); } +// The argument could be moved but is not since copy statement is inside a loop. +void PositiveNoMoveInsideLoop(ExpensiveMovableType E) { + // CHECK-MESSAGES: [[@LINE-1]]:52: warning: the parameter 'E' is copied + // CHECK-FIXES: void PositiveNoMoveInsideLoop(const ExpensiveMovableType& E) { + for (;;) { + auto F = E; + } +} + void PositiveConstRefNotMoveConstructible(ExpensiveToCopyType T) { // CHECK-MESSAGES: [[@LINE-1]]:63: warning: the parameter 'T' is copied // CHECK-FIXES: void PositiveConstRefNotMoveConstructible(const ExpensiveToCopyType& T) {