Skip to content

Commit e45744d

Browse files
authored
Fix #12456 (GUI: unable to suppress warnings in "externals/*" headers) (danmar#6254)
1 parent bd8cb94 commit e45744d

File tree

5 files changed

+60
-2
lines changed

5 files changed

+60
-2
lines changed

gui/mainwindow.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ void MainWindow::doAnalyzeFiles(const QStringList &files, const bool checkLibrar
594594
mThread->setFiles(fileNames);
595595
if (mProjectFile && !checkConfiguration)
596596
mThread->setAddonsAndTools(mProjectFile->getAddonsAndTools());
597-
mThread->setSuppressions(mProjectFile ? mProjectFile->getSuppressions() : QList<SuppressionList::Suppression>());
597+
mThread->setSuppressions(mProjectFile ? mProjectFile->getCheckingSuppressions() : QList<SuppressionList::Suppression>());
598598
QDir inf(mCurrentDirectory);
599599
const QString checkPath = inf.canonicalPath();
600600
setPath(SETTINGS_LAST_CHECK_PATH, checkPath);
@@ -1022,7 +1022,7 @@ QPair<bool,Settings> MainWindow::getCppcheckSettings()
10221022
tryLoadLibrary(&result.library, filename);
10231023
}
10241024

1025-
for (const SuppressionList::Suppression &suppression : mProjectFile->getSuppressions()) {
1025+
for (const SuppressionList::Suppression &suppression : mProjectFile->getCheckingSuppressions()) {
10261026
result.supprs.nomsg.addSuppression(suppression);
10271027
}
10281028

gui/projectfile.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <QDir>
3131
#include <QIODevice>
3232
#include <QLatin1String>
33+
#include <QRegularExpression>
3334
#include <QXmlStreamAttributes>
3435
#include <QXmlStreamReader>
3536
#include <QXmlStreamWriter>
@@ -756,6 +757,21 @@ void ProjectFile::setPlatform(const QString &platform)
756757
mPlatform = platform;
757758
}
758759

760+
QList<SuppressionList::Suppression> ProjectFile::getCheckingSuppressions() const
761+
{
762+
const QRegularExpression re1("^[a-zA-Z0-9_\\-]+/.*");
763+
const QRegularExpression re2("^[^/]+$");
764+
QList<SuppressionList::Suppression> result;
765+
for (SuppressionList::Suppression suppression : mSuppressions) {
766+
if (re1.match(suppression.fileName.c_str()).hasMatch() || re2.match(suppression.fileName.c_str()).hasMatch()) {
767+
if (suppression.fileName[0] != '*')
768+
suppression.fileName = QFileInfo(mFilename).absolutePath().toStdString() + "/" + suppression.fileName;
769+
}
770+
result << suppression;
771+
}
772+
return result;
773+
}
774+
759775
void ProjectFile::setSuppressions(const QList<SuppressionList::Suppression> &suppressions)
760776
{
761777
mSuppressions = suppressions;

gui/projectfile.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,12 @@ class ProjectFile : public QObject {
187187
return mSuppressions;
188188
}
189189

190+
/**
191+
* @brief Get "checking" suppressions. Relative paths are converted to absolute paths.
192+
* @return list of suppressions.
193+
*/
194+
QList<SuppressionList::Suppression> getCheckingSuppressions() const;
195+
190196
/**
191197
* @brief Get list addons.
192198
* @return list of addons.

gui/test/projectfile/testprojectfile.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <string>
2828

29+
#include <QDir>
2930
#include <QFile>
3031
#include <QIODevice>
3132
#include <QList>
@@ -138,4 +139,35 @@ void TestProjectFile::getAddonFilePath() const
138139
QCOMPARE(ProjectFile::getAddonFilePath(tempdir.path(), filepath), filepath);
139140
}
140141

142+
void TestProjectFile::getCheckingSuppressionsRelative() const
143+
{
144+
const SuppressionList::Suppression suppression("*", "externals/*");
145+
const QList<SuppressionList::Suppression> suppressions{suppression};
146+
ProjectFile projectFile;
147+
projectFile.setFilename("/some/path/123.cppcheck");
148+
projectFile.setSuppressions(suppressions);
149+
QCOMPARE(projectFile.getCheckingSuppressions()[0].fileName, "/some/path/externals/*");
150+
}
151+
152+
void TestProjectFile::getCheckingSuppressionsAbsolute() const
153+
{
154+
const SuppressionList::Suppression suppression("*", "/some/path/1.h");
155+
const QList<SuppressionList::Suppression> suppressions{suppression};
156+
ProjectFile projectFile;
157+
projectFile.setFilename("/other/123.cppcheck");
158+
projectFile.setSuppressions(suppressions);
159+
QCOMPARE(projectFile.getCheckingSuppressions()[0].fileName, "/some/path/1.h");
160+
}
161+
162+
void TestProjectFile::getCheckingSuppressionsStar() const
163+
{
164+
const SuppressionList::Suppression suppression("*", "*.cpp");
165+
const QList<SuppressionList::Suppression> suppressions{suppression};
166+
ProjectFile projectFile;
167+
projectFile.setFilename("/some/path/123.cppcheck");
168+
projectFile.setSuppressions(suppressions);
169+
QCOMPARE(projectFile.getCheckingSuppressions()[0].fileName, "*.cpp");
170+
}
171+
141172
QTEST_MAIN(TestProjectFile)
173+

gui/test/projectfile/testprojectfile.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,8 @@ private slots:
2929
void loadSimpleNoroot() const;
3030

3131
void getAddonFilePath() const;
32+
33+
void getCheckingSuppressionsRelative() const;
34+
void getCheckingSuppressionsAbsolute() const;
35+
void getCheckingSuppressionsStar() const;
3236
};

0 commit comments

Comments
 (0)