Fix ticket #486 (C-style pointer casting misses const pointers)

http://sourceforge.net/apps/trac/cppcheck/ticket/486
This commit is contained in:
Reijo Tomperi 2009-07-20 22:52:27 +03:00
parent 05a1c50ff2
commit 973bb164ea
2 changed files with 20 additions and 3 deletions

View File

@ -45,14 +45,18 @@ void CheckOther::warningOldStylePointerCast()
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{ {
// Old style pointer casting.. // Old style pointer casting..
if (!Token::Match(tok, "( %type% * ) %var%")) if (!Token::Match(tok, "( const| %type% * ) %var%"))
continue; continue;
if (Token::simpleMatch(tok->tokAt(4), "const")) int addToIndex = 0;
if (Token::simpleMatch(tok->tokAt(1), "const"))
addToIndex = 1;
if (Token::simpleMatch(tok->tokAt(4 + addToIndex), "const"))
continue; continue;
// Is "type" a class? // Is "type" a class?
const std::string pattern("class " + tok->next()->str()); const std::string pattern("class " + tok->tokAt(1 + addToIndex)->str());
if (!Token::findmatch(_tokenizer->tokens(), pattern.c_str())) if (!Token::findmatch(_tokenizer->tokens(), pattern.c_str()))
continue; continue;

View File

@ -517,6 +517,12 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) C-style pointer casting\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (style) C-style pointer casting\n", errout.str());
checkOldStylePointerCast("class Base;\n"
"void foo()\n"
"{\n"
" Base * b = (const Base *) derived;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) C-style pointer casting\n", errout.str());
checkOldStylePointerCast("class B;\n" checkOldStylePointerCast("class B;\n"
"class A\n" "class A\n"
@ -524,6 +530,13 @@ private:
" virtual void abc(B *) const = 0;\n" " virtual void abc(B *) const = 0;\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkOldStylePointerCast("class B;\n"
"class A\n"
"{\n"
" virtual void abc(const B *) const = 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
} }
}; };