Skip to content

[lldb] Support plain ObjC names in LLDBTypeInfoProvider #9320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,26 @@ class LLDBTypeInfoProvider : public swift::remote::TypeInfoProvider {
std::string wrapped;
// The mangled name passed in is bare. Add global prefix ($s) and type (D).
llvm::raw_string_ostream(wrapped) << "$s" << mangledName << 'D';
swift::Demangle::Demangler dem;
auto *node = dem.demangleSymbol(wrapped);
if (!node) {
// Try `mangledName` as plain ObjC class name. Ex: NSObject, NSView, etc.
auto maybeMangled = swift_demangle::mangleClass(
dem, swift::MANGLING_MODULE_OBJC, mangledName);
if (!maybeMangled.isSuccess()) {
LLDB_LOG(GetLog(LLDBLog::Types),
"[LLDBTypeInfoProvider] invalid mangled name: {0}",
mangledName);
return nullptr;
}
wrapped = maybeMangled.result();
LLDB_LOG(GetLog(LLDBLog::Types),
"[LLDBTypeInfoProvider] using mangled ObjC class name: {0}",
wrapped);
} else {
#ifndef NDEBUG
{
// Check that our hardcoded mangling wrapper is still up-to-date.
swift::Demangle::Context dem;
auto node = dem.demangleSymbolAsNode(wrapped);
assert(node && node->getKind() == swift::Demangle::Node::Kind::Global);
assert(node->getKind() == swift::Demangle::Node::Kind::Global);
assert(node->getNumChildren() == 1);
node = node->getChild(0);
assert(node->getKind() == swift::Demangle::Node::Kind::TypeMangling);
Expand All @@ -332,8 +346,9 @@ class LLDBTypeInfoProvider : public swift::remote::TypeInfoProvider {
assert(node->getNumChildren() == 1);
node = node->getChild(0);
assert(node->getKind() != swift::Demangle::Node::Kind::Type);
}
#endif
}

ConstString mangled(wrapped);
CompilerType swift_type = typesystem.GetTypeFromMangledTypename(mangled);
auto ts = swift_type.GetTypeSystem().dyn_cast_or_null<TypeSystemSwift>();
Expand Down
29 changes: 28 additions & 1 deletion lldb/source/Plugins/TypeSystem/Swift/SwiftDemangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,34 @@ static swift::Demangle::NodePointer
GetDemangledType(swift::Demangle::Demangler &dem, llvm::StringRef name) {
return GetType(dem.demangleSymbol(name));
}


/// Wrap node in Global/TypeMangling/Type.
static swift::Demangle::NodePointer
mangleType(swift::Demangle::Demangler &dem,
swift::Demangle::NodePointer typeNode) {
auto *global = dem.createNode(Node::Kind::Global);
auto *typeMangling = dem.createNode(Node::Kind::TypeMangling);
global->addChild(typeMangling, dem);
auto *type = dem.createNode(Node::Kind::Type);
typeMangling->addChild(type, dem);
type->addChild(typeNode, dem);
return global;
}

/// Produce a type mangling for a class.
inline ManglingErrorOr<std::string> mangleClass(swift::Demangle::Demangler &dem,
llvm::StringRef moduleName,
llvm::StringRef className) {
auto *classNode = dem.createNode(Node::Kind::Class);
auto *module =
dem.createNodeWithAllocatedText(Node::Kind::Module, moduleName);
auto *identifier =
dem.createNodeWithAllocatedText(Node::Kind::Identifier, className);
classNode->addChild(module, dem);
classNode->addChild(identifier, dem);
return mangleNode(mangleType(dem, classNode));
}

} // namespace swift_demangle
} // namespace lldb_private

Expand Down
3 changes: 3 additions & 0 deletions lldb/test/API/lang/swift/typerefs/objc-descendent/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SWIFT_SOURCES := main.swift

include Makefile.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import lldb
from lldbsuite.test.lldbtest import *
from lldbsuite.test.decorators import *
import lldbsuite.test.lldbutil as lldbutil


class TestCase(TestBase):
@swiftTest
@skipUnlessFoundation
def test(self):
"""Print an ObjC derived object without using the AST context."""
self.build()
lldbutil.run_to_source_breakpoint(
self, "// break here", lldb.SBFileSpec("main.swift")
)
self.runCmd("settings set symbols.swift-enable-ast-context false")
self.expect("v", substrs=["num = 15"])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Usually I prefer frame variable c over v since v is technically an alias, so there is a chance that it will be re-aliased later on to something else.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be nice to have a policy on this. The v alias could change, but it would have to be a breaking change for a test like this to be a problem. A breaking change seems unlikely.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are other instances, I'll make a follow up change to catch them all.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

13 changes: 13 additions & 0 deletions lldb/test/API/lang/swift/typerefs/objc-descendent/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Foundation

class C: NSObject {
var num: Int = 15
}

func main() {
let c = C()
// break here
print(c)
}

main()