Fixed #1602 (functions defined in header outside of class cannot be const)

This commit is contained in:
Robert Reif 2010-04-18 15:40:31 +02:00 committed by Daniel Marjamäki
parent a473345f18
commit 76a683a73a
2 changed files with 55 additions and 1 deletions

View File

@ -121,7 +121,7 @@ CheckClass::Var *CheckClass::getVarList(const Token *tok1, bool withClasses, boo
}
// Is it a variable declaration?
if (Token::Match(next, "%type% %var% ;"))
if (Token::Match(next, "%type% %var% ;|:"))
{
if (withClasses)
varname = next->strAt(1);
@ -1818,6 +1818,20 @@ bool CheckClass::isMemberFunc(const Token *tok)
break;
}
}
// check for templates returning pointers or references
else if (Token::Match(tok, "*|&"))
{
int back = -2;
if (tok->strAt(back) == "::")
back -= 2;
if (tok->strAt(back) != "const")
{
if (!isConst)
return false;
}
}
tok = tok->next();
}
}

View File

@ -114,6 +114,7 @@ private:
TEST_CASE(const17); // ticket #1552
TEST_CASE(const18); // ticket #1563
TEST_CASE(const19); // ticket #1612
TEST_CASE(const20); // ticket #1602
TEST_CASE(constoperator1); // operator< can often be const
TEST_CASE(constoperator2); // operator<<
TEST_CASE(constincdec); // increment/decrement => non-const
@ -3231,6 +3232,45 @@ private:
ASSERT_EQUALS("", errout.str());
}
void const20()
{
// ticket #1602
checkConst("class Fred {\n"
" int x : 3;\n"
"public:\n"
" void set(int i) { x = i; }\n"
"};\n");
ASSERT_EQUALS("", errout.str());
checkConst("class Fred {\n"
" list<int *> x;\n"
"public:\n"
" list<int *> get() { return x; }\n"
"};\n");
ASSERT_EQUALS("", errout.str());
checkConst("class Fred {\n"
" list<const int *> x;\n"
"public:\n"
" list<const int *> get() { return x; }\n"
"};\n");
ASSERT_EQUALS("[test.cpp:4]: (style) The function 'Fred::get' can be const\n", errout.str());
checkConst("class Fred {\n"
" std::list<std::string &> x;\n"
"public:\n"
" std::list<std::string &> get() { return x; }\n"
"};\n");
ASSERT_EQUALS("", errout.str());
checkConst("class Fred {\n"
" std::list<const std::string &> x;\n"
"public:\n"
" std::list<const std::string &> get() { return x; }\n"
"};\n");
ASSERT_EQUALS("[test.cpp:4]: (style) The function 'Fred::get' can be const\n", errout.str());
}
// increment/decrement => not const
void constincdec()
{