Tokenizer: fixed variable declaration simplification

This commit is contained in:
Daniel Marjamäki 2010-09-15 19:53:47 +02:00
parent b74f886579
commit 416f093fc3
2 changed files with 19 additions and 10 deletions

View File

@ -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);

View File

@ -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"