[Qt-creator] Extract call graph from QtCreator(2)

Nikolai Kosjar nikolai.kosjar at digia.com
Mon Oct 21 11:37:41 CEST 2013


Hi!

I agree with Tobias regarding the two-step procedure.

How are "similar files" defined and why do you need "all call graphes of 
functions"? This might get quite expensive and only a fraction of it 
might be relevant to the user. I'm not familar with the CodeMap plugin 
of eclipse, but according to the screenshot it display the call graph 
only for a single function?

On 10/20/2013 01:59 AM, 欧三股四 wrote:
> Now, I want to scan all the project files(include .h, .cpp) and in every
> function, find out which functions it calls in order. I think traverse
> the AST is a choice. Here's my problem:
> 1.  How can I get the AST ?

Document::Ptr document = ...;
AST *ast = document->translationUnit()->ast();

Where do you get the document from? Documents are typically kept in a 
Snapshot, which is basically a filePath -> Document::Ptr map. Note that 
the documents in the CppModelManagerInterface::instance()->snapshot() 
have only symbol information, but no source text and AST information 
attached (would take too much memory).

For source files that are opened in an editor you can get the "full" 
document including the AST via CPPEditorWidget::semanticInfo().doc. If 
the source files you are interested in are not opened in an editor, you 
have to read the soure file and parse it by yourself to get a document 
with AST. That's what we are doing for Find Usages, see 
ProcessFile::operator() in cppfindreferences.cpp ("doc->check()" parses 
the document and keeps the AST around). That document is then passed on 
to FindUsages, which is an ASTVisitor.

I recommend you to first study the mentioned code references and their 
code paths.

> 2.  How to filter the symbols I need(For example: all functions)?

As written in a previous reply, you can use CppLocatorData::functions() 
to get all function symbols for the function definitions.

For a given AST, see FindUsages.

> 3.  Is there any other efficient way to do this?

Basically it's fine.

For the call hierarchy of a certain function:

1. Get the symbol for the declaration/definition/call under cursor.
2. Apply your customized version of FindUsages to find all callers and 
callees for the function symbol.
3. Display the call hierachy, e.g. in the side bar.

For the call graph:

...see my question above regarding all call graphs of all functions.

Nikolai



More information about the Qt-creator mailing list