Fixed #1165 (Tokenizer: wrong simplification of 'do { .. } while (0)' if the body contains continue or break)

This commit is contained in:
Daniel Marjamäki 2009-12-29 12:15:39 +01:00
parent 4cbae159b2
commit 160f795710
2 changed files with 19 additions and 3 deletions

View File

@ -5053,6 +5053,17 @@ void Tokenizer::getErrorMessages()
syntaxError(0, ' '); syntaxError(0, ' ');
} }
/** find pattern */
static bool findmatch(const Token *tok1, const Token *tok2, const char pattern[])
{
for (const Token *tok = tok1; tok && tok != tok2; tok = tok->next())
{
if (Token::Match(tok, pattern))
return true;
}
return false;
}
void Tokenizer::simplifyWhile0() void Tokenizer::simplifyWhile0()
{ {
for (Token *tok = _tokens; tok; tok = tok->next()) for (Token *tok = _tokens; tok; tok = tok->next())
@ -5064,8 +5075,11 @@ void Tokenizer::simplifyWhile0()
if (Token::simpleMatch(tok->tokAt(4), "{")) if (Token::simpleMatch(tok->tokAt(4), "{"))
{ {
const Token *end = tok->tokAt(4)->link(); const Token *end = tok->tokAt(4)->link();
Token::eraseTokens(tok, end ? end->next() : 0); if (!findmatch(tok, end, "continue|break"))
tok->deleteThis(); // delete "while" {
Token::eraseTokens(tok, end ? end->next() : 0);
tok->deleteThis(); // delete "while"
}
} }
if (Token::simpleMatch(tok->previous(), "}")) if (Token::simpleMatch(tok->previous(), "}"))
@ -5073,7 +5087,7 @@ void Tokenizer::simplifyWhile0()
// find "do" // find "do"
Token *tok2 = tok->previous()->link(); Token *tok2 = tok->previous()->link();
tok2 = tok2 ? tok2->previous() : 0; tok2 = tok2 ? tok2->previous() : 0;
if (tok2 && tok2->str() == "do") if (tok2 && tok2->str() == "do" && !findmatch(tok2, tok, "continue|break"))
{ {
// delete "do {" // delete "do {"
tok2->deleteThis(); tok2->deleteThis();

View File

@ -2520,6 +2520,8 @@ private:
void while0() void while0()
{ {
ASSERT_EQUALS("; x = 1 ; ;", tok("; do { x = 1 ; } while (0);")); ASSERT_EQUALS("; x = 1 ; ;", tok("; do { x = 1 ; } while (0);"));
ASSERT_EQUALS("; do { continue ; } while ( false ) ;", tok("; do { continue ; } while (0);"));
ASSERT_EQUALS("; do { break ; } while ( false ) ;", tok("; do { break; } while (0);"));
} }
}; };