blob: fcd5a96eb662fea002c50daf0d55a3b108603edd (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
// RUN: %check_clang_tidy %s performance-inefficient-vector-operation %t -- -format-style=llvm --
namespace std {
typedef int size_t;
template <class T>
class vector {
public:
typedef T* iterator;
typedef const T* const_iterator;
typedef T& reference;
typedef const T& const_reference;
typedef size_t size_type;
explicit vector();
explicit vector(size_type n);
void push_back(const T& val);
void reserve(size_t n);
void resize(size_t n);
size_t size();
const_reference operator[] (size_type) const;
reference operator[] (size_type);
};
} // namespace std
void f(std::vector<int>& t) {
{
std::vector<int> v;
// CHECK-FIXES: v.reserve(10);
for (int i = 0; i < 10; ++i)
v.push_back(i);
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called inside a loop; consider pre-allocating the vector capacity before the loop
}
{
std::vector<int> v;
// CHECK-FIXES: v.reserve(10);
for (int i = 0; i < 10; i++)
v.push_back(i);
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called
}
{
std::vector<int> v;
// CHECK-FIXES: v.reserve(10);
for (int i = 0; i < 10; ++i)
v.push_back(0);
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called
}
{
std::vector<int> v;
// CHECK-FIXES: v.reserve(5);
for (int i = 0; i < 5; ++i) {
v.push_back(i);
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called
}
// CHECK-FIXES-NOT: v.reserve(10);
for (int i = 0; i < 10; ++i) {
// No fix for this loop as we encounter the prior loops.
v.push_back(i);
}
}
{
std::vector<int> v;
std::vector<int> v2;
v2.reserve(3);
// CHECK-FIXES: v.reserve(10);
for (int i = 0; i < 10; ++i)
v.push_back(i);
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called
}
{
std::vector<int> v;
// CHECK-FIXES: v.reserve(t.size());
for (std::size_t i = 0; i < t.size(); ++i) {
v.push_back(t[i]);
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called
}
}
{
std::vector<int> v;
// CHECK-FIXES: v.reserve(t.size() - 1);
for (std::size_t i = 0; i < t.size() - 1; ++i) {
v.push_back(t[i]);
} // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called
}
// ---- Non-fixed Cases ----
{
std::vector<int> v;
v.reserve(20);
// CHECK-FIXES-NOT: v.reserve(10);
// There is a "reserve" call already.
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
}
{
std::vector<int> v;
v.reserve(5);
// CHECK-FIXES-NOT: v.reserve(10);
// There is a "reserve" call already.
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
}
{
std::vector<int> v;
v.resize(5);
// CHECK-FIXES-NOT: v.reserve(10);
// There is a ref usage of v before the loop.
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
}
{
std::vector<int> v;
v.push_back(0);
// CHECK-FIXES-NOT: v.reserve(10);
// There is a ref usage of v before the loop.
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
}
{
std::vector<int> v;
f(v);
// CHECK-FIXES-NOT: v.reserve(10);
// There is a ref usage of v before the loop.
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
}
{
std::vector<int> v(20);
// CHECK-FIXES-NOT: v.reserve(10);
// v is not constructed with default constructor.
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
}
{
std::vector<int> v;
// CHECK-FIXES-NOT: v.reserve(10);
// For-loop is not started with 0.
for (int i = 1; i < 10; ++i) {
v.push_back(i);
}
}
{
std::vector<int> v;
// CHECK-FIXES-NOT: v.reserve(t.size());
// v isn't referenced in for-loop body.
for (std::size_t i = 0; i < t.size(); ++i) {
t.push_back(i);
}
}
{
std::vector<int> v;
int k;
// CHECK-FIXES-NOT: v.reserve(10);
// For-loop isn't a fixable loop.
for (std::size_t i = 0; k < 10; ++i) {
v.push_back(t[i]);
}
}
{
std::vector<int> v;
// CHECK-FIXES-NOT: v.reserve(i + 1);
// The loop end expression refers to the loop variable i.
for (int i = 0; i < i + 1; i++)
v.push_back(i);
}
{
std::vector<int> v;
int k;
// CHECK-FIXES-NOT: v.reserve(10);
// For-loop isn't a fixable loop.
for (std::size_t i = 0; i < 10; ++k) {
v.push_back(t[i]);
}
}
}
|