Skip to content

Commit 7c1981b

Browse files
authored
refs #13618 - cleaned up some --debug handling and dependencies (danmar#7592)
1 parent fd7558d commit 7c1981b

File tree

6 files changed

+103
-21
lines changed

6 files changed

+103
-21
lines changed

cli/cmdlineparser.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
415415

416416
bool def = false;
417417
bool maxconfigs = false;
418+
bool debug = false;
418419

419420
ImportProject::Type projectType = ImportProject::Type::NONE;
420421
ImportProject project;
@@ -641,7 +642,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
641642
// Show --debug output after the first simplifications
642643
else if (std::strcmp(argv[i], "--debug") == 0 ||
643644
std::strcmp(argv[i], "--debug-normal") == 0)
644-
mSettings.debugnormal = true;
645+
debug = true;
645646

646647
// Show debug warnings for lookup for configuration files
647648
else if (std::strcmp(argv[i], "--debug-lookup") == 0)
@@ -1572,10 +1573,18 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
15721573

15731574
if (mSettings.force)
15741575
mSettings.maxConfigs = INT_MAX;
1575-
15761576
else if ((def || mSettings.preprocessOnly) && !maxconfigs)
15771577
mSettings.maxConfigs = 1U;
15781578

1579+
if (debug) {
1580+
mSettings.debugnormal = true;
1581+
mSettings.debugvalueflow = true;
1582+
if (mSettings.verbose) {
1583+
mSettings.debugast = true;
1584+
mSettings.debugsymdb = true;
1585+
}
1586+
}
1587+
15791588
if (mSettings.jobs > 1 && mSettings.buildDir.empty()) {
15801589
// TODO: bail out instead?
15811590
if (mSettings.checks.isEnabled(Checks::unusedFunction))

lib/token.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,7 +1655,7 @@ static void astStringXml(const Token *tok, nonneg int indent, std::ostream &out)
16551655
}
16561656
}
16571657

1658-
void Token::printAst(bool verbose, bool xml, const std::vector<std::string> &fileNames, std::ostream &out) const
1658+
void Token::printAst(bool xml, const std::vector<std::string> &fileNames, std::ostream &out) const
16591659
{
16601660
if (!xml)
16611661
out << "\n\n##AST" << std::endl;
@@ -1672,10 +1672,8 @@ void Token::printAst(bool verbose, bool xml, const std::vector<std::string> &fil
16721672
<< "\" column=\"" << tok->column() << "\">" << std::endl;
16731673
astStringXml(tok, 2U, out);
16741674
out << "</ast>" << std::endl;
1675-
} else if (verbose)
1675+
} else
16761676
out << "[" << fileNames[tok->fileIndex()] << ":" << tok->linenr() << "]" << std::endl << tok->astStringVerbose() << std::endl;
1677-
else
1678-
out << tok->astString(" ") << std::endl;
16791677
if (tok->str() == "(")
16801678
tok = tok->link();
16811679
}

lib/token.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1544,6 +1544,7 @@ class CPPCHECKLIB Token {
15441544
mImpl->mValues = nullptr;
15451545
}
15461546

