1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
/* -*- C++ -*-
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2025 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
#ifndef mathlibH
#define mathlibH
//---------------------------------------------------------------------------
#include "config.h"
#include <cstdint>
#include <string>
#if defined(HAVE_BOOST) && defined(HAVE_BOOST_INT128)
#include <boost/multiprecision/cpp_int.hpp>
#endif
class Token;
/// @addtogroup Core
/// @{
/** @brief simple math functions that uses operands stored in std::string. useful when performing math on tokens. */
class CPPCHECKLIB MathLib {
public:
#if defined(HAVE_BOOST) && defined(HAVE_BOOST_INT128)
using bigint = boost::multiprecision::int128_t;
using biguint = boost::multiprecision::uint128_t;
#else
using bigint = long long;
using biguint = unsigned long long;
#endif
/** @brief value class */
class value {
private:
bigint mIntValue{};
double mDoubleValue{};
enum class Type : std::uint8_t { INT, LONG, LONGLONG, FLOAT } mType;
bool mIsUnsigned{};
void promote(const value &v);
public:
/**
* @throws InternalError thrown on invalid value
*/
explicit value(const std::string &s);
std::string str() const;
bool isInt() const {
return mType != Type::FLOAT;
}
bool isFloat() const {
return mType == Type::FLOAT;
}
double getDoubleValue() const {
return isFloat() ? mDoubleValue : static_cast<double>(mIntValue);
}
/**
* @throws InternalError thrown on invalid/unhandled calculation or divison by zero
*/
static value calc(char op, const value &v1, const value &v2);
int compare(const value &v) const;
value add(int v) const;
/**
* @throws InternalError thrown if operand is not an integer
*/
value shiftLeft(const value &v) const;
/**
* @throws InternalError thrown if operand is not an integer
*/
value shiftRight(const value &v) const;
};
static const int bigint_bits;
/** @brief for conversion of numeric literals - for atoi-like conversions please use strToInt() */
static bigint toBigNumber(const Token * tok);
/** @brief for conversion of numeric literals - for atoi-like conversions please use strToInt()
* @throws InternalError thrown if conversion failed
*/
static bigint toBigNumber(const std::string & str, const Token *tok = nullptr);
/** @brief for conversion of numeric literals - for atoi-like conversions please use strToInt() */
static biguint toBigUNumber(const Token * tok);
/** @brief for conversion of numeric literals - for atoi-like conversions please use strToInt()
* @throws InternalError thrown if conversion failed
*/
static biguint toBigUNumber(const std::string & str, const Token *tok = nullptr);
template<class T> static std::string toString(T value) = delete;
/** @brief for conversion of numeric literals */
static double toDoubleNumber(const Token * tok);
/** @brief for conversion of numeric literals
* @throws InternalError thrown if conversion failed
*/
static double toDoubleNumber(const std::string & str, const Token * tok = nullptr);
static bool isInt(const std::string & str);
static bool isFloat(const std::string &str);
static bool isDecimalFloat(const std::string &str);
static bool isNegative(const std::string &str);
static bool isPositive(const std::string &str);
static bool isDec(const std::string & str);
static bool isFloatHex(const std::string& str);
static bool isIntHex(const std::string& str);
static bool isOct(const std::string& str);
static bool isBin(const std::string& str);
static std::string getSuffix(const std::string& value);
/**
* Only used in unit tests
*
* \param[in] str string
* \param[in] supportMicrosoftExtensions support Microsoft extension: i64
* \return true if str is a non-empty valid integer suffix
*/
static bool isValidIntegerSuffix(const std::string& str, bool supportMicrosoftExtensions=true);
static std::string add(const std::string & first, const std::string & second);
static std::string subtract(const std::string & first, const std::string & second);
static std::string multiply(const std::string & first, const std::string & second);
/**
* @throws InternalError thrown on overflow or divison by zero
*/
static std::string divide(const std::string & first, const std::string & second);
/**
* @throws InternalError thrown on division by zero
*/
static std::string mod(const std::string & first, const std::string & second);
/**
* @throws InternalError thrown on unexpected action
*/
static std::string calculate(const std::string & first, const std::string & second, char action);
static std::string sin(const std::string & tok);
static std::string cos(const std::string & tok);
static std::string tan(const std::string & tok);
static std::string abs(const std::string & tok);
static bool isEqual(const std::string & first, const std::string & second);
static bool isNotEqual(const std::string & first, const std::string & second);
static bool isGreater(const std::string & first, const std::string & second);
static bool isGreaterEqual(const std::string & first, const std::string & second);
static bool isLess(const std::string & first, const std::string & second);
static bool isLessEqual(const std::string & first, const std::string & second);
static bool isNullValue(const std::string & str);
/**
* Return true if given character is 0,1,2,3,4,5,6 or 7.
* @param[in] c The character to check
* @return true if given character is octal digit.
*/
static bool isOctalDigit(char c);
static unsigned int encodeMultiChar(const std::string& str);
};
MathLib::value operator+(const MathLib::value &v1, const MathLib::value &v2);
MathLib::value operator-(const MathLib::value &v1, const MathLib::value &v2);
MathLib::value operator*(const MathLib::value &v1, const MathLib::value &v2);
MathLib::value operator/(const MathLib::value &v1, const MathLib::value &v2);
MathLib::value operator%(const MathLib::value &v1, const MathLib::value &v2);
MathLib::value operator&(const MathLib::value &v1, const MathLib::value &v2);
MathLib::value operator|(const MathLib::value &v1, const MathLib::value &v2);
MathLib::value operator^(const MathLib::value &v1, const MathLib::value &v2);
MathLib::value operator<<(const MathLib::value &v1, const MathLib::value &v2);
MathLib::value operator>>(const MathLib::value &v1, const MathLib::value &v2);
template<> CPPCHECKLIB std::string MathLib::toString<MathLib::bigint>(MathLib::bigint value);
template<> CPPCHECKLIB std::string MathLib::toString<MathLib::biguint>(MathLib::biguint value);
template<> CPPCHECKLIB std::string MathLib::toString<double>(double value);
/// @}
//---------------------------------------------------------------------------
#endif // mathlibH
|