1

When I parse a C++ code file using libclang's Python bindings, I found the cursor of the cross-file reference is missing.

Example C++ code:

#include "controller/LocalController.h"

int addition(int a, int b)
{
    return a + b;
}

auto c = addition(1, 2);

auto d = LocalController::getTextByKey("100000");

Python script:

index = clang.cindex.Index.create()
tu = index.parse("MazeController.cpp", clang_args, None, 1)
for child in tu.cursor.walk_preorder():
    if child.location.file != None:
        if child.location.file.name == tu.cursor.displayname:
            print child.displayname, child.kind, child.location.line, child.location.column

The variable clang_args includes the base path of "controller/LocalController.h". Below is the output:

controller/LocalController.h CursorKind.INCLUSION_DIRECTIVE 1 4
addition(int, int) CursorKind.FUNCTION_DECL 3 5
a CursorKind.PARM_DECL 3 18
b CursorKind.PARM_DECL 3 25
 CursorKind.COMPOUND_STMT 4 1
 CursorKind.RETURN_STMT 5 5
 CursorKind.BINARY_OPERATOR 5 12
a CursorKind.UNEXPOSED_EXPR 5 12
a CursorKind.DECL_REF_EXPR 5 12
b CursorKind.UNEXPOSED_EXPR 5 16
b CursorKind.DECL_REF_EXPR 5 16
c CursorKind.VAR_DECL 8 6
addition CursorKind.CALL_EXPR 8 10
addition CursorKind.UNEXPOSED_EXPR 8 10
addition CursorKind.DECL_REF_EXPR 8 10
 CursorKind.INTEGER_LITERAL 8 19
 CursorKind.INTEGER_LITERAL 8 22
d CursorKind.VAR_DECL 10 6

The local function call of addition is perfectly parsed by libclang, but the parsing of cross-file function call of LocalController::getTextByKey is missing, as the last line of output is the cursor of d.

Could anybody help me? Thanks so much!

0 Answers0