1547+
// cppcheck-suppress unusedFunction - used in tests only
15471548
std::string astString(const char *sep = "") const {
15481549
std::string ret;
15491550
if (mImpl->mAstOperand1)
@@ -1559,7 +1560,7 @@ class CPPCHECKLIB Token {
15591560

15601561
std::string expressionString() const;
15611562

1562-
void printAst(bool verbose, bool xml, const std::vector<std::string> &fileNames, std::ostream &out) const;
1563+
void printAst(bool xml, const std::vector<std::string> &fileNames, std::ostream &out) const;
15631564

15641565
void printValueFlow(const std::vector<std::string>& files, bool xml, std::ostream &out) const;
15651566

lib/tokenize.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5941,7 +5941,6 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
59415941
}
59425942
//---------------------------------------------------------------------------
59435943

5944-
// TODO: do not depend on --verbose
59455944
void Tokenizer::printDebugOutput(std::ostream &out) const
59465945
{
59475946
if (!list.front())
@@ -5962,14 +5961,14 @@ void Tokenizer::printDebugOutput(std::ostream &out) const
59625961
if (mSymbolDatabase) {
59635962
if (xml)
59645963
mSymbolDatabase->printXml(out);
5965-
else if (mSettings.debugsymdb || (mSettings.debugnormal && mSettings.verbose))
5964+
else if (mSettings.debugsymdb)
59665965
mSymbolDatabase->printOut("Symbol database");
59675966
}
59685967

5969-
if (mSettings.debugast || (mSettings.debugnormal && mSettings.verbose))
5970-
list.front()->printAst(mSettings.verbose, xml, list.getFiles(), out);
5968+
if (mSettings.debugast)
5969+
list.front()->printAst(xml, list.getFiles(), out);
59715970

5972-
if (mSettings.debugnormal || mSettings.debugvalueflow)
5971+
if (mSettings.debugvalueflow)
59735972
list.front()->printValueFlow(list.getFiles(), xml, out);
59745973

59755974
if (xml)

test/cli/other_test.py

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2701,8 +2701,9 @@ def test_debug_verbose_xml(tmp_path):
27012701
assert len(ast_elem) == 1
27022702

27032703

2704+
# TODO: remove interaction with --debug?
27042705
# TODO: test with --xml
2705-
def __test_debug_template(tmp_path, verbose):
2706+
def __test_debug_template(tmp_path, verbose=False, debug=False):
27062707
test_file = tmp_path / 'test.cpp'
27072708
with open(test_file, "w") as f:
27082709
f.write(
@@ -2722,27 +2723,54 @@ def __test_debug_template(tmp_path, verbose):
27222723

27232724
if verbose:
27242725
args += ['--verbose']
2726+
if debug:
2727+
args += ['--debug']
27252728

27262729
exitcode, stdout, stderr = cppcheck(args)
27272730
assert exitcode == 0, stdout
2728-
assert stdout.find('##file ') == -1
2729-
assert stdout.find('##Value flow') == -1
2730-
assert stdout.find('### Symbol database ###') == -1
2731-
assert stdout.find('##AST') == -1
2732-
assert stdout.find('### Template Simplifier pass ') != -1
2731+
if debug:
2732+
assert stdout.find('##file ') != -1
2733+
else:
2734+
assert stdout.find('##file ') == -1
2735+
if debug:
2736+
assert stdout.find('##Value flow') != -1
2737+
else:
2738+
assert stdout.find('##Value flow') == -1
2739+
if debug and verbose:
2740+
assert stdout.find('### Symbol database ###') != -1
2741+
else:
2742+
assert stdout.find('### Symbol database ###') == -1
2743+
if debug and verbose:
2744+
assert stdout.find('##AST') != -1
2745+
else:
2746+
assert stdout.find('##AST') == -1
2747+
if debug:
2748+
assert stdout.count('### Template Simplifier pass ') == 2
2749+
else:
2750+
assert stdout.count('### Template Simplifier pass ') == 1
27332751
assert stderr.splitlines() == [
27342752
'{}:4:13: error: Null pointer dereference: (int*)nullptr [nullPointer]'.format(test_file)
27352753
]
27362754
return stdout
27372755

27382756

27392757
def test_debug_template(tmp_path):
2740-
__test_debug_template(tmp_path, False)
2758+
__test_debug_template(tmp_path, verbose=False)
27412759

27422760

27432761
def test_debug_template_verbose_nodiff(tmp_path):
27442762
# make sure --verbose does not change the output
2745-
assert __test_debug_template(tmp_path, False) == __test_debug_template(tmp_path, True)
2763+
assert __test_debug_template(tmp_path, verbose=False) == __test_debug_template(tmp_path, verbose=True)
2764+
2765+
2766+
def test_debug_template_debug(tmp_path):
2767+
__test_debug_template(tmp_path, debug=True)
2768+
2769+
2770+
@pytest.mark.xfail(strict=True) # TODO: remove dependency on --verbose
2771+
def test_debug_template_debug_verbose_nodiff(tmp_path):
2772+
# make sure --verbose does not change the output
2773+
assert __test_debug_template(tmp_path, debug=True, verbose=False) == __test_debug_template(tmp_path, debug=True, verbose=True)
27462774

27472775

27482776
def test_file_ignore_2(tmp_path): # #13570
@@ -3467,7 +3495,6 @@ def test_debug_ast(tmp_path):
34673495
__test_debug_ast(tmp_path, False)
34683496

34693497

3470-
@pytest.mark.xfail(strict=True) # TODO: remove dependency on --verbose
34713498
def test_debug_ast_verbose_nodiff(tmp_path):
34723499
# make sure --verbose does not change the output
34733500
assert __test_debug_ast(tmp_path, False) == __test_debug_ast(tmp_path, True)

test/testcmdlineparser.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,10 @@ class TestCmdlineParser : public TestFixture {
463463
TEST_CASE(debugSymdb);
464464
TEST_CASE(debugAst);
465465
TEST_CASE(debugValueflow);
466+
TEST_CASE(debugNormal);
467+
TEST_CASE(debugNormalVerbose);
468+
TEST_CASE(debug);
469+
TEST_CASE(debugVerbose);
466470

467471
TEST_CASE(ignorepaths1);
468472
TEST_CASE(ignorepaths2);
@@ -3186,6 +3190,50 @@ class TestCmdlineParser : public TestFixture {
31863190
ASSERT_EQUALS(true, settings->debugvalueflow);
31873191
}
31883192

3193+
void debugNormal() {
3194+
REDIRECT;
3195+
const char * const argv[] = {"cppcheck", "--debug-normal", "file.cpp"};
3196+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
3197+
ASSERT_EQUALS(true, settings->debugnormal);
3198+
ASSERT_EQUALS(false, settings->debugSimplified);
3199+
ASSERT_EQUALS(true, settings->debugvalueflow);
3200+
ASSERT_EQUALS(false, settings->debugast);
3201+
ASSERT_EQUALS(false, settings->debugsymdb);
3202+
}
3203+
3204+
void debugNormalVerbose() {
3205+
REDIRECT;
3206+
const char * const argv[] = {"cppcheck", "--debug-normal", "--verbose", "file.cpp"};
3207+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
3208+
ASSERT_EQUALS(true, settings->debugnormal);
3209+
ASSERT_EQUALS(false, settings->debugSimplified);
3210+
ASSERT_EQUALS(true, settings->debugvalueflow);
3211+
ASSERT_EQUALS(true, settings->debugast);
3212+
ASSERT_EQUALS(true, settings->debugsymdb);
3213+
}
3214+
3215+
void debug() {
3216+
REDIRECT;
3217+
const char * const argv[] = {"cppcheck", "--debug", "file.cpp"};
3218+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
3219+
ASSERT_EQUALS(true, settings->debugnormal);
3220+
ASSERT_EQUALS(false, settings->debugSimplified);
3221+
ASSERT_EQUALS(true, settings->debugvalueflow);
3222+
ASSERT_EQUALS(false, settings->debugast);
3223+
ASSERT_EQUALS(false, settings->debugsymdb);
3224+
}
3225+
3226+
void debugVerbose() {
3227+
REDIRECT;
3228+
const char * const argv[] = {"cppcheck", "--debug", "--verbose", "file.cpp"};
3229+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
3230+
ASSERT_EQUALS(true, settings->debugnormal);
3231+
ASSERT_EQUALS(false, settings->debugSimplified);
3232+
ASSERT_EQUALS(true, settings->debugvalueflow);
3233+
ASSERT_EQUALS(true, settings->debugast);
3234+
ASSERT_EQUALS(true, settings->debugsymdb);
3235+
}
3236+
31893237
void ignorepaths1() {
31903238
REDIRECT;
31913239
const char * const argv[] = {"cppcheck", "-isrc", "file.cpp"};

0 commit comments

Comments
 (0)