Skip to content

Replace let/nil-check patterns with guard let #157

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

Closed
wants to merge 10 commits into from
11 changes: 1 addition & 10 deletions lib/Markup/Markup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "swift/Markup/LineList.h"
#include "swift/Markup/Markup.h"
#include "cmark.h"
#include "node.h"

using namespace llvm;
using namespace markup;
Expand Down Expand Up @@ -60,15 +59,7 @@ StringRef getLiteralContent(MarkupContext &MC, LineList &LL, cmark_node *Node) {
// its parent.
auto Literal = cmark_node_get_literal(Node);
assert(Literal != nullptr);
size_t Length = 0;
switch (cmark_node_get_type(Node)) {
case CMARK_NODE_CODE_BLOCK:
Length = Node->as.code.literal.len;
break;
default:
Length = Node->as.literal.len;
}
return MC.allocateCopy(StringRef(Literal, Length));
return MC.allocateCopy(StringRef(Literal));
}

ParseResult<MarkupASTNode>
Expand Down
33 changes: 16 additions & 17 deletions stdlib/public/core/ArrayBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -244,32 +244,31 @@ extension _ArrayBuffer {
return _native[subRange]
}

// Look for contiguous storage in the NSArray
let nonNative = self._nonNative
let cocoa = _CocoaArrayWrapper(nonNative)
let cocoaStorageBaseAddress = cocoa.contiguousStorage(self.indices)

if cocoaStorageBaseAddress != nil {
// Look for contiguous storage in the NSArray
if let cocoaStorageBaseAddress = cocoa.contiguousStorage(self.indices) {
return _SliceBuffer(
owner: nonNative,
subscriptBaseAddress: UnsafeMutablePointer(cocoaStorageBaseAddress),
indices: subRange,
hasNativeBuffer: false)
} else {
// No contiguous storage found; we must allocate
let subRangeCount = subRange.count
let result = _ContiguousArrayBuffer<Element>(
count: subRangeCount, minimumCapacity: 0)

// Tell Cocoa to copy the objects into our storage
cocoa.buffer.getObjects(
UnsafeMutablePointer(result.firstElementAddress),
range: _SwiftNSRange(
location: subRange.startIndex,
length: subRangeCount))

return _SliceBuffer(result, shiftedToStartIndex: subRange.startIndex)
}

// No contiguous storage found; we must allocate
let subRangeCount = subRange.count
let result = _ContiguousArrayBuffer<Element>(
count: subRangeCount, minimumCapacity: 0)

// Tell Cocoa to copy the objects into our storage
cocoa.buffer.getObjects(
UnsafeMutablePointer(result.firstElementAddress),
range: _SwiftNSRange(
location: subRange.startIndex,
length: subRangeCount))

return _SliceBuffer(result, shiftedToStartIndex: subRange.startIndex)
}
set {
fatalError("not implemented")
Expand Down
23 changes: 11 additions & 12 deletions stdlib/public/core/Collection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -584,19 +584,18 @@ extension SequenceType
// A fast implementation for when you are backed by a contiguous array.
public func _initializeTo(ptr: UnsafeMutablePointer<Generator.Element>)
-> UnsafeMutablePointer<Generator.Element> {
let s = self._baseAddressIfContiguous
if s != nil {
let count = self.count
ptr.initializeFrom(s, count: count)
_fixLifetime(self._owner)
return ptr + count
} else {
var p = ptr
for x in self {
p++.initialize(x)
if let s = self._baseAddressIfContiguous {
let count = self.count
ptr.initializeFrom(s, count: count)
_fixLifetime(self._owner)
return ptr + count
} else {
var p = ptr
for x in self {
p++.initialize(x)
}
return p
}
return p
}
}
}

Expand Down
24 changes: 24 additions & 0 deletions stdlib/public/core/Misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,30 @@ func _typeName(type: Any.Type, qualified: Bool = true) -> String {
input: UnsafeBufferPointer(start: stringPtr, count: count))
}

@warn_unused_result
@_silgen_name("swift_stdlib_demangleName")
func _stdlib_demangleNameImpl(
mangledName: UnsafePointer<UInt8>,
_ mangledNameLength: UInt,
_ demangledName: UnsafeMutablePointer<String>)

