获取llvm中可变长度数组的大小

获取llvm中可变长度数组的大小,llvm,llvm-clang,llvm-ir,Llvm,Llvm Clang,Llvm Ir,假设我有以下代码 void foo(int n){ int arr[n]; } int main(){ foo(5); } 对于函数foo中的数组,是否仍然需要获取n的值(在本例中为5) 这就是我到目前为止想到的 #include <clang/CodeGen/CodeGenAction.h> #include <clang/Frontend/CompilerInstance.h> #include <clang/Frontend/Compiler

假设我有以下代码

void foo(int n){ 
   int arr[n];
  }
int main(){
  foo(5);
}
对于函数foo中的数组,是否仍然需要获取n的值(在本例中为5)

这就是我到目前为止想到的

#include <clang/CodeGen/CodeGenAction.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Frontend/CompilerInvocation.h>
#include <clang/Basic/DiagnosticOptions.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
#include <llvm/ADT/IntrusiveRefCntPtr.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IRReader/IRReader.h>
#include <llvm/Support/SourceMgr.h>
#include <llvm/IR/Instructions.h>

using namespace llvm;

int main(){
 llvm::LLVMContext *llvmcx;
 static llvm::LLVMContext MyGlobalContext;
 llvmcx = &MyGlobalContext;
 llvm::SMDiagnostic Err= llvm::SMDiagnostic();

 std::unique_ptr<llvm::Module> module=llvm::getLazyIRFileModule("test.ll",Err,*llvmcx,false);

    for (auto curFref = module->getFunctionList().begin(), endFref = module->getFunctionList().end();
 curFref != endFref; ++curFref) {
        for(auto bb=curFref->getBasicBlockList().begin();bb!=curFref->getBasicBlockList().end();bb++){
            for(auto ii=bb->getInstList().begin();ii!=bb->getInstList().end();ii++){
                        if(llvm::dyn_cast<llvm::AllocaInst>(ii)){
                          AllocaInst *t=dyn_cast<AllocaInst>(ii);
                              if(t->isArrayAllocation()) { 
                                  if (llvm::ConstantInt* CI=dyn_cast<llvm::ConstantInt(t->getArraysize()){
                                      // this if is not satisfied                                  
                                   }
                              }                        


                         }
                   }
             }
       }
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间llvm;
int main(){
llvm::LLVMContext*llvmcx;
静态llvm::LLVMContext MyGlobalContext;
llvmcx=&MyGlobalContext;
llvm::SMDiagnostic Err=llvm::SMDiagnostic();
std::unique_ptr module=llvm::getLazyIRFileModule(“test.ll”,Err,*llvmcx,false);
对于(auto curFref=module->getFunctionList().begin(),endFref=module->getFunctionList().end();
curFref!=endFref;++curFref){
对于(自动bb=curFref->getBasicBlockList().begin();bb!=curFref->getBasicBlockList().end();bb++){
对于(自动ii=bb->getInstList().begin();ii!=bb->getInstList().end();ii++){
if(llvm::dyn_铸造(二)){
AllocaInst*t=动态铸造(ii);
如果(t->isArrayAllocation()){
if(llvm::ConstantInt*CI=dyn\u castgetArraysize()){
//这是不满意的
}
}                        
}
}
}
}
}

是否有其他方法可以获取可变大小数组的大小?

可能涉及ArrayType,它有一个名为GetNumements()的方法。但是LLVMIR是一种汇编语言,数组长度不需要检查。在定义良好的前端(如clang)的末尾之外进行访问可能会合理地希望对大小非零的分配使用零长度数组类型。很抱歉,我没有得到答案的最后一部分。。从我所能收集到的信息来看,您认为使用clang会更好吗?如果a是一个大小为7的数组,那么a[100]在大多数编程语言中要么是非法的,要么是未定义的。在LLVM IR中,它是合法的(当然语法不同),并且数组类型中给定的大小不是实际运行时数组大小的硬上限。这意味着您可以从类型中获取大小,但它可能表示您想要的大小,也可能不表示您想要的大小。明白。。谢谢!