OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / tests / tools / cplusplus-dump / main.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
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.
18 **
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.
22 **
23 ** Other Usage
24 **
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.
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 #include <AST.h>
34 #include <ASTVisitor.h>
35 #include <ASTPatternBuilder.h>
36 #include <ASTMatcher.h>
37 #include <Control.h>
38 #include <Scope.h>
39 #include <TranslationUnit.h>
40 #include <Literals.h>
41 #include <Symbols.h>
42 #include <Names.h>
43 #include <CoreTypes.h>
44 #include <CppDocument.h>
45 #include <SymbolVisitor.h>
46 #include <Overview.h>
47
48 #include <QFile>
49 #include <QList>
50 #include <QCoreApplication>
51 #include <QStringList>
52 #include <QFileInfo>
53 #include <QTime>
54 #include <QtDebug>
55
56 #include <cstdio>
57 #include <cstdlib>
58 #include <fstream>
59 #include <iostream>
60 #ifdef __GNUC__
61 #  include <cxxabi.h>
62 #endif
63
64 using namespace CPlusPlus;
65
66 class ASTDump: protected ASTVisitor
67 {
68 public:
69     ASTDump(TranslationUnit *unit)
70         : ASTVisitor(unit) {}
71
72     void operator()(AST *ast) {
73         QByteArray basename = translationUnit()->fileName();
74         int dotIdx = basename.lastIndexOf('.');
75         if (dotIdx != -1)
76             basename.truncate(dotIdx);
77         basename.append(".ast.dot");
78         out.open(basename.constData());
79
80         out << "digraph AST { ordering=out;" << std::endl;
81         // std::cout << "rankdir = \"LR\";" << std::endl;
82
83         generateTokens();
84         accept(ast);
85
86         typedef QPair<QByteArray, QByteArray> Pair;
87
88         foreach (const Pair &conn, _connections)
89             out << conn.first.constData() << " -> " << conn.second.constData() << std::endl;
90
91         alignTerminals();
92
93         out << "}" << std::endl;
94         out.close();
95         std::cout << basename.constData() << std::endl;
96     }
97
98     // the following file can be generated by using:
99     //    generate-ast <path to cpp stuff> <path to dumpers.inc>
100 #include "dumpers.inc"
101
102 protected:
103     void alignTerminals() {
104         out<<"{ rank=same;" << std::endl;
105         foreach (const QByteArray &terminalShape, _terminalShapes) {
106             out << "  " << std::string(terminalShape) << ";" << std::endl;
107         }
108         out<<"}"<<std::endl;
109     }
110
111     static QByteArray name(AST *ast) {
112 #ifdef __GNUC__
113         QByteArray name = abi::__cxa_demangle(typeid(*ast).name(), 0, 0, 0) + 11;
114         name.truncate(name.length() - 3);
115 #else
116         QByteArray name = typeid(*ast).name();
117 #endif
118         return name;
119     }
120
121     QByteArray terminalId(unsigned token)
122     { return 't' + QByteArray::number(token); }
123
124     void terminal(unsigned token, AST *node) {
125         _connections.append(qMakePair(_id[node], terminalId(token)));
126     }
127
128     void generateTokens() {
129         for (unsigned token = 1; token < translationUnit()->tokenCount(); ++token) {
130             if (translationUnit()->tokenKind(token) == T_EOF_SYMBOL)
131                 break;
132
133             QByteArray t;
134
135             t.append(terminalId(token));
136             t.append(" [shape=rect label = \"");
137             t.append(spell(token));
138             t.append("\"]");
139
140             if (token > 1) {
141                 t.append("; ");
142                 t.append(terminalId(token - 1));
143                 t.append(" -> ");
144                 t.append(terminalId(token));
145                 t.append(" [arrowhead=\"vee\" color=\"transparent\"]");
146             }
147
148             _terminalShapes.append(t);
149         }
150     }
151
152     virtual void nonterminal(AST *ast) {
153         accept(ast);
154     }
155
156     virtual void node(AST *ast) {
157         out << _id[ast].constData() << " [label=\"" << name(ast).constData() << "\"];" << std::endl;
158     }
159
160     virtual bool preVisit(AST *ast) {
161         static int count = 1;
162         const QByteArray id = 'n' + QByteArray::number(count++);
163         _id[ast] = id;
164
165
166         if (! _stack.isEmpty())
167             _connections.append(qMakePair(_id[_stack.last()], id));
168
169         _stack.append(ast);
170
171         node(ast);
172
173         return true;
174     }
175
176     virtual void postVisit(AST *) {
177         _stack.removeLast();
178     }
179
180 private:
181     QHash<AST *, QByteArray> _id;
182     QList<QPair<QByteArray, QByteArray> > _connections;
183     QList<AST *> _stack;
184     QList<QByteArray> _terminalShapes;
185     std::ofstream out;
186 };
187
188 class SymbolDump: protected SymbolVisitor
189 {
190 public:
191     SymbolDump(TranslationUnit *unit)
192         : translationUnit(unit)
193     {
194         o.setShowArgumentNames(true);
195         o.setShowFunctionSignatures(true);
196         o.setShowReturnTypes(true);
197     }
198
199     void operator()(Symbol *s) {
200         QByteArray basename = translationUnit->fileName();
201         int dotIdx = basename.lastIndexOf('.');
202         if (dotIdx != -1)
203             basename.truncate(dotIdx);
204         basename.append(".symbols.dot");
205         out.open(basename.constData());
206
207         out << "digraph Symbols { ordering=out;" << std::endl;
208         // std::cout << "rankdir = \"LR\";" << std::endl;
209         accept(s);
210
211         for (int i = 0; i < _connections.size(); ++i) {
212             QPair<Symbol*,Symbol*> connection = _connections.at(i);
213             QByteArray from = _id.value(connection.first);
214             if (from.isEmpty())
215                 from = name(connection.first);
216             QByteArray to = _id.value(connection.second);
217             if (to.isEmpty())
218                 to = name(connection.second);
219             out << from.constData() << " -> " << to.constData() << ";" << std::endl;
220         }
221
222         out << "}" << std::endl;
223         out.close();
224         std::cout << basename.constData() << std::endl;
225     }
226
227 protected:
228     QByteArray name(Symbol *s) {
229 #ifdef __GNUC__
230         QByteArray result = abi::__cxa_demangle(typeid(*s).name(), 0, 0, 0) + 11;
231 #else
232         QByteArray result = typeid(*s).name();
233 #endif
234         if (s->identifier()) {
235             result.append("\\nid: ");
236             result.append(s->identifier()->chars());
237         }
238         if (s->isDeprecated())
239             result.append("\\n(deprecated)");
240
241         return result;
242     }
243
244     virtual bool preVisit(Symbol *s) {
245         static int count = 0;
246         QByteArray nodeId("s");
247         nodeId.append(QByteArray::number(++count));
248         _id[s] = nodeId;
249
250         if (!_stack.isEmpty())
251             _connections.append(qMakePair(_stack.last(), s));
252
253         _stack.append(s);
254
255         return true;
256     }
257
258     virtual void postVisit(Symbol *) {
259         _stack.removeLast();
260     }
261
262     virtual void simpleNode(Symbol *symbol) {
263         out << _id[symbol].constData() << " [label=\"" << name(symbol).constData() << "\"];" << std::endl;
264     }
265
266     virtual bool visit(Class *symbol) {
267         const char *id = _id.value(symbol).constData();
268         out << id << " [label=\"";
269         if (symbol->isClass()) {
270             out << "class";
271         } else if (symbol->isStruct()) {
272             out << "struct";
273         } else if (symbol->isUnion()) {
274             out << "union";
275         } else {
276             out << "UNKNOWN";
277         }
278
279         out << "\\nid: ";
280         if (symbol->identifier()) {
281             out << symbol->identifier()->chars();
282         } else {
283             out << "NO ID";
284         }
285         if (symbol->isDeprecated())
286             out << "\\n(deprecated)";
287         out << "\"];" << std::endl;
288
289         return true;
290     }
291
292     virtual bool visit(UsingNamespaceDirective *symbol) { simpleNode(symbol); return true; }
293     virtual bool visit(UsingDeclaration *symbol) { simpleNode(symbol); return true; }
294
295     virtual bool visit(Declaration *symbol) {
296         out << _id[symbol].constData() << " [label=\"";
297         out << "Declaration\\n";
298         out << qPrintable(o(symbol->name()));
299         out << ": ";
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)";
308
309             if (funTy->isSignal())
310                 out << "\\n(signal)";
311             if (funTy->isSlot())
312                 out << "\\n(slot)";
313             if (funTy->isInvokable())
314                 out << "\\n(invokable)";
315         }
316         out << "\"];" << std::endl;
317
318         return true;
319     }
320
321     virtual bool visit(Argument *symbol) { simpleNode(symbol); return true; }
322     virtual bool visit(TypenameArgument *symbol) { simpleNode(symbol); return true; }
323
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;
330
331         return true;
332     }
333
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; }
347
348 private:
349     TranslationUnit *translationUnit;
350     QHash<Symbol *, QByteArray> _id;
351     QList<QPair<Symbol *,Symbol*> >_connections;
352     QList<Symbol *> _stack;
353     std::ofstream out;
354     Overview o;
355 };
356
357 int main(int argc, char *argv[])
358 {
359     QCoreApplication app(argc, argv);
360
361     QStringList files = app.arguments();
362     files.removeFirst();
363
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;
369             continue;
370         }
371
372         const QByteArray source = file.readAll();
373         file.close();
374
375         Document::Ptr doc = Document::create(fileName);
376         doc->control()->setDiagnosticClient(0);
377         doc->setSource(source);
378         doc->parse();
379
380         doc->check();
381
382         ASTDump dump(doc->translationUnit());
383         dump(doc->translationUnit()->ast());
384
385         SymbolDump dump2(doc->translationUnit());
386         dump2(doc->globalNamespace());
387     }
388
389     return EXIT_SUCCESS;
390 }