1
/****************************************************************************
2
**
3
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4
** All rights reserved.
5
** Contact: Nokia Corporation (qt-info@nokia.com)
6
**
7
** This file is part of the test suite of the Qt Toolkit.
8
**
9
** $QT_BEGIN_LICENSE:LGPL$
10
** GNU Lesser General Public License Usage
11
** This file may be used under the terms of the GNU Lesser General Public
12
** License version 2.1 as published by the Free Software Foundation and
13
** appearing in the file LICENSE.LGPL included in the packaging of this
14
** file. Please review the following information to ensure the GNU Lesser
15
** General Public License version 2.1 requirements will be met:
16
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17
**
18
** In addition, as a special exception, Nokia gives you certain additional
19
** rights. These rights are described in the Nokia Qt LGPL Exception
20
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21
**
22
** GNU General Public License Usage
23
** Alternatively, this file may be used under the terms of the GNU General
24
** Public License version 3.0 as published by the Free Software Foundation
25
** and appearing in the file LICENSE.GPL included in the packaging of this
26
** file. Please review the following information to ensure the GNU General
27
** Public License version 3.0 requirements will be met:
28
** http://www.gnu.org/copyleft/gpl.html.
29
**
30
** Other Usage
31
** Alternatively, this file may be used in accordance with the terms and
32
** conditions contained in a signed written agreement between you and Nokia.
33
**
34
**
35
**
36
**
37
**
38
** $QT_END_LICENSE$
39
**
40
****************************************************************************/
41
42
43
#include "qbic.h"
44
45
#include "QtCore/qfile.h"
46
#include "QtCore/qdebug.h"
47
48
void QBic::addBlacklistedClass(const QString &wildcard)
49
{
50
    blackList.append(QRegExp(wildcard, Qt::CaseSensitive, QRegExp::Wildcard));
51
}
52
53
void QBic::removeBlacklistedClass(const QString &wildcard)
54
{
55
    blackList.removeAll(QRegExp(wildcard, Qt::CaseSensitive, QRegExp::Wildcard));
56
}
57
58
bool QBic::isBlacklisted(const QString &className) const
59
{
60
    // all templates are blacklisted
61
    if (className.contains('<'))
62
        return true;
63
64
    for (int i = 0; i < blackList.count(); ++i)
65
        if (blackList.at(i).exactMatch(className))
66
            return true;
67
    return false;
68
}
69
70
static QStringList normalizedVTable(const QStringList &entry)
71
{
72
    QStringList normalized;
73
74
    for (int i = 2; i < entry.count(); ++i) {
75
        const QString line = entry.at(i).simplified();
76
        bool isOk = false;
77
        int num = line.left(line.indexOf(QLatin1Char(' '))).toInt(&isOk);
78
        if (!isOk) {
79
            qWarning("unrecognized line: %s", qPrintable(line));
80
            continue;
81
        }
82
83
        QString sym = line.mid(line.indexOf(QLatin1Char(' ')) + 1);
84
        if (sym.startsWith(QLatin1Char('('))) {
85
            if (sym.endsWith(QLatin1Char(')'))) {
86
                sym = sym.mid(sym.lastIndexOf('(') + 1);
87
                sym.chop(1);
88
            } else {
89
                sym = sym.mid(sym.lastIndexOf(QLatin1Char(')')) + 1);
90
            }
91
        } else {
92
            sym = sym.left(sym.indexOf(QLatin1Char('(')));
93
        }
94
95
        if (sym.startsWith(QLatin1String("& ")))
96
            sym.remove(1, 1);
97
98
        if (sym.startsWith(QLatin1String("-0")) || sym.startsWith(QLatin1String("0"))) {
99
            if (sym.endsWith('u'))
100
                sym.chop(1);
101
102
            bool isOk = false;
103
            qint64 num = sym.toLongLong(&isOk, 16);
104
            if (!isOk) {
105
                qWarning("unrecognized token: %s", qPrintable(sym));
106
                continue;
107
            }
108
            if (sizeof(void*) == 4)
109
                sym = QString::number(int(num));
110
            else
111
                sym = QString::number(num);
112
        }
113
114
        normalized << QString::number(num) + QLatin1Char(' ') + sym;
115
    }
116
117
    return normalized;
118
}
119
120
QBic::Info QBic::parseOutput(const QByteArray &ba) const
121
{
122
    Info info;
123
    const QStringList source = QString::fromLatin1(ba).split("\n\n");
124
125
    foreach(QString str, source) {
126
        QStringList entry = str.split('\n');
127
        if (entry.count() < 2)
128
            continue;
129
        if (entry.at(0).startsWith("Class ")) {
130
            const QString className = entry.at(0).mid(6);
131
            if (isBlacklisted(className))
132
                continue;
133
            QRegExp rx("size=(\\d+)");
134
            if  (rx.indexIn(entry.at(1)) == -1) {
135
                qWarning("Could not parse class information for className %s", className.toLatin1().constData());
136
                continue;
137
            }
138
            info.classSizes[className] = rx.cap(1).toInt();
139
        } else if (entry.at(0).startsWith("Vtable for ")) {
140
            const QString className = entry.at(0).mid(11);
141
            if (isBlacklisted(className))
142
                continue;
143
            info.classVTables[className] = normalizedVTable(entry);
144
        }
145
    }
146
147
    return info;
148
}
149
150
QBic::Info QBic::parseFile(const QString &fileName) const
151
{
152
    QFile f(fileName);
153
    if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
154
        return Info();
155
156
    QByteArray ba = f.readAll();
157
    f.close();
158
159
    return parseOutput(ba);
160
}
161
162
enum VTableDiffResult { Match, Mismatch, Reimp };
163
static VTableDiffResult diffVTableEntry(const QString &v1, const QString &v2)
164
{
165
    if (v1 == v2)
166
        return Match;
167
    if (v2.endsWith(QLatin1String("__cxa_pure_virtual")))
168
        return Reimp;
169
    if (!v1.contains(QLatin1String("::")) || !v2.contains(QLatin1String("::")))
170
        return Mismatch;
171
172
    const QString sym1 = v1.mid(v1.lastIndexOf(QLatin1String("::")) + 2);
173
    const QString sym2 = v2.mid(v2.lastIndexOf(QLatin1String("::")) + 2);
174
175
    if (sym1 == sym2)
176
        return Reimp;
177
178
    return Mismatch;
179
}
180
181
QBic::VTableDiff QBic::diffVTables(const Info &oldLib, const Info &newLib) const
182
{
183
    VTableDiff result;
184
185
    for (QHash<QString, QStringList>::const_iterator it = newLib.classVTables.constBegin();
186
            it != newLib.classVTables.constEnd(); ++it) {
187
        if (!oldLib.classVTables.contains(it.key())) {
188
            result.addedVTables.append(it.key());
189
            continue;
190
        }
191
        const QStringList oldVTable = oldLib.classVTables.value(it.key());
192
        const QStringList vTable = it.value();
193
        if (vTable.count() != oldVTable.count()) {
194
            result.modifiedVTables.append(QPair<QString, QString>(it.key(),
195
                        QLatin1String("size mismatch")));
196
            continue;
197
        }
198
199
        for (int i = 0; i < vTable.count(); ++i) {
200
            VTableDiffResult diffResult = diffVTableEntry(vTable.at(i), oldVTable.at(i));
201
            switch (diffResult) {
202
            case Match:
203
                // do nothing
204
                break;
205
            case Mismatch:
206
                result.modifiedVTables.append(QPair<QString, QString>(oldVTable.at(i),
207
                            vTable.at(i)));
208
                break;
209
            case Reimp:
210
                result.reimpMethods.append(QPair<QString, QString>(oldVTable.at(i), vTable.at(i)));
211
                break;
212
            }
213
        }
214
    }
215
216
    for (QHash<QString, QStringList>::const_iterator it = oldLib.classVTables.constBegin();
217
            it != oldLib.classVTables.constEnd(); ++it) {
218
        if (!newLib.classVTables.contains(it.key()))
219
            result.removedVTables.append(it.key());
220
    }
221
222
    return result;
223
}
224
225
QBic::SizeDiff QBic::diffSizes(const Info &oldLib, const Info &newLib) const
226
{
227
    QBic::SizeDiff result;
228
229
    for (QHash<QString, int>::const_iterator it = newLib.classSizes.constBegin();
230
            it != newLib.classSizes.constEnd(); ++it) {
231
        if (!oldLib.classSizes.contains(it.key())) {
232
            result.added.append(it.key());
233
            continue;
234
        }
235
        int oldSize = oldLib.classSizes.value(it.key());
236
        int newSize = it.value();
237
238
        if (oldSize != newSize)
239
            result.mismatch.append(it.key());
240
    }
241
242
    for (QHash<QString, int>::const_iterator it = oldLib.classSizes.constBegin();
243
            it != oldLib.classSizes.constEnd(); ++it) {
244
        if (!newLib.classSizes.contains(it.key()))
245
            result.removed.append(it.key());
246
    }
247
248
    return result;
249
}