llvm提取c/c++ ast树
一、llvm提取程序ast树
1.1、源代码
//main.c
#include <iostream>
#include "StaticMath.h"
#include "DynamicMath.h"
int main()
{
double a=1.2;
double b=2.4;
std::cout << "static : a+b="<<StaticMath::add(a,b)<<std::endl;
std::cout << "dynamic : a+b =" << DynamicMath::add(a,b)<<std::endl;
return 0;
}
1.2、获取AST中main的代码
Clang -fsyntax-only -Xclang -ast-dump test.cpp
-fsyntax-only意味着只解析语法,不进行编译和链接;-Xclang 是要使用clang特定的Xclang功能-ast-dump是要打印AST
或者
clang-check -p main.c -ast-dump -ast-dump-filter main
1.3、AST树的结果
FunctionDecl 0x55eed4d4d728 </media/data/clang-llvm/TestC/main.c:8:1, line:19:1> line:8:5 main 'int ()'
`-CompoundStmt 0x55eed4d4e158 <col:11, line:19:1>
|-DeclStmt 0x55eed4d4d840 <line:9:5, col:13>
| `-VarDecl 0x55eed4d4d7d8 <col:5, col:12> col:12 used a 'PARAMS':'struct params'
|-DeclStmt 0x55eed4d4d8d0 <line:10:5, col:13>
| `-VarDecl 0x55eed4d4d868 <col:5, col:12> col:12 used b 'PARAMS':'struct params'
|-DeclStmt 0x55eed4d4da90 <line:11:5, col:28>
| `-VarDecl 0x55eed4d4d8f8 <col:5, col:27> col:12 used c 'RETURN':'struct returns' cinit
| `-CallExpr 0x55eed4d4da30 <col:15, col:27> 'RETURN':'struct returns'
| |-ImplicitCastExpr 0x55eed4d4da18 <col:15> 'RETURN (*)(PARAMS, PARAMS)' <FunctionToPointerDecay>
| | `-DeclRefExpr 0x55eed4d4d960 <col:15> 'RETURN (PARAMS, PARAMS)' Function 0x55eed4d4d268 'Function' 'RETURN (PARAMS, PARAMS)'
| |-ImplicitCastExpr 0x55eed4d4da60 <col:24> 'PARAMS':'struct params' <LValueToRValue>
| | `-DeclRefExpr 0x55eed4d4d980 <col:24> 'PARAMS':'struct params' lvalue Var 0x55eed4d4d7d8 'a' 'PARAMS':'struct params'
| `-ImplicitCastExpr 0x55eed4d4da78 <col:26> 'PARAMS':'struct params' <LValueToRValue>
| `-DeclRefExpr 0x55eed4d4d9a0 <col:26> 'PARAMS':'struct params' lvalue Var 0x55eed4d4d868 'b' 'PARAMS':'struct params'
|-DeclStmt 0x55eed4d4dc50 <line:12:5, col:29>
| `-VarDecl 0x55eed4d4dab8 <col:5, col:28> col:11 used d 'EMPTY':'EMPTY' cinit
| `-CallExpr 0x55eed4d4dbf0 <col:15, col:28> 'EMPTY':'EMPTY'
| |-ImplicitCastExpr 0x55eed4d4dbd8 <col:15> 'EMPTY (*)(PARAMS, PARAMS)' <FunctionToPointerDecay>
| | `-DeclRefExpr 0x55eed4d4db20 <col:15> 'EMPTY (PARAMS, PARAMS)' Function 0x55eed4d4d4d8 'Function2' 'EMPTY (PARAMS, PARAMS)'
| |-ImplicitCastExpr 0x55eed4d4dc20 <col:25> 'PARAMS':'struct params' <LValueToRValue>
| | `-DeclRefExpr 0x55eed4d4db40 <col:25> 'PARAMS':'struct params' lvalue Var 0x55eed4d4d7d8 'a' 'PARAMS':'struct params'
| `-ImplicitCastExpr 0x55eed4d4dc38 <col:27> 'PARAMS':'struct params' <LValueToRValue>
| `-DeclRefExpr 0x55eed4d4db60 <col:27> 'PARAMS':'struct params' lvalue Var 0x55eed4d4d868 'b' 'PARAMS':'struct params'
|-BinaryOperator 0x55eed4d4dd30 <line:13:5, col:17> 'int' '='
| |-DeclRefExpr 0x55eed4d4dc68 <col:5> 'int' lvalue Var 0x55eed4d4d698 'e' 'int'
| `-CallExpr 0x55eed4d4dcf0 <col:7, col:17> 'int'
| `-ImplicitCastExpr 0x55eed4d4dcd8 <col:7> 'int (*)()' <FunctionToPointerDecay>
| `-DeclRefExpr 0x55eed4d4dc88 <col:7> 'int ()' Function 0x55eed4d4d5e0 'Function3' 'int ()'
|-CallExpr 0x55eed4d4de70 <line:15:5, col:27> 'int'
| |-ImplicitCastExpr 0x55eed4d4de58 <col:5> 'int (*)(const char *, ...)' <FunctionToPointerDecay>
| | `-DeclRefExpr 0x55eed4d4dd50 <col:5> 'int (const char *, ...)' Function 0x55eed4d26d70 'printf' 'int (const char *, ...)'
| |-ImplicitCastExpr 0x55eed4d4deb8 <col:12> 'const char *' <NoOp>
| | `-ImplicitCastExpr 0x55eed4d4dea0 <col:12> 'char *' <ArrayToPointerDecay>
| | `-StringLiteral 0x55eed4d4dda8 <col:12> 'char [3]' lvalue "%d"
| `-ImplicitCastExpr 0x55eed4d4ded0 <col:17, col:19> 'int' <LValueToRValue>
| `-MemberExpr 0x55eed4d4dde8 <col:17, col:19> 'int' lvalue .returns2 0x55eed4d4cf90
| `-DeclRefExpr 0x55eed4d4ddc8 <col:17> 'RETURN':'struct returns' lvalue Var 0x55eed4d4d8f8 'c' 'RETURN':'struct returns'
|-CallExpr 0x55eed4d4dfa8 <line:16:5, col:25> 'int'
| |-ImplicitCastExpr 0x55eed4d4df90 <col:5> 'int (*)(const char *, ...)' <FunctionToPointerDecay>
| | `-DeclRefExpr 0x55eed4d4dee8 <col:5> 'int (const char *, ...)' Function 0x55eed4d26d70 'printf' 'int (const char *, ...)'
| |-ImplicitCastExpr 0x55eed4d4dff0 <col:12> 'const char *' <NoOp>
| | `-ImplicitCastExpr 0x55eed4d4dfd8 <col:12> 'char *' <ArrayToPointerDecay>
| | `-StringLiteral 0x55eed4d4df08 <col:12> 'char [3]' lvalue "%d"
| `-ImplicitCastExpr 0x55eed4d4e008 <col:17, col:19> 'int' <LValueToRValue>
| `-MemberExpr 0x55eed4d4df48 <col:17, col:19> 'int' lvalue .empty1 0x55eed4d482a8
| `-DeclRefExpr 0x55eed4d4df28 <col:17> 'EMPTY':'EMPTY' lvalue Var 0x55eed4d4dab8 'd' 'EMPTY':'EMPTY'
|-CallExpr 0x55eed4d4e0b0 <line:17:5, col:18> 'int'
| |-ImplicitCastExpr 0x55eed4d4e098 <col:5> 'int (*)(const char *, ...)' <FunctionToPointerDecay>
| | `-DeclRefExpr 0x55eed4d4e020 <col:5> 'int (const char *, ...)' Function 0x55eed4d26d70 'printf' 'int (const char *, ...)'
| |-ImplicitCastExpr 0x55eed4d4e0f8 <col:12> 'const char *' <NoOp>
| | `-ImplicitCastExpr 0x55eed4d4e0e0 <col:12> 'char *' <ArrayToPointerDecay>
| | `-StringLiteral 0x55eed4d4e040 <col:12> 'char [3]' lvalue "%d"
| `-ImplicitCastExpr 0x55eed4d4e110 <col:17> 'int' <LValueToRValue>
| `-DeclRefExpr 0x55eed4d4e060 <col:17> 'int' lvalue Var 0x55eed4d4d698 'e' 'int'
`-ReturnStmt 0x55eed4d4e148 <line:18:5, col:12>
`-IntegerLiteral 0x55eed4d4e128 <col:12> 'int' 0
二、AST树节点介绍
2.1、TranslationUnitDecl
顶层结构是TranslationUnitDecl,对AST树的遍历
2.2、FunctionDecl
函数符号
2.3、ParmVarDecl
参数符号
2.4、VarDecl
变量定义,如果有初始化,可以通过getInit()获取到对应的初始化Expr
2.5、RecordDecl
union和struct的符号的声明,采用的是RecordDecl的形式。
2.6、FieldDecl
struct内部的符号的定义
2.7、CompoundStmt
代表大括号,函数实现、struct、enum、for的body等一般用此包起来。
2.8、DeclStmt
定义语句,里边可能有VarDecl等类型的定义
2.9、BinaryOperator
赋值语句,例如int a = b;
2.10、CallExpr
函数调用Expr,子节点有调用的参数列表
2.11、ReturnStmt
返回语句
2.12、DeclRefExpr
引用的语句,是引用我们之前声明的一些符号的语句