获取所有被调用的函数(即使被另一个函数调用)libclang python

时间:2018-11-26 01:18:29

标签: python c++ libclang

我正在尝试使用libclang / python 3.7通过将其解析为翻译单元来收集程序中调用的每个函数。我解析main.cpp文件以收集数据。

Testhead.cpp

#include <testhead.h>
void a()
{
   std::cout<<5;
   b();
}
void b()
{
    std::cout<<6;
} 

Main.cpp

#include "testhead.h"

//classtest ct;

int main()
{
  a();
  return 0;
}

输出I'm trying to get all function calls, noted by CALL_EXPR which is only given for function a() when function a() calls function b()

用于获取输出的示例python / libclang

for i in node.get_children():
        if i.kind == clang.cindex.CursorKind.CALL_EXPR:
            print(str(i.kind)+","+i.spelling)
            #print(str(i.extent.start))
            tempSet.add(i.spelling)
        elif i.kind == clang.cindex.CursorKind.FUNCTION_DECL:
            print(str(i.kind)+","+i.spelling)
            #print(str(i.extent.start.file))
            for item in i.get_children():
                if item.kind == clang.cindex.CursorKind.CALL_EXPR:
                    tempSet.add(item.kind)
                    for n in item.get_children():
                        print(str(n.kind) +","+ n.spelling)
                        for z in n.get_children():
                            print(str(z.kind) +","+ z.spelling)
                elif item.kind == clang.cindex.CursorKind.COMPOUND_STMT:
                    print(' ' + str(item.kind) + ', ' + item.spelling)
                    #print(' ' + str(item.extent.start.file))
                    for c in item.get_children():
                        if c.kind == clang.cindex.CursorKind.CALL_EXPR:
                            print(' '*2 + str(c.kind) + ', ' + c.spelling+" , " + c.displayname)
                            #print(' '*2 + str(c.extent.start))
                            a = c.get_definition()
                            print(' '*10 + str(a))
                            tempSet.add(c.spelling)
                            for z in c.get_children():
                                print(' '*3 + str(z.kind) + ", "+ z.spelling)
                                a = z.get_definition()
                                print(' '*10 + str(a))
                                for x in z.get_children():
                                    print(' '*4 + str(x.kind) +","+ x.spelling)
                                    a = x.get_definition()
                                    print(' '*10 + str(a))
                                    for b in x.get_children():
                                        a = b.get_definition()
                                        print(' '*10 + str(a))
                                        print(' '*5 + str(b.kind) +","+ b.spelling)

通过反复试验,我发现只能在main.cpp中定义的函数定义内找到嵌套函数调用。

到目前为止,我发现的唯一解决方案是我需要为.h和.cpp包含一个包含语句 例如:

#include <test.h>
#include <test.cpp>
main.cpp
..............

但是,我无法访问.cpp的文件,我将使用dll的

0 个答案:

没有答案