Files
odfstudio/codemaker/source/rustmaker/cpp_include_manager.cxx
T
Stephan Bergmann 0e2ad911d9 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
<https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=341ad729c2c93b9f01854913b0404e4fc8958512>
"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<char> 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
<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p2319r5.html>
"Prevent path presentation problems" and
<https://cplusplus.github.io/LWG/issue4512> "The system_encoded_string() and
generic_system_encoded_string() member functions of std::filesystem::path are
misnamed" for details.)

Signed-off-by: Stephan Bergmann <stephan.bergmann@allotropia.de>
Change-Id: I7c79c8522608550bb1e0f4cb8e4824480952446b
Reviewed-on: https://gerrit.collaboraoffice.com/c/online/+/3987
Reviewed-by: Noel Grandin <noel.grandin@collabora.com>
Tested-by: Jenkins CPCI <releng@collaboraoffice.com>
(cherry picked from commit f320d95d89ecc9bea81182795969f83b05ec903c)
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/206226
Tested-by: Jenkins
Reviewed-by: Xisco Fauli <xiscofauli@libreoffice.org>
2026-06-09 09:52:05 +02:00

175 lines
5.7 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "cpp_include_manager.hxx"
#include "rustfile.hxx"
#include <filesystem>
#include <algorithm>
#include <sal/log.hxx>
CppIncludeManager::CppIncludeManager(const TypeAnalyzer::TypeInfo& typeInfo)
: m_typeInfo(typeInfo)
{
}
void CppIncludeManager::dumpIncludes(CppFile& file, std::string_view interfaceName)
{
// Write file header comment
file.beginLine()
.append("// Auto-generated C++ bridge functions for ")
.append(interfaceName)
.endLine()
.beginLine()
.append("// Generated by rustmaker with dynamic includes")
.endLine()
.endLine();
// Write pragma once directive to prevent multiple inclusions (only for header files)
if (file.getExtension() == ".hxx")
{
file.beginLine().append("#pragma once").endLine().endLine();
}
// Write standard includes
writeStandardIncludes(file);
// Write UNO-specific includes based on type analysis
writeUnoIncludes(file);
// Write interface-specific includes
writeInterfaceIncludes(file, interfaceName);
file.endLine();
}
void CppIncludeManager::dumpNamespaces(CppFile& file)
{
// Generate specific namespace using declarations for only what we need
for (const auto& ns : m_typeInfo.namespaces)
{
file.beginLine().append("using namespace ").append(ns).append(";").endLine();
}
file.endLine();
}
void CppIncludeManager::writeStandardIncludes(CppFile& file)
{
// Always needed for UNO operations
if (m_typeInfo.needsUnoInterface)
{
file.beginLine().append("#include <com/sun/star/uno/XInterface.hpp>").endLine();
file.beginLine().append("#include <com/sun/star/uno/Reference.hxx>").endLine();
file.beginLine().append("#include <com/sun/star/uno/Exception.hpp>").endLine();
file.beginLine().append("#include <sal/log.hxx>").endLine(); // For SAL_WARN macro
}
// Basic type includes
if (m_typeInfo.needsSalTypes)
{
file.beginLine().append("#include <sal/types.h>").endLine();
}
if (m_typeInfo.needsRtlUstring)
{
file.beginLine().append("#include <rtl/ustring.hxx>").endLine();
}
}
void CppIncludeManager::writeUnoIncludes(CppFile& file)
{
// Core UNO types - implemented: Any
if (m_typeInfo.needsAny)
{
file.beginLine().append("#include <com/sun/star/uno/Any.hxx>").endLine();
file.beginLine().append("#include <uno/data.h>").endLine();
file.beginLine().append("#include <cppu/unotype.hxx>").endLine();
}
if (m_typeInfo.needsSequence)
{
file.beginLine().append("#include <com/sun/star/uno/Sequence.hxx>").endLine();
file.beginLine().append("#include <uno/sequence2.h>").endLine();
file.beginLine().append("#include <typelib/typedescription.h>").endLine();
}
if (m_typeInfo.needsPropertyValue)
{
// TODO: implement PropertyValue handling
file.beginLine().append("// TODO: Add PropertyValue includes").endLine();
file.beginLine().append("#include <com/sun/star/beans/PropertyValue.hpp>").endLine();
}
}
void CppIncludeManager::writeInterfaceIncludes(CppFile& file, std::string_view interfaceName)
{
// Include the interface being processed
OString selfInclude = "<" + OString(interfaceName).replaceAll("."_ostr, "/"_ostr) + ".hpp>";
file.beginLine().append("#include ").append(selfInclude).endLine();
// Include headers for all interface types found in the analysis
for (const auto& interfaceType : m_typeInfo.interfaceTypes)
{
// Don't include ourselves twice
if (interfaceType != interfaceName)
{
OString includeFile = "<" + interfaceType.replaceAll("."_ostr, "/"_ostr) + ".hpp>";
file.beginLine().append("#include ").append(includeFile).endLine();
}
}
}
OString CppIncludeManager::calculateRelativeIncludePath(const CppFile& file,
const OString& targetType,
const std::string& extension)
{
namespace fs = std::filesystem;
// Get the current file's directory
fs::path currentFilePath = file.getPath();
fs::path currentDir = currentFilePath.parent_path();
// Convert target type name to path (dots to slashes)
std::string targetTypeStr(targetType);
std::replace(targetTypeStr.begin(), targetTypeStr.end(), '.', '/');
// Find the base directory (cpp)
fs::path baseDir = currentDir;
while (baseDir.has_parent_path() && baseDir.filename() != "cpp")
{
baseDir = baseDir.parent_path();
}
// Create target file path
fs::path target = (baseDir / (targetTypeStr + extension)).lexically_normal();
// If they're in the same directory, just use the filename
if (target.parent_path() == currentDir)
{
std::string result = target.filename().string<char>();
return OString("\"" + result + "\"");
}
// Otherwise calculate relative path from current directory to target file
fs::path rel;
try
{
rel = fs::relative(target, currentDir);
}
catch (const std::exception&)
{
// Fallback: use the path relative to base directory
rel = fs::relative(target, baseDir);
}
std::string result = rel.generic_string<char>();
return OString("\"" + result + "\"");
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */