美文网首页LLVM
llvm学习日记五:使用libclang了解clang AST

llvm学习日记五:使用libclang了解clang AST

作者: 鸣人的大哥 | 来源:发表于2019-10-31 18:26 被阅读0次

    参考书:《Getting Started with LLVM Core Libraries》

    抽象语法树的概念就不介绍了,网上介绍太多。

    一、makefile

    路径还是根据自己电脑更改。

    LLVM_CONFIG ?= /Users/qinyao/LLVM/LLVM-9.0.0/build-release/bin/llvm-config
    
    # CXX = /Users/qinyao/LLVM/LLVM-9.0.0/build-release/bin/clang++
    CXXINCLUDE+=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1
    # CXXINCLUDE+=/Users/qinyao/LLVM/LLVM-9.0.0/build-release/include/c++/v1
    ISYSROOT+=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk
    LIB+=/Users/qinyao/LLVM/LLVM-9.0.0/build-release/lib
    
    ifndef VERBOSE
    QUIET:=@
    endif
    
    SRC_DIR?=$(PWD)
    LDFLAGS+=$(shell $(LLVM_CONFIG) --ldflags)
    COMMON_FLAGS=-Wall -Wextra -I$(CXXINCLUDE) -isysroot $(ISYSROOT) 
    CXXFLAGS+=$(COMMON_FLAGS) $(shell $(LLVM_CONFIG) --cxxflags)
    CPPFLAGS+=$(shell $(LLVM_CONFIG) --cppflags) -I$(SRC_DIR) 
    
    CLANGLIBS = \
        -Wl\
        -lclang\
        -lclangFrontend\
        -lclangDriver\
        -lclangSerialization\
        -lclangParse\
        -lclangSema\
        -lclangAnalysis\
        -lclangEdit\
        -lclangAST\
        -lclangLex\
        -lclangBasic
    
    LLVMLIBS=$(shell $(LLVM_CONFIG) --libs)
    PROJECT=uselibclang
    PROJECT_OBJECTS=uselibclang.o
    default: $(PROJECT)
    
    
    %.o : $(SRC_DIR)/%.cpp
        @echo Compiling $*.cpp
        $(QUIET)$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $< -I/Users/qinyao/LLVM/LLVM-9.0.0/llvm-9.0.0.src/tools/clang/include 
        
    $(PROJECT) : $(PROJECT_OBJECTS)
        @echo Linking $@
        $(QUIET)$(CXX) -o $@ $(CXXFLAGS) $(LDFLAGS) $^ $(CLANGLIBS) $(LLVMLIBS) `$(LLVM_CONFIG) --system-libs`
    
    clean::
        $(QUIET)rm -f $(PROJECT) $(PROJECT_OBJECTS)
    

    二、cpp代码

    • clang_visitChildren 最重要的函数,使用回调函数visitNode 遍历所有子节点,打印c函数名
    
    extern "C" {
    #include "clang-c/Index.h"
    }
    #include "llvm/Support/CommandLine.h"
    #include <iostream>
    using namespace llvm;
    static cl::opt<std::string> FileName(cl::Positional, cl::desc("Input file"), cl::Required);
    
    enum CXChildVisitResult visitNode (CXCursor cursor, CXCursor parent, CXClientData client_data) {
        
        if (clang_getCursorKind(cursor) == CXCursor_CXXMethod || clang_getCursorKind(cursor) == CXCursor_FunctionDecl) {
            CXString name = clang_getCursorSpelling(cursor);
            CXSourceLocation loc = clang_getCursorLocation(cursor);
            CXString fName;
            unsigned line = 0, col = 0;
            clang_getPresumedLocation(loc, &fName, &line, &col);
            std::cout << clang_getCString(fName) << ":"
            << line << ":"<< col << " declares "
            << clang_getCString(name) << std::endl;
            return CXChildVisit_Continue;
            }
        return CXChildVisit_Recurse;
    }
            
    int main(int argc, char** argv)
    {
        cl::ParseCommandLineOptions(argc, argv, "AST Traversal Example");
        CXIndex index = clang_createIndex(0, 0);
        const char *args[] = {"-I/usr/include","-I." };
        CXTranslationUnit translationUnit = clang_parseTranslationUnit(index, FileName.c_str(), args, 2, NULL, 0,CXTranslationUnit_None);
        CXCursor cur = clang_getTranslationUnitCursor(translationUnit);
        clang_visitChildren(cur, visitNode, NULL);
        clang_disposeTranslationUnit(translationUnit);
        clang_disposeIndex(index);
        return 0;
        
    }
    
    

    三、编译执行

    • 代码比较简单 hello.c
    #include <stdio.h>
    int main() {
        printf("hello, world!");
    }
    
    image.png

    相关文章

      网友评论

        本文标题:llvm学习日记五:使用libclang了解clang AST

        本文链接:https://www.haomeiwen.com/subject/fjgtbctx.html