Skip to content

Commit 6927901

Browse files
committed
added CLI options to control whole program analysis
1 parent 8bc093c commit 6927901

5 files changed

Lines changed: 52 additions & 9 deletions

File tree

cli/cmdlineparser.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,9 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
10271027
else if (std::strcmp(argv[i], "--no-safety") == 0)
10281028
mSettings.safety = false;
10291029

1030+
else if (std::strcmp(argv[i], "--no-whole-program") == 0)
1031+
mSettings.wholeProgram = false;
1032+
10301033
// Write results in file
10311034
else if (std::strncmp(argv[i], "--output-file=", 14) == 0)
10321035
mSettings.outputFile = Path::simplifyPath(argv[i] + 14);
@@ -1566,6 +1569,9 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
15661569
else if (std::strcmp(argv[i], "-v") == 0 || std::strcmp(argv[i], "--verbose") == 0)
15671570
mSettings.verbose = true;
15681571

1572+
else if (std::strcmp(argv[i], "--whole-program") == 0)
1573+
mSettings.wholeProgram = true;
1574+
15691575
// Write results in results.xml
15701576
else if (std::strcmp(argv[i], "--xml") == 0) {
15711577
if (outputFormatOptionProvided) {
@@ -1633,12 +1639,15 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
16331639
// TODO: bail out instead?
16341640
if (mSettings.checks.isEnabled(Checks::unusedFunction))
16351641
{
1636-
mLogger.printMessage("unusedFunction check requires --cppcheck-build-dir to be active with -j.");
1642+
mLogger.printMessage("disabling unusedFunction check as it requires --cppcheck-build-dir to be active with -j");
16371643
mSettings.checks.disable(Checks::unusedFunction);
16381644
// TODO: is there some later logic to remove?
16391645
}
1640-
// TODO: enable
1641-
//mLogger.printMessage("whole program analysis requires --cppcheck-build-dir to be active with -j.");
1646+
// TODO: bail out instead?
1647+
if (mSettings.wholeProgram) {
1648+
mLogger.printMessage("disabling whole program analysis as it requires --cppcheck-build-dir to be active with -j.");
1649+
mSettings.wholeProgram = false;
1650+
}
16421651
}
16431652

16441653
if (!mSettings.checks.isEnabled(Checks::unusedFunction))

lib/cppcheck.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1820,7 +1820,7 @@ bool CppCheck::analyseWholeProgram()
18201820
if (!Settings::unusedFunctionOnly()) {
18211821
// Analyse the tokens
18221822
CTU::FileInfo ctu;
1823-
if (mSettings.useSingleJob() || !mSettings.buildDir.empty())
1823+
if (mSettings.wholeProgram)
18241824
{
18251825
for (const Check::FileInfo *fi : mFileInfo) {
18261826
const auto *fi2 = dynamic_cast<const CTU::FileInfo *>(fi);
@@ -1843,6 +1843,10 @@ bool CppCheck::analyseWholeProgram()
18431843

18441844
unsigned int CppCheck::analyseWholeProgram(const std::string &buildDir, const std::list<FileWithDetails> &files, const std::list<FileSettings>& fileSettings, const std::string& ctuInfo)
18451845
{
1846+
// TODO: is this bailout correct? what happened when builddir was empty?
1847+
if (!mSettings.wholeProgram)
1848+
return mLogger->exitcode();
1849+
18461850
if (mSettings.checks.isEnabled(Checks::unusedFunction))
18471851
CheckUnusedFunctions::analyseWholeProgram(mSettings, mErrorLogger, buildDir);
18481852

lib/settings.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,9 @@ class CPPCHECKLIB WARN_UNUSED Settings {
525525
/** @brief Is --verbose given? */
526526
bool verbose{};
527527

528+
/** @brief Is the whole program analysis enabled? */
529+
bool wholeProgram{true};
530+
528531
/** @brief XML version (--xml-version=..) */
529532
int xml_version = 2; // TODO: integrate into outputFormat enum?
530533

test/cli/other_test.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def test_message_j(tmpdir):
143143
with open(test_file, 'wt') as f:
144144
f.write("")
145145

146-
args = ['-j2', test_file]
146+
args = ['-j2', '--no-whole-program', test_file]
147147

148148
_, stdout, _ = cppcheck(args)
149149
assert stdout == "Checking {} ...\n".format(test_file) # we were adding stray \0 characters at the end
@@ -251,7 +251,15 @@ def test_progress_j(tmpdir):
251251
}
252252
""")
253253

254-
args = ['--report-progress=0', '--enable=all', '--inconclusive', '-j2', '--disable=unusedFunction', test_file]
254+
args = [
255+
'--report-progress=0',
256+
'--enable=all',
257+
'--inconclusive',
258+
'-j2',
259+
'--disable=unusedFunction',
260+
'--no-whole-program',
261+
test_file
262+
]
255263

256264
exitcode, stdout, stderr = cppcheck(args)
257265
assert exitcode == 0, stdout if stdout else stderr
@@ -1303,7 +1311,7 @@ def test_markup_j(tmpdir):
13031311
with open(test_file_4, 'wt'):
13041312
pass
13051313

1306-
args = ['--library=qt', '-j2', test_file_1, test_file_2, test_file_3, test_file_4]
1314+
args = ['--library=qt', '-j2', '--no-whole-program', test_file_1, test_file_2, test_file_3, test_file_4]
13071315

13081316
exitcode, stdout, stderr = cppcheck(args)
13091317
assert exitcode == 0, stdout if stdout else stderr
@@ -2539,7 +2547,7 @@ def test_inline_suppr(tmp_path):
25392547

25402548

25412549
def test_inline_suppr_j(tmp_path):
2542-
__test_inline_suppr(tmp_path, ['-j2'])
2550+
__test_inline_suppr(tmp_path, ['-j2', '--no-whole-program'])
25432551

25442552

25452553
def test_inline_suppr_builddir(tmp_path):
@@ -2768,7 +2776,7 @@ def test_addon_suppr_inline(tmp_path):
27682776

27692777
# TODO: remove override when all issues are fixed
27702778
def test_addon_suppr_inline_j(tmp_path):
2771-
__test_addon_suppr(tmp_path, ['--inline-suppr', '-j2'])
2779+
__test_addon_suppr(tmp_path, ['--inline-suppr', '-j2', '--no-whole-program'])
27722780

27732781

27742782
def test_addon_suppr_cli_line(tmp_path):
@@ -4648,6 +4656,7 @@ def test_ipc(tmp_path):
46484656
'-j2',
46494657
'--executor=process',
46504658
'--no-cppcheck-build-dir',
4659+
'--no-whole-program',
46514660
str(test_file)
46524661
]
46534662

@@ -4680,6 +4689,7 @@ def test_ipc_suppressions(tmp_path):
46804689
'-j2',
46814690
'--executor=process',
46824691
'--no-cppcheck-build-dir',
4692+
'--no-whole-program',
46834693
'--suppress=id0:test1.c',
46844694
str(tmp_path)
46854695
]
@@ -4724,6 +4734,7 @@ def test_ipc_inline_suppressions(tmp_path):
47244734
'-j2',
47254735
'--executor=process',
47264736
'--no-cppcheck-build-dir',
4737+
'--no-whole-program',
47274738
'--inline-suppr',
47284739
str(tmp_path)
47294740
]

test/testcmdlineparser.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,8 @@ class TestCmdlineParser : public TestFixture {
499499
TEST_CASE(noSafetyOverride);
500500
TEST_CASE(debugAnalyzerinfo);
501501
TEST_CASE(debugIpc);
502+
TEST_CASE(wholeProgram);
503+
TEST_CASE(noWholeProgram);
502504

503505
TEST_CASE(ignorepaths1);
504506
TEST_CASE(ignorepaths2);
@@ -3495,6 +3497,20 @@ class TestCmdlineParser : public TestFixture {
34953497
ASSERT_EQUALS(true, settings->debugipc);
34963498
}
34973499

3500+
void wholeProgram() {
3501+
REDIRECT;
3502+
const char * const argv[] = {"cppcheck", "--whole-program", "file.cpp"};
3503+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
3504+
ASSERT_EQUALS(true, settings->wholeProgram);
3505+
}
3506+
3507+
void noWholeProgram() {
3508+
REDIRECT;
3509+
const char * const argv[] = {"cppcheck", "--no-whole-program", "file.cpp"};
3510+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
3511+
ASSERT_EQUALS(false, settings->wholeProgram);
3512+
}
3513+
34983514
void ignorepaths1() {
34993515
REDIRECT;
35003516
const char * const argv[] = {"cppcheck", "-isrc", "file.cpp"};

0 commit comments

Comments
 (0)