基于Ubuntu20.4进行的构建基于 Clang 的LibTooling 工具
参考链接:https://clang.llvm.org/docs/LibASTMatchersTutorial.html
参考了clang中的构建LibTooling 工具内容,但有些部分因为会报错就使用了自己的方法,可能文章会有疏漏的地方,希望指出,会改正的~
第0步:获取Clang
由于Clang是LLVM项目的一部分,因此您首先需要下载LLVM的源代码。Clang和LLVM都在同一个git存储库中,但是在不同的目录下。
mkdir clang-llvm
cd clang-llvm
git clone https://github.com/llvm/llvm-project.git (这个过程可能比较久)
接下来,您需要获取 CMake 构建系统和 Ninja 构建工具。
在官方文档中如下:(但在我运行的时候会有问题,故没有使用该方法)
cd ~/clang-llvm
git clone https://github.com/martine/ninja.git (此下载网址已换为:https://github.com/ninja-build/ninja)
cd ninja
git checkout release (在我运行的时候,找不到该版本,所以之后我就没有用这个方法了)./bootstrap.py
sudo cp ninja /usr/bin/
cd ~/clang-llvm
git clone git://cmake.org/stage/cmake.git(此下载网址已换为https://github.com/Kitware/CMake.git)
cd cmake
git checkout next(也是在我运行的时候,找不到该版本,所以之后我就没有用这个方法了)
./bootstrap
make
sudo make install
我用的方法,是直接使用了
sudo apt install ninja-build #这样会简单好多,而且直接进行了环境变量的配置。
sudo snap install cmake
现在我们将构建 Clang:
cd ~/clang-llvm
mkdir build && cd build
cmake -G Ninja ../llvm -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra" -DLLVM_BUILD_TESTS=ON # Enable tests; default is off.
路径 …/lvm 我猜应该是官方文档写错了,或者路径改变了,我执行时的路径/home/zyc/clang-llvm/llvm-project/llvm 是可以运行的,否则会报错 然后运行时可能会提示你要选择
是否要调试即:-DCMAKE_BUILD_TYPE=“Release” 或者 -DCMAKE_BUILD_TYPE=“Debug” 但后者需要的内存超级大,所以不推荐
这一步完整的是:
cmake -G Ninja /home/zyc/clang-llvm/llvm-project/llvm -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra" -DLLVM_BUILD_TESTS=ON -DCMAKE_BUILD_TYPE="Release"
ninja
ninja check # Test LLVM only.
ninja clang-test # Test Clang only. 这两步是进行测试,可能会报错,但影响不大
ninja install
最后,我们希望将 Clang 设置为自己的编译器。 (当然,这一步也可以省略)
cd ~/clang-llvm/build
cmake /home/zyc/clang-llvm/llvm-project/llvm
第 1 步:创建 ClangTool 例子 :语法检查器
我们可以直接在下载的llvm源码目录llvm-project下的clang-tools-extra目录里来添加我们要定义的工具:
在clang-tools-extra目录下创建文件夹loop-convert:
cd /home/zyc/clang-llvm/llvm-project-main/clang-tools-extra
mkdir loop-convert
在clang-tools-extra目录下的CMakeLists.txt中添加:add_subdirectory(loop-convert)
然后转移到loop-convert文件夹内:
创建loop-convert的CMakeLists.txt
内容如下:
set(LLVM_LINK_COMPONENTS
support
FrontendOpenMP
)
add_clang_tool(loop-convert
LoopConvert.cpp
)
target_link_libraries(loop-convert
PRIVATE
clangAST
clangASTMatchers
clangBasic
clangFrontend
clangSerialization
clangTooling
)
创建loop-convert的LoopConvert.cpp,也就是我们想要的工具,内容如下:
```cpp
// Declares clang::SyntaxOnlyAction.
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
// Declares llvm::cl::extrahelp.
#include "llvm/Support/CommandLine.h"
using namespace clang::tooling;
using namespace llvm;
// Apply a custom category to all command-line options so that they are the
// only ones displayed.
static llvm::cl::OptionCategory MyToolCategory("my-tool options");
// CommonOptionsParser declares HelpMessage with a description of the common
// command-line options related to the compilation database and input files.
// It's nice to have this help message in all tools.
static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
// A help message for this specific tool can be added afterwards.
static cl::extrahelp MoreHelp("\nMore help text...\n");
int main(int argc, const char **argv) {
auto ExpectedParser = CommonOptionsParser::create(argc, argv, MyToolCategory);
if (!ExpectedParser) {
// Fail gracefully for unsupported options.
llvm::errs() << ExpectedParser.takeError();
return 1;
}
CommonOptionsParser& OptionsParser = ExpectedParser.get();
ClangTool Tool(OptionsParser.getCompilations(),
OptionsParser.getSourcePathList());
return Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>().get());
}
然后,我们通过运行 ninja 来编译我们的新工具:
cd /home/zyc/clang-llvm/build
ninja
可以使用test.cpp源文件进行运行刚才构造的工具,即语法检查器
Test.cpp内容如下: 放在了build下
int main() { return 0; }
只用了一句话
然后使用:
bin/loop-convert test.cpp – #是在build中的bin
进行检查,请注意指定源文件后的两个破折号。附加的编译器的选项在短划线之后传递