Fixed #2111 (Class methods writing to a union are detected as 'can be const')

This commit is contained in:
Daniel Marjamäki 2010-10-24 16:43:10 +02:00
parent d605239735
commit cf86e11d05
2 changed files with 23 additions and 0 deletions

View File

@ -2221,6 +2221,13 @@ bool CheckClass::checkConstFunc(const SpaceInfo *info, const Token *tok)
isconst = false;
break;
}
// FIXME: I assume that a member union/struct variable is assigned.
else if (Token::Match(tok1->tokAt(-2), ". %var%"))
{
isconst = false;
break;
}
}
// streaming: <<

View File

@ -157,6 +157,7 @@ private:
TEST_CASE(constVirtualFunc);
TEST_CASE(constIfCfg); // ticket #1881 - fp when there are #if
TEST_CASE(constFriend); // ticket #1921 - fp for friend function
TEST_CASE(constUnion); // ticket #2111 - fp when there are union
TEST_CASE(symboldatabase1);
TEST_CASE(symboldatabase2);
@ -4409,6 +4410,21 @@ private:
ASSERT_EQUALS("", errout.str());
}
void constUnion() // ticket #2111
{
checkConst("class foo {\n"
"public:\n"
" union {\n"
" int i;\n"
" float f;\n"
" } d;\n"
" void setf(float x) {\n"
" d.f = x;\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void symboldatabase1()
{
checkConst("namespace foo {\n"