Felix Berger | 3c8edde | 2016-03-29 02:42:38 +0000 | [diff] [blame] | 1 | //===--- UnnecessaryValueParamCheck.cpp - clang-tidy-----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "UnnecessaryValueParamCheck.h" |
| 11 | |
| 12 | #include "../utils/DeclRefExprUtils.h" |
| 13 | #include "../utils/FixItHintUtils.h" |
| 14 | #include "../utils/Matchers.h" |
Felix Berger | 7f88275 | 2016-07-01 20:12:15 +0000 | [diff] [blame] | 15 | #include "../utils/TypeTraits.h" |
| 16 | #include "clang/Frontend/CompilerInstance.h" |
| 17 | #include "clang/Lex/Lexer.h" |
| 18 | #include "clang/Lex/Preprocessor.h" |
Felix Berger | 3c8edde | 2016-03-29 02:42:38 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace clang::ast_matchers; |
| 21 | |
| 22 | namespace clang { |
| 23 | namespace tidy { |
| 24 | namespace performance { |
| 25 | |
| 26 | namespace { |
| 27 | |
| 28 | std::string paramNameOrIndex(StringRef Name, size_t Index) { |
| 29 | return (Name.empty() ? llvm::Twine('#') + llvm::Twine(Index + 1) |
| 30 | : llvm::Twine('\'') + Name + llvm::Twine('\'')) |
| 31 | .str(); |
| 32 | } |
| 33 | |
Felix Berger | 7f88275 | 2016-07-01 20:12:15 +0000 | [diff] [blame] | 34 | template <typename S> |
| 35 | bool isSubset(const S &SubsetCandidate, const S &SupersetCandidate) { |
| 36 | for (const auto &E : SubsetCandidate) |
| 37 | if (SupersetCandidate.count(E) == 0) |
| 38 | return false; |
| 39 | return true; |
| 40 | } |
| 41 | |
Felix Berger | 85f9e8b3 | 2016-11-10 01:28:22 +0000 | [diff] [blame] | 42 | bool isReferencedOutsideOfCallExpr(const FunctionDecl &Function, |
| 43 | ASTContext &Context) { |
| 44 | auto Matches = match(declRefExpr(to(functionDecl(equalsNode(&Function))), |
| 45 | unless(hasAncestor(callExpr()))), |
| 46 | Context); |
| 47 | return !Matches.empty(); |
| 48 | } |
| 49 | |
Malcolm Parsons | cfa7d37 | 2017-01-03 12:10:44 +0000 | [diff] [blame] | 50 | bool hasLoopStmtAncestor(const DeclRefExpr &DeclRef, const Decl &Decl, |
Felix Berger | 519de4b | 2016-12-16 02:47:56 +0000 | [diff] [blame] | 51 | ASTContext &Context) { |
| 52 | auto Matches = |
Malcolm Parsons | cfa7d37 | 2017-01-03 12:10:44 +0000 | [diff] [blame] | 53 | match(decl(forEachDescendant(declRefExpr( |
Felix Berger | 519de4b | 2016-12-16 02:47:56 +0000 | [diff] [blame] | 54 | equalsNode(&DeclRef), |
| 55 | unless(hasAncestor(stmt(anyOf(forStmt(), cxxForRangeStmt(), |
Malcolm Parsons | cfa7d37 | 2017-01-03 12:10:44 +0000 | [diff] [blame] | 56 | whileStmt(), doStmt()))))))), |
| 57 | Decl, Context); |
Felix Berger | 519de4b | 2016-12-16 02:47:56 +0000 | [diff] [blame] | 58 | return Matches.empty(); |
| 59 | } |
| 60 | |
Felix Berger | 3c8edde | 2016-03-29 02:42:38 +0000 | [diff] [blame] | 61 | } // namespace |
| 62 | |
Felix Berger | 7f88275 | 2016-07-01 20:12:15 +0000 | [diff] [blame] | 63 | UnnecessaryValueParamCheck::UnnecessaryValueParamCheck( |
| 64 | StringRef Name, ClangTidyContext *Context) |
| 65 | : ClangTidyCheck(Name, Context), |
| 66 | IncludeStyle(utils::IncludeSorter::parseIncludeStyle( |
| 67 | Options.get("IncludeStyle", "llvm"))) {} |
| 68 | |
Felix Berger | 3c8edde | 2016-03-29 02:42:38 +0000 | [diff] [blame] | 69 | void UnnecessaryValueParamCheck::registerMatchers(MatchFinder *Finder) { |
| 70 | const auto ExpensiveValueParamDecl = |
Alexander Kornienko | b215d47 | 2017-05-16 17:28:17 +0000 | [diff] [blame^] | 71 | parmVarDecl(hasType(hasCanonicalType(allOf( |
| 72 | unless(referenceType()), matchers::isExpensiveToCopy()))), |
Felix Berger | 3c8edde | 2016-03-29 02:42:38 +0000 | [diff] [blame] | 73 | decl().bind("param")); |
| 74 | Finder->addMatcher( |
Alexander Kornienko | b215d47 | 2017-05-16 17:28:17 +0000 | [diff] [blame^] | 75 | functionDecl(hasBody(stmt()), isDefinition(), unless(isImplicit()), |
Felix Berger | e4ab060 | 2016-12-02 14:44:16 +0000 | [diff] [blame] | 76 | unless(cxxMethodDecl(anyOf(isOverride(), isFinal()))), |
Felix Berger | 3c8edde | 2016-03-29 02:42:38 +0000 | [diff] [blame] | 77 | has(typeLoc(forEach(ExpensiveValueParamDecl))), |
Alexander Kornienko | b215d47 | 2017-05-16 17:28:17 +0000 | [diff] [blame^] | 78 | unless(isInstantiated()), decl().bind("functionDecl")), |
Felix Berger | 3c8edde | 2016-03-29 02:42:38 +0000 | [diff] [blame] | 79 | this); |
| 80 | } |
| 81 | |
| 82 | void UnnecessaryValueParamCheck::check(const MatchFinder::MatchResult &Result) { |
| 83 | const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>("param"); |
| 84 | const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("functionDecl"); |
| 85 | const size_t Index = std::find(Function->parameters().begin(), |
| 86 | Function->parameters().end(), Param) - |
| 87 | Function->parameters().begin(); |
| 88 | bool IsConstQualified = |
| 89 | Param->getType().getCanonicalType().isConstQualified(); |
| 90 | |
Felix Berger | 7f88275 | 2016-07-01 20:12:15 +0000 | [diff] [blame] | 91 | auto AllDeclRefExprs = utils::decl_ref_expr::allDeclRefExprs( |
Malcolm Parsons | cfa7d37 | 2017-01-03 12:10:44 +0000 | [diff] [blame] | 92 | *Param, *Function, *Result.Context); |
Felix Berger | 7f88275 | 2016-07-01 20:12:15 +0000 | [diff] [blame] | 93 | auto ConstDeclRefExprs = utils::decl_ref_expr::constReferenceDeclRefExprs( |
Malcolm Parsons | cfa7d37 | 2017-01-03 12:10:44 +0000 | [diff] [blame] | 94 | *Param, *Function, *Result.Context); |
| 95 | |
| 96 | // Do not trigger on non-const value parameters when they are not only used as |
| 97 | // const. |
Felix Berger | 7f88275 | 2016-07-01 20:12:15 +0000 | [diff] [blame] | 98 | if (!isSubset(AllDeclRefExprs, ConstDeclRefExprs)) |
| 99 | return; |
| 100 | |
| 101 | // If the parameter is non-const, check if it has a move constructor and is |
| 102 | // only referenced once to copy-construct another object or whether it has a |
| 103 | // move assignment operator and is only referenced once when copy-assigned. |
| 104 | // In this case wrap DeclRefExpr with std::move() to avoid the unnecessary |
| 105 | // copy. |
Malcolm Parsons | cfa7d37 | 2017-01-03 12:10:44 +0000 | [diff] [blame] | 106 | if (!IsConstQualified && AllDeclRefExprs.size() == 1) { |
Felix Berger | 7f88275 | 2016-07-01 20:12:15 +0000 | [diff] [blame] | 107 | auto CanonicalType = Param->getType().getCanonicalType(); |
Malcolm Parsons | cfa7d37 | 2017-01-03 12:10:44 +0000 | [diff] [blame] | 108 | const auto &DeclRefExpr = **AllDeclRefExprs.begin(); |
| 109 | |
| 110 | if (!hasLoopStmtAncestor(DeclRefExpr, *Function, *Result.Context) && |
Felix Berger | 7f88275 | 2016-07-01 20:12:15 +0000 | [diff] [blame] | 111 | ((utils::type_traits::hasNonTrivialMoveConstructor(CanonicalType) && |
| 112 | utils::decl_ref_expr::isCopyConstructorArgument( |
Malcolm Parsons | cfa7d37 | 2017-01-03 12:10:44 +0000 | [diff] [blame] | 113 | DeclRefExpr, *Function, *Result.Context)) || |
Felix Berger | 7f88275 | 2016-07-01 20:12:15 +0000 | [diff] [blame] | 114 | (utils::type_traits::hasNonTrivialMoveAssignment(CanonicalType) && |
| 115 | utils::decl_ref_expr::isCopyAssignmentArgument( |
Malcolm Parsons | cfa7d37 | 2017-01-03 12:10:44 +0000 | [diff] [blame] | 116 | DeclRefExpr, *Function, *Result.Context)))) { |
| 117 | handleMoveFix(*Param, DeclRefExpr, *Result.Context); |
Felix Berger | 7f88275 | 2016-07-01 20:12:15 +0000 | [diff] [blame] | 118 | return; |
| 119 | } |
| 120 | } |
| 121 | |
Felix Berger | 3c8edde | 2016-03-29 02:42:38 +0000 | [diff] [blame] | 122 | auto Diag = |
| 123 | diag(Param->getLocation(), |
| 124 | IsConstQualified ? "the const qualified parameter %0 is " |
| 125 | "copied for each invocation; consider " |
| 126 | "making it a reference" |
| 127 | : "the parameter %0 is copied for each " |
| 128 | "invocation but only used as a const reference; " |
| 129 | "consider making it a const reference") |
| 130 | << paramNameOrIndex(Param->getName(), Index); |
Felix Berger | 85f9e8b3 | 2016-11-10 01:28:22 +0000 | [diff] [blame] | 131 | // Do not propose fixes when: |
| 132 | // 1. the ParmVarDecl is in a macro, since we cannot place them correctly |
| 133 | // 2. the function is virtual as it might break overrides |
| 134 | // 3. the function is referenced outside of a call expression within the |
| 135 | // compilation unit as the signature change could introduce build errors. |
Felix Berger | 17934da | 2016-07-05 14:40:44 +0000 | [diff] [blame] | 136 | const auto *Method = llvm::dyn_cast<CXXMethodDecl>(Function); |
Felix Berger | 85f9e8b3 | 2016-11-10 01:28:22 +0000 | [diff] [blame] | 137 | if (Param->getLocStart().isMacroID() || (Method && Method->isVirtual()) || |
| 138 | isReferencedOutsideOfCallExpr(*Function, *Result.Context)) |
Felix Berger | 3c8edde | 2016-03-29 02:42:38 +0000 | [diff] [blame] | 139 | return; |
| 140 | for (const auto *FunctionDecl = Function; FunctionDecl != nullptr; |
| 141 | FunctionDecl = FunctionDecl->getPreviousDecl()) { |
| 142 | const auto &CurrentParam = *FunctionDecl->getParamDecl(Index); |
Etienne Bergeron | 2a4c00f | 2016-05-03 02:54:05 +0000 | [diff] [blame] | 143 | Diag << utils::fixit::changeVarDeclToReference(CurrentParam, |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 144 | *Result.Context); |
Felix Berger | 7c6d289 | 2016-11-04 20:51:31 +0000 | [diff] [blame] | 145 | // The parameter of each declaration needs to be checked individually as to |
| 146 | // whether it is const or not as constness can differ between definition and |
| 147 | // declaration. |
| 148 | if (!CurrentParam.getType().getCanonicalType().isConstQualified()) |
Etienne Bergeron | 2a4c00f | 2016-05-03 02:54:05 +0000 | [diff] [blame] | 149 | Diag << utils::fixit::changeVarDeclToConst(CurrentParam); |
Felix Berger | 3c8edde | 2016-03-29 02:42:38 +0000 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
Felix Berger | 7f88275 | 2016-07-01 20:12:15 +0000 | [diff] [blame] | 153 | void UnnecessaryValueParamCheck::registerPPCallbacks( |
| 154 | CompilerInstance &Compiler) { |
| 155 | Inserter.reset(new utils::IncludeInserter( |
| 156 | Compiler.getSourceManager(), Compiler.getLangOpts(), IncludeStyle)); |
| 157 | Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks()); |
| 158 | } |
| 159 | |
| 160 | void UnnecessaryValueParamCheck::storeOptions( |
| 161 | ClangTidyOptions::OptionMap &Opts) { |
| 162 | Options.store(Opts, "IncludeStyle", |
| 163 | utils::IncludeSorter::toString(IncludeStyle)); |
| 164 | } |
| 165 | |
| 166 | void UnnecessaryValueParamCheck::handleMoveFix(const ParmVarDecl &Var, |
| 167 | const DeclRefExpr &CopyArgument, |
| 168 | const ASTContext &Context) { |
| 169 | auto Diag = diag(CopyArgument.getLocStart(), |
| 170 | "parameter %0 is passed by value and only copied once; " |
| 171 | "consider moving it to avoid unnecessary copies") |
| 172 | << &Var; |
| 173 | // Do not propose fixes in macros since we cannot place them correctly. |
| 174 | if (CopyArgument.getLocStart().isMacroID()) |
| 175 | return; |
| 176 | const auto &SM = Context.getSourceManager(); |
Kirill Bobyrev | 11cea45 | 2016-08-01 12:06:18 +0000 | [diff] [blame] | 177 | auto EndLoc = Lexer::getLocForEndOfToken(CopyArgument.getLocation(), 0, SM, |
| 178 | Context.getLangOpts()); |
Felix Berger | 7f88275 | 2016-07-01 20:12:15 +0000 | [diff] [blame] | 179 | Diag << FixItHint::CreateInsertion(CopyArgument.getLocStart(), "std::move(") |
| 180 | << FixItHint::CreateInsertion(EndLoc, ")"); |
| 181 | if (auto IncludeFixit = Inserter->CreateIncludeInsertion( |
| 182 | SM.getFileID(CopyArgument.getLocStart()), "utility", |
| 183 | /*IsAngled=*/true)) |
| 184 | Diag << *IncludeFixit; |
| 185 | } |
| 186 | |
Felix Berger | 3c8edde | 2016-03-29 02:42:38 +0000 | [diff] [blame] | 187 | } // namespace performance |
| 188 | } // namespace tidy |
| 189 | } // namespace clang |