Skip to content

Commit 9800e82

Browse files
committed
GUI: Allow that platform is selected in project dialog
1 parent af4181f commit 9800e82

6 files changed

Lines changed: 349 additions & 184 deletions

File tree

gui/mainwindow.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,21 @@ Settings MainWindow::getCppcheckSettings()
850850
result.buildDir = (prjpath + '/' + buildDir).toStdString();
851851
}
852852
}
853+
854+
const QString platform = mProjectFile->getPlatform();
855+
if (platform.endsWith(".xml")) {
856+
const QString applicationFilePath = QCoreApplication::applicationFilePath();
857+
const QString appPath = QFileInfo(applicationFilePath).canonicalPath();
858+
result.loadPlatformFile(appPath.toStdString().c_str(), platform.toStdString());
859+
} else {
860+
for (int i = cppcheck::Platform::Native; i <= cppcheck::Platform::Unix64; i++) {
861+
const cppcheck::Platform::PlatformType p = (cppcheck::Platform::PlatformType)i;
862+
if (platform == cppcheck::Platform::platformString(p)) {
863+
result.platform(p);
864+
break;
865+
}
866+
}
867+
}
853868
}
854869

855870
// Include directories (and files) are searched in listed order.
@@ -878,7 +893,8 @@ Settings MainWindow::getCppcheckSettings()
878893
result.jobs = mSettings->value(SETTINGS_CHECK_THREADS, 1).toInt();
879894
result.inlineSuppressions = mSettings->value(SETTINGS_INLINE_SUPPRESSIONS, false).toBool();
880895
result.inconclusive = mSettings->value(SETTINGS_INCONCLUSIVE_ERRORS, false).toBool();
881-
result.platformType = (Settings::PlatformType) mSettings->value(SETTINGS_CHECKED_PLATFORM, 0).toInt();
896+
if (result.platformType == cppcheck::Platform::Unspecified)
897+
result.platform((cppcheck::Platform::PlatformType) mSettings->value(SETTINGS_CHECKED_PLATFORM, 0).toInt());
882898
if (mSettings->value(SETTINGS_STD_CPP03, false).toBool())
883899
result.standards.cpp = Standards::CPP03;
884900
else if (mSettings->value(SETTINGS_STD_CPP11, false).toBool())
@@ -894,7 +910,7 @@ Settings MainWindow::getCppcheckSettings()
894910
if (result.standards.posix)
895911
posix = tryLoadLibrary(&result.library, "posix.cfg");
896912
bool windows = true;
897-
if (result.platformType == Settings::Win32A || result.platformType == Settings::Win32W || result.platformType == Settings::Win64)
913+
if (result.isWindowsPlatform())
898914
windows = tryLoadLibrary(&result.library, "windows.cfg");
899915

900916
if (!std || !posix || !windows)

gui/projectfile.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ static const char ExcludePathName[] = "path";
5050
static const char ExcludePathNameAttrib[] = "name";
5151
static const char LibrariesElementName[] = "libraries";
5252
static const char LibraryElementName[] = "library";
53+
static const char PlatformElementName[] = "platform";
5354
static const char SuppressionsElementName[] = "suppressions";
5455
static const char SuppressionElementName[] = "suppression";
5556
static const char AddonElementName[] = "addon";
@@ -84,6 +85,7 @@ void ProjectFile::clear()
8485
mPaths.clear();
8586
mExcludedPaths.clear();
8687
mLibraries.clear();
88+
mPlatform.clear();
8789
mSuppressions.clear();
8890
mAddons.clear();
8991
mClangAnalyzer = mClangTidy = false;
@@ -149,6 +151,9 @@ bool ProjectFile::read(const QString &filename)
149151
if (insideProject && xmlReader.name() == LibrariesElementName)
150152
readStringList(mLibraries, xmlReader,LibraryElementName);
151153

154+
if (insideProject && xmlReader.name() == PlatformElementName)
155+
readPlatform(xmlReader);
156+
152157
// Find suppressions list from inside project element
153158
if (insideProject && xmlReader.name() == SuppressionsElementName)
154159
readStringList(mSuppressions, xmlReader,SuppressionElementName);
@@ -434,6 +439,30 @@ void ProjectFile::readExcludes(QXmlStreamReader &reader)
434439
} while (!allRead);
435440
}
436441

