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

Code before:


RunCommand(f"settings append target.source-map . {project_root}")
RunCommand(f"settings append target.source-map /proc/self/cwd {project_root}")

# Matches the output of `print Dump(...)` and captures the stuff from inside the
# std::string while discarding the std::string type.
dump_re = re.compile('\\(std::string\\) "((:?.|\n)+)"', re.MULTILINE)


# A helper to ease calling the Dump() free functions.
def cmd_dump(debugger: Any, command: Any, result: Any, dict: Any) -> None:
    def print_usage() -> None:
        print("dump <context> [<ID>|<TYPE><ID>|<TYPE> <ID>|-- <ID>]")
        print("")
        print('TYPE can be "inst", "entity_name", etc.')

    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.

Review comment (suggestion):
Had you considered making this a dict? That might allow something like:

```
  if m := re.fullmatch("(\w+?)(\d+)"):
    id_type = m[0]
    if id_type in id_types:
      id = f"SemIR::{id_types[id_type]}({m[1]})"
```

Note versus the use of `at` below, the above uses `:=` to store the match, and regex capture groups.

Code after:


RunCommand(f"settings append target.source-map . {project_root}")
RunCommand(f"settings append target.source-map /proc/self/cwd {project_root}")

# Matches the output of `print Dump(...)` and captures the stuff from inside the
# std::string while discarding the std::string type.
dump_re = re.compile('\\(std::string\\) "((:?.|\n)+)"', re.MULTILINE)


# A helper to ease calling the Dump() free functions.
def cmd_dump(debugger: Any, command: Any, result: Any, dict: Any) -> None:
    def print_usage() -> None:
        print(
            """
Dumps the value of an associated ID, using the C++ Dump() functions.

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