diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 19e4b08fc..68eed2e7a 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4377,19 +4377,21 @@ void Tokenizer::simplifyCompoundAssignment() continue; } + // backup current token.. + const Token * const tok1 = tok; + // variable.. - std::stack 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()); } } } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 40e100b08..5e9cc3d95 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -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() diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 1cb3074f9..402df2c6f 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -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"