Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion gui/projectfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,9 @@ void ProjectFile::setCheckPaths(const QStringList &paths)

void ProjectFile::setExcludedPaths(const QStringList &paths)
{
mExcludedPaths = paths;
mExcludedPaths.clear();
for (const QString &path : paths)
mExcludedPaths << (QFileInfo(path).isAbsolute() ? getRelativePath(path) : path);
}

void ProjectFile::setLibraries(const QStringList &libraries)
Expand Down Expand Up @@ -1193,6 +1195,15 @@ QStringList ProjectFile::getSearchPaths(const QString& projectPath, const QStrin
return ret;
}

QString ProjectFile::getRelativePath(const QString &absolutePath) const
{
const QDir dir(QFileInfo(mFilename).absolutePath());
const QString relativePath(dir.relativeFilePath(absolutePath));
if (relativePath.startsWith("../.."))
return absolutePath;
return relativePath;
}

QStringList ProjectFile::getSearchPaths(const QString& dir) const {
const QFileInfo inf(mFilename);
const QString applicationFilePath = QCoreApplication::applicationFilePath();
Expand Down
8 changes: 8 additions & 0 deletions gui/projectfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,14 @@ class ProjectFile : public QObject {

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

/**
* @brief Convert an absolute path to a path relative to this project's directory.
* If the relative path would need to walk up more than 2 parent folders
* (i.e. "../../...") the absolute path is returned unchanged instead.
* @param absolutePath Absolute path to convert.
*/
Comment on lines +450 to +455
QString getRelativePath(const QString &absolutePath) const;

/** Set user includes in settings if non-empty */
void setSettingsUserIncludes(Settings &settings) const;

Expand Down
9 changes: 3 additions & 6 deletions gui/projectfiledialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -571,10 +571,7 @@ QString ProjectFileDialog::getExistingDirectory(const QString &caption, bool tra

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

// Trailing slash..
if (trailingSlash && !selectedDir.endsWith('/'))
Expand Down Expand Up @@ -631,7 +628,7 @@ void ProjectFileDialog::browseImportProject()
dir.canonicalPath(),
toFilterString(filters));
if (!fileName.isEmpty()) {
mUI->mEditImportProject->setText(dir.relativeFilePath(fileName));
mUI->mEditImportProject->setText(mProjectFile->getRelativePath(fileName));
updatePathsAndDefines();
setProjectConfigurations(getProjectConfigs(fileName));
for (int row = 0; row < mUI->mListVsConfigs->count(); ++row) {
Expand All @@ -652,7 +649,7 @@ void ProjectFileDialog::browseUserInclude()
dir.canonicalPath(),
toFilterString(filters));
if (!fileName.isEmpty()) {
mUI->mEditUserInclude->setText(dir.relativeFilePath(fileName));
mUI->mEditUserInclude->setText(mProjectFile->getRelativePath(fileName));
}
}

Expand Down
27 changes: 27 additions & 0 deletions gui/test/projectfile/testprojectfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,5 +214,32 @@ void TestProjectFile::emptyUserInclude() const
QCOMPARE(settings.userIncludes.size(), 0);
}

// Absolute path is made relative when it does not require walking up more than 2 parent folders
void TestProjectFile::setExcludedPathsRelative() const
{
ProjectFile projectFile;
projectFile.setFilename("/some/path/123.cppcheck");
projectFile.setExcludedPaths(QStringList() << "/some/externals/foo.cpp");
QCOMPARE(projectFile.getExcludedPaths()[0], QString("../externals/foo.cpp"));
}

// Absolute path is kept as-is when making it relative would require walking up more than 2 parent folders
void TestProjectFile::setExcludedPathsTooFarUp() const
{
ProjectFile projectFile;
projectFile.setFilename("/some/path/123.cppcheck");
projectFile.setExcludedPaths(QStringList() << "/other/deep/foo.cpp");
QCOMPARE(projectFile.getExcludedPaths()[0], QString("/other/deep/foo.cpp"));
}

// Paths that are already relative are kept unchanged
void TestProjectFile::setExcludedPathsAlreadyRelative() const
{
ProjectFile projectFile;
projectFile.setFilename("/some/path/123.cppcheck");
projectFile.setExcludedPaths(QStringList() << "gui/temp/");
QCOMPARE(projectFile.getExcludedPaths()[0], QString("gui/temp/"));
}

QTEST_MAIN(TestProjectFile)

4 changes: 4 additions & 0 deletions gui/test/projectfile/testprojectfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ private slots:
void getCheckingSuppressionsStar() const;

void emptyUserInclude() const;

void setExcludedPathsRelative() const;
void setExcludedPathsTooFarUp() const;
void setExcludedPathsAlreadyRelative() const;
};
Loading