Tokenizer: Better handling of constants. Related with #2920
This commit is contained in:
parent
3cfef6285c
commit
6ce5107e49
|
@ -6709,8 +6709,20 @@ bool Tokenizer::simplifyKnownVariables()
|
||||||
std::map<unsigned int, std::string> constantValues;
|
std::map<unsigned int, std::string> constantValues;
|
||||||
for (Token *tok = _tokens; tok; tok = tok->next())
|
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;
|
Token *tok1 = tok;
|
||||||
|
|
||||||
// start of statement
|
// start of statement
|
||||||
|
@ -6723,8 +6735,8 @@ bool Tokenizer::simplifyKnownVariables()
|
||||||
if (!tok->isStandardType())
|
if (!tok->isStandardType())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
const Token * const vartok = tok->next();
|
const Token * const vartok = (tok->strAt(1) == "const") ? tok->tokAt(2) : tok->next();
|
||||||
const Token * const valuetok = tok->tokAt(3);
|
const Token * const valuetok = vartok->tokAt(2);
|
||||||
if (valuetok->isNumber() || Token::Match(valuetok, "%str% ;"))
|
if (valuetok->isNumber() || Token::Match(valuetok, "%str% ;"))
|
||||||
{
|
{
|
||||||
constantValues[vartok->varId()] = valuetok->str();
|
constantValues[vartok->varId()] = valuetok->str();
|
||||||
|
|
|
@ -1957,15 +1957,19 @@ private:
|
||||||
|
|
||||||
void simplifyKnownVariables32()
|
void simplifyKnownVariables32()
|
||||||
{
|
{
|
||||||
const char code[] = "void foo() {\n"
|
{
|
||||||
" const int x = 0;\n"
|
const char code[] = "void foo() {\n"
|
||||||
" bar(0,x);\n"
|
" const int x = 0;\n"
|
||||||
"}\n";
|
" bar(0,x);\n"
|
||||||
const char expected[] = "void foo ( ) {\n"
|
"}\n";
|
||||||
";\n"
|
const char expected[] = "void foo ( ) {\n;\nbar ( 0 , 0 ) ;\n}";
|
||||||
"bar ( 0 , 0 ) ;\n"
|
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
|
||||||
"}";
|
}
|
||||||
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()
|
void simplifyKnownVariables33()
|
||||||
|
|
Loading…
Reference in New Issue