Utils: Replace FilePath part setters

... by a combined version. This will make it easier to store the parts
in one QString object.

Change-Id: Ie85a77e3957c78a30e49998fe2e617af35a8ad17
Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
hjk
2022-08-04 15:00:24 +02:00
parent 7a3f95a144
commit 7cfc3d26bc
18 changed files with 60 additions and 119 deletions

View File

@@ -503,15 +503,14 @@ void Snapshot::insertLibraryInfo(const Utils::FilePath &path, const LibraryInfo
break; break;
ImportKey iKey(ImportType::Library, QStringList(myPath.mid(iPath)).join(QLatin1Char('.')), ImportKey iKey(ImportType::Library, QStringList(myPath.mid(iPath)).join(QLatin1Char('.')),
importKey.majorVersion, importKey.minorVersion); importKey.majorVersion, importKey.minorVersion);
Utils::FilePath newP(path); Utils::FilePath newP = path.withNewPath(
newP.setPath((iPath == 1) (iPath == 1)
? QLatin1String("/") ? QLatin1String("/")
: QStringList(myPath.mid(0, iPath)).join(QLatin1Char('/'))); : QStringList(myPath.mid(0, iPath)).join(QLatin1Char('/')));
cImport.addPossibleExport(Export(iKey, newP, true)); cImport.addPossibleExport(Export(iKey, newP, true));
} }
} else { } else {
Utils::FilePath requiredPath(path); Utils::FilePath requiredPath = path.withNewPath(
requiredPath.setPath(
QStringList(splitPath.mid(0, splitPath.size() - importKey.splitPath.size())) QStringList(splitPath.mid(0, splitPath.size() - importKey.splitPath.size()))
.join(QLatin1String("/"))); .join(QLatin1String("/")));
cImport.addPossibleExport(Export(importKey, requiredPath, true)); cImport.addPossibleExport(Export(importKey, requiredPath, true));
@@ -549,8 +548,8 @@ void Snapshot::insertLibraryInfo(const Utils::FilePath &path, const LibraryInfo
break; break;
ImportKey iKey(ImportType::Library, QStringList(splitPath.mid(iPath)).join(QLatin1Char('.')), ImportKey iKey(ImportType::Library, QStringList(splitPath.mid(iPath)).join(QLatin1Char('.')),
majorVersion, minorVersion); majorVersion, minorVersion);
Utils::FilePath newP(path); Utils::FilePath newP = path.withNewPath(
newP.setPath((iPath == 1) iPath == 1
? QLatin1String("/") ? QLatin1String("/")
: QStringList(splitPath.mid(0, iPath)).join(QLatin1Char('/'))); : QStringList(splitPath.mid(0, iPath)).join(QLatin1Char('/')));
cImport.addPossibleExport(Export(iKey, newP, true)); cImport.addPossibleExport(Export(iKey, newP, true));

View File

@@ -1200,7 +1200,7 @@ static QList<Utils::FilePath> minimalPrefixPaths(const QList<Utils::FilePath> &p
// find minimal prefix, ensure '/' at end // find minimal prefix, ensure '/' at end
for (Utils::FilePath path : qAsConst(paths)) { for (Utils::FilePath path : qAsConst(paths)) {
if (!path.endsWith("/")) if (!path.endsWith("/"))
path.setPath(QString(path.path() + "/")); path = path.withNewPath(path.path() + "/");
if (path.path().length() > 1) if (path.path().length() > 1)
sortedPaths.append(path); sortedPaths.append(path);
} }

View File

@@ -37,9 +37,7 @@ FilePath BuildableHelperLibrary::qtChooserToQmakePath(const FilePath &qtChooser)
if (end == -1) if (end == -1)
return {}; return {};
FilePath qmake = qtChooser; return qtChooser.withNewPath(output.mid(pos, end - pos) + "/qmake");
qmake.setPath(QString(output.mid(pos, end - pos) + "/qmake"));
return qmake;
} }
static bool isQmake(FilePath path) static bool isQmake(FilePath path)

View File

@@ -150,11 +150,14 @@ QFileInfo FilePath::toFileInfo() const
FilePath FilePath::fromUrl(const QUrl &url) FilePath FilePath::fromUrl(const QUrl &url)
{ {
FilePath fn; return FilePath::fromParts(url.scheme(), url.host(), url.path());
fn.m_scheme = url.scheme(); }
fn.m_host = url.host();
fn.setRootAndPath(url.path(), HostOsInfo::hostOs()); FilePath FilePath::fromParts(const QStringView scheme, const QStringView host, const QStringView path)
return fn; {
FilePath result;
result.setParts(scheme, host, path);
return result;
} }
FilePath FilePath::currentWorkingPath() FilePath FilePath::currentWorkingPath()
@@ -318,22 +321,6 @@ QString FilePath::completeSuffix() const
return {}; return {};
} }
void FilePath::setScheme(const QStringView scheme)
{
QTC_CHECK(!scheme.contains('/'));
m_scheme = scheme.toString();
}
void FilePath::setHost(const QStringView host)
{
m_host = host.toString();
}
void FilePath::setPath(const QStringView path)
{
setRootAndPath(path, HostOsInfo::hostOs());
}
QStringView FilePath::scheme() const QStringView FilePath::scheme() const
{ {
return m_scheme; return m_scheme;
@@ -354,9 +341,12 @@ QStringView FilePath::root() const
return m_root; return m_root;
} }
void FilePath::setRoot(const QStringView root) void FilePath::setParts(const QStringView scheme, const QStringView host, const QStringView path)
{ {
m_root = root.toString(); QTC_CHECK(!m_scheme.contains('/'));
m_scheme = scheme.toString();
m_host = host.toString();
setRootAndPath(path, HostOsInfo::hostOs());
} }
/// \returns a bool indicating whether a file with this /// \returns a bool indicating whether a file with this
@@ -629,9 +619,7 @@ QString FilePath::mapToDevicePath() const
FilePath FilePath::withExecutableSuffix() const FilePath FilePath::withExecutableSuffix() const
{ {
FilePath res = *this; return withNewPath(OsSpecificAspects::withExecutableSuffix(osType(), path()));
res.setPath(OsSpecificAspects::withExecutableSuffix(osType(), path()));
return res;
} }
/// Find the parent directory of a given directory. /// Find the parent directory of a given directory.
@@ -655,9 +643,7 @@ FilePath FilePath::parentDir() const
const QString parent = doCleanPath(path); const QString parent = doCleanPath(path);
QTC_ASSERT(parent != path, return FilePath()); QTC_ASSERT(parent != path, return FilePath());
FilePath result = *this; return withNewPath(parent);
result.setPath(parent);
return result;
} }
FilePath FilePath::absolutePath() const FilePath FilePath::absolutePath() const
@@ -1568,7 +1554,7 @@ void FilePath::clear()
*/ */
bool FilePath::isEmpty() const bool FilePath::isEmpty() const
{ {
return m_root.isEmpty() && m_path.isEmpty(); return root().isEmpty() && path().isEmpty();
} }
/*! /*!
@@ -1596,7 +1582,7 @@ QString FilePath::shortNativePath() const
*/ */
bool FilePath::isRelativePath() const bool FilePath::isRelativePath() const
{ {
return m_root.isEmpty(); return root().isEmpty();
} }
/*! /*!
@@ -1606,9 +1592,9 @@ bool FilePath::isRelativePath() const
*/ */
FilePath FilePath::resolvePath(const FilePath &tail) const FilePath FilePath::resolvePath(const FilePath &tail) const
{ {
if (!tail.m_root.isEmpty()) if (!tail.root().isEmpty())
return tail; return tail;
return pathAppended(tail.m_path); return pathAppended(tail.path());
} }
/*! /*!
@@ -1624,9 +1610,7 @@ FilePath FilePath::resolvePath(const QString &tail) const
FilePath FilePath::cleanPath() const FilePath FilePath::cleanPath() const
{ {
FilePath result = *this; return withNewPath(doCleanPath(path()));
result.setPath(doCleanPath(result.path()));
return result;
} }
QTextStream &operator<<(QTextStream &s, const FilePath &fn) QTextStream &operator<<(QTextStream &s, const FilePath &fn)

View File

@@ -56,11 +56,11 @@ public:
[[nodiscard]] static FilePath fromUtf8(const char *filepath, int filepathSize = -1); [[nodiscard]] static FilePath fromUtf8(const char *filepath, int filepathSize = -1);
[[nodiscard]] static FilePath fromVariant(const QVariant &variant); [[nodiscard]] static FilePath fromVariant(const QVariant &variant);
[[nodiscard]] static FilePath fromUrl(const QUrl &url); [[nodiscard]] static FilePath fromUrl(const QUrl &url);
[[nodiscard]] static FilePath fromParts(const QStringView scheme, const QStringView host, const QStringView path);
[[nodiscard]] static FilePath currentWorkingPath(); [[nodiscard]] static FilePath currentWorkingPath();
[[nodiscard]] static FilePath rootPath(); [[nodiscard]] static FilePath rootPath();
QString toUserOutput() const; QString toUserOutput() const;
QString toString() const; QString toString() const;
QString toFSPathString() const; QString toFSPathString() const;
@@ -68,16 +68,11 @@ public:
QUrl toUrl() const; QUrl toUrl() const;
QStringView scheme() const; QStringView scheme() const;
void setScheme(const QStringView scheme);
QStringView host() const; QStringView host() const;
void setHost(const QStringView host);
QString path() const; QString path() const;
void setPath(const QStringView path);
QStringView root() const; QStringView root() const;
void setRoot(const QStringView root);
void setParts(const QStringView scheme, const QStringView host, const QStringView path);
QString fileName() const; QString fileName() const;
QString fileNameWithPathComponents(int pathComponents) const; QString fileNameWithPathComponents(int pathComponents) const;

View File

@@ -392,7 +392,7 @@ FilePath FileUtils::commonPath(const FilePaths &paths)
}; };
if (!allOf(others, sameScheme)) if (!allOf(others, sameScheme))
return result; return result;
result.setScheme(commonScheme); result.setParts(commonScheme, {}, {});
// Common host // Common host
const QStringView commonHost = first.host(); const QStringView commonHost = first.host();
@@ -401,7 +401,7 @@ FilePath FileUtils::commonPath(const FilePaths &paths)
}; };
if (!allOf(others, sameHost)) if (!allOf(others, sameHost))
return result; return result;
result.setHost(commonHost); result.setParts(commonScheme, commonHost, {});
// Common path // Common path
QString commonPath; QString commonPath;
@@ -413,7 +413,7 @@ FilePath FileUtils::commonPath(const FilePaths &paths)
commonPath += segment + '/'; commonPath += segment + '/';
if (!allOf(others, sameBasePath)) if (!allOf(others, sameBasePath))
return result; return result;
result.setPath(commonPath.chopped(1)); result.setParts(commonScheme, commonHost, commonPath.chopped(1));
} }
return result; return result;

View File

@@ -271,11 +271,10 @@ QString MacroExpander::expand(const QString &stringWithVariables) const
FilePath MacroExpander::expand(const FilePath &fileNameWithVariables) const FilePath MacroExpander::expand(const FilePath &fileNameWithVariables) const
{ {
FilePath result = fileNameWithVariables; return FilePath::fromParts(
result.setPath(expand(result.path())); expand(fileNameWithVariables.scheme().toString()),
result.setHost(expand(result.host().toString())); expand(fileNameWithVariables.host().toString()),
result.setScheme(expand(result.scheme().toString())); expand(fileNameWithVariables.path()));
return result;
} }
QByteArray MacroExpander::expand(const QByteArray &stringWithVariables) const QByteArray MacroExpander::expand(const QByteArray &stringWithVariables) const

View File

@@ -1439,9 +1439,8 @@ FilePath CMakeBuildConfiguration::shadowBuildDirectory(const FilePath &projectFi
bcName, buildType, "cmake"); bcName, buildType, "cmake");
if (CMakeGeneratorKitAspect::isMultiConfigGenerator(k)) { if (CMakeGeneratorKitAspect::isMultiConfigGenerator(k)) {
QString path = buildPath.path(); const QString path = buildPath.path();
path = path.left(path.lastIndexOf(QString("-%1").arg(bcName))); buildPath = buildPath.withNewPath(path.left(path.lastIndexOf(QString("-%1").arg(bcName))));
buildPath.setPath(path);
} }
return buildPath; return buildPath;

View File

@@ -167,7 +167,7 @@ void StackFrame::fixQrcFrame(const DebuggerRunParameters &rp)
relativePath = relativePath.right(relativePath.size() - 5); relativePath = relativePath.right(relativePath.size() - 5);
while (relativePath.startsWith('/')) while (relativePath.startsWith('/'))
relativePath = relativePath.mid(1); relativePath = relativePath.mid(1);
relativeFile.setPath(relativePath); relativeFile = relativeFile.withNewPath(relativePath);
FilePath absFile = findFile(rp.projectSourceDirectory, relativeFile); FilePath absFile = findFile(rp.projectSourceDirectory, relativeFile);
if (absFile.isEmpty()) if (absFile.isEmpty())

View File

@@ -590,10 +590,7 @@ FilePath DockerDevice::mapToGlobalPath(const FilePath &pathOnDevice) const
return pathOnDevice; return pathOnDevice;
} }
FilePath result; return FilePath::fromParts(Constants::DOCKER_DEVICE_SCHEME, d->m_data.repoAndTag(), pathOnDevice.path());
result.setPath(pathOnDevice.path());
result.setScheme(Constants::DOCKER_DEVICE_SCHEME);
result.setHost(d->m_data.repoAndTag());
// The following would work, but gives no hint on repo and tag // The following would work, but gives no hint on repo and tag
// result.setScheme("docker"); // result.setScheme("docker");
@@ -602,8 +599,6 @@ FilePath DockerDevice::mapToGlobalPath(const FilePath &pathOnDevice) const
// The following would work, but gives no hint on repo, tag and imageid // The following would work, but gives no hint on repo, tag and imageid
// result.setScheme("device"); // result.setScheme("device");
// result.setHost(id().toString()); // result.setHost(id().toString());
return result;
} }
QString DockerDevice::mapToDevicePath(const Utils::FilePath &globalPath) const QString DockerDevice::mapToDevicePath(const Utils::FilePath &globalPath) const
@@ -621,11 +616,7 @@ QString DockerDevice::mapToDevicePath(const Utils::FilePath &globalPath) const
Utils::FilePath DockerDevice::rootPath() const Utils::FilePath DockerDevice::rootPath() const
{ {
FilePath root; return FilePath::fromParts(Constants::DOCKER_DEVICE_SCHEME, d->m_data.repoAndTag(), u"/");
root.setScheme(Constants::DOCKER_DEVICE_SCHEME);
root.setHost(d->m_data.repoAndTag());
root.setPath(u"/");
return root;
} }
bool DockerDevice::handlesFile(const FilePath &filePath) const bool DockerDevice::handlesFile(const FilePath &filePath) const

View File

@@ -196,11 +196,7 @@ FilePath IDevice::mapToGlobalPath(const FilePath &pathOnDevice) const
return pathOnDevice; return pathOnDevice;
} }
// match DeviceManager::deviceForPath // match DeviceManager::deviceForPath
FilePath result; return FilePath::fromParts(u"device", id().toString(), pathOnDevice.path());
result.setPath(pathOnDevice.path());
result.setScheme(u"device");
result.setHost(id().toString());
return result;
} }
QString IDevice::mapToDevicePath(const FilePath &globalPath) const QString IDevice::mapToDevicePath(const FilePath &globalPath) const
@@ -758,11 +754,7 @@ void IDevice::setMachineType(MachineType machineType)
FilePath IDevice::rootPath() const FilePath IDevice::rootPath() const
{ {
FilePath root; return FilePath::fromParts(u"device", id().toString(), u"/");
root.setScheme(u"device");
root.setHost(id().toString());
root.setPath(u"/");
return root;
} }
FilePath IDevice::debugServerPath() const FilePath IDevice::debugServerPath() const

View File

@@ -82,7 +82,8 @@ FilePath ProcessParameters::effectiveWorkingDirectory() const
QString path = m_workingDirectory.path(); QString path = m_workingDirectory.path();
if (m_macroExpander) if (m_macroExpander)
path = m_macroExpander->expand(path); path = m_macroExpander->expand(path);
m_effectiveWorkingDirectory.setPath(QDir::cleanPath(m_environment.expandVariables(path))); m_effectiveWorkingDirectory =
m_effectiveWorkingDirectory.withNewPath(QDir::cleanPath(m_environment.expandVariables(path)));
} }
return m_effectiveWorkingDirectory; return m_effectiveWorkingDirectory;
} }

View File

@@ -233,12 +233,10 @@ FilePath WorkingDirectoryAspect::workingDirectory() const
{ {
const Environment env = m_envAspect ? m_envAspect->environment() const Environment env = m_envAspect ? m_envAspect->environment()
: Environment::systemEnvironment(); : Environment::systemEnvironment();
FilePath res = m_workingDirectory;
QString workingDir = m_workingDirectory.path(); QString workingDir = m_workingDirectory.path();
if (m_macroExpander) if (m_macroExpander)
workingDir = m_macroExpander->expandProcessArgs(workingDir); workingDir = m_macroExpander->expandProcessArgs(workingDir);
res.setPath(PathChooser::expandedDirectory(workingDir, env, QString())); return m_workingDirectory.withNewPath(PathChooser::expandedDirectory(workingDir, env, QString()));
return res;
} }
FilePath WorkingDirectoryAspect::defaultWorkingDirectory() const FilePath WorkingDirectoryAspect::defaultWorkingDirectory() const

View File

@@ -780,8 +780,7 @@ FilePath QmakeBuildSystem::buildDir(const FilePath &proFilePath) const
? projectDirectory() ? projectDirectory()
: buildConfigBuildDir; : buildConfigBuildDir;
// FIXME: Convoluted. // FIXME: Convoluted.
buildDir.setPath(QDir::cleanPath(QDir(buildDir.path()).absoluteFilePath(relativeDir))); return buildDir.withNewPath(QDir::cleanPath(QDir(buildDir.path()).absoluteFilePath(relativeDir)));
return buildDir;
} }
void QmakeBuildSystem::proFileParseError(const QString &errorMessage, const FilePath &filePath) void QmakeBuildSystem::proFileParseError(const QString &errorMessage, const FilePath &filePath)

View File

@@ -716,7 +716,7 @@ void QtVersion::fromMap(const QVariantMap &map)
QString string = d->m_qmakeCommand.path(); QString string = d->m_qmakeCommand.path();
if (string.startsWith('~')) if (string.startsWith('~'))
string.remove(0, 1).prepend(QDir::homePath()); string.remove(0, 1).prepend(QDir::homePath());
qmake.setPath(string); qmake = qmake.withNewPath(string);
if (!d->m_qmakeCommand.needsDevice()) { if (!d->m_qmakeCommand.needsDevice()) {
if (BuildableHelperLibrary::isQtChooser(qmake)) { if (BuildableHelperLibrary::isQtChooser(qmake)) {
// we don't want to treat qtchooser as a normal qmake // we don't want to treat qtchooser as a normal qmake

View File

@@ -730,8 +730,7 @@ CommandLine SshProcessInterfacePrivate::fullLocalCommandLine() const
cmd.addArgs(options); cmd.addArgs(options);
CommandLine remoteWithLocalPath = q->m_setup.m_commandLine; CommandLine remoteWithLocalPath = q->m_setup.m_commandLine;
FilePath executable; FilePath executable = FilePath::fromParts({}, {}, remoteWithLocalPath.executable().path());
executable.setPath(remoteWithLocalPath.executable().path());
remoteWithLocalPath.setExecutable(executable); remoteWithLocalPath.setExecutable(executable);
cmd.addArg(q->fullCommandLine(remoteWithLocalPath)); cmd.addArg(q->fullCommandLine(remoteWithLocalPath));
@@ -1028,11 +1027,7 @@ QString LinuxDevice::userAtHost() const
FilePath LinuxDevice::rootPath() const FilePath LinuxDevice::rootPath() const
{ {
FilePath root; return FilePath::fromParts(u"ssh", userAtHost(), u"/");
root.setScheme(u"ssh");
root.setHost(userAtHost());
root.setPath(u"/");
return root;
} }
bool LinuxDevice::handlesFile(const FilePath &filePath) const bool LinuxDevice::handlesFile(const FilePath &filePath) const
@@ -1515,7 +1510,8 @@ private:
m_batchFile->write("-rm " + ProcessArgs::quoteArgUnix( m_batchFile->write("-rm " + ProcessArgs::quoteArgUnix(
file.m_target.path()).toLocal8Bit() + '\n'); file.m_target.path()).toLocal8Bit() + '\n');
// see QTBUG-5817. // see QTBUG-5817.
sourceFileOrLinkTarget.setPath(fi.dir().relativeFilePath(fi.symLinkTarget())); sourceFileOrLinkTarget =
sourceFileOrLinkTarget.withNewPath(fi.dir().relativeFilePath(fi.symLinkTarget()));
} }
} }
m_batchFile->write(transferCommand(direction(), link) + ' ' m_batchFile->write(transferCommand(direction(), link) + ' '
@@ -1597,8 +1593,10 @@ private:
} }
FileToTransfer fixedFile = file; FileToTransfer fixedFile = file;
(direction() == FileTransferDirection::Upload) ? fixedFile.m_source.setPath(localFilePath) if (direction() == FileTransferDirection::Upload)
: fixedFile.m_target.setPath(localFilePath); fixedFile.m_source = fixedFile.m_source.withNewPath(localFilePath);
else
fixedFile.m_target = fixedFile.m_target.withNewPath(localFilePath);
return fixedFile; return fixedFile;
} }

View File

@@ -771,7 +771,7 @@ void MemcheckToolPrivate::heobAction()
QString executablePath = executable.path(); QString executablePath = executable.path();
if (executablePath.startsWith(wdSlashed, Qt::CaseInsensitive)) { if (executablePath.startsWith(wdSlashed, Qt::CaseInsensitive)) {
executablePath.remove(0, wdSlashed.size()); executablePath.remove(0, wdSlashed.size());
executable.setPath(executablePath); executable = executable.withNewPath(executablePath);
} }
// heob arguments // heob arguments

View File

@@ -314,11 +314,7 @@ void tst_fileutils::toString()
QFETCH(QString, result); QFETCH(QString, result);
QFETCH(QString, userResult); QFETCH(QString, userResult);
FilePath filePath; FilePath filePath = FilePath::fromParts(scheme, host, path);
filePath.setScheme(scheme);
filePath.setHost(host);
filePath.setPath(path);
QCOMPARE(filePath.toString(), result); QCOMPARE(filePath.toString(), result);
QString cleanedOutput = filePath.needsDevice() ? filePath.toUserOutput() : QDir::cleanPath(filePath.toUserOutput()); QString cleanedOutput = filePath.needsDevice() ? filePath.toUserOutput() : QDir::cleanPath(filePath.toUserOutput());
QCOMPARE(cleanedOutput, userResult); QCOMPARE(cleanedOutput, userResult);
@@ -428,15 +424,7 @@ void tst_fileutils::fromToString()
QCOMPARE(filePath.host(), host); QCOMPARE(filePath.host(), host);
QCOMPARE(filePath.path(), path); QCOMPARE(filePath.path(), path);
FilePath copy = FilePath::fromParts(scheme, host, path);
FilePath copy = filePath;
copy.setHost(host);
QCOMPARE(copy.toString(), full);
copy.setScheme(scheme);
QCOMPARE(copy.toString(), full);
copy.setPath(path);
QCOMPARE(copy.toString(), full); QCOMPARE(copy.toString(), full);
} }