Tokenizer: Simplify float casts of integer constants

This commit is contained in:
Daniel Marjamäki 2013-11-19 18:07:12 +01:00
parent f8cf64afef
commit 47b98470eb
3 changed files with 27 additions and 0 deletions

View File

@ -1994,6 +1994,9 @@ bool Tokenizer::tokenize(std::istream &code,
simplifyVariableMultipleAssign();
// Simplify float casts (float)1 => 1.0
simplifyFloatCasts();
// Remove redundant parentheses
simplifyRedundantParentheses();
for (Token *tok = list.front(); tok; tok = tok->next())
@ -4789,6 +4792,17 @@ void Tokenizer::simplifyUndefinedSizeArray()
}
}
void Tokenizer::simplifyFloatCasts()
{
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (Token::Match(tok->next(), "( float|double ) %num%") && MathLib::isInt(tok->strAt(4))) {
tok->deleteNext(3);
tok = tok->next();
tok->str(tok->str() + ".0");
}
}
}
void Tokenizer::simplifyCasts()
{
for (Token *tok = list.front(); tok; tok = tok->next()) {

View File

@ -325,6 +325,11 @@ public:
*/
void simplifyTypedef();
/**
* Simplify float casts (float)1 => 1.0
*/
void simplifyFloatCasts();
/**
* Simplify casts
*/

View File

@ -97,6 +97,8 @@ private:
TEST_CASE(removeCast12);
TEST_CASE(removeCast13);
TEST_CASE(simplifyFloatCasts); // float casting a integer
TEST_CASE(inlineasm);
TEST_CASE(ifAddBraces1);
@ -1123,6 +1125,12 @@ private:
tokenizeAndStringify("; float angle = (float) +tilt;", true));
}
void simplifyFloatCasts() { // float casting integers
ASSERT_EQUALS("a = 1.0 ;", tokenizeAndStringify("a = (float)1;"));
ASSERT_EQUALS("a = 1.0 ;", tokenizeAndStringify("a = ((float)1);"));
ASSERT_EQUALS("a = 291.0 ;", tokenizeAndStringify("a = ((float)0x123);"));
}
void inlineasm() {
ASSERT_EQUALS("asm ( \"mov ax , bx\" ) ;", tokenizeAndStringify("asm { mov ax,bx };"));
ASSERT_EQUALS("asm ( \"mov ax , bx\" ) ;", tokenizeAndStringify("_asm { mov ax,bx };"));