Skip to content

Commit 718e42f

Browse files
committed
Fix some clang warnings about type conversions
1 parent 9ac83d7 commit 718e42f

File tree

4 files changed

+51
-54
lines changed

4 files changed

+51
-54
lines changed

cli/cppcheckexecutor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ static const Signaltype listofsignals[] = {
205205
* \return size of array
206206
* */
207207
template<typename T, int size>
208-
int GetArrayLength(const T(&)[size])
208+
size_t GetArrayLength(const T(&)[size])
209209
{
210210
return size;
211211
}

lib/mathlib.cpp

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -21,60 +21,10 @@
2121
#include "mathlib.h"
2222
#include "errorlogger.h"
2323

24-
#include <string>
25-
#include <sstream>
26-
#include <cstdlib>
2724
#include <cmath>
2825
#include <cctype>
2926
#include <limits>
3027

31-
MathLib::bigint MathLib::toLongNumber(const std::string &str)
32-
{
33-
// hexadecimal numbers:
34-
if (isHex(str)) {
35-
if (str[0] == '-') {
36-
bigint ret = 0;
37-
std::istringstream istr(str);
38-
istr >> std::hex >> ret;
39-
return ret;
40-
} else {
41-
unsigned long long ret = 0;
42-
std::istringstream istr(str);
43-
istr >> std::hex >> ret;
44-
return (bigint)ret;
45-
}
46-
}
47-
48-
// octal numbers:
49-
if (isOct(str)) {
50-
bigint ret = 0;
51-
std::istringstream istr(str);
52-
istr >> std::oct >> ret;
53-
return ret;
54-
}
55-
56-
// binary numbers:
57-
if (isBin(str)) {
58-
bigint ret = 0;
59-
for (std::string::size_type i = str[0] == '0'?2:3; i < str.length(); i++) {
60-
ret <<= 1;
61-
if (str[i] == '1')
62-
ret |= 1;
63-
}
64-
if (str[0] == '-')
65-
ret = -ret;
66-
return ret;
67-
}
68-
69-
if (isFloat(str))
70-
return static_cast<bigint>(std::atof(str.c_str()));
71-
72-
bigint ret = 0;
73-
std::istringstream istr(str);
74-
istr >> ret;
75-
return ret;
76-
}
77-
7828
double MathLib::toDoubleNumber(const std::string &str)
7929
{
8030
if (isHex(str))

lib/mathlib.h

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#define mathlibH
2222
//---------------------------------------------------------------------------
2323

24+
#include <cstdlib>
2425
#include <string>
2526
#include <sstream>
2627
#include "config.h"
@@ -35,7 +36,53 @@ class CPPCHECKLIB MathLib {
3536
typedef long long bigint;
3637
typedef unsigned long long biguint;
3738

38-
static bigint toLongNumber(const std::string & str);
39+
template < class T = bigint >
40+
static T toLongNumber(const std::string & str) {
41+
// hexadecimal numbers:
42+
if (isHex(str)) {
43+
if (str[0] == '-') {
44+
T ret = 0;
45+
std::istringstream istr(str);
46+
istr >> std::hex >> ret;
47+
return ret;
48+
} else {
49+
unsigned long long ret = 0;
50+
std::istringstream istr(str);
51+
istr >> std::hex >> ret;
52+
return (T)ret;
53+
}
54+
}
55+
56+
// octal numbers:
57+
if (isOct(str)) {
58+
T ret = 0;
59+
std::istringstream istr(str);
60+
istr >> std::oct >> ret;
61+
return ret;
62+
}
63+
64+
// binary numbers:
65+
if (isBin(str)) {
66+
T ret = 0;
67+
for (std::string::size_type i = str[0] == '0'?2:3; i < str.length(); i++) {
68+
ret <<= 1;
69+
if (str[i] == '1')
70+
ret |= 1;
71+
}
72+
if (str[0] == '-')
73+
ret = -ret;
74+
return ret;
75+
}
76+
77+
if (isFloat(str))
78+
return static_cast<T>(std::atof(str.c_str()));
79+
80+
T ret = 0;
81+
std::istringstream istr(str);
82+
istr >> ret;
83+
return ret;
84+
}
85+
3986
template<class T> static std::string toString(T value) {
4087
std::ostringstream result;
4188
result << value;

lib/templatesimplifier.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -803,8 +803,8 @@ static bool isLowerEqualThanMulDiv(const Token* lower)
803803
template <typename T>
804804
std::string typeCorrectShift(const char cop, const Token* left, const Token* right)
805805
{
806-
const T leftInt=MathLib::toLongNumber(left->str());
807-
const T rightInt=MathLib::toLongNumber(right->str());
806+
const T leftInt=MathLib::toLongNumber<T>(left->str());
807+
const T rightInt=MathLib::toLongNumber<T>(right->str());
808808

809809
if (cop == '&' || cop == '|' || cop == '^')
810810
return MathLib::calculate(left->str(), right->str(), cop);

0 commit comments

Comments
 (0)