Skip to content

[lldb] Refactor IsTaggedPointer to use demangler #6637

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 @@ -17,6 +17,7 @@

#include "Plugins/ExpressionParser/Clang/ClangUtil.h"
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
#include "Plugins/TypeSystem/Swift/SwiftDemangle.h"
#include "lldb/Symbol/Function.h"
#include "lldb/Symbol/Variable.h"
#include "lldb/Symbol/VariableList.h"
Expand Down Expand Up @@ -3123,31 +3124,30 @@ TypeAndOrName SwiftLanguageRuntimeImpl::FixUpDynamicType(

bool SwiftLanguageRuntimeImpl::IsTaggedPointer(lldb::addr_t addr,
CompilerType type) {
swift::CanType swift_can_type = GetCanonicalSwiftType(type);
if (!swift_can_type)
Demangler dem;
auto *root = dem.demangleSymbol(type.GetMangledTypeName().GetStringRef());
using Kind = Node::Kind;
auto *unowned_node = swift_demangle::nodeAtPath(
root, {Kind::TypeMangling, Kind::Type, Kind::Unowned});
if (!unowned_node)
return false;
switch (swift_can_type->getKind()) {
case swift::TypeKind::UnownedStorage: {
Target &target = m_process.GetTarget();
llvm::Triple triple = target.GetArchitecture().GetTriple();
// On Darwin the Swift runtime stores unowned references to
// Objective-C objects as a pointer to a struct that has the
// actual object pointer at offset zero. The least significant bit
// of the reference pointer indicates whether the reference refers
// to an Objective-C or Swift object.
//
// This is a property of the Swift runtime(!). In the future it
// may be necessary to check for the version of the Swift runtime
// (or indirectly by looking at the version of the remote
// operating system) to determine how to interpret references.
if (triple.isOSDarwin())
// Check whether this is a reference to an Objective-C object.
if ((addr & 1) == 1)
return true;
} break;
default:
break;
}

Target &target = m_process.GetTarget();
llvm::Triple triple = target.GetArchitecture().GetTriple();
// On Darwin the Swift runtime stores unowned references to
// Objective-C objects as a pointer to a struct that has the
// actual object pointer at offset zero. The least significant bit
// of the reference pointer indicates whether the reference refers
// to an Objective-C or Swift object.
//
// This is a property of the Swift runtime(!). In the future it
// may be necessary to check for the version of the Swift runtime
// (or indirectly by looking at the version of the remote
// operating system) to determine how to interpret references.
if (triple.isOSDarwin())
// Check whether this is a reference to an Objective-C object.
if ((addr & 1) == 1)
return true;
return false;
}

Expand Down
55 changes: 55 additions & 0 deletions lldb/source/Plugins/TypeSystem/Swift/SwiftDemangle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//===-- SwiftDemangle.h ---------------------------------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#ifndef liblldb_SwiftDemangle_h_
#define liblldb_SwiftDemangle_h_

#include "swift/Demangling/Demangle.h"
#include "llvm/ADT/ArrayRef.h"

namespace lldb_private {
namespace swift_demangle {

/// Access an inner node by following the given Node::Kind path.
///
/// Note: The Node::Kind path is relative to the given root node. The root
/// node's Node::Kind must not be included in the path.
inline swift::Demangle::NodePointer
nodeAtPath(swift::Demangle::NodePointer root,
llvm::ArrayRef<swift::Demangle::Node::Kind> kind_path) {
if (!root)
return nullptr;

auto *node = root;
for (auto kind : kind_path) {
bool found = false;
for (auto *child : *node) {
assert(child && "swift::Demangle::Node has null child");
if (child && child->getKind() == kind) {
node = child;
found = true;
break;
}
}
// The current step (`kind`) of the path was not found in the children of
// the current `node`. The requested path does not exist.
if (!found)
return nullptr;
}

return node;
}

} // namespace swift_demangle
} // namespace lldb_private

#endif