STL boundries: added new check written by Bill Eggert (#247)

This commit is contained in:
Daniel Marjamäki 2009-04-13 17:48:13 +02:00
parent 1c0cd48321
commit 7750f327ff
3 changed files with 55 additions and 0 deletions

View File

@ -281,3 +281,35 @@ void CheckStl::pushbackError(const Token *tok, const std::string &iterator_name)
}
void CheckStl::stlBoundries()
{
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{
if (Token::Match(tok, "for ("))
{
for (const Token *tok2 = tok->tokAt(2); tok2 && tok2->str() != ";"; tok2 = tok2->next())
{
if (Token::Match(tok2, "%var% = %var% . begin ( ) ; %var% < %var% . end ( ) ") &&
tok2->str() == tok2->tokAt(8)->str() &&
tok2->tokAt(2)->str() == tok2->tokAt(10)->str())
{
stlBoundriesError(tok2);
break;
}
}
}
if (Token::Match(tok, "while ( %var% < %var% . end ( )"))
{
stlBoundriesError(tok);
}
}
}
// Error message for bad boundry usage..
void CheckStl::stlBoundriesError(const Token *tok)
{
reportError(tok, "error", "stlBoundries", "STL range check should be using != and not < since the order of the pointers isn't guaranteed");
}

View File

@ -47,6 +47,7 @@ public:
checkStl.iterators();
checkStl.erase();
checkStl.pushback();
checkStl.stlBoundries();
}
@ -72,6 +73,11 @@ public:
*/
void pushback();
/**
* bad condition.. "it < alist.end()"
*/
void stlBoundries();
private:
/**
@ -85,6 +91,7 @@ private:
void iteratorsError(const Token *tok, const std::string &container1, const std::string &container2);
void eraseError(const Token *tok);
void pushbackError(const Token *tok, const std::string &iterator_name);
void stlBoundriesError(const Token *tok);
void getErrorMessages()
{
@ -92,6 +99,7 @@ private:
stlOutOfBoundsError(0, "i", "foo");
eraseError(0);
pushbackError(0, "iterator");
stlBoundriesError(0);
}
};

View File

@ -47,6 +47,8 @@ private:
TEST_CASE(pushback1);
TEST_CASE(invalidcode);
TEST_CASE(stlBoundries1);
}
void check(const char code[])
@ -252,6 +254,19 @@ private:
"}\n");
ASSERT_EQUALS("", errout.str());
}
void stlBoundries1()
{
check("void f()\n"
"{\n"
" for (it = ab.begin(); it < ab.end(); ++it)\n"
" ;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) STL range check should be using != and not < since the order of the pointers isn't guaranteed\n", errout.str());
}
};
REGISTER_TEST(TestStl)