From 085187b4457de330d9796d752fbd9aeff7aad440 Mon Sep 17 00:00:00 2001 From: Slava Semushin Date: Sat, 29 Aug 2009 20:29:19 +0700 Subject: [PATCH] Tokenizer: enhance simplifyNot() and rename to simplifyLogicalOperators(). Don't replace "and" everything becuse it may be used as variable name. Better fix for #620 Corrections for commit eb05cf904df3b9b1e90de5dcc912817e83d422c8 --- src/tokenize.cpp | 19 ++++++++++--------- src/tokenize.h | 9 ++++++--- test/testsimplifytokens.cpp | 24 +++++++++++++++--------- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 722a53433..0d29fa258 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -1504,13 +1504,7 @@ void Tokenizer::simplifyTokenList() } } - // Replace "and" with "&&" - for (Token *tok = _tokens; tok; tok = tok->next()) - { - if (tok->str() == "and") - tok->str("&&"); - } - + simplifyLogicalOperators(); simplifyCasts(); // Simplify simple calculations.. @@ -1571,7 +1565,6 @@ void Tokenizer::simplifyTokenList() elseif(); simplifyIfNot(); simplifyIfNotNull(); - simplifyNot(); simplifyIfAssign(); for (Token *tok = _tokens; tok; tok = tok->next()) @@ -2826,9 +2819,10 @@ bool Tokenizer::simplifyIfNotNull() } -bool Tokenizer::simplifyNot() +bool Tokenizer::simplifyLogicalOperators() { // "if (not p)" => "if (!p)" + // "if (p and q)" => "if (p and q)" bool ret = false; for (Token *tok = _tokens; tok; tok = tok->next()) { @@ -2844,6 +2838,13 @@ bool Tokenizer::simplifyNot() { tok->next()->str("!"); } + // "%var%|) and %var%|(" + else if (tok->str() == "and" && + ((Token::Match(tok->previous(), "%var%") || tok->previous()->str() == ")") || + (Token::Match(tok->next(), "%var%") || tok->next()->str() == "("))) + { + tok->str("&&"); + } } return ret; } diff --git a/src/tokenize.h b/src/tokenize.h index 092bc2290..f5a619c76 100644 --- a/src/tokenize.h +++ b/src/tokenize.h @@ -162,12 +162,15 @@ private: bool simplifyIfNotNull(); /** - * simplify the "not" keyword to "!" - * Example: "if (not p)" => "if (!p)" + * Simplify the "not" and "and" keywords to "!" and "&&" + * accordingly. + * Examples: + * "if (not p)" => "if (!p)" + * "if (p and q)" => "if (p && q)" * @return true if something is modified * false if nothing is done. */ - bool simplifyNot(); + bool simplifyLogicalOperators(); /** * Simplify comma into a semicolon when possible diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index cdeb768e2..92eb9f04d 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -1011,14 +1011,14 @@ private: - std::string simplifyNot(const char code[]) + std::string simplifyLogicalOperators(const char code[]) { // tokenize.. Tokenizer tokenizer; std::istringstream istr(code); tokenizer.tokenize(istr, "test.cpp"); - tokenizer.simplifyNot(); + tokenizer.simplifyLogicalOperators(); std::ostringstream ostr; for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) @@ -1029,18 +1029,24 @@ private: void not1() { - ASSERT_EQUALS("if ( ! p )", simplifyNot("if (not p)")); - ASSERT_EQUALS("if ( p && ! q )", simplifyNot("if (p && not q)")); - ASSERT_EQUALS("void foo ( not i )", simplifyNot("void foo ( not i )")); + ASSERT_EQUALS("if ( ! p )", simplifyLogicalOperators("if (not p)")); + ASSERT_EQUALS("if ( p && ! q )", simplifyLogicalOperators("if (p && not q)")); + ASSERT_EQUALS("void foo ( not i )", simplifyLogicalOperators("void foo ( not i )")); } void and1() { - ASSERT_EQUALS(" if ( p && q )", - sizeof_(" if (p and q)")); + ASSERT_EQUALS("if ( p && q ) ;", + simplifyLogicalOperators("if (p and q) ;")); - ASSERT_EQUALS(" return operator == ( a ) && radius == 4 ;", - sizeof_(" return operator==(a) and radius == 4;")); + ASSERT_EQUALS("if ( foo ( ) && q ) ;", + simplifyLogicalOperators("if (foo() and q) ;")); + + ASSERT_EQUALS("if ( foo ( ) && bar ( ) ) ;", + simplifyLogicalOperators("if (foo() and bar()) ;")); + + ASSERT_EQUALS("if ( p && bar ( ) ) ;", + simplifyLogicalOperators("if (p and bar()) ;")); } void comma_keyword()