Constant variable converting converted struct members (foo.a => foo.45) also, fixed that.

This commit is contained in:
Reijo Tomperi 2009-02-14 21:33:28 +00:00
parent fba8c54758
commit 1e07847ecf
3 changed files with 60 additions and 29 deletions

View File

@ -482,8 +482,8 @@ void Token::printOut(const char *title) const
}
std::cout << t->str();
// if (t->varId())
// std::cout << " (" << t->varId() << ")";
if (t->varId())
std::cout << " (" << t->varId() << ")";
std::cout << " ";
}

View File

@ -639,33 +639,6 @@ void Tokenizer::simplifyTokenList()
}
}
// Replace constants..
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (Token::Match(tok, "const %type% %var% = %num% ;"))
{
const char *sym = tok->strAt(2);
const char *num = tok->strAt(4);
int indent = 1;
for (Token *tok2 = tok->tokAt(6); tok2; tok2 = tok2->next())
{
if (tok2->str() == "{")
{
++indent;
}
else if (tok2->str() == "}")
{
--indent;
if (indent == 0)
break;
}
else if (tok2->str() == sym)
{
tok2->str(num);
}
}
}
}
// Fill the map _typeSize..
@ -840,7 +813,37 @@ void Tokenizer::simplifyTokenList()
}
}
// Replace constants..
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (Token::Match(tok, "const %type% %var% = %num% ;"))
{
const char *sym = tok->strAt(2);
const char *num = tok->strAt(4);
int indent = 1;
for (Token *tok2 = tok->tokAt(6); tok2; tok2 = tok2->next())
{
if (tok2->str() == "{")
{
++indent;
}
else if (tok2->str() == "}")
{
--indent;
if (indent == 0)
break;
}
// Compare constants, but don't touch members of other structures
else if (tok2->str() == sym &&
tok2->previous() &&
tok2->previous()->str() != ".")
{
tok2->str(num);
}
}
}
}
// Simple calculations..

View File

@ -115,6 +115,7 @@ private:
TEST_CASE(tokenize_double);
TEST_CASE(tokenize_strings);
TEST_CASE(simplify_constants);
TEST_CASE(simplify_constants2);
}
@ -1142,6 +1143,33 @@ private:
ostr << " " << tok->str();
ASSERT_EQUALS(std::string(" void f ( ) { const int a = 45 ; { int b ; b = 45 ; } } void g ( ) { int a ; a = 2 ; }"), ostr.str());
}
void simplify_constants2()
{
const char code[] =
"void f( Foo &foo, Foo *foo2 )\n"
"{\n"
"const int a = 45;\n"
"foo.a=a+a;\n"
"foo2->a=a;\n"
"}\n";
// tokenize..
Tokenizer 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();
std::ostringstream oss;
oss << " void f ( Foo & foo , Foo * foo2 ) { const int a = 45 ; foo . a = 90 ; foo2 . a = 45 ; }";
ASSERT_EQUALS(oss.str(), ostr.str());
}
};
REGISTER_TEST(TestTokenizer)