Tokenizer: simplify loops that continue while errno is EINTR

This commit is contained in:
Daniel Marjamäki 2010-05-30 12:49:39 +02:00
parent 62093d764d
commit 83c62eca9c
3 changed files with 49 additions and 0 deletions

View File

@ -3598,6 +3598,7 @@ bool Tokenizer::simplifyTokenList()
simplifyComparisonOrder();
simplifyNestedStrcat();
simplifyWhile0();
simplifyErrNoInWhile();
simplifyFuncInWhile();
simplifyIfAssign(); // could be affected by simplifyIfNot
@ -7480,6 +7481,38 @@ void Tokenizer::simplifyWhile0()
}
}
void Tokenizer::simplifyErrNoInWhile()
{
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (tok->str() != "errno")
continue;
Token *endpar = 0;
if (Token::Match(tok->previous(), "&& errno == EINTR ) { ;| }"))
endpar = tok->tokAt(3);
else if (Token::Match(tok->tokAt(-2), "&& ( errno == EINTR ) ) { ;| }"))
endpar = tok->tokAt(4);
else
continue;
if (Token::simpleMatch(endpar->link()->previous(), "while ("))
{
Token *tok1 = tok->previous();
if (tok1->str() == "(")
tok1 = tok1->previous();
// erase "&& errno == EINTR"
Token::eraseTokens(tok1->previous(), endpar);
// tok is invalid.. move to endpar
tok = endpar;
}
}
}
void Tokenizer::simplifyFuncInWhile()
{
for (Token *tok = _tokens; tok; tok = tok->next())

View File

@ -366,6 +366,11 @@ public:
*/
void simplifyWhile0();
/**
* Simplify while(func() && errno==EINTR)
*/
void simplifyErrNoInWhile();
/**
* Simplify while(func(f))
*/

View File

@ -243,6 +243,9 @@ private:
// x = realloc(y,0); => free(y);x=0;
TEST_CASE(simplifyRealloc);
// while(f() && errno==EINTR) { } => while (f()) { }
TEST_CASE(simplifyErrNoInWhile);
// while(fclose(f)); => r = fclose(f); while(r){r=fclose(f);}
TEST_CASE(simplifyFuncInWhile);
@ -4938,6 +4941,14 @@ private:
tok("; p = realloc(p,0);"));
}
void simplifyErrNoInWhile()
{
ASSERT_EQUALS("; while ( f ( ) ) { }",
tok("; while (f() && errno == EINTR) { }"));
ASSERT_EQUALS("; while ( f ( ) ) { }",
tok("; while (f() && (errno == EINTR)) { }"));
}
void simplifyFuncInWhile()
{
ASSERT_EQUALS("int cppcheck:r = fclose ( f ) ; "