Commit 5c791dc4 authored by Roberto Raggi's avatar Roberto Raggi

Some code to simplify the debugging of ASTs.

parent 0ef213df
......@@ -63,6 +63,8 @@
#include <Names.h>
#include <NameVisitor.h>
#include <TypeVisitor.h>
#include <ASTVisitor.h>
#include <PrettyPrinter.h>
#include <Lexer.h>
#include <Token.h>
......@@ -70,11 +72,55 @@
#include <QtCore/QMutexLocker>
#include <QtCore/QTime>
#include <QtCore/QTimer>
#include <iostream>
#include <sstream>
using namespace CppTools;
using namespace CppTools::Internal;
using namespace CPlusPlus;
#if defined(QTCREATOR_WITH_DUMP_AST) && defined(Q_CC_GNU)
#include <cxxabi.h>
class DumpAST: protected ASTVisitor
{
public:
int depth;
DumpAST(Control *control)
: ASTVisitor(control), depth(0)
{ }
void operator()(AST *ast)
{ accept(ast); }
protected:
virtual bool preVisit(AST *ast)
{
std::ostringstream s;
PrettyPrinter pp(control(), s);
pp(ast);
QString code = QString::fromStdString(s.str());
code.replace('\n', ' ');
code.replace(QRegExp("\\s+"), " ");
const char *name = abi::__cxa_demangle(typeid(*ast).name(), 0, 0, 0) + 11;
QByteArray ind(depth, ' ');
ind += name;
printf("%-40s %s\n", ind.constData(), qPrintable(code));
++depth;
return true;
}
virtual void postVisit(AST *)
{ --depth; }
};
#endif QTCREATOR_WITH_DUMP_AST
static const char pp_configuration_file[] = "<configuration>";
static const char pp_configuration[] =
......@@ -413,6 +459,12 @@ void CppPreprocessor::sourceNeeded(QString &fileName, IncludeType type,
m_currentDoc->setSource(preprocessedCode);
m_currentDoc->parse();
#if defined(QTCREATOR_WITH_DUMP_AST) && defined(Q_CC_GNU)
DumpAST dump(m_currentDoc->control());
dump(m_currentDoc->translationUnit()->ast());
#endif
m_currentDoc->check();
m_currentDoc->releaseTranslationUnit(); // release the AST and the token stream.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment