Repository: carbon-language/carbon-lang
File: scripts/lldbinit.py (Python)

Code before:
    args = command.split(" ")
    if len(args) < 2:
        print_usage()
        return

    context = args[0]

    # The set of "Make" functions in dump.cpp.
    id_types = [
        ("class", "MakeClassId"),
        ("constant", "MakeConstantId"),
        ("symbolic_constant", "MakeSymbolicConstantId"),
        ("entity_name", "MakeEntityNameId"),
        ("facet_type", "MakeFacetTypeId"),
        ("function", "MakeFunctionId"),
        ("generic", "MakeGenericId"),
        ("impl", "MakeImplId"),
        ("inst_block", "MakeInstBlockId"),
        ("inst", "MakeInstId"),
        ("interface", "MakeInterfaceId"),
        ("name", "MakeNameId"),
        ("name_scope", "MakeNameScopeId"),
        ("identified_facet_type", "MakeIdentifiedFacetTypeId"),
        ("specific", "MakeSpecificId"),
        ("specific_interface", "MakeSpecificInterfaceId"),
        ("struct_type_fields", "MakeStructTypeFieldsId"),
        ("type", "MakeTypeId"),
    ]

    # Try find a type + id from the input args. If not, the id will be passed
    # through directly to C++, as it can be a variable name.
    id_type = None

    # Look for <type><id> as a single argument.
    for id_type_pair in id_types:
        if re.fullmatch(f"{id_type_pair[0]}[0-9]+", args[1]):
            at = len(id_type_pair[0])
            id_type = args[1][:at]
            id = args[1][at:]
            break

    # Look for <type> <id> as two arguments.
    if not id_type:
        for id_type_pair in id_types:
            if id_type_pair[0] == args[1]:
                if len(args) < 3:
                    print_usage()
                    return
                id_type = args[1]
                id = args[2]
                break

Review comment (suggestion):
Maybe logic should depend a little more on the number of arguments? What happens with:

```
dump <context> <type><id> blah
```

Code after:
Usage:
  dump <CONTEXT> [<EXPR>|-- <EXPR>|<TYPE><ID>|<TYPE> <ID>]

Args:
  CONTEXT is the dump context, such a SemIR::Context reference, a SemIR::File,
          a Parse::Context, or a Lex::TokenizeBuffer.
  EXPR is a C++ expression such as a variable name. Use `--` to prevent it from
       being treated as a TYPE and ID.
  TYPE can be `inst`, `constant`, `generic`, `impl`, `entity_name`, etc. See
       the `Label` string in `IdBase` classes to find possible TYPE names,
       though only Id types that have a matching `Make...Id()` function are
       supported.
  ID is an integer number, such as `42`.

Example usage:
  # Dumps the `inst_id` local variable, with a `context` local variable.
  dump context inst_id

  # Dumps the instruction with id 42, with a `context()` method for accessing
  # the `Check::Context&`.
  dump context() inst42
"""
        )

    args = command.split(" ")
    if len(args) < 2:
        print_usage()
        return

    context = args[0]

    # The set of "Make" functions in dump.cpp.
    id_types = {
        "class": "SemIR::MakeClassId",
        "constant": "SemIR::MakeConstantId",
        "symbolic_constant": "SemIR::MakeSymbolicConstantId",
        "entity_name": "SemIR::MakeEntityNameId",
        "facet_type": "SemIR::MakeFacetTypeId",
        "function": "SemIR::MakeFunctionId",
        "generic": "SemIR::MakeGenericId",
        "impl": "SemIR::MakeImplId",
        "inst_block": "SemIR::MakeInstBlockId",
        "inst": "SemIR::MakeInstId",
        "interface": "SemIR::MakeInterfaceId",
        "name": "SemIR::MakeNameId",
        "name_scope": "SemIR::MakeNameScopeId",
        "identified_facet_type": "SemIR::MakeIdentifiedFacetTypeId",
        "specific": "SemIR::MakeSpecificId",
        "specific_interface": "SemIR::MakeSpecificInterfaceId",
        "struct_type_fields": "SemIR::MakeStructTypeFieldsId",
        "type": "SemIR::MakeTypeId",