Skip to content

Commit f34e9e6

Browse files
author
Daniel Marjamäki
committed
Tokenizer: better simplification of calculations in conditions
1 parent 64fa7bf commit f34e9e6

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

lib/tokenize.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7383,7 +7383,8 @@ bool Tokenizer::simplifyCalculations()
73837383
Token::Match(tok, "<< %num% [+-*/] %num% [],);=<>+-*/|&^]") ||
73847384
Token::Match(tok, "[[,(=<>+-*|&^] %num% [+-*/] %num% <<|>>") ||
73857385
Token::Match(tok, "<< %num% [+-*/] %num% <<") ||
7386-
Token::Match(tok, "[(,[] %num% [|&^] %num% [];,);]"))
7386+
Token::Match(tok, "[(,[] %num% [|&^] %num% [];,);]") ||
7387+
Token::Match(tok, "(|==|!=|<=|>=|<|>|+ %num% [+-*/] %num% ==|!=|<=|>=|<|>|)"))
73877388
{
73887389
tok = tok->next();
73897390

test/testsimplifytokens.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2719,6 +2719,12 @@ class TestSimplifyTokens : public TestFixture
27192719
ASSERT_EQUALS("; a [ 0 ] ;", tok(";a[0*(*p)];"));
27202720

27212721
ASSERT_EQUALS(";", tok("; x = x + 0;"));
2722+
2723+
ASSERT_EQUALS("if ( a == 2 )", tok("if (a==1+1)"));
2724+
ASSERT_EQUALS("if ( a + 2 != 6 )", tok("if (a+1+1!=1+2+3)"));
2725+
2726+
// TODO: "4/4" should be simplified
2727+
TODO_ASSERT_EQUALS("if ( 4 < a )", "if ( 4 < a * 4 / 4 )", tok("if (14-2*5<a*4/(2*2))"));
27222728
}
27232729

27242730

0 commit comments

Comments
 (0)