// NB: This function is not used directly in the Swift codebase, but is
// exported for Xcode support. Please coordinate before changing.
@warn_unused_result
public // @testable
func _stdlib_demangleName(mangledName: String) -> String {
let mangledNameUTF8 = Array(mangledName.utf8)
return mangledNameUTF8.withUnsafeBufferPointer {
(mangledNameUTF8) in
let (_, demangledName) = _withUninitializedString {
_stdlib_demangleNameImpl(
mangledNameUTF8.baseAddress, UInt(mangledNameUTF8.endIndex),
$0)
}
return demangledName
}
}

/// Returns `floor(log(x))`. This equals to the position of the most
/// significant non-zero bit, or 63 - number-of-zeros before it.
///
Expand Down
9 changes: 4 additions & 5 deletions stdlib/public/core/Runtime.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,11 @@ public // @testable
func _stdlib_atomicLoadARCRef(
object target: UnsafeMutablePointer<AnyObject?>
) -> AnyObject? {
let result = _swift_stdlib_atomicLoadPtrImpl(
object: UnsafeMutablePointer(target))
if result != nil {
return Unmanaged<AnyObject>.fromOpaque(result).takeUnretainedValue()
guard let result = _swift_stdlib_atomicLoadPtrImpl(
object: UnsafeMutablePointer(target)) {
return nil
}
return nil
return Unmanaged<AnyObject>.fromOpaque(result).takeUnretainedValue()
Copy link
Contributor

Choose a reason for hiding this comment

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

This part LGTM.

}

% for operation in [ 'Add', 'And', 'Or', 'Xor' ]:
Expand Down
14 changes: 14 additions & 0 deletions stdlib/public/runtime/Reflection.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1264,3 +1264,17 @@ static Mirror ObjC_getMirrorForSuperclass(Class sup,
T->vw_destroy(value);
return MirrorReturn(result);
}

// NB: This function is not used directly in the Swift codebase, but is
// exported for Xcode support. Please coordinate before changing.
extern "C" void swift_stdlib_demangleName(const char *mangledName,
size_t mangledNameLength,
String *demangledName) {
auto options = Demangle::DemangleOptions();
options.DisplayDebuggerGeneratedModule = false;
auto result =
Demangle::demangleSymbolAsString(mangledName,
mangledNameLength,
options);
new (demangledName) String(result.data(), result.size());
}
10 changes: 10 additions & 0 deletions test/1_stdlib/Runtime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,16 @@ Runtime.test("typeName") {
expectEqual("protocol<>.Protocol", _typeName(a.dynamicType))
}

Runtime.test("demangleName") {
expectEqual("", _stdlib_demangleName(""))
expectEqual("abc", _stdlib_demangleName("abc"))
expectEqual("\0", _stdlib_demangleName("\0"))
expectEqual("Swift.Double", _stdlib_demangleName("_TtSd"))
expectEqual("x.a : x.Foo<x.Foo<x.Foo<Swift.Int, Swift.Int>, x.Foo<Swift.Int, Swift.Int>>, x.Foo<x.Foo<Swift.Int, Swift.Int>, x.Foo<Swift.Int, Swift.Int>>>",
_stdlib_demangleName("_Tv1x1aGCS_3FooGS0_GS0_SiSi_GS0_SiSi__GS0_GS0_SiSi_GS0_SiSi___"))
expectEqual("Foobar", _stdlib_demangleName("_TtC13__lldb_expr_46Foobar"))
}

Runtime.test("_stdlib_atomicCompareExchangeStrongPtr") {
typealias IntPtr = UnsafeMutablePointer<Int>
var origP1 = IntPtr(bitPattern: 0x10101010)
Expand Down
1 change: 1 addition & 0 deletions test/Driver/environment.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// FIXME: This is failing on some of Apple's internal CI.
// FIXME: <rdar://problem/23771412> Fix test/Driver/{environment.swift,options-interpreter.swift}
// REQUIRES: disabled

// RUN: %swift_driver -target x86_64-apple-macosx10.9 -L/foo/ -driver-use-frontend-path %S/Inputs/print-var.sh %s DYLD_LIBRARY_PATH | FileCheck %s
Expand Down
1 change: 1 addition & 0 deletions test/Driver/options-interpreter.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// FIXME: This is failing on some of Apple's internal CI.
// FIXME: <rdar://problem/23771412> Fix test/Driver/{environment.swift,options-interpreter.swift}
// REQUIRES: disabled

// RUN: not %swift_driver -deprecated-integrated-repl -emit-module 2>&1 | FileCheck -check-prefix=IMMEDIATE_NO_MODULE %s
Expand Down