- 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
This commit is contained in:
PKEuS 2012-02-17 15:47:08 +01:00
parent 9d75641ef8
commit 8ea5df62c4
7 changed files with 33 additions and 44 deletions

View File

@ -189,8 +189,6 @@ unsigned int CppCheck::processFile()
return 0; return 0;
} }
_settings.ifcfg = bool(configurations.size() > 1);
if (!_settings.userDefines.empty()) { if (!_settings.userDefines.empty()) {
configurations.clear(); configurations.clear();
configurations.push_back(_settings.userDefines); configurations.push_back(_settings.userDefines);

View File

@ -29,24 +29,22 @@
MathLib::bigint MathLib::toLongNumber(const std::string &str) MathLib::bigint MathLib::toLongNumber(const std::string &str)
{ {
bool sign = str[0]=='-'||str[0]=='+';
// hexadecimal numbers: // hexadecimal numbers:
if (str.compare(0, 2, "0x") == 0 if (str.compare(sign?1:0, 2, "0x") == 0
|| str.compare(0, 3, "+0x") == 0 || str.compare(sign?1:0, 2, "0X") == 0) {
|| str.compare(0, 3, "-0x") == 0) {
bigint ret = 0; bigint ret = 0;
std::istringstream istr(str.substr((str[0]=='0') ? 2U : 3U)); std::istringstream istr(str);
istr >> std::hex >> ret; istr >> std::hex >> ret;
return (str[0]=='-') ? -ret : ret; return ret;
} }
// octal numbers: // octal numbers:
if (str.compare(0, 1, "0") == 0 if (str[sign?1:0] == '0') {
|| str.compare(0, 2, "+0") == 0
|| str.compare(0, 2, "-0") == 0) {
bigint ret = 0; bigint ret = 0;
std::istringstream istr(str.substr((str[0]=='0') ? 1U : 2U)); std::istringstream istr(str);
istr >> std::oct >> ret; istr >> std::oct >> ret;
return (str[0]=='-') ? -ret : ret; return ret;
} }
if (str.find_first_of("eE") != std::string::npos) if (str.find_first_of("eE") != std::string::npos)

View File

@ -22,31 +22,23 @@
#include <fstream> #include <fstream>
#include <set> #include <set>
#include <stack>
Settings::Settings() 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. // This assumes the code you are checking is for the same architecture this is compiled on.
#if defined(_WIN64) #if defined(_WIN64)
platform(Win64); platform(Win64);

View File

@ -159,12 +159,6 @@ public:
/** @brief --report-progress */ /** @brief --report-progress */
bool reportProgress; bool reportProgress;
/**
* @brief Is there any preprocessor configurations in the source code?
* As usual, include guards are not counted.
*/
bool ifcfg;
/** Rule */ /** Rule */
class Rule { class Rule {
public: public:

View File

@ -117,8 +117,8 @@ void Tokenizer::addtoken(const char str[], const unsigned int lineno, const unsi
// Replace hexadecimal value with decimal // Replace hexadecimal value with decimal
std::ostringstream str2; std::ostringstream str2;
if (strncmp(str, "0x", 2) == 0 || strncmp(str, "0X", 2) == 0) { if (strncmp(str, "0x", 2) == 0 || strncmp(str, "0X", 2) == 0 || (str[0] == '0' && std::isdigit(str[1]))) {
str2 << std::strtoul(str + 2, NULL, 16); str2 << MathLib::toLongNumber(str);
} else if (strncmp(str, "_Bool", 5) == 0) { } else if (strncmp(str, "_Bool", 5) == 0) {
str2 << "bool"; str2 << "bool";
} else { } else {

View File

@ -3723,6 +3723,12 @@ private:
" else if ((x = x / 2) < 100) { b = 2; }\n" " else if ((x = x / 2) < 100) { b = 2; }\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); 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() { void duplicateBranch() {

View File

@ -535,7 +535,7 @@ private:
"2: int x1@1 ; x1@1 = g ( ) ;\n" "2: int x1@1 ; x1@1 = g ( ) ;\n"
"3: int x2@2 ; x2@2 = x1@1 ;\n" "3: int x2@2 ; x2@2 = x1@1 ;\n"
"4: }\n", "4: }\n",
tokenizeDebugListing(code.c_str(), false)); tokenizeDebugListing(code, false));
} }
void tokenize9() { void tokenize9() {
@ -581,6 +581,7 @@ private:
void tokenize14() { void tokenize14() {
ASSERT_EQUALS("; 16 ;", tokenizeAndStringify(";0x10;")); ASSERT_EQUALS("; 16 ;", tokenizeAndStringify(";0x10;"));
ASSERT_EQUALS("; 16 ;", tokenizeAndStringify(";0X10;")); ASSERT_EQUALS("; 16 ;", tokenizeAndStringify(";0X10;"));
ASSERT_EQUALS("; 292 ;", tokenizeAndStringify(";0444;"));
} }
// Ticket #2429: 0.125 // Ticket #2429: 0.125
@ -4064,7 +4065,7 @@ private:
{ {
const char code[] = "module ( a , a , sizeof ( a ) , 0444 ) ;"; 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)); ASSERT_EQUALS("void f ( int x ) { }", tokenizeAndStringify("void f(x) int x; { }", true));