Repository: carbon-language/carbon-lang
File: toolchain/sem_ir/inst_namer.cpp (C++)

Code before:
  std::string name;
  switch (sem_ir_->parse_tree().node_kind(loc_id.node_id())) {
    case Parse::NodeKind::IfExprIf:
      name = BranchKindToName("if.expr", branch.kind, "then", "else", "result");
      break;

    case Parse::NodeKind::IfCondition:
      name = BranchKindToName("if", branch.kind, "then", "else",
                              /*branch_with_arg=*/std::nullopt);
      break;

    case Parse::NodeKind::IfStatement:
      name = "if.done";
      break;

    case Parse::NodeKind::ShortCircuitOperandAnd:
      name = BranchKindToName("and", branch.kind, "rhs",
                              /*branch=*/std::nullopt, "result");
      break;
    case Parse::NodeKind::ShortCircuitOperandOr:
      name = BranchKindToName("or", branch.kind, "rhs", /*branch=*/std::nullopt,
                              "result");
      break;

    case Parse::NodeKind::WhileConditionStart:
      name = "while.cond";
      break;

    case Parse::NodeKind::WhileCondition:
      name = BranchKindToName("while", branch.kind, "body", "done",
                              /*branch_with_arg=*/std::nullopt);
      break;

    default:
      break;
  }

  AddBlockLabel(scope_id, branch.target_id, name, loc_id);
}

auto InstNamer::CollectNamesInBlock(ScopeId scope_id, InstBlockId block_id)
    -> void {
  if (block_id.has_value()) {
    CollectNamesInBlock(scope_id, sem_ir_->inst_blocks().Get(block_id));
  }
}

auto InstNamer::CollectNamesInBlock(ScopeId top_scope_id,
                                    llvm::ArrayRef<InstId> block) -> void {
  llvm::SmallVector<std::pair<ScopeId, InstId>> queue;


Review comment (suggestion):
```suggestion
      name = BranchKindToName("while", branch.kind, {.branch = "cond"});
```

Code after:
    switch (inst_kind) {
      case BranchIf::Kind:
        return branch_if;
      case Branch::Kind:
        return branch;
      case BranchWithArg::Kind:
        return branch_with_arg;
      default:
        return "";
    }
  }

  // The kind of branch, based on the node kind.
  llvm::StringLiteral prefix;

  // For labeling branch instruction kinds. Only expected kinds need a value;
  // the empty string is for unexpected kinds.
  llvm::StringLiteral branch_if = "";
  llvm::StringLiteral branch = "";
  llvm::StringLiteral branch_with_arg = "";
};

// Finds and adds a suitable block label for the given SemIR instruction that
// represents some kind of branch.
auto InstNamer::AddBlockLabel(ScopeId scope_id, LocId loc_id, AnyBranch branch)
    -> void {
  std::string label;

  loc_id = sem_ir_->insts().GetCanonicalLocId(loc_id);
  if (loc_id.node_id().has_value()) {
    if (auto names = BranchNames::For(
            sem_ir_->parse_tree().node_kind(loc_id.node_id()))) {
      if (auto suffix = names->GetSuffix(branch.kind); !suffix.empty()) {
        label = llvm::formatv("{0}.{1}", names->prefix, suffix);
      } else {
        label =
            llvm::formatv("{0}.<unexpected {1}>", names->prefix, branch.kind);
      }
    }
  }

  AddBlockLabel(scope_id, branch.target_id, label, loc_id);
}

auto InstNamer::CollectNamesInBlock(ScopeId scope_id, InstBlockId block_id)
    -> void {
  if (block_id.has_value()) {
    CollectNamesInBlock(scope_id, sem_ir_->inst_blocks().Get(block_id));
  }
}