[Qt-creator] AST trouble

Marek Jasovsky jasovsky.marek at gmail.com
Mon Feb 16 09:27:00 CET 2009


Hi Roberto

I will be glad if you could spend one minute on this:

I have modified my ASTView to use CppDocument, instead of just
TranslationUnit

below is part of source, showing both ways of getting AST. //part1 works
(first line after //Common part) is commented out

first part works, second works not.

trouble is when calling ast()->data(int col) which was added by me to
AST.cpp (it just prints a string basically)

ast()->data() is called successfully 5 times and then fails for 6th time for
same node, while debugging during 6th call, I see just Managed in ast() but
no TranslationUnitAST data, so system does not know, what virtual function
to call

Visitor works well and passes all nodes ok.

Thanks for help




//part 1
QString fileName("test.cpp");

Document::Ptr doc = Document::create(fileName);

QByteArray src;

QFile file(fileName);

if (file.open(QFile::ReadOnly)) {

//m_included.insert(absoluteFilePath);

QTextStream stream(&file);

const QString contents = stream.readAll();

src = contents.toUtf8();

file.close();

}

QByteArray src1("int i;");

doc->setSource(src1);

doc->parse();

doc->check();


// part 2

Control control;

TranslationUnit::ParseMode mode = TranslationUnit::ParseTranlationUnit;

StringLiteral *fileId = control.findOrInsertFileName("<stdin>");

TranslationUnit *unit = new TranslationUnit(&control, fileId);

unit->setObjCEnabled(true);

QByteArray arr = textEdit->document()->toPlainText().toAscii();

unit->setSource(arr, arr.length());

unit->parse(mode);




//Common part

unit = doc->translationUnit(); // commented if part1 parsing is used



AST *ast = unit->ast();

// listWidget->clear();

// for (unsigned i = 0; i < unit->tokenCount(); i++)

// {

// Token temp = unit->tokenAt(i);

// //textEdit_2->append( QString(" [%4, %5] token %1 : %2 %3
").arg(i).arg(temp.spell()).arg(temp.name(temp.kind)).arg(temp.begin()).arg(temp._length())
);

// listWidget->addItem(QString("Token# %1 = %2").arg(i).arg(temp.spell()));

// }

TreeBuilder *builder = new TreeBuilder(unit->control());

ASTTreeModel* model = new ASTTreeModel();

model->getRootItem()->setAST(ast);

builder->setRootItem(model->getRootItem());

TranslationUnitAST *tast = ast->asTranslationUnit();

tast->accept(builder);

treeView->setModel(model);
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-creator-old/attachments/20090216/cee9efec/attachment.html 


More information about the Qt-creator-old mailing list