如何在Clang LLVM AST中获取UnaryOperator的DevlRefExpr操作数
How to get the DevlRefExpr operand out of UnaryOperator in the Clang LLVM AST?
我这里有这段代码:
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.
};
这是 voidp_dest->dump();
语句的输出:
UnaryOperator 0x2148d48 'int *' prefix '&'
`-DeclRefExpr 0x2148cf8 'int' lvalue Var 0x21480c0 'number' 'int'
在源代码中,我抓取的表达式如下所示:&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);
}
}
}
另一种用更少的LOC完成相同任务的方法,
DeclRefExpr* ref = nullptr;
if(auto UnOp = dyn_cast<UnaryOperator>(expr)) {
ref = dyn_cast<DeclRefExpr>(UnOp->getSubExp());
}
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 使用指针从C++中的数组中获取最大值
- 如何获取std::result_of函数的返回类型
- 如何在openssl-ecc中获取十六进制格式的私钥
- 使用Unreal C++获取VR耳机的世界位置/方向
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 从C字符串中获取奇怪的字符串长度
- 为什么我的for循环不能正确获取argv
- 从python中调用C++函数并获取返回值
- 如何获取一个数字的前3位
- 获取字符串的长度并将其分配给数组
- 无法获取菜单选择以运行函数.C++
- 数组长度,为什么从命令行获取时不能使用它?
- Boost Spirit,获取迭代器内部语义动作
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- 具有默认值的引用获取函数
- xmake总是报告:错误:无法获取cxx的程序,为什么
- 如何在Clang LLVM AST中获取UnaryOperator的DevlRefExpr操作数