我正在做一个C++源代码分析器项目,看起来clang是一个很好的解析工具。问题是clang严重依赖于基础设施"llvm“项目,我如何配置它才能在没有任何面向具体机器的后端的情况下获得一个干净的前端?就像LCC一样,它们为专注于解析器部分的人提供了一个"null“后端。任何建议都是值得感谢的。
发布于 2012-02-15 01:19:53
我最近在Windows上做到了这一点。
从here下载clang和llvm源代码。
安装cmake和Python (与文档相反,您确实需要Python来构建clang;至少,如果cmake找不到Python运行时,它会放弃)。
您还需要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并从步骤4开始遵循windows build instructions。不要试图使用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 hereIndex.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
}发布于 2012-02-16 19:03:07
不幸的是,如果没有特定于机器的细节,您将无法获得“纯”前端。C/C++本质上是与机器相关的语言。考虑预处理器和内置定义,内置类型的大小等。其中一些可以抽象出来,但不是预处理器。
https://stackoverflow.com/questions/8062548
复制相似问题