stl: checking that iterator is used against a single container
This commit is contained in:
parent
2a200cff00
commit
ad4c7993d1
|
@ -40,30 +40,28 @@ void CheckStl::iterators()
|
|||
{
|
||||
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
||||
{
|
||||
// it = foo.begin(); it != bar.end()
|
||||
if (Token::Match(tok, "%var% = %var% . begin ( ) ; %var% != %var% . end ( ) ;"))
|
||||
if (Token::Match(tok, "%var% = %var% . begin ( ) ;"))
|
||||
{
|
||||
// Different iterators..
|
||||
if (tok->str() != tok->tokAt(8)->str())
|
||||
const unsigned int iteratorId(tok->varId());
|
||||
const unsigned int containerId(tok->tokAt(2)->varId());
|
||||
if (iteratorId == 0 || containerId == 0)
|
||||
continue;
|
||||
// Same container..
|
||||
if (tok->tokAt(2)->str() == tok->tokAt(10)->str())
|
||||
continue;
|
||||
iteratorsError(tok, tok->strAt(2), tok->strAt(10));
|
||||
|
||||
for (const Token *tok2 = tok->tokAt(6); tok2; tok2 = tok2->next())
|
||||
{
|
||||
if (tok2->str() == "}")
|
||||
break;
|
||||
if (tok2->varId() == iteratorId)
|
||||
{
|
||||
if (Token::Match(tok2->next(), "!= %var% . end ( )") && tok2->tokAt(2)->varId() != containerId)
|
||||
iteratorsError(tok2, tok->strAt(2), tok2->strAt(2));
|
||||
}
|
||||
|
||||
// it = foo.begin();
|
||||
// while (it != bar.end())
|
||||
if (Token::Match(tok, "%var% = %var% . begin ( ) ; while ( %var% != %var% . end ( )"))
|
||||
else if (Token::Match(tok2, "%var% . insert|erase ( %varid%", iteratorId))
|
||||
{
|
||||
// Different iterators..
|
||||
if (tok->str() != tok->tokAt(10)->str())
|
||||
continue;
|
||||
// Same container..
|
||||
if (tok->tokAt(2)->str() == tok->tokAt(12)->str())
|
||||
continue;
|
||||
|
||||
iteratorsError(tok, tok->strAt(2), tok->strAt(12));
|
||||
if (tok2->varId() != containerId)
|
||||
iteratorsError(tok2, tok->strAt(2), tok2->str());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -76,35 +76,41 @@ private:
|
|||
|
||||
void iterator1()
|
||||
{
|
||||
check("void foo()\n"
|
||||
check("void f()\n"
|
||||
"{\n"
|
||||
" for (it = foo.begin(); it != bar.end(); ++it)\n"
|
||||
" list<int> l1;\n"
|
||||
" list<int> l2;\n"
|
||||
" for (list<int>::iterator it = l1.begin(); it != l2.end(); ++it)\n"
|
||||
" { }\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:3]: (error) Same iterator is used with both foo and bar\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:5]: (error) Same iterator is used with both l1 and l2\n", errout.str());
|
||||
}
|
||||
|
||||
void iterator2()
|
||||
{
|
||||
check("void foo()\n"
|
||||
"{\n"
|
||||
" it = foo.begin();\n"
|
||||
" while (it != bar.end())\n"
|
||||
" list<int> l1;\n"
|
||||
" list<int> l2;\n"
|
||||
" list<int>::iterator it = l1.begin();\n"
|
||||
" while (it != l2.end())\n"
|
||||
" {\n"
|
||||
" ++it;\n"
|
||||
" }\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:3]: (error) Same iterator is used with both foo and bar\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:6]: (error) Same iterator is used with both l1 and l2\n", errout.str());
|
||||
}
|
||||
|
||||
void iterator3()
|
||||
{
|
||||
check("void foo()\n"
|
||||
"{\n"
|
||||
" i = l1.begin();\n"
|
||||
" l2.insert(i, 0);\n"
|
||||
" list<int> l1;\n"
|
||||
" list<int> l2;\n"
|
||||
" list<int>::iterator it = l1.begin();\n"
|
||||
" l2.insert(it, 0);\n"
|
||||
"}\n");
|
||||
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Same iterator is used with both foo and bar\n", errout.str());
|
||||
ASSERT_EQUALS("[test.cpp:6]: (error) Same iterator is used with both l1 and l2\n", errout.str());
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue