add support to CheckClass::checkConstFunc for ++/-- array elements

This commit is contained in:
Robert Reif 2011-03-25 22:37:32 -04:00
parent ca50bc7850
commit f8e1735b0f
2 changed files with 39 additions and 0 deletions

View File

@ -1539,12 +1539,22 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Token *tok)
// increment/decrement (member variable?)..
else if (Token::Match(tok1, "++|--"))
{
// var++ and var--
if (Token::Match(tok1->previous(), "%var%") &&
tok1->previous()->str() != "return")
{
if (isMemberVar(scope, tok1->previous()))
isconst = false;
}
// var[...]++ and var[...]--
else if (tok1->previous()->str() == "]")
{
if (isMemberVar(scope, tok1->previous()->link()->previous()))
isconst = false;
}
// ++var and --var
else if (Token::Match(tok1->next(), "%var%"))
{
if (isMemberVar(scope, tok1->next()))

View File

@ -176,6 +176,7 @@ private:
TEST_CASE(constoperator3);
TEST_CASE(constoperator4);
TEST_CASE(constincdec); // increment/decrement => non-const
TEST_CASE(constincdecarray); // increment/decrement array element => non-const
TEST_CASE(constReturnReference);
TEST_CASE(constDelete); // delete member variable => not const
TEST_CASE(constLPVOID); // a function that returns LPVOID can't be const
@ -5400,6 +5401,34 @@ private:
ASSERT_EQUALS("", errout.str());
}
// increment/decrement array element => not const
void constincdecarray()
{
checkConst("class Fred {\n"
" int a[2];\n"
" void nextA() { return ++a[0]; }\n"
"};\n");
ASSERT_EQUALS("", errout.str());
checkConst("class Fred {\n"
" int a[2];\n"
" void nextA() { return --a[0]; }\n"
"};\n");
ASSERT_EQUALS("", errout.str());
checkConst("class Fred {\n"
" int a[2];\n"
" void nextA() { return a[0]++; }\n"
"};\n");
ASSERT_EQUALS("", errout.str());
checkConst("class Fred {\n"
" int a[2];\n"
" void nextA() { return a[0]--; }\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}
// return pointer/reference => not const
void constReturnReference()
{