Skip to content

Commit d8927e7

Browse files
author
Daniel Marjamäki
committed
constructors: don't warn about missing constructor if class only has static variable members
1 parent 08f7627 commit d8927e7

3 files changed

Lines changed: 75 additions & 17 deletions

File tree

src/checkclass.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ struct CheckClass::VAR *CheckClass::ClassChecking_GetVarList(const Token *tok1)
7979
// If next token contains a ":".. it is not part of a variable declaration
8080
if (next->str().find(":") != std::string::npos)
8181
{
82-
82+
8383
}
8484

8585
// Is it a variable declaration?
@@ -91,28 +91,12 @@ struct CheckClass::VAR *CheckClass::ClassChecking_GetVarList(const Token *tok1)
9191
varname = next->strAt(1);
9292
}
9393

94-
// Is it a variable declaration?
95-
else if (Token::Match(next, "%type% %type% %var% ;"))
96-
{
97-
const Token *next2 = next->next();
98-
if (next2->isStandardType())
99-
varname = next2->strAt(1);
100-
else if (Token::findmatch(_tokenizer->tokens(), ("enum " + next2->str()).c_str()))
101-
varname = next2->strAt(1);
102-
}
103-
10494
// Pointer?
10595
else if (Token::Match(next, "%type% * %var% ;"))
10696
{
10797
varname = next->strAt(2);
10898
}
10999

110-
// Pointer?
111-
else if (!b && Token::Match(next, "%type% %type% * %var% ;"))
112-
{
113-
varname = next->strAt(3);
114-
}
115-
116100
// If the varname was set in one of the two if-block above, create a entry for this variable..
117101
if (varname)
118102
{

src/tokenize.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,22 @@ void Tokenizer::tokenize(std::istream &code, const char FileName[])
482482
tok->deleteNext();
483483
}
484484
}
485+
486+
// Remove "mutable"
487+
while (Token::simpleMatch(_tokens, "mutable"))
488+
{
489+
Token *tok = _tokens;
490+
_tokens = _tokens->next();
491+
delete tok;
492+
}
493+
for (Token *tok = _tokens; tok; tok = tok->next())
494+
{
495+
while (Token::simpleMatch(tok->next(), "mutable"))
496+
{
497+
tok->deleteNext();
498+
}
499+
}
500+
485501
}
486502
//---------------------------------------------------------------------------
487503

test/testclass.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ class TestClass : public TestFixture
5151
TEST_CASE(uninitVarHeader1); // Class is defined in header
5252
TEST_CASE(uninitVarHeader2); // Class is defined in header
5353
TEST_CASE(uninitVarHeader3); // Class is defined in header
54+
55+
TEST_CASE(noConstructor1);
56+
TEST_CASE(noConstructor2);
5457
}
5558

5659
// Check that base classes have virtual destructors
@@ -296,6 +299,61 @@ class TestClass : public TestFixture
296299
}
297300

298301

302+
303+
304+
305+
void checkNoConstructor(const char code[])
306+
{
307+
// Tokenize..
308+
Tokenizer tokenizer;
309+
std::istringstream istr(code);
310+
tokenizer.tokenize(istr, "test.cpp");
311+
tokenizer.simplifyTokenList();
312+
313+
// Clear the error log
314+
errout.str("");
315+
316+
// Check..
317+
Settings settings;
318+
settings._checkCodingStyle = true;
319+
CheckClass checkClass(&tokenizer, settings, this);
320+
checkClass.constructors();
321+
}
322+
323+
void noConstructor1()
324+
{
325+
// There are nonstatic member variables - constructor is needed
326+
checkNoConstructor("class Fred\n"
327+
"{\n"
328+
" int i;\n"
329+
"};\n");
330+
ASSERT_EQUALS("[test.cpp:1]: (style) The class 'Fred' has no constructor\n", errout.str());
331+
}
332+
333+
void noConstructor2()
334+
{
335+
checkNoConstructor("class Fred\n"
336+
"{\n"
337+
"public:\n"
338+
" static void foobar();\n"
339+
"};\n"
340+
"\n"
341+
"void Fred::foobar()\n"
342+
"{ }\n");
343+
ASSERT_EQUALS("", errout.str());
344+
}
345+
346+
void noConstructor3()
347+
{
348+
checkNoConstructor("class Fred\n"
349+
"{\n"
350+
"public:\n"
351+
" static int foobar;\n"
352+
"};\n");
353+
ASSERT_EQUALS("", errout.str());
354+
}
355+
356+
299357
};
300358

301359
REGISTER_TEST(TestClass)

0 commit comments

Comments
 (0)