Skip to content

Commit 568b24b

Browse files
committed
cr feedback: move impls behind flag, remove addChain(), switch statement, fallthrough instead of if-elses, move import
1 parent 8032d27 commit 568b24b

File tree

4 files changed

+59
-55
lines changed

4 files changed

+59
-55
lines changed

lib/Demangling/NodePrinter.cpp

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include <cassert>
2222
#include <cstdio>
2323
#include <cstdlib>
24-
#include <stack>
2524

2625
using namespace swift;
2726
using namespace Demangle;
@@ -3225,58 +3224,58 @@ std::string Demangle::keypathSourceString(const char *MangledName,
32253224
NodePointer firstChild = root->getChild(0);
32263225
if (firstChild->getKind() == Node::Kind::KeyPathGetterThunkHelper) {
32273226
NodePointer child = firstChild->getChild(0);
3228-
if (child->getKind() == 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;
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+
}
32473248
}
32483249
}
32493250
}
3251+
} else {
3252+
subscriptText += unlabelledArg;
32503253
}
3251-
} else {
3252-
subscriptText += unlabelledArg;
3253-
}
3254-
return subscriptText + ")";
3255-
} else if (child->getKind() == Node::Kind::Variable) {
3256-
child = child->getChild(1);
3257-
if (child == nullptr) {
3258-
return invalid;
3254+
return subscriptText + ")";
32593255
}
3260-
if (child->getKind() == Node::Kind::PrivateDeclName) {
3256+
case Node::Kind::Variable: {
32613257
child = child->getChild(1);
32623258
if (child == nullptr) {
32633259
return invalid;
32643260
}
3265-
if (child->getKind() == Node::Kind::Identifier) {
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){
32663270
return std::string(child->getText());
32673271
}
3268-
} else if (child->getKind() == Node::Kind::Identifier){
3269-
return std::string(child->getText());
3272+
break;
32703273
}
3271-
return invalid;
3272-
} else {
3273-
return invalid;
3274+
default:
3275+
return invalid;
3276+
32743277
}
3275-
} else {
3276-
return invalid;
32773278
}
3278-
} else {
3279-
return invalid;
32803279
}
32813280
return invalid;
32823281
}

stdlib/public/core/KeyPath.swift

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3711,6 +3711,8 @@ internal func _instantiateKeyPathBuffer(
37113711
}
37123712
}
37133713

3714+
#if SWIFT_ENABLE_REFLECTION
3715+
37143716
@_silgen_name("swift_keypath_dladdr")
37153717
internal func keypath_dladdr(_: UnsafeRawPointer) -> UnsafePointer<CChar>?
37163718

@@ -3719,6 +3721,8 @@ internal func demangle(
37193721
name: UnsafePointer<CChar>
37203722
) -> UnsafePointer<CChar>?
37213723

3724+
#endif
3725+
37223726

