C++ 如何在Clang LLVM AST中从一元运算符中获取DevlRefExpr操作数?
我这里有这个代码:C++ 如何在Clang LLVM AST中从一元运算符中获取DevlRefExpr操作数?,c++,abstract-syntax-tree,llvm-c++-api,C++,Abstract Syntax Tree,Llvm C++ Api,我这里有这个代码: class MemcpyMatcher : public MatchFinder::MatchCallback { public: MemcpyMatcher(map<string, Replacements> * replacements) : replacements(replacements) {} /* Callback method for the MatchFinder. * @param result -
class MemcpyMatcher : public MatchFinder::MatchCallback
{
public:
MemcpyMatcher(map<string, Replacements> * replacements)
: replacements(replacements) {}
/* Callback method for the MatchFinder.
* @param result - Found matching results.
*/
virtual void run(const MatchFinder::MatchResult& result)
{
const CallExpr* call_expr = result.Nodes.getNodeAs<CallExpr>("memcpy_call");
if (call_expr != NULL) {
const Expr* voidp_dest = call_expr->getArg(0)->IgnoreImplicit();
const Expr* voidp_src = call_expr->getArg(1)->IgnoreImplicit();
const Expr* size_t_n = call_expr->getArg(2)->IgnoreImplicit();
voidp_dest->dump();
}
private:
map<string, Replacements>* replacements;
// Add other variables here as needed.
};
在源代码中,我获取的表达式如下:&number
我想从
UnaryOperator
中获取DeclRefExpr
,以便将其转换为字符串并获取变量的名称。我不知道怎么做。对于仍在寻找答案的人:
const DeclRefExpr* decl_ref = nullptr;
if (auto unary = dyn_cast<UnaryOperator>(expr)) {
if (unary->getOpcode() == UnaryOperator::Opcode::UO_AddrOf) {
unsigned count = 0;
// there will be only one child
for (auto child : unary->children()) {
decl_ref = dyn_cast<DeclRefExpr>(child);
}
}
}
const DeclRefExpr*decl_ref=nullptr;
if(自动一元=动态转换(expr)){
如果(一元->getOpcode()==一元运算符::操作码::UO_AddrOf){
无符号计数=0;
//只有一个孩子
对于(自动子对象:一元->子对象()){
decl_ref=动态投射(儿童);
}
}
}
对于仍在搜索答案的任何人:
const DeclRefExpr* decl_ref = nullptr;
if (auto unary = dyn_cast<UnaryOperator>(expr)) {
if (unary->getOpcode() == UnaryOperator::Opcode::UO_AddrOf) {
unsigned count = 0;
// there will be only one child
for (auto child : unary->children()) {
decl_ref = dyn_cast<DeclRefExpr>(child);
}
}
}
const DeclRefExpr*decl_ref=nullptr;
if(自动一元=动态转换(expr)){
如果(一元->getOpcode()==一元运算符::操作码::UO_AddrOf){
无符号计数=0;
//只有一个孩子
对于(自动子对象:一元->子对象()){
decl_ref=动态投射(儿童);
}
}
}
用更少的LOC完成相同任务的另一种方法
DeclRefExpr* ref = nullptr;
if(auto UnOp = dyn_cast<UnaryOperator>(expr)) {
ref = dyn_cast<DeclRefExpr>(UnOp->getSubExp());
}
DeclRefExpr*ref=nullptr;
if(自动UnOp=dyn_cast(expr)){
ref=dyn_cast(UnOp->getSubExp());
}
用更少的LOC完成相同任务的另一种方法
DeclRefExpr* ref = nullptr;
if(auto UnOp = dyn_cast<UnaryOperator>(expr)) {
ref = dyn_cast<DeclRefExpr>(UnOp->getSubExp());
}
DeclRefExpr*ref=nullptr;
if(自动UnOp=dyn_cast(expr)){
ref=dyn_cast(UnOp->getSubExp());
}
答案在哪里?答案在哪里?