diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 882faeee4..fd3d414f7 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -6206,6 +6206,9 @@ bool Tokenizer::simplifyKnownVariables() } } + // variable id for float/double variables + std::set 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; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 5d010aee3..25089a374 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -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("");