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

Code before:
  }
  return llvm::formatv("{0}.{1}", prefix, 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 {
  loc_id = sem_ir_->insts().GetCanonicalLocId(loc_id);
  if (!loc_id.node_id().has_value()) {
    AddBlockLabel(scope_id, branch.target_id, "", loc_id);
    return;
  }
  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);

Review comment (suggestion):
May as well add the `<unexpected>` here too:
```suggestion
      name = BranchKindToName("if", branch.kind, {.branch = "done"});
```
(This one should never have a `BranchIf` or a `BranchWithArg` I think.)

Code after:
      case Parse::NodeKind::WhileConditionStart:
        return {{.prefix = "while", .branch = "cond"}};

      case Parse::NodeKind::WhileCondition:
        return {{.prefix = "while", .branch_if = "body", .branch = "done"}};

      default:
        return std::nullopt;
    }
  }

  // Returns the provided suffix for the instruction kind, or an empty string.
  auto GetSuffix(InstKind inst_kind) -> llvm::StringLiteral {
    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);
      }