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