442+
void ProjectFile::readPlatform(QXmlStreamReader &reader)
443+
{
444+
do {
445+
const QXmlStreamReader::TokenType type = reader.readNext();
446+
switch (type) {
447+
case QXmlStreamReader::Characters:
448+
mPlatform = reader.text().toString();
449+
case QXmlStreamReader::EndElement:
450+
return;
451+
// Not handled
452+
case QXmlStreamReader::StartElement:
453+
case QXmlStreamReader::NoToken:
454+
case QXmlStreamReader::Invalid:
455+
case QXmlStreamReader::StartDocument:
456+
case QXmlStreamReader::EndDocument:
457+
case QXmlStreamReader::Comment:
458+
case QXmlStreamReader::DTD:
459+
case QXmlStreamReader::EntityReference:
460+
case QXmlStreamReader::ProcessingInstruction:
461+
break;
462+
}
463+
} while (1);
464+
}
465+
437466

438467
void ProjectFile::readStringList(QStringList &stringlist, QXmlStreamReader &reader, const char elementname[])
439468
{
@@ -498,6 +527,11 @@ void ProjectFile::setLibraries(const QStringList &libraries)
498527
mLibraries = libraries;
499528
}
500529

530+
void ProjectFile::setPlatform(const QString &platform)
531+
{
532+
mPlatform = platform;
533+
}
534+
501535
void ProjectFile::setSuppressions(const QStringList &suppressions)
502536
{
503537
mSuppressions = suppressions;
@@ -535,6 +569,12 @@ bool ProjectFile::write(const QString &filename)
535569
xmlWriter.writeEndElement();
536570
}
537571

572+
if (!mPlatform.isEmpty()) {
573+
xmlWriter.writeStartElement(PlatformElementName);
574+
xmlWriter.writeCharacters(mPlatform);
575+
xmlWriter.writeEndElement();
576+
}
577+
538578
if (!mImportProject.isEmpty()) {
539579
xmlWriter.writeStartElement(ImportProjectElementName);
540580
xmlWriter.writeCharacters(mImportProject);

gui/projectfile.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@ class ProjectFile : public QObject {
106106
return mLibraries;
107107
}
108108

109+
/**
110+
* @brief Get platform.
111+
* @return Current platform. If it ends with .xml then it is a file. Otherwise it must match one of the return values from @sa cppcheck::Platform::platformString() ("win32A", "unix32", ..)
112+
*/
113+
QString getPlatform() const {
114+
return mPlatform;
115+
}
116+
109117
/**
110118
* @brief Get list suppressions.
111119
* @return list of suppressions.
@@ -206,6 +214,12 @@ class ProjectFile : public QObject {
206214
*/
207215
void setLibraries(const QStringList &libraries);
208216

217+
/**
218+
* @brief Set platform.
219+
* @param platform platform.
220+
*/
221+
void setPlatform(const QString &platform);
222+
209223
/**
210224
* @brief Set list of suppressions.
211225
* @param suppressions List of suppressions.
@@ -282,6 +296,12 @@ class ProjectFile : public QObject {
282296
*/
283297
void readExcludes(QXmlStreamReader &reader);
284298

299+
/**
300+
* @brief Read platform text.
301+
* @param reader XML stream reader.
302+
*/
303+
void readPlatform(QXmlStreamReader &reader);
304+
285305
/**
286306
* @brief Read string list
287307
* @param stringlist destination string list
@@ -359,6 +379,11 @@ class ProjectFile : public QObject {
359379
*/
360380
QStringList mLibraries;
361381

382+
/**
383+
* @brief Platform
384+
*/
385+
QString mPlatform;
386+
362387
/**
363388
* @brief List of suppressions.
364389
*/

gui/projectfiledialog.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@
3333
#include "library.h"
3434
#include "cppcheck.h"
3535
#include "errorlogger.h"
36+
#include "platforms.h"
37+
38+
/** Platforms shown in the platform combobox */
39+
static const cppcheck::Platform::PlatformType builtinPlatforms[] = {
40+
cppcheck::Platform::Native,
41+
cppcheck::Platform::Win32A,
42+
cppcheck::Platform::Win32W,
43+
cppcheck::Platform::Win64,
44+
cppcheck::Platform::Unix32,
45+
cppcheck::Platform::Unix64
46+
};
47+
48+
static const int numberOfBuiltinPlatforms = sizeof(builtinPlatforms) / sizeof(builtinPlatforms[0]);
3649

3750
ProjectFileDialog::ProjectFileDialog(ProjectFile *projectFile, QWidget *parent)
3851
: QDialog(parent)
@@ -94,6 +107,32 @@ ProjectFileDialog::ProjectFileDialog(ProjectFile *projectFile, QWidget *parent)
94107
mLibraryCheckboxes << checkbox;
95108
}
96109

