Skip to content

Commit ab827e3

Browse files
author
Daniel Marjamäki
committed
Merge pull request #40 from joshbeck/master
Added option to set #ifdef configuration check limit
2 parents b332ea8 + 5656183 commit ab827e3

6 files changed

Lines changed: 84 additions & 5 deletions

File tree

‎cli/cmdlineparser.cpp‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,25 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
561561
}
562562
}
563563

564+
// Set maximum number of #ifdef configurations to check
565+
else if (strncmp(argv[i], "--max-configs=", 14) == 0)
566+
{
567+
_settings->_force = false;
568+
569+
std::istringstream iss(14+argv[i]);
570+
if (!(iss >> _settings->_maxConfigs))
571+
{
572+
PrintMessage("cppcheck: argument to '--max-configs=' is not a number");
573+
return false;
574+
}
575+
576+
if (_settings->_maxConfigs < 1)
577+
{
578+
PrintMessage("cppcheck: argument to '--max-configs=' must be greater than 0");
579+
return false;
580+
}
581+
}
582+
564583
// Print help
565584
else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
566585
_pathnames.clear();
@@ -660,8 +679,9 @@ void CmdLineParser::PrintHelp()
660679
" --file-list=<file> Specify the files to check in a text file. Add one\n"
661680
" filename per line. When file is -, the file list will\n"
662681
" be read from standard input.\n"
663-
" -f, --force Force checking of all configurations in files that have\n"
664-
" \"too many\" configurations.\n"
682+
" -f, --force Force checking of all configurations in files. If used\n"
683+
" together with --max-ifdefs=, the last option is the one\n"
684+
" that is effective.\n"
665685
" -h, --help Print this help.\n"
666686
" -I <dir> Give include path. Give several -I parameters to give\n"
667687
" several paths. First given path is checked first. If\n"
@@ -674,6 +694,11 @@ void CmdLineParser::PrintHelp()
674694
" more comments, like: // cppcheck-suppress warningId\n"
675695
" on the lines before the warning to suppress.\n"
676696
" -j <jobs> Start [jobs] threads to do the checking simultaneously.\n"
697+
" --max-configs=<limit>\n"
698+
" Maximum number of configurations to check in a file\n"
699+
" before skipping it. Default is 12. If used together\n"
700+
" with --force, the last option is the one that is\n"
701+
" effective.\n"
677702
" --platform=<type> Specifies platform specific types and sizes. The\n"
678703
" available platforms are:\n"
679704
" * unix32\n"

‎lib/cppcheck.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,9 @@ unsigned int CppCheck::processFile()
195195

