diff --git a/src/tokenize.cpp b/src/tokenize.cpp index a15579d5e..13e8f11ba 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -597,10 +597,20 @@ void Tokenizer::simplifyTokenList() { const char *sym = tok->strAt(2); const char *num = tok->strAt(4); - + int indent = 1; for (Token *tok2 = tok->tokAt(6); tok2; tok2 = tok2->next()) { - if (tok2->str() == sym) + if (tok2->str() == "{") + { + ++indent; + } + else if (tok2->str() == "}") + { + --indent; + if (indent == 0) + break; + } + else if (tok2->str() == sym) { tok2->str(num); } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 2401295b0..2698a6d6e 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -110,7 +110,7 @@ private: TEST_CASE(simplify_numeric_condition); TEST_CASE(tokenize_double); TEST_CASE(tokenize_strings); - // TODO TEST_CASE(simplify_constants); + TEST_CASE(simplify_constants); } @@ -1044,6 +1044,8 @@ private: "void f()\n" "{\n" "const int a = 45;\n" + "if( a )\n" + "{ int b = a; }\n" "}\n" "void g()\n" "{\n" @@ -1061,7 +1063,7 @@ private: std::ostringstream ostr; for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) ostr << " " << tok->str(); - ASSERT_EQUALS(std::string(" void f ( ) { const int a = 45 ; } void g ( ) { int a ; a = 2 ; }"), ostr.str()); + ASSERT_EQUALS(std::string(" void f ( ) { const int a = 45 ; { int b ; b = 45 ; } } void g ( ) { int a ; a = 2 ; }"), ostr.str()); } };