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 | 3c8edde | 2016-03-29 02:42:38 +0000 | [diff] [blame] | 42 | } // namespace |
| 43 | |
Felix Berger | 7f88275 | 2016-07-01 20:12:15 +0000 | [diff] [blame^] | 44 | UnnecessaryValueParamCheck::UnnecessaryValueParamCheck( |
| 45 | StringRef Name, ClangTidyContext *Context) |
| 46 | : ClangTidyCheck(Name, Context), |
| 47 | IncludeStyle(utils::IncludeSorter::parseIncludeStyle( |
| 48 | Options.get("IncludeStyle", "llvm"))) {} |
| 49 | |
Felix Berger | 3c8edde | 2016-03-29 02:42:38 +0000 | [diff] [blame] | 50 | void UnnecessaryValueParamCheck::registerMatchers(MatchFinder *Finder) { |
| 51 | const auto ExpensiveValueParamDecl = |
| 52 | parmVarDecl(hasType(hasCanonicalType(allOf(matchers::isExpensiveToCopy(), |
| 53 | unless(referenceType())))), |
| 54 | decl().bind("param")); |
| 55 | Finder->addMatcher( |
| 56 | functionDecl(isDefinition(), unless(cxxMethodDecl(isOverride())), |
| 57 | unless(isInstantiated()), |
| 58 | has(typeLoc(forEach(ExpensiveValueParamDecl))), |
| 59 | decl().bind("functionDecl")), |
| 60 | this); |
| 61 | } |
| 62 | |
| 63 | void UnnecessaryValueParamCheck::check(const MatchFinder::MatchResult &Result) { |
| 64 | const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>("param"); |
| 65 | const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("functionDecl"); |
| 66 | const size_t Index = std::find(Function->parameters().begin(), |
| 67 | Function->parameters().end(), Param) - |
| 68 | Function->parameters().begin(); |
| 69 | bool IsConstQualified = |
| 70 | Param->getType().getCanonicalType().isConstQualified(); |
| 71 | |
Etienne Bergeron | 2c82ebe | 2016-04-07 14:58:13 +0000 | [diff] [blame] | 72 | // Skip declarations delayed by late template parsing without a body. |
| 73 | if (!Function->getBody()) |
| 74 | return; |
| 75 | |
Felix Berger | 3c8edde | 2016-03-29 02:42:38 +0000 | [diff] [blame] | 76 | // Do not trigger on non-const value parameters when: |
| 77 | // 1. they are in a constructor definition since they can likely trigger |
| 78 | // misc-move-constructor-init which will suggest to move the argument. |
Felix Berger | 3c8edde | 2016-03-29 02:42:38 +0000 | [diff] [blame] | 79 | if (!IsConstQualified && (llvm::isa<CXXConstructorDecl>(Function) || |
Felix Berger | 7f88275 | 2016-07-01 20:12:15 +0000 | [diff] [blame^] | 80 | !Function->doesThisDeclarationHaveABody())) |
Felix Berger | 3c8edde | 2016-03-29 02:42:38 +0000 | [diff] [blame] | 81 | return; |
Felix Berger | 7f88275 | 2016-07-01 20:12:15 +0000 | [diff] [blame^] | 82 | |
| 83 | auto AllDeclRefExprs = utils::decl_ref_expr::allDeclRefExprs( |
| 84 | *Param, *Function->getBody(), *Result.Context); |
| 85 | auto ConstDeclRefExprs = utils::decl_ref_expr::constReferenceDeclRefExprs( |
| 86 | *Param, *Function->getBody(), *Result.Context); |
| 87 | // 2. they are not only used as const. |
| 88 | if (!isSubset(AllDeclRefExprs, ConstDeclRefExprs)) |
| 89 | return; |
| 90 | |
| 91 | // If the parameter is non-const, check if it has a move constructor and is |
| 92 | // only referenced once to copy-construct another object or whether it has a |
| 93 | // move assignment operator and is only referenced once when copy-assigned. |
| 94 | // In this case wrap DeclRefExpr with std::move() to avoid the unnecessary |
| 95 | // copy. |
| 96 | if (!IsConstQualified) { |
| 97 | auto CanonicalType = Param->getType().getCanonicalType(); |
| 98 | if (AllDeclRefExprs.size() == 1 && |
| 99 | ((utils::type_traits::hasNonTrivialMoveConstructor(CanonicalType) && |
| 100 | utils::decl_ref_expr::isCopyConstructorArgument( |
| 101 | **AllDeclRefExprs.begin(), *Function->getBody(), |
| 102 | *Result.Context)) || |
| 103 | (utils::type_traits::hasNonTrivialMoveAssignment(CanonicalType) && |
| 104 | utils::decl_ref_expr::isCopyAssignmentArgument( |
| 105 | **AllDeclRefExprs.begin(), *Function->getBody(), |
| 106 | *Result.Context)))) { |
| 107 | handleMoveFix(*Param, **AllDeclRefExprs.begin(), *Result.Context); |
| 108 | return; |
| 109 | } |
| 110 | } |
| 111 | |
Felix Berger | 3c8edde | 2016-03-29 02:42:38 +0000 | [diff] [blame] | 112 | auto Diag = |
| 113 | diag(Param->getLocation(), |
| 114 | IsConstQualified ? "the const qualified parameter %0 is " |
| 115 | "copied for each invocation; consider " |
| 116 | "making it a reference" |
| 117 | : "the parameter %0 is copied for each " |
| 118 | "invocation but only used as a const reference; " |
| 119 | "consider making it a const reference") |
| 120 | << paramNameOrIndex(Param->getName(), Index); |
| 121 | // Do not propose fixes in macros since we cannot place them correctly. |
| 122 | if (Param->getLocStart().isMacroID()) |
| 123 | return; |
| 124 | for (const auto *FunctionDecl = Function; FunctionDecl != nullptr; |
| 125 | FunctionDecl = FunctionDecl->getPreviousDecl()) { |
| 126 | const auto &CurrentParam = *FunctionDecl->getParamDecl(Index); |
Etienne Bergeron | 2a4c00f | 2016-05-03 02:54:05 +0000 | [diff] [blame] | 127 | Diag << utils::fixit::changeVarDeclToReference(CurrentParam, |
Felix Berger | 3c8edde | 2016-03-29 02:42:38 +0000 | [diff] [blame] | 128 | *Result.Context); |
| 129 | if (!IsConstQualified) |
Etienne Bergeron | 2a4c00f | 2016-05-03 02:54:05 +0000 | [diff] [blame] | 130 | Diag << utils::fixit::changeVarDeclToConst(CurrentParam); |
Felix Berger | 3c8edde | 2016-03-29 02:42:38 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
Felix Berger | 7f88275 | 2016-07-01 20:12:15 +0000 | [diff] [blame^] | 134 | void UnnecessaryValueParamCheck::registerPPCallbacks( |
| 135 | CompilerInstance &Compiler) { |
| 136 | Inserter.reset(new utils::IncludeInserter( |
| 137 | Compiler.getSourceManager(), Compiler.getLangOpts(), IncludeStyle)); |
| 138 | Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks()); |
| 139 | } |
| 140 | |
| 141 | void UnnecessaryValueParamCheck::storeOptions( |
| 142 | ClangTidyOptions::OptionMap &Opts) { |
| 143 | Options.store(Opts, "IncludeStyle", |
| 144 | utils::IncludeSorter::toString(IncludeStyle)); |
| 145 | } |
| 146 | |
| 147 | void UnnecessaryValueParamCheck::handleMoveFix(const ParmVarDecl &Var, |
| 148 | const DeclRefExpr &CopyArgument, |
| 149 | const ASTContext &Context) { |
| 150 | auto Diag = diag(CopyArgument.getLocStart(), |
| 151 | "parameter %0 is passed by value and only copied once; " |
| 152 | "consider moving it to avoid unnecessary copies") |
| 153 | << &Var; |
| 154 | // Do not propose fixes in macros since we cannot place them correctly. |
| 155 | if (CopyArgument.getLocStart().isMacroID()) |
| 156 | return; |
| 157 | const auto &SM = Context.getSourceManager(); |
| 158 | auto EndLoc = Lexer::getLocForEndOfToken(CopyArgument.getLocation(), 0, SM, |
| 159 | Context.getLangOpts()); |
| 160 | Diag << FixItHint::CreateInsertion(CopyArgument.getLocStart(), "std::move(") |
| 161 | << FixItHint::CreateInsertion(EndLoc, ")"); |
| 162 | if (auto IncludeFixit = Inserter->CreateIncludeInsertion( |
| 163 | SM.getFileID(CopyArgument.getLocStart()), "utility", |
| 164 | /*IsAngled=*/true)) |
| 165 | Diag << *IncludeFixit; |
| 166 | } |
| 167 | |
Felix Berger | 3c8edde | 2016-03-29 02:42:38 +0000 | [diff] [blame] | 168 | } // namespace performance |
| 169 | } // namespace tidy |
| 170 | } // namespace clang |