Skip to content
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

Add CustomDebugDescription conformance to AnyKeyPath #60133

Merged
merged 3 commits into from
Sep 13, 2022
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
5 changes: 5 additions & 0 deletions include/swift/Demangling/Demangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,11 @@ ManglingErrorOr<const char *> mangleNodeAsObjcCString(NodePointer node,
std::string nodeToString(NodePointer Root,
const DemangleOptions &Options = DemangleOptions());

/// Transforms a mangled key path accessor thunk helper
/// into the identfier/subscript that would be used to invoke it in swift code.
std::string keyPathSourceString(const char *MangledName,
size_t MangledNameLength);

/// A class for printing to a std::string.
class DemanglerPrinter {
public:
Expand Down
143 changes: 142 additions & 1 deletion lib/Demangling/NodePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
//
//===----------------------------------------------------------------------===//

#include "swift/AST/Ownership.h"
#include "swift/Basic/STLExtras.h"
#include "swift/Demangling/Demangle.h"
#include "swift/AST/Ownership.h"
#include "swift/Strings.h"
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <vector>

using namespace swift;
using namespace Demangle;
Expand Down Expand Up @@ -3211,6 +3212,146 @@ void NodePrinter::printEntityType(NodePointer Entity, NodePointer type,
}
}

NodePointer
matchSequenceOfKinds(NodePointer start,
std::vector<std::tuple<Node::Kind, size_t>> pattern) {
if (start != nullptr) {
NodePointer current = start;
size_t idx = 0;
while (idx < pattern.size()) {
std::tuple<Node::Kind, size_t> next = pattern[idx];
idx += 1;
NodePointer nextChild = current->getChild(std::get<1>(next));
if (nextChild != nullptr && nextChild->getKind() == std::get<0>(next)) {
current = nextChild;
} else {
return nullptr;
}
}
if (idx == pattern.size()) {
return current;
} else {
return nullptr;
}
} else {
return nullptr;
}
}

std::string Demangle::keyPathSourceString(const char *MangledName,
size_t MangledNameLength) {
std::string invalid = "";
std::string unlabelledArg = "_: ";
Context ctx;
NodePointer root =
ctx.demangleSymbolAsNode(StringRef(MangledName, MangledNameLength));
if (!root)
return invalid;
if (root->getNumChildren() >= 1) {
NodePointer firstChild = root->getChild(0);
if (firstChild->getKind() == Node::Kind::KeyPathGetterThunkHelper) {
NodePointer child = firstChild->getChild(0);
switch (child->getKind()) {
case Node::Kind::Subscript: {
std::string subscriptText = "subscript(";
std::vector<std::string> argumentTypeNames;
// Multiple arguments case
NodePointer argList = matchSequenceOfKinds(
child, {
std::make_pair(Node::Kind::Type, 2),
std::make_pair(Node::Kind::FunctionType, 0),
std::make_pair(Node::Kind::ArgumentTuple, 0),
std::make_pair(Node::Kind::Type, 0),
std::make_pair(Node::Kind::Tuple, 0),
});
if (argList != nullptr) {
size_t numArgumentTypes = argList->getNumChildren();
size_t idx = 0;
while (idx < numArgumentTypes) {
NodePointer argumentType = argList->getChild(idx);
idx += 1;
if (argumentType->getKind() == Node::Kind::TupleElement) {
argumentType =
argumentType->getChild(0)->getChild(0)->getChild(1);
if (argumentType->getKind() == Node::Kind::Identifier) {
argumentTypeNames.push_back(
std::string(argumentType->getText()));
continue;
}
}
argumentTypeNames.push_back("<Unknown>");
}
} else {
// Case where there is a single argument
argList = matchSequenceOfKinds(
child, {
std::make_pair(Node::Kind::Type, 2),
std::make_pair(Node::Kind::FunctionType, 0),
std::make_pair(Node::Kind::ArgumentTuple, 0),
std::make_pair(Node::Kind::Type, 0),
});
if (argList != nullptr) {
argumentTypeNames.push_back(
std::string(argList->getChild(0)->getChild(1)->getText()));
}
}
child = child->getChild(1);
size_t idx = 0;
// There is an argument label:
if (child != nullptr) {
if (child->getKind() == Node::Kind::LabelList) {
size_t numChildren = child->getNumChildren();
if (numChildren == 0) {
subscriptText += unlabelledArg + argumentTypeNames[0];
} else {
while (idx < numChildren) {
Node *argChild = child->getChild(idx);
idx += 1;
if (argChild->getKind() == Node::Kind::Identifier) {
subscriptText += std::string(argChild->getText()) + ": " +
argumentTypeNames[idx - 1];
if (idx != numChildren) {
subscriptText += ", ";
}
} else if (argChild->getKind() ==
Node::Kind::FirstElementMarker ||
argChild->getKind() == Node::Kind::VariadicMarker) {
subscriptText += unlabelledArg + argumentTypeNames[idx - 1];
}
}
}
}
} else {
subscriptText += unlabelledArg + argumentTypeNames[0];
}
return subscriptText + ")";
}
case Node::Kind::Variable: {
child = child->getChild(1);
if (child == nullptr) {
return invalid;
}
if (child->getKind() == Node::Kind::PrivateDeclName) {
child = child->getChild(1);
if (child == nullptr) {
return invalid;
}
if (child->getKind() == Node::Kind::Identifier) {
return std::string(child->getText());
}
} else if (child->getKind() == Node::Kind::Identifier) {
return std::string(child->getText());
}
break;
}
default:
return invalid;
}
}
}
return invalid;
}

std::string Demangle::nodeToString(NodePointer root,
const DemangleOptions &options) {
if (!root)
Expand Down
118 changes: 118 additions & 0 deletions stdlib/public/core/KeyPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3711,3 +3711,121 @@ internal func _instantiateKeyPathBuffer(
}
}

#if SWIFT_ENABLE_REFLECTION

@_silgen_name("swift_keyPath_dladdr")
fileprivate func keypath_dladdr(_: UnsafeRawPointer) -> UnsafePointer<CChar>?

@_silgen_name("swift_keyPathSourceString")
fileprivate func demangle(
name: UnsafePointer<CChar>
) -> UnsafeMutablePointer<CChar>?

fileprivate func dynamicLibraryAddress<Base, Leaf>(
of pointer: ComputedAccessorsPtr,
_: Base.Type,
_ leaf: Leaf.Type
) -> String {
let getter: ComputedAccessorsPtr.Getter<Base, Leaf> = pointer.getter()
let pointer = unsafeBitCast(getter, to: UnsafeRawPointer.self)
if let cString = keypath_dladdr(UnsafeRawPointer(pointer)) {
if let demangled = demangle(name: cString)
.map({ pointer in
defer {
pointer.deallocate()
}
return String(cString: pointer)
}) {
return demangled
}
}
return "<computed \(pointer) (\(leaf))>"
}

#endif

@available(SwiftStdlib 5.8, *)
extension AnyKeyPath: CustomDebugStringConvertible {

#if SWIFT_ENABLE_REFLECTION
Copy link
Contributor

Choose a reason for hiding this comment

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

This works, but I think the rest of the stdlib has used the style of:

#if SWIFT_ENABLE_REFLECTION
extension AnyKeyPath: CustomDebugStringConvertible {
  ...
}
#else
extension AnyKeyPath: CustomDebugStringConvertible {
  give default message here
}
#endif

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've changed this to not be nested in the function, matching SourceCompatibilityShims.swift.

@available(SwiftStdlib 5.8, *)
public var debugDescription: String {
var description = "\\\(String(describing: Self.rootType))"
return withBuffer {
var buffer = $0
if buffer.data.isEmpty {
_internalInvariantFailure("key path has no components")
}
var valueType: Any.Type = Self.rootType
while true {
let (rawComponent, optNextType) = buffer.next()
let hasEnded = optNextType == nil
let nextType = optNextType ?? Self.valueType
switch rawComponent.value {
case .optionalForce, .optionalWrap, .optionalChain:
break
default:
description.append(".")
}
switch rawComponent.value {
case .class(let offset),
.struct(let offset):
let count = _getRecursiveChildCount(valueType)
let index = (0..<count)
.first(where: { i in
_getChildOffset(
valueType,
index: i
) == offset
})
if let index = index {
var field = _FieldReflectionMetadata()
_ = _getChildMetadata(
valueType,
index: index,
fieldMetadata: &field
)
defer {
field.freeFunc?(field.name)
}
description.append(String(cString: field.name))
} else {
description.append("<offset \(offset) (\(nextType))>")
}
case .get(_, let accessors, _),
.nonmutatingGetSet(_, let accessors, _),
.mutatingGetSet(_, let accessors, _):
func project<Base>(base: Base.Type) -> String {
func project2<Leaf>(leaf: Leaf.Type) -> String {
dynamicLibraryAddress(
of: accessors,
base,
leaf
)
}
return _openExistential(nextType, do: project2)
}
description.append(
_openExistential(valueType, do: project)
)
case .optionalChain, .optionalWrap:
description.append("?")
case .optionalForce:
description.append("!")
}
if hasEnded {
break
}
valueType = nextType
}
return description
}
}
#else
@available(SwiftStdlib 5.8, *)
public var debugDescription: String {
"(value cannot be printed without reflection)"
}
#endif

}
39 changes: 31 additions & 8 deletions stdlib/public/runtime/ReflectionMirror.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@

#ifdef SWIFT_ENABLE_REFLECTION

#include "../SwiftShims/Reflection.h"
#include "ImageInspection.h"
#include "Private.h"
#include "WeakReference.h"
#include "swift/Basic/Lazy.h"
#include "swift/Runtime/Reflection.h"
#include "swift/Basic/Unreachable.h"
#include "swift/Demangling/Demangle.h"
#include "swift/Runtime/Casting.h"
#include "swift/Runtime/Config.h"
#include "swift/Runtime/Debug.h"
#include "swift/Runtime/Enum.h"
#include "swift/Runtime/HeapObject.h"
#include "swift/Runtime/Metadata.h"
#include "swift/Runtime/Enum.h"
#include "swift/Basic/Unreachable.h"
#include "swift/Demangling/Demangle.h"
#include "swift/Runtime/Debug.h"
#include "swift/Runtime/Portability.h"
#include "Private.h"
#include "WeakReference.h"
#include "../SwiftShims/Reflection.h"
#include "swift/Runtime/Reflection.h"
#include <cassert>
#include <cinttypes>
#include <cstdio>
Expand Down Expand Up @@ -1126,4 +1127,26 @@ id swift_reflectionMirror_quickLookObject(OpaqueValue *value, const Metadata *T)
}
#endif

SWIFT_CC(swift)
SWIFT_RUNTIME_STDLIB_INTERNAL const char *swift_keyPath_dladdr(void *address) {
SymbolInfo info;
if (lookupSymbol(address, &info) == 0) {
return 0;
} else {
return info.symbolName.get();
}
}

SWIFT_CC(swift)
SWIFT_RUNTIME_STDLIB_INTERNAL const
char *swift_keyPathSourceString(char *name) {
size_t length = strlen(name);
std::string mangledName = keyPathSourceString(name, length);
if (mangledName == "") {
return 0;
} else {
return strdup(mangledName.c_str());
}
}

#endif // SWIFT_ENABLE_REFLECTION
Loading