From c7b068c17472e73c5718814c0bec254cb50f1bcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 1 Mar 2009 16:37:02 +0000 Subject: [PATCH] variable id: fixed so that the variable ids are assigned correctly (ticket:126) --- src/tokenize.cpp | 6 +----- test/testother.cpp | 2 +- test/testtokenize.cpp | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/tokenize.cpp b/src/tokenize.cpp index cc7b232d9..861e54e2a 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -516,16 +516,12 @@ void Tokenizer::setVarId() if (tok != _tokens && !Token::Match(tok, "[;{}(]")) continue; - if (Token::Match(tok, "[;{}(] %any%")) + if (Token::Match(tok, "[;{}(] %type%")) tok = tok->next(); if (Token::Match(tok, "else|return")) continue; - if (!(firstMatch = Token::Match(tok, "%type% *| %var%")) - && !Token::Match(tok, "%type% %type% *| %var%")) - continue; - // Determine name of declared variable.. const char *varname = 0; Token *tok2 = tok->tokAt(firstMatch ? 1 : 2); diff --git a/test/testother.cpp b/test/testother.cpp index 66240ee81..571450008 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -256,7 +256,7 @@ private: "{\n" " char str[100] = {0};\n" " return str;\n" - "\n"); + "}\n"); ASSERT_EQUALS(std::string("[test.cpp:4]: (error) Returning pointer to local array variable\n"), errout.str()); } }; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 456eac129..3205f1f21 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -99,6 +99,7 @@ private: TEST_CASE(varid4); TEST_CASE(varid5); // TODO TEST_CASE(varid6); // Function parameters aren't handled well yet + TEST_CASE(varid7); TEST_CASE(varidReturn); TEST_CASE(file1); @@ -877,6 +878,38 @@ private: ASSERT_EQUALS(expected, actual); } + + void varid7() + { + const std::string code("void func()\n" + "{\n" + " char a[256] = \"test\";\n" + " {\n" + " char b[256] = \"test\";\n" + " }\n" + "}\n"); + + // tokenize.. + Tokenizer tokenizer; + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); + tokenizer.setVarId(); + + // result.. + const std::string actual(tokenizer.tokens()->stringifyList(true)); + const std::string expected("\n" + "1: void func ( )\n" + "2: {\n" + "3: char a@1 [ 256 ] = \"test\" ;\n" + "4: {\n" + "5: char b@2 [ 256 ] = \"test\" ;\n" + "6: }\n" + "7: }\n"); + + ASSERT_EQUALS(expected, actual); + } + + void varidReturn() { const std::string code("int f()\n"