projectporter.cpp

来自「奇趣公司比较新的qt/emd版本」· C++ 代码 · 共 406 行 · 第 1/2 页

CPP
406
字号
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the qt3to4 porting application of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file.  Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "projectporter.h"#include "proparser.h"#include "textreplacement.h"#include "fileporter.h"#include "logger.h"#include "translationunit.h"#include "codemodelattributes.h"#include <QtDebug>#include <QFile>#include <QDir>#include <QStringList>#include <QFileInfo>#include <QBuffer>using namespace TokenEngine;ProjectPorter::ProjectPorter(QString basePath, QStringList includeDirectories, QStringList qt3HeadersFilenames):basePath(basePath),includeDirectories(includeDirectories),defaultDefinitions(defaultMacros(preprocessorCache)),filePorter(preprocessorCache),qt3HeadersFilenames(qt3HeadersFilenames),analyze(true),warnings(false){}void ProjectPorter::enableCppParsing(bool enable){    analyze = enable;}void ProjectPorter::enableMissingFilesWarnings(bool enable){    warnings = enable;}void ProjectPorter::portProject(QString fileName){    QFileInfo fileInfo(fileName);    portProject(fileInfo.path(), fileInfo.fileName());}/*    Port a single file*/void ProjectPorter::portFile(QString fileName){    if (analyze) {        IncludeFiles includeFiles(basePath, includeDirectories);        PreprocessorController preprocessor(includeFiles, preprocessorCache, qt3HeadersFilenames);        connect(&preprocessor, SIGNAL(error(QString,QString)), SLOT(error(QString,QString)));        Rpp::DefineMap definitionsCopy = *defaultDefinitions;        // Preprocess        TokenSectionSequence translationUnit = preprocessor.evaluate(fileName, &definitionsCopy);        // Parse        TranslationUnit translationUnitData = TranslationUnitAnalyzer().analyze(translationUnit);        // Enable attribute generation for this file.        enableAttributes(includeFiles, fileName);        // Generate attributes.        CodeModelAttributes().createAttributes(translationUnitData);    }    portFiles(QString(), QStringList() << fileName);}void ProjectPorter::error(QString type, QString text){   if (warnings && type == QLatin1String("Error"))        printf("Warning: %s\n", text.toLocal8Bit().constData());}void ProjectPorter::portProject(QString basePath, QString proFileName){    QString fullInFileName = basePath + QLatin1String("/") + proFileName;    QFileInfo infileInfo(fullInFileName);    if (!infileInfo.exists()) {        printf("Could not open file: %s\n", QDir::toNativeSeparators(fullInFileName).toLocal8Bit().constData());        return;    }    QString proFileContents = loadFile(fullInFileName);    QMap<QString, QString> proFileMap = proFileTagMap(proFileContents, QDir(basePath).absolutePath());    // Check if this is a TEMPLATE = subdirs .pro file, in that case we    // process each subdir (recursively).    QString templateTag = proFileMap[QLatin1String("TEMPLATE")];    if (templateTag == QLatin1String("subdirs")) {        QStringList subdirs = proFileMap[QLatin1String("SUBDIRS")].split(QLatin1String(" "), QString::SkipEmptyParts);        foreach(QString subdir, subdirs) {            QString newBasePath  = basePath + QLatin1String("/") + subdir;            QStringList dirsInSubdir = subdir.split(QRegExp(QLatin1String("/|\\\\")), QString::SkipEmptyParts);            QString newProFileName = dirsInSubdir.last() + QLatin1String(".pro");            portProject(newBasePath, newProFileName);        }        return;    }    // Get headers and sources file names from .pro file.    QStringList sources = proFileMap[QLatin1String("SOURCES")].split(QLatin1String(" "), QString::SkipEmptyParts);    QStringList headers = proFileMap[QLatin1String("HEADERS")].split(QLatin1String(" "), QString::SkipEmptyParts);    QStringList forms = proFileMap[QLatin1String("FORMS")].split(QLatin1String(" "), QString::SkipEmptyParts);    QStringList uidoth;    for (int i = 0; i < forms.size(); ++i) {        QString ui_h = forms.at(i) + QLatin1String(".h");        if (QFile::exists(basePath + QLatin1String("/") + ui_h))            uidoth += ui_h;    }    if (analyze) {        printf("Parsing");        // Get include paths from the pro file.        QStringList includeProPaths = proFileMap[QLatin1String("INCLUDEPATH")].split(QLatin1String(" "), QString::SkipEmptyParts);        QStringList dependProPaths = proFileMap[QLatin1String("DEPENDPATH")].split(QLatin1String(" "), QString::SkipEmptyParts);        IncludeFiles includeFiles(basePath, includeDirectories + includeProPaths + dependProPaths);        PreprocessorController preprocessorController(includeFiles, preprocessorCache, qt3HeadersFilenames);        connect(&preprocessorController, SIGNAL(error(QString,QString)), SLOT(error(QString,QString)));        TranslationUnitAnalyzer translationUnitAnalyzer;        CodeModelAttributes codeModelAttributes;        // Enable attribute generation for header files.        foreach(QString headerFile, headers)            enableAttributes(includeFiles, headerFile);        // Enable attribute generation for ui.h files.        foreach(QString headerFile, uidoth)            enableAttributes(includeFiles, headerFile);        // Analyze each translation unit. (one per cpp file)        foreach(QString sourceFile, sources) {            printf(".");            fflush(stdout);            Rpp::DefineMap definitionsCopy = *defaultDefinitions;            TokenSectionSequence translationUnit =                preprocessorController.evaluate(sourceFile, &definitionsCopy);            TranslationUnit translationUnitData =                translationUnitAnalyzer.analyze(translationUnit);            // Enable attribute generation for this file.            enableAttributes(includeFiles, sourceFile);            codeModelAttributes.createAttributes(translationUnitData);        }        puts("");    }    // Port files.    portFiles(basePath, sources);    portFiles(basePath, headers);    if (!uidoth.isEmpty())        portFiles(basePath, uidoth);    Logger::instance()->globalState[QLatin1String("currentFileName")] = proFileName;    Logger::instance()->beginSection();    portProFile(fullInFileName, proFileMap);}/*    Port each file given in the fileNames list. If a file name is relative    it is assumed to be relative to basePath.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?