LLVM 的编译框架基本流程探索(Clang AST语法树)
作者:互联网
上一篇文章末尾提到 AST语法树。语句如下
clang -fmodules -fsyntax-only -Xclang -ast-dump src.pp
CompStream.cpp
#include <iostream>
int main()
{
int a = 10;
int b = 20;
int c = a + b;
std::cout<< c <<std::endl;
return 0;
}
语法树生成
这里衔接上一篇文章:
clang -fmodules -fsyntax-only -Xclang -ast-dump CompStream.cpp>>ast.txt
然后当前文件夹下回出现 ast.txt 里面为执行语句的所有printf。
直接定位ast.txt 中的main函数
AST语法树解析
FunctionDecl 函数定义
0x14********* 变量地址(函数也是变量)
<CompStream.cpp:3:1, line:12:1> 定位文件起始位置 第三行第一个字符,总12行 从第1行开始
line:3:5 main 'int ()' 行3第5个字符 main return int
CompoundStmt 0x14f34c80e70 <line:4:1, line:12:1> 复合多组语句 也就是 {}符号
VarDecl 0x14f34c6dc38 <col:5, col:13> col:9 used a 'int' cinit 初始化变量int a
IntegerLiteral 0x14f34c6dca0 <col:13> 'int' 10 变量赋值
`-BinaryOperator 0x14f34c6dec0 <col:13, col:17> 'int' '+'
| |-ImplicitCastExpr 0x14f34c6de90 <col:13> 'int' <LValueToRValue>
| | `-DeclRefExpr 0x14f34c6de20 <col:13> 'int' lvalue Var 0x14f34c6dc38 'a' 'int'
| `-ImplicitCastExpr 0x14f34c6dea8 <col:17> 'int' <LValueToRValue>
| `-DeclRefExpr 0x14f34c6de58 <col:17> 'int' lvalue Var 0x14f34c6dcf8 'b' 'int'
二进制重载符 +
左值到右值转换。a,b
CXXOperatorCallExpr C++重载调用导出的变量 这里 为std::iostream(std::cout)
ReturnStmt 退出符合 {} 也就是代码块
`-FunctionDecl 0x14f34c6db68 <CompStream.cpp:3:1, line:12:1> line:3:5 main 'int ()'
`-CompoundStmt 0x14f34c80e70 <line:4:1, line:12:1>
|-DeclStmt 0x14f34c6dcc8 <line:5:5, col:15>
| `-VarDecl 0x14f34c6dc38 <col:5, col:13> col:9 used a 'int' cinit
| `-IntegerLiteral 0x14f34c6dca0 <col:13> 'int' 10
|-DeclStmt 0x14f34c6dd88 <line:6:5, col:15>
| `-VarDecl 0x14f34c6dcf8 <col:5, col:13> col:9 used b 'int' cinit
| `-IntegerLiteral 0x14f34c6dd60 <col:13> 'int' 20
|-DeclStmt 0x14f34c6dee0 <line:7:5, col:18>
| `-VarDecl 0x14f34c6ddb8 <col:5, col:17> col:9 used c 'int' cinit
| `-BinaryOperator 0x14f34c6dec0 <col:13, col:17> 'int' '+'
| |-ImplicitCastExpr 0x14f34c6de90 <col:13> 'int' <LValueToRValue>
| | `-DeclRefExpr 0x14f34c6de20 <col:13> 'int' lvalue Var 0x14f34c6dc38 'a' 'int'
| `-ImplicitCastExpr 0x14f34c6dea8 <col:17> 'int' <LValueToRValue>
| `-DeclRefExpr 0x14f34c6de58 <col:17> 'int' lvalue Var 0x14f34c6dcf8 'b' 'int'
|-CXXOperatorCallExpr 0x14f34c80e00 <line:9:5, col:26> 'std::basic_ostream<char, std::char_traits<char> >' lvalue
| |-ImplicitCastExpr 0x14f34c80de8 <col:19> 'std::basic_ostream<char, std::char_traits<char> > &(*)(std::basic_ostream<char, std::char_traits<char> > &(*)(std::basic_ostream<char, std::char_traits<char> > &) __attribute__((cdecl)))' <FunctionToPointerDecay>
| | `-DeclRefExpr 0x14f34c80d58 <col:19> 'std::basic_ostream<char, std::char_traits<char> > &(std::basic_ostream<char, std::char_traits<char> > &(*)(std::basic_ostream<char, std::char_traits<char> > &) __attribute__((cdecl)))' lvalue CXXMethod 0x14f34c782e8 'operator<<' 'std::basic_ostream<char, std::char_traits<char> > &(std::basic_ostream<char, std::char_traits<char> > &(*)(std::basic_ostream<char, std::char_traits<char> > &) __attribute__((cdecl)))'
| |-CXXOperatorCallExpr 0x14f34c7ee00 <col:5, col:17> 'std::basic_ostream<char, std::char_traits<char> >' lvalue
| | |-ImplicitCastExpr 0x14f34c7ede8 <col:14> 'std::basic_ostream<char, std::char_traits<char> > &(*)(int)' <FunctionToPointerDecay>
| | | `-DeclRefExpr 0x14f34c7ed80 <col:14> 'std::basic_ostream<char, std::char_traits<char> > &(int)' lvalue CXXMethod 0x14f34c78ed8 'operator<<' 'std::basic_ostream<char, std::char_traits<char> > &(int)'
| | |-DeclRefExpr 0x14f34c6df30 <col:5, col:10> 'std::ostream':'std::basic_ostream<char, std::char_traits<char> >' lvalue Var 0x14f34c6cb50 'cout' 'std::ostream':'std::basic_ostream<char, std::char_traits<char> >'
| | `-ImplicitCastExpr 0x14f34c7ed68 <col:17> 'int' <LValueToRValue>
| | `-DeclRefExpr 0x14f34c6df60 <col:17> 'int' lvalue Var 0x14f34c6ddb8 'c' 'int'
| `-ImplicitCastExpr 0x14f34c80d40 <col:21, col:26> 'basic_ostream<char, std::char_traits<char> > &(*)(basic_ostream<char, std::char_traits<char> > &) __attribute__((cdecl))' <FunctionToPointerDecay>
| `-DeclRefExpr 0x14f34c80d08 <col:21, col:26> 'basic_ostream<char, std::char_traits<char> > &(basic_ostream<char, std::char_traits<char> > &) __attribute__((cdecl))':'basic_ostream<char, std::char_traits<char> > &(basic_ostream<char, std::char_traits<char> > &)' lvalue Function 0x14f34c7f7b0 'endl' 'basic_ostream<char, std::char_traits<char> > &(basic_ostream<char, std::char_traits<char> > &) __attribute__((cdecl))':'basic_ostream<char, std::char_traits<char> > &(basic_ostream<char, std::char_traits<char> > &)' (FunctionTemplate 0x14f34c48b50 'endl')
`-ReturnStmt 0x14f34c80e60 <line:11:5, col:12>
`-IntegerLiteral 0x14f34c80e38 <col:12> 'int' 0
标签:std,__,LLVM,AST,int,lvalue,Clang,basic,ostream 来源: https://blog.csdn.net/qq_24423085/article/details/100692730