Skip to content

Commit 88eaa23

Browse files
committed
Fix #14983 (GUI: exclude file with relative path)
1 parent b702ead commit 88eaa23

5 files changed

Lines changed: 54 additions & 7 deletions

File tree

gui/projectfile.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,9 @@ void ProjectFile::setCheckPaths(const QStringList &paths)
751751

752752
void ProjectFile::setExcludedPaths(const QStringList &paths)
753753
{
754-
mExcludedPaths = paths;
754+
mExcludedPaths.clear();
755+
for (const QString &path : paths)
756+
mExcludedPaths << (QFileInfo(path).isAbsolute() ? getRelativePath(path) : path);
755757
}
756758

757759
void ProjectFile::setLibraries(const QStringList &libraries)
@@ -1193,6 +1195,15 @@ QStringList ProjectFile::getSearchPaths(const QString& projectPath, const QStrin
11931195
return ret;
11941196
}
11951197

1198+
QString ProjectFile::getRelativePath(const QString &absolutePath) const
1199+
{
1200+
const QDir dir(QFileInfo(mFilename).absolutePath());
1201+
const QString relativePath(dir.relativeFilePath(absolutePath));
1202+
if (relativePath.startsWith("../.."))
1203+
return absolutePath;
1204+
return relativePath;
1205+
}
1206+
11961207
QStringList ProjectFile::getSearchPaths(const QString& dir) const {
11971208
const QFileInfo inf(mFilename);
11981209
const QString applicationFilePath = QCoreApplication::applicationFilePath();

gui/projectfile.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,14 @@ class ProjectFile : public QObject {
447447

448448
static QStringList getSearchPaths(const QString& projectPath, const QString& appPath, const QString& datadir, const QString& dir);
449449

450+
/**
451+
* @brief Convert an absolute path to a path relative to this project's directory.
452+
* If the relative path would need to walk up more than 2 parent folders
453+
* (i.e. "../../...") the absolute path is returned unchanged instead.
454+
* @param absolutePath Absolute path to convert.
455+
*/
456+
QString getRelativePath(const QString &absolutePath) const;
457+
450458
/** Set user includes in settings if non-empty */
451459
void setSettingsUserIncludes(Settings &settings) const;
452460

gui/projectfiledialog.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -571,10 +571,7 @@ QString ProjectFileDialog::getExistingDirectory(const QString &caption, bool tra
571571

572572
// Check if the path is relative to project file's path and if so
573573
// make it a relative path instead of absolute path.
574-
const QDir dir(projectPath);
575-
const QString relpath(dir.relativeFilePath(selectedDir));
576-
if (!relpath.startsWith("../.."))
577-
selectedDir = relpath;
574+
selectedDir = mProjectFile->getRelativePath(selectedDir);
578575

579576
// Trailing slash..
580577
if (trailingSlash && !selectedDir.endsWith('/'))
@@ -631,7 +628,7 @@ void ProjectFileDialog::browseImportProject()
631628
dir.canonicalPath(),
632629
toFilterString(filters));
633630
if (!fileName.isEmpty()) {
634-
mUI->mEditImportProject->setText(dir.relativeFilePath(fileName));
631+
mUI->mEditImportProject->setText(mProjectFile->getRelativePath(fileName));
635632
updatePathsAndDefines();
636633
setProjectConfigurations(getProjectConfigs(fileName));
637634
for (int row = 0; row < mUI->mListVsConfigs->count(); ++row) {
@@ -652,7 +649,7 @@ void ProjectFileDialog::browseUserInclude()
652649
dir.canonicalPath(),
653650
toFilterString(filters));
654651
if (!fileName.isEmpty()) {
655-
mUI->mEditUserInclude->setText(dir.relativeFilePath(fileName));
652+
mUI->mEditUserInclude->setText(mProjectFile->getRelativePath(fileName));
656653
}
657654
}
658655

gui/test/projectfile/testprojectfile.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,5 +214,32 @@ void TestProjectFile::emptyUserInclude() const
214214
QCOMPARE(settings.userIncludes.size(), 0);
215215
}
216216

217+
// Absolute path is made relative when it does not require walking up more than 2 parent folders
218+
void TestProjectFile::setExcludedPathsRelative() const
219+
{
220+
ProjectFile projectFile;
221+
projectFile.setFilename("/some/path/123.cppcheck");
222+
projectFile.setExcludedPaths(QStringList() << "/some/externals/foo.cpp");
223+
QCOMPARE(projectFile.getExcludedPaths()[0], QString("../externals/foo.cpp"));
224+
}
225+
226+
// Absolute path is kept as-is when making it relative would require walking up more than 2 parent folders
227+
void TestProjectFile::setExcludedPathsTooFarUp() const
228+
{
229+
ProjectFile projectFile;
230+
projectFile.setFilename("/some/path/123.cppcheck");
231+
projectFile.setExcludedPaths(QStringList() << "/other/deep/foo.cpp");
232+
QCOMPARE(projectFile.getExcludedPaths()[0], QString("/other/deep/foo.cpp"));
233+
}
234+
235+
// Paths that are already relative are kept unchanged
236+
void TestProjectFile::setExcludedPathsAlreadyRelative() const
237+
{
238+
ProjectFile projectFile;
239+
projectFile.setFilename("/some/path/123.cppcheck");
240+
projectFile.setExcludedPaths(QStringList() << "gui/temp/");
241+
QCOMPARE(projectFile.getExcludedPaths()[0], QString("gui/temp/"));
242+
}
243+
217244
QTEST_MAIN(TestProjectFile)
218245

gui/test/projectfile/testprojectfile.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,8 @@ private slots:
3838
void getCheckingSuppressionsStar() const;
3939

4040
void emptyUserInclude() const;
41+
42+
void setExcludedPathsRelative() const;
43+
void setExcludedPathsTooFarUp() const;
44+
void setExcludedPathsAlreadyRelative() const;
4145
};

0 commit comments

Comments
 (0)