util/normalize/main.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 utils of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    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 occurences 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 }