[cfe-dev] [Solved] Libclang. Cross-referencing translation units with Unified Symbol Resolution USR.

Juan Jose Lopez Villarejo jj.lopezvillarejo at cern.ch
Wed Jan 8 07:40:05 PST 2014


Credits to Jesse van den Kieboom

________________________________
From: Jesse van den Kieboom [jesse.vandenkieboom at epfl.ch]
Sent: 08 January 2014 15:37
To: Juan Jose Lopez Villarejo
Subject: Re: Libclang. Cross-referencing translation units with Unified Symbol Resolution USR.

Hi Juan,

... with regard to your question it is indeed correct that you need to parse all files as TU in the same index. So far so good. Then, to find cursors to which a reference cursor references, you need to use the clang_getCursorReferenced<http://clang.llvm.org/doxygen/group__CINDEX__CURSOR__XREF.html#gabf059155921552e19fc2abed5b4ff73a> method. I guess it depends on your version of the clang python bindings, but the version that I have here doesn't expose this API directly on the cursor instance. Strangely though, it does define the ctypes prototype (it's just not wrapped on CXCursor). So I just did a quick test using the following snippet:

def get_referenced(self):
    return clang.cindex.conf.lib.clang_getCursorReferenced(self)

clang.cindex.Cursor.get_referenced = get_referenced

Afterwards, try calling cursor.get_referenced(). I tried this on a simple file and a cursor of type clang.cindex.CursorKind.CALL_EXPR. Doing so I get a cursor to the declaration of the call in the corresponding header file.

I hope this helps you along,


Jesse



2014/1/8 Juan Jose Lopez Villarejo <jj.lopezvillarejo at cern.ch<mailto:jj.lopezvillarejo at cern.ch>>


Début du message réexpédié :

Hello,

I have spent the last months in developing a documentation tool based on lib clang & its python bindings, which can generate activity diagrams from annotated C++ code. It works for a single translation unit, which in my case is a single .cpp source file (e.g., ‘main.cpp’). Now I would like to make this tool work properly for a code consisting of several source files.

Statement of the issue
==========
In this tool, calls to functions (or class methods) can be displayed on the generated diagrams, and a hyperlink allows access to the diagram for the called function from the ‘caller diagram’.

>From the call_node, I can get the function_node (inside the same translation unit) with a simple

call_node.get_definition()

(this is with the python bindings to libclang). The call_node does not have an associated Unified Symbol Resultion USR, while the associated function_node does:

call_node.get_usr() —>  displays ‘’, nothing
call_node.get_definition().get_usr() —>  displays ‘function’s USR’

The problem comes when there is a call from a given source file to a function (method) that is defined in another source file. In that case,

call_node.get_definition() —> None

So, I have no way to establish a (hyper)link between the caller and the callee.


Attempts of resolution
============

I have tried to put several translation units into the libclang index, with:

index = clang.cindex.Index.create()
args=["-c","-x","c++","-Wall","-ansi","-I./include"]
tu_aux1=index.parse("./include/t.h",args, None, 2)
tu_aux2=index.parse("./src/t.cpp",args)
tu = index.parse("./simple_demo_src.cpp",args)
find_functions(tu.cursor)

In this example, the file being explored is ‘simple_demo_src.cpp’. Some of the functions that are called from ‘simple_demo_src.cpp’ are defined in "./include/t.h" and "./src/t.cpp ».
I get the same behavior as explained before:
call_node.get_definition() —> None

Any Ideas?
===========
(not preferred solution might involve combining all source files into a single one. How?)

Your ideas will be greatly appreciated. I’m hoping to contribute this tool to the community.

Regards,

Juan Lopez-Villarejo


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140108/2ca2deb1/attachment.html>


More information about the cfe-dev mailing list