From 0e2ad911d9cf4eef1fb1480c38a395296b92a07f Mon Sep 17 00:00:00 2001 From: Stephan Bergmann Date: Sun, 7 Jun 2026 10:50:46 +0200 Subject: [PATCH] Avoid deprecated std::filesystem::path::string ...and std::filesystem::path::generic_string, which at least GCC 17 trunk libstdc++ now flags for C++26 (--with-latest-c++) as > In file included from engine/codemaker/source/netmaker/netproduce.cxx:19: > engine/codemaker/source/netmaker/csharpfile.hxx:28:53: error: 'string' is deprecated: use 'display_string' or 'native_encoded_string' instead [-Werror,-Wdeprecated-declarations] > 28 | std::string getPath() const { return m_filePath.string(); } > | ^ > ~/gcc/inst/lib/gcc/x86_64-pc-linux-gnu/17.0.0/../../../../include/c++/17.0.0/bits/fs_path.h:465:5: note: 'string' has been explicitly marked deprecated here > 465 | _GLIBCXX26_DEPRECATED_SUGGEST("display_string' or 'native_encoded_string") > | ^ > ~/gcc/inst/lib/gcc/x86_64-pc-linux-gnu/17.0.0/../../../../include/c++/17.0.0/x86_64-pc-linux-gnu/bits/c++config.h:160:45: note: expanded from macro '_GLIBCXX26_DEPRECATED_SUGGEST' > 160 | # define _GLIBCXX26_DEPRECATED_SUGGEST(ALT) _GLIBCXX_DEPRECATED_SUGGEST(ALT) > | ^ > ~/gcc/inst/lib/gcc/x86_64-pc-linux-gnu/17.0.0/../../../../include/c++/17.0.0/x86_64-pc-linux-gnu/bits/c++config.h:112:19: note: expanded from macro '_GLIBCXX_DEPRECATED_SUGGEST' > 112 | __attribute__ ((__deprecated__ ("use '" ALT "' instead"))) > | ^ since "libstdc++: Add (generic_)display_string and (generic_)native_encoded_string to filesystem::path". Occurrences that are obviously meant for display purposes are #if-conditionally replaced with the new display_string, while all other occurrences are left using the (non-deprecated) [generic_]string template instantiation, matching the original code. (Where, for C++26, the latter could also be replaced with the new [generic_]native_encoded_string, aka [generic_]system_encoded_string; see "Prevent path presentation problems" and "The system_encoded_string() and generic_system_encoded_string() member functions of std::filesystem::path are misnamed" for details.) Signed-off-by: Stephan Bergmann Change-Id: I7c79c8522608550bb1e0f4cb8e4824480952446b Reviewed-on: https://gerrit.collaboraoffice.com/c/online/+/3987 Reviewed-by: Noel Grandin Tested-by: Jenkins CPCI (cherry picked from commit f320d95d89ecc9bea81182795969f83b05ec903c) Reviewed-on: https://gerrit.libreoffice.org/c/core/+/206226 Tested-by: Jenkins Reviewed-by: Xisco Fauli --- codemaker/source/netmaker/csharpfile.hxx | 9 +++++- .../source/rustmaker/cpp_include_manager.cxx | 4 +-- codemaker/source/rustmaker/rustfile.cxx | 32 +++++++++++++------ .../qa/gtktiledviewer/gtv-helpers.cxx | 2 +- 4 files changed, 34 insertions(+), 13 deletions(-) diff --git a/codemaker/source/netmaker/csharpfile.hxx b/codemaker/source/netmaker/csharpfile.hxx index 56ba30429435..2085bb22983c 100644 --- a/codemaker/source/netmaker/csharpfile.hxx +++ b/codemaker/source/netmaker/csharpfile.hxx @@ -25,7 +25,14 @@ public: } public: - std::string getPath() const { return m_filePath.string(); } + std::string getPath() const + { +#if defined __cpp_lib_format_path + return m_filePath.display_string(); +#else + return m_filePath.string(); +#endif + } void openFile() { diff --git a/codemaker/source/rustmaker/cpp_include_manager.cxx b/codemaker/source/rustmaker/cpp_include_manager.cxx index e2a63d0f7a13..2288aecc955c 100644 --- a/codemaker/source/rustmaker/cpp_include_manager.cxx +++ b/codemaker/source/rustmaker/cpp_include_manager.cxx @@ -151,7 +151,7 @@ OString CppIncludeManager::calculateRelativeIncludePath(const CppFile& file, // If they're in the same directory, just use the filename if (target.parent_path() == currentDir) { - std::string result = target.filename().string(); + std::string result = target.filename().string(); return OString("\"" + result + "\""); } @@ -167,7 +167,7 @@ OString CppIncludeManager::calculateRelativeIncludePath(const CppFile& file, rel = fs::relative(target, baseDir); } - std::string result = rel.generic_string(); + std::string result = rel.generic_string(); return OString("\"" + result + "\""); } diff --git a/codemaker/source/rustmaker/rustfile.cxx b/codemaker/source/rustmaker/rustfile.cxx index 0b2b863d87ed..0eb0b037588f 100644 --- a/codemaker/source/rustmaker/rustfile.cxx +++ b/codemaker/source/rustmaker/rustfile.cxx @@ -23,7 +23,14 @@ RustFile::RustFile(std::string_view directory, std::string_view typeName) { } -std::string RustFile::getPath() const { return m_filePath.string(); } +std::string RustFile::getPath() const +{ +#if defined __cpp_lib_format_path + return m_filePath.display_string(); +#else + return m_filePath.string(); +#endif +} void RustFile::openFile() { @@ -255,7 +262,7 @@ void RustFile::addFileToModDeclaration() { std::filesystem::path parentDir = m_filePath.parent_path(); std::filesystem::path modFile = parentDir / "mod.rs"; - std::string fileName = m_filePath.stem().string(); // Get filename without extension + std::string fileName = m_filePath.stem().string(); // Get filename without extension if (std::filesystem::exists(modFile)) { @@ -294,12 +301,12 @@ void RustFile::ensureModFileExists(const std::filesystem::path& dirPath) { if (entry.is_directory()) { - std::string dirName = entry.path().filename().string(); + std::string dirName = entry.path().filename().string(); childModules.push_back(dirName); } else if (entry.is_regular_file() && entry.path().extension() == ".rs") { - std::string fileName = entry.path().stem().string(); + std::string fileName = entry.path().stem().string(); if (fileName != "mod") // Don't include mod.rs itself { childModules.push_back(fileName); @@ -327,7 +334,7 @@ void RustFile::ensureModFileExists(const std::filesystem::path& dirPath) if (parentDir >= outputRoot) { - std::string moduleName = dirPath.filename().string(); + std::string moduleName = dirPath.filename().string(); if (parentDir.filename() == "src") { @@ -452,9 +459,16 @@ CppFile::CppFile(std::string_view directory, std::string_view typeName, std::str { } -std::string CppFile::getPathString() const { return m_filePath.string(); } +std::string CppFile::getPathString() const +{ +#if defined __cpp_lib_format_path + return m_filePath.display_string(); +#else + return m_filePath.string(); +#endif +} std::filesystem::path CppFile::getPath() const { return m_filePath; } -std::string CppFile::getExtension() const { return m_filePath.extension().string(); } +std::string CppFile::getExtension() const { return m_filePath.extension().string(); } void CppFile::openFile() { @@ -619,14 +633,14 @@ void RustFile::processDirectoryRecursively(const std::filesystem::path& currentD { if (entry.is_directory()) { - std::string dirName = entry.path().filename().string(); + std::string dirName = entry.path().filename().string(); childModules.push_back(dirName); // Recursively process subdirectory processDirectoryRecursively(entry.path()); } else if (entry.is_regular_file() && entry.path().extension() == ".rs") { - std::string fileName = entry.path().stem().string(); + std::string fileName = entry.path().stem().string(); if (fileName != "mod" && fileName != "lib") // Don't include mod.rs or lib.rs { childModules.push_back(fileName); diff --git a/libreofficekit/qa/gtktiledviewer/gtv-helpers.cxx b/libreofficekit/qa/gtktiledviewer/gtv-helpers.cxx index a5d623d31b36..fe9f15af1473 100644 --- a/libreofficekit/qa/gtktiledviewer/gtv-helpers.cxx +++ b/libreofficekit/qa/gtktiledviewer/gtv-helpers.cxx @@ -159,7 +159,7 @@ std::string GtvHelpers::getRelativePath(const std::string& aFilePath) if (err) return aFilePath; - return aRelPath.string(); + return aRelPath.string(); } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */