Tokenizer::simplifyDoWhileAddBraces:

1)rewrite fix for ticket #988 (just don't simplify inside macro parenthesis);
2)use a different organization of the code: start from last token and proceed backwards. This way 'simplifyDoWhileAddBracesHelper' can be called just once, hence the 'Helper' code can be improved and moved in the main function.
This commit is contained in:
Edoardo Prezioso 2011-12-12 00:07:08 +01:00
parent cf574072b6
commit 52620e6493
2 changed files with 37 additions and 63 deletions

View File

@ -4961,41 +4961,37 @@ bool Tokenizer::simplifyIfAddBraces()
return true;
}
bool Tokenizer::simplifyDoWhileAddBracesHelper(Token *tok)
void Tokenizer::simplifyDoWhileAddBraces()
{
if (Token::Match(tok->next(), "[),]")) {
//start from the last token and proceed backwards
Token *last = _tokens;
while (last && last->next())
last = last->next();
for (Token *tok = last; tok; tok = tok->previous()) {
// fix for #988
return false;
}
if (tok->str() == ")" || tok->str() == "]" ||
(tok->str() == "}" && Token::simpleMatch(tok->link()->previous(), "= {")))
tok = tok->link();
if (!Token::Match(tok, "do !!{"))
continue;
Token *tok1 = tok; // token with "do"
Token *tok2 = NULL; // token with "while"
Token *tok3 = tok->next();
// skip loop body
bool result = false;
while (tok3) {
if (tok3->str() == "{") {
// skip all tokens until "}"
for (Token *tok3 = tok->next(); tok3; tok3 = tok3->next()) {
if (tok3->str() == "(" || tok3->str() == "[" || tok3->str() == "{") {
tok3 = tok3->link();
} else if (tok3->str() == "while") {
tok2 = tok3;
break;
} else if (Token::simpleMatch(tok3, "do {")) {
// Skip do{}while inside the current "do"
// Skip 'do { } while' inside the current "do"
tok3 = tok3->next()->link();
if (Token::simpleMatch(tok3->next(), "while"))
tok3 = tok3->next();
} else if (Token::Match(tok3, "do !!{") &&
!Token::Match(tok3->next(), "[),]")) {
// Handle do-while inside the current "do"
// first and return true to get the outer
// "do" to be handled later.
tok1 = tok3;
result = true;
}
tok3 = tok3->next();
}
if (tok2) {
@ -5006,21 +5002,6 @@ bool Tokenizer::simplifyDoWhileAddBracesHelper(Token *tok)
tok2->previous()->insertToken("}");
Token::createMutualLinks(tok1->next(), tok2->previous());
} else
result = false;
return result;
}
void Tokenizer::simplifyDoWhileAddBraces()
{
for (Token *tok = _tokens; tok; tok = tok->next()) {
if (Token::Match(tok, "do !!{")) {
while (simplifyDoWhileAddBracesHelper(tok)) {
// Call until the function returns false to
// handle do-while inside do-while
}
}
}
}

View File

@ -595,13 +595,6 @@ public:
*/
bool validate() const;
/**
* Helper function for simplifyDoWhileAddBraces()
* @param tok This must be a "do" token, which is
* not followed by "{".
*/
bool simplifyDoWhileAddBracesHelper(Token *tok);
/**
* Remove __declspec()
*/