diff --git a/src/libs/qmljs/qmljsdocument.cpp b/src/libs/qmljs/qmljsdocument.cpp index 74211314bc0..e5395ad532e 100644 --- a/src/libs/qmljs/qmljsdocument.cpp +++ b/src/libs/qmljs/qmljsdocument.cpp @@ -503,15 +503,14 @@ void Snapshot::insertLibraryInfo(const Utils::FilePath &path, const LibraryInfo break; ImportKey iKey(ImportType::Library, QStringList(myPath.mid(iPath)).join(QLatin1Char('.')), importKey.majorVersion, importKey.minorVersion); - Utils::FilePath newP(path); - newP.setPath((iPath == 1) + Utils::FilePath newP = path.withNewPath( + (iPath == 1) ? QLatin1String("/") : QStringList(myPath.mid(0, iPath)).join(QLatin1Char('/'))); cImport.addPossibleExport(Export(iKey, newP, true)); } } else { - Utils::FilePath requiredPath(path); - requiredPath.setPath( + Utils::FilePath requiredPath = path.withNewPath( QStringList(splitPath.mid(0, splitPath.size() - importKey.splitPath.size())) .join(QLatin1String("/"))); cImport.addPossibleExport(Export(importKey, requiredPath, true)); @@ -549,8 +548,8 @@ void Snapshot::insertLibraryInfo(const Utils::FilePath &path, const LibraryInfo break; ImportKey iKey(ImportType::Library, QStringList(splitPath.mid(iPath)).join(QLatin1Char('.')), majorVersion, minorVersion); - Utils::FilePath newP(path); - newP.setPath((iPath == 1) + Utils::FilePath newP = path.withNewPath( + iPath == 1 ? QLatin1String("/") : QStringList(splitPath.mid(0, iPath)).join(QLatin1Char('/'))); cImport.addPossibleExport(Export(iKey, newP, true)); diff --git a/src/libs/qmljs/qmljsmodelmanagerinterface.cpp b/src/libs/qmljs/qmljsmodelmanagerinterface.cpp index ff7e7f32ac9..60c1906b0a4 100644 --- a/src/libs/qmljs/qmljsmodelmanagerinterface.cpp +++ b/src/libs/qmljs/qmljsmodelmanagerinterface.cpp @@ -1200,7 +1200,7 @@ static QList minimalPrefixPaths(const QList &p // find minimal prefix, ensure '/' at end for (Utils::FilePath path : qAsConst(paths)) { if (!path.endsWith("/")) - path.setPath(QString(path.path() + "/")); + path = path.withNewPath(path.path() + "/"); if (path.path().length() > 1) sortedPaths.append(path); } diff --git a/src/libs/utils/buildablehelperlibrary.cpp b/src/libs/utils/buildablehelperlibrary.cpp index f0511623601..0a2459adc10 100644 --- a/src/libs/utils/buildablehelperlibrary.cpp +++ b/src/libs/utils/buildablehelperlibrary.cpp @@ -37,9 +37,7 @@ FilePath BuildableHelperLibrary::qtChooserToQmakePath(const FilePath &qtChooser) if (end == -1) return {}; - FilePath qmake = qtChooser; - qmake.setPath(QString(output.mid(pos, end - pos) + "/qmake")); - return qmake; + return qtChooser.withNewPath(output.mid(pos, end - pos) + "/qmake"); } static bool isQmake(FilePath path) diff --git a/src/libs/utils/filepath.cpp b/src/libs/utils/filepath.cpp index 93eaac52351..5a354287caf 100644 --- a/src/libs/utils/filepath.cpp +++ b/src/libs/utils/filepath.cpp @@ -150,11 +150,14 @@ QFileInfo FilePath::toFileInfo() const FilePath FilePath::fromUrl(const QUrl &url) { - FilePath fn; - fn.m_scheme = url.scheme(); - fn.m_host = url.host(); - fn.setRootAndPath(url.path(), HostOsInfo::hostOs()); - return fn; + return FilePath::fromParts(url.scheme(), url.host(), url.path()); +} + +FilePath FilePath::fromParts(const QStringView scheme, const QStringView host, const QStringView path) +{ + FilePath result; + result.setParts(scheme, host, path); + return result; } FilePath FilePath::currentWorkingPath() @@ -318,22 +321,6 @@ QString FilePath::completeSuffix() const 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 { return m_scheme; @@ -354,9 +341,12 @@ QStringView FilePath::root() const 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 @@ -629,9 +619,7 @@ QString FilePath::mapToDevicePath() const FilePath FilePath::withExecutableSuffix() const { - FilePath res = *this; - res.setPath(OsSpecificAspects::withExecutableSuffix(osType(), path())); - return res; + return withNewPath(OsSpecificAspects::withExecutableSuffix(osType(), path())); } /// Find the parent directory of a given directory. @@ -655,9 +643,7 @@ FilePath FilePath::parentDir() const const QString parent = doCleanPath(path); QTC_ASSERT(parent != path, return FilePath()); - FilePath result = *this; - result.setPath(parent); - return result; + return withNewPath(parent); } FilePath FilePath::absolutePath() const @@ -1568,7 +1554,7 @@ void FilePath::clear() */ 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 { - return m_root.isEmpty(); + return root().isEmpty(); } /*! @@ -1606,9 +1592,9 @@ bool FilePath::isRelativePath() const */ FilePath FilePath::resolvePath(const FilePath &tail) const { - if (!tail.m_root.isEmpty()) + if (!tail.root().isEmpty()) 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 result = *this; - result.setPath(doCleanPath(result.path())); - return result; + return withNewPath(doCleanPath(path())); } QTextStream &operator<<(QTextStream &s, const FilePath &fn) diff --git a/src/libs/utils/filepath.h b/src/libs/utils/filepath.h index 64cc7d0ab18..2f8e1d0e1e1 100644 --- a/src/libs/utils/filepath.h +++ b/src/libs/utils/filepath.h @@ -56,11 +56,11 @@ public: [[nodiscard]] static FilePath fromUtf8(const char *filepath, int filepathSize = -1); [[nodiscard]] static FilePath fromVariant(const QVariant &variant); [[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 rootPath(); - QString toUserOutput() const; QString toString() const; QString toFSPathString() const; @@ -68,16 +68,11 @@ public: QUrl toUrl() const; QStringView scheme() const; - void setScheme(const QStringView scheme); - QStringView host() const; - void setHost(const QStringView host); - QString path() const; - void setPath(const QStringView path); - QStringView root() const; - void setRoot(const QStringView root); + + void setParts(const QStringView scheme, const QStringView host, const QStringView path); QString fileName() const; QString fileNameWithPathComponents(int pathComponents) const; diff --git a/src/libs/utils/fileutils.cpp b/src/libs/utils/fileutils.cpp index 8a89e54cd7d..74734a00252 100644 --- a/src/libs/utils/fileutils.cpp +++ b/src/libs/utils/fileutils.cpp @@ -392,7 +392,7 @@ FilePath FileUtils::commonPath(const FilePaths &paths) }; if (!allOf(others, sameScheme)) return result; - result.setScheme(commonScheme); + result.setParts(commonScheme, {}, {}); // Common host const QStringView commonHost = first.host(); @@ -401,7 +401,7 @@ FilePath FileUtils::commonPath(const FilePaths &paths) }; if (!allOf(others, sameHost)) return result; - result.setHost(commonHost); + result.setParts(commonScheme, commonHost, {}); // Common path QString commonPath; @@ -413,7 +413,7 @@ FilePath FileUtils::commonPath(const FilePaths &paths) commonPath += segment + '/'; if (!allOf(others, sameBasePath)) return result; - result.setPath(commonPath.chopped(1)); + result.setParts(commonScheme, commonHost, commonPath.chopped(1)); } return result; diff --git a/src/libs/utils/macroexpander.cpp b/src/libs/utils/macroexpander.cpp index 339a825c35b..6e8d9e85c0c 100644 --- a/src/libs/utils/macroexpander.cpp +++ b/src/libs/utils/macroexpander.cpp @@ -271,11 +271,10 @@ QString MacroExpander::expand(const QString &stringWithVariables) const FilePath MacroExpander::expand(const FilePath &fileNameWithVariables) const { - FilePath result = fileNameWithVariables; - result.setPath(expand(result.path())); - result.setHost(expand(result.host().toString())); - result.setScheme(expand(result.scheme().toString())); - return result; + return FilePath::fromParts( + expand(fileNameWithVariables.scheme().toString()), + expand(fileNameWithVariables.host().toString()), + expand(fileNameWithVariables.path())); } QByteArray MacroExpander::expand(const QByteArray &stringWithVariables) const diff --git a/src/plugins/cmakeprojectmanager/cmakebuildconfiguration.cpp b/src/plugins/cmakeprojectmanager/cmakebuildconfiguration.cpp index 36736b6f9de..824976dc26e 100644 --- a/src/plugins/cmakeprojectmanager/cmakebuildconfiguration.cpp +++ b/src/plugins/cmakeprojectmanager/cmakebuildconfiguration.cpp @@ -1439,9 +1439,8 @@ FilePath CMakeBuildConfiguration::shadowBuildDirectory(const FilePath &projectFi bcName, buildType, "cmake"); if (CMakeGeneratorKitAspect::isMultiConfigGenerator(k)) { - QString path = buildPath.path(); - path = path.left(path.lastIndexOf(QString("-%1").arg(bcName))); - buildPath.setPath(path); + const QString path = buildPath.path(); + buildPath = buildPath.withNewPath(path.left(path.lastIndexOf(QString("-%1").arg(bcName)))); } return buildPath; diff --git a/src/plugins/debugger/stackframe.cpp b/src/plugins/debugger/stackframe.cpp index 55dc5cfc00c..1a68e4ae3a0 100644 --- a/src/plugins/debugger/stackframe.cpp +++ b/src/plugins/debugger/stackframe.cpp @@ -167,7 +167,7 @@ void StackFrame::fixQrcFrame(const DebuggerRunParameters &rp) relativePath = relativePath.right(relativePath.size() - 5); while (relativePath.startsWith('/')) relativePath = relativePath.mid(1); - relativeFile.setPath(relativePath); + relativeFile = relativeFile.withNewPath(relativePath); FilePath absFile = findFile(rp.projectSourceDirectory, relativeFile); if (absFile.isEmpty()) diff --git a/src/plugins/docker/dockerdevice.cpp b/src/plugins/docker/dockerdevice.cpp index c3fabc009d5..46dba6aa49d 100644 --- a/src/plugins/docker/dockerdevice.cpp +++ b/src/plugins/docker/dockerdevice.cpp @@ -590,10 +590,7 @@ FilePath DockerDevice::mapToGlobalPath(const FilePath &pathOnDevice) const return pathOnDevice; } - FilePath result; - result.setPath(pathOnDevice.path()); - result.setScheme(Constants::DOCKER_DEVICE_SCHEME); - result.setHost(d->m_data.repoAndTag()); + return FilePath::fromParts(Constants::DOCKER_DEVICE_SCHEME, d->m_data.repoAndTag(), pathOnDevice.path()); // The following would work, but gives no hint on repo and tag // 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 // result.setScheme("device"); // result.setHost(id().toString()); - - return result; } 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 { - FilePath root; - root.setScheme(Constants::DOCKER_DEVICE_SCHEME); - root.setHost(d->m_data.repoAndTag()); - root.setPath(u"/"); - return root; + return FilePath::fromParts(Constants::DOCKER_DEVICE_SCHEME, d->m_data.repoAndTag(), u"/"); } bool DockerDevice::handlesFile(const FilePath &filePath) const diff --git a/src/plugins/projectexplorer/devicesupport/idevice.cpp b/src/plugins/projectexplorer/devicesupport/idevice.cpp index e32afad641b..8cfa6939d83 100644 --- a/src/plugins/projectexplorer/devicesupport/idevice.cpp +++ b/src/plugins/projectexplorer/devicesupport/idevice.cpp @@ -196,11 +196,7 @@ FilePath IDevice::mapToGlobalPath(const FilePath &pathOnDevice) const return pathOnDevice; } // match DeviceManager::deviceForPath - FilePath result; - result.setPath(pathOnDevice.path()); - result.setScheme(u"device"); - result.setHost(id().toString()); - return result; + return FilePath::fromParts(u"device", id().toString(), pathOnDevice.path()); } QString IDevice::mapToDevicePath(const FilePath &globalPath) const @@ -758,11 +754,7 @@ void IDevice::setMachineType(MachineType machineType) FilePath IDevice::rootPath() const { - FilePath root; - root.setScheme(u"device"); - root.setHost(id().toString()); - root.setPath(u"/"); - return root; + return FilePath::fromParts(u"device", id().toString(), u"/"); } FilePath IDevice::debugServerPath() const diff --git a/src/plugins/projectexplorer/processparameters.cpp b/src/plugins/projectexplorer/processparameters.cpp index b184e099445..a2e4dc28b44 100644 --- a/src/plugins/projectexplorer/processparameters.cpp +++ b/src/plugins/projectexplorer/processparameters.cpp @@ -82,7 +82,8 @@ FilePath ProcessParameters::effectiveWorkingDirectory() const QString path = m_workingDirectory.path(); if (m_macroExpander) 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; } diff --git a/src/plugins/projectexplorer/runconfigurationaspects.cpp b/src/plugins/projectexplorer/runconfigurationaspects.cpp index 1540c63d50b..16bd230664e 100644 --- a/src/plugins/projectexplorer/runconfigurationaspects.cpp +++ b/src/plugins/projectexplorer/runconfigurationaspects.cpp @@ -233,12 +233,10 @@ FilePath WorkingDirectoryAspect::workingDirectory() const { const Environment env = m_envAspect ? m_envAspect->environment() : Environment::systemEnvironment(); - FilePath res = m_workingDirectory; QString workingDir = m_workingDirectory.path(); if (m_macroExpander) workingDir = m_macroExpander->expandProcessArgs(workingDir); - res.setPath(PathChooser::expandedDirectory(workingDir, env, QString())); - return res; + return m_workingDirectory.withNewPath(PathChooser::expandedDirectory(workingDir, env, QString())); } FilePath WorkingDirectoryAspect::defaultWorkingDirectory() const diff --git a/src/plugins/qmakeprojectmanager/qmakeproject.cpp b/src/plugins/qmakeprojectmanager/qmakeproject.cpp index 1f68a01e462..dd030ad98a0 100644 --- a/src/plugins/qmakeprojectmanager/qmakeproject.cpp +++ b/src/plugins/qmakeprojectmanager/qmakeproject.cpp @@ -780,8 +780,7 @@ FilePath QmakeBuildSystem::buildDir(const FilePath &proFilePath) const ? projectDirectory() : buildConfigBuildDir; // FIXME: Convoluted. - buildDir.setPath(QDir::cleanPath(QDir(buildDir.path()).absoluteFilePath(relativeDir))); - return buildDir; + return buildDir.withNewPath(QDir::cleanPath(QDir(buildDir.path()).absoluteFilePath(relativeDir))); } void QmakeBuildSystem::proFileParseError(const QString &errorMessage, const FilePath &filePath) diff --git a/src/plugins/qtsupport/baseqtversion.cpp b/src/plugins/qtsupport/baseqtversion.cpp index 92dc355cfce..8bef5b61c0e 100644 --- a/src/plugins/qtsupport/baseqtversion.cpp +++ b/src/plugins/qtsupport/baseqtversion.cpp @@ -716,7 +716,7 @@ void QtVersion::fromMap(const QVariantMap &map) QString string = d->m_qmakeCommand.path(); if (string.startsWith('~')) string.remove(0, 1).prepend(QDir::homePath()); - qmake.setPath(string); + qmake = qmake.withNewPath(string); if (!d->m_qmakeCommand.needsDevice()) { if (BuildableHelperLibrary::isQtChooser(qmake)) { // we don't want to treat qtchooser as a normal qmake diff --git a/src/plugins/remotelinux/linuxdevice.cpp b/src/plugins/remotelinux/linuxdevice.cpp index 9fc6e88b97f..d6920f7bc56 100644 --- a/src/plugins/remotelinux/linuxdevice.cpp +++ b/src/plugins/remotelinux/linuxdevice.cpp @@ -730,8 +730,7 @@ CommandLine SshProcessInterfacePrivate::fullLocalCommandLine() const cmd.addArgs(options); CommandLine remoteWithLocalPath = q->m_setup.m_commandLine; - FilePath executable; - executable.setPath(remoteWithLocalPath.executable().path()); + FilePath executable = FilePath::fromParts({}, {}, remoteWithLocalPath.executable().path()); remoteWithLocalPath.setExecutable(executable); cmd.addArg(q->fullCommandLine(remoteWithLocalPath)); @@ -1028,11 +1027,7 @@ QString LinuxDevice::userAtHost() const FilePath LinuxDevice::rootPath() const { - FilePath root; - root.setScheme(u"ssh"); - root.setHost(userAtHost()); - root.setPath(u"/"); - return root; + return FilePath::fromParts(u"ssh", userAtHost(), u"/"); } bool LinuxDevice::handlesFile(const FilePath &filePath) const @@ -1515,7 +1510,8 @@ private: m_batchFile->write("-rm " + ProcessArgs::quoteArgUnix( file.m_target.path()).toLocal8Bit() + '\n'); // see QTBUG-5817. - sourceFileOrLinkTarget.setPath(fi.dir().relativeFilePath(fi.symLinkTarget())); + sourceFileOrLinkTarget = + sourceFileOrLinkTarget.withNewPath(fi.dir().relativeFilePath(fi.symLinkTarget())); } } m_batchFile->write(transferCommand(direction(), link) + ' ' @@ -1597,8 +1593,10 @@ private: } FileToTransfer fixedFile = file; - (direction() == FileTransferDirection::Upload) ? fixedFile.m_source.setPath(localFilePath) - : fixedFile.m_target.setPath(localFilePath); + if (direction() == FileTransferDirection::Upload) + fixedFile.m_source = fixedFile.m_source.withNewPath(localFilePath); + else + fixedFile.m_target = fixedFile.m_target.withNewPath(localFilePath); return fixedFile; } diff --git a/src/plugins/valgrind/memchecktool.cpp b/src/plugins/valgrind/memchecktool.cpp index 9f05880c067..a8c62b3e41f 100644 --- a/src/plugins/valgrind/memchecktool.cpp +++ b/src/plugins/valgrind/memchecktool.cpp @@ -771,7 +771,7 @@ void MemcheckToolPrivate::heobAction() QString executablePath = executable.path(); if (executablePath.startsWith(wdSlashed, Qt::CaseInsensitive)) { executablePath.remove(0, wdSlashed.size()); - executable.setPath(executablePath); + executable = executable.withNewPath(executablePath); } // heob arguments diff --git a/tests/auto/utils/fileutils/tst_fileutils.cpp b/tests/auto/utils/fileutils/tst_fileutils.cpp index 798e0357a92..c8dfe3ed451 100644 --- a/tests/auto/utils/fileutils/tst_fileutils.cpp +++ b/tests/auto/utils/fileutils/tst_fileutils.cpp @@ -314,11 +314,7 @@ void tst_fileutils::toString() QFETCH(QString, result); QFETCH(QString, userResult); - FilePath filePath; - filePath.setScheme(scheme); - filePath.setHost(host); - filePath.setPath(path); - + FilePath filePath = FilePath::fromParts(scheme, host, path); QCOMPARE(filePath.toString(), result); QString cleanedOutput = filePath.needsDevice() ? filePath.toUserOutput() : QDir::cleanPath(filePath.toUserOutput()); QCOMPARE(cleanedOutput, userResult); @@ -428,15 +424,7 @@ void tst_fileutils::fromToString() QCOMPARE(filePath.host(), host); QCOMPARE(filePath.path(), path); - - FilePath copy = filePath; - copy.setHost(host); - QCOMPARE(copy.toString(), full); - - copy.setScheme(scheme); - QCOMPARE(copy.toString(), full); - - copy.setPath(path); + FilePath copy = FilePath::fromParts(scheme, host, path); QCOMPARE(copy.toString(), full); }