STL: refactoring and fixing

This commit is contained in:
Daniel Marjamäki 2009-11-02 21:53:01 +01:00
parent 89d94895a4
commit 87800185c4
2 changed files with 21 additions and 4 deletions

View File

@ -381,21 +381,28 @@ void CheckStl::pushback()
if (Token::Match(tok2, "%varid% =", iteratorid))
{
if (Token::Match(tok2->tokAt(2), "%var% . begin|end|rbegin|rend ( )"))
{
vectorname = tok2->strAt(2);
tok2 = tok2->tokAt(6);
}
else
vectorname = "";
invalidIterator = "";
}
// push_back on vector..
if (vectorname.size() && Token::Match(tok2, (vectorname + " . push_front|push_back|insert").c_str()))
if (vectorname.size() && Token::Match(tok2, (vectorname + " . push_front|push_back|insert (").c_str()))
{
if (!invalidIterator.empty() && Token::Match(tok2->tokAt(2), "insert ( %varid% ,", iteratorid))
{
invalidIteratorError(tok2, invalidIterator, tok2->strAt(4));
break;
}
invalidIterator = tok2->strAt(2);
if (!iteratorDeclaredInsideLoop)
{
while (tok2 && tok2->str() != "(")
tok2 = tok2->next();
tok2 = tok2 ? tok2->link() : 0;
tok2 = tok2->tokAt(3)->link();
if (!tok2)
break;
}

View File

@ -495,10 +495,20 @@ private:
check("void f()\n"
"{\n"
" std::vector<int> ints;\n"
" std::vector<int>::iterator iter = ints.begin();\n"
" ints.insert(iter, 1);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f()\n"
"{\n"
" std::vector<int> ints;\n"
" std::vector<int>::iterator iter = ints.begin();\n"
" ints.insert(iter, 1);\n"
" ints.insert(iter, 2);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6]: (error) After insert, the iterator 'iter' may be invalid\n", errout.str());
}