Skip to content

Commit

Permalink
added CLI option --debug-clang-ast to print Clang AST
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Jan 13, 2025
1 parent d0b7476 commit 751bcd7
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,10 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
mSettings.cppHeaderProbe = true;
}

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

// Show --debug output after the first simplifications
else if (std::strcmp(argv[i], "--debug") == 0 ||
std::strcmp(argv[i], "--debug-normal") == 0)
Expand Down
4 changes: 4 additions & 0 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,10 @@ unsigned int CppCheck::checkClang(const FileWithDetails &file)
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
3 changes: 3 additions & 0 deletions lib/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ class CPPCHECKLIB WARN_UNUSED Settings {
/** @brief Are we running from DACA script? */
bool daca{};

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

/** @brief Internal: Is --debug-lookup or --debug-lookup=all given? */
bool debuglookup{};

Expand Down
22 changes: 22 additions & 0 deletions test/cli/clang-import_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,25 @@ def test_cmd_std_c_enforce_alias_2(tmp_path): # #13128/#13129/#13130

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):
test_file = tmp_path / 'test.c'
with open(test_file, 'wt') as f:
f.write(
"""
void f() {}
""")

args = [
'-q',
'--clang',
'--debug-clang-ast',
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
8 changes: 8 additions & 0 deletions test/testcmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ class TestCmdlineParser : public TestFixture {
TEST_CASE(maxTemplateRecursion);
TEST_CASE(maxTemplateRecursionMissingCount);
TEST_CASE(emitDuplicates);
TEST_CASE(debugClangAst);

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

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

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

0 comments on commit 751bcd7

Please sign in to comment.