From 8ea5df62c4bfe882a2e4d825260c4b0561e7ce65 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Fri, 17 Feb 2012 15:47:08 +0100 Subject: [PATCH] - 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 --- lib/cppcheck.cpp | 2 -- lib/mathlib.cpp | 18 ++++++++---------- lib/settings.cpp | 36 ++++++++++++++---------------------- lib/settings.h | 6 ------ lib/tokenize.cpp | 4 ++-- test/testother.cpp | 6 ++++++ test/testtokenize.cpp | 5 +++-- 7 files changed, 33 insertions(+), 44 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 01c5130a1..7780dcf5f 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -189,8 +189,6 @@ unsigned int CppCheck::processFile() return 0; } - _settings.ifcfg = bool(configurations.size() > 1); - if (!_settings.userDefines.empty()) { configurations.clear(); configurations.push_back(_settings.userDefines); diff --git a/lib/mathlib.cpp b/lib/mathlib.cpp index e521df6b4..e54ea46b2 100644 --- a/lib/mathlib.cpp +++ b/lib/mathlib.cpp @@ -29,24 +29,22 @@ MathLib::bigint MathLib::toLongNumber(const std::string &str) { + bool sign = str[0]=='-'||str[0]=='+'; // hexadecimal numbers: - if (str.compare(0, 2, "0x") == 0 - || str.compare(0, 3, "+0x") == 0 - || str.compare(0, 3, "-0x") == 0) { + if (str.compare(sign?1:0, 2, "0x") == 0 + || str.compare(sign?1:0, 2, "0X") == 0) { bigint ret = 0; - std::istringstream istr(str.substr((str[0]=='0') ? 2U : 3U)); + std::istringstream istr(str); istr >> std::hex >> ret; - return (str[0]=='-') ? -ret : ret; + return ret; } // octal numbers: - if (str.compare(0, 1, "0") == 0 - || str.compare(0, 2, "+0") == 0 - || str.compare(0, 2, "-0") == 0) { + if (str[sign?1:0] == '0') { bigint ret = 0; - std::istringstream istr(str.substr((str[0]=='0') ? 1U : 2U)); + std::istringstream istr(str); istr >> std::oct >> ret; - return (str[0]=='-') ? -ret : ret; + return ret; } if (str.find_first_of("eE") != std::string::npos) diff --git a/lib/settings.cpp b/lib/settings.cpp index 5ae5f2035..7e1911c5e 100644 --- a/lib/settings.cpp +++ b/lib/settings.cpp @@ -22,31 +22,23 @@ #include #include -#include Settings::Settings() + : debug(false), debugwarnings(false), debugFalsePositive(false), + _errorsOnly(false), + _inlineSuppressions(false), + _verbose(false), + _force(false), _maxConfigs(12), + _xml(false), _xml_version(1), + _jobs(1), + _exitCode(0), + _showtime(0), + _terminate(false), + inconclusive(false), experimental(false), + test_2_pass(false), + reportProgress(false), + checkConfiguration(false) { - debug = debugwarnings = false; - debugFalsePositive = false; - _errorsOnly = false; - _inlineSuppressions = false; - _verbose = false; - _force = false; - _xml = false; - _xml_version = 1; - _jobs = 1; - _exitCode = 0; - _showtime = 0; // TODO: use enum - _append = ""; - _terminate = false; - _maxConfigs = 12; - inconclusive = false; - experimental = false; - test_2_pass = false; - reportProgress = false; - ifcfg = false; - checkConfiguration = false; - // This assumes the code you are checking is for the same architecture this is compiled on. #if defined(_WIN64) platform(Win64); diff --git a/lib/settings.h b/lib/settings.h index d11536e96..ac410eafd 100644 --- a/lib/settings.h +++ b/lib/settings.h @@ -159,12 +159,6 @@ public: /** @brief --report-progress */ bool reportProgress; - /** - * @brief Is there any preprocessor configurations in the source code? - * As usual, include guards are not counted. - */ - bool ifcfg; - /** Rule */ class Rule { public: diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index e05e0838d..5a35976e5 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -117,8 +117,8 @@ void Tokenizer::addtoken(const char str[], const unsigned int lineno, const unsi // Replace hexadecimal value with decimal std::ostringstream str2; - if (strncmp(str, "0x", 2) == 0 || strncmp(str, "0X", 2) == 0) { - str2 << std::strtoul(str + 2, NULL, 16); + if (strncmp(str, "0x", 2) == 0 || strncmp(str, "0X", 2) == 0 || (str[0] == '0' && std::isdigit(str[1]))) { + str2 << MathLib::toLongNumber(str); } else if (strncmp(str, "_Bool", 5) == 0) { str2 << "bool"; } else { diff --git a/test/testother.cpp b/test/testother.cpp index 5f3e0e026..d6fee3bfb 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -3723,6 +3723,12 @@ private: " else if ((x = x / 2) < 100) { b = 2; }\n" "}"); ASSERT_EQUALS("", errout.str()); + + check("void f(int i) {\n" + " if(i == 0x02e2000000 || i == 0xa0c6000000)\n" + " foo(i);\n" + "}"); + ASSERT_EQUALS("", errout.str()); } void duplicateBranch() { diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 548526ae7..d173a4da2 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -535,7 +535,7 @@ private: "2: int x1@1 ; x1@1 = g ( ) ;\n" "3: int x2@2 ; x2@2 = x1@1 ;\n" "4: }\n", - tokenizeDebugListing(code.c_str(), false)); + tokenizeDebugListing(code, false)); } void tokenize9() { @@ -581,6 +581,7 @@ private: void tokenize14() { ASSERT_EQUALS("; 16 ;", tokenizeAndStringify(";0x10;")); ASSERT_EQUALS("; 16 ;", tokenizeAndStringify(";0X10;")); + ASSERT_EQUALS("; 292 ;", tokenizeAndStringify(";0444;")); } // Ticket #2429: 0.125 @@ -4064,7 +4065,7 @@ private: { const char code[] = "module ( a , a , sizeof ( a ) , 0444 ) ;"; - ASSERT_EQUALS(code, tokenizeAndStringify(code, true)); + ASSERT_EQUALS("module ( a , a , sizeof ( a ) , 292 ) ;", tokenizeAndStringify(code, true)); } ASSERT_EQUALS("void f ( int x ) { }", tokenizeAndStringify("void f(x) int x; { }", true));