blob: 33c68f014db56a3c7a94c25749fe3bdb08926db8 [file] [log] [blame]
Haojian Wuc5cc0332017-04-18 07:46:39 +00001//===--- InefficientVectorOperationCheck.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 "InefficientVectorOperationCheck.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/Lex/Lexer.h"
14#include "../utils/DeclRefExprUtils.h"
Haojian Wu489f3632017-04-26 18:13:05 +000015#include "../utils/OptionsUtils.h"
Haojian Wuc5cc0332017-04-18 07:46:39 +000016
17using namespace clang::ast_matchers;
18
19namespace clang {
20namespace tidy {
21namespace performance {
22
23namespace {
24
25// Matcher names. Given the code:
26//
27// \code
28// void f() {
29// vector<T> v;
30// for (int i = 0; i < 10 + 1; ++i) {
31// v.push_back(i);
32// }
33// }
34// \endcode
35//
36// The matcher names are bound to following parts of the AST:
Haojian Wu489f3632017-04-26 18:13:05 +000037// - LoopCounterName: The entire for loop (as ForStmt).
Haojian Wuc5cc0332017-04-18 07:46:39 +000038// - LoopParentName: The body of function f (as CompoundStmt).
39// - VectorVarDeclName: 'v' in (as VarDecl).
40// - VectorVarDeclStmatName: The entire 'std::vector<T> v;' statement (as
41// DeclStmt).
Haojian Wube6ef0e2017-05-16 10:39:55 +000042// - PushBackOrEmplaceBackCallName: 'v.push_back(i)' (as cxxMemberCallExpr).
Haojian Wuc5cc0332017-04-18 07:46:39 +000043// - LoopInitVarName: 'i' (as VarDecl).
44// - LoopEndExpr: '10+1' (as Expr).
45static const char LoopCounterName[] = "for_loop_counter";
46static const char LoopParentName[] = "loop_parent";
47static const char VectorVarDeclName[] = "vector_var_decl";
48static const char VectorVarDeclStmtName[] = "vector_var_decl_stmt";
Haojian Wube6ef0e2017-05-16 10:39:55 +000049static const char PushBackOrEmplaceBackCallName[] = "append_call";
Haojian Wuc5cc0332017-04-18 07:46:39 +000050static const char LoopInitVarName[] = "loop_init_var";
51static const char LoopEndExprName[] = "loop_end_expr";
52
Haojian Wu489f3632017-04-26 18:13:05 +000053static const char RangeLoopName[] = "for_range_loop";
54
55ast_matchers::internal::Matcher<Expr> supportedContainerTypesMatcher() {
56 return hasType(cxxRecordDecl(hasAnyName(
57 "::std::vector", "::std::set", "::std::unordered_set", "::std::map",
Haojian Wuce4b17f2017-05-15 18:18:28 +000058 "::std::unordered_map", "::std::array", "::std::deque")));
Haojian Wu489f3632017-04-26 18:13:05 +000059}
60
Haojian Wuc5cc0332017-04-18 07:46:39 +000061} // namespace
62
Haojian Wu489f3632017-04-26 18:13:05 +000063InefficientVectorOperationCheck::InefficientVectorOperationCheck(
64 StringRef Name, ClangTidyContext *Context)
65 : ClangTidyCheck(Name, Context),
66 VectorLikeClasses(utils::options::parseStringList(
67 Options.get("VectorLikeClasses", "::std::vector"))) {}
68
69void InefficientVectorOperationCheck::storeOptions(
70 ClangTidyOptions::OptionMap &Opts) {
71 Options.store(Opts, "VectorLikeClasses",
72 utils::options::serializeStringList(VectorLikeClasses));
73}
74
Haojian Wuc5cc0332017-04-18 07:46:39 +000075void InefficientVectorOperationCheck::registerMatchers(MatchFinder *Finder) {
Haojian Wu489f3632017-04-26 18:13:05 +000076 const auto VectorDecl = cxxRecordDecl(hasAnyName(SmallVector<StringRef, 5>(
77 VectorLikeClasses.begin(), VectorLikeClasses.end())));
Haojian Wuc5cc0332017-04-18 07:46:39 +000078 const auto VectorDefaultConstructorCall = cxxConstructExpr(
79 hasType(VectorDecl),
80 hasDeclaration(cxxConstructorDecl(isDefaultConstructor())));
81 const auto VectorVarDecl =
82 varDecl(hasInitializer(VectorDefaultConstructorCall))
83 .bind(VectorVarDeclName);
Haojian Wube6ef0e2017-05-16 10:39:55 +000084 const auto VectorAppendCallExpr =
Haojian Wuc5cc0332017-04-18 07:46:39 +000085 cxxMemberCallExpr(
Haojian Wube6ef0e2017-05-16 10:39:55 +000086 callee(cxxMethodDecl(hasAnyName("push_back", "emplace_back"))),
87 on(hasType(VectorDecl)),
Haojian Wuc5cc0332017-04-18 07:46:39 +000088 onImplicitObjectArgument(declRefExpr(to(VectorVarDecl))))
Haojian Wube6ef0e2017-05-16 10:39:55 +000089 .bind(PushBackOrEmplaceBackCallName);
90 const auto VectorAppendCall = expr(ignoringImplicit(VectorAppendCallExpr));
Haojian Wuc5cc0332017-04-18 07:46:39 +000091 const auto VectorVarDefStmt =
92 declStmt(hasSingleDecl(equalsBoundNode(VectorVarDeclName)))
93 .bind(VectorVarDeclStmtName);
94
95 const auto LoopVarInit =
96 declStmt(hasSingleDecl(varDecl(hasInitializer(integerLiteral(equals(0))))
97 .bind(LoopInitVarName)));
98 const auto RefersToLoopVar = ignoringParenImpCasts(
99 declRefExpr(to(varDecl(equalsBoundNode(LoopInitVarName)))));
100
Haojian Wube6ef0e2017-05-16 10:39:55 +0000101 // Matchers for the loop whose body has only 1 push_back/emplace_back calling
102 // statement.
103 const auto HasInterestingLoopBody =
104 hasBody(anyOf(compoundStmt(statementCountIs(1), has(VectorAppendCall)),
105 VectorAppendCall));
Haojian Wu489f3632017-04-26 18:13:05 +0000106 const auto InInterestingCompoundStmt =
107 hasParent(compoundStmt(has(VectorVarDefStmt)).bind(LoopParentName));
108
Haojian Wuc5cc0332017-04-18 07:46:39 +0000109 // Match counter-based for loops:
110 // for (int i = 0; i < n; ++i) { v.push_back(...); }
111 //
112 // FIXME: Support more types of counter-based loops like decrement loops.
113 Finder->addMatcher(
114 forStmt(
115 hasLoopInit(LoopVarInit),
116 hasCondition(binaryOperator(
117 hasOperatorName("<"), hasLHS(RefersToLoopVar),
118 hasRHS(expr(unless(hasDescendant(expr(RefersToLoopVar))))
119 .bind(LoopEndExprName)))),
120 hasIncrement(unaryOperator(hasOperatorName("++"),
121 hasUnaryOperand(RefersToLoopVar))),
Haojian Wu489f3632017-04-26 18:13:05 +0000122 HasInterestingLoopBody, InInterestingCompoundStmt)
Haojian Wuc5cc0332017-04-18 07:46:39 +0000123 .bind(LoopCounterName),
124 this);
Haojian Wu489f3632017-04-26 18:13:05 +0000125
126 // Match for-range loops:
127 // for (const auto& E : data) { v.push_back(...); }
128 //
129 // FIXME: Support more complex range-expressions.
130 Finder->addMatcher(
131 cxxForRangeStmt(
132 hasRangeInit(declRefExpr(supportedContainerTypesMatcher())),
133 HasInterestingLoopBody, InInterestingCompoundStmt)
134 .bind(RangeLoopName),
135 this);
Haojian Wuc5cc0332017-04-18 07:46:39 +0000136}
137
138void InefficientVectorOperationCheck::check(
139 const MatchFinder::MatchResult &Result) {
Haojian Wuc2c22832017-04-18 20:47:34 +0000140 auto* Context = Result.Context;
141 if (Context->getDiagnostics().hasUncompilableErrorOccurred())
Haojian Wuc5cc0332017-04-18 07:46:39 +0000142 return;
143
144 const SourceManager &SM = *Result.SourceManager;
Haojian Wu489f3632017-04-26 18:13:05 +0000145 const auto *VectorVarDecl =
146 Result.Nodes.getNodeAs<VarDecl>(VectorVarDeclName);
Haojian Wuc5cc0332017-04-18 07:46:39 +0000147 const auto *ForLoop = Result.Nodes.getNodeAs<ForStmt>(LoopCounterName);
Haojian Wu489f3632017-04-26 18:13:05 +0000148 const auto *RangeLoop =
149 Result.Nodes.getNodeAs<CXXForRangeStmt>(RangeLoopName);
Haojian Wube6ef0e2017-05-16 10:39:55 +0000150 const auto *VectorAppendCall =
151 Result.Nodes.getNodeAs<CXXMemberCallExpr>(PushBackOrEmplaceBackCallName);
Haojian Wuc5cc0332017-04-18 07:46:39 +0000152 const auto *LoopEndExpr = Result.Nodes.getNodeAs<Expr>(LoopEndExprName);
153 const auto *LoopParent = Result.Nodes.getNodeAs<CompoundStmt>(LoopParentName);
Haojian Wu489f3632017-04-26 18:13:05 +0000154
155 const Stmt *LoopStmt = ForLoop;
156 if (!LoopStmt)
157 LoopStmt = RangeLoop;
Haojian Wuc5cc0332017-04-18 07:46:39 +0000158
159 llvm::SmallPtrSet<const DeclRefExpr *, 16> AllVectorVarRefs =
160 utils::decl_ref_expr::allDeclRefExprs(*VectorVarDecl, *LoopParent,
Haojian Wuc2c22832017-04-18 20:47:34 +0000161 *Context);
Haojian Wuc5cc0332017-04-18 07:46:39 +0000162 for (const auto *Ref : AllVectorVarRefs) {
163 // Skip cases where there are usages (defined as DeclRefExpr that refers to
164 // "v") of vector variable `v` before the for loop. We consider these usages
165 // are operations causing memory preallocation (e.g. "v.resize(n)",
166 // "v.reserve(n)").
167 //
168 // FIXME: make it more intelligent to identify the pre-allocating operations
169 // before the for loop.
170 if (SM.isBeforeInTranslationUnit(Ref->getLocation(),
Haojian Wu489f3632017-04-26 18:13:05 +0000171 LoopStmt->getLocStart())) {
Haojian Wuc5cc0332017-04-18 07:46:39 +0000172 return;
173 }
174 }
175
Haojian Wuc2c22832017-04-18 20:47:34 +0000176 llvm::StringRef VectorVarName = Lexer::getSourceText(
Haojian Wuc5cc0332017-04-18 07:46:39 +0000177 CharSourceRange::getTokenRange(
Haojian Wube6ef0e2017-05-16 10:39:55 +0000178 VectorAppendCall->getImplicitObjectArgument()->getSourceRange()),
Haojian Wuc2c22832017-04-18 20:47:34 +0000179 SM, Context->getLangOpts());
Haojian Wuc5cc0332017-04-18 07:46:39 +0000180
Haojian Wu489f3632017-04-26 18:13:05 +0000181 std::string ReserveStmt;
182 // Handle for-range loop cases.
183 if (RangeLoop) {
184 // Get the range-expression in a for-range statement represented as
185 // `for (range-declarator: range-expression)`.
186 StringRef RangeInitExpName = Lexer::getSourceText(
187 CharSourceRange::getTokenRange(
188 RangeLoop->getRangeInit()->getSourceRange()),
189 SM, Context->getLangOpts());
190
191 ReserveStmt =
192 (VectorVarName + ".reserve(" + RangeInitExpName + ".size()" + ");\n")
193 .str();
194 } else if (ForLoop) {
195 // Handle counter-based loop cases.
196 StringRef LoopEndSource = Lexer::getSourceText(
197 CharSourceRange::getTokenRange(LoopEndExpr->getSourceRange()), SM,
198 Context->getLangOpts());
199 ReserveStmt = (VectorVarName + ".reserve(" + LoopEndSource + ");\n").str();
200 }
201
Haojian Wube6ef0e2017-05-16 10:39:55 +0000202 auto Diag =
203 diag(VectorAppendCall->getLocStart(),
204 "%0 is called inside a loop; "
205 "consider pre-allocating the vector capacity before the loop")
206 << VectorAppendCall->getMethodDecl()->getDeclName();
Haojian Wu489f3632017-04-26 18:13:05 +0000207
208 if (!ReserveStmt.empty())
209 Diag << FixItHint::CreateInsertion(LoopStmt->getLocStart(), ReserveStmt);
Haojian Wuc5cc0332017-04-18 07:46:39 +0000210}
211
212} // namespace performance
213} // namespace tidy
214} // namespace clang