summaryrefslogtreecommitdiffstats
path: root/src/tools/uic/cpp/cppwriteincludes.cpp
blob: 7cf7c4e59e40c2c714d1c1646ad40da99704fee4 (plain)
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
175
176
177
178
179
180
181
182
183
184
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "cppwriteincludes.h"
#include "driver.h"
#include "ui4.h"
#include "uic.h"
#include "databaseinfo.h"

#include <qdebug.h>
#include <qfileinfo.h>
#include <qtextstream.h>

#include <stdio.h>

QT_BEGIN_NAMESPACE

using namespace Qt::StringLiterals;

enum { debugWriteIncludes = 0 };
enum { warnHeaderGeneration = 0 };

// Format a module header as 'QtCore/QObject'
static inline QString moduleHeader(const QString &module, const QString &header)
{
    QString rc = module;
    rc += u'/';
    rc += header;
    return rc;
}

namespace CPP {

WriteIncludes::WriteIncludes(Uic *uic) : WriteIncludesBase(uic),
    m_output(uic->output())
{
    // When possible (no namespace) use the "QtModule/QClass" convention
    // and create a re-mapping of the old header "qclass.h" to it. Do not do this
    // for the "Phonon::Someclass" classes, however.
    const QLatin1StringView namespaceDelimiter = "::"_L1;
    for (const auto &e : classInfoEntries()) {
        const QString klass = QLatin1StringView(e.klass);
        const QString module = QLatin1StringView(e.module);
        QLatin1StringView header = QLatin1StringView(e.header);
        if (klass.contains(namespaceDelimiter)) {
            m_classToHeader.insert(klass, moduleHeader(module, header));
        } else {
            const QString newHeader = moduleHeader(module, klass);
            m_classToHeader.insert(klass, newHeader);
            m_oldHeaderToNewHeader.insert(header, newHeader);
        }
    }
}

void WriteIncludes::acceptUI(DomUI *node)
{
    m_localIncludes.clear();
    m_globalIncludes.clear();
    m_includeBaseNames.clear();

    WriteIncludesBase::acceptUI(node);

    const auto includeFile = uic()->option().includeFile;
    if (!includeFile.isEmpty())
        m_globalIncludes.insert(includeFile);

    writeHeaders(m_globalIncludes, true);
    writeHeaders(m_localIncludes, false);

    m_output << '\n';
}

void WriteIncludes::insertIncludeForClass(const QString &className, QString header, bool global)
{
    if (debugWriteIncludes)
        fprintf(stderr, "%s %s '%s' %d\n", Q_FUNC_INFO, qPrintable(className), qPrintable(header), global);

    do {
        if (!header.isEmpty())
            break;

        // Known class
        const StringMap::const_iterator it = m_classToHeader.constFind(className);
        if (it != m_classToHeader.constEnd()) {
            header = it.value();
            global =  true;
            break;
        }

        // Quick check by class name to detect includehints provided for custom widgets.
        // Remove namespaces
        QString lowerClassName = className.toLower();
        static const auto namespaceSeparator = "::"_L1;
        const int namespaceIndex = lowerClassName.lastIndexOf(namespaceSeparator);
        if (namespaceIndex != -1)
            lowerClassName.remove(0, namespaceIndex + namespaceSeparator.size());
        if (m_includeBaseNames.contains(lowerClassName)) {
            header.clear();
            break;
        }

        // Last resort: Create default header
        if (!uic()->option().implicitIncludes)
            break;
        header = lowerClassName;
        header += ".h"_L1;
        if (warnHeaderGeneration) {
            qWarning("%s: Warning: generated header '%s' for class '%s'.",
                     qPrintable(uic()->option().messagePrefix()),
                     qPrintable(header), qPrintable(className));

        }

        global = true;
    } while (false);

    if (!header.isEmpty())
        insertInclude(header, global);
}

void WriteIncludes::doAdd(const QString &className, const DomCustomWidget *dcw)
{
    if (dcw != nullptr)
        addCppCustomWidget(className, dcw);
    else
        insertIncludeForClass(className, {}, false);
}

void WriteIncludes::addCppCustomWidget(const QString &className, const DomCustomWidget *dcw)
{
    const DomHeader *domHeader = dcw->elementHeader();
    if (domHeader != nullptr && !domHeader->text().isEmpty()) {
        // custom header unless it is a built-in qt class
        QString header;
        bool global = false;
        if (!m_classToHeader.contains(className)) {
            global = domHeader->attributeLocation().toLower() == "global"_L1;
            header = domHeader->text();
        }
        insertIncludeForClass(className, header, global);
        return;
    }
}

void WriteIncludes::acceptInclude(DomInclude *node)
{
    bool global = true;
    if (node->hasAttributeLocation())
        global = node->attributeLocation() == "global"_L1;
    insertInclude(node->text(), global);
}

void WriteIncludes::insertInclude(const QString &header, bool global)
{
    if (debugWriteIncludes)
        fprintf(stderr, "%s %s %d\n", Q_FUNC_INFO, qPrintable(header), global);

    OrderedSet &includes = global ?  m_globalIncludes : m_localIncludes;
    // Insert (if not already done).
    const bool isNewHeader = includes.insert(header).second;
    if (!isNewHeader)
        return;
    // Also remember base name for quick check of suspicious custom plugins
    const QString lowerBaseName = QFileInfo(header).completeBaseName ().toLower();
    m_includeBaseNames.insert(lowerBaseName);
}

void WriteIncludes::writeHeaders(const OrderedSet &headers, bool global)
{
    const QChar openingQuote = global ? u'<' : u'"';
    const QChar closingQuote = global ? u'>' : u'"';

    // Check for the old headers 'qslider.h' and replace by 'QtGui/QSlider'
    for (const QString &header : headers) {
        const QString value = m_oldHeaderToNewHeader.value(header, header);
        const auto trimmed = QStringView(value).trimmed();
        if (!trimmed.isEmpty())
            m_output << "#include " << openingQuote << trimmed << closingQuote << '\n';
    }
}

} // namespace CPP

QT_END_NAMESPACE