Tokenizer: simplify 'x[0] += 1;'

This commit is contained in:
Daniel Marjamäki 2010-11-02 19:22:48 +01:00
parent 0fe72839d8
commit dd4b2b8b46
3 changed files with 20 additions and 17 deletions

View File

@ -4377,19 +4377,21 @@ void Tokenizer::simplifyCompoundAssignment()
continue;
}
// backup current token..
const Token * const tok1 = tok;
// variable..
std::stack<Token *> vartok;
vartok.push(tok->next());
while (Token::Match(tok->tokAt(2), ". %var%"))
{
tok = tok->tokAt(2);
while (Token::Match(tok, ". %var%"))
tok = tok->tokAt(2);
vartok.push(tok->next());
}
while (Token::Match(tok, "[ %any% ]"))
tok = tok->tokAt(3);
// assignment..
const std::string str = tok->strAt(2);
if (!tok)
break;
// Is it a +=|-=|.. ?
// Is current token at a compound assignment: +=|-=|.. ?
const std::string &str = tok->str();
std::string op; // operator used in assignment
if (str.size() == 2 && str[1] == '=' && str.find_first_of("+-*/%&|^")==0)
op = str.substr(0, 1);
@ -4399,16 +4401,14 @@ void Tokenizer::simplifyCompoundAssignment()
continue;
// modify the token list..
tok = tok->tokAt(2);
tok->str("=");
tok->insertToken(op);
while (!vartok.empty())
for (const Token *tok2 = tok->previous(); tok2 && tok2 != tok1; tok2 = tok2->previous())
{
tok->insertToken(vartok.top()->str());
tok->next()->varId(vartok.top()->varId());
vartok.pop();
if (!vartok.empty())
tok->insertToken(".");
tok->insertToken(tok2->str());
tok->next()->varId(tok2->varId());
if (Token::Match(tok->next(), "[ %any% ]"))
Token::createMutualLinks(tok->next(), tok->next()->next()->next());
}
}
}

View File

@ -4613,6 +4613,8 @@ private:
ASSERT_EQUALS("case 0 : x = x + y ; break ;", tokenizeAndStringify("case 0: x += y; break;"));
ASSERT_EQUALS("; x . y = x . y + 1 ;", tokenizeAndStringify("; x.y += 1;"));
ASSERT_EQUALS("; x [ 0 ] = x [ 0 ] + 1 ;", tokenizeAndStringify("; x[0] += 1;"));
}
void simplifyAssignmentInFunctionCall()

View File

@ -383,7 +383,8 @@ private:
" int a[10];\n"
" a[0] += 10;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: a\n", errout.str());
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: a\n", errout.str());
ASSERT_EQUALS("", errout.str());
// goto/setjmp/longjmp..
checkUninitVar("void foo(int x)\n"