110+
// Platforms..
111+
Platforms p;
112+
for (int i = 0; i < numberOfBuiltinPlatforms; i++)
113+
mUI.mComboBoxPlatform->addItem(p.get(builtinPlatforms[i]).mTitle);
114+
QStringList platformFiles;
115+
foreach (QString sp, searchPaths) {
116+
if (sp.endsWith("/cfg"))
117+
sp = sp.mid(0,sp.length()-3) + "platforms";
118+
QDir dir(sp);
119+
dir.setSorting(QDir::Name);
120+
dir.setNameFilters(QStringList("*.xml"));
121+
dir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
122+
foreach (QFileInfo item, dir.entryInfoList()) {
123+
const QString platformFile = item.fileName();
124+
125+
cppcheck::Platform p;
126+
if (!p.loadPlatformFile(appPath.toStdString().c_str(), platformFile.toStdString()))
127+
continue;
128+
129+
if (platformFiles.indexOf(platformFile) == -1)
130+
platformFiles << platformFile;
131+
}
132+
}
133+
qSort(platformFiles);
134+
mUI.mComboBoxPlatform->addItems(platformFiles);
135+
97136
mUI.mEditTags->setValidator(new QRegExpValidator(QRegExp("[a-zA-Z0-9 ;]*"),this));
98137

99138
connect(mUI.mButtons, &QDialogButtonBox::accepted, this, &ProjectFileDialog::ok);
@@ -156,6 +195,33 @@ void ProjectFileDialog::loadFromProjectFile(const ProjectFile *projectFile)
156195
mUI.mChkAllVsConfigs->setChecked(projectFile->getAnalyzeAllVsConfigs());
157196
setExcludedPaths(projectFile->getExcludedPaths());
158197
setLibraries(projectFile->getLibraries());
198+
const QString platform = projectFile->getPlatform();
199+
if (platform.endsWith(".xml")) {
200+
int i;
201+
for (i = numberOfBuiltinPlatforms; i < mUI.mComboBoxPlatform->count(); ++i) {
202+
if (mUI.mComboBoxPlatform->itemText(i) == platform)
203+
break;
204+
}
205+
if (i < mUI.mComboBoxPlatform->count())
206+
mUI.mComboBoxPlatform->setCurrentIndex(i);
207+
else {
208+
mUI.mComboBoxPlatform->addItem(platform);
209+
mUI.mComboBoxPlatform->setCurrentIndex(i);
210+
}
211+
} else {
212+
int i;
213+
for (i = 0; i < numberOfBuiltinPlatforms; ++i) {
214+
const cppcheck::Platform::PlatformType p = builtinPlatforms[i];
215+
if (platform == cppcheck::Platform::platformString(p))
216+
break;
217+
}
218+
if (i < numberOfBuiltinPlatforms)
219+
mUI.mComboBoxPlatform->setCurrentIndex(i);
220+
else
221+
mUI.mComboBoxPlatform->setCurrentIndex(-1);
222+
}
223+
224+
mUI.mComboBoxPlatform->setCurrentText(projectFile->getPlatform());
159225
setSuppressions(projectFile->getSuppressions());
160226

161227
QSettings settings;
@@ -193,6 +259,15 @@ void ProjectFileDialog::saveToProjectFile(ProjectFile *projectFile) const
193259
projectFile->setCheckPaths(getCheckPaths());
194260
projectFile->setExcludedPaths(getExcludedPaths());
195261
projectFile->setLibraries(getLibraries());
262+
if (mUI.mComboBoxPlatform->currentText().endsWith(".xml"))
263+
projectFile->setPlatform(mUI.mComboBoxPlatform->currentText());
264+
else {
265+
int i = mUI.mComboBoxPlatform->currentIndex();
266+
if (i < numberOfBuiltinPlatforms)
267+
projectFile->setPlatform(cppcheck::Platform::platformString(builtinPlatforms[i]));
268+
else
269+
projectFile->setPlatform(QString());
270+
}
196271
projectFile->setSuppressions(getSuppressions());
197272
QStringList list;
198273
if (mUI.mAddonThreadSafety->isChecked())

0 commit comments

Comments
 (0)