From 71b4e5a912fc48ecb3425f2ea4e0fa7d477f657b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 10 Feb 2009 20:01:39 +0000 Subject: [PATCH] stl: added testcase for bad iterator usage --- src/checkstl.cpp | 14 ++++++++++++++ test/teststl.cpp | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/checkstl.cpp b/src/checkstl.cpp index 8b5882800..e21454e6f 100644 --- a/src/checkstl.cpp +++ b/src/checkstl.cpp @@ -35,6 +35,7 @@ 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 ( ) ;")) { // Different iterators.. @@ -45,6 +46,19 @@ void CheckStl::iterators() continue; _errorLogger->iteratorUsage(_tokenizer, tok, tok->tokAt(2)->str(), tok->tokAt(10)->str()); } + + // it = foo.begin(); + // while (it != bar.end()) + if (Token::Match(tok, "%var% = %var% . begin ( ) ; while ( %var% != %var% . end ( )")) + { + // Different iterators.. + if (tok->str() != tok->tokAt(10)->str()) + continue; + // Same container.. + if (tok->tokAt(2)->str() == tok->tokAt(12)->str()) + continue; + _errorLogger->iteratorUsage(_tokenizer, tok, tok->tokAt(2)->str(), tok->tokAt(12)->str()); + } } } diff --git a/test/teststl.cpp b/test/teststl.cpp index 435db8e47..a46025b61 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -37,6 +37,7 @@ private: void run() { TEST_CASE(iterator1); + TEST_CASE(iterator2); TEST_CASE(STLSize); TEST_CASE(STLSizeNoErr); } @@ -68,6 +69,19 @@ private: ASSERT_EQUALS("[test.cpp:3]: (error) Same iterator is used with both foo and bar\n", errout.str()); } + void iterator2() + { + check("void foo()\n" + "{\n" + " it = foo.begin();\n" + " while (it != bar.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()); + } + void STLSize() {