2008-12-18 22:28:57 +01:00
|
|
|
/*
|
2009-01-21 21:04:20 +01:00
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2010-04-13 21:23:17 +02:00
|
|
|
* Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team.
|
2008-12-18 22:28:57 +01:00
|
|
|
*
|
|
|
|
* 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
|
2009-09-27 17:08:31 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-12-18 22:28:57 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-10-25 12:49:06 +01:00
|
|
|
#include "tokenize.h"
|
|
|
|
#include "checkclass.h"
|
2008-12-18 22:28:57 +01:00
|
|
|
#include "testsuite.h"
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
extern std::ostringstream errout;
|
|
|
|
|
|
|
|
class TestConstructors : public TestFixture
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TestConstructors() : TestFixture("TestConstructors")
|
|
|
|
{ }
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
2009-05-01 12:03:25 +02:00
|
|
|
void check(const char code[], bool showAll = false)
|
2008-12-18 22:28:57 +01:00
|
|
|
{
|
|
|
|
// Clear the error buffer..
|
|
|
|
errout.str("");
|
|
|
|
|
2010-04-17 09:23:54 +02:00
|
|
|
Settings settings;
|
|
|
|
settings.inconclusive = showAll;
|
2008-12-18 22:28:57 +01:00
|
|
|
settings._checkCodingStyle = true;
|
2010-12-01 18:00:55 +01:00
|
|
|
|
|
|
|
// Tokenize..
|
|
|
|
Tokenizer tokenizer(&settings, this);
|
|
|
|
std::istringstream istr(code);
|
|
|
|
tokenizer.tokenize(istr, "test.cpp");
|
|
|
|
tokenizer.simplifyTokenList();
|
|
|
|
|
|
|
|
// Check class constructors..
|
2009-03-20 17:30:24 +01:00
|
|
|
CheckClass checkClass(&tokenizer, &settings, this);
|
2008-12-20 09:48:52 +01:00
|
|
|
checkClass.constructors();
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void run()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
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);
|
2010-11-16 07:30:55 +01:00
|
|
|
TEST_CASE(initvar_operator_eq4); // ticket #2204
|
2009-01-05 16:49:57 +01:00
|
|
|
TEST_CASE(initvar_same_classname); // BUG 2208157
|
|
|
|
TEST_CASE(initvar_chained_assign); // BUG 2270433
|
|
|
|
TEST_CASE(initvar_2constructors); // BUG 2270353
|
2009-03-12 21:33:10 +01:00
|
|
|
TEST_CASE(initvar_constvar);
|
2009-05-17 19:27:16 +02:00
|
|
|
TEST_CASE(initvar_staticvar);
|
2009-01-05 16:49:57 +01:00
|
|
|
|
|
|
|
TEST_CASE(initvar_private_constructor); // BUG 2354171 - private constructor
|
2010-07-14 19:00:52 +02:00
|
|
|
TEST_CASE(initvar_copy_constructor); // ticket #1611
|
2010-07-15 10:16:16 +02:00
|
|
|
TEST_CASE(initvar_nested_constructor); // ticket #1375
|
2009-01-05 16:49:57 +01:00
|
|
|
|
|
|
|
TEST_CASE(initvar_destructor); // No variables need to be initialized in a destructor
|
2009-04-28 20:50:10 +02:00
|
|
|
|
|
|
|
TEST_CASE(operatorEqSTL);
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void simple1()
|
|
|
|
{
|
2010-01-02 17:29:55 +01:00
|
|
|
check("class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" int i;\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
check("class Fred\n"
|
|
|
|
"{\n"
|
2009-12-19 17:58:52 +01:00
|
|
|
"private:\n"
|
2009-01-05 16:49:57 +01:00
|
|
|
" int i;\n"
|
|
|
|
"};\n");
|
2010-11-27 09:17:03 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:1]: (style) The class 'Fred' does not have a constructor.\n", errout.str());
|
2010-01-02 17:29:55 +01:00
|
|
|
|
|
|
|
check("struct Fred\n"
|
|
|
|
"{\n"
|
|
|
|
" int i;\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
check("struct Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"private:\n"
|
|
|
|
" int i;\n"
|
|
|
|
"};\n");
|
2010-11-27 09:17:03 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:1]: (style) The struct 'Fred' does not have a constructor.\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void simple2()
|
|
|
|
{
|
2010-01-02 17:29:55 +01:00
|
|
|
check("class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" Fred() : i(0) { }\n"
|
|
|
|
" int i;\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
check("class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" Fred() { i = 0; }\n"
|
|
|
|
" int i;\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
check("class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" Fred() { }\n"
|
|
|
|
" int i;\n"
|
|
|
|
"};\n");
|
2010-10-17 14:41:00 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (warning) Member variable not initialized in the constructor 'Fred::i'\n", errout.str());
|
2010-01-02 17:29:55 +01:00
|
|
|
|
|
|
|
check("struct Fred\n"
|
|
|
|
"{\n"
|
|
|
|
" Fred() : i(0) { }\n"
|
|
|
|
" int i;\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
check("struct Fred\n"
|
|
|
|
"{\n"
|
|
|
|
" Fred() { i = 0; }\n"
|
|
|
|
" int i;\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
check("struct Fred\n"
|
|
|
|
"{\n"
|
|
|
|
" Fred() { }\n"
|
|
|
|
" int i;\n"
|
|
|
|
"};\n");
|
2010-10-17 14:41:00 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (warning) Member variable not initialized in the constructor 'Fred::i'\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void simple3()
|
|
|
|
{
|
2010-01-02 17:29:55 +01:00
|
|
|
check("class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" Fred();\n"
|
|
|
|
" int i;\n"
|
|
|
|
"};\n"
|
|
|
|
"Fred::Fred() :i(0)\n"
|
|
|
|
"{ }\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
check("class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" Fred();\n"
|
|
|
|
" int i;\n"
|
|
|
|
"};\n"
|
|
|
|
"Fred::Fred()\n"
|
|
|
|
"{ i = 0; }\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
check("class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" Fred();\n"
|
|
|
|
" int i;\n"
|
|
|
|
"};\n"
|
|
|
|
"Fred::Fred()\n"
|
|
|
|
"{ }\n");
|
2010-10-17 14:41:00 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:7]: (warning) Member variable not initialized in the constructor 'Fred::i'\n", errout.str());
|
2010-01-02 17:29:55 +01:00
|
|
|
|
|
|
|
check("struct Fred\n"
|
|
|
|
"{\n"
|
|
|
|
" Fred();\n"
|
|
|
|
" int i;\n"
|
|
|
|
"};\n"
|
|
|
|
"Fred::Fred() :i(0)\n"
|
|
|
|
"{ }\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
check("struct Fred\n"
|
|
|
|
"{\n"
|
|
|
|
" Fred();\n"
|
|
|
|
" int i;\n"
|
|
|
|
"};\n"
|
|
|
|
"Fred::Fred()\n"
|
|
|
|
"{ i = 0; }\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
check("struct Fred\n"
|
|
|
|
"{\n"
|
|
|
|
" Fred();\n"
|
|
|
|
" int i;\n"
|
|
|
|
"};\n"
|
|
|
|
"Fred::Fred()\n"
|
|
|
|
"{ }\n");
|
2010-10-17 14:41:00 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:6]: (warning) Member variable not initialized in the constructor 'Fred::i'\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void simple4()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
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");
|
2010-10-17 14:41:00 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:8]: (warning) Member variable not initialized in the constructor 'Fred::i'\n", errout.str());
|
2010-01-02 17:29:55 +01:00
|
|
|
|
|
|
|
check("struct Fred\n"
|
|
|
|
"{\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");
|
2010-10-17 14:41:00 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:7]: (warning) Member variable not initialized in the constructor 'Fred::i'\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void initvar_with_this()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" Fred()\n"
|
|
|
|
" { this->i = 0; }\n"
|
|
|
|
" int i;\n"
|
|
|
|
"};\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2010-01-02 17:29:55 +01:00
|
|
|
|
|
|
|
check("struct Fred\n"
|
|
|
|
"{\n"
|
|
|
|
" Fred()\n"
|
|
|
|
" { this->i = 0; }\n"
|
|
|
|
" int i;\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void initvar_if()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
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");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2010-01-02 17:29:55 +01:00
|
|
|
|
|
|
|
check("struct 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());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void initvar_operator_eq1()
|
|
|
|
{
|
|
|
|
// Bug 2190376 - False positive, Uninitialized member variable with operator=
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
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");
|
|
|
|
|
2009-06-14 07:33:46 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-06-16 23:58:16 +02:00
|
|
|
|
|
|
|
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());
|
2010-01-02 17:29:55 +01:00
|
|
|
|
|
|
|
check("struct Fred\n"
|
|
|
|
"{\n"
|
|
|
|
" int i;\n"
|
|
|
|
"\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("struct A\n"
|
|
|
|
"{\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());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void initvar_operator_eq2()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" Fred() { i = 0; }\n"
|
2009-06-16 23:58:16 +02:00
|
|
|
" void operator=(const Fred &fred) { }\n"
|
2009-01-05 16:49:57 +01:00
|
|
|
" int i;\n"
|
|
|
|
"};\n");
|
2010-10-17 14:41:00 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (warning) Member variable 'Fred::i' is not assigned a value in 'Fred::operator='\n", errout.str());
|
2010-01-02 17:29:55 +01:00
|
|
|
|
|
|
|
check("struct Fred\n"
|
|
|
|
"{\n"
|
|
|
|
" Fred() { i = 0; }\n"
|
|
|
|
" void operator=(const Fred &fred) { }\n"
|
|
|
|
" int i;\n"
|
|
|
|
"};\n");
|
2010-10-17 14:41:00 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (warning) Member variable 'Fred::i' is not assigned a value in 'Fred::operator='\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void initvar_operator_eq3()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" Fred() { Init(); }\n"
|
2009-06-16 23:58:16 +02:00
|
|
|
" void operator=(const Fred &fred) { Init(); }\n"
|
2009-01-05 16:49:57 +01:00
|
|
|
"private:\n"
|
2009-06-16 23:58:16 +02:00
|
|
|
" void Init() { i = 0; }\n"
|
2009-01-05 16:49:57 +01:00
|
|
|
" int i;\n"
|
|
|
|
"};\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2010-01-02 17:29:55 +01:00
|
|
|
|
|
|
|
check("struct Fred\n"
|
|
|
|
"{\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());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2010-11-16 07:30:55 +01:00
|
|
|
void initvar_operator_eq4()
|
|
|
|
{
|
|
|
|
check("class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
" int i;\n"
|
|
|
|
"public:\n"
|
|
|
|
" Fred() : i(5) { }\n"
|
|
|
|
" Fred & operator=(const Fred &fred)\n"
|
|
|
|
" {\n"
|
|
|
|
" if (&fred != this)\n"
|
|
|
|
" {\n"
|
|
|
|
" }\n"
|
|
|
|
" return *this\n"
|
|
|
|
" }\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:6]: (warning) Member variable 'Fred::i' is not assigned a value in 'Fred::operator='\n", errout.str());
|
|
|
|
|
|
|
|
check("class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
" int * i;\n"
|
|
|
|
"public:\n"
|
|
|
|
" Fred() : i(NULL) { }\n"
|
|
|
|
" Fred & operator=(const Fred &fred)\n"
|
|
|
|
" {\n"
|
|
|
|
" if (&fred != this)\n"
|
|
|
|
" {\n"
|
|
|
|
" }\n"
|
|
|
|
" return *this\n"
|
|
|
|
" }\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:6]: (warning) Member variable 'Fred::i' is not assigned a value in 'Fred::operator='\n", errout.str());
|
|
|
|
|
|
|
|
check("class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
" const int * i;\n"
|
|
|
|
"public:\n"
|
|
|
|
" Fred() : i(NULL) { }\n"
|
|
|
|
" Fred & operator=(const Fred &fred)\n"
|
|
|
|
" {\n"
|
|
|
|
" if (&fred != this)\n"
|
|
|
|
" {\n"
|
|
|
|
" }\n"
|
|
|
|
" return *this\n"
|
|
|
|
" }\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:6]: (warning) Member variable 'Fred::i' is not assigned a value in 'Fred::operator='\n", errout.str());
|
|
|
|
|
|
|
|
check("class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
" const int i;\n"
|
|
|
|
"public:\n"
|
|
|
|
" Fred() : i(5) { }\n"
|
|
|
|
" Fred & operator=(const Fred &fred)\n"
|
|
|
|
" {\n"
|
|
|
|
" if (&fred != this)\n"
|
|
|
|
" {\n"
|
|
|
|
" }\n"
|
|
|
|
" return *this\n"
|
|
|
|
" }\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
2008-12-18 22:28:57 +01:00
|
|
|
void initvar_same_classname()
|
|
|
|
{
|
|
|
|
// Bug 2208157 - False positive: Uninitialized variable, same class name
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
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");
|
|
|
|
|
2009-06-14 07:33:46 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2010-01-02 17:29:55 +01:00
|
|
|
|
|
|
|
check("void func1()\n"
|
|
|
|
"{\n"
|
|
|
|
" struct 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());
|
|
|
|
|
|
|
|
check("void func1()\n"
|
|
|
|
"{\n"
|
|
|
|
" struct Fred\n"
|
|
|
|
" {\n"
|
|
|
|
" int a;\n"
|
|
|
|
" Fred() { a = 0; }\n"
|
|
|
|
" };\n"
|
|
|
|
"}\n"
|
|
|
|
"\n"
|
|
|
|
"void func2()\n"
|
|
|
|
"{\n"
|
|
|
|
" struct Fred\n"
|
|
|
|
" {\n"
|
|
|
|
" int b;\n"
|
|
|
|
" Fred() { b = 0; }\n"
|
|
|
|
" };\n"
|
|
|
|
"}\n");
|
|
|
|
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2010-11-09 17:06:07 +01:00
|
|
|
|
|
|
|
check("class Foo {\n"
|
|
|
|
" void func1()\n"
|
|
|
|
" {\n"
|
|
|
|
" struct Fred\n"
|
|
|
|
" {\n"
|
|
|
|
" int a;\n"
|
|
|
|
" Fred() { a = 0; }\n"
|
|
|
|
" };\n"
|
|
|
|
" }\n"
|
|
|
|
"\n"
|
|
|
|
" void func2()\n"
|
|
|
|
" {\n"
|
|
|
|
" struct Fred\n"
|
|
|
|
" {\n"
|
|
|
|
" int b;\n"
|
|
|
|
" Fred() { b = 0; }\n"
|
|
|
|
" };\n"
|
|
|
|
" }\n"
|
|
|
|
"};\n");
|
|
|
|
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
check("class Foo {\n"
|
|
|
|
" void func1()\n"
|
|
|
|
" {\n"
|
|
|
|
" struct Fred\n"
|
|
|
|
" {\n"
|
|
|
|
" int a;\n"
|
|
|
|
" Fred() { }\n"
|
|
|
|
" };\n"
|
|
|
|
" }\n"
|
|
|
|
"\n"
|
|
|
|
" void func2()\n"
|
|
|
|
" {\n"
|
|
|
|
" struct Fred\n"
|
|
|
|
" {\n"
|
|
|
|
" int b;\n"
|
|
|
|
" Fred() { }\n"
|
|
|
|
" };\n"
|
|
|
|
" }\n"
|
|
|
|
"};\n");
|
|
|
|
|
|
|
|
ASSERT_EQUALS("[test.cpp:7]: (warning) Member variable not initialized in the constructor 'Fred::a'\n"
|
|
|
|
"[test.cpp:16]: (warning) Member variable not initialized in the constructor 'Fred::b'\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void initvar_chained_assign()
|
|
|
|
{
|
|
|
|
// Bug 2270433 - Uninitialized variable false positive on chained assigns
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
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");
|
|
|
|
|
2009-06-14 07:33:46 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2010-01-02 17:29:55 +01:00
|
|
|
|
|
|
|
check("struct 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());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void initvar_2constructors()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
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");
|
|
|
|
|
2009-06-14 07:33:46 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2010-01-02 17:29:55 +01:00
|
|
|
|
|
|
|
check("struct 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());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-12 21:33:10 +01:00
|
|
|
void initvar_constvar()
|
|
|
|
{
|
2010-01-02 17:29:55 +01:00
|
|
|
check("class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" const char *s;\n"
|
|
|
|
" Fred();\n"
|
|
|
|
"};\n"
|
|
|
|
"Fred::Fred() : s(NULL)\n"
|
|
|
|
"{ }");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
check("class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" const char *s;\n"
|
|
|
|
" Fred();\n"
|
|
|
|
"};\n"
|
|
|
|
"Fred::Fred()\n"
|
|
|
|
"{ s = NULL; }");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2009-03-12 21:33:10 +01:00
|
|
|
check("class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" const char *s;\n"
|
|
|
|
" Fred();\n"
|
|
|
|
"};\n"
|
|
|
|
"Fred::Fred()\n"
|
|
|
|
"{ }");
|
2010-10-17 14:41:00 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:7]: (warning) Member variable not initialized in the constructor 'Fred::s'\n", errout.str());
|
2010-01-02 17:29:55 +01:00
|
|
|
|
|
|
|
check("struct Fred\n"
|
|
|
|
"{\n"
|
|
|
|
" const char *s;\n"
|
|
|
|
" Fred();\n"
|
|
|
|
"};\n"
|
|
|
|
"Fred::Fred() : s(NULL)\n"
|
|
|
|
"{ }");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
check("struct Fred\n"
|
|
|
|
"{\n"
|
|
|
|
" const char *s;\n"
|
|
|
|
" Fred();\n"
|
|
|
|
"};\n"
|
|
|
|
"Fred::Fred()\n"
|
|
|
|
"{ s = NULL; }");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
check("struct Fred\n"
|
|
|
|
"{\n"
|
|
|
|
" const char *s;\n"
|
|
|
|
" Fred();\n"
|
|
|
|
"};\n"
|
|
|
|
"Fred::Fred()\n"
|
|
|
|
"{ }");
|
2010-10-17 14:41:00 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:6]: (warning) Member variable not initialized in the constructor 'Fred::s'\n", errout.str());
|
2009-03-12 21:33:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-17 19:27:16 +02:00
|
|
|
void initvar_staticvar()
|
|
|
|
{
|
|
|
|
check("class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" Fred() { }\n"
|
|
|
|
" static void *p;\n"
|
|
|
|
"};\n");
|
2009-05-18 22:30:43 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-05-17 19:27:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-12-18 22:28:57 +01:00
|
|
|
void initvar_private_constructor()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
check("class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"private:\n"
|
|
|
|
" int var;\n"
|
|
|
|
" Fred();\n"
|
|
|
|
"};\n"
|
|
|
|
"Fred::Fred()\n"
|
|
|
|
"{ }");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2010-07-14 19:00:52 +02:00
|
|
|
void initvar_copy_constructor() // ticket #1611
|
|
|
|
{
|
|
|
|
check("class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"private:\n"
|
|
|
|
" std::string var;\n"
|
|
|
|
"public:\n"
|
|
|
|
" Fred() { };\n"
|
|
|
|
" Fred(const Fred &) { };\n"
|
|
|
|
"};");
|
2010-10-17 14:41:00 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:7]: (warning) Member variable not initialized in the constructor 'Fred::var'\n", errout.str());
|
2010-07-14 19:00:52 +02:00
|
|
|
|
|
|
|
check("class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"private:\n"
|
|
|
|
" std::string var;\n"
|
|
|
|
"public:\n"
|
|
|
|
" Fred();\n"
|
|
|
|
" Fred(const Fred &);\n"
|
|
|
|
"};\n"
|
|
|
|
"Fred::Fred() { };\n"
|
|
|
|
"Fred::Fred(const Fred &) { };\n");
|
2010-10-17 14:41:00 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:10]: (warning) Member variable not initialized in the constructor 'Fred::var'\n", errout.str());
|
2010-07-14 19:00:52 +02:00
|
|
|
}
|
|
|
|
|
2010-07-15 10:16:16 +02:00
|
|
|
void initvar_nested_constructor() // ticket #1375
|
|
|
|
{
|
|
|
|
check("class A {\n"
|
|
|
|
"public:\n"
|
|
|
|
" A();\n"
|
|
|
|
" struct B {\n"
|
2010-12-02 07:35:01 +01:00
|
|
|
" B(int x);\n"
|
2010-07-15 10:16:16 +02:00
|
|
|
" struct C {\n"
|
2010-12-02 07:35:01 +01:00
|
|
|
" C(int y);\n"
|
2010-07-15 10:16:16 +02:00
|
|
|
" struct D {\n"
|
|
|
|
" int d;\n"
|
2010-12-02 07:35:01 +01:00
|
|
|
" D(int z);\n"
|
2010-07-15 10:16:16 +02:00
|
|
|
" };\n"
|
|
|
|
" int c;\n"
|
|
|
|
" };\n"
|
|
|
|
" int b;\n"
|
|
|
|
" };\n"
|
|
|
|
"private:\n"
|
|
|
|
" int a;\n"
|
|
|
|
" B b;\n"
|
|
|
|
"};\n"
|
|
|
|
"A::A(){}\n"
|
2010-12-02 07:35:01 +01:00
|
|
|
"A::B::B(int x){}\n"
|
|
|
|
"A::B::C::C(int y){}\n"
|
|
|
|
"A::B::C::D::D(int z){}\n");
|
2010-10-17 14:41:00 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:20]: (warning) Member variable not initialized in the constructor 'A::a'\n"
|
2010-12-02 07:35:01 +01:00
|
|
|
"[test.cpp:20]: (warning) Member variable not initialized in the constructor 'A::b'\n"
|
2010-10-17 14:41:00 +02:00
|
|
|
"[test.cpp:21]: (warning) Member variable not initialized in the constructor 'B::b'\n"
|
|
|
|
"[test.cpp:22]: (warning) Member variable not initialized in the constructor 'C::c'\n"
|
|
|
|
"[test.cpp:23]: (warning) Member variable not initialized in the constructor 'D::d'\n", errout.str());
|
2010-07-18 20:43:51 +02:00
|
|
|
|
|
|
|
check("class A {\n"
|
|
|
|
"public:\n"
|
|
|
|
" A();\n"
|
|
|
|
" struct B {\n"
|
2010-12-02 07:35:01 +01:00
|
|
|
" B(int x);\n"
|
2010-07-18 20:43:51 +02:00
|
|
|
" struct C {\n"
|
2010-12-02 07:35:01 +01:00
|
|
|
" C(int y);\n"
|
2010-07-18 20:43:51 +02:00
|
|
|
" struct D {\n"
|
|
|
|
" D(const D &);\n"
|
|
|
|
" int d;\n"
|
|
|
|
" };\n"
|
|
|
|
" int c;\n"
|
|
|
|
" };\n"
|
|
|
|
" int b;\n"
|
|
|
|
" };\n"
|
|
|
|
"private:\n"
|
|
|
|
" int a;\n"
|
|
|
|
" B b;\n"
|
|
|
|
"};\n"
|
|
|
|
"A::A(){}\n"
|
2010-12-02 07:35:01 +01:00
|
|
|
"A::B::B(int x){}\n"
|
|
|
|
"A::B::C::C(int y){}\n"
|
2010-07-18 20:43:51 +02:00
|
|
|
"A::B::C::D::D(const A::B::C::D & d){}\n");
|
2010-10-17 14:41:00 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:20]: (warning) Member variable not initialized in the constructor 'A::a'\n"
|
2010-12-02 07:35:01 +01:00
|
|
|
"[test.cpp:20]: (warning) Member variable not initialized in the constructor 'A::b'\n"
|
2010-10-17 14:41:00 +02:00
|
|
|
"[test.cpp:21]: (warning) Member variable not initialized in the constructor 'B::b'\n"
|
|
|
|
"[test.cpp:22]: (warning) Member variable not initialized in the constructor 'C::c'\n"
|
|
|
|
"[test.cpp:23]: (warning) Member variable not initialized in the constructor 'D::d'\n", errout.str());
|
2010-07-18 20:43:51 +02:00
|
|
|
|
|
|
|
check("class A {\n"
|
|
|
|
"public:\n"
|
|
|
|
" A();\n"
|
|
|
|
" struct B {\n"
|
2010-12-02 07:35:01 +01:00
|
|
|
" B(int x);\n"
|
2010-07-18 20:43:51 +02:00
|
|
|
" struct C {\n"
|
2010-12-02 07:35:01 +01:00
|
|
|
" C(int y);\n"
|
2010-07-18 20:43:51 +02:00
|
|
|
" struct D {\n"
|
2010-12-02 07:35:01 +01:00
|
|
|
" struct E { int e; };\n"
|
2010-07-26 16:46:37 +02:00
|
|
|
" struct E d;\n"
|
2010-07-18 20:43:51 +02:00
|
|
|
" D(const E &);\n"
|
|
|
|
" };\n"
|
|
|
|
" int c;\n"
|
|
|
|
" };\n"
|
|
|
|
" int b;\n"
|
|
|
|
" };\n"
|
|
|
|
"private:\n"
|
|
|
|
" int a;\n"
|
|
|
|
" B b;\n"
|
|
|
|
"};\n"
|
|
|
|
"A::A(){}\n"
|
2010-12-02 07:35:01 +01:00
|
|
|
"A::B::B(int x){}\n"
|
|
|
|
"A::B::C::C(int y){}\n"
|
2010-07-18 20:43:51 +02:00
|
|
|
"A::B::C::D::D(const A::B::C::D::E & e){}\n");
|
2010-10-17 14:41:00 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:21]: (warning) Member variable not initialized in the constructor 'A::a'\n"
|
2010-12-02 07:35:01 +01:00
|
|
|
"[test.cpp:21]: (warning) Member variable not initialized in the constructor 'A::b'\n"
|
2010-10-17 14:41:00 +02:00
|
|
|
"[test.cpp:22]: (warning) Member variable not initialized in the constructor 'B::b'\n"
|
|
|
|
"[test.cpp:23]: (warning) Member variable not initialized in the constructor 'C::c'\n"
|
|
|
|
"[test.cpp:24]: (warning) Member variable not initialized in the constructor 'D::d'\n", errout.str());
|
2010-07-15 10:16:16 +02:00
|
|
|
}
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
void initvar_destructor()
|
|
|
|
{
|
|
|
|
check("class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"private:\n"
|
|
|
|
" int var;\n"
|
|
|
|
"public:\n"
|
|
|
|
" Fred() : var(0) {}\n"
|
|
|
|
" ~Fred() {}\n"
|
|
|
|
"};\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-01-05 16:49:57 +01:00
|
|
|
}
|
2009-04-28 20:50:10 +02:00
|
|
|
|
|
|
|
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"
|
2009-05-01 12:01:55 +02:00
|
|
|
"{ }", true);
|
2010-10-17 14:41:00 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:13]: (warning) Member variable 'Fred::ints' is not assigned a value in 'Fred::operator='\n", errout.str());
|
2009-04-28 20:50:10 +02:00
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
};
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
REGISTER_TEST(TestConstructors)
|