blob: 66773a65f4ce38d0a1402d3f6c1ff7c6bdd67c01 [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
Felix Berger98e40192016-05-31 00:25:57 +000041 // 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 Kornienkob959f4c2015-12-30 10:24:40 +000048 callee(cxxMethodDecl(returns(ConstReference))),
Felix Berger98e40192016-05-31 00:25:57 +000049 on(declRefExpr(to(varDecl().bind("objectArg")))));
Alexander Kornienkob959f4c2015-12-30 10:24:40 +000050 auto ConstRefReturningFunctionCall =
51 callExpr(callee(functionDecl(returns(ConstReference))),
52 unless(callee(cxxMethodDecl())));
Haojian Wu6ccb1dd2016-03-23 09:33:07 +000053
54 auto localVarCopiedFrom = [](const internal::Matcher<Expr> &CopyCtorArg) {
55 return compoundStmt(
56 forEachDescendant(
Felix Berger6d3d7462016-05-13 02:47:56 +000057 declStmt(
58 has(varDecl(hasLocalStorage(),
59 hasType(matchers::isExpensiveToCopy()),
Haojian Wu06e39a32016-11-08 00:45:34 +000060 unless(isImplicit()),
Felix Berger6d3d7462016-05-13 02:47:56 +000061 hasInitializer(
62 cxxConstructExpr(
63 hasDeclaration(cxxConstructorDecl(
64 isCopyConstructor())),
65 hasArgument(0, CopyCtorArg))
66 .bind("ctorCall")))
67 .bind("newVarDecl"))).bind("declStmt")))
Haojian Wu6ccb1dd2016-03-23 09:33:07 +000068 .bind("blockStmt");
69 };
70
Alexander Kornienkob959f4c2015-12-30 10:24:40 +000071 Finder->addMatcher(
Haojian Wu6ccb1dd2016-03-23 09:33:07 +000072 localVarCopiedFrom(anyOf(ConstRefReturningFunctionCall,
Felix Berger98e40192016-05-31 00:25:57 +000073 ConstRefReturningMethodCall)),
Alexander Kornienkob959f4c2015-12-30 10:24:40 +000074 this);
Haojian Wu6ccb1dd2016-03-23 09:33:07 +000075
76 Finder->addMatcher(localVarCopiedFrom(declRefExpr(
77 to(varDecl(hasLocalStorage()).bind("oldVarDecl")))),
78 this);
Alexander Kornienkob959f4c2015-12-30 10:24:40 +000079}
80
81void UnnecessaryCopyInitialization::check(
Haojian Wu6ccb1dd2016-03-23 09:33:07 +000082 const MatchFinder::MatchResult &Result) {
83 const auto *NewVar = Result.Nodes.getNodeAs<VarDecl>("newVarDecl");
84 const auto *OldVar = Result.Nodes.getNodeAs<VarDecl>("oldVarDecl");
Felix Berger98e40192016-05-31 00:25:57 +000085 const auto *ObjectArg = Result.Nodes.getNodeAs<VarDecl>("objectArg");
Felix Bergeradfdc142016-03-05 21:17:58 +000086 const auto *BlockStmt = Result.Nodes.getNodeAs<Stmt>("blockStmt");
Haojian Wu6ccb1dd2016-03-23 09:33:07 +000087 const auto *CtorCall = Result.Nodes.getNodeAs<CXXConstructExpr>("ctorCall");
Felix Berger6d3d7462016-05-13 02:47:56 +000088 // Do not propose fixes if the DeclStmt has multiple VarDecls or in macros
89 // since we cannot place them correctly.
90 bool IssueFix =
91 Result.Nodes.getNodeAs<DeclStmt>("declStmt")->isSingleDecl() &&
92 !NewVar->getLocation().isMacroID();
Haojian Wu6ccb1dd2016-03-23 09:33:07 +000093
94 // A constructor that looks like T(const T& t, bool arg = false) counts as a
95 // copy only when it is called with default arguments for the arguments after
96 // the first.
97 for (unsigned int i = 1; i < CtorCall->getNumArgs(); ++i)
98 if (!CtorCall->getArg(i)->isDefaultArgument())
99 return;
100
101 if (OldVar == nullptr) {
Felix Berger98e40192016-05-31 00:25:57 +0000102 handleCopyFromMethodReturn(*NewVar, *BlockStmt, IssueFix, ObjectArg,
103 *Result.Context);
Haojian Wu6ccb1dd2016-03-23 09:33:07 +0000104 } else {
Felix Berger6d3d7462016-05-13 02:47:56 +0000105 handleCopyFromLocalVar(*NewVar, *OldVar, *BlockStmt, IssueFix,
106 *Result.Context);
Haojian Wu6ccb1dd2016-03-23 09:33:07 +0000107 }
108}
109
110void UnnecessaryCopyInitialization::handleCopyFromMethodReturn(
Felix Berger6d3d7462016-05-13 02:47:56 +0000111 const VarDecl &Var, const Stmt &BlockStmt, bool IssueFix,
Felix Berger98e40192016-05-31 00:25:57 +0000112 const VarDecl *ObjectArg, ASTContext &Context) {
Haojian Wu6ccb1dd2016-03-23 09:33:07 +0000113 bool IsConstQualified = Var.getType().isConstQualified();
114 if (!IsConstQualified && !isOnlyUsedAsConst(Var, BlockStmt, Context))
Felix Bergeradfdc142016-03-05 21:17:58 +0000115 return;
Felix Berger98e40192016-05-31 00:25:57 +0000116 if (ObjectArg != nullptr &&
117 !isOnlyUsedAsConst(*ObjectArg, BlockStmt, Context))
118 return;
Haojian Wu6ccb1dd2016-03-23 09:33:07 +0000119
120 auto Diagnostic =
121 diag(Var.getLocation(),
122 IsConstQualified ? "the const qualified variable %0 is "
Felix Bergeradfdc142016-03-05 21:17:58 +0000123 "copy-constructed from a const reference; "
124 "consider making it a const reference"
Haojian Wu6ccb1dd2016-03-23 09:33:07 +0000125 : "the variable %0 is copy-constructed from a "
Felix Bergeradfdc142016-03-05 21:17:58 +0000126 "const reference but is only used as const "
127 "reference; consider making it a const reference")
Haojian Wu6ccb1dd2016-03-23 09:33:07 +0000128 << &Var;
Felix Berger6d3d7462016-05-13 02:47:56 +0000129 if (IssueFix)
130 recordFixes(Var, Context, Diagnostic);
Haojian Wu6ccb1dd2016-03-23 09:33:07 +0000131}
132
133void UnnecessaryCopyInitialization::handleCopyFromLocalVar(
134 const VarDecl &NewVar, const VarDecl &OldVar, const Stmt &BlockStmt,
Felix Berger6d3d7462016-05-13 02:47:56 +0000135 bool IssueFix, ASTContext &Context) {
Haojian Wu6ccb1dd2016-03-23 09:33:07 +0000136 if (!isOnlyUsedAsConst(NewVar, BlockStmt, Context) ||
137 !isOnlyUsedAsConst(OldVar, BlockStmt, Context))
Felix Bergeradfdc142016-03-05 21:17:58 +0000138 return;
Haojian Wu6ccb1dd2016-03-23 09:33:07 +0000139
140 auto Diagnostic = diag(NewVar.getLocation(),
141 "local copy %0 of the variable %1 is never modified; "
142 "consider avoiding the copy")
143 << &NewVar << &OldVar;
Felix Berger6d3d7462016-05-13 02:47:56 +0000144 if (IssueFix)
145 recordFixes(NewVar, Context, Diagnostic);
Alexander Kornienkob959f4c2015-12-30 10:24:40 +0000146}
147
148} // namespace performance
149} // namespace tidy
150} // namespace clang