variable id: give std::string variables an id

This commit is contained in:
Daniel Marjamäki 2009-03-25 18:10:03 +01:00
parent 8db1ac8c5d
commit c74da7bab9
2 changed files with 46 additions and 1 deletions

View File

@ -626,6 +626,9 @@ void Tokenizer::setVarId()
if (Token::Match(tok, "else|return")) if (Token::Match(tok, "else|return"))
continue; continue;
if (Token::simpleMatch(tok, "std ::"))
tok = tok->tokAt(2);
// Determine name of declared variable.. // Determine name of declared variable..
const char *varname = 0; const char *varname = 0;
Token *tok2 = tok->tokAt(1); Token *tok2 = tok->tokAt(1);
@ -638,8 +641,24 @@ void Tokenizer::setVarId()
tok2 = tok2->next(); tok2 = tok2->next();
} }
// Is it a function?
if (tok2->str() == "(")
{
bool isfunc = false;
for (const Token *tok3 = tok2; tok3; tok3 = tok3->next())
{
if (tok3->str() == ")")
{
isfunc = Token::simpleMatch(tok3, ") {");
break;
}
}
if (isfunc)
continue;
}
// Variable declaration found => Set variable ids // Variable declaration found => Set variable ids
if (Token::Match(tok2, "[,);[=]") && varname) if (Token::Match(tok2, "[,();[=]") && varname)
{ {
++_varId; ++_varId;
int indentlevel = 0; int indentlevel = 0;

View File

@ -105,6 +105,7 @@ private:
// TODO TEST_CASE(varid6); // Function parameters aren't handled well yet // TODO TEST_CASE(varid6); // Function parameters aren't handled well yet
TEST_CASE(varid7); TEST_CASE(varid7);
TEST_CASE(varidReturn); TEST_CASE(varidReturn);
TEST_CASE(varid8);
TEST_CASE(file1); TEST_CASE(file1);
TEST_CASE(file2); TEST_CASE(file2);
@ -1002,6 +1003,31 @@ private:
ASSERT_EQUALS(expected, actual); ASSERT_EQUALS(expected, actual);
} }
void varid8()
{
const std::string code("void func()\n"
"{\n"
" std::string str(\"test\");\n"
" str.clear();\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: void func ( )\n"
"2: {\n"
"3: std :: string str@1 ( \"test\" ) ;\n"
"4: str@1 . clear@2 ( ) ;\n"
"5: }\n");
ASSERT_EQUALS(expected, actual);
}