Function boundary identification using libclang

有些话、适合烂在心里 提交于 2019-12-14 03:52:48

问题


I am learning to parse C++ files using Python + libclang with the help of this very informative (but slightly outdated) tutorial by Eli Bendersky.

My objective is to parse C++ files and identify the function boundaries for functions present in those file. I am expecting to build a python dictionary of this form:

{<func_name>:(<func_start_loc>, <func_end_loc>), ...}

To this end, I am able to get the function name (using cursor.spelling for AST nodes that are of CursorKind.FUNCTION_DECL or CursorKind.CXX_METHOD kind) and the start location (using cursor.location)

My question is, how do I get the end of function location


回答1:


You're looking for the extent property on the Cursor class. For example:

s = '''
void f();
void g() 
{}
void f() 
{}
'''

idx = clang.cindex.Index.create()
tu = idx.parse('tmp.cpp', unsaved_files=[('tmp.cpp', s)])
for f in tu.cursor.walk_preorder():
    if f.kind == CursorKind.FUNCTION_DECL:
        print f.extent

Will return a Python equivalent of a source range:

<SourceRange start <SourceLocation file 'tmp.cpp', line 2, column 1>, end <SourceLocation file 'tmp.cpp', line 2, column 9>>
<SourceRange start <SourceLocation file 'tmp.cpp', line 3, column 1>, end <SourceLocation file 'tmp.cpp', line 4, column 3>>
<SourceRange start <SourceLocation file 'tmp.cpp', line 5, column 1>, end <SourceLocation file 'tmp.cpp', line 6, column 3>>

You may want to consider restricting attention to definitions using Cursor.is_definition if you want function bodies rather than just their declarations.




回答2:


#include "clang/Basic/SourceManager.h"

FunctionDecl *f;

SourceLocation ST = f->getSourceRange().getBegin();
SourceLocation ED = f->getSourceRange().getEnd();

https://github.com/eliben/llvm-clang-samples/blob/master/src_clang/rewritersample.cpp

https://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html https://clang.llvm.org/doxygen/classclang_1_1SourceRange.html



来源:https://stackoverflow.com/questions/43460605/function-boundary-identification-using-libclang

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!