Fixed #5560 (false positive: C-style pointer casting)

This commit is contained in:
Daniel Marjamäki 2014-04-29 06:21:30 +02:00
parent 648479d909
commit fc24d491cc
2 changed files with 27 additions and 12 deletions

View File

@ -480,21 +480,25 @@ void CheckOther::warningOldStylePointerCast()
if (!_settings->isEnabled("style") || !_tokenizer->isCPP())
return;
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
// Old style pointer casting..
if (!Token::Match(tok, "( const| %type% * ) (| %var%") &&
!Token::Match(tok, "( const| %type% * ) (| new"))
continue;
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); ++i) {
const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->classStart; tok && tok != scope->classEnd; tok = tok->next()) {
// Old style pointer casting..
if (!Token::Match(tok, "( const| %type% * ) (| %var%") &&
!Token::Match(tok, "( const| %type% * ) (| new"))
continue;
if (tok->strAt(1) == "const")
tok = tok->next();
if (tok->strAt(1) == "const")
tok = tok->next();
if (tok->strAt(4) == "const")
continue;
if (tok->strAt(4) == "const")
continue;
// Is "type" a class?
if (_tokenizer->getSymbolDatabase()->isClassOrStruct(tok->strAt(1)))
cstyleCastError(tok);
// Is "type" a class?
if (_tokenizer->getSymbolDatabase()->isClassOrStruct(tok->strAt(1)))
cstyleCastError(tok);
}
}
}

View File

@ -1324,6 +1324,7 @@ private:
Settings settings;
settings.addEnabled("style");
settings.standards.cpp = Standards::CPP03; // #5560
// Tokenize..
Tokenizer tokenizerCpp(&settings, this);
@ -1390,6 +1391,16 @@ private:
" virtual void abc(const B *) const = 0;\n"
"}");
ASSERT_EQUALS("", errout.str());
// #5560
checkOldStylePointerCast("class C;\n"
"\n"
"class B\n"
"{ virtual G* createGui(S*, C*) const = 0; };\n"
"\n"
"class MS : public M\n"
"{ virtual void addController(C*) override {} };");
ASSERT_EQUALS("", errout.str());
}
void checkInvalidPointerCast(const char code[], bool portability = false, bool inconclusive = false) {