From 6f9cd110d02d729034de004540c529804744da66 Mon Sep 17 00:00:00 2001 From: Edoardo Prezioso Date: Wed, 4 Jan 2012 13:45:27 +0100 Subject: [PATCH] Improve Tokenizer: improve 'simplifyDoublePlusAndDoubleMinus' when there are negative numbers. --- lib/tokenize.cpp | 30 ++++++++++++++++----- test/testsimplifytokens.cpp | 53 +++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 7 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 6626f182f..5f2164b82 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2542,18 +2542,34 @@ void Tokenizer::simplifyDoublePlusAndDoubleMinus() if (tok->next()->str() == "+") { tok->deleteNext(); continue; - } else if (tok->next()->str() == "-") { - tok->str("-"); - tok->deleteNext(); + } else if (tok->next()->str()[0] == '-') { + tok = tok->next(); + if (tok->str().size() == 1) { + tok = tok->previous(); + tok->str("-"); + tok->deleteNext(); + } else if (tok->isNumber()) { + tok->str(tok->str().substr(1)); + tok = tok->previous(); + tok->str("-"); + } continue; } } else if (tok->str() == "-") { - if (tok->next()->str() == "-") { - tok->str("+"); + if (tok->next()->str() == "+") { tok->deleteNext(); continue; - } else if (tok->next()->str() == "+") { - tok->deleteNext(); + } else if (tok->next()->str()[0] == '-') { + tok = tok->next(); + if (tok->str().size() == 1) { + tok = tok->previous(); + tok->str("+"); + tok->deleteNext(); + } else if (tok->isNumber()) { + tok->str(tok->str().substr(1)); + tok = tok->previous(); + tok->str("+"); + } continue; } } diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 7a67cbf21..987dcdeca 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -49,6 +49,7 @@ private: TEST_CASE(combine_strings); TEST_CASE(double_plus); TEST_CASE(redundant_plus); + TEST_CASE(redundant_plus_numbers); TEST_CASE(parentheses1); TEST_CASE(parenthesesVar); // Remove redundant parentheses around variable .. "( %var% )" TEST_CASE(declareVar); @@ -697,6 +698,58 @@ private: } } + void redundant_plus_numbers() { + { + const char code1[] = "void foo( int a )\n" + "{\n" + "a=a + + 1;\n" + "}\n"; + ASSERT_EQUALS("void foo ( int a ) { a = a + 1 ; }", tok(code1)); + } + { + const char code1[] = "void foo( int a )\n" + "{\n" + "a=a + + + 1;\n" + "}\n"; + ASSERT_EQUALS("void foo ( int a ) { a = a + 1 ; }", tok(code1)); + } + { + const char code1[] = "void foo( int a )\n" + "{\n" + "a=a + - 1;\n" + "}\n"; + ASSERT_EQUALS("void foo ( int a ) { a = a - 1 ; }", tok(code1)); + } + { + const char code1[] = "void foo( int a )\n" + "{\n" + "a=a - + 1;\n" + "}\n"; + ASSERT_EQUALS("void foo ( int a ) { a = a - 1 ; }", tok(code1)); + } + { + const char code1[] = "void foo( int a )\n" + "{\n" + "a=a - - 1;\n" + "}\n"; + ASSERT_EQUALS("void foo ( int a ) { a = a + 1 ; }", tok(code1)); + } + { + const char code1[] = "void foo( int a )\n" + "{\n" + "a=a - + - 1;\n" + "}\n"; + ASSERT_EQUALS("void foo ( int a ) { a = a + 1 ; }", tok(code1)); + } + { + const char code1[] = "void foo( int a )\n" + "{\n" + "a=a - - - 1;\n" + "}\n"; + ASSERT_EQUALS("void foo ( int a ) { a = a - 1 ; }", tok(code1)); + } + } + void parentheses1() { ASSERT_EQUALS("<= 110 ;", tok("<= (10+100);"));