From 78f46c5848c9197407be049fcf7d50c505c774a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 22 Dec 2008 18:32:04 +0000 Subject: [PATCH] simplifyIfAddBraces : Fixed minor bug that caused the closing brace to be put on the wrong place --- testtokenize.cpp | 26 ++++++++++++++++++++++++++ tokenize.cpp | 1 + 2 files changed, 27 insertions(+) diff --git a/testtokenize.cpp b/testtokenize.cpp index 6dc0a71e8..8cf4b0b48 100644 --- a/testtokenize.cpp +++ b/testtokenize.cpp @@ -49,6 +49,7 @@ private: TEST_CASE( ifAddBraces1 ); TEST_CASE( ifAddBraces2 ); TEST_CASE( ifAddBraces3 ); + TEST_CASE( ifAddBraces4 ); TEST_CASE( numeric_true_condition ); @@ -288,6 +289,31 @@ private: ASSERT_EQUALS( std::string(" void f ( ) { if ( a ) { for ( ; ; ) { } } }"), ostr.str() ); } + void ifAddBraces4() + { + const char code[] = "char * foo ()\n" + "{\n" + " char *str = malloc(10);\n" + " if (somecondition)\n" + " for ( ; ; )\n" + " { }\n" + " return str;\n" + "}\n"; + + + // tokenize.. + Tokenizer tokenizer; + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); + + ASSERT_EQUALS( true, tokenizer.simplifyIfAddBraces() ); + + std::ostringstream ostr; + for (const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next()) + ostr << " " << tok->str(); + ASSERT_EQUALS( std::string(" char * foo ( ) { char * str = malloc ( 10 ) ; if ( somecondition ) { for ( ; ; ) { } } return str ; }"), ostr.str() ); + } + void simplifyKnownVariables1() { const char code[] = "void f()\n" diff --git a/tokenize.cpp b/tokenize.cpp index 8dec11cac..1ba7a8255 100644 --- a/tokenize.cpp +++ b/tokenize.cpp @@ -1242,6 +1242,7 @@ bool Tokenizer::simplifyIfAddBraces() // insert open brace.. tok->insertToken("{"); + tok = tok->next(); // insert close brace.. // In most cases it would work to just search for the next ';' and insert a closing brace after it.