Skip to content

Commit 92431a8

Browse files
authored
Fix #12928 (GUI: running cppcheck premium with LOC license) (#6854)
1 parent 51546e8 commit 92431a8

7 files changed

Lines changed: 89 additions & 17 deletions

File tree

gui/mainwindow.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,6 +1837,30 @@ void MainWindow::analyzeProject(const ProjectFile *projectFile, const QStringLis
18371837
QFileInfo inf(projectFile->getFilename());
18381838
const QString& rootpath = projectFile->getRootPath();
18391839

1840+
if (isCppcheckPremium() && !projectFile->getLicenseFile().isEmpty()) {
1841+
if (rootpath.isEmpty() || rootpath == ".")
1842+
QDir::setCurrent(inf.absolutePath());
1843+
else if (QDir(rootpath).isAbsolute())
1844+
QDir::setCurrent(rootpath);
1845+
else
1846+
QDir::setCurrent(inf.absolutePath() + "/" + rootpath);
1847+
1848+
QString licenseFile = projectFile->getLicenseFile();
1849+
if (!QFileInfo(licenseFile).isAbsolute() && !rootpath.isEmpty())
1850+
licenseFile = inf.absolutePath() + "/" + licenseFile;
1851+
1852+
#ifdef Q_OS_WIN
1853+
const QString premiumaddon = QCoreApplication::applicationDirPath() + "/premiumaddon.exe";
1854+
#else
1855+
const QString premiumaddon = QCoreApplication::applicationDirPath() + "/premiumaddon";
1856+
#endif
1857+
const std::vector<std::string> args{"--check-loc-license", licenseFile.toStdString()};
1858+
std::string output;
1859+
CheckThread::executeCommand(premiumaddon.toStdString(), args, "", output);
1860+
std::ofstream fout(inf.absolutePath().toStdString() + "/cppcheck-premium-loc");
1861+
fout << output;
1862+
}
1863+
18401864
QDir::setCurrent(inf.absolutePath());
18411865

18421866
mThread->setAddonsAndTools(projectFile->getAddonsAndTools());

gui/projectfile.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ void ProjectFile::clear()
9090
mBughunting = false;
9191
mCertIntPrecision = 0;
9292
mCodingStandards.clear();
93+
mPremiumLicenseFile.clear();
9394
}
9495

9596
bool ProjectFile::read(const QString &filename)
@@ -225,6 +226,8 @@ bool ProjectFile::read(const QString &filename)
225226
readStringList(mCodingStandards, xmlReader, CppcheckXml::CodingStandardElementName);
226227
if (xmlReader.name() == QString(CppcheckXml::CertIntPrecisionElementName))
227228
mCertIntPrecision = readInt(xmlReader, 0);
229+
if (xmlReader.name() == QString(CppcheckXml::LicenseFileElementName))
230+
mPremiumLicenseFile = readString(xmlReader);
228231
if (xmlReader.name() == QString(CppcheckXml::ProjectNameElementName))
229232
mProjectName = readString(xmlReader);
230233

@@ -1036,6 +1039,12 @@ bool ProjectFile::write(const QString &filename)
10361039
xmlWriter.writeEndElement();
10371040
}
10381041

1042+
if (!mPremiumLicenseFile.isEmpty()) {
1043+
xmlWriter.writeStartElement(CppcheckXml::LicenseFileElementName);
1044+
xmlWriter.writeCharacters(mPremiumLicenseFile);
1045+
xmlWriter.writeEndElement();
1046+
}
1047+
10391048
xmlWriter.writeEndDocument();
10401049
file.close();
10411050
return true;

gui/projectfile.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,13 @@ class ProjectFile : public QObject {
396396
return mCertIntPrecision;
397397
}
398398

399+
/** Cppcheck Premium: License file */
400+
void setLicenseFile(const QString& licenseFile) {
401+
mPremiumLicenseFile = licenseFile;
402+
}
403+
const QString& getLicenseFile() const {
404+
return mPremiumLicenseFile;
405+
}
399406

400407
/**
401408
* @brief Write project file (to disk).
@@ -626,6 +633,9 @@ class ProjectFile : public QObject {
626633
*/
627634
QStringList mCodingStandards;
628635

636+
/** @brief Cppcheck Premium: license file */
637+
QString mPremiumLicenseFile;
638+
629639
/** @brief Project name, used when generating compliance report */
630640
QString mProjectName;
631641

gui/projectfile.ui

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -595,17 +595,23 @@
595595
</widget>
596596
</item>
597597
<item>
598-
<spacer name="verticalSpacer_7">
599-
<property name="orientation">
600-
<enum>Qt::Vertical</enum>
601-
</property>
602-
<property name="sizeHint" stdset="0">
603-
<size>
604-
<width>20</width>
605-
<height>73</height>
606-
</size>
598+
<widget class="QGroupBox" name="premiumLicense">
599+
<property name="title">
600+
<string>Premium License</string>
607601
</property>
608-
</spacer>
602+
<layout class="QHBoxLayout" name="horizontalLayout_12">
603+
<item>
604+
<widget class="QLineEdit" name="mEditLicenseFile"/>
605+
</item>
606+
<item>
607+
<widget class="QPushButton" name="mBtnBrowseLicense">
608+
<property name="text">
609+
<string>Browse...</string>
610+
</property>
611+
</widget>
612+
</item>
613+
</layout>
614+
</widget>
609615
</item>
610616
</layout>
611617
</widget>

