Fixed #2454 (Tokenizer::simplifyKnownVariables: problem with float/double variables)

This commit is contained in:
Daniel Marjamäki 2011-01-15 08:48:42 +01:00
parent cf32016444
commit 97d0755750
2 changed files with 34 additions and 0 deletions

View File

@ -6206,6 +6206,9 @@ bool Tokenizer::simplifyKnownVariables()
}
}
// variable id for float/double variables
std::set<unsigned int> floatvars;
// auto variables..
for (Token *tok = _tokens; tok; tok = tok->next())
{
@ -6218,6 +6221,11 @@ bool Tokenizer::simplifyKnownVariables()
Token *tok2 = tok;
for (; tok2; tok2 = tok2->next())
{
if (Token::Match(tok2, "[;{}] float|double %var% ;"))
{
floatvars.insert(tok2->tokAt(2)->varId());
}
if (tok2->str() == "{")
++indentlevel;
@ -6325,8 +6333,18 @@ bool Tokenizer::simplifyKnownVariables()
if (tok2->strAt(4) == ";")
valueIsPointer = true;
}
// float value should contain a "."
else if (tok2->tokAt(2)->isNumber() &&
floatvars.find(tok2->varId()) != floatvars.end() &&
value.find(".") == std::string::npos)
{
value += ".0";
}
if (Token::simpleMatch(tok2->next(), "= &"))
tok2 = tok2->tokAt(3);
tok3 = tok2->next();
}
Token* bailOutFromLoop = 0;

View File

@ -132,6 +132,7 @@ private:
TEST_CASE(simplifyKnownVariablesBailOutMemberFunction);
TEST_CASE(simplifyKnownVariablesBailOutConditionalIncrement);
TEST_CASE(simplifyKnownVariablesBailOutSwitchBreak); // ticket #2324
TEST_CASE(simplifyKnownVariablesFloat); // #2454 - float variable
TEST_CASE(varid1);
TEST_CASE(varid2);
@ -2104,6 +2105,21 @@ private:
ASSERT_EQUALS(expected, tokenizeAndStringify(code,true));
}
void simplifyKnownVariablesFloat()
{
// Ticket #2454
const char code[] = "void f() {\n"
" float a = 40;\n"
" x(10 / a);\n"
"}\n";
const char expected[] = "void f ( ) {\n;\nx ( 0.25 ) ;\n}";
ASSERT_EQUALS(expected, tokenizeAndStringify(code,true));
}
std::string tokenizeDebugListing(const std::string &code, bool simplify = false)
{
errout.str("");