[英]How to get the entire expression on the right hand side of clang abstract syntax tree?
讓我們以一個玩具示例為例,說我在test.cpp文件中有以下代碼:
int main()
{
int gt = 3;
int g = 10 / gt;
}
我想在除法運算中找到分母的變量名,然后使用clang命令clang -Xclang -ast-dump -fsyntax-only test.cpp
獲得上述代碼的抽象語法樹(AST)。 我得到以下輸出
TranslationUnitDecl 0x34f8100 <<invalid sloc>> <invalid sloc>
|-TypedefDecl 0x34f8638 <<invalid sloc>> <invalid sloc> implicit __int128_t '__int128'
| `-BuiltinType 0x34f8350 '__int128'
|-TypedefDecl 0x34f8698 <<invalid sloc>> <invalid sloc> implicit __uint128_t 'unsigned __int128'
| `-BuiltinType 0x34f8370 'unsigned __int128'
|-TypedefDecl 0x34f8728 <<invalid sloc>> <invalid sloc> implicit __builtin_ms_va_list 'char *'
| `-PointerType 0x34f86f0 'char *'
| `-BuiltinType 0x34f8190 'char'
|-TypedefDecl 0x34f8a48 <<invalid sloc>> <invalid sloc> implicit __builtin_va_list 'struct __va_list_tag [1]'
| `-ConstantArrayType 0x34f89f0 'struct __va_list_tag [1]' 1
| `-RecordType 0x34f8810 'struct __va_list_tag'
| `-CXXRecord 0x34f8778 '__va_list_tag'
`-FunctionDecl 0x34f8af0 <test.cpp:1:1, line:5:1> line:1:5 main 'int (void)'
`-CompoundStmt 0x34f8dc0 <line:2:1, line:5:1>
|-DeclStmt 0x34f8c98 <line:3:2, col:12>
| `-VarDecl 0x34f8c18 <col:2, col:11> col:6 used gt 'int' cinit
| `-IntegerLiteral 0x34f8c78 <col:11> 'int' 3
`-DeclStmt 0x34f8da8 <line:4:2, col:17>
`-VarDecl 0x34f8cc0 <col:2, col:15> col:6 g 'int' cinit
`-BinaryOperator 0x34f8d80 <col:10, col:15> 'int' '/'
|-IntegerLiteral 0x34f8d20 <col:10> 'int' 10
`-ImplicitCastExpr 0x34f8d68 <col:15> 'int' <LValueToRValue>
`-DeclRefExpr 0x34f8d40 <col:15> 'int' lvalue Var 0x34f8c18 'gt' 'int'
根據以上AST的知識並使用clang-query,我可以使用以下命令獲取分母的變量名稱
clang-query> match declRefExpr(isExpansionInMainFile(), allOf(hasAncestor(binaryOperator(hasOperatorName("/"))), hasAncestor(declStmt()) ))
我得到的輸出為
Match #1:
/home/clang-llvm/cpp/code/test.cpp:4:15: note: "root" binds here
int g = 10 / gt;
^~
1 match.
現在我們在同一頁上,我有兩個問題。
在上面的玩具示例中,如果我有另一個變量,那么我的查詢將匹配兩個變量(分子和分母),而不是10。 如何限制我的clang查詢僅匹配作為除法運算分母的變量? 換句話說,如何找到存在於二進制運算符“ /”右側的變量? 一個例子是int g = gw / gt;
如果我在分母中有一個表達式,而不是變量gt,那么如何使用clang獲得整個表達式? 換句話說,如何獲取抽象語法樹中二進制運算符“ /”右側的表達式? 一個簡單的例子可以是int g = gw / (gt - gw);
一個復雜的例子可能是int g = gw / gt - gw / gr * gg / sqrt( gt - gw ^ 2) + gq;
我感謝在這方面的任何幫助。
Clang有一個遍歷匹配器“ hasRHS()”,它可以完全滿足您的要求。
int main()
{
int gt = 3;
int g = 10 / gt;
int gw, gg, gr, gq;
int g1 = gw / gt;
int g2 = gw / (gt-gw);
int g3 = gw / gt - gw / gr * gg / ( gt - gw ^ 2) + gq;
return 0;
}
輸出:
clang-query> match varDecl(hasDescendant(binaryOperator(hasOperatorName("/"), hasRHS(expr().bind("myExpr")))))
Match #1:
/home/test.cpp:4:18: note: "myExpr" binds here
int g = 10 / gt;
^~
/home/test.cpp:4:5: note: "root" binds here
int g = 10 / gt;
^~~~~~~~~~~~~~~
Match #2:
/home/test.cpp:7:19: note: "myExpr" binds here
int g1 = gw / gt;
^~
/home/test.cpp:7:5: note: "root" binds here
int g1 = gw / gt;
^~~~~~~~~~~~~~~~
Match #3:
/home/test.cpp:8:19: note: "myExpr" binds here
int g2 = gw / (gt-gw);
^~~~~~~
/home/test.cpp:8:5: note: "root" binds here
int g2 = gw / (gt-gw);
^~~~~~~~~~~~~~~~~~~~~
Match #4:
/home/test.cpp:9:19: note: "myExpr" binds here
int g3 = gw / gt - gw / gr * gg / ( gt - gw ^ 2) + gq;
^~
/home/test.cpp:9:5: note: "root" binds here
int g3 = gw / gt - gw / gr * gg / ( gt - gw ^ 2) + gq;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 matches.
“ myExpr”綁定到您想要的內容。 參考: http : //clang.llvm.org/docs/LibASTMatchersReference.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.