Skip to content

Commit 3f6dbf2

Browse files
committed
added CLI option --debug-clang-ast to print Clang AST
1 parent 0be0ca2 commit 3f6dbf2

File tree

5 files changed

+41
-0
lines changed

5 files changed

+41
-0
lines changed

cli/cmdlineparser.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,10 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
614614
mSettings.cppHeaderProbe = true;
615615
}
616616

617+
// Show debug warnings for lookup for configuration files
618+
else if (std::strcmp(argv[i], "--debug-clang-ast") == 0)
619+
mSettings.debugClangAst = true;
620+
617621
// Show --debug output after the first simplifications
618622
else if (std::strcmp(argv[i], "--debug") == 0 ||
619623
std::strcmp(argv[i], "--debug-normal") == 0)

lib/cppcheck.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,10 @@ unsigned int CppCheck::checkClang(const FileWithDetails &file)
684684
return 0; // TODO: report as failure?
685685
}
686686

687+
if (mSettings.debugClangAst) {
688+
std::cout << output2 << std::endl;
689+
}
690+
687691
if (output2.find("TranslationUnitDecl") == std::string::npos) {
688692
// TODO: report as proper error
689693
std::cerr << "Failed to execute '" << exe << " " << args2 << " " << redirect2 << "' - (no TranslationUnitDecl in output)" << std::endl;

lib/settings.h

+3
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ class CPPCHECKLIB WARN_UNUSED Settings {
178178
/** @brief Are we running from DACA script? */
179179
bool daca{};
180180

181+
/** @brief Is --debug-clang-ast given? */
182+
bool debugClangAst{};
183+
181184
/** @brief Internal: Is --debug-lookup or --debug-lookup=all given? */
182185
bool debuglookup{};
183186

test/cli/clang-import_test.py

+22
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,25 @@ def test_cmd_std_c_enforce_alias_2(tmp_path): # #13128/#13129/#13130
239239

240240
def test_cmd_std_cpp_enforce_alias(tmp_path): # #13128/#13129/#13130
241241
__test_cmd(tmp_path, 'test.c',['--language=c++', '--std=gnu99', '--std=gnu++11'], '-x c++ -std=gnu++11')
242+
243+
244+
def test_debug_ast(tmp_path):
245+
test_file = tmp_path / 'test.c'
246+
with open(test_file, 'wt') as f:
247+
f.write(
248+
"""
249+
void f() {}
250+
""")
251+
252+
args = [
253+
'-q',
254+
'--clang',
255+
'--debug-clang-ast',
256+
str(test_file)
257+
]
258+
259+
exitcode, stdout, stderr = cppcheck(args)
260+
assert exitcode == 0, stderr if not stdout else stdout
261+
assert stderr == ''
262+
assert stdout.startswith('TranslationUnitDecl'), stdout
263+
assert stdout.find(str(test_file)) != -1, stdout

test/testcmdlineparser.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ class TestCmdlineParser : public TestFixture {
422422
TEST_CASE(maxTemplateRecursion);
423423
TEST_CASE(maxTemplateRecursionMissingCount);
424424
TEST_CASE(emitDuplicates);
425+
TEST_CASE(debugClangAst);
425426

426427
TEST_CASE(ignorepaths1);
427428
TEST_CASE(ignorepaths2);
@@ -2907,6 +2908,13 @@ class TestCmdlineParser : public TestFixture {
29072908
ASSERT_EQUALS(true, settings->emitDuplicates);
29082909
}
29092910

2911+
void debugClangAst() {
2912+
REDIRECT;
2913+
const char * const argv[] = {"cppcheck", "--debug-clang-ast", "file.cpp"};
2914+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv));
2915+
ASSERT_EQUALS(true, settings->debugClangAst);
2916+
}
2917+
29102918
void ignorepaths1() {
29112919
REDIRECT;
29122920
const char * const argv[] = {"cppcheck", "-isrc", "file.cpp"};

0 commit comments

Comments
 (0)