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.
Clang is a compiler front end for the C, C++, Objective-C, and Objective-C++ programming languages, as well as the OpenMP, OpenCL, RenderScript, CUDA, and HIP frameworks. It acts as a drop-in replacement for the GNU Compiler Collection (GCC), supporting most of its compilation flags and unofficial language extensions.
C++20 implementation statusClang has support for some of the features of the ISO C++ 2020 standard. You can use Clang in C++20 mode with the -std=c++20 option (use -std=c++2a in Clang 9 and earlier).
The Clang project provides a language front-end and tooling infrastructure for languages in the C language family (C, C++, Objective C/C++, OpenCL, CUDA, and RenderScript) for the LLVM project.
While LLVM and GCC both support a wide variety languages and libraries, they are licensed and developed differently. LLVM libraries are licensed more liberally and GCC has more restrictions for its reuse. When it comes to performance differences, GCC has been considered superior in the past.
I recently did this on Windows.
Download the clang and llvm source from here.
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).
You also need VS2008 or 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.
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.
To get started using clang you need something like:
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 }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With