1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
34 #include <ASTVisitor.h>
35 #include <ASTPatternBuilder.h>
36 #include <ASTMatcher.h>
39 #include <TranslationUnit.h>
43 #include <CoreTypes.h>
44 #include <CppDocument.h>
45 #include <SymbolVisitor.h>
50 #include <QCoreApplication>
51 #include <QStringList>
64 using namespace CPlusPlus;
66 class ASTDump: protected ASTVisitor
69 ASTDump(TranslationUnit *unit)
72 void operator()(AST *ast) {
73 QByteArray basename = translationUnit()->fileName();
74 int dotIdx = basename.lastIndexOf('.');
76 basename.truncate(dotIdx);
77 basename.append(".ast.dot");
78 out.open(basename.constData());
80 out << "digraph AST { ordering=out;" << std::endl;
81 // std::cout << "rankdir = \"LR\";" << std::endl;
86 typedef QPair<QByteArray, QByteArray> Pair;
88 foreach (const Pair &conn, _connections)
89 out << conn.first.constData() << " -> " << conn.second.constData() << std::endl;
93 out << "}" << std::endl;
95 std::cout << basename.constData() << std::endl;
98 // the following file can be generated by using:
99 // generate-ast <path to cpp stuff> <path to dumpers.inc>
100 #include "dumpers.inc"
103 void alignTerminals() {
104 out<<"{ rank=same;" << std::endl;
105 foreach (const QByteArray &terminalShape, _terminalShapes) {
106 out << " " << std::string(terminalShape) << ";" << std::endl;
111 static QByteArray name(AST *ast) {
113 QByteArray name = abi::__cxa_demangle(typeid(*ast).name(), 0, 0, 0) + 11;
114 name.truncate(name.length() - 3);
116 QByteArray name = typeid(*ast).name();
121 QByteArray terminalId(unsigned token)
122 { return 't' + QByteArray::number(token); }
124 void terminal(unsigned token, AST *node) {
125 _connections.append(qMakePair(_id[node], terminalId(token)));
128 void generateTokens() {
129 for (unsigned token = 1; token < translationUnit()->tokenCount(); ++token) {
130 if (translationUnit()->tokenKind(token) == T_EOF_SYMBOL)
135 t.append(terminalId(token));
136 t.append(" [shape=rect label = \"");
137 t.append(spell(token));
142 t.append(terminalId(token - 1));
144 t.append(terminalId(token));
145 t.append(" [arrowhead=\"vee\" color=\"transparent\"]");
148 _terminalShapes.append(t);
152 virtual void nonterminal(AST *ast) {
156 virtual void node(AST *ast) {
157 out << _id[ast].constData() << " [label=\"" << name(ast).constData() << "\"];" << std::endl;
160 virtual bool preVisit(AST *ast) {
161 static int count = 1;
162 const QByteArray id = 'n' + QByteArray::number(count++);
166 if (! _stack.isEmpty())
167 _connections.append(qMakePair(_id[_stack.last()], id));
176 virtual void postVisit(AST *) {
181 QHash<AST *, QByteArray> _id;
182 QList<QPair<QByteArray, QByteArray> > _connections;
184 QList<QByteArray> _terminalShapes;
188 class SymbolDump: protected SymbolVisitor
191 SymbolDump(TranslationUnit *unit)
192 : translationUnit(unit)
194 o.setShowArgumentNames(true);
195 o.setShowFunctionSignatures(true);
196 o.setShowReturnTypes(true);
199 void operator()(Symbol *s) {
200 QByteArray basename = translationUnit->fileName();
201 int dotIdx = basename.lastIndexOf('.');
203 basename.truncate(dotIdx);
204 basename.append(".symbols.dot");
205 out.open(basename.constData());
207 out << "digraph Symbols { ordering=out;" << std::endl;
208 // std::cout << "rankdir = \"LR\";" << std::endl;
211 for (int i = 0; i < _connections.size(); ++i) {
212 QPair<Symbol*,Symbol*> connection = _connections.at(i);
213 QByteArray from = _id.value(connection.first);
215 from = name(connection.first);
216 QByteArray to = _id.value(connection.second);
218 to = name(connection.second);
219 out << from.constData() << " -> " << to.constData() << ";" << std::endl;
222 out << "}" << std::endl;
224 std::cout << basename.constData() << std::endl;
228 QByteArray name(Symbol *s) {
230 QByteArray result = abi::__cxa_demangle(typeid(*s).name(), 0, 0, 0) + 11;
232 QByteArray result = typeid(*s).name();
234 if (s->identifier()) {
235 result.append("\\nid: ");
236 result.append(s->identifier()->chars());
238 if (s->isDeprecated())
239 result.append("\\n(deprecated)");
244 virtual bool preVisit(Symbol *s) {
245 static int count = 0;
246 QByteArray nodeId("s");
247 nodeId.append(QByteArray::number(++count));
250 if (!_stack.isEmpty())
251 _connections.append(qMakePair(_stack.last(), s));
258 virtual void postVisit(Symbol *) {
262 virtual void simpleNode(Symbol *symbol) {
263 out << _id[symbol].constData() << " [label=\"" << name(symbol).constData() << "\"];" << std::endl;
266 virtual bool visit(Class *symbol) {
267 const char *id = _id.value(symbol).constData();
268 out << id << " [label=\"";
269 if (symbol->isClass()) {
271 } else if (symbol->isStruct()) {
273 } else if (symbol->isUnion()) {
280 if (symbol->identifier()) {
281 out << symbol->identifier()->chars();
285 if (symbol->isDeprecated())
286 out << "\\n(deprecated)";
287 out << "\"];" << std::endl;
292 virtual bool visit(UsingNamespaceDirective *symbol) { simpleNode(symbol); return true; }
293 virtual bool visit(UsingDeclaration *symbol) { simpleNode(symbol); return true; }
295 virtual bool visit(Declaration *symbol) {
296 out << _id[symbol].constData() << " [label=\"";
297 out << "Declaration\\n";
298 out << qPrintable(o(symbol->name()));
300 out << qPrintable(o(symbol->type()));
301 if (symbol->isDeprecated())
302 out << "\\n(deprecated)";
303 if (Function *funTy = symbol->type()->asFunctionType()) {
304 if (funTy->isPureVirtual())
305 out << "\\n(pure virtual)";
306 else if (funTy->isVirtual())
307 out << "\\n(virtual)";
309 if (funTy->isSignal())
310 out << "\\n(signal)";
313 if (funTy->isInvokable())
314 out << "\\n(invokable)";
316 out << "\"];" << std::endl;
321 virtual bool visit(Argument *symbol) { simpleNode(symbol); return true; }
322 virtual bool visit(TypenameArgument *symbol) { simpleNode(symbol); return true; }
324 virtual bool visit(BaseClass *symbol) {
325 out << _id[symbol].constData() << " [label=\"BaseClass\\n";
326 out << qPrintable(o(symbol->name()));
327 if (symbol->isDeprecated())
328 out << "\\n(deprecated)";
329 out << "\"];" << std::endl;
334 virtual bool visit(Enum *symbol) { simpleNode(symbol); return true; }
335 virtual bool visit(Function *symbol) { simpleNode(symbol); return true; }
336 virtual bool visit(Namespace *symbol) { simpleNode(symbol); return true; }
337 virtual bool visit(Block *symbol) { simpleNode(symbol); return true; }
338 virtual bool visit(ForwardClassDeclaration *symbol) { simpleNode(symbol); return true; }
339 virtual bool visit(ObjCBaseClass *symbol) { simpleNode(symbol); return true; }
340 virtual bool visit(ObjCBaseProtocol *symbol) { simpleNode(symbol); return true; }
341 virtual bool visit(ObjCClass *symbol) { simpleNode(symbol); return true; }
342 virtual bool visit(ObjCForwardClassDeclaration *symbol) { simpleNode(symbol); return true; }
343 virtual bool visit(ObjCProtocol *symbol) { simpleNode(symbol); return true; }
344 virtual bool visit(ObjCForwardProtocolDeclaration *symbol) { simpleNode(symbol); return true; }
345 virtual bool visit(ObjCMethod *symbol) { simpleNode(symbol); return true; }
346 virtual bool visit(ObjCPropertyDeclaration *symbol) { simpleNode(symbol); return true; }
349 TranslationUnit *translationUnit;
350 QHash<Symbol *, QByteArray> _id;
351 QList<QPair<Symbol *,Symbol*> >_connections;
352 QList<Symbol *> _stack;
357 int main(int argc, char *argv[])
359 QCoreApplication app(argc, argv);
361 QStringList files = app.arguments();
364 foreach (const QString &fileName, files) {
365 QFile file(fileName);
366 if (! file.open(QFile::ReadOnly)) {
367 std::cerr << "Cannot open \"" << qPrintable(fileName)
368 << "\", skipping it." << std::endl;
372 const QByteArray source = file.readAll();
375 Document::Ptr doc = Document::create(fileName);
376 doc->control()->setDiagnosticClient(0);
377 doc->setSource(source);
382 ASTDump dump(doc->translationUnit());
383 dump(doc->translationUnit()->ast());
385 SymbolDump dump2(doc->translationUnit());
386 dump2(doc->globalNamespace());