Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed #13517 - added command-line option --{no-}check-headers #7285

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,9 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
else if (std::strcmp(argv[i], "--check-config") == 0)
mSettings.checkConfiguration = true;

else if (std::strcmp(argv[i], "--check-headers") == 0)
mSettings.checkHeaders = true;

// Check level
else if (std::strncmp(argv[i], "--check-level=", 14) == 0) {
Settings::CheckLevel level = Settings::CheckLevel::normal;
Expand Down Expand Up @@ -996,6 +999,9 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
return Result::Fail;
}

else if (std::strcmp(argv[i], "--no-check-headers") == 0)
mSettings.checkHeaders = false;

// undocumented option for usage in Python tests to indicate that no build dir should be injected
else if (std::strcmp(argv[i], "--no-cppcheck-build-dir") == 0) {
mSettings.buildDir.clear();
Expand Down
2 changes: 1 addition & 1 deletion lib/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class CPPCHECKLIB WARN_UNUSED Settings {
/**
* Check code in the headers, this is on by default but can
* be turned off to save CPU */
bool checkHeaders = true; // TODO: CLI
bool checkHeaders = true;

/** Check for incomplete info in library files? */
bool checkLibrary{};
Expand Down
34 changes: 33 additions & 1 deletion test/cli/other_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3035,4 +3035,36 @@ def test_debug_template(tmp_path):
assert stdout.find('### Symbol database ###') == -1
assert stdout.find('##AST') == -1
assert stdout.find('### Template Simplifier pass ') != -1
assert stderr.splitlines() == []
assert stderr.splitlines() == []


def test_check_headers(tmp_path):
test_file_h = tmp_path / 'test.h'
with open(test_file_h, 'wt') as f:
f.write(
"""
inline void hdr()
{
(void)(*((int*)0));
}
""")

test_file_c = tmp_path / 'test.c'
with open(test_file_c, 'wt') as f:
f.write(
"""
#include "test.h"

void f() {}
""")

args = [
'-q',
'--template=simple',
'--no-check-headers',
str(test_file_c)
]
exitcode, stdout, stderr = cppcheck(args)
assert exitcode == 0, stdout
assert stdout.splitlines() == []
assert stderr.splitlines() == [] # no error since the header is not checked
24 changes: 24 additions & 0 deletions test/testcmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,9 @@ class TestCmdlineParser : public TestFixture {
TEST_CASE(debugClangOutput);
TEST_CASE(debugXmlMultiple);
TEST_CASE(debugNormalXmlMultiple);
TEST_CASE(checkHeaders);
TEST_CASE(noCheckHeaders);
TEST_CASE(noCheckHeaders2);

TEST_CASE(ignorepaths1);
TEST_CASE(ignorepaths2);
Expand Down Expand Up @@ -2942,6 +2945,27 @@ class TestCmdlineParser : public TestFixture {
ASSERT_EQUALS("cppcheck: error: printing debug output in XML format does not support multiple input files.\n", logger->str());
}

void checkHeaders() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--check-headers", "file.cpp"};
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv));
ASSERT_EQUALS(true, settings->checkHeaders);
}

void noCheckHeaders() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--no-check-headers", "file.cpp"};
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv));
ASSERT_EQUALS(false, settings->checkHeaders);
}

void noCheckHeaders2() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--check-headers", "--no-check-headers", "file.cpp"};
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(4, argv));
ASSERT_EQUALS(false, settings->checkHeaders);
}

void ignorepaths1() {
REDIRECT;
const char * const argv[] = {"cppcheck", "-isrc", "file.cpp"};
Expand Down
Loading