constructors: don't warn about missing constructor if class only has static variable members

This commit is contained in:
Daniel Marjamäki 2009-02-21 13:35:55 +00:00
parent 08f76279ed
commit d8927e720d
3 changed files with 75 additions and 17 deletions

View File

@ -91,28 +91,12 @@ struct CheckClass::VAR *CheckClass::ClassChecking_GetVarList(const Token *tok1)
varname = next->strAt(1); varname = next->strAt(1);
} }
// Is it a variable declaration?
else if (Token::Match(next, "%type% %type% %var% ;"))
{
const Token *next2 = next->next();
if (next2->isStandardType())
varname = next2->strAt(1);
else if (Token::findmatch(_tokenizer->tokens(), ("enum " + next2->str()).c_str()))
varname = next2->strAt(1);
}
// Pointer? // Pointer?
else if (Token::Match(next, "%type% * %var% ;")) else if (Token::Match(next, "%type% * %var% ;"))
{ {
varname = next->strAt(2); varname = next->strAt(2);
} }
// Pointer?
else if (!b && Token::Match(next, "%type% %type% * %var% ;"))
{
varname = next->strAt(3);
}
// If the varname was set in one of the two if-block above, create a entry for this variable.. // If the varname was set in one of the two if-block above, create a entry for this variable..
if (varname) if (varname)
{ {

View File

@ -482,6 +482,22 @@ void Tokenizer::tokenize(std::istream &code, const char FileName[])
tok->deleteNext(); tok->deleteNext();
} }
} }
// Remove "mutable"
while (Token::simpleMatch(_tokens, "mutable"))
{
Token *tok = _tokens;
_tokens = _tokens->next();
delete tok;
}
for (Token *tok = _tokens; tok; tok = tok->next())
{
while (Token::simpleMatch(tok->next(), "mutable"))
{
tok->deleteNext();
}
}
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -51,6 +51,9 @@ private:
TEST_CASE(uninitVarHeader1); // Class is defined in header TEST_CASE(uninitVarHeader1); // Class is defined in header
TEST_CASE(uninitVarHeader2); // Class is defined in header TEST_CASE(uninitVarHeader2); // Class is defined in header
TEST_CASE(uninitVarHeader3); // Class is defined in header TEST_CASE(uninitVarHeader3); // Class is defined in header
TEST_CASE(noConstructor1);
TEST_CASE(noConstructor2);
} }
// Check that base classes have virtual destructors // Check that base classes have virtual destructors
@ -296,6 +299,61 @@ private:
} }
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;
CheckClass checkClass(&tokenizer, settings, this);
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());
}
}; };
REGISTER_TEST(TestClass) REGISTER_TEST(TestClass)