I'm working on a C++ source analyzer project and it seems that clang is nice candidate for the parsing work. The problem is that clang heavily depends on the infrastructure "llvm" project, How do I configure it to get a clean front-end without any concrete machine oriented backend? Just like LCC does, they provide a "null" backend for people who focus on parser parts. Any suggestion is appreciated.
我正在研究一个C ++源代码分析器项目,看起来clang很适合解析工作。问题是clang在很大程度上取决于基础设施“llvm”项目,如何配置它以获得干净的前端而没有任何具体的机器导向后端?就像LCC一样,它们为专注于解析器部分的人提供了“空”后端。任何建议表示赞赏。
2 个解决方案
#1
8
I recently did this on Windows.
我最近在Windows上做过这个。
Download the clang and llvm source from here.
从这里下载clang和llvm源代码。
Install cmake and Python (contrary to the docs, you do need Python just to build clang; at least, cmake gives up if it can't find a Python runtime).
安装cmake和Python(与文档相反,你只需要Python来构建clang;至少,如果找不到Python运行时,cmake会放弃)。
You also need VS2008 or VS2010.
您还需要VS2008或VS2010。
One thing that's not entirely obvious is the required directory structure:
有一点不完全明显的是所需的目录结构:
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
And follow the windows build instructions from step 4 onwards. Don't attempt to use the cmake GUI, it's a horror; just use the commands given in the build instructions.
并按照步骤4以后的Windows构建说明进行操作。不要试图使用cmake GUI,这是一个恐怖;只需使用构建说明中给出的命令。
Once the build is complete (which takes a while) you'll have:
一旦构建完成(需要一段时间),您将拥有:
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 defines the API to access information about your source code; it contains quite a bit of documentation about the APIs.
Index.h定义了API以访问有关源代码的信息;它包含了很多关于API的文档。
To get started using clang you need something like:
要开始使用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
}
#2
1
Unfortunately, you cannot get "pure" front-end without machine-specific details. C/C++ are inherently machine-tied languages. Think about preprocessor and built-in defines, the sizes of the builtin types, etc. Some of these can be abstracted out, but not e.g. preprocessor.
不幸的是,如果没有特定于机器的细节,你就无法获得“纯粹”的前端。 C / C ++本质上是与机器相关的语言。考虑预处理器和内置定义,内置类型的大小等。其中一些可以被抽象出来,但不是例如。预处理。
#1
8
I recently did this on Windows.
我最近在Windows上做过这个。
Download the clang and llvm source from here.
从这里下载clang和llvm源代码。
Install cmake and Python (contrary to the docs, you do need Python just to build clang; at least, cmake gives up if it can't find a Python runtime).
安装cmake和Python(与文档相反,你只需要Python来构建clang;至少,如果找不到Python运行时,cmake会放弃)。
You also need VS2008 or VS2010.
您还需要VS2008或VS2010。
One thing that's not entirely obvious is the required directory structure:
有一点不完全明显的是所需的目录结构:
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
And follow the windows build instructions from step 4 onwards. Don't attempt to use the cmake GUI, it's a horror; just use the commands given in the build instructions.
并按照步骤4以后的Windows构建说明进行操作。不要试图使用cmake GUI,这是一个恐怖;只需使用构建说明中给出的命令。
Once the build is complete (which takes a while) you'll have:
一旦构建完成(需要一段时间),您将拥有:
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 defines the API to access information about your source code; it contains quite a bit of documentation about the APIs.
Index.h定义了API以访问有关源代码的信息;它包含了很多关于API的文档。
To get started using clang you need something like:
要开始使用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
}
#2
1
Unfortunately, you cannot get "pure" front-end without machine-specific details. C/C++ are inherently machine-tied languages. Think about preprocessor and built-in defines, the sizes of the builtin types, etc. Some of these can be abstracted out, but not e.g. preprocessor.
不幸的是,如果没有特定于机器的细节,你就无法获得“纯粹”的前端。 C / C ++本质上是与机器相关的语言。考虑预处理器和内置定义,内置类型的大小等。其中一些可以被抽象出来,但不是例如。预处理。