From 886e30622d95dc63e1d9852ba3739a8a450d7aaa Mon Sep 17 00:00:00 2001 From: olabetskyi <153490942+olabetskyi@users.noreply.github.com> Date: Fri, 31 May 2024 13:09:42 +0300 Subject: [PATCH] Fix #12798: Add inline supression to projectDialog (#6453) --- gui/mainwindow.cpp | 5 ++++- gui/projectfile.cpp | 8 ++++++++ gui/projectfile.h | 13 +++++++++++++ gui/projectfile.ui | 7 +++++++ gui/projectfiledialog.cpp | 2 ++ gui/test/projectfile/testprojectfile.cpp | 15 +++++++++++++++ gui/test/projectfile/testprojectfile.h | 3 +++ lib/importproject.cpp | 3 +++ lib/importproject.h | 1 + test/testimportproject.cpp | 2 ++ 10 files changed, 58 insertions(+), 1 deletion(-) diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 637e49263b1..270082d314f 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -1018,6 +1018,8 @@ QPair MainWindow::getCppcheckSettings() QStringList dirs = mProjectFile->getIncludeDirs(); addIncludeDirs(dirs, result); + result.inlineSuppressions = mProjectFile->getInlineSuppression(); + const QStringList defines = mProjectFile->getDefines(); for (const QString& define : defines) { if (!result.userDefines.empty()) @@ -1111,6 +1113,8 @@ QPair MainWindow::getCppcheckSettings() result.setMisraRuleTexts(CheckThread::executeCommand); } } + else + result.inlineSuppressions = mSettings->value(SETTINGS_INLINE_SUPPRESSIONS, false).toBool(); // Include directories (and files) are searched in listed order. // Global include directories must be added AFTER the per project include @@ -1135,7 +1139,6 @@ QPair MainWindow::getCppcheckSettings() result.force = mSettings->value(SETTINGS_CHECK_FORCE, 1).toBool(); result.xml = false; result.jobs = mSettings->value(SETTINGS_CHECK_THREADS, 1).toInt(); - result.inlineSuppressions = mSettings->value(SETTINGS_INLINE_SUPPRESSIONS, false).toBool(); result.certainty.setEnabled(Certainty::inconclusive, mSettings->value(SETTINGS_INCONCLUSIVE_ERRORS, false).toBool()); if (!mProjectFile || result.platform.type == Platform::Type::Unspecified) result.platform.set((Platform::Type) mSettings->value(SETTINGS_CHECKED_PLATFORM, 0).toInt()); diff --git a/gui/projectfile.cpp b/gui/projectfile.cpp index b15404368b8..6d32b9e3d4a 100644 --- a/gui/projectfile.cpp +++ b/gui/projectfile.cpp @@ -77,6 +77,7 @@ void ProjectFile::clear() mAnalyzeAllVsConfigs = false; mCheckHeaders = true; mCheckUnusedTemplates = true; + mInlineSuppression = true; mMaxCtuDepth = settings.maxCtuDepth; mMaxTemplateRecursion = settings.maxTemplateRecursion; mCheckUnknownFunctionReturn.clear(); @@ -143,6 +144,9 @@ bool ProjectFile::read(const QString &filename) if (xmlReader.name() == QString(CppcheckXml::CheckUnusedTemplatesElementName)) mCheckUnusedTemplates = readBool(xmlReader); + if (xmlReader.name() == QString(CppcheckXml::InlineSuppression)) + mInlineSuppression = readBool(xmlReader); + if (xmlReader.name() == QString(CppcheckXml::CheckLevelExhaustiveElementName)) mCheckLevel = CheckLevel::exhaustive; @@ -873,6 +877,10 @@ bool ProjectFile::write(const QString &filename) xmlWriter.writeCharacters(bool_to_string(mCheckUnusedTemplates)); xmlWriter.writeEndElement(); + xmlWriter.writeStartElement(CppcheckXml::InlineSuppression); + xmlWriter.writeCharacters(bool_to_string(mInlineSuppression)); + xmlWriter.writeEndElement(); + xmlWriter.writeStartElement(CppcheckXml::MaxCtuDepthElementName); xmlWriter.writeCharacters(QString::number(mMaxCtuDepth)); xmlWriter.writeEndElement(); diff --git a/gui/projectfile.h b/gui/projectfile.h index c86a68a0127..6da0e491196 100644 --- a/gui/projectfile.h +++ b/gui/projectfile.h @@ -107,6 +107,14 @@ class ProjectFile : public QObject { mCheckUnusedTemplates = b; } + bool getInlineSuppression() const { + return mInlineSuppression; + } + + void setInlineSuppression(bool b) { + mInlineSuppression = b; + } + /** * @brief Get list of include directories. * @return list of directories. @@ -557,6 +565,11 @@ class ProjectFile : public QObject { /** Check code in unused templates */ bool mCheckUnusedTemplates; + /** + * @brief Enable inline suppression. + */ + bool mInlineSuppression; + /** * @brief List of include directories used to search include files. */ diff --git a/gui/projectfile.ui b/gui/projectfile.ui index 7bc0ef3a9ae..29b9b46b8e2 100644 --- a/gui/projectfile.ui +++ b/gui/projectfile.ui @@ -746,6 +746,13 @@ + + + + Enable inline suppressions + + + diff --git a/gui/projectfiledialog.cpp b/gui/projectfiledialog.cpp index b198fb980a5..0cad08a2151 100644 --- a/gui/projectfiledialog.cpp +++ b/gui/projectfiledialog.cpp @@ -312,6 +312,7 @@ void ProjectFileDialog::loadFromProjectFile(const ProjectFile *projectFile) mUI->mCheckLevelNormal->setChecked(true); mUI->mCheckHeaders->setChecked(projectFile->getCheckHeaders()); mUI->mCheckUnusedTemplates->setChecked(projectFile->getCheckUnusedTemplates()); + mUI->mInlineSuppressions->setChecked(projectFile->getInlineSuppression()); mUI->mMaxCtuDepth->setValue(projectFile->getMaxCtuDepth()); mUI->mMaxTemplateRecursion->setValue(projectFile->getMaxTemplateRecursion()); if (projectFile->clangParser) @@ -435,6 +436,7 @@ void ProjectFileDialog::saveToProjectFile(ProjectFile *projectFile) const projectFile->setVSConfigurations(getProjectConfigurations()); projectFile->setCheckHeaders(mUI->mCheckHeaders->isChecked()); projectFile->setCheckUnusedTemplates(mUI->mCheckUnusedTemplates->isChecked()); + projectFile->setInlineSuppression(mUI->mInlineSuppressions->isChecked()); projectFile->setMaxCtuDepth(mUI->mMaxCtuDepth->value()); projectFile->setMaxTemplateRecursion(mUI->mMaxTemplateRecursion->value()); projectFile->setIncludes(getIncludePaths()); diff --git a/gui/test/projectfile/testprojectfile.cpp b/gui/test/projectfile/testprojectfile.cpp index 5c69917e8b2..7e301dbc48a 100644 --- a/gui/test/projectfile/testprojectfile.cpp +++ b/gui/test/projectfile/testprojectfile.cpp @@ -139,6 +139,21 @@ void TestProjectFile::getAddonFilePath() const QCOMPARE(ProjectFile::getAddonFilePath(tempdir.path(), filepath), filepath); } +void TestProjectFile::getInlineSuppressionDefaultValue() const +{ + ProjectFile projectFile; + projectFile.setFilename("/some/path/123.cppcheck"); + QCOMPARE(projectFile.getInlineSuppression(), true); +} + +void TestProjectFile::getInlineSuppression() const +{ + ProjectFile projectFile; + projectFile.setFilename("/some/path/123.cppcheck"); + projectFile.setInlineSuppression(false); + QCOMPARE(projectFile.getInlineSuppression(), false); +} + void TestProjectFile::getCheckingSuppressionsRelative() const { const SuppressionList::Suppression suppression("*", "externals/*"); diff --git a/gui/test/projectfile/testprojectfile.h b/gui/test/projectfile/testprojectfile.h index 7da5900238d..805e06cf9b5 100644 --- a/gui/test/projectfile/testprojectfile.h +++ b/gui/test/projectfile/testprojectfile.h @@ -30,6 +30,9 @@ private slots: void getAddonFilePath() const; + void getInlineSuppressionDefaultValue() const; + void getInlineSuppression() const; + void getCheckingSuppressionsRelative() const; void getCheckingSuppressionsAbsolute() const; void getCheckingSuppressionsStar() const; diff --git a/lib/importproject.cpp b/lib/importproject.cpp index 42c21613c57..e6453f4ddd0 100644 --- a/lib/importproject.cpp +++ b/lib/importproject.cpp @@ -1221,6 +1221,8 @@ bool ImportProject::importCppcheckGuiProject(std::istream &istr, Settings *setti checkLevelExhaustive = true; else if (strcmp(node->Name(), CppcheckXml::CheckUnusedTemplatesElementName) == 0) temp.checkUnusedTemplates = (strcmp(readSafe(node->GetText(), ""), "true") == 0); + else if (strcmp(node->Name(), CppcheckXml::InlineSuppression) == 0) + temp.inlineSuppressions = (strcmp(readSafe(node->GetText(), ""), "true") == 0); else if (strcmp(node->Name(), CppcheckXml::MaxCtuDepthElementName) == 0) temp.maxCtuDepth = strToInt(readSafe(node->GetText(), "2")); // TODO: bail out when missing? else if (strcmp(node->Name(), CppcheckXml::MaxTemplateRecursionElementName) == 0) @@ -1284,6 +1286,7 @@ bool ImportProject::importCppcheckGuiProject(std::istream &istr, Settings *setti settings->checkUnusedTemplates = temp.checkUnusedTemplates; settings->maxCtuDepth = temp.maxCtuDepth; settings->maxTemplateRecursion = temp.maxTemplateRecursion; + settings->inlineSuppressions |= temp.inlineSuppressions; settings->safeChecks = temp.safeChecks; if (checkLevelExhaustive) diff --git a/lib/importproject.h b/lib/importproject.h index 54a8f6f0cd2..8db25b96ba7 100644 --- a/lib/importproject.h +++ b/lib/importproject.h @@ -163,6 +163,7 @@ namespace CppcheckXml { static constexpr char MaxCtuDepthElementName[] = "max-ctu-depth"; static constexpr char MaxTemplateRecursionElementName[] = "max-template-recursion"; static constexpr char CheckUnknownFunctionReturn[] = "check-unknown-function-return-values"; + static constexpr char InlineSuppression[] = "inline-suppression"; static constexpr char ClangTidy[] = "clang-tidy"; static constexpr char Name[] = "name"; static constexpr char VSConfigurationElementName[] = "vs-configurations"; diff --git a/test/testimportproject.cpp b/test/testimportproject.cpp index 8486a1351d0..6b351990ffd 100644 --- a/test/testimportproject.cpp +++ b/test/testimportproject.cpp @@ -358,6 +358,7 @@ class TestImportProject : public TestFixture { " \n" " \n" " \n" + " true\n" " test test\n" "\n"; std::istringstream istr(xml); @@ -368,6 +369,7 @@ class TestImportProject : public TestFixture { ASSERT_EQUALS("cli/", project.guiProject.pathNames[0]); ASSERT_EQUALS(1, s.includePaths.size()); ASSERT_EQUALS("lib/", s.includePaths.front()); + ASSERT_EQUALS(true, s.inlineSuppressions); } void ignorePaths() const {