From fc24d491cc427c82b2d488acaf8c6cb403dbfc2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 29 Apr 2014 06:21:30 +0200 Subject: [PATCH] Fixed #5560 (false positive: C-style pointer casting) --- lib/checkother.cpp | 28 ++++++++++++++++------------ test/testother.cpp | 11 +++++++++++ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 9bdb97889..7726b2a7f 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -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); + } } } diff --git a/test/testother.cpp b/test/testother.cpp index ce5685319..48ee348e1 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -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) {