1,clang query 概述
作用:
检查一个程序源码的抽象语法树,测试 AST 匹配器;
帮助检查哪些 AST 节点与指定的 AST 匹配器相匹配;
2,clang-query 安装
准备:
git clone --recursive https://github.com/llvm/llvm-project.git
cd llvm-project/
git checkout llvmorg-3.5.0
cp -r ./clang ./llvm/tools/
cp -r clang-tools-extra ./llvm/tools/clang/tools/extra
mkdir build_d ../local_d
cd build_d #其绝对路径为:/home/hipper/llvm_3_4_0_ex/browse/local_d,如下配置所用
配置:
cmake -G "Unix Makefiles" \
-DCLANG_ENABLE_STATIC_ANALYZER=1 \
-DLLVM_ENABLE_ASSERTIONS=On \
-DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_INSTALL_PREFIX=/home/hipper/llvm_3_4_0_ex/browse/local_d \
-DLLVM_ENABLE_LIBCXX=1 \
-DLLVM_ENABLE_PROJECTS="all" \../llvm
编译安装:
make -j34
make install
cp bin/clang-query /home/hipper/llvm_3_4_0_ex/browse/local_d/bin/
3,测试运行
待测试程序源代码:
hello.c
#if 0
#include <stdio.h>
#endif#ifdef CAPITALIZE
#define WORLD "WORLD"
#else
#define WORLD "world"
#endifextern int write(int, const char*, unsigned long);int main(){write(1, "Hello, ", 7);write(1, WORLD, 5);write(1, "!\n", 2);return 0;}
测试:
$ clang-query hello.c --
效果:
识别出函数调用表达式,并打印出来了