Fix #703 (Tokenizer: Create links for [])

http://sourceforge.net/apps/trac/cppcheck/ticket/703
This commit is contained in:
Reijo Tomperi 2009-09-20 23:13:06 +03:00
parent 2dc1fe5dc1
commit 1c01dd64f3
2 changed files with 87 additions and 0 deletions

View File

@ -1204,6 +1204,7 @@ bool Tokenizer::createLinks()
{ {
std::list<Token*> links; std::list<Token*> links;
std::list<Token*> links2; std::list<Token*> links2;
std::list<Token*> links3;
for (Token *token = _tokens; token; token = token->next()) for (Token *token = _tokens; token; token = token->next())
{ {
if (token->link()) if (token->link())
@ -1243,6 +1244,22 @@ bool Tokenizer::createLinks()
Token::createMutualLinks(links2.back(), token); Token::createMutualLinks(links2.back(), token);
links2.pop_back(); links2.pop_back();
} }
else if (token->str() == "[")
{
links3.push_back(token);
}
else if (token->str() == "]")
{
if (links3.size() == 0)
{
// Error, ( and ) don't match.
syntaxError(token, '[');
return false;
}
Token::createMutualLinks(links3.back(), token);
links3.pop_back();
}
} }
if (links.size() > 0) if (links.size() > 0)
@ -1259,6 +1276,13 @@ bool Tokenizer::createLinks()
return false; return false;
} }
if (links3.size() > 0)
{
// Error, [ and ] don't match.
syntaxError(_tokens, '[');
return false;
}
return true; return true;
} }

View File

@ -150,6 +150,7 @@ private:
// unsigned i; => unsigned int i; // unsigned i; => unsigned int i;
TEST_CASE(unsigned1); TEST_CASE(unsigned1);
TEST_CASE(testUpdateClassList); TEST_CASE(testUpdateClassList);
TEST_CASE(createLinks);
} }
@ -2376,6 +2377,68 @@ private:
} }
} }
void createLinks()
{
{
const char code[] = "class A{\n"
" void f() {}\n"
"};";
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
const Token *tok = tokenizer.tokens();
// A body {}
ASSERT_EQUALS(true, tok->tokAt(2)->link() == tok->tokAt(9));
ASSERT_EQUALS(true, tok->tokAt(9)->link() == tok->tokAt(2));
// f body {}
ASSERT_EQUALS(true, tok->tokAt(7)->link() == tok->tokAt(8));
ASSERT_EQUALS(true, tok->tokAt(8)->link() == tok->tokAt(7));
// f ()
ASSERT_EQUALS(true, tok->tokAt(5)->link() == tok->tokAt(6));
ASSERT_EQUALS(true, tok->tokAt(6)->link() == tok->tokAt(5));
}
{
const char code[] = "void f(){\n"
" char a[10];\n"
" char *b ; b = new char[a[0]];\n"
"};";
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
const Token *tok = tokenizer.tokens();
// a[10]
ASSERT_EQUALS(true, tok->tokAt(7)->link() == tok->tokAt(9));
ASSERT_EQUALS(true, tok->tokAt(9)->link() == tok->tokAt(7));
// new char[]
ASSERT_EQUALS(true, tok->tokAt(19)->link() == tok->tokAt(24));
ASSERT_EQUALS(true, tok->tokAt(24)->link() == tok->tokAt(19));
// a[0]
ASSERT_EQUALS(true, tok->tokAt(21)->link() == tok->tokAt(23));
ASSERT_EQUALS(true, tok->tokAt(23)->link() == tok->tokAt(21));
}
{
const char code[] = "void f(){\n"
" foo(g());\n"
"};";
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
const Token *tok = tokenizer.tokens();
// foo(
ASSERT_EQUALS(true, tok->tokAt(6)->link() == tok->tokAt(10));
ASSERT_EQUALS(true, tok->tokAt(10)->link() == tok->tokAt(6));
// g(
ASSERT_EQUALS(true, tok->tokAt(8)->link() == tok->tokAt(9));
ASSERT_EQUALS(true, tok->tokAt(9)->link() == tok->tokAt(8));
}
}
}; };
REGISTER_TEST(TestTokenizer) REGISTER_TEST(TestTokenizer)