File: cpp_include_manager.cxx

package info (click to toggle)
libreoffice 4%3A26.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,833,120 kB
  • sloc: cpp: 4,395,780; xml: 499,109; java: 254,438; python: 81,820; ansic: 33,823; perl: 30,297; javascript: 19,722; sh: 12,050; makefile: 10,854; cs: 8,865; yacc: 8,549; objc: 2,131; lex: 1,385; asm: 1,231; awk: 996; pascal: 914; csh: 20; sed: 5
file content (174 lines) | stat: -rw-r--r-- 5,805 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* -*- 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();
        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();
    return OString("\"" + result + "\"");
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */