Skip to content

Commit 69d89c5

Browse files
authored
Fix #14671 (GUI: add --include option to project file dialog) (danmar#8454)
1 parent 30cdc63 commit 69d89c5

10 files changed

Lines changed: 108 additions & 29 deletions

.github/workflows/selfcheck.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ jobs:
121121
122122
- name: Self check (unusedFunction / no test / no gui)
123123
run: |
124-
supprs="--suppress=unusedFunction:lib/errorlogger.h:197 --suppress=unusedFunction:lib/importproject.cpp:1660 --suppress=unusedFunction:lib/importproject.cpp:1684"
124+
supprs="--suppress=unusedFunction:lib/errorlogger.h:197 --suppress=unusedFunction:lib/importproject.cpp:1665 --suppress=unusedFunction:lib/importproject.cpp:1689"
125125
./cppcheck -q --template=selfcheck --error-exitcode=1 --library=cppcheck-lib -D__CPPCHECK__ -D__GNUC__ --enable=unusedFunction,information --exception-handling -rp=. --project=cmake.output.notest_nogui/compile_commands.json --suppressions-list=.selfcheck_unused_suppressions --inline-suppr $supprs
126126
env:
127127
DISABLE_VALUEFLOW: 1

gui/mainwindow.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,8 @@ bool MainWindow::getCppcheckSettings(Settings& settings, Suppressions& supprs)
11261126
for (const QString& undefine : undefines)
11271127
settings.userUndefs.insert(undefine.toStdString());
11281128

1129+
settings.userIncludes.push_back(mProjectFile->getUserInclude().toStdString());
1130+
11291131
const QStringList libraries = mProjectFile->getLibraries();
11301132
for (const QString& library : libraries) {
11311133
settings.libraries.emplace_back(library.toStdString());

gui/projectfile.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ void ProjectFile::clear()
6363
mIncludeDirs.clear();
6464
mDefines.clear();
6565
mUndefines.clear();
66+
mUserInclude.clear();
6667
mPaths.clear();
6768
mExcludedPaths.clear();
6869
mLibraries.clear();
@@ -166,6 +167,9 @@ bool ProjectFile::read(const QString &filename)
166167
if (xmlReader.name() == QString(CppcheckXml::UndefinesElementName))
167168
readStringList(mUndefines, xmlReader, CppcheckXml::UndefineName);
168169

170+
if (xmlReader.name() == QString(CppcheckXml::UserIncludeElementName))
171+
mUserInclude = readString(xmlReader);
172+
169173
// Find exclude list from inside project element
170174
if (xmlReader.name() == QString(CppcheckXml::ExcludeElementName))
171175
readExcludes(xmlReader);
@@ -347,15 +351,13 @@ bool ProjectFile::readBool(QXmlStreamReader &reader)
347351

348352
int ProjectFile::readInt(QXmlStreamReader &reader, int defaultValue)
349353
{
350-
int ret = defaultValue;
351354
do {
352355
const QXmlStreamReader::TokenType type = reader.readNext();
353356
switch (type) {
354357
case QXmlStreamReader::Characters:
355-
ret = reader.text().toString().toInt();
356-
FALLTHROUGH;
358+
return reader.text().toString().toInt();
357359
case QXmlStreamReader::EndElement:
358-
return ret;
360+
return defaultValue;
359361
// Not handled
360362
case QXmlStreamReader::StartElement:
361363
case QXmlStreamReader::NoToken:
@@ -373,15 +375,13 @@ int ProjectFile::readInt(QXmlStreamReader &reader, int defaultValue)
373375

374376
QString ProjectFile::readString(QXmlStreamReader &reader)
375377
{
376-
QString ret;
377378
do {
378379
const QXmlStreamReader::TokenType type = reader.readNext();
379380
switch (type) {
380381
case QXmlStreamReader::Characters:
381-
ret = reader.text().toString();
382-
FALLTHROUGH;
382+
return reader.text().toString();
383383
case QXmlStreamReader::EndElement:
384-
return ret;
384+
return {};
385385
// Not handled
386386
case QXmlStreamReader::StartElement:
387387
case QXmlStreamReader::NoToken:
@@ -910,6 +910,12 @@ bool ProjectFile::write(const QString &filename)
910910
xmlWriter.writeEndElement();
911911
}
912912

913+
if (!mUserInclude.isEmpty()) {
914+
xmlWriter.writeStartElement(CppcheckXml::UserIncludeElementName);
915+
xmlWriter.writeCharacters(mUserInclude);
916+
xmlWriter.writeEndElement();
917+
}
918+
913919
if (!mVsConfigurations.isEmpty()) {
914920
writeStringList(xmlWriter,
915921
mVsConfigurations,

gui/projectfile.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@ class ProjectFile : public QObject {
141141
return mUndefines;
142142
}
143143

144+
const QString& getUserInclude() const {
145+
return mUserInclude;
146+
}
147+
148+
void setUserInclude(const QString& userInclude) {
149+
mUserInclude = userInclude;
150+
}
151+
144152
/**
145153
* @brief Get list of paths to check.
146154
* @return list of paths.
@@ -601,6 +609,9 @@ class ProjectFile : public QObject {
601609
*/
602610
QStringList mUndefines;
603611

612+
/** @brief --include file */
613+
QString mUserInclude;
614+
604615
/**
605616
* @brief List of paths to check.
606617
*/

gui/projectfile.ui

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<x>0</x>
88
<y>0</y>
99
<width>940</width>
10-
<height>701</height>
10+
<height>741</height>
1111
</rect>
1212
</property>
1313
<property name="windowTitle">
@@ -80,7 +80,7 @@
8080
<item>
8181
<spacer name="verticalSpacer_11">
8282
<property name="orientation">
83-
<enum>Qt::Vertical</enum>
83+
<enum>Qt::Orientation::Vertical</enum>
8484
</property>
8585
<property name="sizeHint" stdset="0">
8686
<size>
@@ -95,7 +95,7 @@
9595
<item>
9696
<spacer name="horizontalSpacer_2">
9797
<property name="orientation">
98-
<enum>Qt::Horizontal</enum>
98+
<enum>Qt::Orientation::Horizontal</enum>
9999
</property>
100100
<property name="sizeHint" stdset="0">
101101
<size>
@@ -117,7 +117,7 @@
117117
<item>
118118
<spacer name="verticalSpacer_12">
119119
<property name="orientation">
120-
<enum>Qt::Vertical</enum>
120+
<enum>Qt::Orientation::Vertical</enum>
121121
</property>
122122
<property name="sizeHint" stdset="0">
123123
<size>
@@ -151,7 +151,7 @@
151151
<item>
152152
<spacer name="verticalSpacer_2">
153153
<property name="orientation">
154-
<enum>Qt::Vertical</enum>
154+
<enum>Qt::Orientation::Vertical</enum>
155155
</property>
156156
<property name="sizeHint" stdset="0">
157157
<size>
@@ -199,7 +199,7 @@
199199
<item>
200200
<spacer name="verticalSpacer_3">
201201
<property name="orientation">
202-
<enum>Qt::Vertical</enum>
202+
<enum>Qt::Orientation::Vertical</enum>
203203
</property>
204204
<property name="sizeHint" stdset="0">
205205
<size>
@@ -269,7 +269,7 @@
269269
<item>
270270
<spacer name="verticalSpacer_6">
271271
<property name="orientation">
272-
<enum>Qt::Vertical</enum>
272+
<enum>Qt::Orientation::Vertical</enum>
273273
</property>
274274
<property name="sizeHint" stdset="0">
275275
<size>
@@ -284,7 +284,7 @@
284284
<item>
285285
<widget class="QListWidget" name="mListIncludeDirs">
286286
<property name="selectionBehavior">
287-
<enum>QAbstractItemView::SelectRows</enum>
287+
<enum>QAbstractItemView::SelectionBehavior::SelectRows</enum>
288288
</property>
289289
</widget>
290290
</item>
@@ -314,7 +314,7 @@
314314
<item>
315315
<spacer name="verticalSpacer">
316316
<property name="orientation">
317-
<enum>Qt::Vertical</enum>
317+
<enum>Qt::Orientation::Vertical</enum>
318318
</property>
319319
<property name="sizeHint" stdset="0">
320320
<size>
@@ -342,10 +342,38 @@
342342
</item>
343343
</layout>
344344
</item>
345+
<item>
346+
<layout class="QHBoxLayout" name="mLayoutDefines_2">
347+
<item>
348+
<widget class="QLabel" name="mLabelDefines_2">
349+
<property name="text">
350+
<string>Include file</string>
351+
</property>
352+
<property name="buddy">
353+
<cstring>mEditUserInclude</cstring>
354+
</property>
355+
</widget>
356+
</item>
357+
<item>
358+
<widget class="QLineEdit" name="mEditUserInclude">
359+
<property name="toolTip">
360+
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;span style=&quot; font-family:'monospace'; color:#000000; background-color:#ffffff;&quot;&gt;Force inclusion of a header file&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
361+
</property>
362+
</widget>
363+
</item>
364+
<item>
365+
<widget class="QPushButton" name="mBtnBrowseUserInclude">
366+
<property name="text">
367+
<string>Browse..</string>
368+
</property>
369+
</widget>
370+
</item>
371+
</layout>
372+
</item>
345373
<item>
346374
<spacer name="verticalSpacer_8">
347375
<property name="orientation">
348-
<enum>Qt::Vertical</enum>
376+
<enum>Qt::Orientation::Vertical</enum>
349377
</property>
350378
<property name="sizeHint" stdset="0">
351379
<size>
@@ -552,7 +580,7 @@
552580
<item>
553581
<spacer name="horizontalSpacer">
554582
<property name="orientation">
555-
<enum>Qt::Horizontal</enum>
583+
<enum>Qt::Orientation::Horizontal</enum>
556584
</property>
557585
<property name="sizeHint" stdset="0">
558586
<size>
@@ -586,7 +614,7 @@
586614
<item>
587615
<spacer name="horizontalSpacer_3">
588616
<property name="orientation">
589-
<enum>Qt::Horizontal</enum>
617+
<enum>Qt::Orientation::Horizontal</enum>
590618
</property>
591619
<property name="sizeHint" stdset="0">
592620
<size>
@@ -701,7 +729,7 @@
701729
<item>
702730
<spacer name="verticalSpacer_4">
703731
<property name="orientation">
704-
<enum>Qt::Vertical</enum>
732+
<enum>Qt::Orientation::Vertical</enum>
705733
</property>
706734
<property name="sizeHint" stdset="0">
707735
<size>
@@ -744,7 +772,7 @@
744772
<item>
745773
<spacer name="verticalSpacer_10">
746774
<property name="orientation">
747-
<enum>Qt::Vertical</enum>
775+
<enum>Qt::Orientation::Vertical</enum>
748776
</property>
749777
<property name="sizeHint" stdset="0">
750778
<size>
@@ -769,7 +797,7 @@
769797
<item>
770798
<spacer name="verticalSpacer_9">
771799
<property name="orientation">
772-
<enum>Qt::Vertical</enum>
800+
<enum>Qt::Orientation::Vertical</enum>
773801
</property>
774802
<property name="sizeHint" stdset="0">
775803
<size>
@@ -856,7 +884,7 @@
856884
<item>
857885
<spacer name="horizontalSpacer_4">
858886
<property name="orientation">
859-
<enum>Qt::Horizontal</enum>
887+
<enum>Qt::Orientation::Horizontal</enum>
860888
</property>
861889
<property name="sizeHint" stdset="0">
862890
<size>
@@ -919,7 +947,7 @@
919947
<item>
920948
<spacer name="horizontalSpacer_5">
921949
<property name="orientation">
922-
<enum>Qt::Horizontal</enum>
950+
<enum>Qt::Orientation::Horizontal</enum>
923951
</property>
924952
<property name="sizeHint" stdset="0">
925953
<size>
@@ -1018,7 +1046,7 @@
10181046
<item>
10191047
<spacer name="verticalSpacer_5">
10201048
<property name="orientation">
1021-
<enum>Qt::Vertical</enum>
1049+
<enum>Qt::Orientation::Vertical</enum>
10221050
</property>
10231051
<property name="sizeHint" stdset="0">
10241052
<size>
@@ -1035,10 +1063,10 @@
10351063
<item>
10361064
<widget class="QDialogButtonBox" name="mButtons">
10371065
<property name="orientation">
1038-
<enum>Qt::Horizontal</enum>
1066+
<enum>Qt::Orientation::Horizontal</enum>
10391067
</property>
10401068
<property name="standardButtons">
1041-
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
1069+
<set>QDialogButtonBox::StandardButton::Cancel|QDialogButtonBox::StandardButton::Ok</set>
10421070
</property>
10431071
</widget>
10441072
</item>

gui/projectfiledialog.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ ProjectFileDialog::ProjectFileDialog(ProjectFile *projectFile, bool premium, QWi
235235
connect(mUI->mBtnBrowseBuildDir, &QPushButton::clicked, this, &ProjectFileDialog::browseBuildDir);
236236
connect(mUI->mBtnClearImportProject, &QPushButton::clicked, this, &ProjectFileDialog::clearImportProject);
237237
connect(mUI->mBtnBrowseImportProject, &QPushButton::clicked, this, &ProjectFileDialog::browseImportProject);
238+
connect(mUI->mBtnBrowseUserInclude, &QPushButton::clicked, this, &ProjectFileDialog::browseUserInclude);
238239
connect(mUI->mBtnAddCheckPath, SIGNAL(clicked()), this, SLOT(addCheckPath()));
239240
connect(mUI->mBtnEditCheckPath, &QPushButton::clicked, this, &ProjectFileDialog::editCheckPath);
240241
connect(mUI->mBtnRemoveCheckPath, &QPushButton::clicked, this, &ProjectFileDialog::removeCheckPath);
@@ -303,6 +304,7 @@ void ProjectFileDialog::loadFromProjectFile(const ProjectFile *projectFile)
303304
setIncludepaths(projectFile->getIncludeDirs());
304305
setDefines(projectFile->getDefines());
305306
setUndefines(projectFile->getUndefines());
307+
mUI->mEditUserInclude->setText(projectFile->getUserInclude());
306308
setCheckPaths(projectFile->getCheckPaths());
307309
setImportProject(projectFile->getImportProject());
308310
mUI->mChkAllVsConfigs->setChecked(projectFile->getAnalyzeAllVsConfigs());
@@ -481,6 +483,7 @@ void ProjectFileDialog::saveToProjectFile(ProjectFile *projectFile) const
481483
projectFile->setIncludes(getIncludePaths());
482484
projectFile->setDefines(getDefines());
483485
projectFile->setUndefines(getUndefines());
486+
projectFile->setUserInclude(mUI->mEditUserInclude->text());
484487
projectFile->setCheckPaths(getCheckPaths());
485488
projectFile->setExcludedPaths(getExcludedPaths());
486489
projectFile->setLibraries(getLibraries());
@@ -643,6 +646,21 @@ void ProjectFileDialog::browseImportProject()
643646
}
644647
}
645648

649+
void ProjectFileDialog::browseUserInclude()
650+
{
651+
const QFileInfo inf(mProjectFile->getFilename());
652+
const QDir &dir = inf.absoluteDir();
653+
QMap<QString,QString> filters;
654+
filters[tr("C/C++ header")] = "*.h";
655+
filters[tr("All files")] = "*.*";
656+
QString fileName = QFileDialog::getOpenFileName(this, tr("Include file"),
657+
dir.canonicalPath(),
658+
toFilterString(filters));
659+
if (!fileName.isEmpty()) {
660+
mUI->mEditUserInclude->setText(dir.relativeFilePath(fileName));
661+
}
662+
}
663+
646664
QStringList ProjectFileDialog::getProjectConfigurations() const
647665
{
648666
QStringList configs;

gui/projectfiledialog.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@ private slots:
201201
*/
202202
void browseImportProject();
203203

204+
/**
205+
* @brief Browse for include file
206+
*/
207+
void browseUserInclude();
208+
204209
/**
205210
* @brief Add new path to check.
206211
*/

lib/importproject.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,6 +1484,10 @@ bool ImportProject::importCppcheckGuiProject(std::istream &istr, Settings &setti
14841484
else if (strcmp(name, CppcheckXml::UndefinesElementName) == 0) {
14851485
for (const std::string &u : readXmlStringList(node, "", CppcheckXml::UndefineName, nullptr))
14861486
temp.userUndefs.insert(u);
1487+
} else if (strcmp(name, CppcheckXml::UserIncludeElementName) == 0) {
1488+
const char* i = node->GetText();
1489+
if (i)
1490+
temp.userIncludes.emplace_back(i);
14871491
} else if (strcmp(name, CppcheckXml::ImportProjectElementName) == 0) {
14881492
const std::string t_str = empty_if_null(node->GetText());
14891493
if (!t_str.empty())
@@ -1605,6 +1609,7 @@ bool ImportProject::importCppcheckGuiProject(std::istream &istr, Settings &setti
16051609
settings.includePaths = temp.includePaths; // TODO: append instead of overwrite
16061610
settings.userDefines = temp.userDefines; // TODO: append instead of overwrite
16071611
settings.userUndefs = temp.userUndefs; // TODO: append instead of overwrite
1612+
settings.userIncludes = temp.userIncludes; // TODO: append instead of overwrite
16081613
for (const std::string &addon : temp.addons)
16091614
settings.addons.emplace(addon);
16101615
settings.clang = temp.clang;

0 commit comments

Comments
 (0)