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