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
|
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 TestClass : public TestFixture
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TestClass() : TestFixture("TestClass")
|
|
|
|
{ }
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void run()
|
|
|
|
{
|
2009-08-24 17:15:29 +02:00
|
|
|
TEST_CASE(virtualDestructor1); // Base class not found => no error
|
2009-01-05 16:49:57 +01:00
|
|
|
TEST_CASE(virtualDestructor2); // Base class doesn't have a destructor
|
2009-08-24 17:15:29 +02:00
|
|
|
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-08-04 21:23:22 +02:00
|
|
|
TEST_CASE(virtualDestructorInherited);
|
2009-08-10 16:58:13 +02:00
|
|
|
TEST_CASE(virtualDestructorTemplate);
|
2009-01-14 20:34:10 +01:00
|
|
|
|
|
|
|
TEST_CASE(uninitVar1);
|
2009-12-11 21:34:04 +01:00
|
|
|
TEST_CASE(uninitVar2);
|
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-12-13 09:35:08 +01:00
|
|
|
TEST_CASE(uninitVarArray1);
|
|
|
|
TEST_CASE(uninitVarArray2);
|
2009-09-03 22:28:00 +02:00
|
|
|
TEST_CASE(uninitMissingFuncDef);// can't expand function in constructor
|
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-08-31 19:40:49 +02:00
|
|
|
TEST_CASE(uninitVarPublished); // Variables in the published section are auto-initialized
|
|
|
|
|
2009-02-21 14:35:55 +01:00
|
|
|
TEST_CASE(noConstructor1);
|
|
|
|
TEST_CASE(noConstructor2);
|
2009-12-19 17:58:52 +01:00
|
|
|
TEST_CASE(noConstructor3);
|
|
|
|
TEST_CASE(noConstructor4);
|
2009-08-09 19:57:48 +02:00
|
|
|
|
|
|
|
TEST_CASE(operatorEq1);
|
2009-12-31 13:44:03 +01:00
|
|
|
TEST_CASE(operatorEqRetRefThis);
|
2010-01-03 08:26:02 +01:00
|
|
|
TEST_CASE(operatorEqToSelf1); // single class
|
|
|
|
TEST_CASE(operatorEqToSelf2); // nested class
|
|
|
|
TEST_CASE(operatorEqToSelf3); // multiple inheritance
|
|
|
|
TEST_CASE(operatorEqToSelf4); // nested class with multiple inheritance
|
2009-09-02 22:32:15 +02:00
|
|
|
TEST_CASE(memsetOnStruct);
|
|
|
|
TEST_CASE(memsetOnClass);
|
2009-09-12 15:25:02 +02:00
|
|
|
|
|
|
|
TEST_CASE(this_subtraction); // warn about "this-x"
|
2009-08-09 19:57:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the operator Equal
|
|
|
|
void checkOpertorEq(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;
|
|
|
|
CheckClass checkClass(&tokenizer, &settings, this);
|
|
|
|
checkClass.operatorEq();
|
|
|
|
}
|
|
|
|
|
|
|
|
void operatorEq1()
|
|
|
|
{
|
|
|
|
checkOpertorEq("class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
2009-08-24 17:15:29 +02:00
|
|
|
" void goo() {}"
|
2009-12-31 13:44:03 +01:00
|
|
|
" void operator=(const A&);\n"
|
2009-08-09 19:57:48 +02:00
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (style) 'operator=' should return something\n", errout.str());
|
|
|
|
|
|
|
|
checkOpertorEq("class A\n"
|
|
|
|
"{\n"
|
|
|
|
"private:\n"
|
2009-12-31 13:44:03 +01:00
|
|
|
" void operator=(const A&);\n"
|
2009-08-09 19:57:48 +02:00
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
checkOpertorEq("class A\n"
|
|
|
|
"{\n"
|
2009-12-31 13:44:03 +01:00
|
|
|
" void operator=(const A&);\n"
|
2009-08-09 19:57:48 +02:00
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
checkOpertorEq("class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" void goo() {}\n"
|
|
|
|
"private:\n"
|
2009-12-31 13:44:03 +01:00
|
|
|
" void operator=(const A&);\n"
|
2009-08-09 19:57:48 +02:00
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2009-12-29 07:48:37 +01:00
|
|
|
checkOpertorEq("class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
2009-12-31 13:44:03 +01:00
|
|
|
" void operator=(const A&);\n"
|
2009-12-29 07:48:37 +01:00
|
|
|
"};\n"
|
|
|
|
"class B\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
2009-12-31 13:44:03 +01:00
|
|
|
" void operator=(const B&);\n"
|
2009-12-29 07:48:37 +01:00
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (style) 'operator=' should return something\n"
|
|
|
|
"[test.cpp:9]: (style) 'operator=' should return something\n", errout.str());
|
|
|
|
|
2009-12-30 18:40:02 +01:00
|
|
|
checkOpertorEq("struct A\n"
|
|
|
|
"{\n"
|
2009-12-31 13:44:03 +01:00
|
|
|
" void operator=(const A&);\n"
|
2009-12-30 18:40:02 +01:00
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (style) 'operator=' should return something\n", errout.str());
|
|
|
|
|
2008-12-18 22:28:57 +01:00
|
|
|
}
|
|
|
|
|
2009-12-31 13:44:03 +01:00
|
|
|
// Check that operator Equal returns reference to this
|
|
|
|
void checkOpertorEqRetRefThis(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;
|
|
|
|
CheckClass checkClass(&tokenizer, &settings, this);
|
|
|
|
checkClass.operatorEqRetRefThis();
|
|
|
|
}
|
|
|
|
|
|
|
|
void operatorEqRetRefThis()
|
|
|
|
{
|
|
|
|
checkOpertorEqRetRefThis(
|
|
|
|
"class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" A & operator=(const A &a) { return *this; }\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
checkOpertorEqRetRefThis(
|
|
|
|
"class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" A & operator=(const A &a) { return a; }\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (style) 'operator=' should return reference to self\n", errout.str());
|
|
|
|
|
|
|
|
checkOpertorEqRetRefThis(
|
|
|
|
"class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" A & operator=(const A &);\n"
|
|
|
|
"};\n"
|
|
|
|
"A & A::operator=(const A &a) { return *this; }\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
checkOpertorEqRetRefThis(
|
|
|
|
"class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" A & operator=(const A &a);\n"
|
|
|
|
"};\n"
|
|
|
|
"A & A::operator=(const A &a) { return *this; }\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
checkOpertorEqRetRefThis(
|
|
|
|
"class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" A & operator=(const A &)\n"
|
|
|
|
"};\n"
|
|
|
|
"A & A::operator=(const A &a) { return a; }\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:6]: (style) 'operator=' should return reference to self\n", errout.str());
|
|
|
|
|
|
|
|
checkOpertorEqRetRefThis(
|
|
|
|
"class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" A & operator=(const A &a)\n"
|
|
|
|
"};\n"
|
|
|
|
"A & A::operator=(const A &a) { return a; }\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:6]: (style) 'operator=' should return reference to self\n", errout.str());
|
|
|
|
|
|
|
|
checkOpertorEqRetRefThis(
|
|
|
|
"class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" class B\n"
|
|
|
|
" {\n"
|
|
|
|
" public:\n"
|
|
|
|
" B & operator=(const B &b) { return *this; }\n"
|
|
|
|
" };\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
checkOpertorEqRetRefThis(
|
|
|
|
"class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" class B\n"
|
|
|
|
" {\n"
|
|
|
|
" public:\n"
|
|
|
|
" B & operator=(const B &b) { return b; }\n"
|
|
|
|
" };\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:7]: (style) 'operator=' should return reference to self\n", errout.str());
|
|
|
|
|
|
|
|
checkOpertorEqRetRefThis(
|
|
|
|
"class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" class B\n"
|
|
|
|
" {\n"
|
|
|
|
" public:\n"
|
|
|
|
" B & operator=(const B &);\n"
|
|
|
|
" };\n"
|
|
|
|
"};\n"
|
|
|
|
"A::B & A::B::operator=(const A::B &b) { return *this; }\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
checkOpertorEqRetRefThis(
|
|
|
|
"class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" class B\n"
|
|
|
|
" {\n"
|
|
|
|
" public:\n"
|
|
|
|
" B & operator=(const B &);\n"
|
|
|
|
" };\n"
|
|
|
|
"};\n"
|
|
|
|
"A::B & A::B::operator=(const A::B &b) { return b; }\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:10]: (style) 'operator=' should return reference to self\n", errout.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that operator Equal checks for assignment to self
|
|
|
|
void checkOpertorEqToSelf(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;
|
|
|
|
settings._showAll = true;
|
|
|
|
CheckClass checkClass(&tokenizer, &settings, this);
|
|
|
|
checkClass.operatorEqToSelf();
|
|
|
|
}
|
|
|
|
|
2010-01-03 08:26:02 +01:00
|
|
|
void operatorEqToSelf1()
|
2009-12-31 13:44:03 +01:00
|
|
|
{
|
2010-01-03 08:26:02 +01:00
|
|
|
// this test has an assignment test but it is not needed
|
2009-12-31 13:44:03 +01:00
|
|
|
checkOpertorEqToSelf(
|
|
|
|
"class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
2010-01-03 08:26:02 +01:00
|
|
|
" A & operator=(const A &a) { if (&a != this) { } return *this; }\n"
|
2009-12-31 13:44:03 +01:00
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2010-01-03 08:26:02 +01:00
|
|
|
// this test doesn't have an assignment test but it is not needed
|
2009-12-31 13:44:03 +01:00
|
|
|
checkOpertorEqToSelf(
|
|
|
|
"class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" A & operator=(const A &a) { return *this; }\n"
|
|
|
|
"};\n");
|
2010-01-03 08:26:02 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-12-31 14:03:35 +01:00
|
|
|
|
2010-01-03 08:26:02 +01:00
|
|
|
// this test needs an assignment test and has it
|
|
|
|
checkOpertorEqToSelf(
|
|
|
|
"class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" char *s;\n"
|
|
|
|
" A & operator=(const A &a)\n"
|
|
|
|
" {\n"
|
|
|
|
" if (&a != this)\n"
|
|
|
|
" {\n"
|
|
|
|
" free(s);\n"
|
|
|
|
" s = strdup(a.s);\n"
|
|
|
|
" }\n"
|
|
|
|
" return *this;\n"
|
|
|
|
" }\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// this class needs an assignment test but doesn't have it
|
2009-12-31 14:03:35 +01:00
|
|
|
checkOpertorEqToSelf(
|
|
|
|
"class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" char *s;\n"
|
|
|
|
" A & operator=(const A &a)\n"
|
|
|
|
" {\n"
|
|
|
|
" free(s);\n"
|
|
|
|
" s = strdup(a.s);\n"
|
|
|
|
" return *this;\n"
|
|
|
|
" }\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (possible style) 'operator=' should check for assignment to self\n", errout.str());
|
2009-12-31 13:44:03 +01:00
|
|
|
|
2010-01-03 08:26:02 +01:00
|
|
|
// this test has an assignment test but doesn't need it
|
2009-12-31 13:44:03 +01:00
|
|
|
checkOpertorEqToSelf(
|
|
|
|
"class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" A & operator=(const A &);\n"
|
|
|
|
"};\n"
|
2010-01-03 08:26:02 +01:00
|
|
|
"A & A::operator=(const A &a) { if (&a != this) { } return *this; }\n");
|
2009-12-31 13:44:03 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2010-01-03 08:26:02 +01:00
|
|
|
// this test doesn't have an assignment test but doesn't need it
|
2009-12-31 13:44:03 +01:00
|
|
|
checkOpertorEqToSelf(
|
|
|
|
"class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
2010-01-03 08:26:02 +01:00
|
|
|
" A & operator=(const A &)\n"
|
2009-12-31 13:44:03 +01:00
|
|
|
"};\n"
|
2010-01-03 08:26:02 +01:00
|
|
|
"A & A::operator=(const A &a) { return *this; }\n");
|
2009-12-31 13:44:03 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2010-01-03 08:26:02 +01:00
|
|
|
// this test needs an assignment test and has it
|
2009-12-31 13:44:03 +01:00
|
|
|
checkOpertorEqToSelf(
|
|
|
|
"class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
2010-01-03 08:26:02 +01:00
|
|
|
" char *s;\n"
|
|
|
|
" A & operator=(const A &);\n"
|
2009-12-31 13:44:03 +01:00
|
|
|
"};\n"
|
2010-01-03 08:26:02 +01:00
|
|
|
"A & operator=(const A &a)\n"
|
|
|
|
"{\n"
|
|
|
|
" if (&a != this)\n"
|
|
|
|
" {\n"
|
|
|
|
" free(s);\n"
|
|
|
|
" s = strdup(a.s);\n"
|
|
|
|
" }\n"
|
|
|
|
" return *this;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-12-31 13:44:03 +01:00
|
|
|
|
2010-01-03 08:26:02 +01:00
|
|
|
// this test needs an assignment test but doesnt have it
|
2009-12-31 13:44:03 +01:00
|
|
|
checkOpertorEqToSelf(
|
|
|
|
"class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
2010-01-03 08:26:02 +01:00
|
|
|
" char *s;\n"
|
|
|
|
" A & operator=(const A &);\n"
|
2009-12-31 13:44:03 +01:00
|
|
|
"};\n"
|
2010-01-03 08:26:02 +01:00
|
|
|
"A & operator=(const A &a)\n"
|
|
|
|
"{\n"
|
|
|
|
" free(s);\n"
|
|
|
|
" s = strdup(a.s);\n"
|
|
|
|
" return *this;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:7]: (possible style) 'operator=' should check for assignment to self\n", errout.str());
|
|
|
|
}
|
2009-12-31 13:44:03 +01:00
|
|
|
|
2010-01-03 08:26:02 +01:00
|
|
|
void operatorEqToSelf2()
|
|
|
|
{
|
|
|
|
// this test has an assignment test but doesn't need it
|
2009-12-31 13:44:03 +01:00
|
|
|
checkOpertorEqToSelf(
|
|
|
|
"class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" class B\n"
|
|
|
|
" {\n"
|
|
|
|
" public:\n"
|
2010-01-03 08:26:02 +01:00
|
|
|
" B & operator=(const B &b) { if (&b != this) { } return *this; }\n"
|
2009-12-31 13:44:03 +01:00
|
|
|
" };\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2010-01-03 08:26:02 +01:00
|
|
|
// this test doesn't have an assignment test but doesn't need it
|
2009-12-31 13:44:03 +01:00
|
|
|
checkOpertorEqToSelf(
|
|
|
|
"class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" class B\n"
|
|
|
|
" {\n"
|
|
|
|
" public:\n"
|
2010-01-03 08:26:02 +01:00
|
|
|
" B & operator=(const B &b) { return *this; }\n"
|
2009-12-31 13:44:03 +01:00
|
|
|
" };\n"
|
|
|
|
"};\n");
|
2010-01-03 08:26:02 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-12-31 13:44:03 +01:00
|
|
|
|
2010-01-03 08:26:02 +01:00
|
|
|
// this test needs an assignment test but has it
|
|
|
|
checkOpertorEqToSelf(
|
|
|
|
"class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" class B\n"
|
|
|
|
" {\n"
|
|
|
|
" public:\n"
|
|
|
|
" char *s;\n"
|
|
|
|
" B & operator=(const B &b)\n"
|
|
|
|
" {\n"
|
|
|
|
" if (&b != this)\n"
|
|
|
|
" {\n"
|
|
|
|
" }\n"
|
|
|
|
" return *this;\n"
|
|
|
|
" }\n"
|
|
|
|
" };\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// this test needs an assignment test but doesn't have it
|
|
|
|
checkOpertorEqToSelf(
|
|
|
|
"class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" class B\n"
|
|
|
|
" {\n"
|
|
|
|
" public:\n"
|
|
|
|
" char *s;\n"
|
|
|
|
" B & operator=(const B &b)\n"
|
|
|
|
" {\n"
|
|
|
|
" free(s);\n"
|
|
|
|
" s = strdup(b.s);\n"
|
|
|
|
" return *this;\n"
|
|
|
|
" }\n"
|
|
|
|
" };\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:8]: (possible style) 'operator=' should check for assignment to self\n", errout.str());
|
|
|
|
|
|
|
|
// this test has an assignment test but doesn't need it
|
2009-12-31 13:44:03 +01:00
|
|
|
checkOpertorEqToSelf(
|
|
|
|
"class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" class B\n"
|
|
|
|
" {\n"
|
|
|
|
" public:\n"
|
|
|
|
" B & operator=(const B &);\n"
|
|
|
|
" };\n"
|
|
|
|
"};\n"
|
2010-01-03 08:26:02 +01:00
|
|
|
"A::B & A::B::operator=(const A::B &b) { if (&b != this) { } return *this; }\n");
|
2009-12-31 13:44:03 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2010-01-03 08:26:02 +01:00
|
|
|
// this test doesn't have an assignment test but doesn't need it
|
2009-12-31 13:44:03 +01:00
|
|
|
checkOpertorEqToSelf(
|
|
|
|
"class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" class B\n"
|
|
|
|
" {\n"
|
|
|
|
" public:\n"
|
|
|
|
" B & operator=(const B &);\n"
|
|
|
|
" };\n"
|
|
|
|
"};\n"
|
2010-01-03 08:26:02 +01:00
|
|
|
"A::B & A::B::operator=(const A::B &b) { return *this; }\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// this test needs an assignment test and has it
|
|
|
|
checkOpertorEqToSelf(
|
|
|
|
"class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" class B\n"
|
|
|
|
" {\n"
|
|
|
|
" public:\n"
|
|
|
|
" char * s;\n"
|
|
|
|
" B & operator=(const B &);\n"
|
|
|
|
" };\n"
|
|
|
|
"};\n"
|
|
|
|
"A::B & A::B::operator=(const A::B &b)\n"
|
|
|
|
"{\n"
|
|
|
|
" if (&b != this)\n"
|
|
|
|
" {\n"
|
|
|
|
" free(s);\n"
|
|
|
|
" s = strdup(b.s);\n"
|
|
|
|
" }\n"
|
|
|
|
" return *this;\n"
|
|
|
|
" }\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// this test needs an assignment test but doesn't have it
|
|
|
|
checkOpertorEqToSelf(
|
|
|
|
"class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" class B\n"
|
|
|
|
" {\n"
|
|
|
|
" public:\n"
|
|
|
|
" char * s;\n"
|
|
|
|
" B & operator=(const B &);\n"
|
|
|
|
" };\n"
|
|
|
|
"};\n"
|
|
|
|
"A::B & A::B::operator=(const A::B &b)\n"
|
|
|
|
"{\n"
|
|
|
|
" free(s);\n"
|
|
|
|
" s = strdup(b.s);\n"
|
|
|
|
" return *this;\n"
|
|
|
|
" }\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:11]: (possible style) 'operator=' should check for assignment to self\n", errout.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void operatorEqToSelf3()
|
|
|
|
{
|
|
|
|
// this test has multiple inheritance so there is no trivial way to test for self assignment but doesn't need it
|
|
|
|
checkOpertorEqToSelf(
|
|
|
|
"class A : public B, public C\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" A & operator=(const A &a) { return *this; }\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// this test has multiple inheritance and needs an assignment test but there is no trivial way to test for it
|
|
|
|
checkOpertorEqToSelf(
|
|
|
|
"class A : public B, public C\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" char *s;\n"
|
|
|
|
" A & operator=(const A &a)\n"
|
|
|
|
" {\n"
|
|
|
|
" free(s);\n"
|
|
|
|
" s = strdup(a.s);\n"
|
|
|
|
" return *this;\n"
|
|
|
|
" }\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// this test has multiple inheritance so there is no trivial way to test for self assignment but doesn't need it
|
|
|
|
checkOpertorEqToSelf(
|
|
|
|
"class A : public B, public C\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" A & operator=(const A &);\n"
|
|
|
|
"};\n"
|
|
|
|
"A & A::operator=(const A &a) { return *this; }\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// this test has multiple inheritance and needs an assignment test but there is no trivial way to test for it
|
|
|
|
checkOpertorEqToSelf(
|
|
|
|
"class A : public B, public C\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" char *s;\n"
|
|
|
|
" A & operator=(const A &);\n"
|
|
|
|
"};\n"
|
|
|
|
"A & A::operator=(const A &a)\n"
|
|
|
|
"{\n"
|
|
|
|
" free(s);\n"
|
|
|
|
" s = strdup(a.s);\n"
|
|
|
|
" return *this;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void operatorEqToSelf4()
|
|
|
|
{
|
|
|
|
// this test has multiple inheritance so there is no trivial way to test for self assignment but doesn't need it
|
|
|
|
checkOpertorEqToSelf(
|
|
|
|
"class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" class B : public C, public D\n"
|
|
|
|
" {\n"
|
|
|
|
" public:\n"
|
|
|
|
" B & operator=(const B &b) { return *this; }\n"
|
|
|
|
" };\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// this test has multiple inheritance and needs an assignment test but there is no trivial way to test for it
|
|
|
|
checkOpertorEqToSelf(
|
|
|
|
"class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" class B : public C, public D\n"
|
|
|
|
" {\n"
|
|
|
|
" public:\n"
|
|
|
|
" char * s;\n"
|
|
|
|
" B & operator=(const B &b)\n"
|
|
|
|
" {\n"
|
|
|
|
" free(s);\n"
|
|
|
|
" s = strdup(b.s);\n"
|
|
|
|
" return *this;\n"
|
|
|
|
" }\n"
|
|
|
|
" };\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// this test has multiple inheritance so there is no trivial way to test for self assignment but doesn't need it
|
|
|
|
checkOpertorEqToSelf(
|
|
|
|
"class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" class B : public C, public D\n"
|
|
|
|
" {\n"
|
|
|
|
" public:\n"
|
|
|
|
" B & operator=(const B &);\n"
|
|
|
|
" };\n"
|
|
|
|
"};\n"
|
|
|
|
"A::B & A::B::operator=(const A::B &b) { return *this; }\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// this test has multiple inheritance and needs an assignment test but there is no trivial way to test for it
|
|
|
|
checkOpertorEqToSelf(
|
|
|
|
"class A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" class B : public C, public D\n"
|
|
|
|
" {\n"
|
|
|
|
" public:\n"
|
|
|
|
" char * s;\n"
|
|
|
|
" B & operator=(const B &);\n"
|
|
|
|
" };\n"
|
|
|
|
"};\n"
|
|
|
|
"A::B & A::B::operator=(const A::B &b)\n"
|
|
|
|
"{\n"
|
|
|
|
" free(s);\n"
|
|
|
|
" s = strdup(b.s);\n"
|
|
|
|
" return *this;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-12-31 13:44:03 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2009-08-04 21:23:22 +02:00
|
|
|
void virtualDestructorInherited()
|
|
|
|
{
|
|
|
|
// class A inherits virtual destructor from class Base -> no error
|
|
|
|
checkVirtualDestructor("class Base\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
"virtual ~Base() {}\n"
|
|
|
|
"};\n"
|
|
|
|
"class A : private Base\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" ~A() { }\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"class B : public A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" ~B() { int a; }\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// class A inherits virtual destructor from struct Base -> no error
|
|
|
|
// also notice that public is not given, but destructor is public, because
|
|
|
|
// we are using struct instead of class
|
|
|
|
checkVirtualDestructor("struct Base\n"
|
|
|
|
"{\n"
|
|
|
|
"virtual ~Base() {}\n"
|
|
|
|
"};\n"
|
|
|
|
"class A : public Base\n"
|
|
|
|
"{\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"class B : public A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" ~B() { int a; }\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// Unknown Base class -> it could have virtual destructor, so ignore
|
|
|
|
checkVirtualDestructor("class A : private Base\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" ~A() { }\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"class B : public A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" ~B() { int a; }\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// Virtual destructor is inherited -> no error
|
|
|
|
checkVirtualDestructor("class Base2\n"
|
|
|
|
"{\n"
|
|
|
|
"virtual ~Base2() {}\n"
|
|
|
|
"};\n"
|
|
|
|
"class Base : public Base2\n"
|
|
|
|
"{\n"
|
|
|
|
"};\n"
|
|
|
|
"class A : private Base\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" ~A() { }\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"class B : public A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" ~B() { int a; }\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// class A doesn't inherit virtual destructor from class Base -> error
|
|
|
|
checkVirtualDestructor("class Base\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
"~Base() {}\n"
|
|
|
|
"};\n"
|
|
|
|
"class A : private Base\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" ~A() { }\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"class B : public A\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" ~B() { int a; }\n"
|
|
|
|
"};\n");
|
|
|
|
TODO_ASSERT_EQUALS("[test.cpp:7]: (error) Class A which is inherited by class B does not have a virtual destructor\n", errout.str());
|
|
|
|
}
|
|
|
|
|
2009-08-10 16:58:13 +02:00
|
|
|
void virtualDestructorTemplate()
|
|
|
|
{
|
|
|
|
checkVirtualDestructor("template <typename T> class A\n"
|
|
|
|
"{\n"
|
|
|
|
" public:\n"
|
|
|
|
" virtual ~A(){}\n"
|
|
|
|
"};\n"
|
|
|
|
"template <typename T> class AA\n"
|
|
|
|
"{\n"
|
|
|
|
" public:\n"
|
|
|
|
" ~AA(){}\n"
|
|
|
|
"};\n"
|
|
|
|
"class B : public A<int>, public AA<double>\n"
|
|
|
|
"{\n"
|
|
|
|
" public:\n"
|
|
|
|
" ~B(){int a;}\n"
|
|
|
|
"};\n");
|
2009-11-01 19:03:52 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:7]: (error) Class AA<double> which is inherited by class B does not have a virtual destructor\n", errout.str());
|
2009-08-10 16:58:13 +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-12-06 18:35:32 +01:00
|
|
|
settings._showAll = true;
|
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-12-11 21:34:04 +01:00
|
|
|
void uninitVar2()
|
|
|
|
{
|
|
|
|
checkUninitVar("class John\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" John() { (*this).i = 0; }\n"
|
|
|
|
"private:\n"
|
|
|
|
" int i;\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
2009-12-13 09:35:08 +01:00
|
|
|
void uninitVarArray1()
|
2009-08-23 21:54:41 +02:00
|
|
|
{
|
|
|
|
checkUninitVar("class John\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" John() {}\n"
|
|
|
|
"\n"
|
|
|
|
"private:\n"
|
|
|
|
" char name[255];\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (style) Member variable not initialized in the constructor 'John::name'\n", errout.str());
|
2009-11-10 19:30:37 +01:00
|
|
|
|
2009-08-23 21:54:41 +02:00
|
|
|
checkUninitVar("class John\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
2009-08-25 23:42:07 +02:00
|
|
|
" John() {John::name[0] = '\\0';}\n"
|
2009-08-23 21:54:41 +02:00
|
|
|
"\n"
|
|
|
|
"private:\n"
|
|
|
|
" char name[255];\n"
|
|
|
|
"};\n");
|
2009-08-26 22:33:23 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-08-24 17:15:29 +02:00
|
|
|
|
|
|
|
checkUninitVar("class John\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" John() { strcpy(name, ""); }\n"
|
|
|
|
"\n"
|
|
|
|
"private:\n"
|
|
|
|
" char name[255];\n"
|
|
|
|
"};\n");
|
2009-09-03 22:28:00 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-09-16 20:07:03 +02:00
|
|
|
|
|
|
|
checkUninitVar("class John\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" John() { }\n"
|
|
|
|
"\n"
|
|
|
|
" double operator[](const unsigned long i);\n"
|
|
|
|
"};\n");
|
2009-11-10 19:30:37 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-09-16 20:07:03 +02:00
|
|
|
|
2009-11-10 19:30:37 +01:00
|
|
|
checkUninitVar("class A;\n"
|
|
|
|
"class John\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" John() { }\n"
|
|
|
|
" A a[5];\n"
|
|
|
|
"};\n");
|
2009-09-16 20:07:03 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-11-10 19:35:54 +01:00
|
|
|
|
|
|
|
checkUninitVar("class A;\n"
|
|
|
|
"class John\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" John() { }\n"
|
|
|
|
" A *a[5];\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (style) Member variable not initialized in the constructor 'John::a'\n", errout.str());
|
2009-09-03 22:28:00 +02:00
|
|
|
}
|
|
|
|
|
2009-12-13 09:35:08 +01:00
|
|
|
void uninitVarArray2()
|
|
|
|
{
|
|
|
|
checkUninitVar("class John\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" John() { *name = 0; }\n"
|
|
|
|
"\n"
|
|
|
|
"private:\n"
|
|
|
|
" char name[255];\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
2009-09-03 22:28:00 +02:00
|
|
|
void uninitMissingFuncDef()
|
|
|
|
{
|
|
|
|
// Unknown member function
|
|
|
|
checkUninitVar("class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" Fred() { Init(); }\n"
|
|
|
|
"private:\n"
|
|
|
|
" void Init();"
|
|
|
|
" int i;\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// Unknown non-member function
|
|
|
|
checkUninitVar("class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" Fred() { Init(); }\n"
|
|
|
|
"private:\n"
|
|
|
|
" int i;\n"
|
|
|
|
"};\n");
|
|
|
|
TODO_ASSERT_EQUALS("[test.cpp:4]: (style) Member variable not initialized in the constructor 'Fred::i'\n", errout.str());
|
2009-08-23 21:54:41 +02: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-12-06 18:35:32 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (possible style) Member variable not initialized in the constructor 'Foo::foo'\n", 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-12-06 18:35:32 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (possible style) Member variable not initialized in the constructor 'Foo::foo'\n"
|
|
|
|
"[test.cpp:7]: (possible style) Member variable not initialized in the constructor 'Foo::foo'\n", 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-08-31 19:40:49 +02:00
|
|
|
void uninitVarPublished()
|
|
|
|
{
|
|
|
|
checkUninitVar("class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"__published:\n"
|
|
|
|
" int *i;\n"
|
|
|
|
"public:\n"
|
|
|
|
" Fred() { }\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
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");
|
2009-09-29 20:27:17 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:1]: (style) The class 'Fred' has no constructor. Member variables not initialized.\n", errout.str());
|
2009-02-21 14:35:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
2009-12-19 17:58:52 +01:00
|
|
|
"private:\n"
|
2009-02-21 14:35:55 +01:00
|
|
|
" static int foobar;\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
2009-12-19 17:58:52 +01:00
|
|
|
void noConstructor4()
|
|
|
|
{
|
|
|
|
checkNoConstructor("class Fred\n"
|
|
|
|
"{\n"
|
|
|
|
"public:\n"
|
|
|
|
" int foobar;\n"
|
|
|
|
"};\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
2009-09-02 22:32:15 +02:00
|
|
|
void checkNoMemset(const char code[])
|
|
|
|
{
|
|
|
|
// Tokenize..
|
|
|
|
Tokenizer tokenizer;
|
|
|
|
std::istringstream istr(code);
|
|
|
|
tokenizer.tokenize(istr, "test.cpp");
|
|
|
|
|
|
|
|
|
|
|
|
// Clear the error log
|
|
|
|
errout.str("");
|
|
|
|
|
|
|
|
// Check..
|
|
|
|
Settings settings;
|
|
|
|
CheckClass checkClass(&tokenizer, &settings, this);
|
|
|
|
checkClass.noMemset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void memsetOnClass()
|
|
|
|
{
|
|
|
|
checkNoMemset("class A\n"
|
|
|
|
"{\n"
|
|
|
|
"};\n"
|
|
|
|
"void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" A fail;\n"
|
|
|
|
" memset(&fail, 0, sizeof(A));\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:7]: (error) Using 'memset' on class\n", errout.str());
|
|
|
|
|
|
|
|
checkNoMemset("struct A\n"
|
|
|
|
"{\n"
|
|
|
|
"};\n"
|
|
|
|
"void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" struct A fail;\n"
|
|
|
|
" memset(&fail, 0, sizeof(A));\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void memsetOnStruct()
|
|
|
|
{
|
|
|
|
checkNoMemset("class A\n"
|
|
|
|
"{\n"
|
|
|
|
" void g( struct sockaddr_in6& a);\n"
|
|
|
|
"private:\n"
|
|
|
|
" std::string b; \n"
|
|
|
|
"};\n"
|
|
|
|
"void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" struct sockaddr_in6 fail;\n"
|
|
|
|
" memset(&fail, 0, sizeof(struct sockaddr_in6));\n"
|
|
|
|
"}\n");
|
2009-09-02 22:51:07 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2009-09-02 22:32:15 +02:00
|
|
|
|
|
|
|
checkNoMemset("struct A\n"
|
|
|
|
"{\n"
|
|
|
|
" void g( struct sockaddr_in6& a);\n"
|
|
|
|
"private:\n"
|
|
|
|
" std::string b; \n"
|
|
|
|
"};\n"
|
|
|
|
"void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" struct A fail;\n"
|
|
|
|
" memset(&fail, 0, sizeof(struct A));\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:10]: (error) Using 'memset' on struct that contains a 'std::string'\n", errout.str());
|
|
|
|
}
|
2009-09-12 15:25:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
void checkThisSubtraction(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;
|
|
|
|
settings._showAll = true;
|
|
|
|
CheckClass checkClass(&tokenizer, &settings, this);
|
|
|
|
checkClass.thisSubtraction();
|
|
|
|
}
|
|
|
|
|
|
|
|
void this_subtraction()
|
|
|
|
{
|
|
|
|
checkThisSubtraction("; this-x ;");
|
|
|
|
ASSERT_EQUALS("[test.cpp:1]: (possible style) Suspicious pointer subtraction\n", errout.str());
|
2009-11-04 20:36:27 +01:00
|
|
|
|
|
|
|
checkThisSubtraction("; *this = *this-x ;");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
checkThisSubtraction("; *this = *this-x ;\n"
|
|
|
|
"this-x ;");
|
|
|
|
ASSERT_EQUALS("[test.cpp:2]: (possible style) Suspicious pointer subtraction\n", errout.str());
|
|
|
|
|
2009-11-04 20:38:40 +01:00
|
|
|
checkThisSubtraction("; *this = *this-x ;\n"
|
|
|
|
"this-x ;\n"
|
|
|
|
"this-x ;\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:2]: (possible style) Suspicious pointer subtraction\n"
|
|
|
|
"[test.cpp:3]: (possible style) Suspicious pointer subtraction\n", errout.str());
|
2009-09-12 15:25:02 +02:00
|
|
|
}
|
2008-12-18 22:28:57 +01:00
|
|
|
};
|
|
|
|
|
2009-01-05 16:49:57 +01:00
|
|
|
REGISTER_TEST(TestClass)
|