From 5af0d65aef4633b3b74e48a1c8c6a77247549791 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 9 Sep 2010 17:43:09 +0200 Subject: [PATCH] Fixed #2025 (iso646 c++ operators are not handled correctly) --- lib/tokenize.cpp | 48 ++++++++++++++++++++++++++++++------------- test/testtokenize.cpp | 14 +++++++++++++ 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 523f2b7e2..2ac8387b8 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -5546,31 +5546,51 @@ bool Tokenizer::simplifyLogicalOperators() // "if (p or q)" => "if (p || q)" for (Token *tok = _tokens; tok; tok = tok->next()) { - if (Token::Match(tok, "if|while ( not %var%")) + if (Token::Match(tok, "if|while ( not|compl %var%")) { - tok->tokAt(2)->str("!"); + tok->tokAt(2)->str(tok->strAt(2) == "not" ? "!" : "~"); ret = true; } - else if (Token::Match(tok, "&& not %var%")) + else if (Token::Match(tok, "&& not|compl %var%")) { - tok->next()->str("!"); + tok->next()->str(tok->strAt(1) == "not" ? "!" : "~"); ret = true; } - else if (Token::Match(tok, "|| not %var%")) + else if (Token::Match(tok, "|| not|compl %var%")) { - tok->next()->str("!"); + tok->next()->str(tok->strAt(1) == "not" ? "!" : "~"); ret = true; } // "%var%|) and %var%|(" - else if (Token::Match(tok->previous(), "%any% and|or %any%") && - ((tok->previous()->isName() || tok->previous()->str() == ")") || - (tok->next()->isName() || tok->next()->str() == "("))) + else if (Token::Match(tok->previous(), "%any% %var% %any%")) { - if (tok->str() == "and") - tok->str("&&"); - else - tok->str("||"); - ret = true; + if (!Token::Match(tok, "and|or|bitand|bitor|xor|not_eq")) + continue; + + const Token *tok2 = tok; + while (0 != (tok2 = tok2->previous())) + { + if (tok2->str() == ")") + tok2 = tok2->link(); + else if (Token::Match(tok2, "(|;|{|}")) + break; + } + if (tok2 && Token::Match(tok2->previous(), "if|while (")) + { + if (tok->str() == "and") + tok->str("&&"); + else if (tok->str() == "or") + tok->str("||"); + else if (tok->str() == "bitand") + tok->str("&"); + else if (tok->str() == "bitor") + tok->str("|"); + else if (tok->str() == "xor") + tok->str("^"); + else if (tok->str() == "not_eq") + tok->str("!="); + ret = true; + } } } return ret; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 9e6ff07ff..e486e1e0c 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -259,6 +259,8 @@ private: TEST_CASE(borland); TEST_CASE(sql); + + TEST_CASE(simplifyLogicalOperators); } @@ -4541,6 +4543,18 @@ private: ASSERT_EQUALS("; asm ( ) ;", tokenizeAndStringify(code1,false)); } + + void simplifyLogicalOperators() + { + ASSERT_EQUALS("if ( a && b )", tokenizeAndStringify("if (a and b)")); + ASSERT_EQUALS("if ( a || b )", tokenizeAndStringify("if (a or b)")); + ASSERT_EQUALS("if ( a & b )", tokenizeAndStringify("if (a bitand b)")); + ASSERT_EQUALS("if ( a | b )", tokenizeAndStringify("if (a bitor b)")); + ASSERT_EQUALS("if ( a ^ b )", tokenizeAndStringify("if (a xor b)")); + ASSERT_EQUALS("if ( ~ b )", tokenizeAndStringify("if (compl b)")); + ASSERT_EQUALS("if ( ! b )", tokenizeAndStringify("if (not b)")); + ASSERT_EQUALS("if ( a != b )", tokenizeAndStringify("if (a not_eq b)")); + } }; REGISTER_TEST(TestTokenizer)