diff --git a/src/tools/cplusplus/Main.cpp b/src/tools/cplusplus/Main.cpp index ec11bfba7cb189be62da8f4874d3a25548c40062..434b060ef35bdd79a596830121b7b3ddcf31b97f 100644 --- a/src/tools/cplusplus/Main.cpp +++ b/src/tools/cplusplus/Main.cpp @@ -4,6 +4,7 @@ #include <QTextDocument> #include <QTextCursor> #include <QTextBlock> +#include <QDir> #include <QDebug> #include <Control.h> @@ -163,77 +164,88 @@ private: Overview oo; }; -int main(int argc, char *argv[]) +void generateAST_H(const Snapshot &snapshot, const QDir &cplusplusDir) { - QCoreApplication app(argc, argv); - QStringList files = app.arguments(); - files.removeFirst(); + QFileInfo fileAST_h(cplusplusDir, QLatin1String("AST.h")); + Q_ASSERT(fileAST_h.exists()); - if (files.isEmpty()) { - std::cerr << "Usage: cplusplus AST.h" << std::endl; - return EXIT_FAILURE; - } + const QString fileName = fileAST_h.absoluteFilePath(); - Snapshot snapshot; + QFile file(fileName); + if (! file.open(QFile::ReadOnly)) + return; - //const QString configuration = QLatin1String("#define CPLUSPLUS_EXPORT\n"); + const QString source = QTextStream(&file).readAll(); + file.close(); - foreach (const QString &fileName, files) { - QFile file(fileName); - if (! file.open(QFile::ReadOnly)) - continue; + QTextDocument document; + document.setPlainText(source); - const QString source = QTextStream(&file).readAll(); - QTextDocument document; - document.setPlainText(source); + Document::Ptr doc = Document::create(fileName); + const QByteArray preprocessedCode = snapshot.preprocessedCode(source, fileName); + doc->setSource(preprocessedCode); + doc->check(); - Document::Ptr doc = Document::create(fileName); - //doc->control()->setDiagnosticClient(0); - const QByteArray preprocessedCode = snapshot.preprocessedCode(source, fileName); - doc->setSource(preprocessedCode); - doc->check(); + FindASTNodes process(doc, &document); + ASTNodes astNodes = process(doc->translationUnit()->ast()); - FindASTNodes process(doc, &document); - ASTNodes astNodes = process(doc->translationUnit()->ast()); + RemoveCastMethods removeCastMethods(doc, &document); - RemoveCastMethods removeCastMethods(doc, &document); + QList<QTextCursor> baseCastMethodCursors = removeCastMethods(astNodes.base); + QMap<ClassSpecifierAST *, QList<QTextCursor> > cursors; + QMap<ClassSpecifierAST *, QString> replacementCastMethods; - QList<QTextCursor> baseCastMethodCursors = removeCastMethods(astNodes.base); - QMap<ClassSpecifierAST *, QList<QTextCursor> > cursors; - QMap<ClassSpecifierAST *, QString> replacementCastMethods; + Overview oo; - Overview oo; + QStringList castMethods; + foreach (ClassSpecifierAST *classAST, astNodes.deriveds) { + cursors[classAST] = removeCastMethods(classAST); + const QString className = oo(classAST->symbol->name()); + const QString methodName = QLatin1String("as") + className.mid(0, className.length() - 3); + replacementCastMethods[classAST] = QString(" virtual %1 *%2() { return this; }\n").arg(className, methodName); + castMethods.append(QString(" virtual %1 *%2() { return 0; }\n").arg(className, methodName)); + } - QStringList castMethods; - foreach (ClassSpecifierAST *classAST, astNodes.deriveds) { - cursors[classAST] = removeCastMethods(classAST); - const QString className = oo(classAST->symbol->name()); - const QString methodName = QLatin1String("as") + className.mid(0, className.length() - 3); - replacementCastMethods[classAST] = QString(" virtual %1 *%2() { return this; }\n").arg(className, methodName); - castMethods.append(QString(" virtual %1 *%2() { return 0; }").arg(className, methodName)); + if (! baseCastMethodCursors.isEmpty()) { + castMethods.sort(); + for (int i = 0; i < baseCastMethodCursors.length(); ++i) { + baseCastMethodCursors[i].removeSelectedText(); } - if (! baseCastMethodCursors.isEmpty()) { - castMethods.sort(); - for (int i = 0; i < baseCastMethodCursors.length(); ++i) { - baseCastMethodCursors[i].removeSelectedText(); - } + baseCastMethodCursors.first().insertText(castMethods.join(QLatin1String(""))); + } + + for (int classIndex = 0; classIndex < astNodes.deriveds.size(); ++classIndex) { + ClassSpecifierAST *classAST = astNodes.deriveds.at(classIndex); - baseCastMethodCursors.first().insertText(castMethods.join(QLatin1String("\n"))); + // remove the cast methods. + QList<QTextCursor> c = cursors.value(classAST); + for (int i = 0; i < c.length(); ++i) { + c[i].removeSelectedText(); } - for (int classIndex = 0; classIndex < astNodes.deriveds.size(); ++classIndex) { - ClassSpecifierAST *classAST = astNodes.deriveds.at(classIndex); + astNodes.endOfPublicClassSpecifiers[classIndex].insertText(replacementCastMethods.value(classAST)); + } - // remove the cast methods. - QList<QTextCursor> c = cursors.value(classAST); - for (int i = 0; i < c.length(); ++i) { - c[i].removeSelectedText(); - } + if (file.open(QFile::WriteOnly)) { + QTextStream out(&file); + out << document.toPlainText(); + } +} - astNodes.endOfPublicClassSpecifiers[classIndex].insertText(replacementCastMethods.value(classAST)); - } +int main(int argc, char *argv[]) +{ + QCoreApplication app(argc, argv); + QStringList files = app.arguments(); + files.removeFirst(); - std::cout << qPrintable(document.toPlainText()); + if (files.isEmpty()) { + std::cerr << "Usage: cplusplus [path to C++ front-end]" << std::endl; + return EXIT_FAILURE; } + + QDir cplusplusDir(files.first()); + Snapshot snapshot; + + generateAST_H(snapshot, cplusplusDir); }