Fixed #3262 (Tokenizer: wrong 'return a=1,b=2,...;' simplification;)

This commit is contained in:
Daniel Marjamäki 2014-11-10 17:31:19 +01:00
parent e730f525ae
commit ea765c24f2
2 changed files with 32 additions and 21 deletions

View File

@ -8630,6 +8630,8 @@ void Tokenizer::simplifyMathFunctions()
void Tokenizer::simplifyComma()
{
bool inReturn = false;
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (Token::Match(tok, "(|[") ||
@ -8642,6 +8644,12 @@ void Tokenizer::simplifyComma()
if (tok->link() && tok->str() == "<")
tok = tok->link();
if (tok->str() == "return" && Token::Match(tok->previous(), "[;{}]"))
inReturn = true;
if (inReturn && Token::Match(tok, "[;{}?:]"))
inReturn = false;
if (!tok->next() || tok->str() != ",")
continue;
@ -8657,7 +8665,7 @@ void Tokenizer::simplifyComma()
// Handle "delete a, b;"
tok->str(";");
tok->insertToken("delete");
} else if (tok->tokAt(-2)) {
} else if (!inReturn && tok->tokAt(-2)) {
bool replace = false;
for (Token *tok2 = tok->previous(); tok2; tok2 = tok2->previous()) {
if (tok2->str() == "=") {
@ -8675,28 +8683,19 @@ void Tokenizer::simplifyComma()
}
}
bool inReturn = false;
Token *startFrom = nullptr; // "[;{}]" token before "return"
Token *endAt = nullptr; // first ";" token after "[;{}] return"
// find "; return" pattern before comma
for (Token *tok2 = tok->previous(); tok2; tok2 = tok2->previous()) {
if (Token::Match(tok2, "[;{}?]")) {
break;
} else if (Token::Match(tok2, ")|]") ||
(tok2->str() == "}" && tok2->link()->previous() && tok2->link()->previous()->str() == "=")) {
tok2 = tok2->link();
} else if (tok2->str() == "return" && Token::Match(tok2->previous(), "[;{}]")) {
inReturn = true;
startFrom = tok2->previous();
break;
}
}
// find token where return ends and also count commas
if (inReturn) {
Token *startFrom = nullptr; // "[;{}]" token before "return"
Token *endAt = nullptr; // first ";" token after "[;{}] return"
// find "; return" pattern before comma
for (Token *tok2 = tok->previous(); tok2; tok2 = tok2->previous()) {
if (tok2->str() == "return") {
startFrom = tok2->previous();
break;
}
}
std::size_t commaCounter = 0;
for (Token *tok2 = startFrom->next(); tok2; tok2 = tok2->next()) {

View File

@ -1926,6 +1926,18 @@ private:
"}";
ASSERT_EQUALS(expected, tok(code));
}
{
const char code[] = "int foo ()\n"
"{\n"
" return a=1, b=2;\n"
"}\n";
const char expected[] = "int foo ( ) "
"{"
" a = 1 ; return b = 2 ; "
"}";
ASSERT_EQUALS(expected, tok(code));
}
}
void simplifyConditionOperator() {