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