Skip to content

Commit

Permalink
SwiftShims: don't redeclare libc functions
Browse files Browse the repository at this point in the history
Clang importer thinks that SwiftShims is the primary module where they
live, and this confuses code completion.

rdar://22488333

Swift SVN r32218
  • Loading branch information
gribozavr committed Sep 25, 2015
1 parent 787b276 commit 153d95e
Show file tree
Hide file tree
Showing 20 changed files with 161 additions and 121 deletions.
3 changes: 0 additions & 3 deletions stdlib/private/StdlibUnittest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ if(SWIFT_HOST_VARIANT MATCHES "${SWIFT_DARWIN_VARIANTS}")
Foundation)
endif()
if(SWIFT_HOST_VARIANT STREQUAL "linux")
find_package(BSD REQUIRED)
list(APPEND swift_stdlib_unittest_private_link_libraries
${BSD_LIBRARIES})
list(APPEND swift_stdlib_unittest_module_depends
Glibc)
endif()
Expand Down
6 changes: 0 additions & 6 deletions stdlib/private/SwiftPrivate/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
set(swift_private_private_link_libraries)
if(SWIFT_HOST_VARIANT STREQUAL "linux")
# Needed for arc4random
find_package(BSD REQUIRED)
list(APPEND swift_private_private_link_libraries
${BSD_LIBRARIES})
endif()

