1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25
26 #include "debuggerprotocol.h"
27
28 #include <QtTest>
29
30 //TESTED_COMPONENT=src/plugins/debugger/gdb
31
32 class tst_gdb : public QObject
33 {
34 Q_OBJECT
35
36 public:
tst_gdb()37 tst_gdb() {}
38
39 private slots:
40 void version();
41 void version_data();
42
43 void niceType();
44 void niceType_data();
45 };
46
version()47 void tst_gdb::version()
48 {
49 QFETCH(QString, msg);
50 QFETCH(int, gdbVersion);
51 QFETCH(int, gdbBuildVersion);
52 QFETCH(bool, isMacGdb);
53 QFETCH(bool, isQnxGdb);
54 int v = 0, bv = 0;
55 bool mac = true;
56 bool qnx = true;
57 Debugger::Internal::extractGdbVersion(msg, &v, &bv, &mac, &qnx);
58 //qDebug() << msg << " -> " << v << bv << mac << qnx;
59 QCOMPARE(v, gdbVersion);
60 QCOMPARE(bv, gdbBuildVersion);
61 QCOMPARE(mac, isMacGdb);
62 QCOMPARE(qnx, isQnxGdb);
63 }
64
version_data()65 void tst_gdb::version_data()
66 {
67 QTest::addColumn<QString>("msg");
68 QTest::addColumn<int>("gdbVersion");
69 QTest::addColumn<int>("gdbBuildVersion");
70 QTest::addColumn<bool>("isMacGdb");
71 QTest::addColumn<bool>("isQnxGdb");
72
73 QTest::newRow("Debian")
74 << "GNU gdb (GDB) 7.0.1-debian"
75 << 70001 << 0 << false << false;
76
77 QTest::newRow("CVS 7.0.90")
78 << "GNU gdb (GDB) 7.0.90.20100226-cvs"
79 << 70090 << 20100226 << false << false;
80
81 QTest::newRow("Ubuntu Lucid")
82 << "GNU gdb (GDB) 7.1-ubuntu"
83 << 70100 << 0 << false << false;
84
85 QTest::newRow("Fedora 13")
86 << "GNU gdb (GDB) Fedora (7.1-22.fc13)"
87 << 70100 << 22 << false << false;
88
89 QTest::newRow("Gentoo")
90 << "GNU gdb (Gentoo 7.1 p1) 7.1"
91 << 70100 << 1 << false << false;
92
93 QTest::newRow("Fedora EL5")
94 << "GNU gdb Fedora (6.8-37.el5)"
95 << 60800 << 37 << false << false;
96
97 QTest::newRow("SUSE")
98 << "GNU gdb (GDB) SUSE (6.8.91.20090930-2.4)"
99 << 60891 << 20090930 << false << false;
100
101 QTest::newRow("SLES")
102 << "GNU gdb (GDB; SUSE Linux Enterprise 10) 7.9.1"
103 << 70901 << 0 << false << false;
104
105 QTest::newRow("Apple")
106 << "GNU gdb 6.3.50-20050815 (Apple version gdb-1461.2)"
107 << 60350 << 1461 << true << false;
108
109 QTest::newRow("Apple")
110 << "GNU gdb 6.3.50-20050815 (Apple version gdb-960)"
111 << 60350 << 960 << true << false;
112
113 QTest::newRow("QNX")
114 << "GNU gdb (GDB) 7.3 qnx (rev. 613)"
115 << 70300 << 613 << false << true;
116
117 QTest::newRow("rubenvb")
118 << "GNU gdb (rubenvb-4.7.2-release) 7.5.50.20120920-cvs"
119 << 70550 << 20120920 << false << false;
120
121 QTest::newRow("openSUSE 13.1")
122 << "GNU gdb (GDB; openSUSE 13.1) 7.6.50.20130731-cvs"
123 << 70650 << 20130731 << false << false;
124
125 QTest::newRow("openSUSE 13.2")
126 << "GNU gdb (GDB; openSUSE 13.2) 7.8"
127 << 70800 << 0 << false << false;
128
129 QTest::newRow("Fedora 26")
130 << "GNU gdb (GDB) Fedora 8.0-13.fc26"
131 << 80000 << 13 << false << false;
132
133 QTest::newRow("Debian 7.12 git")
134 << "GNU gdb (Debian 7.12-6) 7.12.0.20161007-git"
135 << 71200 << 6 << false << false;
136 }
137
chopConst(QString type)138 static QString chopConst(QString type)
139 {
140 while (1) {
141 if (type.startsWith("const"))
142 type = type.mid(5);
143 else if (type.startsWith(' '))
144 type = type.mid(1);
145 else if (type.endsWith("const"))
146 type.chop(5);
147 else if (type.endsWith(' '))
148 type.chop(1);
149 else
150 break;
151 }
152 return type;
153 }
154
niceType(QString type)155 QString niceType(QString type)
156 {
157 type.replace('*', '@');
158
159 for (int i = 0; i < 10; ++i) {
160 int start = type.indexOf("std::allocator<");
161 if (start == -1)
162 break;
163 // search for matching '>'
164 int pos;
165 int level = 0;
166 for (pos = start + 12; pos < type.size(); ++pos) {
167 int c = type.at(pos).unicode();
168 if (c == '<') {
169 ++level;
170 } else if (c == '>') {
171 --level;
172 if (level == 0)
173 break;
174 }
175 }
176 QString alloc = type.mid(start, pos + 1 - start).trimmed();
177 QString inner = alloc.mid(15, alloc.size() - 16).trimmed();
178 //qDebug() << "MATCH: " << pos << alloc << inner;
179
180 if (inner == QLatin1String("char"))
181 // std::string
182 type.replace(QLatin1String("basic_string<char, std::char_traits<char>, "
183 "std::allocator<char> >"), QLatin1String("string"));
184 else if (inner == QLatin1String("wchar_t"))
185 // std::wstring
186 type.replace(QLatin1String("basic_string<wchar_t, std::char_traits<wchar_t>, "
187 "std::allocator<wchar_t> >"), QLatin1String("wstring"));
188
189 // std::vector, std::deque, std::list
190 const QRegularExpression re1(QString("(vector|list|deque)<%1, %2\\s*?>").arg(inner, alloc));
191 QRegularExpressionMatch match = re1.match(type);
192 if (match.hasMatch())
193 type.replace(match.captured(), QString("%1<%2>").arg(match.captured(1), inner));
194
195
196 // std::stack
197 const QRegularExpression re6(QString("stack<%1, std::deque<%2> >").arg(inner, inner));
198 match = re6.match(type);
199 if (match.hasMatch())
200 type.replace(match.captured(), QString("stack<%1>").arg(inner));
201
202 // std::set
203 const QRegularExpression re4(QString("set<%1, std::less<%2>, %3\\s*?>").arg(inner, inner, alloc));
204 match = re4.match(type);
205 if (match.hasMatch())
206 type.replace(match.captured(), QString("set<%1>").arg(inner));
207
208
209 // std::map
210 if (inner.startsWith("std::pair<")) {
211 // search for outermost ','
212 int pos;
213 int level = 0;
214 for (pos = 10; pos < inner.size(); ++pos) {
215 int c = inner.at(pos).unicode();
216 if (c == '<')
217 ++level;
218 else if (c == '>')
219 --level;
220 else if (c == ',' && level == 0)
221 break;
222 }
223 QString ckey = inner.mid(10, pos - 10);
224 QString key = chopConst(ckey);
225 QString value = inner.mid(pos + 2, inner.size() - 3 - pos);
226
227 const QRegularExpression re5(QString("map<%1, %2, std::less<%3>, %4\\s*?>")
228 .arg(key, value, key, alloc));
229 match = re5.match(type);
230 if (match.hasMatch()) {
231 type.replace(match.captured(), QString("map<%1, %2>").arg(key, value));
232 } else {
233 const QRegularExpression re7(QString("map<const %1, %2, std::less<const %3>, %4\\s*?>")
234 .arg(key, value, key, alloc));
235 match = re7.match(type);
236 if (match.hasMatch())
237 type.replace(match.captured(), QString("map<const %1, %2>").arg(key, value));
238 }
239 }
240 }
241 type.replace('@', '*');
242 type.replace(QLatin1String(" >"), QString(QLatin1Char('>')));
243 return type;
244 }
245
niceType()246 void tst_gdb::niceType()
247 {
248 // cf. watchutils.cpp
249 QFETCH(QString, input);
250 QFETCH(QString, simplified);
251 QCOMPARE(::niceType(input), simplified);
252 }
253
niceType_data()254 void tst_gdb::niceType_data()
255 {
256 QTest::addColumn<QString>("input");
257 QTest::addColumn<QString>("simplified");
258
259 QTest::newRow("list")
260 << "std::list<int, std::allocator<int> >"
261 << "std::list<int>";
262
263 QTest::newRow("combined")
264 << "std::vector<std::list<int, std::allocator<int> >*, "
265 "std::allocator<std::list<int, std::allocator<int> >*> >"
266 << "std::vector<std::list<int>*>";
267
268 QTest::newRow("stack")
269 << "std::stack<int, std::deque<int, std::allocator<int> > >"
270 << "std::stack<int>";
271
272 QTest::newRow("map")
273 << "std::map<myns::QString, Foo, std::less<myns::QString>, "
274 "std::allocator<std::pair<const myns::QString, Foo> > >"
275 << "std::map<myns::QString, Foo>";
276
277 QTest::newRow("map2")
278 << "std::map<const char*, Foo, std::less<const char*>, "
279 "std::allocator<std::pair<const char* const, Foo> > >"
280 << "std::map<const char*, Foo>";
281 }
282
283 QTEST_APPLESS_MAIN(tst_gdb);
284
285 #include "tst_gdb.moc"
286
287