Skip to content

Commit f4703e0

Browse files
PKEuSaggro80
authored andcommitted
Refactoring various issues in cmdlineparser, cppcheckexecutor, check64bit and tokenize.
1 parent 8cae17f commit f4703e0

File tree

4 files changed

+105
-130
lines changed

4 files changed

+105
-130
lines changed

cli/cmdlineparser.cpp

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
172172

173173
// Filter errors
174174
else if (strncmp(argv[i], "--suppressions-list=", 20) == 0) {
175-
std::string filename = argv[i];
176-
filename = filename.substr(20);
175+
std::string filename = argv[i]+20;
177176
std::ifstream f(filename.c_str());
178177
if (!f.is_open()) {
179178
std::string message("cppcheck: Couldn't open the file: \"");
@@ -191,8 +190,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
191190

192191
// Filter errors
193192
// This is deprecated, see --supressions-list above
194-
else if (strcmp(argv[i], "--suppressions") == 0 &&
195-
strlen(argv[i]) == 14) {
193+
else if (strcmp(argv[i], "--suppressions") == 0) {
196194
++i;
197195

198196
if (i >= argc) {
@@ -216,8 +214,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
216214
}
217215

218216
else if (strncmp(argv[i], "--suppress=", 11) == 0) {
219-
std::string suppression = argv[i];
220-
suppression = suppression.substr(11);
217+
std::string suppression = argv[i]+11;
221218
const std::string errmsg(_settings->nomsg.addSuppressionLine(suppression));
222219
if (!errmsg.empty()) {
223220
PrintMessage(errmsg);
@@ -243,8 +240,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
243240

244241
// Define the XML file version (and enable XML output)
245242
else if (strncmp(argv[i], "--xml-version=", 14) == 0) {
246-
std::string numberString(argv[i]);
247-
numberString = numberString.substr(14);
243+
std::string numberString(argv[i]+14);
248244

249245
std::istringstream iss(numberString);
250246
if (!(iss >> _settings->_xml_version)) {
@@ -282,16 +278,15 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
282278
return false;
283279
}
284280
// when "style" is enabled, also enable "performance" and "portability"
285-
else if (strstr(argv[i]+9, "style")) {
281+
if (_settings->isEnabled("style")) {
286282
_settings->addEnabled("performance");
287283
_settings->addEnabled("portability");
288284
}
289285
}
290286

291287
// --error-exitcode=1
292288
else if (strncmp(argv[i], "--error-exitcode=", 17) == 0) {
293-
std::string temp = argv[i];
294-
temp = temp.substr(17);
289+
std::string temp = argv[i]+17;
295290
std::istringstream iss(temp);
296291
if (!(iss >> _settings->_exitCode)) {
297292
_settings->_exitCode = 0;
@@ -307,8 +302,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
307302
// "-D define"
308303
if (strcmp(argv[i], "-D") == 0) {
309304
++i;
310-
if (i >= argc || strncmp(argv[i], "-", 1) == 0 ||
311-
strncmp(argv[i], "--", 2) == 0) {
305+
if (i >= argc || argv[i][0] == '-') {
312306
PrintMessage("cppcheck: argument to '-D' is missing.");
313307
return false;
314308
}
@@ -331,8 +325,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
331325
// "-U undef"
332326
if (strcmp(argv[i], "-U") == 0) {
333327
++i;
334-
if (i >= argc || strncmp(argv[i], "-", 1) == 0 ||
335-
strncmp(argv[i], "--", 2) == 0) {
328+
if (i >= argc || argv[i][0] == '-') {
336329
PrintMessage("cppcheck: argument to '-U' is missing.");
337330
return false;
338331
}
@@ -354,7 +347,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
354347
// "-I path/"
355348
if (strcmp(argv[i], "-I") == 0) {
356349
++i;
357-
if (i >= argc) {
350+
if (i >= argc || argv[i][0] == '-') {
358351
PrintMessage("cppcheck: argument to '-I' is missing.");
359352
return false;
360353
}
@@ -391,7 +384,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
391384
// "-i path/"
392385
if (strcmp(argv[i], "-i") == 0) {
393386
++i;
394-
if (i >= argc) {
387+
if (i >= argc || argv[i][0] == '-') {
395388
PrintMessage("cppcheck: argument to '-i' is missing.");
396389
return false;
397390
}
@@ -442,11 +435,10 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
442435
// "--template path/"
443436
if (argv[i][10] == '=')
444437
_settings->_outputFormat = argv[i] + 11;
445-
else {
438+
else if ((i+1) < argc && argv[i+1][0] != '-') {
446439
++i;
447-
_settings->_outputFormat = (argv[i] ? argv[i] : "");
448-
}
449-
if (_settings->_outputFormat.empty()) {
440+
_settings->_outputFormat = argv[i];
441+
} else {
450442
PrintMessage("cppcheck: argument to '--template' is missing.");
451443
return false;
452444
}
@@ -460,14 +452,13 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
460452
}
461453

462454
// Checking threads
463-
else if (strcmp(argv[i], "-j") == 0 ||
464-
strncmp(argv[i], "-j", 2) == 0) {
455+
else if (strncmp(argv[i], "-j", 2) == 0) {
465456
std::string numberString;
466457

467458
// "-j 3"
468459
if (strcmp(argv[i], "-j") == 0) {
469460
++i;
470-
if (i >= argc) {
461+
if (i >= argc || argv[i][0] == '-') {
471462
PrintMessage("cppcheck: argument to '-j' is missing.");
472463
return false;
473464
}
@@ -476,10 +467,8 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
476467
}
477468

478469
// "-j3"
479-
else if (strncmp(argv[i], "-j", 2) == 0) {
480-
numberString = argv[i];
481-
numberString = numberString.substr(2);
482-
}
470+
else
471+
numberString = argv[i]+2;
483472

484473
std::istringstream iss(numberString);
485474
if (!(iss >> _settings->_jobs)) {

cli/cppcheckexecutor.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,32 +105,31 @@ bool CppCheckExecutor::parseFromArgs(CppCheck *cppcheck, int argc, const char* c
105105
// Remove header files from the list of ignored files.
106106
// Also output a warning for the user.
107107
// TODO: Remove all unknown files? (use FileLister::acceptFile())
108-
bool warned = false;
108+
bool warn = false;
109109
std::vector<std::string> ignored = parser.GetIgnoredPaths();
110-
std::vector<std::string>::iterator iterIgnored = ignored.begin();
111-
for (size_t i = 0 ; i < ignored.size();) {
112-
const std::string extension = Path::getFilenameExtension(ignored[i]);
110+
for (std::vector<std::string>::iterator i = ignored.begin(); i != ignored.end();) {
111+
const std::string extension = Path::getFilenameExtension(*i);
113112
if (extension == ".h" || extension == ".hpp") {
114-
ignored.erase(iterIgnored + i);
115-
if (!warned) {
116-
std::cout << "cppcheck: filename exclusion does not apply to header (.h and .hpp) files." << std::endl;
117-
std::cout << "cppcheck: Please use --suppress for ignoring results from the header files." << std::endl;
118-
warned = true; // Warn only once
119-
}
113+
i = ignored.erase(i);
114+
warn = true;
120115
} else
121116
++i;
122117
}
118+
if (warn) {
119+
std::cout << "cppcheck: filename exclusion does not apply to header (.h and .hpp) files." << std::endl;
120+
std::cout << "cppcheck: Please use --suppress for ignoring results from the header files." << std::endl;
121+
}
123122

124123
PathMatch matcher(parser.GetIgnoredPaths());
125-
for (size_t i = 0 ; i < filenames.size();) {
124+
for (std::vector<std::string>::iterator i = filenames.begin() ; i != filenames.end();) {
126125
#if defined(_WIN32)
127126
// For Windows we want case-insensitive path matching
128127
const bool caseSensitive = false;
129128
#else
130129
const bool caseSensitive = true;
131130
#endif
132-
if (matcher.Match(filenames[i], caseSensitive))
133-
filenames.erase(filenames.begin() + i);
131+
if (matcher.Match(*i, caseSensitive))
132+
i = filenames.erase(i);
134133
else
135134
++i;
136135
}

lib/check64bit.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ namespace {
3333
/** Is given variable a pointer or array? */
3434
static bool isaddr(const Variable *var)
3535
{
36-
const Token *nametok = var ? var->nameToken() : 0;
37-
return (var && (nametok->strAt(-2) == "*" || nametok->strAt(-1) == "*" || nametok->strAt(1) == "["));
36+
return (var && (var->isPointer() || var->isArray()));
3837
}
3938

4039
/** Is given variable an integer variable */

0 commit comments

Comments
 (0)