fixed todo test case - detect iterator usage after push_front|push_back

This commit is contained in:
Daniel Marjamäki 2009-07-14 12:40:47 +02:00
parent 69b1df3bb8
commit 456c20b705
2 changed files with 32 additions and 1 deletions

View File

@ -288,6 +288,37 @@ void CheckStl::pushback()
--indent; --indent;
} }
// Using push_back or push_front inside a loop..
if (Token::Match(tok2, "for ( %varid% = %var% . begin ( ) ; %varid% != %var% . end ( ) ; ++ %varid% ) {", iteratorid))
{
const unsigned int vectorid(tok2->tokAt(4)->varId());
const Token *pushback = 0;
int indent3 = 0;
for (const Token *tok3 = tok2->tokAt(22); tok3; tok3 = tok3->next())
{
if (tok3->str() == "{")
++indent3;
else if (tok3->str() == "}")
{
if (indent3 == 0)
break;
--indent3;
}
else if (tok3->str() == "break")
{
pushback = 0;
break;
}
else if (Token::Match(tok3, "%varid% . push_front|push_back (", vectorid))
{
pushback = tok3;
}
}
if (pushback)
pushbackError(pushback, tok2->strAt(2));
}
// Assigning iterator.. // Assigning iterator..
if (Token::Match(tok2, "%varid% = %var% . begin ( )", iteratorid)) if (Token::Match(tok2, "%varid% = %var% . begin ( )", iteratorid))
{ {

View File

@ -323,7 +323,7 @@ private:
" foo.push_back(123);\n" " foo.push_back(123);\n"
" }\n" " }\n"
"}\n"); "}\n");
TODO_ASSERT_EQUALS("[test.cpp:6]: (error) After push_back or push_front, the iterator 'it' may be invalid\n", errout.str()); ASSERT_EQUALS("[test.cpp:6]: (error) After push_back or push_front, the iterator 'it' may be invalid\n", errout.str());
} }
void pushback4() void pushback4()