diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index b100a77a0..9e134debe 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2019,6 +2019,28 @@ void Tokenizer::simplifyTypedef() } } +void Tokenizer::simplifyMulAnd(void) +{ + for (Token *tok = _tokens; tok; tok = tok->next()) + { + //fix Ticket #2784 + if (Token::Match(tok->next(), "* & %any% =")) + { + tok->deleteNext(); //del * + tok->deleteNext(); //del & + continue; + } + if (Token::Match(tok->next(), "* ( & %any% ) =")) + { + tok->deleteNext(); //del * + tok->deleteNext(); //del ( + tok->deleteNext(); //del & + tok->next()->deleteNext(); //del ) + continue; + } + } +} + bool Tokenizer::tokenize(std::istream &code, const char FileName[], const std::string &configuration, @@ -2034,6 +2056,9 @@ bool Tokenizer::tokenize(std::istream &code, createTokens(code); + // simplify '* & %any% =' to '%any% =' + simplifyMulAnd(); + // Convert C# code if (_files[0].find(".cs")) { diff --git a/lib/tokenize.h b/lib/tokenize.h index 1a1377325..08ab55e60 100644 --- a/lib/tokenize.h +++ b/lib/tokenize.h @@ -114,6 +114,11 @@ public: */ static void deleteTokens(Token *tok); + /** + * Simplify '* & %any% =' to '%any% =' + */ + void simplifyMulAnd(void); + /** * Get parameter name of function * @param ftok The token for the function name in a function diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 6bc29510b..19bff3d67 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -230,6 +230,7 @@ private: TEST_CASE(simplify_constants2); TEST_CASE(simplify_constants3); TEST_CASE(simplify_null); + TEST_CASE(simplifyMulAnd); // #2784 TEST_CASE(vardecl1); TEST_CASE(vardecl2); @@ -4001,6 +4002,23 @@ private: ASSERT_EQUALS(expected, tokenizeAndStringify(code,true)); } + void simplifyMulAnd() + { + // (error) Resource leak + ASSERT_EQUALS( + "void f ( ) { int f ; f = open ( ) ; }", + tokenizeAndStringify( + "void f() {int f; *&f=open(); }" + ) + ); + ASSERT_EQUALS( + "void f ( ) { int f ; f = open ( ) ; }", + tokenizeAndStringify( + "void f() {int f; *(&f)=open(); }" + ) + ); + } + void vardecl1() { const char code[] = "unsigned int a, b;";