From bb2bda0be474e5041b5b45467fd8c05faa6ca04d Mon Sep 17 00:00:00 2001 From: danmar Date: Fri, 28 Aug 2009 12:13:46 +0200 Subject: [PATCH] Fixed #622 (Tokenizer: Calculations are wrong) --- src/tokenize.cpp | 9 +++++++++ test/testsimplifytokens.cpp | 3 +++ test/testsuite.h | 5 ++++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/tokenize.cpp b/src/tokenize.cpp index c540e70b4..99e6ba9e9 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -3100,6 +3100,15 @@ bool Tokenizer::simplifyCalculations() if (Token::simpleMatch(tok->next(), "/ 0")) continue; + // + and - are calculated after * + if (Token::Match(tok->next(),"[+-]")) + { + if (tok->previous()->str() == "*") + continue; + if (Token::simpleMatch(tok->tokAt(3), "*")) + continue; + } + tok->str(MathLib::calculate(tok->str(), tok->tokAt(2)->str(), *(tok->strAt(1)))); Token::eraseTokens(tok, tok->tokAt(3)); diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index d33cf3b96..93a64c5d9 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -1204,6 +1204,9 @@ private: const char code[] = "a[10+10-10-10]"; ASSERT_EQUALS("a [ 0 ]", tok(code)); } + + ASSERT_EQUALS("x = 1 + 2 * y ;", tok("x=1+2*y;")); + ASSERT_EQUALS("x = 7 ;", tok("x=1+2*3;")); } diff --git a/test/testsuite.h b/test/testsuite.h index 435640bfe..990b50b26 100644 --- a/test/testsuite.h +++ b/test/testsuite.h @@ -17,7 +17,8 @@ */ - +#ifndef testsuiteH +#define testsuiteH #include #include "../src/errorlogger.h" @@ -63,3 +64,5 @@ public: #define TODO_ASSERT_EQUALS( EXPECTED , ACTUAL ) if (EXPECTED==ACTUAL) assertEquals(__FILE__, __LINE__, "TODO assertion", "The assertion succeeded") #define REGISTER_TEST( CLASSNAME ) namespace { CLASSNAME instance; } +#endif +