diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index dc8a90063..df9bd3204 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -198,6 +198,10 @@ bool CmdLineParser::fillSettingsFromArgs(int argc, const char* const argv[]) assert(!(!pathnamesRef.empty() && !fileSettingsRef.empty())); if (!fileSettingsRef.empty()) { + // TODO: handle ignored? + + // TODO: de-duplicate + std::list fileSettings; if (!mSettings.fileFilters.empty()) { // filter only for the selected filenames from all project files @@ -244,6 +248,19 @@ bool CmdLineParser::fillSettingsFromArgs(int argc, const char* const argv[]) } } + // de-duplicate files + { + auto it = filesResolved.begin(); + while (it != filesResolved.end()) { + const std::string& name = it->first; + // TODO: log if duplicated files were dropped + filesResolved.erase(std::remove_if(std::next(it), filesResolved.end(), [&](const std::pair& entry) { + return entry.first == name; + }), filesResolved.end()); + ++it; + } + } + std::list> files; if (!mSettings.fileFilters.empty()) { std::copy_if(filesResolved.cbegin(), filesResolved.cend(), std::inserter(files, files.end()), [&](const decltype(filesResolved)::value_type& entry) { diff --git a/test/cli/test-more-projects.py b/test/cli/test-more-projects.py index 3693f930d..d84ff8746 100644 --- a/test/cli/test-more-projects.py +++ b/test/cli/test-more-projects.py @@ -432,3 +432,75 @@ def test_project_file_order(tmpdir): '4/4 files checked 0% done' ] assert stderr == '' + + +def test_project_file_duplicate(tmpdir): + test_file_a = os.path.join(tmpdir, 'a.c') + with open(test_file_a, 'wt'): + pass + + project_file = os.path.join(tmpdir, 'test.cppcheck') + with open(project_file, 'wt') as f: + f.write( + """ + + + + + + +""".format(test_file_a, test_file_a, tmpdir)) + + args = ['--project={}'.format(project_file)] + + exitcode, stdout, stderr = cppcheck(args) + assert exitcode == 0 + lines = stdout.splitlines() + assert lines == [ + 'Checking {} ...'.format(test_file_a) + ] + assert stderr == '' + + +def test_project_file_duplicate_2(tmpdir): + test_file_a = os.path.join(tmpdir, 'a.c') + with open(test_file_a, 'wt'): + pass + test_file_b = os.path.join(tmpdir, 'b.c') + with open(test_file_b, 'wt'): + pass + test_file_c = os.path.join(tmpdir, 'c.c') + with open(test_file_c, 'wt'): + pass + + project_file = os.path.join(tmpdir, 'test.cppcheck') + with open(project_file, 'wt') as f: + f.write( + """ + + + + + + + + + + + +""".format(test_file_c, test_file_a, test_file_b, tmpdir, test_file_b, test_file_c, test_file_a, tmpdir)) + + args = ['--project={}'.format(project_file)] + + exitcode, stdout, stderr = cppcheck(args) + assert exitcode == 0 + lines = stdout.splitlines() + assert lines == [ + 'Checking {} ...'.format(test_file_c), + '1/3 files checked 0% done', + 'Checking {} ...'.format(test_file_a), + '2/3 files checked 0% done', + 'Checking {} ...'.format(test_file_b), + '3/3 files checked 0% done' + ] + assert stderr == '' diff --git a/test/cli/test-other.py b/test/cli/test-other.py index a689bc92c..0ad6573f6 100644 --- a/test/cli/test-other.py +++ b/test/cli/test-other.py @@ -782,3 +782,46 @@ Line 6 0 always 0 '''.format(test_file_cpp, test_file_h_2, test_file_h, test_file_cpp, test_file_h_2, test_file_h, test_file_cpp) assert stderr == '' + + +def test_file_duplicate(tmpdir): + test_file_a = os.path.join(tmpdir, 'a.c') + with open(test_file_a, 'wt'): + pass + + args = [test_file_a, test_file_a, str(tmpdir)] + + exitcode, stdout, stderr = cppcheck(args) + assert exitcode == 0 + lines = stdout.splitlines() + assert lines == [ + 'Checking {} ...'.format(test_file_a) + ] + assert stderr == '' + + +def test_file_duplicate_2(tmpdir): + test_file_a = os.path.join(tmpdir, 'a.c') + with open(test_file_a, 'wt'): + pass + test_file_b = os.path.join(tmpdir, 'b.c') + with open(test_file_b, 'wt'): + pass + test_file_c = os.path.join(tmpdir, 'c.c') + with open(test_file_c, 'wt'): + pass + + args = [test_file_c, test_file_a, test_file_b, str(tmpdir), test_file_b, test_file_c, test_file_a, str(tmpdir)] + + exitcode, stdout, stderr = cppcheck(args) + assert exitcode == 0 + lines = stdout.splitlines() + assert lines == [ + 'Checking {} ...'.format(test_file_c), + '1/3 files checked 0% done', + 'Checking {} ...'.format(test_file_a), + '2/3 files checked 0% done', + 'Checking {} ...'.format(test_file_b), + '3/3 files checked 0% done' + ] + assert stderr == ''