我正在编写一个文档生成器,正确地获取include路径是非常糟糕的,因此在解析文件时,我会完全跳过所有include。我还手动调优所有有问题的defines或#ifdef块,它们会因为缺少include而被跳过(以及与生产版本不同的命令行)。在
我注意到的问题是:
struct ComplexBuffer : IAnimatable
{
};
WithIAnimatable
未声明(或被转发声明)。
我使用的python绑定叮当·辛迪克斯所以我使用get_children进行迭代:
结果如下:
如果我完成基本类型:
class IAnimatable {};
struct ComplexBuffer : IAnimatable
我得到正确的输出:
Found grammar element "IAnimatable" {CursorKind.CLASS_DECL} [line=37, col=8]
Found grammar element "ComplexBuffer" {CursorKind.STRUCT_DECL} [line=39, col=9]
Found grammar element "class IAnimatable" {CursorKind.CXX_BASE_SPECIFIER} [line=39, col=25]
Found grammar element "class IAnimatable" {CursorKind.TYPE_REF} [line=39, col=25]
正是我想要的,因为我可以检测到要放入文档中的继承列表。在
这个问题只是因为我跳过了所有的包含。在
也许我可以通过手动重新分析声明行来解决这个问题?在
编辑PS:my parsing python脚本以完成:
import clang.cindex
index = clang.cindex.Index.create()
tu = index.parse(sys.argv[1], args=["-std=c++98"], options=clang.cindex.TranslationUnit.PARSE_SKIP_FUNCTION_BODIES)
def printall_visitor(node):
print 'Found grammar element "%s" {%s} [line=%s, col=%s]' % (node.displayname, node.kind, node.location.line, node.location.column)
def visit(node, func):
func(node)
for c in node.get_children():
visit(c, func)
visit(tu.cursor, printall_visitor)
我将亲自回答这个问题,因为我提出的代码对未来的谷歌用户可能有用。在
最后,我对这两个方法进行了编码,这两个方法都是用来在类声明行上检索继承列表中的基类列表的。在
使用AST光标和一个完全手动,尽可能地应对C++复杂度。在
整个结果如下:
此代码允许我接受不完整的C++翻译单元,正确地解析声明,例如:
^{pr2}$同时应对这些问题:
给我这个输出:
很好,
clang
函数版本在基列表中没有返回一个类。 现在可以使用set
合并这两个函数的结果,以防手动解析器遗漏某些内容。但是我认为这可能会导致细微的重复,因为displayname
和我自己的解析器之间的差异。在但是,googlers,这是一个不错的clang python文档生成器模板,它不需要完全正确的构建选项,而且非常快,因为它完全忽略了
include
的语句。在祝大家今天愉快。在
相关问题 更多 >
编程相关推荐