2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-03-08 12:12:58 +01:00
|
|
|
**
|
2013-01-28 17:12:19 +01:00
|
|
|
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
2012-10-02 09:12:39 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2010-03-08 12:12:58 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-03-08 12:12:58 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
2010-03-08 12:12:58 +01:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2010-03-08 12:12:58 +01:00
|
|
|
|
2013-03-27 18:54:03 +01:00
|
|
|
#include <cplusplus/AST.h>
|
|
|
|
|
#include <cplusplus/ASTMatcher.h>
|
|
|
|
|
#include <cplusplus/ASTPatternBuilder.h>
|
|
|
|
|
#include <cplusplus/ASTVisitor.h>
|
|
|
|
|
#include <cplusplus/Control.h>
|
|
|
|
|
#include <cplusplus/CoreTypes.h>
|
|
|
|
|
#include <cplusplus/CppDocument.h>
|
|
|
|
|
#include <cplusplus/Literals.h>
|
|
|
|
|
#include <cplusplus/Names.h>
|
|
|
|
|
#include <cplusplus/Overview.h>
|
|
|
|
|
#include <cplusplus/Scope.h>
|
|
|
|
|
#include <cplusplus/SymbolVisitor.h>
|
|
|
|
|
#include <cplusplus/Symbols.h>
|
|
|
|
|
#include <cplusplus/TranslationUnit.h>
|
2010-03-08 12:12:58 +01:00
|
|
|
|
2012-10-29 13:54:33 +01:00
|
|
|
#include "cplusplus-tools-utils.h"
|
|
|
|
|
|
|
|
|
|
#include <QDir>
|
2010-03-08 12:12:58 +01:00
|
|
|
#include <QFile>
|
|
|
|
|
#include <QList>
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
|
#include <QStringList>
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
#include <QTime>
|
2012-08-06 13:42:46 +02:00
|
|
|
#include <QDebug>
|
2010-03-08 12:12:58 +01:00
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstdlib>
|
2010-03-08 16:42:14 +01:00
|
|
|
#include <fstream>
|
2010-03-08 12:12:58 +01:00
|
|
|
#include <iostream>
|
2013-01-16 12:07:29 +01:00
|
|
|
#include <string>
|
2010-07-13 11:16:17 +02:00
|
|
|
#ifdef __GNUC__
|
|
|
|
|
# include <cxxabi.h>
|
|
|
|
|
#endif
|
2010-03-08 12:12:58 +01:00
|
|
|
|
2012-10-29 13:54:33 +01:00
|
|
|
// For isatty(), _isatty()
|
|
|
|
|
#if defined(Q_OS_WIN)
|
|
|
|
|
# include <io.h>
|
|
|
|
|
#else
|
|
|
|
|
# include <unistd.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
bool tty_for_stdin()
|
|
|
|
|
{
|
|
|
|
|
#if defined(Q_OS_WIN)
|
|
|
|
|
return _isatty(_fileno(stdin));
|
|
|
|
|
#else
|
|
|
|
|
return isatty(fileno(stdin));
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-08 12:12:58 +01:00
|
|
|
using namespace CPlusPlus;
|
|
|
|
|
|
|
|
|
|
class ASTDump: protected ASTVisitor
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
ASTDump(TranslationUnit *unit)
|
|
|
|
|
: ASTVisitor(unit) {}
|
|
|
|
|
|
|
|
|
|
void operator()(AST *ast) {
|
2010-03-08 16:42:14 +01:00
|
|
|
QByteArray basename = translationUnit()->fileName();
|
|
|
|
|
basename.append(".ast.dot");
|
|
|
|
|
out.open(basename.constData());
|
|
|
|
|
|
2010-07-05 19:20:14 +02:00
|
|
|
out << "digraph AST { ordering=out;" << std::endl;
|
2010-03-08 12:21:42 +01:00
|
|
|
// std::cout << "rankdir = \"LR\";" << std::endl;
|
2010-09-21 18:41:09 +02:00
|
|
|
|
|
|
|
|
generateTokens();
|
2010-03-08 12:12:58 +01:00
|
|
|
accept(ast);
|
2010-03-08 16:42:14 +01:00
|
|
|
|
2010-07-05 19:20:14 +02:00
|
|
|
typedef QPair<QByteArray, QByteArray> Pair;
|
|
|
|
|
|
|
|
|
|
foreach (const Pair &conn, _connections)
|
|
|
|
|
out << conn.first.constData() << " -> " << conn.second.constData() << std::endl;
|
2010-03-08 16:42:14 +01:00
|
|
|
|
|
|
|
|
alignTerminals();
|
|
|
|
|
|
|
|
|
|
out << "}" << std::endl;
|
|
|
|
|
out.close();
|
2010-03-08 12:12:58 +01:00
|
|
|
}
|
|
|
|
|
|
2010-07-05 19:20:14 +02:00
|
|
|
// the following file can be generated by using:
|
2012-10-29 13:54:33 +01:00
|
|
|
// cplusplus-update-frontend <frontend-dir> <dumpers-file>
|
2010-07-05 19:20:14 +02:00
|
|
|
#include "dumpers.inc"
|
|
|
|
|
|
2010-03-08 12:12:58 +01:00
|
|
|
protected:
|
2010-03-08 16:42:14 +01:00
|
|
|
void alignTerminals() {
|
2010-03-08 17:34:32 +01:00
|
|
|
out<<"{ rank=same;" << std::endl;
|
2010-03-08 16:42:14 +01:00
|
|
|
foreach (const QByteArray &terminalShape, _terminalShapes) {
|
2013-01-16 12:07:29 +01:00
|
|
|
out << " " << std::string(terminalShape.constData(), terminalShape.size()).c_str() << ";" << std::endl;
|
2010-03-08 16:42:14 +01:00
|
|
|
}
|
|
|
|
|
out<<"}"<<std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-05 19:20:14 +02:00
|
|
|
static QByteArray name(AST *ast) {
|
2010-07-13 11:16:17 +02:00
|
|
|
#ifdef __GNUC__
|
2010-03-08 12:12:58 +01:00
|
|
|
QByteArray name = abi::__cxa_demangle(typeid(*ast).name(), 0, 0, 0) + 11;
|
|
|
|
|
name.truncate(name.length() - 3);
|
2010-07-13 11:16:17 +02:00
|
|
|
#else
|
|
|
|
|
QByteArray name = typeid(*ast).name();
|
|
|
|
|
#endif
|
2010-07-05 19:20:14 +02:00
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-21 18:41:09 +02:00
|
|
|
QByteArray terminalId(unsigned token)
|
|
|
|
|
{ return 't' + QByteArray::number(token); }
|
|
|
|
|
|
2010-07-05 19:20:14 +02:00
|
|
|
void terminal(unsigned token, AST *node) {
|
2010-09-21 18:41:09 +02:00
|
|
|
_connections.append(qMakePair(_id[node], terminalId(token)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void generateTokens() {
|
|
|
|
|
for (unsigned token = 1; token < translationUnit()->tokenCount(); ++token) {
|
|
|
|
|
if (translationUnit()->tokenKind(token) == T_EOF_SYMBOL)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
QByteArray t;
|
|
|
|
|
|
|
|
|
|
t.append(terminalId(token));
|
|
|
|
|
t.append(" [shape=rect label = \"");
|
|
|
|
|
t.append(spell(token));
|
|
|
|
|
t.append("\"]");
|
|
|
|
|
|
|
|
|
|
if (token > 1) {
|
|
|
|
|
t.append("; ");
|
|
|
|
|
t.append(terminalId(token - 1));
|
|
|
|
|
t.append(" -> ");
|
|
|
|
|
t.append(terminalId(token));
|
|
|
|
|
t.append(" [arrowhead=\"vee\" color=\"transparent\"]");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_terminalShapes.append(t);
|
|
|
|
|
}
|
2010-07-05 19:20:14 +02:00
|
|
|
}
|
2010-03-08 16:42:14 +01:00
|
|
|
|
2010-07-05 19:20:14 +02:00
|
|
|
virtual void nonterminal(AST *ast) {
|
|
|
|
|
accept(ast);
|
|
|
|
|
}
|
2010-03-08 12:12:58 +01:00
|
|
|
|
2010-07-05 19:20:14 +02:00
|
|
|
virtual void node(AST *ast) {
|
|
|
|
|
out << _id[ast].constData() << " [label=\"" << name(ast).constData() << "\"];" << std::endl;
|
2010-03-08 12:12:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool preVisit(AST *ast) {
|
|
|
|
|
static int count = 1;
|
2010-07-05 19:20:14 +02:00
|
|
|
const QByteArray id = 'n' + QByteArray::number(count++);
|
|
|
|
|
_id[ast] = id;
|
|
|
|
|
|
2010-03-08 12:12:58 +01:00
|
|
|
|
|
|
|
|
if (! _stack.isEmpty())
|
2010-07-05 19:20:14 +02:00
|
|
|
_connections.append(qMakePair(_id[_stack.last()], id));
|
2010-03-08 12:12:58 +01:00
|
|
|
|
|
|
|
|
_stack.append(ast);
|
|
|
|
|
|
2010-07-05 19:20:14 +02:00
|
|
|
node(ast);
|
|
|
|
|
|
2010-03-08 12:12:58 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-08 12:21:42 +01:00
|
|
|
virtual void postVisit(AST *) {
|
2010-03-08 16:42:14 +01:00
|
|
|
_stack.removeLast();
|
2010-03-08 12:12:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2010-07-05 19:20:14 +02:00
|
|
|
QHash<AST *, QByteArray> _id;
|
|
|
|
|
QList<QPair<QByteArray, QByteArray> > _connections;
|
2010-03-08 12:12:58 +01:00
|
|
|
QList<AST *> _stack;
|
2010-07-05 19:20:14 +02:00
|
|
|
QList<QByteArray> _terminalShapes;
|
2010-03-08 16:42:14 +01:00
|
|
|
std::ofstream out;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class SymbolDump: protected SymbolVisitor
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
SymbolDump(TranslationUnit *unit)
|
|
|
|
|
: translationUnit(unit)
|
2010-03-08 17:31:14 +01:00
|
|
|
{
|
2012-10-23 10:50:38 +02:00
|
|
|
o.showArgumentNames = true;
|
|
|
|
|
o.showFunctionSignatures = true;
|
|
|
|
|
o.showReturnTypes = true;
|
2010-03-08 17:31:14 +01:00
|
|
|
}
|
2010-03-08 16:42:14 +01:00
|
|
|
|
|
|
|
|
void operator()(Symbol *s) {
|
|
|
|
|
QByteArray basename = translationUnit->fileName();
|
|
|
|
|
basename.append(".symbols.dot");
|
|
|
|
|
out.open(basename.constData());
|
|
|
|
|
|
2010-07-05 19:20:14 +02:00
|
|
|
out << "digraph Symbols { ordering=out;" << std::endl;
|
2010-03-08 16:42:14 +01:00
|
|
|
// std::cout << "rankdir = \"LR\";" << std::endl;
|
|
|
|
|
accept(s);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < _connections.size(); ++i) {
|
|
|
|
|
QPair<Symbol*,Symbol*> connection = _connections.at(i);
|
|
|
|
|
QByteArray from = _id.value(connection.first);
|
|
|
|
|
if (from.isEmpty())
|
|
|
|
|
from = name(connection.first);
|
|
|
|
|
QByteArray to = _id.value(connection.second);
|
|
|
|
|
if (to.isEmpty())
|
|
|
|
|
to = name(connection.second);
|
|
|
|
|
out << from.constData() << " -> " << to.constData() << ";" << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out << "}" << std::endl;
|
|
|
|
|
out.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
QByteArray name(Symbol *s) {
|
2010-07-13 11:16:17 +02:00
|
|
|
#ifdef __GNUC__
|
2010-03-23 11:59:32 +01:00
|
|
|
QByteArray result = abi::__cxa_demangle(typeid(*s).name(), 0, 0, 0) + 11;
|
2010-07-13 11:16:17 +02:00
|
|
|
#else
|
|
|
|
|
QByteArray result = typeid(*s).name();
|
|
|
|
|
#endif
|
2010-03-08 16:42:14 +01:00
|
|
|
if (s->identifier()) {
|
2010-03-23 11:59:32 +01:00
|
|
|
result.append("\\nid: ");
|
|
|
|
|
result.append(s->identifier()->chars());
|
2010-03-08 16:42:14 +01:00
|
|
|
}
|
2010-03-23 11:59:32 +01:00
|
|
|
if (s->isDeprecated())
|
|
|
|
|
result.append("\\n(deprecated)");
|
2010-03-08 16:42:14 +01:00
|
|
|
|
2010-03-23 11:59:32 +01:00
|
|
|
return result;
|
2010-03-08 16:42:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool preVisit(Symbol *s) {
|
|
|
|
|
static int count = 0;
|
|
|
|
|
QByteArray nodeId("s");
|
|
|
|
|
nodeId.append(QByteArray::number(++count));
|
|
|
|
|
_id[s] = nodeId;
|
|
|
|
|
|
|
|
|
|
if (!_stack.isEmpty())
|
|
|
|
|
_connections.append(qMakePair(_stack.last(), s));
|
|
|
|
|
|
|
|
|
|
_stack.append(s);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void postVisit(Symbol *) {
|
|
|
|
|
_stack.removeLast();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void simpleNode(Symbol *symbol) {
|
|
|
|
|
out << _id[symbol].constData() << " [label=\"" << name(symbol).constData() << "\"];" << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool visit(Class *symbol) {
|
|
|
|
|
const char *id = _id.value(symbol).constData();
|
|
|
|
|
out << id << " [label=\"";
|
|
|
|
|
if (symbol->isClass()) {
|
|
|
|
|
out << "class";
|
|
|
|
|
} else if (symbol->isStruct()) {
|
|
|
|
|
out << "struct";
|
|
|
|
|
} else if (symbol->isUnion()) {
|
|
|
|
|
out << "union";
|
|
|
|
|
} else {
|
|
|
|
|
out << "UNKNOWN";
|
|
|
|
|
}
|
2010-03-08 17:31:14 +01:00
|
|
|
|
2010-03-08 16:42:14 +01:00
|
|
|
out << "\\nid: ";
|
|
|
|
|
if (symbol->identifier()) {
|
|
|
|
|
out << symbol->identifier()->chars();
|
|
|
|
|
} else {
|
|
|
|
|
out << "NO ID";
|
|
|
|
|
}
|
2010-03-23 11:59:32 +01:00
|
|
|
if (symbol->isDeprecated())
|
|
|
|
|
out << "\\n(deprecated)";
|
2010-03-08 16:42:14 +01:00
|
|
|
out << "\"];" << std::endl;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool visit(UsingNamespaceDirective *symbol) { simpleNode(symbol); return true; }
|
|
|
|
|
virtual bool visit(UsingDeclaration *symbol) { simpleNode(symbol); return true; }
|
2010-03-08 17:31:14 +01:00
|
|
|
|
|
|
|
|
virtual bool visit(Declaration *symbol) {
|
|
|
|
|
out << _id[symbol].constData() << " [label=\"";
|
|
|
|
|
out << "Declaration\\n";
|
|
|
|
|
out << qPrintable(o(symbol->name()));
|
|
|
|
|
out << ": ";
|
|
|
|
|
out << qPrintable(o(symbol->type()));
|
2010-03-23 11:59:32 +01:00
|
|
|
if (symbol->isDeprecated())
|
|
|
|
|
out << "\\n(deprecated)";
|
2010-08-24 12:14:30 +02:00
|
|
|
if (Function *funTy = symbol->type()->asFunctionType()) {
|
|
|
|
|
if (funTy->isPureVirtual())
|
|
|
|
|
out << "\\n(pure virtual)";
|
|
|
|
|
else if (funTy->isVirtual())
|
|
|
|
|
out << "\\n(virtual)";
|
|
|
|
|
|
|
|
|
|
if (funTy->isSignal())
|
|
|
|
|
out << "\\n(signal)";
|
|
|
|
|
if (funTy->isSlot())
|
|
|
|
|
out << "\\n(slot)";
|
|
|
|
|
if (funTy->isInvokable())
|
|
|
|
|
out << "\\n(invokable)";
|
|
|
|
|
}
|
2010-03-08 17:31:14 +01:00
|
|
|
out << "\"];" << std::endl;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-08 16:42:14 +01:00
|
|
|
virtual bool visit(Argument *symbol) { simpleNode(symbol); return true; }
|
|
|
|
|
virtual bool visit(TypenameArgument *symbol) { simpleNode(symbol); return true; }
|
2010-03-08 17:31:14 +01:00
|
|
|
|
|
|
|
|
virtual bool visit(BaseClass *symbol) {
|
|
|
|
|
out << _id[symbol].constData() << " [label=\"BaseClass\\n";
|
|
|
|
|
out << qPrintable(o(symbol->name()));
|
2010-03-23 11:59:32 +01:00
|
|
|
if (symbol->isDeprecated())
|
|
|
|
|
out << "\\n(deprecated)";
|
2010-03-08 17:31:14 +01:00
|
|
|
out << "\"];" << std::endl;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-08 16:42:14 +01:00
|
|
|
virtual bool visit(Enum *symbol) { simpleNode(symbol); return true; }
|
|
|
|
|
virtual bool visit(Function *symbol) { simpleNode(symbol); return true; }
|
|
|
|
|
virtual bool visit(Namespace *symbol) { simpleNode(symbol); return true; }
|
|
|
|
|
virtual bool visit(Block *symbol) { simpleNode(symbol); return true; }
|
|
|
|
|
virtual bool visit(ForwardClassDeclaration *symbol) { simpleNode(symbol); return true; }
|
|
|
|
|
virtual bool visit(ObjCBaseClass *symbol) { simpleNode(symbol); return true; }
|
|
|
|
|
virtual bool visit(ObjCBaseProtocol *symbol) { simpleNode(symbol); return true; }
|
|
|
|
|
virtual bool visit(ObjCClass *symbol) { simpleNode(symbol); return true; }
|
|
|
|
|
virtual bool visit(ObjCForwardClassDeclaration *symbol) { simpleNode(symbol); return true; }
|
|
|
|
|
virtual bool visit(ObjCProtocol *symbol) { simpleNode(symbol); return true; }
|
|
|
|
|
virtual bool visit(ObjCForwardProtocolDeclaration *symbol) { simpleNode(symbol); return true; }
|
|
|
|
|
virtual bool visit(ObjCMethod *symbol) { simpleNode(symbol); return true; }
|
|
|
|
|
virtual bool visit(ObjCPropertyDeclaration *symbol) { simpleNode(symbol); return true; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
TranslationUnit *translationUnit;
|
|
|
|
|
QHash<Symbol *, QByteArray> _id;
|
|
|
|
|
QList<QPair<Symbol *,Symbol*> >_connections;
|
|
|
|
|
QList<Symbol *> _stack;
|
|
|
|
|
std::ofstream out;
|
2010-03-08 17:31:14 +01:00
|
|
|
Overview o;
|
2010-03-08 12:12:58 +01:00
|
|
|
};
|
|
|
|
|
|
2013-01-31 16:23:39 +01:00
|
|
|
static void createImageFromDot(const QString &inputFile, const QString &outputFile, bool verbose)
|
2012-10-29 13:54:33 +01:00
|
|
|
{
|
|
|
|
|
const QString command = CplusplusToolsUtils::portableExecutableName(QLatin1String("dot"));
|
|
|
|
|
const QStringList arguments = QStringList()
|
|
|
|
|
<< QLatin1String("-Tpng") << QLatin1String("-o") << outputFile << inputFile;
|
|
|
|
|
CplusplusToolsUtils::executeCommand(command, arguments, QString(), verbose);
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-31 16:23:39 +01:00
|
|
|
static const char PATH_STDIN_FILE[] = "_stdincontents.cpp";
|
2012-10-29 13:54:33 +01:00
|
|
|
|
2013-01-31 16:23:39 +01:00
|
|
|
static QString example()
|
2012-10-29 13:54:33 +01:00
|
|
|
{
|
|
|
|
|
return
|
|
|
|
|
#if defined(Q_OS_WIN)
|
|
|
|
|
QString::fromLatin1("> echo int foo() {} | %1 && %2.ast.png")
|
|
|
|
|
#elif defined(Q_OS_MAC)
|
|
|
|
|
QString::fromLatin1("$ echo \"int foo() {}\" | ./%1 && open %2.ast.png")
|
|
|
|
|
#else
|
|
|
|
|
QString::fromLatin1("$ echo \"int foo() {}\" | ./%1 && xdg-open %2.ast.png")
|
|
|
|
|
#endif
|
2012-12-04 11:07:08 +01:00
|
|
|
.arg(QFileInfo(qApp->arguments().at(0)).fileName(), QLatin1String(PATH_STDIN_FILE));
|
2012-10-29 13:54:33 +01:00
|
|
|
}
|
|
|
|
|
|
2013-01-31 16:23:39 +01:00
|
|
|
static QString parseModeToString(Document::ParseMode parseMode)
|
|
|
|
|
{
|
|
|
|
|
switch (parseMode) {
|
|
|
|
|
case Document::ParseTranlationUnit:
|
|
|
|
|
return QLatin1String("TranlationUnit");
|
|
|
|
|
case Document::ParseDeclaration:
|
|
|
|
|
return QLatin1String("Declaration");
|
|
|
|
|
case Document::ParseExpression:
|
|
|
|
|
return QLatin1String("Expression");
|
|
|
|
|
case Document::ParseDeclarator:
|
|
|
|
|
return QLatin1String("Declarator");
|
|
|
|
|
case Document::ParseStatement:
|
|
|
|
|
return QLatin1String("Statement");
|
|
|
|
|
default:
|
|
|
|
|
return QLatin1String("UnknownParseMode");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Counts errors and appends error messages containing the parse mode to an error string
|
|
|
|
|
class ErrorHandler: public DiagnosticClient {
|
|
|
|
|
public:
|
|
|
|
|
int m_errorCount;
|
|
|
|
|
QByteArray *m_errorString;
|
|
|
|
|
Document::ParseMode m_parseMode;
|
|
|
|
|
|
|
|
|
|
ErrorHandler(Document::ParseMode parseMode, QByteArray *errorStringOutput)
|
|
|
|
|
: m_errorCount(0)
|
|
|
|
|
, m_errorString(errorStringOutput)
|
|
|
|
|
, m_parseMode(parseMode) {}
|
|
|
|
|
|
|
|
|
|
void report(int level,
|
|
|
|
|
const StringLiteral *fileName,
|
|
|
|
|
unsigned line, unsigned column,
|
|
|
|
|
const char *format, va_list ap)
|
|
|
|
|
{
|
|
|
|
|
++m_errorCount;
|
|
|
|
|
|
|
|
|
|
if (! m_errorString)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
static const char *const pretty[] = { "warning", "error", "fatal" };
|
|
|
|
|
|
|
|
|
|
QString str;
|
|
|
|
|
str.sprintf("%s:%d:%d: When parsing as %s: %s: ", fileName->chars(), line, column,
|
|
|
|
|
parseModeToString(m_parseMode).toUtf8().constData(), pretty[level]);
|
|
|
|
|
m_errorString->append(str.toUtf8());
|
|
|
|
|
|
|
|
|
|
str.vsprintf(format, ap);
|
|
|
|
|
m_errorString->append(str.toUtf8());
|
|
|
|
|
m_errorString->append('\n');
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// Try to parse with given parseModes. Returns a document pointer if it was possible to
|
|
|
|
|
/// successfully parse with one of the given parseModes (one parse mode after the other
|
|
|
|
|
/// is tried), otherwise a null pointer.
|
|
|
|
|
static Document::Ptr parse(const QString &fileName, const QByteArray &source,
|
|
|
|
|
QList<Document::ParseMode> parseModes, QByteArray *errors,
|
|
|
|
|
bool verbose = false)
|
|
|
|
|
{
|
|
|
|
|
foreach (const Document::ParseMode parseMode, parseModes) {
|
|
|
|
|
ErrorHandler *errorHandler = new ErrorHandler(parseMode, errors); // Deleted by ~Document.
|
|
|
|
|
if (verbose)
|
|
|
|
|
std::cout << "Parsing as " << qPrintable(parseModeToString(parseMode)) << "...";
|
|
|
|
|
|
|
|
|
|
Document::Ptr doc = Document::create(fileName);
|
|
|
|
|
doc->control()->setDiagnosticClient(errorHandler);
|
|
|
|
|
doc->setUtf8Source(source);
|
|
|
|
|
const bool parsed = doc->parse(parseMode);
|
|
|
|
|
if (parsed && errorHandler->m_errorCount == 0) {
|
|
|
|
|
if (verbose)
|
|
|
|
|
std::cout << "succeeded." << std::endl;
|
|
|
|
|
return doc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
|
std::cout << "failed." << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Document::Ptr();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Convenience function
|
|
|
|
|
static Document::Ptr parse(const QString &fileName, const QByteArray &source,
|
|
|
|
|
Document::ParseMode parseMode, QByteArray *errors,
|
|
|
|
|
bool verbose = false)
|
|
|
|
|
{
|
|
|
|
|
QList<Document::ParseMode> parseModes = QList<Document::ParseMode>() << parseMode;
|
|
|
|
|
return parse(fileName, source, parseModes, errors, verbose);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void printUsage()
|
2012-10-29 13:54:33 +01:00
|
|
|
{
|
|
|
|
|
std::cout << "Usage: " << qPrintable(QFileInfo(qApp->arguments().at(0)).fileName())
|
2013-01-31 16:23:39 +01:00
|
|
|
<< " [-v] [-p ast] <file1> <file2> ...\n\n";
|
|
|
|
|
|
|
|
|
|
std::cout
|
|
|
|
|
<< "Visualize AST and symbol hierarchy of given C++ files by generating png image files\n"
|
|
|
|
|
<< "in the same directory as the input files. Print paths to generated image files.\n"
|
|
|
|
|
<< "\n"
|
|
|
|
|
<< "Options:\n"
|
|
|
|
|
<< " -v Run with increased verbosity.\n"
|
|
|
|
|
<< " -p <ast> Parse each file as <ast>. <ast> is one of:\n"
|
|
|
|
|
<< " - 'declarator' or 'dr'\n"
|
|
|
|
|
<< " - 'expression' or 'ex'\n"
|
|
|
|
|
<< " - 'declaration' or 'dn'\n"
|
|
|
|
|
<< " - 'statement' or 'st'\n"
|
|
|
|
|
<< " - 'translationunit' or 'tr'\n"
|
|
|
|
|
<< " If this option is not provided, each file is tried to be parsed as\n"
|
|
|
|
|
<< " declarator, expression, etc. using the stated order.\n"
|
|
|
|
|
<< "\n";
|
2012-10-29 13:54:33 +01:00
|
|
|
|
2012-12-04 11:07:08 +01:00
|
|
|
std::cout << QString::fromLatin1(
|
2013-01-31 16:23:39 +01:00
|
|
|
"Standard input is also read. The resulting files start with \"%1\"\n"
|
2012-10-29 13:54:33 +01:00
|
|
|
"and are created in the current working directory. To show the AST for simple snippets\n"
|
|
|
|
|
"you might want to execute:\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" %2\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"Prerequisites:\n"
|
|
|
|
|
" 1) Make sure to have 'dot' from graphviz locatable by PATH.\n"
|
|
|
|
|
" 2) Make sure to have an up to date dumpers file by using 'cplusplus-update-frontend'.\n"
|
2012-12-04 11:07:08 +01:00
|
|
|
).arg(QLatin1String(PATH_STDIN_FILE), example()).toLocal8Bit().constData();
|
2012-10-29 13:54:33 +01:00
|
|
|
}
|
|
|
|
|
|
2010-03-08 12:12:58 +01:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
QCoreApplication app(argc, argv);
|
2012-10-29 13:54:33 +01:00
|
|
|
QStringList args = app.arguments();
|
|
|
|
|
args.removeFirst();
|
|
|
|
|
|
|
|
|
|
bool optionVerbose = false;
|
2013-01-31 16:23:39 +01:00
|
|
|
int optionParseMode = -1;
|
2012-10-29 13:54:33 +01:00
|
|
|
|
2013-01-31 16:23:39 +01:00
|
|
|
// Test only for stdin if not input files are specified.
|
|
|
|
|
const bool doTestForStdIn = args.isEmpty()
|
|
|
|
|
|| (args.count() == 1 && args.contains(QLatin1String("-v")));
|
|
|
|
|
if (doTestForStdIn && !tty_for_stdin()) {
|
2012-12-04 11:07:08 +01:00
|
|
|
QFile file((QLatin1String(PATH_STDIN_FILE)));
|
2012-10-29 13:54:33 +01:00
|
|
|
if (! file.open(QFile::WriteOnly)) {
|
|
|
|
|
std::cerr << "Error: Cannot open file for writing\"" << qPrintable(file.fileName())
|
|
|
|
|
<< "\"" << std::endl;
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
file.write(QTextStream(stdin).readAll().toLocal8Bit());
|
|
|
|
|
file.close();
|
|
|
|
|
args.append(file.fileName());
|
|
|
|
|
}
|
2010-03-08 12:12:58 +01:00
|
|
|
|
2012-10-29 13:54:33 +01:00
|
|
|
// Process options & arguments
|
2013-01-31 16:23:39 +01:00
|
|
|
const bool helpRequested = args.contains(QLatin1String("-h"))
|
|
|
|
|
|| args.contains(QLatin1String("-help"));
|
|
|
|
|
if (helpRequested) {
|
|
|
|
|
printUsage();
|
|
|
|
|
return helpRequested ? EXIT_SUCCESS : EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-04 11:07:08 +01:00
|
|
|
if (args.contains(QLatin1String("-v"))) {
|
2012-10-29 13:54:33 +01:00
|
|
|
optionVerbose = true;
|
2012-12-04 11:07:08 +01:00
|
|
|
args.removeOne(QLatin1String("-v"));
|
2012-10-29 13:54:33 +01:00
|
|
|
}
|
2013-01-31 16:23:39 +01:00
|
|
|
if (args.contains(QLatin1String("-p"))) {
|
|
|
|
|
args.removeOne(QLatin1String("-p"));
|
|
|
|
|
if (args.isEmpty()) {
|
|
|
|
|
std::cerr << "Error: Expected ast after option \"-p\"." << std::endl;
|
|
|
|
|
printUsage();
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
const QString parseAs = args.first();
|
|
|
|
|
if (parseAs == QLatin1String("declarator") || parseAs == QLatin1String("dr"))
|
|
|
|
|
optionParseMode = Document::ParseDeclarator;
|
|
|
|
|
else if (parseAs == QLatin1String("expression") || parseAs == QLatin1String("ex"))
|
|
|
|
|
optionParseMode = Document::ParseExpression;
|
|
|
|
|
else if (parseAs == QLatin1String("declaration") || parseAs == QLatin1String("dn"))
|
|
|
|
|
optionParseMode = Document::ParseDeclaration;
|
|
|
|
|
else if (parseAs == QLatin1String("statement") || parseAs == QLatin1String("st"))
|
|
|
|
|
optionParseMode = Document::ParseStatement;
|
|
|
|
|
else if (parseAs == QLatin1String("translationunit") || parseAs == QLatin1String("tr"))
|
|
|
|
|
optionParseMode = Document::ParseTranlationUnit;
|
|
|
|
|
else {
|
|
|
|
|
std::cerr << "Error: Invalid ast for option \"-p\"." << std::endl;
|
|
|
|
|
printUsage();
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
args.removeOne(parseAs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (args.isEmpty()) {
|
2012-10-29 13:54:33 +01:00
|
|
|
printUsage();
|
2013-01-31 16:23:39 +01:00
|
|
|
return EXIT_SUCCESS;
|
2012-10-29 13:54:33 +01:00
|
|
|
}
|
2010-03-08 12:12:58 +01:00
|
|
|
|
2012-10-29 13:54:33 +01:00
|
|
|
// Process files
|
|
|
|
|
const QStringList files = args;
|
2010-03-08 12:12:58 +01:00
|
|
|
foreach (const QString &fileName, files) {
|
2012-10-29 13:54:33 +01:00
|
|
|
if (! QFile::exists(fileName)) {
|
|
|
|
|
std::cerr << "Error: File \"" << qPrintable(fileName) << "\" does not exist."
|
|
|
|
|
<< std::endl;
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Run the preprocessor
|
|
|
|
|
const QString fileNamePreprocessed = fileName + QLatin1String(".preprocessed");
|
|
|
|
|
CplusplusToolsUtils::SystemPreprocessor preprocessor(optionVerbose);
|
|
|
|
|
preprocessor.preprocessFile(fileName, fileNamePreprocessed);
|
|
|
|
|
|
|
|
|
|
// Convert to dot
|
|
|
|
|
QFile file(fileNamePreprocessed);
|
2010-03-08 16:42:14 +01:00
|
|
|
if (! file.open(QFile::ReadOnly)) {
|
2012-10-29 13:54:33 +01:00
|
|
|
std::cerr << "Error: Could not open file \"" << qPrintable(fileNamePreprocessed)
|
|
|
|
|
<< "\"" << std::endl;
|
|
|
|
|
exit(EXIT_FAILURE);
|
2010-03-08 16:42:14 +01:00
|
|
|
}
|
2010-03-08 12:12:58 +01:00
|
|
|
|
|
|
|
|
const QByteArray source = file.readAll();
|
|
|
|
|
file.close();
|
|
|
|
|
|
2013-01-31 16:23:39 +01:00
|
|
|
// Parse Document
|
|
|
|
|
QByteArray errors;
|
|
|
|
|
Document::Ptr doc;
|
|
|
|
|
if (optionParseMode == -1) {
|
|
|
|
|
QList<Document::ParseMode> parseModes;
|
|
|
|
|
parseModes
|
|
|
|
|
<< Document::ParseDeclarator
|
|
|
|
|
<< Document::ParseExpression
|
|
|
|
|
<< Document::ParseDeclaration
|
|
|
|
|
<< Document::ParseStatement
|
|
|
|
|
<< Document::ParseTranlationUnit;
|
|
|
|
|
doc = parse(fileName, source, parseModes, &errors, optionVerbose);
|
|
|
|
|
} else {
|
|
|
|
|
doc = parse(fileName, source, static_cast<Document::ParseMode>(optionParseMode),
|
|
|
|
|
&errors, optionVerbose);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!doc) {
|
|
|
|
|
std::cerr << "Error: Could not parse file \"" << qPrintable(fileName) << "\".\n";
|
|
|
|
|
std::cerr << errors.constData();
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-08 16:42:14 +01:00
|
|
|
doc->check();
|
|
|
|
|
|
2013-01-31 16:23:39 +01:00
|
|
|
// Run AST dumper
|
2010-03-08 12:12:58 +01:00
|
|
|
ASTDump dump(doc->translationUnit());
|
|
|
|
|
dump(doc->translationUnit()->ast());
|
2010-03-08 16:42:14 +01:00
|
|
|
|
|
|
|
|
SymbolDump dump2(doc->translationUnit());
|
|
|
|
|
dump2(doc->globalNamespace());
|
2012-10-29 13:54:33 +01:00
|
|
|
|
|
|
|
|
// Create images
|
|
|
|
|
typedef QPair<QString, QString> Pair;
|
|
|
|
|
QList<Pair> inputOutputFiles;
|
|
|
|
|
inputOutputFiles.append(qMakePair(QString(fileName + QLatin1String(".ast.dot")),
|
|
|
|
|
QString(fileName + QLatin1String(".ast.png"))));
|
|
|
|
|
inputOutputFiles.append(qMakePair(QString(fileName + QLatin1String(".symbols.dot")),
|
|
|
|
|
QString(fileName + QLatin1String(".symbols.png"))));
|
|
|
|
|
foreach (const Pair &pair, inputOutputFiles) {
|
|
|
|
|
createImageFromDot(pair.first, pair.second, optionVerbose);
|
|
|
|
|
std::cout << qPrintable(QDir::toNativeSeparators(pair.second)) << std::endl;
|
|
|
|
|
}
|
2010-03-08 12:12:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
}
|