From d68e2ae966f4fe259ddf426c3ff78196d3bd3795 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 22 Nov 2008 17:53:22 +0000 Subject: [PATCH] tokenizer: simplifyConditions tries to simplify conditions.. those that are always true to '( true )' and those that are always false to '( false )' --- CheckMemoryLeak.cpp | 4 +-- testtokenize.cpp | 42 +++--------------------------- tokenize.cpp | 63 +++++++++++++++++++++++++++++++-------------- tokenize.h | 7 +++-- 4 files changed, 52 insertions(+), 64 deletions(-) diff --git a/CheckMemoryLeak.cpp b/CheckMemoryLeak.cpp index b6997c392..06e01c024 100644 --- a/CheckMemoryLeak.cpp +++ b/CheckMemoryLeak.cpp @@ -382,11 +382,11 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list { addtoken("if(!var)"); } - else if ( Tokenizer::Match(tok, "if (") && _tokenizer->alwaysTrue(tok->next) ) + else if ( Tokenizer::Match(tok, "if ( true )") ) { addtoken("if(true)"); } - else if ( Tokenizer::Match(tok, "if (") && _tokenizer->alwaysTrue(tok->next) ) + else if ( Tokenizer::Match(tok, "if ( false )") ) { addtoken("if(false)"); } diff --git a/testtokenize.cpp b/testtokenize.cpp index fcc66984a..f24d980c5 100644 --- a/testtokenize.cpp +++ b/testtokenize.cpp @@ -43,8 +43,7 @@ private: TEST_CASE( dupfuncname ); - TEST_CASE( alwaysTrue ); - TEST_CASE( alwaysFalse ); + TEST_CASE( simplifyConditions ); } @@ -154,46 +153,11 @@ private: ASSERT_EQUALS( std::string("b"), tokenizer.FunctionList[0]->str ); } - - bool alwaysTrueCheck(const char condition[]) + void simplifyConditions() { - // tokenize.. - Tokenizer tokenizer( this ); - tokenizer.getFiles()->push_back( "test.cpp" ); - std::istringstream istr(condition); - tokenizer.TokenizeCode(istr, 0); + // TODO Simplify various conditions.. - // check.. - return tokenizer.alwaysTrue( tokenizer.tokens() ); } - - void alwaysTrue() - { - ASSERT_EQUALS(true, alwaysTrueCheck("(1)")); - ASSERT_EQUALS(true, alwaysTrueCheck("(true)")); - ASSERT_EQUALS(true, alwaysTrueCheck("(true || false)")); - ASSERT_EQUALS(false, alwaysTrueCheck("(true && false)")); - } - - bool alwaysFalseCheck(const char condition[]) - { - // tokenize.. - Tokenizer tokenizer( this ); - tokenizer.getFiles()->push_back( "test.cpp" ); - std::istringstream istr(condition); - tokenizer.TokenizeCode(istr, 0); - - // check.. - return tokenizer.alwaysFalse( tokenizer.tokens() ); - } - - void alwaysFalse() - { - ASSERT_EQUALS(true, alwaysFalseCheck("(0)")); - ASSERT_EQUALS(true, alwaysFalseCheck("(false)")); - ASSERT_EQUALS(true, alwaysFalseCheck("(false && true)")); - ASSERT_EQUALS(false, alwaysFalseCheck("(false || true)")); - } }; REGISTER_TEST( TestTokenizer ) diff --git a/tokenize.cpp b/tokenize.cpp index a6bfb3c5d..2e23a3564 100644 --- a/tokenize.cpp +++ b/tokenize.cpp @@ -820,12 +820,8 @@ void Tokenizer::SimplifyTokenList() // Simple calculations.. - - bool done = false; - while (!done) + for ( bool done = false; !done; done = true ) { - done = true; - for (TOKEN *tok = _tokens; tok; tok = tok->next) { if (Tokenizer::Match(tok->next, "* 1") || Tokenizer::Match(tok->next, "1 *")) @@ -1036,11 +1032,53 @@ void Tokenizer::SimplifyTokenList() while (!Tokenizer::Match(tok->next,"0")) DeleteNextToken(tok); } - } + } + + + for ( bool done = false; !done; done = true) + { + done &= simplifyConditions(); + }; } //--------------------------------------------------------------------------- +bool Tokenizer::simplifyConditions() +{ + bool ret = true; + + for ( TOKEN *tok = _tokens; tok; tok = tok->next ) + { + if (Match(tok, "( true &&") || Match(tok, "&& true &&") || Match(tok->next, "&& true )")) + { + DeleteNextToken( tok ); + DeleteNextToken( tok ); + ret = false; + } + + else if (Match(tok, "( false ||") || Match(tok, "|| false ||") || Match(tok->next, "|| false )")) + { + DeleteNextToken( tok ); + DeleteNextToken( tok ); + ret = false; + } + + // Change numeric constant in condition to "true" or "false" + const TOKEN *tok2 = gettok(tok, 2); + if ((Match(tok, "(") || Match(tok, "&&") || Match(tok, "||")) && + Match(tok->next, "%num%") && + (Match(tok2, ")") || Match(tok2, "&&") || Match(tok2, "||")) ) + { + tok->next->setstr((strcmp(tok->next->str, "0")!=0) ? "true" : "false"); + ret = false; + } + } + + return ret; +} + + + @@ -1518,16 +1556,3 @@ bool Tokenizer::IsStandardType(const char str[]) return Ret; } //--------------------------------------------------------------------------- - -bool Tokenizer::alwaysTrue( const TOKEN *tok ) -{ - return (Match(tok,"( 1 [|)]") | Match(tok,"( 1 ||") | - Match(tok,"( true [|)]") | Match(tok,"( true ||")); -} -//--------------------------------------------------------------------------- -bool Tokenizer::alwaysFalse( const TOKEN *tok ) -{ - return (Match(tok,"( 0 [&)]") | Match(tok,"( 0 &&") | - Match(tok,"( false [&)]") | Match(tok,"( false &&")); -} -//--------------------------------------------------------------------------- diff --git a/tokenize.h b/tokenize.h index 1a3e12649..e9fdeaf03 100644 --- a/tokenize.h +++ b/tokenize.h @@ -90,9 +90,6 @@ public: static bool IsNumber(const char str[]); static bool IsStandardType(const char str[]); - bool alwaysTrue( const TOKEN *tok ); - bool alwaysFalse( const TOKEN *tok ); - std::string fileLine( const TOKEN *tok ); // Return size. @@ -146,7 +143,9 @@ private: void combine_2tokens(TOKEN *tok, const char str1[], const char str2[]); - void DeleteNextToken(TOKEN *tok); + void DeleteNextToken(TOKEN *tok); + + bool simplifyConditions(); TOKEN *_gettok(TOKEN *tok, int index);