Skip to content

Commit

Permalink
refs #3450 (CLI --report-progress is currently useless) - added `--re…
Browse files Browse the repository at this point in the history
…port-progress=<val>` to specify interval (#5353)
  • Loading branch information
firewave committed Aug 23, 2023
1 parent 73b9442 commit 0901e49
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 11 deletions.
11 changes: 9 additions & 2 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,14 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])

// Report progress
else if (std::strcmp(argv[i], "--report-progress") == 0) {
mSettings.reportProgress = true;
mSettings.reportProgress = 10;
}

else if (std::strncmp(argv[i], "--report-progress=", 18) == 0) {
int tmp;
if (!parseNumberArg(argv[i], 18, tmp, true))
return false;
mSettings.reportProgress = tmp;
}

#ifdef HAVE_RULES
Expand Down Expand Up @@ -1299,7 +1306,7 @@ void CmdLineParser::printHelp()
" using e.g. ~ for home folder does not work. It is\n"
" currently only possible to apply the base paths to\n"
" files that are on a lower level in the directory tree.\n"
" --report-progress Report progress messages while checking a file.\n"
" --report-progress Report progress messages while checking a file (single job only).\n"
#ifdef HAVE_RULES
" --rule=<rule> Match regular expression.\n"
" --rule-file=<file> Use given rule file. For more information, see:\n"
Expand Down
8 changes: 5 additions & 3 deletions cli/cppcheckexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck)
{
Settings& settings = cppcheck.settings();

if (settings.reportProgress)
if (settings.reportProgress >= 0)
mLatestProgressOutputTime = std::time(nullptr);

if (!settings.outputFile.empty()) {
Expand Down Expand Up @@ -399,16 +399,18 @@ void CppCheckExecutor::reportOut(const std::string &outmsg, Color c)
std::cout << c << ansiToOEM(outmsg, true) << Color::Reset << std::endl;
}

// TODO: remove filename parameter?
void CppCheckExecutor::reportProgress(const std::string &filename, const char stage[], const std::size_t value)
{
(void)filename;

if (!mLatestProgressOutputTime)
return;

// Report progress messages every 10 seconds
// Report progress messages every x seconds
const std::time_t currentTime = std::time(nullptr);
if (currentTime >= (mLatestProgressOutputTime + 10)) {
if (currentTime >= (mLatestProgressOutputTime + mSettings->reportProgress))
{
mLatestProgressOutputTime = currentTime;

// format a progress message
Expand Down
2 changes: 1 addition & 1 deletion lib/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ class CPPCHECKLIB WARN_UNUSED Settings {
bool relativePaths{};

/** @brief --report-progress */
bool reportProgress{};
int reportProgress{-1};

/** Rule */
struct CPPCHECKLIB Rule {
Expand Down
60 changes: 59 additions & 1 deletion test/cli/test-other.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from testutils import cppcheck


def __test_missing_include(tmpdir, use_j):
test_file = os.path.join(tmpdir, 'test.c')
with open(test_file, 'wt') as f:
Expand All @@ -20,12 +21,15 @@ def __test_missing_include(tmpdir, use_j):
_, _, stderr = cppcheck(args)
assert stderr == '{}:2:0: information: Include file: "test.h" not found. [missingInclude]\n'.format(test_file)


def test_missing_include(tmpdir):
__test_missing_include(tmpdir, False)


def test_missing_include_j(tmpdir): #11283
__test_missing_include(tmpdir, True)


def __test_missing_include_check_config(tmpdir, use_j):
test_file = os.path.join(tmpdir, 'test.c')
with open(test_file, 'wt') as f:
Expand All @@ -41,12 +45,15 @@ def __test_missing_include_check_config(tmpdir, use_j):
_, _, stderr = cppcheck(args.split())
assert stderr == '' # --check-config no longer reports the missing includes


def test_missing_include_check_config(tmpdir):
__test_missing_include_check_config(tmpdir, False)


def test_missing_include_check_config_j(tmpdir):
__test_missing_include_check_config(tmpdir, True)


def test_missing_include_inline_suppr(tmpdir):
test_file = os.path.join(tmpdir, 'test.c')
with open(test_file, 'wt') as f:
Expand All @@ -62,6 +69,7 @@ def test_missing_include_inline_suppr(tmpdir):
_, _, stderr = cppcheck(args)
assert stderr == ''


def test_invalid_library(tmpdir):
args = ['--library=none', '--library=posix', '--library=none2', '--platform=native', 'file.c']

Expand All @@ -82,4 +90,54 @@ def test_message_j(tmpdir):
_, stdout, _ = cppcheck(args)
assert stdout == "Checking {} ...\n".format(test_file) # we were adding stray \0 characters at the end

# TODO: test missing std.cfg
# TODO: test missing std.cfg


def test_progress(tmpdir):
test_file = os.path.join(tmpdir, 'test.c')
with open(test_file, 'wt') as f:
f.write("""
int main(int argc)
{
}
""")

args = ['--report-progress=0', '--enable=all', '--inconclusive', '--platform=native', test_file]

exitcode, stdout, stderr = cppcheck(args)
assert exitcode == 0
pos = stdout.find('\n')
assert(pos != -1)
pos += 1
assert stdout[:pos] == "Checking {} ...\n".format(test_file)
assert (stdout[pos:] ==
"progress: Tokenize (typedef) 0%\n"
"progress: Tokenize (typedef) 12%\n"
"progress: Tokenize (typedef) 25%\n"
"progress: Tokenize (typedef) 37%\n"
"progress: Tokenize (typedef) 50%\n"
"progress: Tokenize (typedef) 62%\n"
"progress: Tokenize (typedef) 75%\n"
"progress: Tokenize (typedef) 87%\n"
"progress: SymbolDatabase 0%\n"
"progress: SymbolDatabase 12%\n"
"progress: SymbolDatabase 87%\n"
)
assert stderr == ""


def test_progress_j(tmpdir):
test_file = os.path.join(tmpdir, 'test.c')
with open(test_file, 'wt') as f:
f.write("""
int main(int argc)
{
}
""")

args = ['--report-progress=0', '--enable=all', '--inconclusive', '-j2', '--disable=unusedFunction', '--platform=native', test_file]

exitcode, stdout, stderr = cppcheck(args)
assert exitcode == 0
assert stdout == "Checking {} ...\n".format(test_file)
assert stderr == ""
46 changes: 42 additions & 4 deletions test/testcmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@ class TestCmdlineParser : public TestFixture {
TEST_CASE(maxConfigsMissingCount);
TEST_CASE(maxConfigsInvalid);
TEST_CASE(maxConfigsTooSmall);
TEST_CASE(reportProgressTest); // "Test" suffix to avoid hiding the parent's reportProgress
TEST_CASE(reportProgress1);
TEST_CASE(reportProgress2);
TEST_CASE(reportProgress3);
TEST_CASE(reportProgress4);
TEST_CASE(reportProgress5);
TEST_CASE(stdc99);
TEST_CASE(stdcpp11);
TEST_CASE(stdunknown1);
Expand Down Expand Up @@ -1060,12 +1064,46 @@ class TestCmdlineParser : public TestFixture {
ASSERT_EQUALS("cppcheck: error: argument to '--max-configs=' must be greater than 0.\n", GET_REDIRECT_OUTPUT);
}

void reportProgressTest() {
void reportProgress1() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--report-progress", "file.cpp"};
settings->reportProgress = false;
settings->reportProgress = -1;
ASSERT(parser->parseFromArgs(3, argv));
ASSERT(settings->reportProgress);
ASSERT_EQUALS(10, settings->reportProgress);
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
}

void reportProgress2() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--report-progress=", "file.cpp"};
settings->reportProgress = -1;
ASSERT(!parser->parseFromArgs(3, argv));
ASSERT_EQUALS("cppcheck: error: argument to '--report-progress=' is not valid - not an integer.\n", GET_REDIRECT_OUTPUT);
}

void reportProgress3() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--report-progress=-1", "file.cpp"};
settings->reportProgress = -1;
ASSERT(!parser->parseFromArgs(3, argv));
ASSERT_EQUALS("cppcheck: error: argument to '--report-progress=' needs to be a positive integer.\n", GET_REDIRECT_OUTPUT);
}

void reportProgress4() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--report-progress=0", "file.cpp"};
settings->reportProgress = -1;
ASSERT(parser->parseFromArgs(3, argv));
ASSERT_EQUALS(0, settings->reportProgress);
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
}

void reportProgress5() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--report-progress=1", "file.cpp"};
settings->reportProgress = -1;
ASSERT(parser->parseFromArgs(3, argv));
ASSERT_EQUALS(1, settings->reportProgress);
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
}

Expand Down

0 comments on commit 0901e49

Please sign in to comment.