blob: 3fe13bc7d33c9c69574724c465c9705a0e6badd9 [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) {
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 Kornienkob959f4c2015-12-30 10:24:40 +000034
35using namespace ::clang::ast_matchers;
Haojian Wu6ccb1dd2016-03-23 09:33:07 +000036using decl_ref_expr_utils::isOnlyUsedAsConst;
Alexander Kornienkob959f4c2015-12-30 10:24:40 +000037
Haojian Wu6ccb1dd2016-03-23 09:33:07 +000038void UnnecessaryCopyInitialization::registerMatchers(MatchFinder *Finder) {
Alexander Kornienkob959f4c2015-12-30 10:24:40 +000039 auto ConstReference = referenceType(pointee(qualType(isConstQualified())));
40 auto ConstOrConstReference =
41 allOf(anyOf(ConstReference, isConstQualified()),
Felix Bergeradfdc142016-03-05 21:17:58 +000042 unless(allOf(pointerType(), unless(pointerType(pointee(
43 qualType(isConstQualified())))))));
Haojian Wu6ccb1dd2016-03-23 09:33:07 +000044
Alexander Kornienkob959f4c2015-12-30 10:24:40 +000045 // 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 Wu6ccb1dd2016-03-23 09:33:07 +000056
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 Kornienkob959f4c2015-12-30 10:24:40 +000071 Finder->addMatcher(
Haojian Wu6ccb1dd2016-03-23 09:33:07 +000072 localVarCopiedFrom(anyOf(ConstRefReturningFunctionCall,
73 ConstRefReturningMethodCallOfConstParam)),
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 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");
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
102void 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 Bergeradfdc142016-03-05 21:17:58 +0000106 return;
Haojian Wu6ccb1dd2016-03-23 09:33:07 +0000107
108 auto Diagnostic =
109 diag(Var.getLocation(),
110 IsConstQualified ? "the const qualified variable %0 is "
Felix Bergeradfdc142016-03-05 21:17:58 +0000111 "copy-constructed from a const reference; "
112 "consider making it a const reference"
Haojian Wu6ccb1dd2016-03-23 09:33:07 +0000113 : "the variable %0 is copy-constructed from a "
Felix Bergeradfdc142016-03-05 21:17:58 +0000114 "const reference but is only used as const "
115 "reference; consider making it a const reference")
Haojian Wu6ccb1dd2016-03-23 09:33:07 +0000116 << &Var;
117 recordFixes(Var, Context, Diagnostic);
118}
119
120void 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 Bergeradfdc142016-03-05 21:17:58 +0000125 return;
Haojian Wu6ccb1dd2016-03-23 09:33:07 +0000126
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 Kornienkob959f4c2015-12-30 10:24:40 +0000132}
133
134} // namespace performance
135} // namespace tidy
136} // namespace clang