How to add a Metadata String to an LLVM module with the C++ API?

坚强是说给别人听的谎言 提交于 2019-12-01 05:37:20

Try this:

#include <llvm/LLVMContext.h>
#include <llvm/Module.h>
#include <llvm/Metadata.h>

using namespace llvm;

int main() {
  Module* module = new Module("test", getGlobalContext());

  Value *Elts[] = {
    MDString::get(module->getContext(), "test1")
  };
  MDNode *Node = MDNode::get(getGlobalContext(), Elts);

  NamedMDNode *NMD = module->getOrInsertNamedMetadata("test2");
  NMD->addOperand(Node);

  module->dump();
}

I am not sure if you are able to have metadata "floating around" as you say. If it's not attached to any part of your program then what good is it doing? I've been looking into MD a bit lately... I found similar code in lib/Analysis/DIBuilder.cpp. Good luck.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!