diff --git a/testmemleak.cpp b/testmemleak.cpp index ddfbf0d0f..643306932 100644 --- a/testmemleak.cpp +++ b/testmemleak.cpp @@ -117,7 +117,7 @@ private: TEST_CASE( func6 ); // TODO TEST_CASE( func7 ); TEST_CASE( func8 ); // Using callback - // TODO TEST_CASE( func9 ); // Embedding the function call in a if-condition + TEST_CASE( func9 ); // Embedding the function call in a if-condition TEST_CASE( class1 ); TEST_CASE( class2 ); diff --git a/tokenize.cpp b/tokenize.cpp index a7d808b73..86d9013f4 100644 --- a/tokenize.cpp +++ b/tokenize.cpp @@ -1011,12 +1011,15 @@ void Tokenizer::simplifyTokenList() } } - - for ( bool done = false; !done; done = true) - { + + bool done = false; + while ( ! done ) + { + done = true; done &= simplifyConditions(); - done &= simplifyCasts(); - }; + done &= simplifyCasts(); + done &= simplifyFunctionReturn(); + } } //--------------------------------------------------------------------------- @@ -1130,6 +1133,43 @@ bool Tokenizer::simplifyCasts() return ret; } + + +bool Tokenizer::simplifyFunctionReturn() +{ + bool ret = true; + int indentlevel = 0; + for ( const TOKEN *tok = tokens(); tok; tok = tok->next() ) + { + if ( tok->str() == "{" ) + ++indentlevel; + + else if ( tok->str() == "}" ) + --indentlevel; + + else if ( indentlevel == 0 && TOKEN::Match(tok, "%var% ( ) { return %num% ; }") ) + { + std::ostringstream pattern; + pattern << "[(=+-*/] " << tok->str() << " ( ) [;)+-*/]"; + for ( TOKEN *tok2 = _tokens; tok2; tok2 = tok2->next() ) + { + if ( TOKEN::Match(tok2, pattern.str().c_str()) ) + { + tok2 = tok2->next(); + tok2->setstr( tok->strAt(5) ); + tok2->deleteNext(); + tok2->deleteNext(); + ret = false; + } + } + } + } + + return ret; +} + + + //--------------------------------------------------------------------------- // Helper functions for handling the tokens list diff --git a/tokenize.h b/tokenize.h index a2f611bd1..ad9073d61 100644 --- a/tokenize.h +++ b/tokenize.h @@ -94,10 +94,15 @@ private: void Define(const char Name[], const char Value[]); void addtoken(const char str[], const unsigned int lineno, const unsigned int fileno); - + + /** Simplify conditions */ bool simplifyConditions(); + /** Simplify casts */ bool simplifyCasts(); + + /** Simplify function calls - constant return value */ + bool simplifyFunctionReturn(); TOKEN *_gettok(TOKEN *tok, int index);