Repository: llvm/llvm-project
File: llvm/lib/CodeGen/MIRParser/MIRParser.cpp (C++)

Code before:
      return true;
    In.nextDocument();
  } while (In.setCurrentDocument());

  return false;
}

Function *MIRParserImpl::createDummyFunction(StringRef Name, Module &M) {
  auto &Context = M.getContext();
  Function *F =
      Function::Create(FunctionType::get(Type::getVoidTy(Context), false),
                       Function::ExternalLinkage, Name, M);
  BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
  new UnreachableInst(Context, BB);

  if (ProcessIRFunction)
    ProcessIRFunction(*F);

  return F;
}

Function *MIRParserImpl::getNextUnusedUnnamedFunction(Module &M) {
  for (; FirstUnvisitedFunction != M.end(); ++FirstUnvisitedFunction)
    if (!FirstUnvisitedFunction->hasName()) {
      auto *F = &*FirstUnvisitedFunction;
      ++FirstUnvisitedFunction;
      return F;
    }

  return nullptr;
}

bool MIRParserImpl::parseMachineFunction(Module &M, MachineModuleInfo &MMI,
                                         ModuleAnalysisManager *MAM) {
  // Parse the yaml.
  yaml::MachineFunction YamlMF;
  yaml::EmptyContext Ctx;

  const TargetMachine &TM = MMI.getTarget();
  YamlMF.MachineFuncInfo = std::unique_ptr<yaml::MachineFunctionInfo>(
      TM.createDefaultFuncInfoYAML());

  yaml::yamlize(In, YamlMF, false, Ctx);
  if (In.error())
    return true;

  // Search for the corresponding IR function.
  StringRef FunctionName = YamlMF.Name;
  Function *F = M.getFunction(FunctionName);
  if (!F) {
    if (NoLLVMIR) {

Review comment (suggestion):
I think this shouldn't be stored in MIRParserImpl. I think you can contain this fully within parseMachineFUnctions, and just pass in the iterator to parseMachineFunction

Code after:
  } while (In.setCurrentDocument());

  return false;
}

Function *MIRParserImpl::createDummyFunction(StringRef Name, Module &M) {
  auto &Context = M.getContext();
  Function *F =
      Function::Create(FunctionType::get(Type::getVoidTy(Context), false),
                       Function::ExternalLinkage, Name, M);
  BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
  new UnreachableInst(Context, BB);

  if (ProcessIRFunction)
    ProcessIRFunction(*F);

  return F;
}

static Function *
getNextUnusedUnnamedFunction(const Module &M,
                             Module::iterator &FirstUnvisitedFunction) {
  for (; FirstUnvisitedFunction != M.end(); ++FirstUnvisitedFunction)
    if (!FirstUnvisitedFunction->hasName())
      return &*FirstUnvisitedFunction++;

  return nullptr;
}

bool MIRParserImpl::parseMachineFunction(
    Module &M, MachineModuleInfo &MMI, ModuleAnalysisManager *MAM,
    Module::iterator &FirstUnvisitedFunction) {
  // Parse the yaml.
  yaml::MachineFunction YamlMF;
  yaml::EmptyContext Ctx;

  const TargetMachine &TM = MMI.getTarget();
  YamlMF.MachineFuncInfo = std::unique_ptr<yaml::MachineFunctionInfo>(
      TM.createDefaultFuncInfoYAML());

  yaml::yamlize(In, YamlMF, false, Ctx);
  if (In.error())
    return true;

  // Search for the corresponding IR function.
  StringRef FunctionName = YamlMF.Name;
  Function *F = M.getFunction(FunctionName);
  if (!F) {
    if (NoLLVMIR) {
      F = createDummyFunction(FunctionName, M);
    } else if (!FunctionName.empty() ||