From 02d6e367e7a77eaf433758b7959106b52eb699e2 Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Sat, 13 Dec 2008 18:54:48 +0000 Subject: [PATCH] Refactoring, simplifyConditions(), simplifyCasts() and simplifyFunctionReturn() now return the opposite of their boolean return value. --- tokenize.cpp | 31 +++++++++++++++++++------------ tokenize.h | 17 +++++++++++++---- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/tokenize.cpp b/tokenize.cpp index 86d9013f4..7c3561258 100644 --- a/tokenize.cpp +++ b/tokenize.cpp @@ -1016,9 +1016,14 @@ void Tokenizer::simplifyTokenList() while ( ! done ) { done = true; - done &= simplifyConditions(); - done &= simplifyCasts(); - done &= simplifyFunctionReturn(); + if( simplifyConditions() ) + done = false; + + if( simplifyCasts() ) + done = false; + + if( simplifyFunctionReturn() ) + done = false; } } //--------------------------------------------------------------------------- @@ -1026,7 +1031,7 @@ void Tokenizer::simplifyTokenList() bool Tokenizer::simplifyConditions() { - bool ret = true; + bool ret = false; for ( TOKEN *tok = _tokens; tok; tok = tok->next() ) { @@ -1034,14 +1039,14 @@ bool Tokenizer::simplifyConditions() { tok->deleteNext(); tok->deleteNext(); - ret = false; + ret = true; } else if (TOKEN::Match(tok, "( false ||") || TOKEN::Match(tok, "|| false ||") || TOKEN::Match(tok->next(), "|| false )")) { tok->deleteNext(); tok->deleteNext(); - ret = false; + ret = true; } // Change numeric constant in condition to "true" or "false" @@ -1051,7 +1056,7 @@ bool Tokenizer::simplifyConditions() (TOKEN::Match(tok2, ")") || TOKEN::Match(tok2, "&&") || TOKEN::Match(tok2, "||")) ) { tok->next()->setstr((tok->next()->str() != "0") ? "true" : "false"); - ret = false; + ret = true; } // Reduce "(%num% == %num%)" => "(true)"/"(false)" @@ -1086,7 +1091,7 @@ bool Tokenizer::simplifyConditions() tok->deleteNext(); tok->setstr( result ? "true" : "false" ); - ret = false; + ret = true; } } } @@ -1097,7 +1102,7 @@ bool Tokenizer::simplifyConditions() bool Tokenizer::simplifyCasts() { - bool ret = true; + bool ret = false; for ( TOKEN *tok = _tokens; tok; tok = tok->next() ) { if ( TOKEN::Match(tok->next(), "( %type% * )") ) @@ -1106,7 +1111,7 @@ bool Tokenizer::simplifyCasts() tok->deleteNext(); tok->deleteNext(); tok->deleteNext(); - ret = false; + ret = true; } else if ( TOKEN::Match(tok->next(), "dynamic_cast|reinterpret_cast|const_cast <" ) ) @@ -1127,6 +1132,8 @@ bool Tokenizer::simplifyCasts() } if (tok2->next()) tok2->deleteNext(); + + ret = true; } } @@ -1137,7 +1144,7 @@ bool Tokenizer::simplifyCasts() bool Tokenizer::simplifyFunctionReturn() { - bool ret = true; + bool ret = false; int indentlevel = 0; for ( const TOKEN *tok = tokens(); tok; tok = tok->next() ) { @@ -1159,7 +1166,7 @@ bool Tokenizer::simplifyFunctionReturn() tok2->setstr( tok->strAt(5) ); tok2->deleteNext(); tok2->deleteNext(); - ret = false; + ret = true; } } } diff --git a/tokenize.h b/tokenize.h index ad9073d61..b3400b70d 100644 --- a/tokenize.h +++ b/tokenize.h @@ -78,7 +78,7 @@ public: void fillFunctionList(); const TOKEN *GetFunctionTokenByName( const char funcname[] ) const; const TOKEN *tokens() const; - + #ifndef UNIT_TESTING private: @@ -95,13 +95,22 @@ private: void addtoken(const char str[], const unsigned int lineno, const unsigned int fileno); - /** Simplify conditions */ + /** Simplify conditions + * @return true if something is modified + * false if nothing is done. + */ bool simplifyConditions(); - /** Simplify casts */ + /** Simplify casts + * @return true if something is modified + * false if nothing is done. + */ bool simplifyCasts(); - /** Simplify function calls - constant return value */ + /** Simplify function calls - constant return value + * @return true if something is modified + * false if nothing is done. + */ bool simplifyFunctionReturn(); TOKEN *_gettok(TOKEN *tok, int index);