add_swift_library(swiftSwiftPrivate SHARED IS_STDLIB
# This file should be listed the first. Module name is inferred from the
Expand Down
8 changes: 4 additions & 4 deletions stdlib/private/SwiftPrivate/IO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public struct _FDInputStream {
let fd = self.fd
let addr = _buffer.baseAddress + self._bufferUsed
let size = bufferFree
return SwiftShims.read(fd, addr, size)
return _swift_stdlib_read(fd, addr, size)
}
if readResult == 0 {
isEOF = true
Expand All @@ -73,7 +73,7 @@ public struct _FDInputStream {
if isClosed {
return
}
let result = SwiftShims.close(fd)
let result = _swift_stdlib_close(fd)
if result < 0 {
fatalError("close() returned an error")
}
Expand Down Expand Up @@ -106,7 +106,7 @@ public struct _FDOutputStream : OutputStreamType {
var writtenBytes = 0
let bufferSize = utf8.count - 1
while writtenBytes != bufferSize {
let result = SwiftShims.write(
let result = _swift_stdlib_write(
self.fd, UnsafePointer(utf8.baseAddress + Int(writtenBytes)),
bufferSize - writtenBytes)
if result < 0 {
Expand All @@ -121,7 +121,7 @@ public struct _FDOutputStream : OutputStreamType {
if isClosed {
return
}
let result = SwiftShims.close(fd)
let result = _swift_stdlib_close(fd)
if result < 0 {
fatalError("close() returned an error")
}
Expand Down
8 changes: 5 additions & 3 deletions stdlib/private/SwiftPrivate/PRNG.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@ import SwiftShims

@inline(never) @_semantics("stdlib_binary_only") // Hide the libbsd dependency
public func rand32() -> UInt32 {
return arc4random()
return _swift_stdlib_arc4random()
}

@inline(never) @_semantics("stdlib_binary_only") // Hide the libbsd dependency
public func rand32(exclusiveUpperBound limit: UInt32) -> UInt32 {
return arc4random_uniform(limit)
return _swift_stdlib_arc4random_uniform(limit)
}

@inline(never) @_semantics("stdlib_binary_only") // Hide the libbsd dependency
public func rand64() -> UInt64 {
return (UInt64(arc4random()) << 32) | UInt64(arc4random())
return
(UInt64(_swift_stdlib_arc4random()) << 32) |
UInt64(_swift_stdlib_arc4random())
}

public func randInt() -> Int {
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/SwiftShims/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
set(sources
CoreFoundationShims.h
DarwinShims.h
FoundationShims.h
GlobalObjects.h
HeapObject.h
LibcShims.h
RefCount.h
RuntimeShims.h
RuntimeStubs.h
Expand Down
48 changes: 0 additions & 48 deletions stdlib/public/SwiftShims/DarwinShims.h

This file was deleted.

60 changes: 60 additions & 0 deletions stdlib/public/SwiftShims/LibcShims.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//===--- LibcShims.h - Access to POSIX for Swift's core stdlib -----------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Using the Darwin (or Glibc) module in the core stdlib would create a
// circular dependency, so instead we import these declarations as part of
// SwiftShims.
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_STDLIB_SHIMS_LIBCSHIMS_H
#define SWIFT_STDLIB_SHIMS_LIBCSHIMS_H

#include "SwiftStdint.h"
#include "SwiftStddef.h"

#ifdef __cplusplus
namespace swift { extern "C" {
#endif

// This declaration is not universally correct. We verify its correctness for
// the current platform in the runtime code.
typedef long int __swift_ssize_t;

// General utilities <stdlib.h>
// Memory management functions
void _swift_stdlib_free(void *ptr);

// Input/output <stdio.h>
int _swift_stdlib_putchar(int c);

// String handling <string.h>
__swift_size_t _swift_stdlib_strlen(const char *s);
int _swift_stdlib_memcmp(const void *s1, const void *s2, __swift_size_t n);

// <unistd.h>
__swift_ssize_t _swift_stdlib_read(int fd, void *buf, __swift_size_t nbyte);
__swift_ssize_t _swift_stdlib_write(int fd, const void *buf,
__swift_size_t nbyte);
int _swift_stdlib_close(int fd);

// Non-standard extensions
__swift_size_t _swift_stdlib_malloc_size(const void *ptr);
__swift_uint32_t _swift_stdlib_arc4random(void);
__swift_uint32_t _swift_stdlib_arc4random_uniform(__swift_uint32_t upper_bound);

#ifdef __cplusplus
}} // extern "C", namespace swift
#endif

#endif // SWIFT_STDLIB_SHIMS_LIBCSHIMS_H

2 changes: 1 addition & 1 deletion stdlib/public/SwiftShims/RuntimeStubs.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#ifndef SWIFT_STDLIB_SHIMS_RUNTIMESTUBS_H_
#define SWIFT_STDLIB_SHIMS_RUNTIMESTUBS_H_

#include "DarwinShims.h"
#include "LibcShims.h"

__swift_ssize_t swift_stdlib_readLine_stdin(char **LinePtr);

Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/SwiftShims/module.map
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module SwiftShims {
header "CoreFoundationShims.h"
header "DarwinShims.h"
header "FoundationShims.h"
header "GlobalObjects.h"
header "HeapObject.h"
header "LibcShims.h"
header "RefCount.h"
header "RuntimeShims.h"
header "RuntimeStubs.h"
Expand Down
4 changes: 3 additions & 1 deletion stdlib/public/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,10 @@ else()
# -Wl,--whole-archive swiftRuntime -Wl,--no-whole-archive)
list(APPEND swift_core_private_link_libraries swiftRuntime)
find_package(ICU REQUIRED COMPONENTS uc i18n)
find_package(BSD REQUIRED)
list(APPEND swift_core_private_link_libraries
${ICU_UC_LIBRARY} ${ICU_I18N_LIBRARY})
${ICU_UC_LIBRARY} ${ICU_I18N_LIBRARY}
${BSD_LIBRARIES})
endif()

option(SWIFT_CHECK_ESSENTIAL_STDLIB
Expand Down
8 changes: 4 additions & 4 deletions stdlib/public/core/CString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// String interop with C
//===----------------------------------------------------------------------===//

import SwiftShims // for strlen, strcpy, strcmp
import SwiftShims

extension String {
/// Creates a new `String` by copying the nul-terminated UTF-8 data
Expand All @@ -25,7 +25,7 @@ extension String {
if cs._isNull {
return .None
}
let len = Int(strlen(cs))
let len = Int(_swift_stdlib_strlen(cs))
return String._fromCodeUnitSequence(UTF8.self,
input: UnsafeBufferPointer(start: UnsafeMutablePointer(cs), count: len))
}
Expand All @@ -43,7 +43,7 @@ extension String {
if cs._isNull {
return (.None, hadError: false)
}
let len = Int(strlen(cs))
let len = Int(_swift_stdlib_strlen(cs))
let (result, hadError) = String._fromCodeUnitSequenceWithRepair(UTF8.self,
input: UnsafeBufferPointer(start: UnsafeMutablePointer(cs), count: len))
return (result, hadError: hadError)
Expand All @@ -58,7 +58,7 @@ public func _persistCString(s: UnsafePointer<CChar>) -> [CChar]? {
if s == nil {
return .None
}
let length = Int(strlen(s))
let length = Int(_swift_stdlib_strlen(s))
var result = [CChar](count: length + 1, repeatedValue: 0)
for var i = 0; i < length; ++i {
// FIXME: this will not compile on platforms where 'CChar' is unsigned.
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/HeapBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ struct _HeapBuffer<Value, Element> : Equatable {

@warn_unused_result
func _allocatedSize() -> Int {
return swift_malloc_size(_address)
return _swift_stdlib_malloc_size(_address)
}

@warn_unused_result
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/InputStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public func readLine(stripNewline stripNewline: Bool = true) -> String? {
input: UnsafeMutableBufferPointer(
start: UnsafeMutablePointer<UTF8.CodeUnit>(linePtr),
count: readBytes)).0
free(linePtr)
_swift_stdlib_free(linePtr)
return result
}

2 changes: 1 addition & 1 deletion stdlib/public/core/ManagedBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ public struct ManagedBufferPointer<Value, Element> : Equatable {

/// The actual number of bytes allocated for this object.
internal var _allocatedByteCount: Int {
return swift_malloc_size(_address)
return _swift_stdlib_malloc_size(_address)
}

/// The address of this instance in a convenient pointer-to-bytes form
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/core/OutputStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//
//===----------------------------------------------------------------------===//

import SwiftShims // for putchar
import SwiftShims

//===----------------------------------------------------------------------===//
// Input/Output interfaces
Expand Down Expand Up @@ -246,7 +246,7 @@ internal struct _Stdout : OutputStreamType {
// It is important that we use stdio routines in order to correctly
// interoperate with stdio buffering.
for c in string.utf8 {
putchar(Int32(c))
_swift_stdlib_putchar(Int32(c))
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/core/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public func ==(lhs: String, rhs: String) -> Bool {
if lhs._core.count != rhs._core.count {
return false
}
return memcmp(
return _swift_stdlib_memcmp(
lhs._core.startASCII, rhs._core.startASCII,
rhs._core.count) == 0
}
Expand All @@ -348,7 +348,7 @@ extension String {
@warn_unused_result
public // @testable
func _compareASCII(rhs: String) -> Int {
var compare = Int(memcmp(
var compare = Int(_swift_stdlib_memcmp(
self._core.startASCII, rhs._core.startASCII,
min(self._core.count, rhs._core.count)))
if compare == 0 {
Expand Down
10 changes: 5 additions & 5 deletions stdlib/public/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,19 @@ append_if(CXX_SUPPORTS_GLOBAL_CONSTRUCTORS_WARNING "-Wglobal-constructors" swift
add_swift_library(swiftRuntime IS_STDLIB IS_STDLIB_CORE
Casting.cpp
Demangle.cpp
Enum.cpp
ErrorObject.cpp
Errors.cpp
GlobalObjects.cpp
Heap.cpp
HeapObject.cpp
KnownMetadata.cpp
LibcShims.cpp
Metadata.cpp
Once.cpp
Reflection.cpp
ShimsChecker.cpp
Stubs.cpp
SwiftObject.cpp
Enum.cpp
Once.cpp
Heap.cpp
Errors.cpp
UnicodeExtendedGraphemeClusters.cpp.gyb
${swift_runtime_objc_sources}
${swift_runtime_dtrace_sources}
Expand Down
Loading

0 comments on commit 153d95e

Please sign in to comment.