Fixed ticket #539 (Tokenizer: don't replace constants variables with varid 0)

http://sourceforge.net/apps/trac/cppcheck/ticket/539
This commit is contained in:
Slava Semushin 2009-08-01 20:57:54 +07:00
parent b5d172b6e9
commit 0e2273833e
2 changed files with 26 additions and 0 deletions

View File

@ -1400,6 +1400,12 @@ void Tokenizer::simplifyTokenList()
if (Token::Match(tok, "const %type% %var% = %num% ;"))
{
unsigned int varId = tok->tokAt(2)->varId();
if (varId == 0)
{
tok = tok->tokAt(5);
continue;
}
const char *num = tok->strAt(4);
int indent = 1;
for (Token *tok2 = tok->tokAt(6); tok2; tok2 = tok2->next())

View File

@ -98,6 +98,7 @@ private:
TEST_CASE(simplifyKnownVariables9);
TEST_CASE(simplifyKnownVariables10);
TEST_CASE(simplifyKnownVariables11);
TEST_CASE(simplifyKnownVariables12);
TEST_CASE(match1);
@ -864,6 +865,25 @@ private:
ASSERT_EQUALS(" const int foo = 0 ; int main ( ) { int foo ; foo = 0 ; }", ostr.str());
}
void simplifyKnownVariables12()
{
const char code[] = "ENTER_NAMESPACE(project_namespace)\n"
"const double pi = 3.14;\n"
"int main(){}\n";
// tokenize..
OurTokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.setVarId();
tokenizer.simplifyTokenList();
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(" ENTER_NAMESPACE ( project_namespace ) const double pi = 3.14 ; int main ( ) { }", ostr.str());
}
void match1()
{
// Match "%var% | %var%"