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
|
2009-05-30 07:48:12 +02:00
|
|
|
* Copyright (C) 2007-2009 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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-01-06 15:18:36 +01:00
|
|
|
#include "../src/tokenize.h"
|
|
|
|
#include "../src/checkclass.h"
|
2008-12-18 22:28:57 +01:00
|
|
|
#include "testsuite.h"
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
extern std::ostringstream errout;
|
|
|
|
|
|
|
|
class TestClass : public TestFixture
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TestClass() : TestFixture("TestClass")
|
|
|
|
{ }
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void run()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
TEST_CASE(virtualDestructor1); // Base class not found => no error
|
|
|
|
TEST_CASE(virtualDestructor2); // Base class doesn't have a destructor
|
|
|
|
TEST_CASE(virtualDestructor3); // Base class has a destructor, but it's not virtual
|
|
|
|
TEST_CASE(virtualDestructor4); // Derived class doesn't have a destructor => no error
|
|
|
|
TEST_CASE(virtualDestructor5); // Derived class has empty destructor => no error
|
2009-05-06 22:22:26 +02:00
|
|
|
TEST_CASE(virtualDestructorProtected);
|
2009-01-14 20:34:10 +01:00
|
|
|
|
|
|
|
TEST_CASE(uninitVar1);
|
2009-02-09 08:47:41 +01:00
|
|
|
TEST_CASE(uninitVarEnum);
|
2009-01-17 21:17:57 +01:00
|
|
|
TEST_CASE(uninitVarStream);
|
2009-06-05 02:34:12 +02:00
|
|
|
TEST_CASE(uninitVarTypedef);
|
2009-02-09 08:47:41 +01:00
|
|
|
TEST_CASE(privateCtor1); // If constructor is private..
|
2009-03-25 18:31:40 +01:00
|
|
|
TEST_CASE(privateCtor2); // If constructor is private..
|
2009-02-04 20:42:40 +01:00
|
|
|
TEST_CASE(function); // Function is not variable
|
2009-02-09 08:47:41 +01:00
|
|
|
TEST_CASE(uninitVarHeader1); // Class is defined in header
|
|
|
|
TEST_CASE(uninitVarHeader2); // Class is defined in header
|
2009-02-21 09:24:57 +01:00
|
|
|
TEST_CASE(uninitVarHeader3); // Class is defined in header
|
2009-02-21 14:35:55 +01:00
|
|
|
|
|
|
|
TEST_CASE(noConstructor1);
|
|
|
|
TEST_CASE(noConstructor2);
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that base classes have virtual destructors
|
|
|
|
void checkVirtualDestructor(const char code[])
|
|
|
|
{
|
|
|
|
// Tokenize..
|
|
|
|
Tokenizer tokenizer;
|
|
|
|
std::istringstream istr(code);
|
2009-01-05 16:49:57 +01:00
|
|
|
tokenizer.tokenize(istr, "test.cpp");
|
2008-12-18 22:28:57 +01:00
|
|
|
tokenizer.simplifyTokenList();
|
|
|
|
|
|
|
|
// Clear the error log
|
|
|
|
errout.str("");
|
|
|
|
|
|
|
|
// Check..
|
|
|
|
Settings settings;
|
2009-03-20 17:30:24 +01:00
|
|
|
CheckClass checkClass(&tokenizer, &settings, this);
|
2008-12-18 22:28:57 +01:00
|
|
|
checkClass.virtualDestructor();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void virtualDestructor1()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
// Base class not found
|
2008-12-18 22:28:57 +01:00
|
|
|
|
|
|
|
checkVirtualDestructor("class Derived : public Base { };");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
|
|
|
|
checkVirtualDestructor("class Derived : Base { };");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void virtualDestructor2()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
// Base class doesn't have a destructor
|
2008-12-18 22:28:57 +01:00
|
|
|
|
|
|
|
checkVirtualDestructor("class Base { };\n"
|
|
|
|
"class Derived : public Base { public: ~Derived() { (void)11; } };");
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:1]: (error) Class Base which is inherited by class Derived does not have a virtual destructor\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
|
|
|
|
checkVirtualDestructor("class Base { };\n"
|
|
|
|
"class Derived : Base { public: ~Derived() { (void)11; } };");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void virtualDestructor3()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
// Base class has a destructor, but it's not virtual
|
2008-12-18 22:28:57 +01:00
|
|
|
|
|
|
|
checkVirtualDestructor("class Base { public: ~Base(); };\n"
|
|
|
|
"class Derived : public Base { public: ~Derived() { (void)11; } };");
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:1]: (error) Class Base which is inherited by class Derived does not have a virtual destructor\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
|
|
|
|
checkVirtualDestructor("class Base { public: ~Base(); };\n"
|
|
|
|
"class Derived : private Fred, public Base { public: ~Derived() { (void)11; } };");
|
2009-05-31 21:48:55 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:1]: (error) Class Base which is inherited by class Derived does not have a virtual destructor\n", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void virtualDestructor4()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
// Derived class doesn't have a destructor => no error
|
2008-12-18 22:28:57 +01:00
|
|
|
|
|
|
|
checkVirtualDestructor("class Base { public: ~Base(); };\n"
|
|
|
|
"class Derived : public Base { };");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
|
|
|
|
checkVirtualDestructor("class Base { public: ~Base(); };\n"
|
|
|
|
"class Derived : private Fred, public Base { };");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
2008-12-19 21:30:33 +01:00
|
|
|
|
|
|
|
void virtualDestructor5()
|
|
|
|
{
|
2009-01-05 16:49:57 +01:00
|
|
|
// Derived class has empty destructor => no error
|
2008-12-19 21:30:33 +01:00
|
|
|
|
|
|
|
checkVirtualDestructor("class Base { public: ~Base(); };\n"
|
|
|
|
"class Derived : public Base { public: ~Derived() {} };");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-19 21:30:33 +01:00
|
|
|
|
|
|
|
checkVirtualDestructor("class Base { public: ~Base(); };\n"
|
|
|
|
"class Derived : public Base { public: ~Derived(); }; Derived::~Derived() {}");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2008-12-19 21:30:33 +01:00
|
|
|
}
|
2009-01-14 20:34:10 +01:00
|
|
|
|
2009-05-06 22:22:26 +02:00
|
|
|
void virtualDestructorProtected()
|
|
|
|
{
|
|
|
|
// Base class has protected destructor, it makes Base *p = new Derived(); fail
|
|
|
|
// during compilation time, so error is not possible. => no error
|
|
|
|
checkVirtualDestructor("class A\n"
|
|
|
|
"{\n"
|
|
|
|
"protected:\n"
|
|
|
|
" ~A() { }\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"class B : public A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" ~B() { int a; }\n"
|
|
|
|
"};\n");
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-05-06 22:22:26 +02:00
|
|
|
}
|
2009-01-14 20:34:10 +01:00
|
|
|
|
|
|
|
void checkUninitVar(const char code[])
|
|
|
|
{
|
|
|
|
// Tokenize..
|
|
|
|
Tokenizer tokenizer;
|
|
|
|
std::istringstream istr(code);
|
|
|
|
tokenizer.tokenize(istr, "test.cpp");
|
|
|
|
tokenizer.simplifyTokenList();
|
|
|
|
|
|
|
|
// Clear the error log
|
|
|
|
errout.str("");
|
|
|
|
|
|
|
|
// Check..
|
|
|
|
Settings settings;
|
2009-03-20 17:30:24 +01:00
|
|
|
CheckClass checkClass(&tokenizer, &settings, this);
|
2009-01-14 20:34:10 +01:00
|
|
|
checkClass.constructors();
|
|
|
|
}
|
|
|
|
|
|
|
|
void uninitVar1()
|
|
|
|
{
|
|
|
|
checkUninitVar("enum ECODES\n"
|
|
|
|
"{\n"
|
|
|
|
" CODE_1 = 0,\n"
|
|
|
|
" CODE_2 = 1\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" Fred() {}\n"
|
|
|
|
"\n"
|
|
|
|
"private:\n"
|
|
|
|
" ECODES _code;\n"
|
2009-01-14 20:43:28 +01:00
|
|
|
"};\n");
|
2009-01-14 20:34:10 +01:00
|
|
|
|
2009-02-14 09:52:03 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:10]: (style) Member variable not initialized in the constructor 'Fred::_code'\n", errout.str());
|
2009-07-03 20:22:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
checkUninitVar("class A{};\n"
|
|
|
|
"\n"
|
|
|
|
"class B : public A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" B() {}\n"
|
|
|
|
"private:\n"
|
|
|
|
" float f;\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:6]: (style) Member variable not initialized in the constructor 'B::f'\n", errout.str());
|
2009-07-17 18:50:49 +02:00
|
|
|
|
|
|
|
checkUninitVar("class C\n"
|
|
|
|
"{\n"
|
|
|
|
" FILE *fp;\n"
|
|
|
|
"\n"
|
|
|
|
"public:\n"
|
|
|
|
" C(FILE *fp);\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"C::C(FILE *fp) {\n"
|
|
|
|
" C::fp = fp;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-01-14 20:34:10 +01:00
|
|
|
}
|
|
|
|
|
2009-02-09 08:47:41 +01:00
|
|
|
void uninitVarEnum()
|
|
|
|
{
|
|
|
|
checkUninitVar("class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" enum abc {a,b,c};\n"
|
|
|
|
" Fred() {}\n"
|
|
|
|
"private:\n"
|
|
|
|
" unsigned int i;\n"
|
|
|
|
"};\n");
|
|
|
|
|
2009-02-14 09:52:03 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (style) Member variable not initialized in the constructor 'Fred::i'\n", errout.str());
|
2009-02-09 08:47:41 +01:00
|
|
|
}
|
|
|
|
|
2009-01-17 20:26:58 +01:00
|
|
|
void uninitVarStream()
|
|
|
|
{
|
|
|
|
checkUninitVar("#include <fstream>\n"
|
2009-02-09 08:47:41 +01:00
|
|
|
"class Foo\n"
|
|
|
|
"{\n"
|
|
|
|
"private:\n"
|
|
|
|
" int foo;\n"
|
2009-01-17 20:26:58 +01:00
|
|
|
"public:\n"
|
2009-02-09 08:47:41 +01:00
|
|
|
" Foo(std::istream &in)\n"
|
|
|
|
" {\n"
|
|
|
|
" if(!(in >> foo))\n"
|
|
|
|
" throw 0;\n"
|
|
|
|
" }\n"
|
2009-01-17 20:26:58 +01:00
|
|
|
"};\n");
|
|
|
|
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-01-17 20:26:58 +01:00
|
|
|
}
|
|
|
|
|
2009-06-05 02:34:12 +02:00
|
|
|
void uninitVarTypedef()
|
|
|
|
{
|
|
|
|
checkUninitVar("class Foo\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" typedef int * pointer;\n"
|
|
|
|
" Foo() : a(0) {}\n"
|
|
|
|
" pointer a;\n"
|
|
|
|
"};\n");
|
|
|
|
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
2009-01-27 18:39:06 +01:00
|
|
|
|
2009-02-09 08:47:41 +01:00
|
|
|
void privateCtor1()
|
2009-01-27 18:39:06 +01:00
|
|
|
{
|
|
|
|
checkUninitVar("class Foo {\n"
|
|
|
|
" int foo;\n"
|
|
|
|
" Foo() { }\n"
|
|
|
|
"};\n");
|
|
|
|
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-01-27 18:39:06 +01:00
|
|
|
}
|
|
|
|
|
2009-02-09 08:47:41 +01:00
|
|
|
void privateCtor2()
|
|
|
|
{
|
|
|
|
checkUninitVar("class Foo\n"
|
|
|
|
"{\n"
|
|
|
|
"private:\n"
|
|
|
|
" int foo;\n"
|
|
|
|
" Foo() { }\n"
|
|
|
|
"public:\n"
|
|
|
|
" Foo(int _i) { }\n"
|
|
|
|
"};\n");
|
|
|
|
|
2009-05-31 21:48:55 +02:00
|
|
|
TODO_ASSERT_EQUALS("[test.cpp:7] (style) Member variable not initialized in the constructor Foo::foo", errout.str());
|
2009-02-09 08:47:41 +01:00
|
|
|
}
|
|
|
|
|
2009-01-27 18:39:06 +01:00
|
|
|
|
2009-02-04 20:31:25 +01:00
|
|
|
void function()
|
|
|
|
{
|
|
|
|
checkUninitVar("class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" A();\n"
|
|
|
|
" int* f(int*);\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"A::A()\n"
|
|
|
|
"{\n"
|
|
|
|
"}\n"
|
|
|
|
"\n"
|
|
|
|
"int* A::f(int* p)\n"
|
|
|
|
"{\n"
|
|
|
|
" return p;\n"
|
|
|
|
"}\n");
|
|
|
|
|
2009-06-05 02:39:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-02-04 20:31:25 +01:00
|
|
|
}
|
2009-01-27 18:39:06 +01:00
|
|
|
|
2009-02-09 08:47:41 +01:00
|
|
|
|
|
|
|
void uninitVarHeader1()
|
|
|
|
{
|
|
|
|
checkUninitVar("#file \"fred.h\"\n"
|
|
|
|
"class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"private:\n"
|
|
|
|
" unsigned int i;\n"
|
|
|
|
"public:\n"
|
|
|
|
" Fred();\n"
|
|
|
|
"};\n"
|
|
|
|
"#endfile\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void uninitVarHeader2()
|
|
|
|
{
|
|
|
|
checkUninitVar("#file \"fred.h\"\n"
|
|
|
|
"class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"private:\n"
|
|
|
|
" unsigned int i;\n"
|
|
|
|
"public:\n"
|
|
|
|
" Fred() { }\n"
|
|
|
|
"};\n"
|
|
|
|
"#endfile\n");
|
2009-02-14 09:52:03 +01:00
|
|
|
ASSERT_EQUALS("[fred.h:6]: (style) Member variable not initialized in the constructor 'Fred::i'\n", errout.str());
|
2009-02-09 08:47:41 +01:00
|
|
|
}
|
|
|
|
|
2009-02-21 09:24:57 +01:00
|
|
|
void uninitVarHeader3()
|
|
|
|
{
|
|
|
|
checkUninitVar("#file \"fred.h\"\n"
|
|
|
|
"class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"private:\n"
|
|
|
|
" mutable int i;\n"
|
|
|
|
"public:\n"
|
|
|
|
" Fred() { }\n"
|
|
|
|
"};\n"
|
|
|
|
"#endfile\n");
|
|
|
|
ASSERT_EQUALS("[fred.h:6]: (style) Member variable not initialized in the constructor 'Fred::i'\n", errout.str());
|
|
|
|
}
|
|
|
|
|
2009-02-09 08:47:41 +01:00
|
|
|
|
2009-02-21 14:35:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void checkNoConstructor(const char code[])
|
|
|
|
{
|
|
|
|
// Tokenize..
|
|
|
|
Tokenizer tokenizer;
|
|
|
|
std::istringstream istr(code);
|
|
|
|
tokenizer.tokenize(istr, "test.cpp");
|
|
|
|
tokenizer.simplifyTokenList();
|
|
|
|
|
|
|
|
// Clear the error log
|
|
|
|
errout.str("");
|
|
|
|
|
|
|
|
// Check..
|
|
|
|
Settings settings;
|
|
|
|
settings._checkCodingStyle = true;
|
2009-03-20 17:30:24 +01:00
|
|
|
CheckClass checkClass(&tokenizer, &settings, this);
|
2009-02-21 14:35:55 +01:00
|
|
|
checkClass.constructors();
|
|
|
|
}
|
|
|
|
|
|
|
|
void noConstructor1()
|
|
|
|
{
|
|
|
|
// There are nonstatic member variables - constructor is needed
|
|
|
|
checkNoConstructor("class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
" int i;\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:1]: (style) The class 'Fred' has no constructor\n", errout.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void noConstructor2()
|
|
|
|
{
|
|
|
|
checkNoConstructor("class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" static void foobar();\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"void Fred::foobar()\n"
|
|
|
|
"{ }\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void noConstructor3()
|
|
|
|
{
|
|
|
|
checkNoConstructor("class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" static int foobar;\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-18 22:28:57 +01:00
|
|
|
};
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
REGISTER_TEST(TestClass)
|