variable id: fixed so that the variable ids are assigned correctly (ticket:126)

This commit is contained in:
Daniel Marjamäki 2009-03-01 16:37:02 +00:00
parent b39c15410b
commit c7b068c174
3 changed files with 35 additions and 6 deletions

View File

@ -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);

View File

@ -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());
}
};

View File

@ -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"