diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index cbce6bb60..094bf7e2b 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -406,8 +406,8 @@ void CheckBufferOverrun::parse_for_body(const Token *tok2, const ArrayInfo &arra //printf("min_index: %s %c %s\n", min_counter_value.c_str(), action, second.c_str()); //printf("max_index: %s %c %s\n", max_counter_value.c_str(), action, second.c_str()); - min_index = std::atoi(MathLib::calculate(min_counter_value, second, action).c_str()); - max_index = std::atoi(MathLib::calculate(max_counter_value, second, action).c_str()); + min_index = std::atoi(MathLib::calculate(min_counter_value, second, action, _tokenizer).c_str()); + max_index = std::atoi(MathLib::calculate(max_counter_value, second, action, _tokenizer).c_str()); } else if (Token::Match(tok2, "%varid% [ %num% +|-|*|/ %var% ]", arrayInfo.varid) && tok2->tokAt(4)->varId() == counter_varid) @@ -418,8 +418,8 @@ void CheckBufferOverrun::parse_for_body(const Token *tok2, const ArrayInfo &arra //printf("min_index: %s %c %s\n", first.c_str(), action, min_counter_value.c_str()); //printf("max_index: %s %c %s\n", first.c_str(), action, max_counter_value.c_str()); - min_index = std::atoi(MathLib::calculate(first, min_counter_value, action).c_str()); - max_index = std::atoi(MathLib::calculate(first, max_counter_value, action).c_str()); + min_index = std::atoi(MathLib::calculate(first, min_counter_value, action, _tokenizer).c_str()); + max_index = std::atoi(MathLib::calculate(first, max_counter_value, action, _tokenizer).c_str()); } //printf("min_index = %d, max_index = %d, size = %d\n", min_index, max_index, size); diff --git a/lib/mathlib.cpp b/lib/mathlib.cpp index ad00d7140..5ac1997ed 100644 --- a/lib/mathlib.cpp +++ b/lib/mathlib.cpp @@ -19,7 +19,7 @@ #include "mathlib.h" - +#include "tokenize.h" #include #include @@ -245,7 +245,7 @@ std::string MathLib::multiply(const std::string &first, const std::string &secon return toString(toDoubleNumber(first) * toDoubleNumber(second)); } -std::string MathLib::calculate(const std::string &first, const std::string &second, char action) +std::string MathLib::calculate(const std::string &first, const std::string &second, char action, const Tokenizer *tokenizer) { std::string result("0"); @@ -268,11 +268,7 @@ std::string MathLib::calculate(const std::string &first, const std::string &seco break; default: - std::cerr << "##### If you see this, there is a bug: " - << "MathLib::calculate() was called with unknown action '" - << action - << "' #####" - << std::endl; + tokenizer->cppcheckError(0); break; } diff --git a/lib/mathlib.h b/lib/mathlib.h index 52503f275..8d4b44bd4 100644 --- a/lib/mathlib.h +++ b/lib/mathlib.h @@ -27,6 +27,8 @@ /** @brief simple math functions that uses operands stored in std::string. useful when performing math on tokens. */ +class Tokenizer; + class MathLib { public: @@ -44,7 +46,7 @@ public: static std::string subtract(const std::string & first, const std::string & second); static std::string multiply(const std::string & first, const std::string & second); static std::string divide(const std::string & first, const std::string & second); - static std::string calculate(const std::string & first, const std::string & second, char action); + static std::string calculate(const std::string & first, const std::string & second, char action, const Tokenizer *tokenizer); static std::string sin(const std::string & tok); static std::string cos(const std::string & tok); diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index d1bc7e58d..76996b695 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -5880,9 +5880,9 @@ bool Tokenizer::simplifyCalculations() if (Token::Match(tok->previous(), "- %num% - %num%")) tok->str(MathLib::add(tok->str(), tok->tokAt(2)->str())); else if (Token::Match(tok->previous(), "- %num% + %num%")) - tok->str(MathLib::sub(tok->str(), tok->tokAt(2)->str())); + tok->str(MathLib::subtract(tok->str(), tok->tokAt(2)->str())); else - tok->str(MathLib::calculate(tok->str(), tok->tokAt(2)->str(), tok->strAt(1)[0])); + tok->str(MathLib::calculate(tok->str(), tok->tokAt(2)->str(), tok->strAt(1)[0], this)); Token::eraseTokens(tok, tok->tokAt(3));