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

Code before:

    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.
    if m := re.fullmatch("([a-z_]+)(\\d+)", args[1]):
        if m[1] in id_types:
            if len(args) != 2:
                print_usage()
                return
            id_type = m[1]
            id = f"SemIR::{id_types[id_type]}({m[2]})"

    # Look for <type> <id> as two arguments.
    if not id_type:
        if args[1] in id_types:
            if len(args) != 3:
                print_usage()
                return
            id_type = args[1]
            id = f"SemIR::{id_types[id_type]}({args[2]})"

    if not id_type:
        # Use `--` to escape a variable name like `inst22`.
        if args[1] == "--":
            id = " ".join(args[2:])

Review comment (suggestion):
Minor typo
```suggestion
    # Try finding a type + id from the input args. If not, the id will be passed
```

Code after:

    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",
    }

    def print_dump(context: str, expr: str) -> None:
        cmd = f"p Dump({context}, {expr})"
        out = RunCommand(cmd, print_command=False)
        if m := re.match(dump_re, out):
            # Use the `dump_re` match to print just the interesting part of the
            # dump output.
            print(m[1])
        else:
            # Unexpected output, show the command that was run.
            print(f"(lldb) {cmd}")
            print(out)

    # Try to 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.
    if m := re.fullmatch("([a-z_]+)(\\d+)", args[1]):
        if m[1] in id_types:
            if len(args) != 2:
                print_usage()
                return
            id_type = m[1]
            print_dump(context, f"{id_types[id_type]}({m[2]})")

    # Look for <type> <id> as two arguments.