如何制作一个干净的铿锵前端?

Hai*_* Li 22 c++ frontend backend llvm clang

我正在研究一个C++源代码分析器项目,看起来clang很适合解析工作.问题是clang在很大程度上取决于基础设施"llvm"项目,如何配置它以获得干净的前端而没有任何具体的机器导向后端?就像LCC一样,它们为专注于解析器部分的人提供了"空"后端.任何建议表示赞赏.

arx*_*arx 8

我最近在Windows上做过这个.

这里下载clang和llvm源代码.

安装cmake和Python(与文档相反,你只需要Python来构建clang;至少,如果找不到Python运行时,cmake会放弃).

您还需要VS2008或VS2010.

有一点不完全明显的是所需的目录结构:

projectRoot
    build  <- intermediate build files and DLLs, etc. will go here
    llvm  <- contents of llvm-3.0.src from llvm-3.0.tar go here
        tools
            clang  <- contents of clang-3.0.src from clang-3.0.tar go here
Run Code Online (Sandbox Code Playgroud)

并按照步骤4以后的Windows构建说明进行操作.不要试图使用cmake GUI,这是一个恐怖; 只需使用构建说明中给出的命令.

一旦构建完成(需要一段时间),您将拥有:

projectRoot
    build
        bin
            Release  <- libclang.dll will be here
        lib
            Release  <- libclang.lib will be here
    llvm
        tools
            clang
                include
                    clang-c  <- Index.h is here
Run Code Online (Sandbox Code Playgroud)

Index.h定义了API来访问有关源代码的信息; 它包含了很多关于API的文档.

要开始使用clang,您需要以下内容:

CXIndex index = clang_createIndex(1, 1);

// Support Microsoft extensions
char *args[] = {"-fms-extensions"};

CXTranslationUnit tu = clang_parseTranslationUnit(index, "mySource.c", args, ARRAY_SIZE(args), 0, 0, 0);

if (tu)
{
    CXCursor cursor = clang_getTranslationUnitCursor(tu);

    // Use the cursor functions to navigate through the AST
}
Run Code Online (Sandbox Code Playgroud)