From 81fff2edf12b987ae3d3b1849e7ef79c361d9753 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 9 Nov 2019 17:51:16 +0100 Subject: [PATCH] Fixed #9464 (Import compile database; only check given configuration) --- cli/cmdlineparser.cpp | 4 ++++ gui/mainwindow.cpp | 4 ++++ lib/cppcheck.cpp | 2 +- lib/settings.cpp | 1 + lib/settings.h | 3 +++ test/cli/helloworld/main.c | 3 +++ test/cli/proj2/a/a.c | 3 +++ test/cli/test-helloworld.py | 7 ------- test/cli/test-proj2.py | 18 ++++++++++++++++++ 9 files changed, 37 insertions(+), 8 deletions(-) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index 34420fc69..ac71b28d5 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -547,6 +547,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[]) // --project else if (std::strncmp(argv[i], "--project=", 10) == 0) { + mSettings->checkAllConfigurations = false; // Can be overriden with --max-configs or --force const std::string projectFile = argv[i]+10; ImportProject::Type projType = mSettings->project.import(projectFile, mSettings); if (projType == ImportProject::Type::CPPCHECK_GUI) { @@ -899,6 +900,9 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[]) mSettings->project.ignorePaths(mIgnoredPaths); + if (mSettings->force || maxconfigs) + mSettings->checkAllConfigurations = true; + if (mSettings->force) mSettings->maxConfigs = ~0U; diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 6f2644cc8..5259a20f0 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -857,6 +857,10 @@ Settings MainWindow::getCppcheckSettings() if (!defines.isEmpty()) result.maxConfigs = 1; + // If importing a project, only check the given configuration + if (!mProjectFile->getImportProject().isEmpty()) + result.checkAllConfigurations = false; + const QString &buildDir = mProjectFile->getBuildDir(); if (!buildDir.isEmpty()) { if (QDir(buildDir).isAbsolute()) { diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 790aa159d..7d4bce36e 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -386,7 +386,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string preprocessor.setPlatformInfo(&tokens1); // Get configurations.. - if (mSettings.userDefines.empty() || mSettings.force) { + if ((mSettings.checkAllConfigurations && mSettings.userDefines.empty()) || mSettings.force) { Timer t("Preprocessor::getConfigs", mSettings.showtime, &S_timerResults); configurations = preprocessor.getConfigs(tokens1); } else { diff --git a/lib/settings.cpp b/lib/settings.cpp index 443b5fdbd..cb965ac2b 100644 --- a/lib/settings.cpp +++ b/lib/settings.cpp @@ -52,6 +52,7 @@ Settings::Settings() jointSuppressionReport(false), loadAverage(0), maxConfigs(12), + checkAllConfigurations(true), maxCtuDepth(2), preprocessOnly(false), quiet(false), diff --git a/lib/settings.h b/lib/settings.h index c2bc8da46..feedf3f68 100644 --- a/lib/settings.h +++ b/lib/settings.h @@ -220,6 +220,9 @@ public: Default is 12. (--max-configs=N) */ unsigned int maxConfigs; + /** @brief --check all configurations */ + bool checkAllConfigurations; + /** @brief --max-ctu-depth */ int maxCtuDepth; diff --git a/test/cli/helloworld/main.c b/test/cli/helloworld/main.c index f2518a3ef..530016101 100644 --- a/test/cli/helloworld/main.c +++ b/test/cli/helloworld/main.c @@ -6,3 +6,6 @@ int main() { return 0; } +#ifdef SOME_CONFIG +void foo(); +#endif diff --git a/test/cli/proj2/a/a.c b/test/cli/proj2/a/a.c index a87462ab5..2959aad8c 100644 --- a/test/cli/proj2/a/a.c +++ b/test/cli/proj2/a/a.c @@ -1,2 +1,5 @@ x = 3 / 0; +#ifdef AAA +void aa; +#endif diff --git a/test/cli/test-helloworld.py b/test/cli/test-helloworld.py index f6a767bc3..f3fbaa47d 100644 --- a/test/cli/test-helloworld.py +++ b/test/cli/test-helloworld.py @@ -38,14 +38,12 @@ def test_relative_path(): ret, stdout, stderr = cppcheck(['--template=cppcheck1', 'helloworld']) filename = os.path.join('helloworld', 'main.c') assert ret == 0 - assert stdout == 'Checking %s ...\n' % (filename) assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename) def test_local_path(): ret, stdout, stderr = cppcheck_local(['--template=cppcheck1', '.']) assert ret == 0 - assert stdout == 'Checking main.c ...\n' assert stderr == '[main.c:5]: (error) Division by zero.\n' def test_absolute_path(): @@ -53,13 +51,11 @@ def test_absolute_path(): ret, stdout, stderr = cppcheck(['--template=cppcheck1', prjpath]) filename = os.path.join(prjpath, 'main.c') assert ret == 0 - assert stdout == 'Checking %s ...\n' % (filename) assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename) def test_addon_local_path(): ret, stdout, stderr = cppcheck_local(['--addon=misra', '--template=cppcheck1', '.']) assert ret == 0 - assert stdout == 'Checking main.c ...\n' assert stderr == ('[main.c:5]: (error) Division by zero.\n' '[main.c:1]: (style) misra violation (use --rule-texts= to get proper output)\n') @@ -68,7 +64,6 @@ def test_addon_absolute_path(): ret, stdout, stderr = cppcheck(['--addon=misra', '--template=cppcheck1', prjpath]) filename = os.path.join(prjpath, 'main.c') assert ret == 0 - assert stdout == 'Checking %s ...\n' % (filename) assert stderr == ('[%s:5]: (error) Division by zero.\n' '[%s:1]: (style) misra violation (use --rule-texts= to get proper output)\n' % (filename, filename)) @@ -96,7 +91,6 @@ def test_basepath_relative_path(): ret, stdout, stderr = cppcheck([prjpath, '--template=cppcheck1', '-rp=' + prjpath]) filename = os.path.join(prjpath, 'main.c') assert ret == 0 - assert stdout == 'Checking %s ...\n' % (filename) assert stderr == '[main.c:5]: (error) Division by zero.\n' def test_basepath_absolute_path(): @@ -104,7 +98,6 @@ def test_basepath_absolute_path(): ret, stdout, stderr = cppcheck(['--template=cppcheck1', prjpath, '-rp=' + prjpath]) filename = os.path.join(prjpath, 'main.c') assert ret == 0 - assert stdout == 'Checking %s ...\n' % (filename) assert stderr == '[main.c:5]: (error) Division by zero.\n' def test_vs_project_local_path(): diff --git a/test/cli/test-proj2.py b/test/cli/test-proj2.py index 6235ddd05..953934d4f 100644 --- a/test/cli/test-proj2.py +++ b/test/cli/test-proj2.py @@ -40,6 +40,24 @@ def test_local_path(): assert stdout.find('Checking %s ...' % (file1)) >= 0 assert stdout.find('Checking %s ...' % (file2)) >= 0 +def test_local_path_force(): + create_compile_commands() + ret, stdout, stderr = cppcheck_local(['--project=compile_commands.json', '--force']) + cwd = os.getcwd() + file1 = os.path.join(cwd, 'proj2', 'a', 'a.c') + file2 = os.path.join(cwd, 'proj2', 'b', 'b.c') + assert ret == 0 + assert stdout.find('AAA') >= 0 + +def test_local_path_maxconfigs(): + create_compile_commands() + ret, stdout, stderr = cppcheck_local(['--project=compile_commands.json', '--max-configs=2']) + cwd = os.getcwd() + file1 = os.path.join(cwd, 'proj2', 'a', 'a.c') + file2 = os.path.join(cwd, 'proj2', 'b', 'b.c') + assert ret == 0 + assert stdout.find('AAA') >= 0 + def test_relative_path(): create_compile_commands() ret, stdout, stderr = cppcheck(['--project=' + COMPILE_COMMANDS_JSON])