From 638d18cfc8816c338e278ea77dba4dc307fdd243 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 6 Apr 2009 19:23:30 +0200 Subject: [PATCH] tokenize: use mathlib when simplifying calculations (ticket: 236) --- Makefile | 2 +- src/tokenize.cpp | 29 ++++++++++------------------- 2 files changed, 11 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index 38e301412..d2dc83f11 100644 --- a/Makefile +++ b/Makefile @@ -166,7 +166,7 @@ src/threadexecutor.o: src/threadexecutor.cpp src/threadexecutor.h src/settings.h src/token.o: src/token.cpp src/token.h $(CXX) $(CXXFLAGS) -c -o src/token.o src/token.cpp -src/tokenize.o: src/tokenize.cpp src/tokenize.h src/settings.h src/errorlogger.h src/token.h src/filelister.h +src/tokenize.o: src/tokenize.cpp src/tokenize.h src/settings.h src/errorlogger.h src/token.h src/filelister.h src/mathlib.h $(CXX) $(CXXFLAGS) -c -o src/tokenize.o src/tokenize.cpp test/testautovariables.o: test/testautovariables.cpp src/tokenize.h src/settings.h src/errorlogger.h src/token.h src/checkautovariables.h src/check.h test/testsuite.h diff --git a/src/tokenize.cpp b/src/tokenize.cpp index bb1e7444c..d84d2ed26 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -21,6 +21,7 @@ //--------------------------------------------------------------------------- #include "tokenize.h" #include "filelister.h" +#include "mathlib.h" #include #include @@ -2109,36 +2110,26 @@ bool Tokenizer::simplifyCalculations() // (1-2) if (Token::Match(tok, "[[,(=<>] %num% [+-*/] %num% [],);=<>]")) { - int i1 = std::atoi(tok->strAt(1)); - int i2 = std::atoi(tok->strAt(3)); - if (i2 == 0 && *(tok->strAt(2)) == '/') - { - continue; - } + tok = tok->next(); - switch (*(tok->strAt(2))) + switch (*(tok->strAt(1))) { case '+': - i1 += i2; + tok->str(MathLib::add(tok->str(), tok->strAt(2)).c_str()); break; case '-': - i1 -= i2; + tok->str(MathLib::subtract(tok->str(), tok->strAt(2)).c_str()); break; case '*': - i1 *= i2; + tok->str(MathLib::multiply(tok->str(), tok->strAt(2)).c_str()); break; case '/': - i1 /= i2; + tok->str(MathLib::divide(tok->str(), tok->strAt(2)).c_str()); break; } - tok = tok->next(); - std::ostringstream str; - str << i1; - tok->str(str.str().c_str()); - for (int i = 0; i < 2; i++) - { - tok->deleteNext(); - } + + tok->deleteNext(); + tok->deleteNext(); ret = true; }