Skip to content

Commit 5b1d722

Browse files
committed
fixed #13517 - added command-line option --{no-}check-headers
1 parent 1d1093d commit 5b1d722

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

Diff for: cli/cmdlineparser.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,9 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
536536
else if (std::strcmp(argv[i], "--check-config") == 0)
537537
mSettings.checkConfiguration = true;
538538

539+
else if (std::strcmp(argv[i], "--check-headers") == 0)
540+
mSettings.checkHeaders = true;
541+
539542
// Check level
540543
else if (std::strncmp(argv[i], "--check-level=", 14) == 0) {
541544
Settings::CheckLevel level = Settings::CheckLevel::normal;
@@ -991,6 +994,9 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
991994
return Result::Fail;
992995
}
993996

997+
else if (std::strcmp(argv[i], "--no-check-headers") == 0)
998+
mSettings.checkHeaders = false;
999+
9941000
// undocumented option for usage in Python tests to indicate that no build dir should be injected
9951001
else if (std::strcmp(argv[i], "--no-cppcheck-build-dir") == 0) {
9961002
mSettings.buildDir.clear();

Diff for: lib/settings.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class CPPCHECKLIB WARN_UNUSED Settings {
137137
/**
138138
* Check code in the headers, this is on by default but can
139139
* be turned off to save CPU */
140-
bool checkHeaders = true; // TODO: CLI
140+
bool checkHeaders = true;
141141

142142
/** Check for incomplete info in library files? */
143143
bool checkLibrary{};

Diff for: test/cli/other_test.py

+34-1
Original file line numberDiff line numberDiff line change
@@ -3163,4 +3163,37 @@ def test_dir_ignore(tmp_path):
31633163
'Checking {} ...'.format(test_file)
31643164
]
31653165

3166-
assert_cppcheck(args, ec_exp=0, err_exp=[], out_exp=out_lines, cwd=str(tmp_path))
3166+
assert_cppcheck(args, ec_exp=0, err_exp=[], out_exp=out_lines, cwd=str(tmp_path))
3167+
3168+
3169+
3170+
def test_check_headers(tmp_path):
3171+
test_file_h = tmp_path / 'test.h'
3172+
with open(test_file_h, 'wt') as f:
3173+
f.write(
3174+
"""
3175+
inline void hdr()
3176+
{
3177+
(void)(*((int*)0));
3178+
}
3179+
""")
3180+
3181+
test_file_c = tmp_path / 'test.c'
3182+
with open(test_file_c, 'wt') as f:
3183+
f.write(
3184+
"""
3185+
#include "test.h"
3186+
3187+
void f() {}
3188+
""")
3189+
3190+
args = [
3191+
'-q',
3192+
'--template=simple',
3193+
'--no-check-headers',
3194+
str(test_file_c)
3195+
]
3196+
exitcode, stdout, stderr = cppcheck(args)
3197+
assert exitcode == 0, stdout
3198+
assert stdout.splitlines() == []
3199+
assert stderr.splitlines() == [] # no error since the header is not checked

Diff for: test/testcmdlineparser.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,9 @@ class TestCmdlineParser : public TestFixture {
441441
TEST_CASE(debugXmlMultiple);
442442
TEST_CASE(debugNormalXmlMultiple);
443443
TEST_CASE(debugIgnore);
444+
TEST_CASE(checkHeaders);
445+
TEST_CASE(noCheckHeaders);
446+
TEST_CASE(noCheckHeaders2);
444447

445448
TEST_CASE(ignorepaths1);
446449
TEST_CASE(ignorepaths2);
@@ -2988,6 +2991,27 @@ class TestCmdlineParser : public TestFixture {
29882991
ASSERT_EQUALS(true, settings->debugignore);
29892992
}
29902993

2994+
void checkHeaders() {
2995+
REDIRECT;
2996+
const char * const argv[] = {"cppcheck", "--check-headers", "file.cpp"};
2997+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv));
2998+
ASSERT_EQUALS(true, settings->checkHeaders);
2999+
}
3000+
3001+
void noCheckHeaders() {
3002+
REDIRECT;
3003+
const char * const argv[] = {"cppcheck", "--no-check-headers", "file.cpp"};
3004+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv));
3005+
ASSERT_EQUALS(false, settings->checkHeaders);
3006+
}
3007+
3008+
void noCheckHeaders2() {
3009+
REDIRECT;
3010+
const char * const argv[] = {"cppcheck", "--check-headers", "--no-check-headers", "file.cpp"};
3011+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(4, argv));
3012+
ASSERT_EQUALS(false, settings->checkHeaders);
3013+
}
3014+
29913015
void ignorepaths1() {
29923016
REDIRECT;
29933017
const char * const argv[] = {"cppcheck", "-isrc", "file.cpp"};

0 commit comments

Comments
 (0)