Partial rewrite of Tokenizer::simplifyComma:

skip '= { .. }' where possible;
hence remove '%num% after comma' check because it's redundant now;
replace round braces counter method with faster 'link skip';
if 'endAt' is 'NULL' after finding the ';' token, exit the function;
hence remove redundant 'tok != NULL' check after simplifying 'return' code.
This commit is contained in:
Edoardo Prezioso 2012-09-20 21:45:16 +02:00
parent 406483b618
commit 8e1e8525a3
2 changed files with 40 additions and 38 deletions

View File

@ -7845,14 +7845,9 @@ void Tokenizer::simplifyMathFunctions()
void Tokenizer::simplifyComma()
{
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (Token::simpleMatch(tok, "for (") ||
Token::simpleMatch(tok, "= {")) {
tok = tok->next()->link();
continue;
}
if (tok->str() == "(" || tok->str() == "[") {
if (tok->str() == "(" || tok->str() == "[" ||
(tok->str() == "{" && tok->previous() && tok->previous()->str() == "=")) {
tok = tok->link();
continue;
}
@ -7865,8 +7860,7 @@ void Tokenizer::simplifyComma()
tok = tok2;
}
// If token after the comma is a constant number, simplification is not required.
if (!tok->next() || tok->str() != "," || tok->next()->isNumber())
if (!tok->next() || tok->str() != ",")
continue;
// We must not accept just any keyword, e.g. accepting int
@ -7901,17 +7895,21 @@ void Tokenizer::simplifyComma()
}
bool inReturn = false;
Token *startFrom = NULL; // next tokean after "; return"
Token *endAt = NULL; // first ";" token after "; return"
Token *startFrom = NULL; // "[;{}]" token before "return"
Token *endAt = NULL; // first ";" token after "[;{}] return"
// find "; return" pattern before comma
for (Token *tok2 = tok; tok2; tok2 = tok2->previous()) {
for (Token *tok2 = tok->previous(); tok2; tok2 = tok2->previous()) {
if (Token::Match(tok2, "[;{}]")) {
break;
} else if (tok2->str() == ")" || tok2->str() == "]" ||
(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->next();
startFrom = tok2->previous();
break;
}
}
@ -7919,39 +7917,39 @@ void Tokenizer::simplifyComma()
// find token where return ends and also count commas
if (inReturn) {
std::size_t commaCounter = 0;
std::size_t indentlevel = 0;
for (Token *tok2 = startFrom; tok2; tok2 = tok2->next()) {
for (Token *tok2 = startFrom->next(); tok2; tok2 = tok2->next()) {
if (tok2->str() == ";") {
endAt = tok2;
break;
} else if (tok2->str() == "(") {
++indentlevel;
} else if (tok2->str() == "(" || tok2->str() == "[" ||
(tok2->str() == "{" && tok2->previous() && tok2->previous()->str() == "=")) {
tok2 = tok2->link();
} else if (tok2->str() == ")") {
--indentlevel;
} else if (tok2->str() == "," && indentlevel == 0) {
} else if (tok2->str() == ",") {
++commaCounter;
}
}
if (commaCounter) {
indentlevel = 0;
if (!endAt)
//probably a syntax error
return;
if (commaCounter) {
// change tokens:
// "; return a ( ) , b ( ) , c ;"
// to
// "; return a ( ) ; b ( ) ; c ;"
for (Token *tok2 = startFrom; tok2 != endAt; tok2 = tok2->next()) {
if (tok2->str() == "(") {
++indentlevel;
// "; a ( ) ; b ( ) ; return c ;"
} else if (tok2->str() == ")") {
--indentlevel;
// remove "return"
startFrom->deleteNext();
for (Token *tok2 = startFrom->next(); tok2 != endAt; tok2 = tok2->next()) {
if (tok2->str() == "(" || tok2->str() == "[" ||
(tok2->str() == "{" && tok2->previous() && tok2->previous()->str() == "=")) {
tok2 = tok2->link();
} else if (tok2->str() == "," && indentlevel == 0) {
} else if (tok2->str() == ",") {
tok2->str(";");
--commaCounter;
if (commaCounter == 0) {
@ -7959,17 +7957,9 @@ void Tokenizer::simplifyComma()
}
}
}
// delete old "return"
startFrom->previous()->deleteThis();
startFrom = 0; // give dead pointer a value
tok = endAt;
if (!tok)
return;
}
}
}
}

View File

@ -2697,6 +2697,18 @@ private:
"}";
ASSERT_EQUALS(expected, tok(code));
}
{
const char code[] = "int foo ()\n"
"{\n"
" return doSomething(), 0;\n"
"}\n";
const char expected[] = "int foo ( ) "
"{"
" doSomething ( ) ; return 0 ; "
"}";
ASSERT_EQUALS(expected, tok(code));
}
}
void conditionOperator() {