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;
}

Module::iterator MIRParserImpl::getNextUnnamedFunction(Module &M) {
  if (UnusedUnnamedFunction == M.end())
    return UnusedUnnamedFunction;

  for (auto F = ++UnusedUnnamedFunction; F != M.end(); F++)
    if (!F->hasName())
      return F;

  return M.end();
}

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):
You can just directly use UnusedUnnamedFunction, and don't need this special case early exit or return of M.end 

Code after:
  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() ||
               !(F = getNextUnusedUnnamedFunction(M, FirstUnvisitedFunction))) {
      return error(Twine("function '") + FunctionName +
                   "' isn't defined in the provided LLVM IR");
    }
  }

  if (!MAM) {
    if (MMI.getMachineFunction(*F) != nullptr)
      return error(Twine("redefinition of machine function '") + FunctionName +
                   "'");