[Qt-creator] Extract complete prototype of a function from AST

dydx 549088764 at qq.com
Wed Oct 30 17:04:45 CET 2013


Hello all, I have read the AST traverse code in FindUsages class. It seems that a AST just organize all tokens in a file into a tree. Each node in the tree corresponds a syntax element(For example, a statement, or an expression). Also, a leaf node will carry a token index, which can be used to query the actual token by calling ASTVisitor::tokenAt(idx).  

Here's my code for building AST.

    const CppTools::CppModelManagerInterface::WorkingCopy workingCopy = mgr->workingCopy();
    QString fileName = doc->fileName();
    const QByteArray unpreprocessedSource = getSource(fileName, workingCopy);

    doc = snapshot.preprocessedDocument(unpreprocessedSource, fileName);
    doc->tokenize();
    //doc->check();    // This invocation will crash the program, because Q_ASSERT(!_globalNamespace) in it fails, why ?
    doc->parse();

    // It seems that both AST are the same, isn't it ?
    CPlusPlus::AST* ast = doc->translationUnit()->ast();            
    CPlusPlus::AST* ast2 = doc->control()->translationUnit()->ast();

    CPlusPlus::TranslationUnitAST* trAst = ast->asTranslationUnit();

OK, now I have some problems:
(1) Is the code above right? Why doc->check() will crash?
(2) How to release memory when I don't need the AST any more? Just call Document::releaseSourceAndAST() ?
(3) Most importantly, as I want to extract the call graph, I want to get the complete and exact declaration of any function I encounter.
     For example,  there are several overloaded versions of the following function:
     int ClassA::funcA();
     int ClassA::funcA(int varA);
     Suppose when I traverse the AST, I encounter "objA->funcA(5);" . So how can I find its prototype "int ClassA::funcA(int varA);", 
     instead of "int ClassA::funcA();"? Is there any "binding" for a ast element, such as the definition of a function?
(4) An extra question, when I debugged the QtCreator code in visual studio, I found it sometimes took a lot of time for QtCreator to 
     parse the source after a project was opened, but quite fast (several seconds) in other times. This was really strange because the 
     project was the same. That wasted me a lot of time. How to solve it ?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/qt-creator/attachments/20131031/66cdb219/attachment.html>


More information about the Qt-creator mailing list