From 718e42f5ca39562188b47bfd6f956e51f2986b72 Mon Sep 17 00:00:00 2001 From: Alexander Mai Date: Fri, 11 Apr 2014 23:35:53 +0200 Subject: [PATCH] Fix some clang warnings about type conversions --- cli/cppcheckexecutor.cpp | 2 +- lib/mathlib.cpp | 50 -------------------------------------- lib/mathlib.h | 49 ++++++++++++++++++++++++++++++++++++- lib/templatesimplifier.cpp | 4 +-- 4 files changed, 51 insertions(+), 54 deletions(-) diff --git a/cli/cppcheckexecutor.cpp b/cli/cppcheckexecutor.cpp index bd2590805..b947fea01 100644 --- a/cli/cppcheckexecutor.cpp +++ b/cli/cppcheckexecutor.cpp @@ -205,7 +205,7 @@ static const Signaltype listofsignals[] = { * \return size of array * */ template -int GetArrayLength(const T(&)[size]) +size_t GetArrayLength(const T(&)[size]) { return size; } diff --git a/lib/mathlib.cpp b/lib/mathlib.cpp index ec40a37ba..b5cf4bfc2 100644 --- a/lib/mathlib.cpp +++ b/lib/mathlib.cpp @@ -21,60 +21,10 @@ #include "mathlib.h" #include "errorlogger.h" -#include -#include -#include #include #include #include -MathLib::bigint MathLib::toLongNumber(const std::string &str) -{ - // hexadecimal numbers: - if (isHex(str)) { - if (str[0] == '-') { - bigint ret = 0; - std::istringstream istr(str); - istr >> std::hex >> ret; - return ret; - } else { - unsigned long long ret = 0; - std::istringstream istr(str); - istr >> std::hex >> ret; - return (bigint)ret; - } - } - - // octal numbers: - if (isOct(str)) { - bigint ret = 0; - std::istringstream istr(str); - istr >> std::oct >> ret; - return ret; - } - - // binary numbers: - if (isBin(str)) { - bigint ret = 0; - for (std::string::size_type i = str[0] == '0'?2:3; i < str.length(); i++) { - ret <<= 1; - if (str[i] == '1') - ret |= 1; - } - if (str[0] == '-') - ret = -ret; - return ret; - } - - if (isFloat(str)) - return static_cast(std::atof(str.c_str())); - - bigint ret = 0; - std::istringstream istr(str); - istr >> ret; - return ret; -} - double MathLib::toDoubleNumber(const std::string &str) { if (isHex(str)) diff --git a/lib/mathlib.h b/lib/mathlib.h index 4e66f748c..233d601ba 100644 --- a/lib/mathlib.h +++ b/lib/mathlib.h @@ -21,6 +21,7 @@ #define mathlibH //--------------------------------------------------------------------------- +#include #include #include #include "config.h" @@ -35,7 +36,53 @@ public: typedef long long bigint; typedef unsigned long long biguint; - static bigint toLongNumber(const std::string & str); + template < class T = bigint > + static T toLongNumber(const std::string & str) { + // hexadecimal numbers: + if (isHex(str)) { + if (str[0] == '-') { + T ret = 0; + std::istringstream istr(str); + istr >> std::hex >> ret; + return ret; + } else { + unsigned long long ret = 0; + std::istringstream istr(str); + istr >> std::hex >> ret; + return (T)ret; + } + } + + // octal numbers: + if (isOct(str)) { + T ret = 0; + std::istringstream istr(str); + istr >> std::oct >> ret; + return ret; + } + + // binary numbers: + if (isBin(str)) { + T ret = 0; + for (std::string::size_type i = str[0] == '0'?2:3; i < str.length(); i++) { + ret <<= 1; + if (str[i] == '1') + ret |= 1; + } + if (str[0] == '-') + ret = -ret; + return ret; + } + + if (isFloat(str)) + return static_cast(std::atof(str.c_str())); + + T ret = 0; + std::istringstream istr(str); + istr >> ret; + return ret; + } + template static std::string toString(T value) { std::ostringstream result; result << value; diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index c46f87641..55b1d4364 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -803,8 +803,8 @@ static bool isLowerEqualThanMulDiv(const Token* lower) template std::string typeCorrectShift(const char cop, const Token* left, const Token* right) { - const T leftInt=MathLib::toLongNumber(left->str()); - const T rightInt=MathLib::toLongNumber(right->str()); + const T leftInt=MathLib::toLongNumber(left->str()); + const T rightInt=MathLib::toLongNumber(right->str()); if (cop == '&' || cop == '|' || cop == '^') return MathLib::calculate(left->str(), right->str(), cop);