Skip to content

Commit

Permalink
renamed --debug-clang-ast to -debug-clang-output as it might cont…
Browse files Browse the repository at this point in the history
…ain compiler warnings/errors - also log it earlier
  • Loading branch information
firewave committed Jan 13, 2025
1 parent c1ab84c commit 7ad1ab4
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 14 deletions.
4 changes: 2 additions & 2 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,8 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
}

// Show debug warnings for lookup for configuration files
else if (std::strcmp(argv[i], "--debug-clang-ast") == 0)
mSettings.debugClangAst = true;
else if (std::strcmp(argv[i], "--debug-clang-output") == 0)
mSettings.debugClangOutput = true;

// Show --debug output after the first simplifications
else if (std::strcmp(argv[i], "--debug") == 0 ||
Expand Down
7 changes: 3 additions & 4 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,16 +680,15 @@ unsigned int CppCheck::checkClang(const FileWithDetails &file)

std::string output2;
const int exitcode = mExecuteCommand(exe,split(args2),redirect2,output2);
if (mSettings.debugClangOutput) {
std::cout << output2 << std::endl;
}
if (exitcode != EXIT_SUCCESS) {
// TODO: report as proper error
std::cerr << "Failed to execute '" << exe << " " << args2 << " " << redirect2 << "' - (exitcode: " << exitcode << " / output: " << output2 << ")" << std::endl;
return 0; // TODO: report as failure?
}

if (mSettings.debugClangAst) {
std::cout << output2 << std::endl;
}

if (output2.find("TranslationUnitDecl") == std::string::npos) {
// TODO: report as proper error
std::cerr << "Failed to execute '" << exe << " " << args2 << " " << redirect2 << "' - (no TranslationUnitDecl in output)" << std::endl;
Expand Down
4 changes: 2 additions & 2 deletions lib/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ class CPPCHECKLIB WARN_UNUSED Settings {
/** @brief Are we running from DACA script? */
bool daca{};

/** @brief Is --debug-clang-ast given? */
bool debugClangAst{};
/** @brief Is --debug-clang-output given? */
bool debugClangOutput{};

/** @brief Internal: Is --debug-lookup or --debug-lookup=all given? */
bool debuglookup{};
Expand Down
39 changes: 37 additions & 2 deletions test/cli/clang-import_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def test_cmd_std_cpp_enforce_alias(tmp_path): # #13128/#13129/#13130
__test_cmd(tmp_path, 'test.c',['--language=c++', '--std=gnu99', '--std=gnu++11'], '-x c++ -std=gnu++11')


def test_debug_ast(tmp_path):
def test_debug_clang_output(tmp_path):
test_file = tmp_path / 'test.c'
with open(test_file, 'wt') as f:
f.write(
Expand All @@ -252,12 +252,47 @@ def test_debug_ast(tmp_path):
args = [
'-q',
'--clang',
'--debug-clang-ast',
'--debug-clang-output',
str(test_file)
]

exitcode, stdout, stderr = cppcheck(args)
assert exitcode == 0, stderr if not stdout else stdout
assert stderr == ''
assert stdout.startswith('TranslationUnitDecl'), stdout
assert stdout.find(str(test_file)) != -1, stdout


def test_debug_clang_output_failure_exitcode(tmp_path):
# the given code will cause clang to fail with an exitcode
#
# Failed to execute 'clang -fsyntax-only -Xclang -ast-dump -fno-color-diagnostics -x c++ a.cpp 2>&1' - (exitcode: 1 / output: a.cpp:3:12: error: indirection requires pointer operand ('int' invalid)
# 3 | (void)(*0);
# | ^~
# 1 error generated.
# TranslationUnitDecl 0x6127d5d9d4e8 <<invalid sloc>> <invalid sloc>
# ...
test_file = tmp_path / 'test.c'
with open(test_file, 'wt') as f:
f.write(
"""void f()
{
(void)(*0);
}
""")

args = [
'-q',
'--clang',
'--debug-clang-output',
str(test_file)
]

exitcode, stdout, stderr = cppcheck(args)
assert exitcode == 0, stderr if not stdout else stdout
stderr_lines = stderr.splitlines()
assert len(stderr_lines) > 5, stderr_lines
assert (stderr_lines[0] ==
"Failed to execute 'clang -fsyntax-only -Xclang -ast-dump -fno-color-diagnostics -x c {} 2>&1' - (exitcode: 1 / output: {}:3:12: error: indirection requires pointer operand ('int' invalid)".format(test_file, test_file))
assert stdout.find('TranslationUnitDecl') != -1, stdout
assert stdout.find(str(test_file)) != -1, stdout
8 changes: 4 additions & 4 deletions test/testcmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ class TestCmdlineParser : public TestFixture {
TEST_CASE(maxTemplateRecursion);
TEST_CASE(maxTemplateRecursionMissingCount);
TEST_CASE(emitDuplicates);
TEST_CASE(debugClangAst);
TEST_CASE(debugClangOutput);

TEST_CASE(ignorepaths1);
TEST_CASE(ignorepaths2);
Expand Down Expand Up @@ -2908,11 +2908,11 @@ class TestCmdlineParser : public TestFixture {
ASSERT_EQUALS(true, settings->emitDuplicates);
}

void debugClangAst() {
void debugClangOutput() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--debug-clang-ast", "file.cpp"};
const char * const argv[] = {"cppcheck", "--debug-clang-output", "file.cpp"};
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv));
ASSERT_EQUALS(true, settings->debugClangAst);
ASSERT_EQUALS(true, settings->debugClangOutput);
}

void ignorepaths1() {
Expand Down

0 comments on commit 7ad1ab4

Please sign in to comment.