forked from mathieu/cppcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestconstructors.cpp
More file actions
389 lines (336 loc) · 10.2 KB
/
testconstructors.cpp
File metadata and controls
389 lines (336 loc) · 10.2 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2009 Daniel Marjamäki and Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/
*/
#include "../src/tokenize.h"
#include "../src/checkclass.h"
#include "testsuite.h"
#include <sstream>
extern std::ostringstream errout;
class TestConstructors : public TestFixture
{
public:
TestConstructors() : TestFixture("TestConstructors")
{ }
private:
void check(const char code[], bool showAll = false)
{
// Tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList();
// Clear the error buffer..
errout.str("");
// Check class constructors..
Settings settings;
settings._checkCodingStyle = true;
settings._showAll = showAll;
CheckClass checkClass(&tokenizer, &settings, this);
checkClass.constructors();
}
void run()
{
TEST_CASE(simple1);
TEST_CASE(simple2);
TEST_CASE(simple3);
TEST_CASE(simple4);
TEST_CASE(initvar_with_this); // BUG 2190300
TEST_CASE(initvar_if); // BUG 2190290
TEST_CASE(initvar_operator_eq1); // BUG 2190376
TEST_CASE(initvar_operator_eq2); // BUG 2190376
TEST_CASE(initvar_operator_eq3);
TEST_CASE(initvar_same_classname); // BUG 2208157
TEST_CASE(initvar_chained_assign); // BUG 2270433
TEST_CASE(initvar_2constructors); // BUG 2270353
TEST_CASE(initvar_constvar);
TEST_CASE(initvar_staticvar);
TEST_CASE(initvar_private_constructor); // BUG 2354171 - private constructor
TEST_CASE(initvar_destructor); // No variables need to be initialized in a destructor
TEST_CASE(operatorEqSTL);
}
void simple1()
{
check("class Fred\n"
"{\n"
"public:\n"
" int i;\n"
"};\n");
ASSERT_EQUALS("[test.cpp:1]: (style) The class 'Fred' has no constructor\n", errout.str());
}
void simple2()
{
check("class Fred\n"
"{\n"
"public:\n"
" Fred() { }\n"
" int i;\n"
"};\n");
ASSERT_EQUALS("[test.cpp:4]: (style) Member variable not initialized in the constructor 'Fred::i'\n", errout.str());
}
void simple3()
{
check("class Fred\n"
"{\n"
"public:\n"
" Fred();\n"
" int i;\n"
"};\n"
"Fred::Fred()\n"
"{ }\n");
ASSERT_EQUALS("[test.cpp:7]: (style) Member variable not initialized in the constructor 'Fred::i'\n", errout.str());
}
void simple4()
{
check("class Fred\n"
"{\n"
"public:\n"
" Fred();\n"
" Fred(int _i);\n"
" int i;\n"
"};\n"
"Fred::Fred()\n"
"{ }\n"
"Fred::Fred(int _i)\n"
"{\n"
" i = _i;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:8]: (style) Member variable not initialized in the constructor 'Fred::i'\n", errout.str());
}
void initvar_with_this()
{
check("class Fred\n"
"{\n"
"public:\n"
" Fred()\n"
" { this->i = 0; }\n"
" int i;\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}
void initvar_if()
{
check("class Fred\n"
"{\n"
"public:\n"
" Fred()\n"
" {\n"
" if (true)\n"
" i = 0;\n"
" else\n"
" i = 1;\n"
" }\n"
" int i;\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}
void initvar_operator_eq1()
{
// Bug 2190376 - False positive, Uninitialized member variable with operator=
check("class Fred\n"
"{\n"
"private:\n"
" int i;\n"
"\n"
"public:\n"
" Fred()\n"
" { i = 0; }\n"
"\n"
" Fred(const Fred &fred)\n"
" { *this = fred; }\n"
"\n"
" const Fred & operator=(const Fred &fred)\n"
" { i = fred.i; return *this; }\n"
"};\n");
ASSERT_EQUALS("", errout.str());
check("class A\n"
"{\n"
" public:\n"
" A() : i(0), j(0) {}\n"
"\n"
" A &operator=(const int &value)\n"
" {\n"
" i = value;\n"
" return (*this);\n"
" }\n"
"\n"
" int i;\n"
" int j;\n"
"};\n"
"\n"
"int main() {}\n");
ASSERT_EQUALS("", errout.str());
}
void initvar_operator_eq2()
{
check("class Fred\n"
"{\n"
"public:\n"
" Fred() { i = 0; }\n"
" void operator=(const Fred &fred) { }\n"
" int i;\n"
"};\n");
ASSERT_EQUALS("[test.cpp:5]: (possible style) Member variable 'Fred::i' is not assigned a value in 'Fred::operator='\n", errout.str());
}
void initvar_operator_eq3()
{
check("class Fred\n"
"{\n"
"public:\n"
" Fred() { Init(); }\n"
" void operator=(const Fred &fred) { Init(); }\n"
"private:\n"
" void Init() { i = 0; }\n"
" int i;\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}
void initvar_same_classname()
{
// Bug 2208157 - False positive: Uninitialized variable, same class name
check("void func1()\n"
"{\n"
" class Fred\n"
" {\n"
" int a;\n"
" Fred() { a = 0; }\n"
" };\n"
"}\n"
"\n"
"void func2()\n"
"{\n"
" class Fred\n"
" {\n"
" int b;\n"
" Fred() { b = 0; }\n"
" };\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void initvar_chained_assign()
{
// Bug 2270433 - Uninitialized variable false positive on chained assigns
check("class c\n"
"{\n"
" c();\n"
"\n"
" int m_iMyInt1;\n"
" int m_iMyInt2;\n"
"}\n"
"\n"
"c::c()\n"
"{\n"
" m_iMyInt1 = m_iMyInt2 = 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void initvar_2constructors()
{
check("class c\n"
"{\n"
" c();\n"
" c(bool b);"
"\n"
" void InitInt();\n"
"\n"
" int m_iMyInt;\n"
" int m_bMyBool;\n"
"}\n"
"\n"
"c::c()\n"
"{\n"
" m_bMyBool = false;\n"
" InitInt();"
"}\n"
"\n"
"c::c(bool b)\n"
"{\n"
" m_bMyBool = b;\n"
" InitInt();\n"
"}\n"
"\n"
"void c::InitInt()\n"
"{\n"
" m_iMyInt = 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void initvar_constvar()
{
check("class Fred\n"
"{\n"
"public:\n"
" const char *s;\n"
" Fred();\n"
"};\n"
"Fred::Fred()\n"
"{ }");
ASSERT_EQUALS("[test.cpp:7]: (style) Member variable not initialized in the constructor 'Fred::s'\n", errout.str());
}
void initvar_staticvar()
{
check("class Fred\n"
"{\n"
"public:\n"
" Fred() { }\n"
" static void *p;\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}
void initvar_private_constructor()
{
check("class Fred\n"
"{\n"
"private:\n"
" int var;\n"
" Fred();\n"
"};\n"
"Fred::Fred()\n"
"{ }");
ASSERT_EQUALS("", errout.str());
}
void initvar_destructor()
{
check("class Fred\n"
"{\n"
"private:\n"
" int var;\n"
"public:\n"
" Fred() : var(0) {}\n"
" ~Fred() {}\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}
void operatorEqSTL()
{
check("class Fred\n"
"{\n"
"private:\n"
" std::vector<int> ints;\n"
"public:\n"
" Fred();\n"
" void operator=(const Fred &f);\n"
"};\n"
"\n"
"Fred::Fred()\n"
"{ }\n"
"\n"
"void Fred::operator=(const Fred &f)\n"
"{ }", true);
ASSERT_EQUALS("[test.cpp:13]: (possible style) Member variable 'Fred::ints' is not assigned a value in 'Fred::operator='\n", errout.str());
}
};
REGISTER_TEST(TestConstructors)