From 574db37a6f78d319cee83bf968e14dc1ed6e5510 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 9 Feb 2009 07:47:41 +0000 Subject: [PATCH] uninitialized variables: added testcases and made a fix --- src/checkclass.cpp | 2 +- test/testclass.cpp | 78 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 74 insertions(+), 6 deletions(-) diff --git a/src/checkclass.cpp b/src/checkclass.cpp index 2709e10f1..0116fdf79 100644 --- a/src/checkclass.cpp +++ b/src/checkclass.cpp @@ -168,7 +168,7 @@ const Token * CheckClass::FindClassFunction(const Token *tok, const char classna // Member function implemented in the class declaration? if (tok->str() != "~" && Token::Match(tok->next(), internalPattern.str().c_str())) { - const Token *tok2 = tok; + const Token *tok2 = tok->next(); while (tok2 && tok2->str() != "{" && tok2->str() != ";") tok2 = tok2->next(); if (tok2 && tok2->str() == "{") diff --git a/test/testclass.cpp b/test/testclass.cpp index 4170df75e..bed25a0bc 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -43,9 +43,13 @@ private: TEST_CASE(virtualDestructor5); // Derived class has empty destructor => no error TEST_CASE(uninitVar1); + TEST_CASE(uninitVarEnum); TEST_CASE(uninitVarStream); - TEST_CASE(privateCtor); // If constructor is private.. + TEST_CASE(privateCtor1); // If constructor is private.. + // TODO TEST_CASE(privateCtor2); // If constructor is private.. TEST_CASE(function); // Function is not variable + TEST_CASE(uninitVarHeader1); // Class is defined in header + TEST_CASE(uninitVarHeader2); // Class is defined in header } // Check that base classes have virtual destructors @@ -168,20 +172,40 @@ private: ASSERT_EQUALS("[test.cpp:10]: (error) Uninitialized member variable 'Fred::_code'\n", errout.str()); } + void uninitVarEnum() + { + checkUninitVar("class Fred\n" + "{\n" + "public:\n" + " enum abc {a,b,c};\n" + " Fred() {}\n" + "private:\n" + " unsigned int i;\n" + "};\n"); + + ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized member variable 'Fred::i'\n", errout.str()); + } + void uninitVarStream() { checkUninitVar("#include \n" - "class Foo {\n" - "int foo;\n" + "class Foo\n" + "{\n" + "private:\n" + " int foo;\n" "public:\n" - "Foo(std::istream &in) { if(!(in >> foo)) throw 0; }\n" + " Foo(std::istream &in)\n" + " {\n" + " if(!(in >> foo))\n" + " throw 0;\n" + " }\n" "};\n"); ASSERT_EQUALS(std::string(""), errout.str()); } - void privateCtor() + void privateCtor1() { checkUninitVar("class Foo {\n" " int foo;\n" @@ -191,6 +215,20 @@ private: ASSERT_EQUALS(std::string(""), errout.str()); } + void privateCtor2() + { + checkUninitVar("class Foo\n" + "{\n" + "private:\n" + " int foo;\n" + " Foo() { }\n" + "public:\n" + " Foo(int _i) { }\n" + "};\n"); + + ASSERT_EQUALS(std::string("[test.cpp:7] uninitialized member variable Foo::foo"), errout.str()); + } + void function() { @@ -213,6 +251,36 @@ private: ASSERT_EQUALS(std::string(""), errout.str()); } + + 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"); + ASSERT_EQUALS("[fred.h:6]: (error) Uninitialized member variable 'Fred::i'\n", errout.str()); + } + + }; REGISTER_TEST(TestClass)