lvxuzhou


私信TA

用户名:lvxuzhou

访问量:96817

签 名:

lvxuzhou

等  级
排  名 48
经  验 11135
参赛次数 0
文章发表 56
年  龄 0
在职情况 学生
学  校 西安
专  业

  自我简介:

环境:win7 64位+llvm11+mingw+python3.7+libclang11

基于libclang写了一个提取函数原型的工具

C语言测试代码:

/*calc1.c*/
//#include<stdio.h>
//#include<stdlib.h>
/*将操作定义为枚举类型*/
typedef enum
{
    OP_ADD = 0,
    OP_SUB,
    OP_MUL,
    OP_DIV,
}OP_TYPE;
/*加减乘除处理函数*/
double ADD(double op1,double op2){
    return op1+op2;
}
double SUB(double op1,double op2)
{
    return op1-op2;
}
double MUL(double op1,double op2)
{
    return op1*op2;
}
double DIV(double op1,double op2)
{
    return op1/op2;
}
double calc(int op,double op1,double op2)
{
    /*使用switch,根据操作类型,选择操作*/
    double result = 0;
    switch(op)
    {
        case OP_ADD:
        {
            result = ADD(op1,op2);
            break;
        }
        case OP_SUB:
        {
            result = SUB(op1,op2);
            break;
        }
        case OP_MUL:
        {
            result = MUL(op1,op2);
            break;
        }
        case OP_DIV:
        {
            result = DIV(op1,op2);
            break;
        }
        default:
        {
            printf("unsupport opration\n");
            break;
        }
    }
    return result;
}

int main(int argc,char *argv[1])
{
    if(4 > argc)
    {
        printf("usage:op num1 num2\n");
        printf("op[0:add,1:sub,2:mul;3:div]\n");
        return 0;
    }
    int op = atoi(argv[1]);
    double op1 = atof(argv[2]);
    double op2 = atof(argv[3]);
    printf("op:%d,op1:%.1f,op2:%.1f\n",op,op1,op2);
    double result = calc(op,op1,op2);
    printf("result is %.1f\n",result);
    return 0;

python代码:

from clang.cindex import Config #配置
from clang.cindex import TypeKind
from clang.cindex import CursorKind
from clang.cindex import Index #主要API
from clang.cindex import Token

def GetTranslationUnit(filename):
    index = Index.create()
    tu = index.parse(filename)
    print("tTranslation Unit=%s"%(tu.spelling))
    return tu
def LoadLibClang(libclangPath):
    if Config.loaded == True:
        pass
    else:
        Config.set_library_file(libclangPath)
def retStr(node):

    argStr = ''
    tokens = node.get_tokens()
    for token in tokens:
            if(token.spelling=='{'):break
            print(token.spelling,end=' ')
        #     if node.spelling == token.spelling: break;
        # argStr = argStr + ' ' + token.spelling
def PrtFunction(node):

    args=node.get_arguments()
    argStr=''
    for arg in args:
        tokens=arg.get_tokens()
        for token in tokens:
            if node.spelling== token.spelling:break;
            argStr=argStr+' '+token.spelling
        argStr=argStr+','
    argStr=argStr.strip(',')
    argStr=argStr.strip(' ')
    funStr=node.spelling+'('+argStr+')'
    print(funStr)
#主函数
if __name__ == '__main__':
    filename='calc.c' # C语言文件
    libclangPath = r'C:\Program Files\LLVM\bin\libclang.dll' #libclang库路径
    LoadLibClang(libclangPath) #加载libclang库
    tu=GetTranslationUnit(filename)#解析main.c
    ast_root_node=tu.cursor #获取cursor根节点

    functionList=[]
    print("Print all nodes in ast tree:")
    for node in ast_root_node.walk_preorder():  # 遍历词法分析结果
        if node.kind==CursorKind.FUNCTION_DECL:
            if node.is_definition()and str(node.location.file)==filename:
                functionList.append(node)
    print('函数原型')
    for node in functionList:
        retStr(node)
        print('')

运行结果:

QQ图片20211005181034.png

 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区