From 92431a865c911a38cbf90f761fc96237c9b38c95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 1 Oct 2024 16:59:19 +0200 Subject: [PATCH] Fix #12928 (GUI: running cppcheck premium with LOC license) (#6854) --- gui/mainwindow.cpp | 24 ++++++++++++++++++++++++ gui/projectfile.cpp | 9 +++++++++ gui/projectfile.h | 10 ++++++++++ gui/projectfile.ui | 26 ++++++++++++++++---------- gui/projectfiledialog.cpp | 25 ++++++++++++++++++++++--- gui/projectfiledialog.h | 11 +++++++---- lib/importproject.h | 1 + 7 files changed, 89 insertions(+), 17 deletions(-) diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 56243334c64..8c34882e09d 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -1837,6 +1837,30 @@ void MainWindow::analyzeProject(const ProjectFile *projectFile, const QStringLis QFileInfo inf(projectFile->getFilename()); const QString& rootpath = projectFile->getRootPath(); + if (isCppcheckPremium() && !projectFile->getLicenseFile().isEmpty()) { + if (rootpath.isEmpty() || rootpath == ".") + QDir::setCurrent(inf.absolutePath()); + else if (QDir(rootpath).isAbsolute()) + QDir::setCurrent(rootpath); + else + QDir::setCurrent(inf.absolutePath() + "/" + rootpath); + + QString licenseFile = projectFile->getLicenseFile(); + if (!QFileInfo(licenseFile).isAbsolute() && !rootpath.isEmpty()) + licenseFile = inf.absolutePath() + "/" + licenseFile; + +#ifdef Q_OS_WIN + const QString premiumaddon = QCoreApplication::applicationDirPath() + "/premiumaddon.exe"; +#else + const QString premiumaddon = QCoreApplication::applicationDirPath() + "/premiumaddon"; +#endif + const std::vector args{"--check-loc-license", licenseFile.toStdString()}; + std::string output; + CheckThread::executeCommand(premiumaddon.toStdString(), args, "", output); + std::ofstream fout(inf.absolutePath().toStdString() + "/cppcheck-premium-loc"); + fout << output; + } + QDir::setCurrent(inf.absolutePath()); mThread->setAddonsAndTools(projectFile->getAddonsAndTools()); diff --git a/gui/projectfile.cpp b/gui/projectfile.cpp index 6d32b9e3d4a..64a3b0d00bf 100644 --- a/gui/projectfile.cpp +++ b/gui/projectfile.cpp @@ -90,6 +90,7 @@ void ProjectFile::clear() mBughunting = false; mCertIntPrecision = 0; mCodingStandards.clear(); + mPremiumLicenseFile.clear(); } bool ProjectFile::read(const QString &filename) @@ -225,6 +226,8 @@ bool ProjectFile::read(const QString &filename) readStringList(mCodingStandards, xmlReader, CppcheckXml::CodingStandardElementName); if (xmlReader.name() == QString(CppcheckXml::CertIntPrecisionElementName)) mCertIntPrecision = readInt(xmlReader, 0); + if (xmlReader.name() == QString(CppcheckXml::LicenseFileElementName)) + mPremiumLicenseFile = readString(xmlReader); if (xmlReader.name() == QString(CppcheckXml::ProjectNameElementName)) mProjectName = readString(xmlReader); @@ -1036,6 +1039,12 @@ bool ProjectFile::write(const QString &filename) xmlWriter.writeEndElement(); } + if (!mPremiumLicenseFile.isEmpty()) { + xmlWriter.writeStartElement(CppcheckXml::LicenseFileElementName); + xmlWriter.writeCharacters(mPremiumLicenseFile); + xmlWriter.writeEndElement(); + } + xmlWriter.writeEndDocument(); file.close(); return true; diff --git a/gui/projectfile.h b/gui/projectfile.h index 656ee9d4269..9647256195a 100644 --- a/gui/projectfile.h +++ b/gui/projectfile.h @@ -396,6 +396,13 @@ class ProjectFile : public QObject { return mCertIntPrecision; } + /** Cppcheck Premium: License file */ + void setLicenseFile(const QString& licenseFile) { + mPremiumLicenseFile = licenseFile; + } + const QString& getLicenseFile() const { + return mPremiumLicenseFile; + } /** * @brief Write project file (to disk). @@ -626,6 +633,9 @@ class ProjectFile : public QObject { */ QStringList mCodingStandards; + /** @brief Cppcheck Premium: license file */ + QString mPremiumLicenseFile; + /** @brief Project name, used when generating compliance report */ QString mProjectName; diff --git a/gui/projectfile.ui b/gui/projectfile.ui index 29b9b46b8e2..8bcd8b4879d 100644 --- a/gui/projectfile.ui +++ b/gui/projectfile.ui @@ -595,17 +595,23 @@ - - - Qt::Vertical - - - - 20 - 73 - + + + Premium License - + + + + + + + + Browse... + + + + + diff --git a/gui/projectfiledialog.cpp b/gui/projectfiledialog.cpp index ee2a630fabf..d56893c0543 100644 --- a/gui/projectfiledialog.cpp +++ b/gui/projectfiledialog.cpp @@ -119,6 +119,8 @@ ProjectFileDialog::ProjectFileDialog(ProjectFile *projectFile, bool premium, QWi setWindowTitle(title); loadSettings(); + mUI->premiumLicense->setVisible(premium); + // Checkboxes for the libraries.. const QString applicationFilePath = QCoreApplication::applicationFilePath(); const QString appPath = QFileInfo(applicationFilePath).canonicalPath(); @@ -245,6 +247,7 @@ ProjectFileDialog::ProjectFileDialog(ProjectFile *projectFile, bool premium, QWi connect(mUI->mListSuppressions, &QListWidget::doubleClicked, this, &ProjectFileDialog::editSuppression); connect(mUI->mBtnBrowseMisraFile, &QPushButton::clicked, this, &ProjectFileDialog::browseMisraFile); connect(mUI->mChkAllVsConfigs, &QCheckBox::clicked, this, &ProjectFileDialog::checkAllVSConfigs); + connect(mUI->mBtnBrowseLicense, &QPushButton::clicked, this, &ProjectFileDialog::browseLicenseFile); loadFromProjectFile(projectFile); } @@ -435,6 +438,7 @@ void ProjectFileDialog::loadFromProjectFile(const ProjectFile *projectFile) mUI->mToolClangTidy->setEnabled(false); } mUI->mEditTags->setText(projectFile->getTags().join(';')); + mUI->mEditLicenseFile->setText(projectFile->getLicenseFile()); updatePathsAndDefines(); } @@ -512,6 +516,8 @@ void ProjectFileDialog::saveToProjectFile(ProjectFile *projectFile) const projectFile->setBughunting(mUI->mBughunting->isChecked()); projectFile->setClangAnalyzer(mUI->mToolClangAnalyzer->isChecked()); projectFile->setClangTidy(mUI->mToolClangTidy->isChecked()); + if (mPremium) + projectFile->setLicenseFile(mUI->mEditLicenseFile->text()); #if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) projectFile->setTags(mUI->mEditTags->text().split(";", Qt::SkipEmptyParts)); #else @@ -529,17 +535,17 @@ void ProjectFileDialog::ok() QString ProjectFileDialog::getExistingDirectory(const QString &caption, bool trailingSlash) { const QFileInfo inf(mProjectFile->getFilename()); - const QString rootpath = inf.absolutePath(); + const QString projectPath = inf.absolutePath(); QString selectedDir = QFileDialog::getExistingDirectory(this, caption, - rootpath); + projectPath); if (selectedDir.isEmpty()) return QString(); // Check if the path is relative to project file's path and if so // make it a relative path instead of absolute path. - const QDir dir(rootpath); + const QDir dir(projectPath); const QString relpath(dir.relativeFilePath(selectedDir)); if (!relpath.startsWith("../..")) selectedDir = relpath; @@ -949,3 +955,16 @@ void ProjectFileDialog::browseMisraFile() updateAddonCheckBox(mUI->mMisraC, nullptr, getDataDir(), ADDON_MISRA); } } + +void ProjectFileDialog::browseLicenseFile() +{ + const QFileInfo inf(mProjectFile->getFilename()); + const QString projectPath = inf.absolutePath(); + + const QString fileName = QFileDialog::getOpenFileName(this, tr("Select license file"), projectPath, tr("License file (%1)").arg("*.lic")); + if (fileName.isEmpty()) + return; + + const QDir dir(projectPath); + mUI->mEditLicenseFile->setText(dir.relativeFilePath(fileName)); +} diff --git a/gui/projectfiledialog.h b/gui/projectfiledialog.h index 77374087957..b90cbf04862 100644 --- a/gui/projectfiledialog.h +++ b/gui/projectfiledialog.h @@ -172,7 +172,7 @@ class ProjectFileDialog : public QDialog { */ void setSuppressions(const QList &suppressions); -protected slots: +private slots: /** ok button pressed, save changes and accept */ void ok(); @@ -278,6 +278,9 @@ protected slots: */ void checkAllVSConfigs(); + /** @brief Browse for Cppcheck Premium license file */ + void browseLicenseFile(); + protected: /** @@ -319,15 +322,15 @@ protected slots: private: static QStringList getProjectConfigs(const QString &fileName); - Ui::ProjectFile *mUI; + Ui::ProjectFile * const mUI; /** * @brief Projectfile path. */ - ProjectFile *mProjectFile; + ProjectFile * const mProjectFile; /** Is this Cppcheck Premium? */ - bool mPremium; + const bool mPremium; QString getExistingDirectory(const QString &caption, bool trailingSlash); diff --git a/lib/importproject.h b/lib/importproject.h index 6ffc6896a9c..8c4c171e38c 100644 --- a/lib/importproject.h +++ b/lib/importproject.h @@ -183,6 +183,7 @@ namespace CppcheckXml { static constexpr char CodingStandardsElementName[] = "coding-standards"; static constexpr char CodingStandardElementName[] = "coding-standard"; static constexpr char CertIntPrecisionElementName[] = "cert-c-int-precision"; + static constexpr char LicenseFileElementName[] = "license-file"; static constexpr char ProjectNameElementName[] = "project-name"; }