Tokenizer: Fixed bug in setVarId. functions should never have a variable id

This commit is contained in:
Daniel Marjamäki 2010-02-07 13:04:33 +01:00
parent ed8f3bc806
commit 1b0c81a24b
2 changed files with 33 additions and 3 deletions

View File

@ -1901,12 +1901,12 @@ void Tokenizer::setVarId()
}
// Determine name of declared variable..
const char *varname = 0;
std::string varname;
Token *tok2 = tok->tokAt(1);
while (tok2)
{
if (tok2->isName())
varname = tok2->strAt(0);
varname = tok2->str();
else if (tok2->str() != "*" && tok2->str() != "&")
break;
tok2 = tok2->next();
@ -1916,6 +1916,9 @@ void Tokenizer::setVarId()
if (!tok2)
break;
if (varname == "operator" && Token::Match(tok2, "=|+|-|*|/|[| ]| ("))
continue;
// Is it a function?
if (tok2->str() == "(")
{
@ -1938,7 +1941,7 @@ void Tokenizer::setVarId()
}
// Variable declaration found => Set variable ids
if (Token::Match(tok2, "[,();[=]") && varname)
if (Token::Match(tok2, "[,();[=]") && !varname.empty())
{
++_varId;
int indentlevel = 0;

View File

@ -120,6 +120,7 @@ private:
TEST_CASE(varid_functions);
TEST_CASE(varid_reference_to_containers);
TEST_CASE(varid_in_class);
TEST_CASE(varid_operator);
TEST_CASE(varidclass1);
TEST_CASE(varidclass2);
@ -1742,6 +1743,32 @@ private:
ASSERT_EQUALS(expected, actual);
}
void varid_operator()
{
const std::string code("class Foo\n"
"{\n"
"public:\n"
" void operator=(const Foo &);\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\n##file 0\n"
"1: class Foo\n"
"2: {\n"
"3: public:\n"
"4: void operator = ( const Foo & ) ;\n"
"5: } ;\n");
ASSERT_EQUALS(expected, actual);
}
void varidclass1()
{
const std::string code("class Fred\n"