Skip to content

Commit

Permalink
Fix #12928 (GUI: running cppcheck premium with LOC license) (#6854)
Browse files Browse the repository at this point in the history
  • Loading branch information
danmar authored Oct 1, 2024
1 parent 51546e8 commit 92431a8
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 17 deletions.
24 changes: 24 additions & 0 deletions gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> 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());
Expand Down
9 changes: 9 additions & 0 deletions gui/projectfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ void ProjectFile::clear()
mBughunting = false;
mCertIntPrecision = 0;
mCodingStandards.clear();
mPremiumLicenseFile.clear();
}

bool ProjectFile::read(const QString &filename)
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions gui/projectfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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;

Expand Down
26 changes: 16 additions & 10 deletions gui/projectfile.ui
Original file line number Diff line number Diff line change
Expand Up @@ -595,17 +595,23 @@
</widget>
</item>
<item>
<spacer name="verticalSpacer_7">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>73</height>
</size>
<widget class="QGroupBox" name="premiumLicense">
<property name="title">
<string>Premium License</string>
</property>
</spacer>
<layout class="QHBoxLayout" name="horizontalLayout_12">
<item>
<widget class="QLineEdit" name="mEditLicenseFile"/>
</item>
<item>
<widget class="QPushButton" name="mBtnBrowseLicense">
<property name="text">
<string>Browse...</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
Expand Down
25 changes: 22 additions & 3 deletions gui/projectfiledialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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));
}
11 changes: 7 additions & 4 deletions gui/projectfiledialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class ProjectFileDialog : public QDialog {
*/
void setSuppressions(const QList<SuppressionList::Suppression> &suppressions);

protected slots:
private slots:

/** ok button pressed, save changes and accept */
void ok();
Expand Down Expand Up @@ -278,6 +278,9 @@ protected slots:
*/
void checkAllVSConfigs();

/** @brief Browse for Cppcheck Premium license file */
void browseLicenseFile();

protected:

/**
Expand Down Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions lib/importproject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

Expand Down

0 comments on commit 92431a8

Please sign in to comment.