Skip to content

Commit fd2d369

Browse files
committed
renamed --debug-clang-ast to -debug-clang-output as it might contain compiler warnings/errors - also log it earlier
1 parent f9e0814 commit fd2d369

File tree

5 files changed

+50
-14
lines changed

5 files changed

+50
-14
lines changed

Diff for: cli/cmdlineparser.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -615,8 +615,8 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
615615
}
616616

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

621621
// Show --debug output after the first simplifications
622622
else if (std::strcmp(argv[i], "--debug") == 0 ||

Diff for: lib/cppcheck.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -680,16 +680,16 @@ unsigned int CppCheck::checkClang(const FileWithDetails &file)
680680

681681
std::string output2;
682682
const int exitcode = mExecuteCommand(exe,split(args2),redirect2,output2);
683+
if (mSettings.debugClangOutput) {
684+
std::cout << output2 << std::endl;
685+
}
686+
// TODO: this might also fail if compiler errors are encountered - we should report them properly
683687
if (exitcode != EXIT_SUCCESS) {
684688
// TODO: report as proper error
685689
std::cerr << "Failed to execute '" << exe << " " << args2 << " " << redirect2 << "' - (exitcode: " << exitcode << " / output: " << output2 << ")" << std::endl;
686690
return 0; // TODO: report as failure?
687691
}
688692

689-
if (mSettings.debugClangAst) {
690-
std::cout << output2 << std::endl;
691-
}
692-
693693
if (output2.find("TranslationUnitDecl") == std::string::npos) {
694694
// TODO: report as proper error
695695
std::cerr << "Failed to execute '" << exe << " " << args2 << " " << redirect2 << "' - (no TranslationUnitDecl in output)" << std::endl;

Diff for: lib/settings.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ 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{};
181+
/** @brief Is --debug-clang-output given? */
182+
bool debugClangOutput{};
183183

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

Diff for: test/cli/clang-import_test.py

+38-2
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ 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')
242242

243243

244-
def test_debug_ast(tmp_path):
244+
def test_debug_clang_output(tmp_path):
245245
test_file = tmp_path / 'test.c'
246246
with open(test_file, 'wt') as f:
247247
f.write(
@@ -252,12 +252,48 @@ def test_debug_ast(tmp_path):
252252
args = [
253253
'-q',
254254
'--clang',
255-
'--debug-clang-ast',
255+
'--debug-clang-output',
256256
str(test_file)
257257
]
258258

259259
exitcode, stdout, stderr = cppcheck(args)
260260
assert exitcode == 0, stderr if not stdout else stdout
261261
assert stderr == ''
262262
assert stdout.startswith('TranslationUnitDecl'), stdout
263+
assert stdout.find(str(test_file)) != -1, stdout
264+
265+
266+
def test_debug_clang_output_failure_exitcode(tmp_path):
267+
# the given code will cause clang to fail with an exitcode
268+
#
269+
# 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)
270+
# 3 | (void)(*0);
271+
# | ^~
272+
# 1 error generated.
273+
# TranslationUnitDecl 0x6127d5d9d4e8 <<invalid sloc>> <invalid sloc>
274+
# ...
275+
test_file = tmp_path / 'test.c'
276+
with open(test_file, 'wt') as f:
277+
f.write(
278+
"""void f()
279+
{
280+
(void)(*0);
281+
}
282+
""")
283+
284+
args = [
285+
'-q',
286+
'--clang',
287+
'--debug-clang-output',
288+
'--no-cppcheck-build-dir', # TODO: test without this?
289+
str(test_file)
290+
]
291+
292+
exitcode, stdout, stderr = cppcheck(args)
293+
assert exitcode == 0, stderr if not stdout else stdout
294+
stderr_lines = stderr.splitlines()
295+
assert len(stderr_lines) > 5, stderr_lines
296+
assert (stderr_lines[0] ==
297+
"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))
298+
assert stdout.find('TranslationUnitDecl') != -1, stdout
263299
assert stdout.find(str(test_file)) != -1, stdout

Diff for: test/testcmdlineparser.cpp

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

427427
TEST_CASE(ignorepaths1);
428428
TEST_CASE(ignorepaths2);
@@ -2908,11 +2908,11 @@ class TestCmdlineParser : public TestFixture {
29082908
ASSERT_EQUALS(true, settings->emitDuplicates);
29092909
}
29102910

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

29182918
void ignorepaths1() {

0 commit comments

Comments
 (0)