From 416f093fc3857f47ac8e3748cc6f355e4f4003cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 15 Sep 2010 19:53:47 +0200 Subject: [PATCH] Tokenizer: fixed variable declaration simplification --- lib/tokenize.cpp | 20 ++++++++++---------- test/testtokenize.cpp | 9 +++++++++ 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 806936c89..d5adfb82f 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -5143,22 +5143,22 @@ void Tokenizer::simplifyVarDecl() { Token *eq = tok2; - unsigned int parlevel = 0; + unsigned int level = 0; while (tok2) { - if (Token::Match(tok2, "[{(<]")) + if (Token::Match(tok2, "[{(]")) + tok2 = tok2->link(); + + else if (tok2->str() == "<") { - ++parlevel; + if (tok2->previous()->isName() && !tok2->previous()->varId()) + ++level; } - else if (Token::Match(tok2, "[})>]")) - { - if (parlevel == 0) - break; - --parlevel; - } + else if (level > 0 && tok2->str() == ">") + --level; - else if (parlevel == 0 && strchr(";,", tok2->str()[0])) + else if (level == 0 && strchr(";,", tok2->str()[0])) { // "type var =" => "type var; var =" Token *VarTok = type0->tokAt((int)typelen); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index e5ac551f0..76afbb739 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -206,6 +206,7 @@ private: TEST_CASE(vardecl10); TEST_CASE(vardecl11); TEST_CASE(vardecl12); + TEST_CASE(vardecl13); TEST_CASE(vardecl_stl); TEST_CASE(vardecl_template); TEST_CASE(vardecl_union); @@ -3547,6 +3548,14 @@ private: ASSERT_EQUALS("struct A { public: B a ; B b ; B c ; B d ; } ;", tokenizeAndStringify(code)); } + void vardecl13() + { + const char code[] = "void f() {\n" + " int a = (x < y) ? 1 : 0;\n" + "}"; + ASSERT_EQUALS("void f ( ) {\nint a ; a = ( x < y ) ? 1 : 0 ;\n}", tokenizeAndStringify(code)); + } + void volatile_variables() { const char code[] = "volatile int a=0;\n"