Fixed ticket #522 (Tokenizer: recognize comma in container with STL type declaration)

http://sourceforge.net/apps/trac/cppcheck/ticket/522
This commit is contained in:
Slava Semushin 2009-07-29 02:46:33 +07:00
parent 524b0cd9b8
commit 8b0c1daf98
2 changed files with 73 additions and 0 deletions

View File

@ -2309,6 +2309,61 @@ bool Tokenizer::simplifyVarDecl()
tok2 = tok2->tokAt(6); // The ',' token
}
else if (Token::Match(tok2, "std :: %type% <") || Token::Match(tok2, "%type% <"))
{
//
// Deal with templates and standart types
//
if (Token::simpleMatch(tok2, "std ::"))
{
typelen += 1;
tok2 = tok2->tokAt(2);
}
typelen += 2;
tok2 = tok2->tokAt(2);
size_t indentlevel = 1;
for (Token *tok3 = tok2; tok3; tok3 = tok3->next())
{
++typelen;
if (tok3->str() == "<")
{
++indentlevel;
}
else if (tok3->str() == ">")
{
--indentlevel;
if (indentlevel == 0)
{
tok2 = tok3->next();
break;
}
}
}
if (Token::Match(tok2, ":: %type%"))
{
typelen += 2;
tok2 = tok2->tokAt(2);
}
if (tok2->str() == "*" || tok2->str() == "&")
{
tok2 = tok2->next();
}
if (Token::Match(tok2, "%var% ,"))
{
tok2 = tok2->next(); // The ',' token
}
else
{
tok2 = NULL;
typelen = 0;
}
}
else
{
tok2 = NULL;

View File

@ -153,6 +153,7 @@ private:
TEST_CASE(vardecl3);
TEST_CASE(vardecl4);
TEST_CASE(vardecl5);
TEST_CASE(vardecl_stl);
TEST_CASE(volatile_variables);
TEST_CASE(syntax_error);
@ -2202,6 +2203,23 @@ private:
ASSERT_EQUALS(res4, tokenizeAndStringify(code4));
}
void vardecl_stl()
{
// ticket #520
const char code1[] = "std::vector<std::string>a, b;";
const char res1[] = "std :: vector < std :: string > a ; std :: vector < std :: string > b ;";
ASSERT_EQUALS(res1, tokenizeAndStringify(code1));
const char code2[] = "std::vector<std::string>::const_iterator it, cit;";
const char res2[] = "std :: vector < std :: string > :: const_iterator it ; std :: vector < std :: string > :: const_iterator cit ;";
ASSERT_EQUALS(res2, tokenizeAndStringify(code2));
const char code3[] = "std::vector<std::pair<std::string, std::string > > *c, d;";
const char res3[] = "std :: vector < std :: pair < std :: string , std :: string > > * c ; std :: vector < std :: pair < std :: string , std :: string > > d ;";
ASSERT_EQUALS(res3, tokenizeAndStringify(code3));
}
void vardecl5()
{
// don't simplify declarations of static variables