37233727
fileprivate func dynamicLibraryAddress<Base, Leaf>(
37243728
of pointer: ComputedAccessorsPtr,
@@ -3743,6 +3747,7 @@ extension AnyKeyPath: CustomDebugStringConvertible {
37433747

37443748
@available(SwiftStdLib 5.7, *)
37453749
public var debugDescription: String {
3750+
#if SWIFT_ENABLE_REFLECTION
37463751
var description = "\\\(String(describing: Self.rootType))"
37473752
return withBuffer {
37483753
var buffer = $0
@@ -3755,7 +3760,6 @@ extension AnyKeyPath: CustomDebugStringConvertible {
37553760
let hasEnded = optNextType == nil
37563761
let nextType = optNextType ?? Self.valueType
37573762
func name(for offset: Int) -> String {
3758-
#if SWIFT_ENABLE_REFLECTION
37593763
let count = _getRecursiveChildCount(valueType)
37603764
for i in 0..<count {
37613765
if _getChildOffset(
@@ -3774,7 +3778,6 @@ extension AnyKeyPath: CustomDebugStringConvertible {
37743778
return String(cString: field.name)
37753779
}
37763780
}
3777-
#endif
37783781
return "<offset \(offset) (\(nextType))>"
37793782
}
37803783
func name(
@@ -3794,15 +3797,16 @@ extension AnyKeyPath: CustomDebugStringConvertible {
37943797
}
37953798
return _openExistential(base, do: project)
37963799
}
3797-
func addChain() {
3800+
switch rawComponent.value {
3801+
case .optionalForce, .optionalWrap, .optionalChain:
3802+
break
3803+
default:
37983804
description.append(".")
37993805
}
38003806
switch rawComponent.value {
38013807
case .class(let offset):
3802-
addChain()
38033808
description.append(name(for: offset))
38043809
case .get(_, let accessors, _):
3805-
addChain()
38063810
description.append(
38073811
name(
38083812
for: accessors,
@@ -3811,7 +3815,6 @@ extension AnyKeyPath: CustomDebugStringConvertible {
38113815
)
38123816
)
38133817
case .mutatingGetSet(_, let accessors, _):
3814-
addChain()
38153818
description.append(
38163819
name(
38173820
for: accessors,
@@ -3820,7 +3823,6 @@ extension AnyKeyPath: CustomDebugStringConvertible {
38203823
)
38213824
)
38223825
case .nonmutatingGetSet(_, let accessors, _):
3823-
addChain()
38243826
description.append(
38253827
name(
38263828
for: accessors,
@@ -3833,7 +3835,6 @@ extension AnyKeyPath: CustomDebugStringConvertible {
38333835
case .optionalForce:
38343836
description.append("!")
38353837
case .struct(let offset):
3836-
addChain()
38373838
description.append(name(for: offset))
38383839
}
38393840
if hasEnded {
@@ -3843,6 +3844,9 @@ extension AnyKeyPath: CustomDebugStringConvertible {
38433844
}
38443845
return description
38453846
}
3847+
#else
3848+
return noReflectionMetadataErrorMessage
3849+
#endif
38463850
}
38473851

38483852
}

stdlib/public/core/OutputStream.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ internal func _print_unlocked<T, TargetStream: TextOutputStream>(
441441
let mirror = Mirror(reflecting: value)
442442
_adHocPrint_unlocked(value, mirror, &target, isDebugPrint: false)
443443
#else
444-
target.write("(value cannot be printed without reflection)")
444+
target.write(noReflectionMetadataErrorMessage)
445445
#endif
446446
}
447447

@@ -473,7 +473,7 @@ public func _debugPrint_unlocked<T, TargetStream: TextOutputStream>(
473473
let mirror = Mirror(reflecting: value)
474474
_adHocPrint_unlocked(value, mirror, &target, isDebugPrint: true)
475475
#else
476-
target.write("(value cannot be printed without reflection)")
476+
target.write()
477477
#endif
478478
}
479479

@@ -638,3 +638,5 @@ internal struct _TeeStream<L: TextOutputStream, R: TextOutputStream>
638638
internal mutating func _lock() { left._lock(); right._lock() }
639639
internal mutating func _unlock() { right._unlock(); left._unlock() }
640640
}
641+
642+
internal let noReflectionMetadataErrorMessage = "(value cannot be printed without reflection)"

stdlib/public/runtime/ReflectionMirror.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/Demangling/Demangle.h"
2424
#include "swift/Runtime/Debug.h"
2525
#include "swift/Runtime/Portability.h"
26+
#include "ImageInspection.h"
2627
#include "Private.h"
2728
#include "WeakReference.h"
2829
#include "../SwiftShims/Reflection.h"
@@ -1126,11 +1127,7 @@ id swift_reflectionMirror_quickLookObject(OpaqueValue *value, const Metadata *T)
11261127
}
11271128
#endif
11281129

1129-
#endif // SWIFT_ENABLE_REFLECTION
1130-
1131-
#include "ImageInspection.h"
1132-
1133-
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API
1130+
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERNAL
11341131
const char *swift_keypath_dladdr(void *address) {
11351132
SymbolInfo info;
11361133
if (lookupSymbol(address, &info) == 0) {
@@ -1140,7 +1137,7 @@ const char *swift_keypath_dladdr(void *address) {
11401137
}
11411138
}
11421139

1143-
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API
1140+
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERNAL
11441141
const char *swift_keypathSourceString(char *name) {
11451142
size_t length = strlen(name);
11461143
std::string mangledName = keypathSourceString(name, length);
@@ -1152,3 +1149,5 @@ const char *swift_keypathSourceString(char *name) {
11521149
return rtnValue;
11531150
}
11541151
}
1152+
1153+
#endif // SWIFT_ENABLE_REFLECTION

0 commit comments

Comments
 (0)