196196
int checkCount = 0;
197197
for (std::list<std::string>::const_iterator it = configurations.begin(); it != configurations.end(); ++it) {
198-
// Check only 12 first configurations, after that bail out, unless --force
198+
// Check only a few configurations (default 12), after that bail out, unless --force
199199
// was used.
200-
if (!_settings._force && checkCount > 11) {
200+
if (!_settings._force && checkCount >= _settings._maxConfigs) {
201201

202202
const std::string fixedpath = Path::toNativeSeparators(_filename);
203203
ErrorLogger::ErrorMessage::FileLocation location;

‎lib/settings.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Settings::Settings()
3838
_showtime = 0; // TODO: use enum
3939
_append = "";
4040
_terminate = false;
41+
_maxConfigs = 12;
4142
inconclusive = false;
4243
experimental = false;
4344
test_2_pass = false;

‎lib/settings.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ class Settings {
121121
/** @brief get append code (--append) */
122122
std::string append() const;
123123

124+
/** @brief Maximum number of configurations to check before bailing.
125+
Default is 12. (--max-configs=N) */
126+
int _maxConfigs;
127+
124128
/**
125129
* @brief Returns true if given id is in the list of
126130
* enabled extra checks (--enable)

‎man/cppcheck.1.xml‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ man(1), man(7), http://www.tldp.org/HOWTO/Man-Page/
116116
<arg choice="opt"><option>-i&lt;dir&gt;</option></arg>
117117
<arg choice="opt"><option>--inline-suppr</option></arg>
118118
<arg choice="opt"><option>-j&lt;jobs&gt;</option></arg>
119+
<arg choice="opt"><option>--max-configs=&lt;limit&gt;</option></arg>
119120
<arg choice="opt"><option>--platform=&lt;type&gt;</option></arg>
120121
<arg choice="opt"><option>--quiet</option></arg>
121122
<arg choice="opt"><option>--report-progress</option></arg>
@@ -253,7 +254,7 @@ Example: -DDEBUG=1 -D__cplusplus</para>
253254
<term><option>--force</option></term>
254255
<listitem>
255256
<para>Force checking of files that have a lot of configurations. Error is printed if such a file is found so there is no reason to use this by
256-
default.</para>
257+
default. If used together with --max-ifdefs=, the last option is the one that is effective.</para>
257258
</listitem>
258259
</varlistentry>
259260
<varlistentry>
@@ -292,6 +293,13 @@ Directory name is matched to all parts of the path.</para>
292293
<para>Start &lt;jobs&gt; threads to do the checking work.</para>
293294
</listitem>
294295
</varlistentry>
296+
<varlistentry>
297+
<term><option>--max-configs=&lt;limit&gt;</option></term>
298+
<listitem>
299+
<para>Maximum number of configurations to check in a file before skipping it. Default is 12. If used together with --force, the last option is
300+
the one that is effective.</para>
301+
</listitem>
302+
</varlistentry>
295303
<varlistentry>
296304
<term><option>--platform=&lt;type&gt;</option></term>
297305
<listitem>

‎test/testcmdlineparser.cpp‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ class TestCmdlineParser : public TestFixture {
7474
TEST_CASE(jobs);
7575
TEST_CASE(jobsMissingCount);
7676
TEST_CASE(jobsInvalid);
77+
TEST_CASE(maxConfigs);
78+
TEST_CASE(maxConfigsMissingCount);
79+
TEST_CASE(maxConfigsInvalid);
80+
TEST_CASE(maxConfigsTooSmall);
7781
TEST_CASE(reportProgressTest); // "Test" suffix to avoid hiding the parent's reportProgress
7882
TEST_CASE(stdposix);
7983
TEST_CASE(stdc99);
@@ -537,6 +541,43 @@ class TestCmdlineParser : public TestFixture {
537541
ASSERT_EQUALS(false, parser.ParseFromArgs(4, argv));
538542
}
539543

544+
void maxConfigs() {
545+
REDIRECT;
546+
const char *argv[] = {"cppcheck", "-f", "--max-configs=12", "file.cpp"};
547+
Settings settings;
548+
CmdLineParser parser(&settings);
549+
ASSERT(parser.ParseFromArgs(4, argv));
550+
ASSERT_EQUALS(12, settings._maxConfigs);
551+
ASSERT_EQUALS(false, settings._force);
552+
}
553+
554+
void maxConfigsMissingCount() {
555+
REDIRECT;
556+
const char *argv[] = {"cppcheck", "--max-configs=", "file.cpp"};
557+
Settings settings;
558+
CmdLineParser parser(&settings);
559+
// Fails since --max-configs= is missing limit
560+
ASSERT_EQUALS(false, parser.ParseFromArgs(3, argv));
561+
}
562+
563+
void maxConfigsInvalid() {
564+
REDIRECT;
565+
const char *argv[] = {"cppcheck", "--max-configs=e", "file.cpp"};
566+
Settings settings;
567+
CmdLineParser parser(&settings);
568+
// Fails since invalid count given for --max-configs=
569+
ASSERT_EQUALS(false, parser.ParseFromArgs(3, argv));
570+
}
571+
572+
void maxConfigsTooSmall() {
573+
REDIRECT;
574+
const char *argv[] = {"cppcheck", "--max-configs=0", "file.cpp"};
575+
Settings settings;
576+
CmdLineParser parser(&settings);
577+
// Fails since limit must be greater than 0
578+
ASSERT_EQUALS(false, parser.ParseFromArgs(3, argv));
579+
}
580+
540581
void reportProgressTest() {
541582
REDIRECT;
542583
const char *argv[] = {"cppcheck", "--report-progress", "file.cpp"};

0 commit comments

Comments
 (0)