【Clang】 使用LibClang打印Cursor和CursorKind及其位置的简例

流程就是先提取出TU,get其中的cursor,将之打印出来:

CXIndex Index = clang_createIndex(0,0);
    CXTranslationUnit TU = clang_parseTranslationUnit(Index, 0, argv, argc,
                                                      0, 0,
                                                      CXTranslationUnit_None);
    CXCursor C = clang_getTranslationUnitCursor(TU);

打印出cursor和cursorKind的方法实现很简单,不多赘述

主要是CXChildVisitResult要注意,其返回值虽然可以有不同,但是我们一般都是返回CXChildVisit_Recurse

clang_visitChildren(cursor,
                        [](CXCursor c,CXCursor parent,CXClientData data){
                            printSpell(c);
                            printLocation(c);
                            return CXChildVisit_Recurse;},
                        NULL);

这里我套进去了,上面的代码拆开来看应该是这样的:

enum CXChildVisitResult printVisitor(CXCursor cursor, CXCursor parent,
                                     CXClientData client_data) {
    
    printSpelling(cursor);
    printKindSpelling(cursor);
    printLocation(cursor);

    return CXChildVisit_Recurse;
}

 clang_visitChildren(cursor, printVisitor, NULL);

完整代码如下:

#include <stdio.h>
#include <stdbool.h>
#include <iostream>
#include <string.h>
#include <fcntl.h>
#include "clang-c/Index.h"

using namespace std;

bool printSpell(CXCursor cursor){
    CXCursorKind kind=clang_getCursorKind(cursor);
    printf("the cursorSpelling is:%s\n",clang_getCString(clang_getCursorSpelling(cursor)));
    printf("the cursorKindSpelling is:%s\n",clang_getCString(clang_getCursorKindSpelling(kind)));
    return true;
}

bool printLocation(CXCursor cursor){
    CXSourceRange range=clang_getCursorExtent(cursor);
    CXSourceLocation startLocation=clang_getRangeStart(range);
    CXSourceLocation endLocation=clang_getRangeEnd(range);
    CXFile file;
    unsigned  int line,col,offset;
    clang_getInstantiationLocation(startLocation,&file,&line,&col,&offset);
    printf("start_Location is:l:%u  c:%u  offset:%u\n",line,col,offset);
    clang_getInstantiationLocation(endLocation,&file,&line,&col,&offset);
    printf("end_Location is:l:%u  c:%u  offset:%u\n",line,col,offset);
}



int main(int argc , char *argv[]){
    CXIndex index = clang_createIndex(0,0);
    CXTranslationUnit TU=clang_parseTranslationUnit(index,0,argv,
                                                    argc,0,
                                                    0,CXTranslationUnit_None);
    CXCursor cursor=clang_getTranslationUnitCursor(TU);
    printSpell(cursor);
    clang_visitChildren(cursor,
                        [](CXCursor c,CXCursor parent,CXClientData data){
                            printSpell(c);
                            printLocation(c);
                            return CXChildVisit_Recurse;},
                        NULL);
    clang_disposeTranslationUnit(TU);
    clang_disposeIndex(index);
    return 0;
}

CMakeLists.txt我也贴出来吧,没怎么好好写,有的没的都加上去了,路径有不一样改一下就好了。

CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT(example)

ADD_EXECUTABLE(example example.cpp)
INCLUDE_DIRECTORIES(./include/)

#Need to do more about find library#
FIND_LIBRARY(LIBCLANG_PATH clang HINTS /home/xioazhiming/clang+llvm-10.0.0/lib)

TARGET_LINK_LIBRARIES(example ${LIBCLANG_PATH})

add_compile_options(-fno-rtti)

include_directories("/home/xioazhiming/clang+llvm-10.0.0/include")
include_directories("/home/xioazhiming/clang+llvm-10.0.0/include/llvm")
include_directories("/home/xioazhiming/clang+llvm-10.0.0/include/clang")
include_directories("/usr/include")
include_directories("/usr/local/include")
include_directories("/usr/lib/gcc/x86_64-linux-gnu/7/include")
include_directories("/usr/lib/gcc/x86_64-linux-gnu")
include_directories("/usr/lib/gcc/x86_64-linux-gnu/7/include-fixed")
include_directories("/home/xioazhiming/clang+llvm-10.0.0/include/llvm/ADT")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值