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