summaryrefslogtreecommitdiff
path: root/src/plugins/debugger/simplifytype.cpp
blob: 005f8a57b99fdd7c2c7f299af321acc09629eebd (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://www.qt.io/licensing.  For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "simplifytype.h"

#include <QRegExp>

#include <ctype.h>

#define QTC_ASSERT_STRINGIFY_HELPER(x) #x
#define QTC_ASSERT_STRINGIFY(x) QTC_ASSERT_STRINGIFY_HELPER(x)
#define QTC_ASSERT_STRING(cond) qDebug("SOFT ASSERT: \"" cond"\" in file " __FILE__ ", line " QTC_ASSERT_STRINGIFY(__LINE__))
#define QTC_ASSERT(cond, action) if (cond) {} else { QTC_ASSERT_STRING(#cond); action; } do {} while (0)

namespace Debugger {
namespace Internal {

// Simplify complicated STL template types,
// such as 'std::basic_string<char,std::char_traits<char>,std::allocator<char> >'
// -> 'std::string' and helpers.

static QString chopConst(QString type)
{
    while (1) {
        if (type.startsWith(QLatin1String("const")))
            type = type.mid(5);
        else if (type.startsWith(QLatin1Char(' ')))
            type = type.mid(1);
        else if (type.endsWith(QLatin1String("const")))
            type.chop(5);
        else if (type.endsWith(QLatin1Char(' ')))
            type.chop(1);
        else
            break;
    }
    return type;
}

static inline QRegExp stdStringRegExp(const QString &charType)
{
    QString rc = QLatin1String("basic_string<");
    rc += charType;
    rc += QLatin1String(",[ ]?std::char_traits<");
    rc += charType;
    rc += QLatin1String(">,[ ]?std::allocator<");
    rc += charType;
    rc += QLatin1String("> >");
    const QRegExp re(rc);
    QTC_ASSERT(re.isValid(), /**/);
    return re;
}

// Simplify string types in a type
// 'std::set<std::basic_string<char... > >' -> std::set<std::string>'
static inline void simplifyStdString(const QString &charType, const QString &replacement,
                                     QString *type)
{
    QRegExp stringRegexp = stdStringRegExp(charType);
    const int replacementSize = replacement.size();
    for (int pos = 0; pos < type->size(); ) {
        // Check next match
        const int matchPos = stringRegexp.indexIn(*type, pos);
        if (matchPos == -1)
            break;
        const int matchedLength = stringRegexp.matchedLength();
        type->replace(matchPos, matchedLength, replacement);
        pos = matchPos + replacementSize;
        // If we were inside an 'allocator<std::basic_string..char > >'
        // kill the following blank -> 'allocator<std::string>'
        if (pos + 1 < type->size() && type->at(pos) == QLatin1Char(' ')
                && type->at(pos + 1) == QLatin1Char('>'))
            type->remove(pos, 1);
    }
}

// Fix 'std::allocator<std::string >' -> 'std::allocator<std::string>',
// which can happen when replacing/simplifying
static inline QString fixNestedTemplates(QString s)
{
    const int size = s.size();
    if (size > 3
            && s.at(size - 1) == QLatin1Char('>')
            && s.at(size - 2) == QLatin1Char(' ')
            && s.at(size - 3) != QLatin1Char('>'))
        s.remove(size - 2, 1);
    return s;
}

QString simplifyType(const QString &typeIn)
{
    QString type = typeIn;
    if (type.startsWith(QLatin1String("class "))) // MSVC prepends class,struct
        type.remove(0, 6);
    if (type.startsWith(QLatin1String("struct ")))
        type.remove(0, 7);

    const bool isLibCpp = type.contains(QLatin1String("std::__1"));
    type.replace(QLatin1String("std::__1::"), QLatin1String("std::"));
    type.replace(QLatin1String("std::__debug::"), QLatin1String("std::"));
    QRegExp simpleStringRE(QString::fromLatin1("std::basic_string<char> ?"));
    type.replace(simpleStringRE, QLatin1String("std::string"));
    type.replace(QLatin1Char('*'), QLatin1Char('@'));

    for (int i = 0; i < 10; ++i) {
        // boost::shared_ptr<...>::element_type
        if (type.startsWith(QLatin1String("boost::shared_ptr<"))
                && type.endsWith(QLatin1String(">::element_type")))
            type = type.mid(18, type.size() - 33);

        // std::shared_ptr<...>::element_type
        if (type.startsWith(QLatin1String("std::shared_ptr<"))
                && type.endsWith(QLatin1String(">::element_type")))
            type = type.mid(16, type.size() - 31);

        // std::ifstream
        QRegExp ifstreamRE(QLatin1String("std::basic_ifstream<char,\\s*std::char_traits<char>\\s*>"));
        ifstreamRE.setMinimal(true);
        QTC_ASSERT(ifstreamRE.isValid(), return typeIn);
        if (ifstreamRE.indexIn(type) != -1)
            type.replace(ifstreamRE.cap(0), QLatin1String("std::ifstream"));


        // std::__1::hash_node<int, void *>::value_type -> int
        if (isLibCpp) {
            //QRegExp hashNodeRE(QLatin1String("std::__hash_node<([^<>]*),\\s*void\\s*@>::value_type"));
            QRegExp hashNodeRE(QLatin1String("std::__hash_node<([^<>]*),\\s*void\\s*@>::value_type"));
            QTC_ASSERT(hashNodeRE.isValid(), return typeIn);
            if (hashNodeRE.indexIn(type) != -1)
                type.replace(hashNodeRE.cap(0), hashNodeRE.cap(1));
        }

        // Anything with a std::allocator
        int start = type.indexOf(QLatin1String("std::allocator<"));
        if (start != -1) {
            // search for matching '>'
            int pos;
            int level = 0;
            for (pos = start + 12; pos < type.size(); ++pos) {
                int c = type.at(pos).unicode();
                if (c == '<') {
                    ++level;
                } else if (c == '>') {
                    --level;
                    if (level == 0)
                        break;
                }
            }
            const QString alloc = fixNestedTemplates(type.mid(start, pos + 1 - start).trimmed());
            const QString inner = fixNestedTemplates(alloc.mid(15, alloc.size() - 16).trimmed());

            const QString allocEsc = QRegExp::escape(alloc);
            const QString innerEsc = QRegExp::escape(inner);
            if (inner == QLatin1String("char")) { // std::string
                simplifyStdString(QLatin1String("char"), QLatin1String("string"), &type);
            } else if (inner == QLatin1String("wchar_t")) { // std::wstring
                simplifyStdString(QLatin1String("wchar_t"), QLatin1String("wstring"), &type);
            } else if (inner == QLatin1String("unsigned short")) { // std::wstring/MSVC
                simplifyStdString(QLatin1String("unsigned short"), QLatin1String("wstring"), &type);
            }
            // std::vector, std::deque, std::list
            QRegExp re1(QString::fromLatin1("(vector|list|deque)<%1, ?%2\\s*>").arg(innerEsc, allocEsc));
            QTC_ASSERT(re1.isValid(), return typeIn);
            if (re1.indexIn(type) != -1)
                type.replace(re1.cap(0), QString::fromLatin1("%1<%2>").arg(re1.cap(1), inner));

            // std::stack
            QRegExp stackRE(QString::fromLatin1("stack<%1, ?std::deque<%2> >").arg(innerEsc, innerEsc));
            stackRE.setMinimal(true);
            QTC_ASSERT(stackRE.isValid(), return typeIn);
            if (stackRE.indexIn(type) != -1)
                type.replace(stackRE.cap(0), QString::fromLatin1("stack<%1>").arg(inner));

            // std::hash<char>
            QRegExp hashCharRE(QString::fromLatin1("hash<char, std::char_traits<char>, ?%1\\s*>").arg(allocEsc));
            hashCharRE.setMinimal(true);
            QTC_ASSERT(hashCharRE.isValid(), return typeIn);
            if (hashCharRE.indexIn(type) != -1)
                type.replace(hashCharRE.cap(0), QString::fromLatin1("hash<char>"));

            // std::set
            QRegExp setRE(QString::fromLatin1("set<%1, ?std::less<%2>, ?%3\\s*>").arg(innerEsc, innerEsc, allocEsc));
            setRE.setMinimal(true);
            QTC_ASSERT(setRE.isValid(), return typeIn);
            if (setRE.indexIn(type) != -1)
                type.replace(setRE.cap(0), QString::fromLatin1("set<%1>").arg(inner));

            // std::unordered_set
            QRegExp unorderedSetRE(QString::fromLatin1("unordered_set<%1, ?std::hash<%2>, ?std::equal_to<%3>, ?%4\\s*>")
                .arg(innerEsc, innerEsc, innerEsc, allocEsc));
            unorderedSetRE.setMinimal(true);
            QTC_ASSERT(unorderedSetRE.isValid(), return typeIn);
            if (unorderedSetRE.indexIn(type) != -1)
                type.replace(unorderedSetRE.cap(0), QString::fromLatin1("unordered_set<%1>").arg(inner));

            // std::map
            if (inner.startsWith(QLatin1String("std::pair<"))) {
                // search for outermost ',', split key and value
                int pos;
                int level = 0;
                for (pos = 10; pos < inner.size(); ++pos) {
                    int c = inner.at(pos).unicode();
                    if (c == '<')
                        ++level;
                    else if (c == '>')
                        --level;
                    else if (c == ',' && level == 0)
                        break;
                }
                const QString key = chopConst(inner.mid(10, pos - 10));
                const QString keyEsc = QRegExp::escape(key);
                // Get value: MSVC: 'pair<a const ,b>', gcc: 'pair<const a, b>'
                if (inner.at(++pos) == QLatin1Char(' '))
                    pos++;
                const QString value = inner.mid(pos, inner.size() - pos - 1).trimmed();
                const QString valueEsc = QRegExp::escape(value);
                QRegExp mapRE1(QString::fromLatin1("map<%1, ?%2, ?std::less<%3 ?>, ?%4\\s*>")
                               .arg(keyEsc, valueEsc, keyEsc, allocEsc));
                mapRE1.setMinimal(true);
                QTC_ASSERT(mapRE1.isValid(), return typeIn);
                if (mapRE1.indexIn(type) != -1) {
                    type.replace(mapRE1.cap(0), QString::fromLatin1("map<%1, %2>").arg(key, value));
                } else {
                    QRegExp mapRE2(QString::fromLatin1("map<const %1, ?%2, ?std::less<const %3>, ?%4\\s*>")
                                   .arg(keyEsc, valueEsc, keyEsc, allocEsc));
                    mapRE2.setMinimal(true);
                    if (mapRE2.indexIn(type) != -1)
                        type.replace(mapRE2.cap(0), QString::fromLatin1("map<const %1, %2>").arg(key, value));
                }
            }

            // std::unordered_map
            if (inner.startsWith(QLatin1String("std::pair<"))) {
                // search for outermost ',', split key and value
                int pos;
                int level = 0;
                for (pos = 10; pos < inner.size(); ++pos) {
                    int c = inner.at(pos).unicode();
                    if (c == '<')
                        ++level;
                    else if (c == '>')
                        --level;
                    else if (c == ',' && level == 0)
                        break;
                }
                const QString key = chopConst(inner.mid(10, pos - 10));
                const QString keyEsc = QRegExp::escape(key);
                // Get value: MSVC: 'pair<a const ,b>', gcc: 'pair<const a, b>'
                if (inner.at(++pos) == QLatin1Char(' '))
                    pos++;
                const QString value = inner.mid(pos, inner.size() - pos - 1).trimmed();
                const QString valueEsc = QRegExp::escape(value);
                QRegExp mapRE1(QString::fromLatin1("unordered_map<%1, ?%2, ?std::hash<%3 ?>, ?std::equal_to<%4 ?>, ?%5\\s*>")
                               .arg(keyEsc, valueEsc, keyEsc, keyEsc, allocEsc));
                mapRE1.setMinimal(true);
                QTC_ASSERT(mapRE1.isValid(), return typeIn);
                if (mapRE1.indexIn(type) != -1)
                    type.replace(mapRE1.cap(0), QString::fromLatin1("unordered_map<%1, %2>").arg(key, value));

                if (isLibCpp) {
                    QRegExp mapRE2(QString::fromLatin1("unordered_map<std::string, ?%1, "
                                    "?std::hash<char>, ?std::equal_to<std::string>, ?%2\\s*>")
                                   .arg(valueEsc, allocEsc));
                    mapRE2.setMinimal(true);
                    QTC_ASSERT(mapRE2.isValid(), return typeIn);
                    if (mapRE2.indexIn(type) != -1)
                        type.replace(mapRE2.cap(0), QString::fromLatin1("unordered_map<std::string, %2>").arg(value));
                }
            }
        } // with std::allocator
    }
    type.replace(QLatin1Char('@'), QLatin1Char('*'));
    type.replace(QLatin1String(" >"), QLatin1String(">"));
    return type;
}

} // namespace Internal
} // namespace Debugger