##// END OF EJS Templates
Add normalize tool source
Jani Honkonen -
r2109:69472ec64f97
parent child
Show More
@@ -0,0 +1,16
1 This tool greps through files for SIGNAL() or SLOT() macros and pipes the signals/slots
2 through QMetaObject::normalizedSignature().
3
4 Rationale: In connect statements, you'll get a micro-speed update when passing in normalized
5 signatures for signals and slots.
6
7 Run it without any arguments to see the command line parameters.
8
9 Typical usage on Qt:
10
11 # find all files with non-normalized signal/slot connections and p4 edit them
12 normalize $QTDIR/src | xargs p4 edit
13 # replace all non-normalized signal/slots
14 normalize --modify $QTDIR/src
15 # p4 diff to see that everything is OK then submit :)
16 p4 submit $QTDIR/src/...
@@ -0,0 +1,193
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the utils of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include <qcoreapplication.h>
42 #include <qdiriterator.h>
43 #include <qfile.h>
44 #include <qmetaobject.h>
45 #include <qstring.h>
46 #include <qtextstream.h>
47
48 #include <qdebug.h>
49
50 #include <limits.h>
51 #include <stdio.h>
52
53 static bool printFilename = true;
54 static bool modify = false;
55
56 QString signature(const QString &line, int pos)
57 {
58 int start = pos;
59 // find first open parentheses
60 while (start < line.length() && line.at(start) != QLatin1Char('('))
61 ++start;
62 int i = ++start;
63 int par = 1;
64 // find matching closing parentheses
65 while (i < line.length() && par > 0) {
66 if (line.at(i) == QLatin1Char('('))
67 ++par;
68 else if (line.at(i) == QLatin1Char(')'))
69 --par;
70 ++i;
71 }
72 if (par == 0)
73 return line.mid(start, i - start - 1);
74 return QString();
75 }
76
77 bool checkSignature(const QString &fileName, QString &line, const char *sig)
78 {
79 static QStringList fileList;
80
81 int idx = -1;
82 bool found = false;
83 while ((idx = line.indexOf(sig, ++idx)) != -1) {
84 const QByteArray sl(signature(line, idx).toLocal8Bit());
85 QByteArray nsl(QMetaObject::normalizedSignature(sl.constData()));
86 if (sl != nsl) {
87 found = true;
88 if (printFilename && !fileList.contains(fileName)) {
89 fileList.prepend(fileName);
90 printf("%s\n", fileName.toLocal8Bit().constData());
91 }
92 if (modify)
93 line.replace(sl, nsl);
94 //qDebug("expected '%s', got '%s'", nsl.data(), sl.data());
95 }
96 }
97 return found;
98 }
99
100 void writeChanges(const QString &fileName, const QStringList &lines)
101 {
102 QFile file(fileName);
103 if (!file.open(QIODevice::WriteOnly)) {
104 qDebug("unable to open file '%s' for writing (%s)", fileName.toLocal8Bit().constData(), file.errorString().toLocal8Bit().constData());
105 return;
106 }
107 QTextStream stream(&file);
108 for (int i = 0; i < lines.count(); ++i)
109 stream << lines.at(i);
110 file.close();
111 }
112
113 void check(const QString &fileName)
114 {
115 QFile file(fileName);
116 if (!file.open(QIODevice::ReadOnly)) {
117 qDebug("unable to open file: '%s' (%s)", fileName.toLocal8Bit().constData(), file.errorString().toLocal8Bit().constData());
118 return;
119 }
120 QStringList lines;
121 bool found = false;
122 while (true) {
123 QByteArray bline = file.readLine(16384);
124 if (bline.isEmpty())
125 break;
126 QString line = QString::fromLocal8Bit(bline);
127 Q_ASSERT_X(line.endsWith("\n"), "check()", fileName.toLocal8Bit().constData());
128 found |= checkSignature(fileName, line, "SLOT");
129 found |= checkSignature(fileName, line, "SIGNAL");
130 if (modify)
131 lines << line;
132 }
133 file.close();
134
135 if (found && modify) {
136 printf("Modifying file: '%s'\n", fileName.toLocal8Bit().constData());
137 writeChanges(fileName, lines);
138 }
139 }
140
141 void traverse(const QString &path)
142 {
143 QDirIterator dirIterator(path, QDir::NoDotAndDotDot | QDir::Dirs | QDir::Files | QDir::NoSymLinks);
144
145 while (dirIterator.hasNext()) {
146 QString filePath = dirIterator.next();
147 if (filePath.endsWith(".cpp"))
148 check(filePath);
149 else if (QFileInfo(filePath).isDir())
150 traverse(filePath); // recurse
151 }
152 }
153
154 int main(int argc, char *argv[])
155 {
156 QCoreApplication app(argc, argv);
157
158 if (app.argc() < 2 || (app.argc() == 2 && (app.argv()[1][0] == '-'))) {
159 printf("usage: normalize [--modify] <path>\n");
160 printf(" <path> can be a single file or a directory (default: look for *.cpp recursively)");
161 printf(" Outputs all filenames that contain non-normalized SIGNALs and SLOTs\n");
162 printf(" with --modify: fix all occurrences of non-normalized SIGNALs and SLOTs\n");
163 return 1;
164 }
165
166 QString path;
167 if (qstrcmp(app.argv()[1], "--modify") == 0) {
168 printFilename = false;
169 modify = true;
170 path = app.argv()[2];
171 } else {
172 path = app.argv()[1];
173 }
174
175 if (path.startsWith("-")) {
176 qWarning("unknown parameter: %s", path.toLocal8Bit().constData());
177 return 1;
178 }
179
180 QFileInfo fi(path);
181 if (fi.isFile()) {
182 check(path);
183 } else if (fi.isDir()) {
184 if (!path.endsWith("/"))
185 path.append("/");
186 traverse(path);
187 } else {
188 qWarning("Don't know what to do with '%s'", path.toLocal8Bit().constData());
189 return 1;
190 }
191
192 return 0;
193 }
@@ -0,0 +1,9
1 TEMPLATE = app
2 CONFIG -= moc
3
4 # Input
5 SOURCES += main.cpp
6
7 QT = core
8 CONFIG += warn_on console
9 mac:CONFIG -= app_bundle
General Comments 0
You need to be logged in to leave comments. Login now