From 1c01dd64f353d6872ec4b04867393aa283423445 Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Sun, 20 Sep 2009 23:13:06 +0300 Subject: [PATCH] Fix #703 (Tokenizer: Create links for []) http://sourceforge.net/apps/trac/cppcheck/ticket/703 --- src/tokenize.cpp | 24 +++++++++++++++++ test/testtokenize.cpp | 63 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 3cbdba3c5..9557b56a5 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -1204,6 +1204,7 @@ bool Tokenizer::createLinks() { std::list links; std::list links2; + std::list links3; for (Token *token = _tokens; token; token = token->next()) { if (token->link()) @@ -1243,6 +1244,22 @@ bool Tokenizer::createLinks() Token::createMutualLinks(links2.back(), token); 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) @@ -1259,6 +1276,13 @@ bool Tokenizer::createLinks() return false; } + if (links3.size() > 0) + { + // Error, [ and ] don't match. + syntaxError(_tokens, '['); + return false; + } + return true; } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index abe5e13b3..3d33c20ff 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -150,6 +150,7 @@ private: // unsigned i; => unsigned int i; TEST_CASE(unsigned1); 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)