Refactoring MathLib: Report errors through the tokenizer. Ticket: #1839

This commit is contained in:
Daniel Marjamäki 2010-07-24 10:25:03 +02:00
parent 84e576de04
commit dd07d82c34
4 changed files with 12 additions and 14 deletions

View File

@ -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);

View File

@ -19,7 +19,7 @@
#include "mathlib.h"
#include "tokenize.h"
#include <fstream>
#include <string>
@ -245,7 +245,7 @@ std::string MathLib::multiply(const std::string &first, const std::string &secon
return toString<double>(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;
}

View File

@ -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);

View File

@ -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));