如何做一个干净的前端
How to make a clean clang front-end?
我正在做一个c++源代码分析器项目,看起来clang是一个不错的候选解析工作。问题是clang严重依赖基础设施"llvm"项目,我如何配置它,以获得一个干净的前端没有任何具体的面向机器的后端?就像LCC一样,它们为专注于解析器部分的人提供了一个"空"后端。
我最近在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
并从步骤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
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
}
不幸的是,如果没有特定于机器的细节,就无法获得"纯"前端。C/c++本质上是机器绑定的语言。想想预处理器和内置定义,内置类型的大小,等等。其中一些可以抽象出来,但不能,例如preprocessor.
相关文章:
- 在做一段时间内检查字符的无限循环
- 我必须将 5 个数字放入一个数组中,而不是 cin 计算平均值并输出它,我做错了
- 条件是每x个循环只做一次
- 虽然我只做过一次LoadLibrary,但我必须做两次FreeLibrary.此外,在卸载DLL后,当试图再次加载它时,
- 你如何做一个 for 循环作为一段时间的条件
- 为什么刹车后做一段时间的循环会更好
- LLVM clang编译器优化器以一种非常奇怪的方式重新排列代码,该怎么做
- 如果我只想在模板中专门研究一种方法,我该怎么做
- 离散傅立叶变换C++-下一步要做什么
- 在做一次选择(GL_SELECT模式)后,一半Fps (OpenGL)
- 如何做一个干净的前端
- 是否有一种方法,有一个模板做具体的事情基于如果参数是模板类型和值
- 如何在Windows上每毫秒做一件事或做得更好
- 读取16位wav文件并将其归一化,如Matlab所做的
- 为什么我不能做一根绳子
- 我必须检查数组的所有元素是否都等于 1.并将其用作一段时间的条件..我该怎么做
- 在一次调用中重置使用 iomanip 所做的所有更改的类:setprecision、setfill 和标志
- 一种对不同变量做相同的方法
- 我做了一件阴暗的事情
- 有没有一种标准的方法可以做findfirst,findnext与gcc在linux上使用stl