CallInst 构造函数是私有的

CallInst constructor is private?

本文关键字:构造函数 CallInst      更新时间:2023-10-16

我正在尝试使用LLVM构建代码分析工具的简单版本.我有几个.ll文件,其中包含某些程序的中间LLVM表示形式,并且我正在尝试获取在程序的每个函数中执行的函数调用列表。

这是我的代码,由于我上一篇文章的答案而获得。

void getFunctionCalls(const Module *M)
{
   for (const Function &F : *M) {
      for (const BasicBlock &BB : F) {
        for (const Instruction &I : BB) {
          if (CallInst callInst = dyn_cast<CallInst>(I)) {
            if (Function *calledFunction = callInst->getCalledFunction())     {
              if (calledFunction->getName().startswith("llvm.dbg.declare")) {
                // Do something
              }
            }
          }
        }
      }
    }

}

当我编译它时,我收到一个错误说:

home/kike/llvm-3.9.0.src/include/llvm/IR/Instructions.h: In function ‘void getFunctionCalls(const llvm::Module*)’:
/home/kike/llvm-3.9.0.src/include/llvm/IR/Instructions.h:1357:3: error: ‘llvm::CallInst::CallInst(const llvm::CallInst&)’ is private

这意味着 CallInst 构造函数是私有的?在这种情况下,如何获取函数调用列表?

[编辑1]:

我也尝试通过 I 作为参考,如下所示:

void getFunctionCalls(const Module *M)
{
   for (const Function &F : *M) {
      for (const BasicBlock &BB : F) {
        for (const Instruction &I : BB) {
          if (CallInst * callInst = dyn_cast<CallInst>(&I)) {
            if (Function *calledFunction = callInst->getCalledFunction())     {
              if (calledFunction->getName().startswith("llvm.dbg.declare")) {
                // Do something
              }
            }
          }
        }
      }
    }

}

我收到此错误:

invalid conversion from ‘llvm::cast_retty<llvm::CallInst, const llvm::Instruction*>::ret_type {aka const llvm::CallInst*}’ to ‘llvm::CallInst*’

CallInst 没有复制构造函数,因为它不是按值传递的。用

const CallInst* callInst = dyn_cast<CallInst>(&I)

而不是

CallInst callInst = dyn_cast<CallInst>(I)