From c9bc5a076f293ebb1668debd02fc59f573e0ab38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 28 Feb 2021 21:43:51 +0100 Subject: [PATCH] Fixed #10014 (-U switch ignored when using --project=compile_commands.json and --force) --- lib/cppcheck.cpp | 2 +- test/cli/test-more-projects.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 test/cli/test-more-projects.py diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index a80c3a1f9..b229971ca 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -489,7 +489,7 @@ unsigned int CppCheck::check(const ImportProject::FileSettings &fs) else temp.mSettings.userDefines += fs.cppcheckDefines(); temp.mSettings.includePaths = fs.includePaths; - temp.mSettings.userUndefs = fs.undefs; + temp.mSettings.userUndefs.insert(fs.undefs.cbegin(), fs.undefs.cend()); if (fs.standard.find("++") != std::string::npos) temp.mSettings.standards.setCPP(fs.standard); else if (!fs.standard.empty()) diff --git a/test/cli/test-more-projects.py b/test/cli/test-more-projects.py new file mode 100644 index 000000000..48e50793a --- /dev/null +++ b/test/cli/test-more-projects.py @@ -0,0 +1,34 @@ + +# python -m pytest test-helloworld.py + +import os +import tempfile +from testutils import cppcheck + + +def test_project_force_U(): + # 10018 + # -U does not work with compile_commands.json + with tempfile.TemporaryDirectory('10018') as temp_folder: + with open(os.path.join(temp_folder, 'bug1.cpp'), 'wt') as f: + f.write(""" + int x = 123 / 0; + #ifdef MACRO1 + int y = 1000 / 0; + #endif + """) + + compile_commands = os.path.join(temp_folder, 'compile_commands.json') + + with open(compile_commands, 'wt') as f: + f.write('[ { "directory": "%s", "command": "c++ -o bug1.o -c bug1.cpp", "file": "bug1.cpp", "output": "bug1.o" } ]' % str(temp_folder)) + + # Without -U => both bugs are found + ret, stdout, stderr = cppcheck(['--project=' + compile_commands, '--force', '-rp=' + temp_folder, '--template=cppcheck1']) + assert (stderr == '[bug1.cpp:2]: (error) Division by zero.\n' + '[bug1.cpp:4]: (error) Division by zero.\n') + + # With -U => only first bug is found + ret, stdout, stderr = cppcheck(['--project=' + compile_commands, '--force', '-UMACRO1', '-rp=' + temp_folder, '--template=cppcheck1']) + assert stderr == '[bug1.cpp:2]: (error) Division by zero.\n' +