blob: dfaff827dd6a85610db566361b033e8b5371f2ef [file] [log] [blame]
Alexander Kornienkob959f4c2015-12-30 10:24:40 +00001//===--- 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 Bergeradfdc142016-03-05 21:17:58 +000012#include "../utils/DeclRefExprUtils.h"
13#include "../utils/FixItHintUtils.h"
Alexander Kornienkob959f4c2015-12-30 10:24:40 +000014#include "../utils/Matchers.h"
15
16namespace clang {
17namespace tidy {
18namespace performance {
Haojian Wu6ccb1dd2016-03-23 09:33:07 +000019namespace {
20
21void recordFixes(const VarDecl &Var, ASTContext &Context,
22 DiagnosticBuilder &Diagnostic) {
Etienne Bergeron2a4c00f2016-05-03 02:54:05 +000023 Diagnostic << utils::fixit::changeVarDeclToReference(Var, Context);
Haojian Wu6ccb1dd2016-03-23 09:33:07 +000024 if (!Var.getType().isLocalConstQualified())
Etienne Bergeron2a4c00f2016-05-03 02:54:05 +000025 Diagnostic << utils::fixit::changeVarDeclToConst(Var);
Haojian Wu6ccb1dd2016-03-23 09:33:07 +000026}
27
28} // namespace
29
Alexander Kornienkob959f4c2015-12-30 10:24:40 +000030
31using namespace ::clang::ast_matchers;
Etienne Bergeron2a4c00f2016-05-03 02:54:05 +000032using utils::decl_ref_expr::isOnlyUsedAsConst;
Alexander Kornienkob959f4c2015-12-30 10:24:40 +000033
Haojian Wu6ccb1dd2016-03-23 09:33:07 +000034void UnnecessaryCopyInitialization::registerMatchers(MatchFinder *Finder) {
Alexander Kornienkob959f4c2015-12-30 10:24:40 +000035 auto ConstReference = referenceType(pointee(qualType(isConstQualified())));
36 auto ConstOrConstReference =
37 allOf(anyOf(ConstReference, isConstQualified()),
Felix Bergeradfdc142016-03-05 21:17:58 +000038 unless(allOf(pointerType(), unless(pointerType(pointee(
39 qualType(isConstQualified())))))));
Haojian Wu6ccb1dd2016-03-23 09:33:07 +000040
Alexander Kornienkob959f4c2015-12-30 10:24:40 +000041 // Match method call expressions where the this argument is a const
42 // type or const reference. This returned const reference is highly likely to
43 // outlive the local const reference of the variable being declared.
44 // The assumption is that the const reference being returned either points
45 // to a global static variable or to a member of the called object.
46 auto ConstRefReturningMethodCallOfConstParam = cxxMemberCallExpr(
47 callee(cxxMethodDecl(returns(ConstReference))),
48 on(declRefExpr(to(varDecl(hasType(qualType(ConstOrConstReference)))))));
49 auto ConstRefReturningFunctionCall =
50 callExpr(callee(functionDecl(returns(ConstReference))),
51 unless(callee(cxxMethodDecl())));
Haojian Wu6ccb1dd2016-03-23 09:33:07 +000052
53 auto localVarCopiedFrom = [](const internal::Matcher<Expr> &CopyCtorArg) {
54 return compoundStmt(
55 forEachDescendant(
Felix Berger6d3d7462016-05-13 02:47:56 +000056 declStmt(
57 has(varDecl(hasLocalStorage(),
58 hasType(matchers::isExpensiveToCopy()),
59 hasInitializer(
60 cxxConstructExpr(
61 hasDeclaration(cxxConstructorDecl(
62 isCopyConstructor())),
63 hasArgument(0, CopyCtorArg))
64 .bind("ctorCall")))
65 .bind("newVarDecl"))).bind("declStmt")))
Haojian Wu6ccb1dd2016-03-23 09:33:07 +000066 .bind("blockStmt");
67 };
68
Alexander Kornienkob959f4c2015-12-30 10:24:40 +000069 Finder->addMatcher(
Haojian Wu6ccb1dd2016-03-23 09:33:07 +000070 localVarCopiedFrom(anyOf(ConstRefReturningFunctionCall,
71 ConstRefReturningMethodCallOfConstParam)),
Alexander Kornienkob959f4c2015-12-30 10:24:40 +000072 this);
Haojian Wu6ccb1dd2016-03-23 09:33:07 +000073
74 Finder->addMatcher(localVarCopiedFrom(declRefExpr(
75 to(varDecl(hasLocalStorage()).bind("oldVarDecl")))),
76 this);
Alexander Kornienkob959f4c2015-12-30 10:24:40 +000077}
78
79void UnnecessaryCopyInitialization::check(
Haojian Wu6ccb1dd2016-03-23 09:33:07 +000080 const MatchFinder::MatchResult &Result) {
81 const auto *NewVar = Result.Nodes.getNodeAs<VarDecl>("newVarDecl");
82 const auto *OldVar = Result.Nodes.getNodeAs<VarDecl>("oldVarDecl");
Felix Bergeradfdc142016-03-05 21:17:58 +000083 const auto *BlockStmt = Result.Nodes.getNodeAs<Stmt>("blockStmt");
Haojian Wu6ccb1dd2016-03-23 09:33:07 +000084 const auto *CtorCall = Result.Nodes.getNodeAs<CXXConstructExpr>("ctorCall");
Felix Berger6d3d7462016-05-13 02:47:56 +000085 // Do not propose fixes if the DeclStmt has multiple VarDecls or in macros
86 // since we cannot place them correctly.
87 bool IssueFix =
88 Result.Nodes.getNodeAs<DeclStmt>("declStmt")->isSingleDecl() &&
89 !NewVar->getLocation().isMacroID();
Haojian Wu6ccb1dd2016-03-23 09:33:07 +000090
91 // A constructor that looks like T(const T& t, bool arg = false) counts as a
92 // copy only when it is called with default arguments for the arguments after
93 // the first.
94 for (unsigned int i = 1; i < CtorCall->getNumArgs(); ++i)
95 if (!CtorCall->getArg(i)->isDefaultArgument())
96 return;
97
98 if (OldVar == nullptr) {
Felix Berger6d3d7462016-05-13 02:47:56 +000099 handleCopyFromMethodReturn(*NewVar, *BlockStmt, IssueFix, *Result.Context);
Haojian Wu6ccb1dd2016-03-23 09:33:07 +0000100 } else {
Felix Berger6d3d7462016-05-13 02:47:56 +0000101 handleCopyFromLocalVar(*NewVar, *OldVar, *BlockStmt, IssueFix,
102 *Result.Context);
Haojian Wu6ccb1dd2016-03-23 09:33:07 +0000103 }
104}
105
106void UnnecessaryCopyInitialization::handleCopyFromMethodReturn(
Felix Berger6d3d7462016-05-13 02:47:56 +0000107 const VarDecl &Var, const Stmt &BlockStmt, bool IssueFix,
108 ASTContext &Context) {
Haojian Wu6ccb1dd2016-03-23 09:33:07 +0000109 bool IsConstQualified = Var.getType().isConstQualified();
110 if (!IsConstQualified && !isOnlyUsedAsConst(Var, BlockStmt, Context))
Felix Bergeradfdc142016-03-05 21:17:58 +0000111 return;
Haojian Wu6ccb1dd2016-03-23 09:33:07 +0000112
113 auto Diagnostic =
114 diag(Var.getLocation(),
115 IsConstQualified ? "the const qualified variable %0 is "
Felix Bergeradfdc142016-03-05 21:17:58 +0000116 "copy-constructed from a const reference; "
117 "consider making it a const reference"
Haojian Wu6ccb1dd2016-03-23 09:33:07 +0000118 : "the variable %0 is copy-constructed from a "
Felix Bergeradfdc142016-03-05 21:17:58 +0000119 "const reference but is only used as const "
120 "reference; consider making it a const reference")
Haojian Wu6ccb1dd2016-03-23 09:33:07 +0000121 << &Var;
Felix Berger6d3d7462016-05-13 02:47:56 +0000122 if (IssueFix)
123 recordFixes(Var, Context, Diagnostic);
Haojian Wu6ccb1dd2016-03-23 09:33:07 +0000124}
125
126void UnnecessaryCopyInitialization::handleCopyFromLocalVar(
127 const VarDecl &NewVar, const VarDecl &OldVar, const Stmt &BlockStmt,
Felix Berger6d3d7462016-05-13 02:47:56 +0000128 bool IssueFix, ASTContext &Context) {
Haojian Wu6ccb1dd2016-03-23 09:33:07 +0000129 if (!isOnlyUsedAsConst(NewVar, BlockStmt, Context) ||
130 !isOnlyUsedAsConst(OldVar, BlockStmt, Context))
Felix Bergeradfdc142016-03-05 21:17:58 +0000131 return;
Haojian Wu6ccb1dd2016-03-23 09:33:07 +0000132
133 auto Diagnostic = diag(NewVar.getLocation(),
134 "local copy %0 of the variable %1 is never modified; "
135 "consider avoiding the copy")
136 << &NewVar << &OldVar;
Felix Berger6d3d7462016-05-13 02:47:56 +0000137 if (IssueFix)
138 recordFixes(NewVar, Context, Diagnostic);
Alexander Kornienkob959f4c2015-12-30 10:24:40 +0000139}
140
141} // namespace performance
142} // namespace tidy
143} // namespace clang