Skip to content

Commit 8bdf106

Browse files
committed
reindent, cr feedback: removes manual memory management
1 parent deaf466 commit 8bdf106

File tree

3 files changed

+182
-166
lines changed

3 files changed

+182
-166
lines changed

lib/Demangling/NodePrinter.cpp

Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3213,71 +3213,71 @@ void NodePrinter::printEntityType(NodePointer Entity, NodePointer type,
32133213

32143214
std::string Demangle::keypathSourceString(const char *MangledName,
32153215
size_t MangledNameLength) {
3216-
std::string invalid = "";
3217-
std::string unlabelledArg = "_: ";
3218-
Context ctx;
3219-
NodePointer root = ctx
3220-
.demangleSymbolAsNode(StringRef(MangledName, MangledNameLength));
3221-
if (!root)
3222-
return invalid;
3223-
if (root->getNumChildren() >= 1) {
3224-
NodePointer firstChild = root->getChild(0);
3225-
if (firstChild->getKind() == Node::Kind::KeyPathGetterThunkHelper) {
3226-
NodePointer child = firstChild->getChild(0);
3227-
switch (child->getKind()) {
3228-
case Node::Kind::Subscript: {
3229-
std::string subscriptText = "subscript(";
3230-
child = child->getChild(1);
3231-
if (child != nullptr) {
3232-
// There is an argument label:
3233-
if (child->getKind() == Node::Kind::LabelList) {
3234-
size_t idx = 0;
3235-
size_t numChildren = child->getNumChildren();
3236-
if (numChildren == 0) {
3237-
subscriptText += unlabelledArg;
3238-
} else {
3239-
while (idx < numChildren) {
3240-
Node *argChild = child->getChild(idx);
3241-
idx += 1;
3242-
if (argChild->getKind() == Node::Kind::Identifier) {
3243-
subscriptText += std::string(argChild->getText()) + ": ";
3244-
} else if (argChild->getKind() == Node::Kind::FirstElementMarker ||
3245-
argChild->getKind() == Node::Kind::VariadicMarker) {
3246-
subscriptText += unlabelledArg;
3247-
}
3248-
}
3249-
}
3250-
}
3251-
} else {
3252-
subscriptText += unlabelledArg;
3253-
}
3254-
return subscriptText + ")";
3255-
}
3256-
case Node::Kind::Variable: {
3257-
child = child->getChild(1);
3258-
if (child == nullptr) {
3259-
return invalid;
3260-
}
3261-
if (child->getKind() == Node::Kind::PrivateDeclName) {
3262-
child = child->getChild(1);
3263-
if (child == nullptr) {
3264-
return invalid;
3265-
}
3266-
if (child->getKind() == Node::Kind::Identifier) {
3267-
return std::string(child->getText());
3268-
}
3269-
} else if (child->getKind() == Node::Kind::Identifier){
3270-
return std::string(child->getText());
3271-
}
3272-
break;
3216+
std::string invalid = "";
3217+
std::string unlabelledArg = "_: ";
3218+
Context ctx;
3219+
NodePointer root = ctx
3220+
.demangleSymbolAsNode(StringRef(MangledName, MangledNameLength));
3221+
if (!root)
3222+
return invalid;
3223+
if (root->getNumChildren() >= 1) {
3224+
NodePointer firstChild = root->getChild(0);
3225+
if (firstChild->getKind() == Node::Kind::KeyPathGetterThunkHelper) {
3226+
NodePointer child = firstChild->getChild(0);
3227+
switch (child->getKind()) {
3228+
case Node::Kind::Subscript: {
3229+
std::string subscriptText = "subscript(";
3230+
child = child->getChild(1);
3231+
if (child != nullptr) {
3232+
// There is an argument label:
3233+
if (child->getKind() == Node::Kind::LabelList) {
3234+
size_t idx = 0;
3235+
size_t numChildren = child->getNumChildren();
3236+
if (numChildren == 0) {
3237+
subscriptText += unlabelledArg;
3238+
} else {
3239+
while (idx < numChildren) {
3240+
Node *argChild = child->getChild(idx);
3241+
idx += 1;
3242+
if (argChild->getKind() == Node::Kind::Identifier) {
3243+
subscriptText += std::string(argChild->getText()) + ": ";
3244+
} else if (argChild->getKind() == Node::Kind::FirstElementMarker ||
3245+
argChild->getKind() == Node::Kind::VariadicMarker) {
3246+
subscriptText += unlabelledArg;
3247+
}
32733248
}
3274-
default:
3275-
return invalid;
3276-
3249+
}
32773250
}
3251+
} else {
3252+
subscriptText += unlabelledArg;
3253+
}
3254+
return subscriptText + ")";
32783255
}
3256+
case Node::Kind::Variable: {
3257+
child = child->getChild(1);
3258+
if (child == nullptr) {
3259+
return invalid;
3260+
}
3261+
if (child->getKind() == Node::Kind::PrivateDeclName) {
3262+
child = child->getChild(1);
3263+
if (child == nullptr) {
3264+
return invalid;
3265+
}
3266+
if (child->getKind() == Node::Kind::Identifier) {
3267+
return std::string(child->getText());
3268+
}
3269+
} else if (child->getKind() == Node::Kind::Identifier){
3270+
return std::string(child->getText());
3271+
}
3272+
break;
3273+
}
3274+
default:
3275+
return invalid;
3276+
3277+
}
32793278
}
3280-
return invalid;
3279+
}
3280+
return invalid;
32813281
}
32823282

32833283
std::string Demangle::nodeToString(NodePointer root,

stdlib/public/core/KeyPath.swift

Lines changed: 104 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -3713,113 +3713,128 @@ internal func _instantiateKeyPathBuffer(
37133713

37143714
#if SWIFT_ENABLE_REFLECTION
37153715

3716+
fileprivate class StringBox {
3717+
3718+
init(_ wrapped: String) {
3719+
self.wrapped = wrapped
3720+
}
3721+
3722+
let wrapped: String
3723+
}
3724+
37163725
@_silgen_name("swift_keypath_dladdr")
37173726
fileprivate func keypath_dladdr(_: UnsafeRawPointer) -> UnsafePointer<CChar>?
37183727

37193728
@_silgen_name("swift_keypathSourceString")
37203729
fileprivate func demangle(
3721-
name: UnsafePointer<CChar>
3722-
) -> UnsafePointer<CChar>?
3730+
name: UnsafePointer<CChar>,
3731+
_: @convention(c) (UnsafePointer<CChar>) -> (UnsafeMutableRawPointer)
3732+
) -> UnsafeMutableRawPointer?
37233733

37243734
fileprivate func dynamicLibraryAddress<Base, Leaf>(
3725-
of pointer: ComputedAccessorsPtr,
3726-
_: Base.Type,
3727-
_ leaf: Leaf.Type
3735+
of pointer: ComputedAccessorsPtr,
3736+
_: Base.Type,
3737+
_ leaf: Leaf.Type
37283738
) -> String {
3729-
let getter: ComputedAccessorsPtr.Getter<Base, Leaf> = pointer.getter()
3730-
let pointer = unsafeBitCast(getter, to: UnsafeRawPointer.self)
3731-
if let cString = keypath_dladdr(UnsafeRawPointer(pointer)),
3732-
let demangled = demangle(name: cString) {
3733-
defer {
3734-
demangled.deallocate()
3735-
}
3736-
return String(cString: demangled)
3737-
} else {
3738-
return "<computed \(pointer) (\(leaf))>"
3739-
}
3739+
let getter: ComputedAccessorsPtr.Getter<Base, Leaf> = pointer.getter()
3740+
let pointer = unsafeBitCast(getter, to: UnsafeRawPointer.self)
3741+
if let cString = keypath_dladdr(UnsafeRawPointer(pointer)) {
3742+
if let demangled = demangle(
3743+
name: cString,
3744+
{ pointer in
3745+
Unmanaged.passRetained(StringBox(String(cString: pointer))).toOpaque()
3746+
}
3747+
)
3748+
.map({ Unmanaged<StringBox>.fromOpaque(UnsafeRawPointer($0)) })?
3749+
.takeRetainedValue()
3750+
.wrapped {
3751+
return demangled
3752+
}
3753+
}
3754+
return "<computed \(pointer) (\(leaf))>"
37403755
}
37413756

37423757
#endif
37433758

37443759
@available(SwiftStdLib 5.7, *)
37453760
extension AnyKeyPath: CustomDebugStringConvertible {
3746-
3747-
@available(SwiftStdLib 5.7, *)
3748-
public var debugDescription: String {
3761+
3762+
@available(SwiftStdLib 5.7, *)
3763+
public var debugDescription: String {
37493764
#if SWIFT_ENABLE_REFLECTION
3750-
var description = "\\\(String(describing: Self.rootType))"
3751-
return withBuffer {
3752-
var buffer = $0
3753-
if buffer.data.isEmpty {
3754-
_internalInvariantFailure("key path has no components")
3765+
var description = "\\\(String(describing: Self.rootType))"
3766+
return withBuffer {
3767+
var buffer = $0
3768+
if buffer.data.isEmpty {
3769+
_internalInvariantFailure("key path has no components")
3770+
}
3771+
var valueType: Any.Type = Self.rootType
3772+
while true {
3773+
let (rawComponent, optNextType) = buffer.next()
3774+
let hasEnded = optNextType == nil
3775+
let nextType = optNextType ?? Self.valueType
3776+
switch rawComponent.value {
3777+
case .optionalForce, .optionalWrap, .optionalChain:
3778+
break
3779+
default:
3780+
description.append(".")
3781+
}
3782+
switch rawComponent.value {
3783+
case .class(let offset),
3784+
.struct(let offset):
3785+
let count = _getRecursiveChildCount(valueType)
3786+
let index = (0..<count)
3787+
.first(where: { i in
3788+
_getChildOffset(
3789+
valueType,
3790+
index: i
3791+
) == offset
3792+
})
3793+
if let index = index {
3794+
var field = _FieldReflectionMetadata()
3795+
_ = _getChildMetadata(
3796+
valueType,
3797+
index: index,
3798+
fieldMetadata: &field
3799+
)
3800+
defer {
3801+
field.freeFunc?(field.name)
37553802
}
3756-
var valueType: Any.Type = Self.rootType
3757-
while true {
3758-
let (rawComponent, optNextType) = buffer.next()
3759-
let hasEnded = optNextType == nil
3760-
let nextType = optNextType ?? Self.valueType
3761-
switch rawComponent.value {
3762-
case .optionalForce, .optionalWrap, .optionalChain:
3763-
break
3764-
default:
3765-
description.append(".")
3766-
}
3767-
switch rawComponent.value {
3768-
case .class(let offset),
3769-
.struct(let offset):
3770-
let count = _getRecursiveChildCount(valueType)
3771-
let index = (0..<count)
3772-
.first(where: { i in
3773-
_getChildOffset(
3774-
valueType,
3775-
index: i
3776-
) == offset
3777-
})
3778-
if let index = index {
3779-
var field = _FieldReflectionMetadata()
3780-
_ = _getChildMetadata(
3781-
valueType,
3782-
index: index,
3783-
fieldMetadata: &field
3784-
)
3785-
defer {
3786-
field.freeFunc?(field.name)
3787-
}
3788-
description.append(String(cString: field.name))
3789-
} else {
3790-
description.append("<offset \(offset) (\(nextType))>")
3791-
}
3792-
case .get(_, let accessors, _),
3793-
.nonmutatingGetSet(_, let accessors, _),
3794-
.mutatingGetSet(_, let accessors, _):
3795-
func project<Base>(base: Base.Type) -> String {
3796-
func project2<Leaf>(leaf: Leaf.Type) -> String {
3797-
dynamicLibraryAddress(
3798-
of: accessors,
3799-
base,
3800-
leaf
3801-
)
3802-
}
3803-
return _openExistential(nextType, do: project2)
3804-
}
3805-
description.append(
3806-
_openExistential(valueType, do: project)
3807-
)
3808-
case .optionalChain, .optionalWrap:
3809-
description.append("?")
3810-
case .optionalForce:
3811-
description.append("!")
3812-
}
3813-
if hasEnded {
3814-
break
3815-
}
3816-
valueType = nextType
3803+
description.append(String(cString: field.name))
3804+
} else {
3805+
description.append("<offset \(offset) (\(nextType))>")
3806+
}
3807+
case .get(_, let accessors, _),
3808+
.nonmutatingGetSet(_, let accessors, _),
3809+
.mutatingGetSet(_, let accessors, _):
3810+
func project<Base>(base: Base.Type) -> String {
3811+
func project2<Leaf>(leaf: Leaf.Type) -> String {
3812+
dynamicLibraryAddress(
3813+
of: accessors,
3814+
base,
3815+
leaf
3816+
)
38173817
}
3818-
return description
3818+
return _openExistential(nextType, do: project2)
3819+
}
3820+
description.append(
3821+
_openExistential(valueType, do: project)
3822+
)
3823+
case .optionalChain, .optionalWrap:
3824+
description.append("?")
3825+
case .optionalForce:
3826+
description.append("!")
3827+
}
3828+
if hasEnded {
3829+
break
38193830
}
3831+
valueType = nextType
3832+
}
3833+
return description
3834+
}
38203835
#else
3821-
return noReflectionMetadataErrorMessage
3836+
return noReflectionMetadataErrorMessage
38223837
#endif
3823-
}
3824-
3838+
}
3839+
38253840
}

stdlib/public/runtime/ReflectionMirror.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,25 +1129,26 @@ id swift_reflectionMirror_quickLookObject(OpaqueValue *value, const Metadata *T)
11291129

11301130
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERNAL
11311131
const char *swift_keypath_dladdr(void *address) {
1132-
SymbolInfo info;
1133-
if (lookupSymbol(address, &info) == 0) {
1134-
return 0;
1135-
} else {
1136-
return info.symbolName.get();
1137-
}
1132+
SymbolInfo info;
1133+
if (lookupSymbol(address, &info) == 0) {
1134+
return 0;
1135+
} else {
1136+
return info.symbolName.get();
1137+
}
11381138
}
11391139

11401140
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERNAL
1141-
const char *swift_keypathSourceString(char *name) {
1142-
size_t length = strlen(name);
1143-
std::string mangledName = keypathSourceString(name, length);
1144-
if (mangledName == "") {
1145-
return 0;
1146-
} else {
1147-
char *rtnValue = new char[mangledName.length() + 1];
1148-
strcpy(rtnValue, mangledName.c_str());
1149-
return rtnValue;
1150-
}
1141+
const void *swift_keypathSourceString(
1142+
char *name,
1143+
void *(callback)(const char *)
1144+
) {
1145+
size_t length = strlen(name);
1146+
std::string mangledName = keypathSourceString(name, length);
1147+
if (mangledName == "") {
1148+
return 0;
1149+
} else {
1150+
return callback(mangledName.c_str());
1151+
}
11511152
}
11521153

11531154
#endif // SWIFT_ENABLE_REFLECTION

0 commit comments

Comments
 (0)