Skip to content

Commit 8ea5df6

Browse files
committed
- Improved support for numbers in code:
-- Use MathLib::toLongNumber for conversion in tokenizer (Fix #3610) -- Handle octal numbers in tokenizer - Refactorizations in MathLib::toLongNumber and Settings
1 parent 9d75641 commit 8ea5df6

7 files changed

Lines changed: 33 additions & 44 deletions

File tree

lib/cppcheck.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,6 @@ unsigned int CppCheck::processFile()
189189
return 0;
190190
}
191191

192-
_settings.ifcfg = bool(configurations.size() > 1);
193-
194192
if (!_settings.userDefines.empty()) {
195193
configurations.clear();
196194
configurations.push_back(_settings.userDefines);

lib/mathlib.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,22 @@
2929

3030
MathLib::bigint MathLib::toLongNumber(const std::string &str)
3131
{
32+
bool sign = str[0]=='-'||str[0]=='+';
3233
// hexadecimal numbers:
33-
if (str.compare(0, 2, "0x") == 0
34-
|| str.compare(0, 3, "+0x") == 0
35-
|| str.compare(0, 3, "-0x") == 0) {
34+
if (str.compare(sign?1:0, 2, "0x") == 0
35+
|| str.compare(sign?1:0, 2, "0X") == 0) {
3636
bigint ret = 0;
37-
std::istringstream istr(str.substr((str[0]=='0') ? 2U : 3U));
37+
std::istringstream istr(str);
3838
istr >> std::hex >> ret;
39-
return (str[0]=='-') ? -ret : ret;
39+
return ret;
4040
}
4141

4242
// octal numbers:
43-
if (str.compare(0, 1, "0") == 0
44-
|| str.compare(0, 2, "+0") == 0
45-
|| str.compare(0, 2, "-0") == 0) {
43+
if (str[sign?1:0] == '0') {
4644
bigint ret = 0;
47-
std::istringstream istr(str.substr((str[0]=='0') ? 1U : 2U));
45+
std::istringstream istr(str);
4846
istr >> std::oct >> ret;
49-
return (str[0]=='-') ? -ret : ret;
47+
return ret;
5048
}
5149

5250
if (str.find_first_of("eE") != std::string::npos)

lib/settings.cpp

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,23 @@
2222

2323
#include <fstream>
2424
#include <set>
25-
#include <stack>
2625

2726
Settings::Settings()
27+
: debug(false), debugwarnings(false), debugFalsePositive(false),
28+
_errorsOnly(false),
29+
_inlineSuppressions(false),
30+
_verbose(false),
31+
_force(false), _maxConfigs(12),
32+
_xml(false), _xml_version(1),
33+
_jobs(1),
34+
_exitCode(0),
35+
_showtime(0),
36+
_terminate(false),
37+
inconclusive(false), experimental(false),
38+
test_2_pass(false),
39+
reportProgress(false),
40+
checkConfiguration(false)
2841
{
29-
debug = debugwarnings = false;
30-
debugFalsePositive = false;
31-
_errorsOnly = false;
32-
_inlineSuppressions = false;
33-
_verbose = false;
34-
_force = false;
35-
_xml = false;
36-
_xml_version = 1;
37-
_jobs = 1;
38-
_exitCode = 0;
39-
_showtime = 0; // TODO: use enum
40-
_append = "";
41-
_terminate = false;
42-
_maxConfigs = 12;
43-
inconclusive = false;
44-
experimental = false;
45-
test_2_pass = false;
46-
reportProgress = false;
47-
ifcfg = false;
48-
checkConfiguration = false;
49-
5042
// This assumes the code you are checking is for the same architecture this is compiled on.
5143
#if defined(_WIN64)
5244
platform(Win64);

lib/settings.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,6 @@ class Settings {
159159
/** @brief --report-progress */
160160
bool reportProgress;
161161

162-
/**
163-
* @brief Is there any preprocessor configurations in the source code?
164-
* As usual, include guards are not counted.
165-
*/
166-
bool ifcfg;
167-
168162
/** Rule */
169163
class Rule {
170164
public:

lib/tokenize.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ void Tokenizer::addtoken(const char str[], const unsigned int lineno, const unsi
117117

118118
// Replace hexadecimal value with decimal
119119
std::ostringstream str2;
120-
if (strncmp(str, "0x", 2) == 0 || strncmp(str, "0X", 2) == 0) {
121-
str2 << std::strtoul(str + 2, NULL, 16);
120+
if (strncmp(str, "0x", 2) == 0 || strncmp(str, "0X", 2) == 0 || (str[0] == '0' && std::isdigit(str[1]))) {
121+
str2 << MathLib::toLongNumber(str);
122122
} else if (strncmp(str, "_Bool", 5) == 0) {
123123
str2 << "bool";
124124
} else {

test/testother.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3723,6 +3723,12 @@ class TestOther : public TestFixture {
37233723
" else if ((x = x / 2) < 100) { b = 2; }\n"
37243724
"}");
37253725
ASSERT_EQUALS("", errout.str());
3726+
3727+
check("void f(int i) {\n"
3728+
" if(i == 0x02e2000000 || i == 0xa0c6000000)\n"
3729+
" foo(i);\n"
3730+
"}");
3731+
ASSERT_EQUALS("", errout.str());
37263732
}
37273733

37283734
void duplicateBranch() {

test/testtokenize.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ class TestTokenizer : public TestFixture {
535535
"2: int x1@1 ; x1@1 = g ( ) ;\n"
536536
"3: int x2@2 ; x2@2 = x1@1 ;\n"
537537
"4: }\n",
538-
tokenizeDebugListing(code.c_str(), false));
538+
tokenizeDebugListing(code, false));
539539
}
540540

541541
void tokenize9() {
@@ -581,6 +581,7 @@ class TestTokenizer : public TestFixture {
581581
void tokenize14() {
582582
ASSERT_EQUALS("; 16 ;", tokenizeAndStringify(";0x10;"));
583583
ASSERT_EQUALS("; 16 ;", tokenizeAndStringify(";0X10;"));
584+
ASSERT_EQUALS("; 292 ;", tokenizeAndStringify(";0444;"));
584585
}
585586

586587
// Ticket #2429: 0.125
@@ -4064,7 +4065,7 @@ class TestTokenizer : public TestFixture {
40644065

40654066
{
40664067
const char code[] = "module ( a , a , sizeof ( a ) , 0444 ) ;";
4067-
ASSERT_EQUALS(code, tokenizeAndStringify(code, true));
4068+
ASSERT_EQUALS("module ( a , a , sizeof ( a ) , 292 ) ;", tokenizeAndStringify(code, true));
40684069
}
40694070

40704071
ASSERT_EQUALS("void f ( int x ) { }", tokenizeAndStringify("void f(x) int x; { }", true));

0 commit comments

Comments
 (0)