gui/projectfiledialog.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ ProjectFileDialog::ProjectFileDialog(ProjectFile *projectFile, bool premium, QWi
119119
setWindowTitle(title);
120120
loadSettings();
121121

122+
mUI->premiumLicense->setVisible(premium);
123+
122124
// Checkboxes for the libraries..
123125
const QString applicationFilePath = QCoreApplication::applicationFilePath();
124126
const QString appPath = QFileInfo(applicationFilePath).canonicalPath();
@@ -245,6 +247,7 @@ ProjectFileDialog::ProjectFileDialog(ProjectFile *projectFile, bool premium, QWi
245247
connect(mUI->mListSuppressions, &QListWidget::doubleClicked, this, &ProjectFileDialog::editSuppression);
246248
connect(mUI->mBtnBrowseMisraFile, &QPushButton::clicked, this, &ProjectFileDialog::browseMisraFile);
247249
connect(mUI->mChkAllVsConfigs, &QCheckBox::clicked, this, &ProjectFileDialog::checkAllVSConfigs);
250+
connect(mUI->mBtnBrowseLicense, &QPushButton::clicked, this, &ProjectFileDialog::browseLicenseFile);
248251
loadFromProjectFile(projectFile);
249252
}
250253

@@ -435,6 +438,7 @@ void ProjectFileDialog::loadFromProjectFile(const ProjectFile *projectFile)
435438
mUI->mToolClangTidy->setEnabled(false);
436439
}
437440
mUI->mEditTags->setText(projectFile->getTags().join(';'));
441+
mUI->mEditLicenseFile->setText(projectFile->getLicenseFile());
438442
updatePathsAndDefines();
439443
}
440444

@@ -512,6 +516,8 @@ void ProjectFileDialog::saveToProjectFile(ProjectFile *projectFile) const
512516
projectFile->setBughunting(mUI->mBughunting->isChecked());
513517
projectFile->setClangAnalyzer(mUI->mToolClangAnalyzer->isChecked());
514518
projectFile->setClangTidy(mUI->mToolClangTidy->isChecked());
519+
if (mPremium)
520+
projectFile->setLicenseFile(mUI->mEditLicenseFile->text());
515521
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
516522
projectFile->setTags(mUI->mEditTags->text().split(";", Qt::SkipEmptyParts));
517523
#else
@@ -529,17 +535,17 @@ void ProjectFileDialog::ok()
529535
QString ProjectFileDialog::getExistingDirectory(const QString &caption, bool trailingSlash)
530536
{
531537
const QFileInfo inf(mProjectFile->getFilename());
532-
const QString rootpath = inf.absolutePath();
538+
const QString projectPath = inf.absolutePath();
533539
QString selectedDir = QFileDialog::getExistingDirectory(this,
534540
caption,
535-
rootpath);
541+
projectPath);
536542

537543
if (selectedDir.isEmpty())
538544
return QString();
539545

540546
// Check if the path is relative to project file's path and if so
541547
// make it a relative path instead of absolute path.
542-
const QDir dir(rootpath);
548+
const QDir dir(projectPath);
543549
const QString relpath(dir.relativeFilePath(selectedDir));
544550
if (!relpath.startsWith("../.."))
545551
selectedDir = relpath;
@@ -949,3 +955,16 @@ void ProjectFileDialog::browseMisraFile()
949955
updateAddonCheckBox(mUI->mMisraC, nullptr, getDataDir(), ADDON_MISRA);
950956
}
951957
}
958+
959+
void ProjectFileDialog::browseLicenseFile()
960+
{
961+
const QFileInfo inf(mProjectFile->getFilename());
962+
const QString projectPath = inf.absolutePath();
963+
964+
const QString fileName = QFileDialog::getOpenFileName(this, tr("Select license file"), projectPath, tr("License file (%1)").arg("*.lic"));
965+
if (fileName.isEmpty())
966+
return;
967+
968+
const QDir dir(projectPath);
969+
mUI->mEditLicenseFile->setText(dir.relativeFilePath(fileName));
970+
}

gui/projectfiledialog.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class ProjectFileDialog : public QDialog {
172172
*/
173173
void setSuppressions(const QList<SuppressionList::Suppression> &suppressions);
174174

175-
protected slots:
175+
private slots:
176176

177177
/** ok button pressed, save changes and accept */
178178
void ok();
@@ -278,6 +278,9 @@ protected slots:
278278
*/
279279
void checkAllVSConfigs();
280280

281+
/** @brief Browse for Cppcheck Premium license file */
282+
void browseLicenseFile();
283+
281284
protected:
282285

283286
/**
@@ -319,15 +322,15 @@ protected slots:
319322
private:
320323
static QStringList getProjectConfigs(const QString &fileName);
321324

322-
Ui::ProjectFile *mUI;
325+
Ui::ProjectFile * const mUI;
323326

324327
/**
325328
* @brief Projectfile path.
326329
*/
327-
ProjectFile *mProjectFile;
330+
ProjectFile * const mProjectFile;
328331

329332
/** Is this Cppcheck Premium? */
330-
bool mPremium;
333+
const bool mPremium;
331334

332335
QString getExistingDirectory(const QString &caption, bool trailingSlash);
333336

lib/importproject.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ namespace CppcheckXml {
183183
static constexpr char CodingStandardsElementName[] = "coding-standards";
184184
static constexpr char CodingStandardElementName[] = "coding-standard";
185185
static constexpr char CertIntPrecisionElementName[] = "cert-c-int-precision";
186+
static constexpr char LicenseFileElementName[] = "license-file";
186187
static constexpr char ProjectNameElementName[] = "project-name";
187188
}
188189

0 commit comments

Comments
 (0)