Alexander Kornienko | b959f4c | 2015-12-30 10:24:40 +0000 | [diff] [blame] | 1 | //===--- UnnecessaryCopyInitialization.cpp - clang-tidy--------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Alexander Kornienko | b959f4c | 2015-12-30 10:24:40 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "UnnecessaryCopyInitialization.h" |
| 10 | |
Felix Berger | adfdc14 | 2016-03-05 21:17:58 +0000 | [diff] [blame] | 11 | #include "../utils/DeclRefExprUtils.h" |
| 12 | #include "../utils/FixItHintUtils.h" |
Alexander Kornienko | b959f4c | 2015-12-30 10:24:40 +0000 | [diff] [blame] | 13 | #include "../utils/Matchers.h" |
Adam Balogh | abd72e9 | 2018-10-12 13:05:21 +0000 | [diff] [blame] | 14 | #include "../utils/OptionsUtils.h" |
Alexander Kornienko | b959f4c | 2015-12-30 10:24:40 +0000 | [diff] [blame] | 15 | |
| 16 | namespace clang { |
| 17 | namespace tidy { |
| 18 | namespace performance { |
Haojian Wu | 6ccb1dd | 2016-03-23 09:33:07 +0000 | [diff] [blame] | 19 | namespace { |
| 20 | |
| 21 | void recordFixes(const VarDecl &Var, ASTContext &Context, |
| 22 | DiagnosticBuilder &Diagnostic) { |
Etienne Bergeron | 2a4c00f | 2016-05-03 02:54:05 +0000 | [diff] [blame] | 23 | Diagnostic << utils::fixit::changeVarDeclToReference(Var, Context); |
Haojian Wu | 6ccb1dd | 2016-03-23 09:33:07 +0000 | [diff] [blame] | 24 | if (!Var.getType().isLocalConstQualified()) |
Etienne Bergeron | 2a4c00f | 2016-05-03 02:54:05 +0000 | [diff] [blame] | 25 | Diagnostic << utils::fixit::changeVarDeclToConst(Var); |
Haojian Wu | 6ccb1dd | 2016-03-23 09:33:07 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | } // namespace |
| 29 | |
Alexander Kornienko | b959f4c | 2015-12-30 10:24:40 +0000 | [diff] [blame] | 30 | using namespace ::clang::ast_matchers; |
Etienne Bergeron | 2a4c00f | 2016-05-03 02:54:05 +0000 | [diff] [blame] | 31 | using utils::decl_ref_expr::isOnlyUsedAsConst; |
Alexander Kornienko | b959f4c | 2015-12-30 10:24:40 +0000 | [diff] [blame] | 32 | |
Adam Balogh | abd72e9 | 2018-10-12 13:05:21 +0000 | [diff] [blame] | 33 | UnnecessaryCopyInitialization::UnnecessaryCopyInitialization( |
| 34 | StringRef Name, ClangTidyContext *Context) |
| 35 | : ClangTidyCheck(Name, Context), |
| 36 | AllowedTypes( |
| 37 | utils::options::parseStringList(Options.get("AllowedTypes", ""))) {} |
| 38 | |
Haojian Wu | 6ccb1dd | 2016-03-23 09:33:07 +0000 | [diff] [blame] | 39 | void UnnecessaryCopyInitialization::registerMatchers(MatchFinder *Finder) { |
Alexander Kornienko | b959f4c | 2015-12-30 10:24:40 +0000 | [diff] [blame] | 40 | auto ConstReference = referenceType(pointee(qualType(isConstQualified()))); |
Haojian Wu | 6ccb1dd | 2016-03-23 09:33:07 +0000 | [diff] [blame] | 41 | |
Felix Berger | 98e4019 | 2016-05-31 00:25:57 +0000 | [diff] [blame] | 42 | // Match method call expressions where the `this` argument is only used as |
| 43 | // const, this will be checked in `check()` part. This returned const |
| 44 | // reference is highly likely to outlive the local const reference of the |
| 45 | // variable being declared. The assumption is that the const reference being |
| 46 | // returned either points to a global static variable or to a member of the |
| 47 | // called object. |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 48 | auto ConstRefReturningMethodCall = |
| 49 | cxxMemberCallExpr(callee(cxxMethodDecl(returns(ConstReference))), |
| 50 | on(declRefExpr(to(varDecl().bind("objectArg"))))); |
Alexander Kornienko | b959f4c | 2015-12-30 10:24:40 +0000 | [diff] [blame] | 51 | auto ConstRefReturningFunctionCall = |
| 52 | callExpr(callee(functionDecl(returns(ConstReference))), |
| 53 | unless(callee(cxxMethodDecl()))); |
Haojian Wu | 6ccb1dd | 2016-03-23 09:33:07 +0000 | [diff] [blame] | 54 | |
Adam Balogh | abd72e9 | 2018-10-12 13:05:21 +0000 | [diff] [blame] | 55 | auto localVarCopiedFrom = [this](const internal::Matcher<Expr> &CopyCtorArg) { |
Haojian Wu | 6ccb1dd | 2016-03-23 09:33:07 +0000 | [diff] [blame] | 56 | return compoundStmt( |
| 57 | forEachDescendant( |
Felix Berger | 6d3d746 | 2016-05-13 02:47:56 +0000 | [diff] [blame] | 58 | declStmt( |
| 59 | has(varDecl(hasLocalStorage(), |
Adam Balogh | abd72e9 | 2018-10-12 13:05:21 +0000 | [diff] [blame] | 60 | hasType(qualType( |
Alexander Kornienko | 976e0c0 | 2018-11-25 02:41:01 +0000 | [diff] [blame] | 61 | hasCanonicalType( |
| 62 | matchers::isExpensiveToCopy()), |
| 63 | unless(hasDeclaration(namedDecl( |
| 64 | matchers::matchesAnyListedName( |
| 65 | AllowedTypes)))))), |
Haojian Wu | 06e39a3 | 2016-11-08 00:45:34 +0000 | [diff] [blame] | 66 | unless(isImplicit()), |
Felix Berger | 6d3d746 | 2016-05-13 02:47:56 +0000 | [diff] [blame] | 67 | hasInitializer( |
| 68 | cxxConstructExpr( |
| 69 | hasDeclaration(cxxConstructorDecl( |
| 70 | isCopyConstructor())), |
| 71 | hasArgument(0, CopyCtorArg)) |
| 72 | .bind("ctorCall"))) |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 73 | .bind("newVarDecl"))) |
| 74 | .bind("declStmt"))) |
Haojian Wu | 6ccb1dd | 2016-03-23 09:33:07 +0000 | [diff] [blame] | 75 | .bind("blockStmt"); |
| 76 | }; |
| 77 | |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 78 | Finder->addMatcher(localVarCopiedFrom(anyOf(ConstRefReturningFunctionCall, |
| 79 | ConstRefReturningMethodCall)), |
| 80 | this); |
Haojian Wu | 6ccb1dd | 2016-03-23 09:33:07 +0000 | [diff] [blame] | 81 | |
| 82 | Finder->addMatcher(localVarCopiedFrom(declRefExpr( |
| 83 | to(varDecl(hasLocalStorage()).bind("oldVarDecl")))), |
| 84 | this); |
Alexander Kornienko | b959f4c | 2015-12-30 10:24:40 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void UnnecessaryCopyInitialization::check( |
Haojian Wu | 6ccb1dd | 2016-03-23 09:33:07 +0000 | [diff] [blame] | 88 | const MatchFinder::MatchResult &Result) { |
| 89 | const auto *NewVar = Result.Nodes.getNodeAs<VarDecl>("newVarDecl"); |
| 90 | const auto *OldVar = Result.Nodes.getNodeAs<VarDecl>("oldVarDecl"); |
Felix Berger | 98e4019 | 2016-05-31 00:25:57 +0000 | [diff] [blame] | 91 | const auto *ObjectArg = Result.Nodes.getNodeAs<VarDecl>("objectArg"); |
Felix Berger | adfdc14 | 2016-03-05 21:17:58 +0000 | [diff] [blame] | 92 | const auto *BlockStmt = Result.Nodes.getNodeAs<Stmt>("blockStmt"); |
Haojian Wu | 6ccb1dd | 2016-03-23 09:33:07 +0000 | [diff] [blame] | 93 | const auto *CtorCall = Result.Nodes.getNodeAs<CXXConstructExpr>("ctorCall"); |
Adam Balogh | abd72e9 | 2018-10-12 13:05:21 +0000 | [diff] [blame] | 94 | |
Felix Berger | 6d3d746 | 2016-05-13 02:47:56 +0000 | [diff] [blame] | 95 | // Do not propose fixes if the DeclStmt has multiple VarDecls or in macros |
| 96 | // since we cannot place them correctly. |
| 97 | bool IssueFix = |
| 98 | Result.Nodes.getNodeAs<DeclStmt>("declStmt")->isSingleDecl() && |
| 99 | !NewVar->getLocation().isMacroID(); |
Haojian Wu | 6ccb1dd | 2016-03-23 09:33:07 +0000 | [diff] [blame] | 100 | |
| 101 | // A constructor that looks like T(const T& t, bool arg = false) counts as a |
| 102 | // copy only when it is called with default arguments for the arguments after |
| 103 | // the first. |
| 104 | for (unsigned int i = 1; i < CtorCall->getNumArgs(); ++i) |
| 105 | if (!CtorCall->getArg(i)->isDefaultArgument()) |
| 106 | return; |
| 107 | |
| 108 | if (OldVar == nullptr) { |
Felix Berger | 98e4019 | 2016-05-31 00:25:57 +0000 | [diff] [blame] | 109 | handleCopyFromMethodReturn(*NewVar, *BlockStmt, IssueFix, ObjectArg, |
| 110 | *Result.Context); |
Haojian Wu | 6ccb1dd | 2016-03-23 09:33:07 +0000 | [diff] [blame] | 111 | } else { |
Felix Berger | 6d3d746 | 2016-05-13 02:47:56 +0000 | [diff] [blame] | 112 | handleCopyFromLocalVar(*NewVar, *OldVar, *BlockStmt, IssueFix, |
| 113 | *Result.Context); |
Haojian Wu | 6ccb1dd | 2016-03-23 09:33:07 +0000 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
| 117 | void UnnecessaryCopyInitialization::handleCopyFromMethodReturn( |
Felix Berger | 6d3d746 | 2016-05-13 02:47:56 +0000 | [diff] [blame] | 118 | const VarDecl &Var, const Stmt &BlockStmt, bool IssueFix, |
Felix Berger | 98e4019 | 2016-05-31 00:25:57 +0000 | [diff] [blame] | 119 | const VarDecl *ObjectArg, ASTContext &Context) { |
Haojian Wu | 6ccb1dd | 2016-03-23 09:33:07 +0000 | [diff] [blame] | 120 | bool IsConstQualified = Var.getType().isConstQualified(); |
| 121 | if (!IsConstQualified && !isOnlyUsedAsConst(Var, BlockStmt, Context)) |
Felix Berger | adfdc14 | 2016-03-05 21:17:58 +0000 | [diff] [blame] | 122 | return; |
Felix Berger | 98e4019 | 2016-05-31 00:25:57 +0000 | [diff] [blame] | 123 | if (ObjectArg != nullptr && |
| 124 | !isOnlyUsedAsConst(*ObjectArg, BlockStmt, Context)) |
| 125 | return; |
Haojian Wu | 6ccb1dd | 2016-03-23 09:33:07 +0000 | [diff] [blame] | 126 | |
| 127 | auto Diagnostic = |
| 128 | diag(Var.getLocation(), |
| 129 | IsConstQualified ? "the const qualified variable %0 is " |
Felix Berger | adfdc14 | 2016-03-05 21:17:58 +0000 | [diff] [blame] | 130 | "copy-constructed from a const reference; " |
| 131 | "consider making it a const reference" |
Haojian Wu | 6ccb1dd | 2016-03-23 09:33:07 +0000 | [diff] [blame] | 132 | : "the variable %0 is copy-constructed from a " |
Felix Berger | adfdc14 | 2016-03-05 21:17:58 +0000 | [diff] [blame] | 133 | "const reference but is only used as const " |
| 134 | "reference; consider making it a const reference") |
Haojian Wu | 6ccb1dd | 2016-03-23 09:33:07 +0000 | [diff] [blame] | 135 | << &Var; |
Felix Berger | 6d3d746 | 2016-05-13 02:47:56 +0000 | [diff] [blame] | 136 | if (IssueFix) |
| 137 | recordFixes(Var, Context, Diagnostic); |
Haojian Wu | 6ccb1dd | 2016-03-23 09:33:07 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void UnnecessaryCopyInitialization::handleCopyFromLocalVar( |
| 141 | const VarDecl &NewVar, const VarDecl &OldVar, const Stmt &BlockStmt, |
Felix Berger | 6d3d746 | 2016-05-13 02:47:56 +0000 | [diff] [blame] | 142 | bool IssueFix, ASTContext &Context) { |
Haojian Wu | 6ccb1dd | 2016-03-23 09:33:07 +0000 | [diff] [blame] | 143 | if (!isOnlyUsedAsConst(NewVar, BlockStmt, Context) || |
| 144 | !isOnlyUsedAsConst(OldVar, BlockStmt, Context)) |
Felix Berger | adfdc14 | 2016-03-05 21:17:58 +0000 | [diff] [blame] | 145 | return; |
Haojian Wu | 6ccb1dd | 2016-03-23 09:33:07 +0000 | [diff] [blame] | 146 | |
| 147 | auto Diagnostic = diag(NewVar.getLocation(), |
| 148 | "local copy %0 of the variable %1 is never modified; " |
| 149 | "consider avoiding the copy") |
| 150 | << &NewVar << &OldVar; |
Felix Berger | 6d3d746 | 2016-05-13 02:47:56 +0000 | [diff] [blame] | 151 | if (IssueFix) |
| 152 | recordFixes(NewVar, Context, Diagnostic); |
Alexander Kornienko | b959f4c | 2015-12-30 10:24:40 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Adam Balogh | abd72e9 | 2018-10-12 13:05:21 +0000 | [diff] [blame] | 155 | void UnnecessaryCopyInitialization::storeOptions( |
| 156 | ClangTidyOptions::OptionMap &Opts) { |
| 157 | Options.store(Opts, "AllowedTypes", |
| 158 | utils::options::serializeStringList(AllowedTypes)); |
| 159 | } |
| 160 | |
Alexander Kornienko | b959f4c | 2015-12-30 10:24:40 +0000 | [diff] [blame] | 161 | } // namespace performance |
| 162 | } // namespace tidy |
| 163 | } // namespace clang |