Tokenizer: Better handling of constants. Related with #2920

This commit is contained in:
Daniel Marjamäki 2011-07-20 09:07:47 +02:00
parent 3cfef6285c
commit 6ce5107e49
2 changed files with 28 additions and 12 deletions

View File

@ -6709,8 +6709,20 @@ bool Tokenizer::simplifyKnownVariables()
std::map<unsigned int, std::string> constantValues;
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (Token::Match(tok, "static| const static| %type% %var% = %any% ;"))
if (tok->isName() && Token::Match(tok, "static| const| static| %type% const| %var% = %any% ;"))
{
bool isconst = false;
for (const Token *tok2 = tok; tok2->str() != "="; tok2 = tok2->next())
{
if (tok2->str() == "const")
{
isconst = true;
break;
}
}
if (!isconst)
continue;
Token *tok1 = tok;
// start of statement
@ -6723,8 +6735,8 @@ bool Tokenizer::simplifyKnownVariables()
if (!tok->isStandardType())
continue;
const Token * const vartok = tok->next();
const Token * const valuetok = tok->tokAt(3);
const Token * const vartok = (tok->strAt(1) == "const") ? tok->tokAt(2) : tok->next();
const Token * const valuetok = vartok->tokAt(2);
if (valuetok->isNumber() || Token::Match(valuetok, "%str% ;"))
{
constantValues[vartok->varId()] = valuetok->str();

View File

@ -1957,15 +1957,19 @@ private:
void simplifyKnownVariables32()
{
const char code[] = "void foo() {\n"
" const int x = 0;\n"
" bar(0,x);\n"
"}\n";
const char expected[] = "void foo ( ) {\n"
";\n"
"bar ( 0 , 0 ) ;\n"
"}";
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
{
const char code[] = "void foo() {\n"
" const int x = 0;\n"
" bar(0,x);\n"
"}\n";
const char expected[] = "void foo ( ) {\n;\nbar ( 0 , 0 ) ;\n}";
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
}
{
const char code[] = "static int const SZ = 22; char str[SZ];\n";
ASSERT_EQUALS("; char str [ 22 ] ;", tokenizeAndStringify(code,true));
}
}
void simplifyKnownVariables33()