4

我刚刚开始通过 python 绑定使用libclang 。我知道我可以使用 遍历整个语法树 (AST) get_children,但我无法找到一个get_next_sibling()(或任何可能被调用的)函数,以便我可以跳过不感兴趣的子树。有这样的功能吗?

4

3 回答 3

3

正如francesco 指出的那样,可以跳过元素。由于最新 cindex.py 修订版的更改,mentoined 代码示例不再起作用。

下面是从 AST 获取特定节点的最小示例。

示例.cpp 文件:

int i; 
char var[10]; 
double tmp;

int add (int a, int b)
{
  int r;
  r=a+b;
  return (r);
}

示例python代码:

import sys
from clang.cindex import *

index = Index.create()
tu = index.parse('example.cpp')

root_node = tu.cursor

#for further working with children nodes i tend to save them in a seperate list
#wanted nodes in extra list "result"
wanted_nodes = ['var', 'tmp']
result = []
node_list= []

for i in node.get_children():
    node_list.append(i)

for i in node_list:
    if i.spelling in wanted_nodes:
        result.append(i)

#now result contains the two nodes "var" and "add"

#print the name
for i in result:
    print i.spelling

#print the type
for i in result:
    print i.type.kind

######OUTPUT#######
>>> var
>>> add
>>> TypeKind.CONSTANTARRAY
>>> TypeKind.DOUBLE

如果您还想要数组中每个元素的类型,您可以通过:

result[1].type.element_type.kind

#######OUTPUT######
>>> TypeKind.CHAR_S

由于 modul cindex.py有据可查,因此应该不难找到如何获取所需信息。

于 2013-06-19T13:47:44.067 回答
3

我认为get_next_siblingPython API 中不存在函数,但我也不明白为什么需要它。

在 python API 中,AST 中的每个节点都知道它的所有子节点,因此只需在循环中跳过父节点的子节点即可轻松跳过不感兴趣的子树。重用 Eli Bendersky 关于 libclang Python API 的优秀博客文章中的一个示例:

def find_typerefs(node, typename):
    """ Find all references to the type named 'typename'
    """
    if node.kind.is_reference():
        ref_node = clang.cindex.Cursor_ref(node)
        if ref_node.spelling == typename:
            print 'Found %s [line=%s, col=%s]' % (
                typename, node.location.line, node.location.column)

    # Recurse for children of this node,
    # skipping all nodes not beginning with "a"
    for c in node.get_children():
        if c.spelling.startswith ("a"):
            find_typerefs(c, typename)
于 2013-05-27T09:38:53.887 回答
-1

在 clang-c 中,枚举 CXChildVisitResult 有 3 个值,CXChildVisit_Continue 跳过访问孩子,因此访问者来到下一个兄弟。类似的东西也应该在python中。

于 2013-05-27T05:22:55.000 回答