Fixed #8059 (Tokenizer::simplifyWhile0: for loop that modifies local variable)

This commit is contained in:
Daniel Marjamäki 2017-07-09 22:44:10 +02:00
parent dff9d13758
commit f85e43d21f
2 changed files with 7 additions and 3 deletions

View File

@ -8310,6 +8310,8 @@ void Tokenizer::simplifyWhile0()
if (Token::simpleMatch(tok->next()->link(), ") {")) {
Token *end = tok->next()->link(), *old_prev = tok->previous();
end = end->next()->link();
if (Token::Match(tok, "for ( %name% ="))
old_prev = end->link();
eraseDeadCode(old_prev, end->next());
if (old_prev && old_prev->next())
tok = old_prev->next();

View File

@ -2814,15 +2814,17 @@ private:
void while0for() {
// for (condition is always false)
ASSERT_EQUALS("void f ( ) { }", tok("void f() { int i; for (i = 0; i < 0; i++) { a; } }"));
ASSERT_EQUALS("void f ( ) { int i ; for ( i = 0 ; i < 0 ; i ++ ) { } }", tok("void f() { int i; for (i = 0; i < 0; i++) { a; } }"));
//ticket #3140
ASSERT_EQUALS("void f ( ) { }", tok("void f() { int i; for (i = 0; i < 0; i++) { foo(); break; } }"));
ASSERT_EQUALS("void f ( ) { }", tok("void f() { int i; for (i = 0; i < 0; i++) { foo(); continue; } }"));
ASSERT_EQUALS("void f ( ) { int i ; for ( i = 0 ; i < 0 ; i ++ ) { } }", tok("void f() { int i; for (i = 0; i < 0; i++) { foo(); break; } }"));
ASSERT_EQUALS("void f ( ) { int i ; for ( i = 0 ; i < 0 ; i ++ ) { } }", tok("void f() { int i; for (i = 0; i < 0; i++) { foo(); continue; } }"));
ASSERT_EQUALS("void f ( ) { }", tok("void f() { for (int i = 0; i < 0; i++) { a; } }"));
ASSERT_EQUALS("void f ( ) { }", tok("void f() { for (unsigned int i = 0; i < 0; i++) { a; } }"));
ASSERT_EQUALS("void f ( ) { }", tok("void f() { for (long long i = 0; i < 0; i++) { a; } }"));
ASSERT_EQUALS("void f ( ) { }", tok("void f() { for (signed long long i = 0; i < 0; i++) { a; } }"));
ASSERT_EQUALS("void f ( ) { }", tok("void f() { int n = 0; for (signed long long i = 0; i < n; i++) { a; } }"));
// #8059
ASSERT_EQUALS("void f ( ) { int i ; for ( i = 0 ; i < 0 ; ++ i ) { } return i ; }", tok("void f() { int i; for (i=0;i<0;++i){ dostuff(); } return i; }"));
}
void while1() {