Fixed #1165 (Tokenizer: wrong simplification of 'do { .. } while (0)' if the body contains continue or break)
This commit is contained in:
parent
4cbae159b2
commit
160f795710
|
@ -5053,6 +5053,17 @@ void Tokenizer::getErrorMessages()
|
|||
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()
|
||||
{
|
||||
for (Token *tok = _tokens; tok; tok = tok->next())
|
||||
|
@ -5064,16 +5075,19 @@ void Tokenizer::simplifyWhile0()
|
|||
if (Token::simpleMatch(tok->tokAt(4), "{"))
|
||||
{
|
||||
const Token *end = tok->tokAt(4)->link();
|
||||
if (!findmatch(tok, end, "continue|break"))
|
||||
{
|
||||
Token::eraseTokens(tok, end ? end->next() : 0);
|
||||
tok->deleteThis(); // delete "while"
|
||||
}
|
||||
}
|
||||
|
||||
if (Token::simpleMatch(tok->previous(), "}"))
|
||||
{
|
||||
// find "do"
|
||||
Token *tok2 = tok->previous()->link();
|
||||
tok2 = tok2 ? tok2->previous() : 0;
|
||||
if (tok2 && tok2->str() == "do")
|
||||
if (tok2 && tok2->str() == "do" && !findmatch(tok2, tok, "continue|break"))
|
||||
{
|
||||
// delete "do {"
|
||||
tok2->deleteThis();
|
||||
|
|
|
@ -2520,6 +2520,8 @@ private:
|
|||
void while0()
|
||||
{
|
||||
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);"));
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue