blob: e4863509ef9a5540eab8c3442720028447f3d2cd [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()),
60 hasInitializer(
61 cxxConstructExpr(
62 hasDeclaration(cxxConstructorDecl(
63 isCopyConstructor())),
64 hasArgument(0, CopyCtorArg))
65 .bind("ctorCall")))
66 .bind("newVarDecl"))).bind("declStmt")))
Haojian Wu6ccb1dd2016-03-23 09:33:07 +000067 .bind("blockStmt");
68 };
69
Alexander Kornienkob959f4c2015-12-30 10:24:40 +000070 Finder->addMatcher(
Haojian Wu6ccb1dd2016-03-23 09:33:07 +000071 localVarCopiedFrom(anyOf(ConstRefReturningFunctionCall,
Felix Berger98e40192016-05-31 00:25:57 +000072 ConstRefReturningMethodCall)),
Alexander Kornienkob959f4c2015-12-30 10:24:40 +000073 this);
Haojian Wu6ccb1dd2016-03-23 09:33:07 +000074
75 Finder->addMatcher(localVarCopiedFrom(declRefExpr(
76 to(varDecl(hasLocalStorage()).bind("oldVarDecl")))),
77 this);
Alexander Kornienkob959f4c2015-12-30 10:24:40 +000078}
79
80void UnnecessaryCopyInitialization::check(
Haojian Wu6ccb1dd2016-03-23 09:33:07 +000081 const MatchFinder::MatchResult &Result) {
82 const auto *NewVar = Result.Nodes.getNodeAs<VarDecl>("newVarDecl");
83 const auto *OldVar = Result.Nodes.getNodeAs<VarDecl>("oldVarDecl");
Felix Berger98e40192016-05-31 00:25:57 +000084 const auto *ObjectArg = Result.Nodes.getNodeAs<VarDecl>("objectArg");
Felix Bergeradfdc142016-03-05 21:17:58 +000085 const auto *BlockStmt = Result.Nodes.getNodeAs<Stmt>("blockStmt");
Haojian Wu6ccb1dd2016-03-23 09:33:07 +000086 const auto *CtorCall = Result.Nodes.getNodeAs<CXXConstructExpr>("ctorCall");
Felix Berger6d3d7462016-05-13 02:47:56 +000087 // 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 Wu6ccb1dd2016-03-23 09:33:07 +000092
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 Berger98e40192016-05-31 00:25:57 +0000101 handleCopyFromMethodReturn(*NewVar, *BlockStmt, IssueFix, ObjectArg,
102 *Result.Context);
Haojian Wu6ccb1dd2016-03-23 09:33:07 +0000103 } else {
Felix Berger6d3d7462016-05-13 02:47:56 +0000104 handleCopyFromLocalVar(*NewVar, *OldVar, *BlockStmt, IssueFix,
105 *Result.Context);
Haojian Wu6ccb1dd2016-03-23 09:33:07 +0000106 }
107}
108
109void UnnecessaryCopyInitialization::handleCopyFromMethodReturn(
Felix Berger6d3d7462016-05-13 02:47:56 +0000110 const VarDecl &Var, const Stmt &BlockStmt, bool IssueFix,
Felix Berger98e40192016-05-31 00:25:57 +0000111 const VarDecl *ObjectArg, ASTContext &Context) {
Haojian Wu6ccb1dd2016-03-23 09:33:07 +0000112 bool IsConstQualified = Var.getType().isConstQualified();
113 if (!IsConstQualified && !isOnlyUsedAsConst(Var, BlockStmt, Context))
Felix Bergeradfdc142016-03-05 21:17:58 +0000114 return;
Felix Berger98e40192016-05-31 00:25:57 +0000115 if (ObjectArg != nullptr &&
116 !isOnlyUsedAsConst(*ObjectArg, BlockStmt, Context))
117 return;
Haojian Wu6ccb1dd2016-03-23 09:33:07 +0000118
119 auto Diagnostic =
120 diag(Var.getLocation(),
121 IsConstQualified ? "the const qualified variable %0 is "
Felix Bergeradfdc142016-03-05 21:17:58 +0000122 "copy-constructed from a const reference; "
123 "consider making it a const reference"
Haojian Wu6ccb1dd2016-03-23 09:33:07 +0000124 : "the variable %0 is copy-constructed from a "
Felix Bergeradfdc142016-03-05 21:17:58 +0000125 "const reference but is only used as const "
126 "reference; consider making it a const reference")
Haojian Wu6ccb1dd2016-03-23 09:33:07 +0000127 << &Var;
Felix Berger6d3d7462016-05-13 02:47:56 +0000128 if (IssueFix)
129 recordFixes(Var, Context, Diagnostic);
Haojian Wu6ccb1dd2016-03-23 09:33:07 +0000130}
131
132void UnnecessaryCopyInitialization::handleCopyFromLocalVar(
133 const VarDecl &NewVar, const VarDecl &OldVar, const Stmt &BlockStmt,
Felix Berger6d3d7462016-05-13 02:47:56 +0000134 bool IssueFix, ASTContext &Context) {
Haojian Wu6ccb1dd2016-03-23 09:33:07 +0000135 if (!isOnlyUsedAsConst(NewVar, BlockStmt, Context) ||
136 !isOnlyUsedAsConst(OldVar, BlockStmt, Context))
Felix Bergeradfdc142016-03-05 21:17:58 +0000137 return;
Haojian Wu6ccb1dd2016-03-23 09:33:07 +0000138
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 Berger6d3d7462016-05-13 02:47:56 +0000143 if (IssueFix)
144 recordFixes(NewVar, Context, Diagnostic);
Alexander Kornienkob959f4c2015-12-30 10:24:40 +0000145}
146
147} // namespace performance
148} // namespace tidy
149} // namespace clang