【问题标题】:LLDB for Swift: Custom type summary for generic typeLLDB for Swift:泛型类型的自定义类型摘要
【发布时间】:2020-06-14 01:02:38
【问题描述】:

使用 LLDB,我可以为类型添加自定义摘要:

(lldb) type summary add -s "This is a Foo" Baz.Foo

但是,对于具有两个或更多泛型的泛型类型,我无法执行此操作。

给定一个模块 Baz,其类型为 Foo,具有两个或多个泛型:

struct Foo<Bar: Numeric, Bar2: Numeric> {}

我尝试了以下方法;都没有成功:

  • type summary add -s "This is a Foo" Baz.Foo
  • type summary add -s "This is a Foo" Baz.Foo&lt;A, B&gt;
  • type summary add -s "This is a Foo" Baz.Foo&lt;Float, Float&gt;
  • type summary add -s "This is a Foo" Baz.Foo&lt;Bar, Bar2&gt;
  • type summary add -s "This is a Foo" Baz.Foo&lt;Float&gt;

在所有情况下,打印的是标准描述而不是自定义描述。

那么如何为具有两个或更多泛型的泛型类型添加自定义摘要,最好不必为BarBar2 指定具体类型?

【问题讨论】:

  • 我不熟悉摘要功能,但它能够在值上调用方法?如果是这样,您可以制作一个类似于CustomDebugStringConvertible 的摘要,并让Foo 使用所需的debugDescription 实现它
  • 可以在 LLDB 中加载 Python 脚本,当使用 fr v 命令(也可能是 p 命令)时,它将评估 debugDescriptiondescription 属性,即实际上我的最终目标。使用type summary add -s "whatever" Module.Type,我可以成功地定位非泛型类型或只有一个泛型参数的类型。但是,我的问题是这似乎不适用于具有两个或更多泛型类型参数的类型。
  • 您能否以CustomDebugStringConvertible 协议类型本身为目标,并让所有符合标准的类型(包括您的通用Foo&lt;Bar, Bar2&gt;)获取相同的摘要?
  • 这仅适用于我在定义类型摘要后第一次使用p value as CustomStringConvertible 打印任何类型的值。之后,它可以在没有as CustomStringConvertible 的情况下工作。此外,这不适用于 ~/.lldbinit 文件。因此,虽然这让我更接近我的目标,但它并不是 100%。
  • 如果我使用自定义标记协议(例如 protocol FooProtocol {})而不是 CustomStringConvertible,我可以通过我的 ~/.lldbinit 文件使其工作,但它仍然需要初始演员表。

标签: swift lldb


【解决方案1】:

使用 --regex/-x 标志在 Swift 中模式匹配泛型类型,或在 C++ 中使用模板类型。

type summary add -s "This is a Foo" -x "^Baz\.Foo<.+,.+>$"

您可以通过运行type summary list -l swift 查看许多示例。以下是Dictionary 的显示方式:

^Swift\.Dictionary<.+,.+>$:  (show children) (hide value) (skip references) Swift.Dictionary summary provider

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-13
    • 2019-07-21
    • 2013-09-30
    • 1970-01-01
    相关资源
    最近更新 更多