Skip to content

Commit 2fa7968

Browse files
authored
Turn build-swift-stdlib-static-print on for freestanding preset (#41260)
1 parent 4e3d4d2 commit 2fa7968

22 files changed

+163
-47
lines changed

stdlib/private/StdlibUnittest/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ if(SWIFT_ENABLE_REFLECTION)
1414
list(APPEND swift_stdlib_unittest_compile_flags "-DSWIFT_ENABLE_REFLECTION")
1515
endif()
1616

17+
if(SWIFT_STDLIB_STATIC_PRINT)
18+
list(APPEND swift_stdlib_unittest_compile_flags "-D" "SWIFT_STDLIB_STATIC_PRINT")
19+
endif()
20+
1721
set(swift_stdlib_unittest_link_libraries "")
1822
set(swift_stdlib_unittest_modules "")
1923
if (SWIFT_ENABLE_EXPERIMENTAL_CONCURRENCY)

stdlib/private/StdlibUnittest/StdlibCoreExtras.swift

+13
Original file line numberDiff line numberDiff line change
@@ -304,3 +304,16 @@ public func dump<T, TargetStream: TextOutputStream>(_ value: T, to target: inout
304304
}
305305

306306
#endif
307+
308+
#if SWIFT_STDLIB_STATIC_PRINT
309+
310+
public func print(_ s: Any, terminator: String = "\n") {
311+
let data = Array("\(s)\(terminator)".utf8)
312+
write(STDOUT_FILENO, data, data.count)
313+
}
314+
315+
public func print<Target>(_ s: Any, terminator: String = "\n", to output: inout Target) where Target : TextOutputStream {
316+
output.write("\(s)\(terminator)")
317+
}
318+
319+
#endif

stdlib/private/SwiftPrivateLibcExtras/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ if(SWIFT_STDLIB_HAS_ENVIRON)
33
set(swift_private_libc_extras_flags "-D" "SWIFT_STDLIB_HAS_ENVIRON")
44
endif()
55

6+
if(SWIFT_STDLIB_STATIC_PRINT)
7+
list(APPEND swift_private_libc_extras_flags "-D" "SWIFT_STDLIB_STATIC_PRINT")
8+
endif()
9+
610
set(swift_private_libc_extras_incorporate_object_libraries)
711
if(SWIFT_STDLIB_HAS_COMMANDLINE)
812
list(APPEND swift_private_libc_extras_flags "-D" "SWIFT_STDLIB_HAS_COMMANDLINE")

stdlib/private/SwiftPrivateLibcExtras/Subprocess.swift

+7
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,13 @@ var environ: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?> {
244244
}
245245
#endif
246246

247+
#if SWIFT_STDLIB_STATIC_PRINT
248+
func print(_ s: String) {
249+
let data = Array("\(s)\n".utf8)
250+
write(STDOUT_FILENO, data, data.count)
251+
}
252+
#endif
253+
247254
/// Start the same executable as a child process, redirecting its stdout and
248255
/// stderr.
249256
public func spawnChild(_ args: [String])

stdlib/public/core/ArrayShared.swift

+4
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ extension Collection {
9898
internal func _makeCollectionDescription(
9999
withTypeName type: String? = nil
100100
) -> String {
101+
#if !SWIFT_STDLIB_STATIC_PRINT
101102
var result = ""
102103
if let type = type {
103104
result += "\(type)(["
@@ -116,6 +117,9 @@ extension Collection {
116117
}
117118
result += type != nil ? "])" : "]"
118119
return result
120+
#else
121+
return "(collection printing not available)"
122+
#endif
119123
}
120124
}
121125

stdlib/public/core/Dictionary.swift

+4
Original file line numberDiff line numberDiff line change
@@ -1677,6 +1677,7 @@ extension Collection {
16771677
internal func _makeKeyValuePairDescription<K, V>(
16781678
withTypeName type: String? = nil
16791679
) -> String where Element == (key: K, value: V) {
1680+
#if !SWIFT_STDLIB_STATIC_PRINT
16801681
if self.isEmpty {
16811682
return "[:]"
16821683
}
@@ -1695,6 +1696,9 @@ extension Collection {
16951696
}
16961697
result += "]"
16971698
return result
1699+
#else
1700+
return "(collection printing not available)"
1701+
#endif
16981702
}
16991703
}
17001704

stdlib/public/core/Optional.swift

+4
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,14 @@ extension Optional: CustomDebugStringConvertible {
295295
public var debugDescription: String {
296296
switch self {
297297
case .some(let value):
298+
#if !SWIFT_STDLIB_STATIC_PRINT
298299
var result = "Optional("
299300
debugPrint(value, terminator: "", to: &result)
300301
result += ")"
301302
return result
303+
#else
304+
return "(optional printing not available)"
305+
#endif
302306
case .none:
303307
return "nil"
304308
}

stdlib/public/core/Print.swift

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#if !SWIFT_STDLIB_STATIC_PRINT
14+
1315
/// Writes the textual representations of the given items into the standard
1416
/// output.
1517
///
@@ -247,3 +249,5 @@ internal func _debugPrint<Target: TextOutputStream>(
247249
}
248250
output.write(terminator)
249251
}
252+
253+
#endif

stdlib/public/core/REPL.swift

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#if !SWIFT_STDLIB_STATIC_PRINT
14+
1315
/// Print a string as is to stdout.
1416
public // COMPILER_INTRINSIC
1517
func _replPrintLiteralString(_ text: String) {
@@ -22,3 +24,5 @@ public // COMPILER_INTRINSIC
2224
func _replDebugPrintln<T>(_ value: T) {
2325
debugPrint(value)
2426
}
27+
28+
#endif

stdlib/public/core/StaticPrint.swift

+12-4
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,9 @@ public struct ConstantVPrintFMessage :
789789
@_semantics("oslog.message.init_interpolation")
790790
@_semantics("constant_evaluable")
791791
public init(stringInterpolation: ConstantVPrintFInterpolation) {
792-
self.interpolation = stringInterpolation
792+
var s = stringInterpolation
793+
s.appendLiteral("\n")
794+
self.interpolation = s
793795
}
794796

795797
@inlinable
@@ -802,6 +804,7 @@ public struct ConstantVPrintFMessage :
802804
interpolationCount: 0
803805
)
804806
s.appendLiteral(value)
807+
s.appendLiteral("\n")
805808
self.interpolation = s
806809
}
807810
}
@@ -836,13 +839,18 @@ internal func constant_vprintf_backend(
836839
if let closure = argumentClosures.first {
837840
closure { newArg in
838841
args.append(contentsOf: newArg)
839-
print(argumentClosures.count)
840842
constant_vprintf_backend_recurse(
841843
fmt: fmt,
842844
argumentClosures: argumentClosures.dropFirst(),
843845
args: &args
844846
)
845847
}
848+
} else {
849+
constant_vprintf_backend_recurse(
850+
fmt: fmt,
851+
argumentClosures: ArraySlice(argumentClosures),
852+
args: &args
853+
)
846854
}
847855
}
848856

@@ -851,10 +859,10 @@ internal func constant_vprintf_backend(
851859
@_transparent
852860
@_alwaysEmitIntoClient
853861
@_optimize(none)
854-
public func constant_vprintf(_ message: ConstantVPrintFMessage) {
862+
public func print(_ message: ConstantVPrintFMessage) {
855863
let formatString = message.interpolation.formatString
856864
let argumentClosures = message.interpolation.arguments.argumentClosures
857-
if Bool(_builtinBooleanLiteral: Builtin.ifdef_PRINT_DISABLED()) { return }
865+
if Bool(_builtinBooleanLiteral: Builtin.ifdef_SWIFT_STDLIB_PRINT_DISABLED()) { return }
858866
let formatStringPointer = _getGlobalStringTablePointer(formatString)
859867
constant_vprintf_backend(
860868
fmt: formatStringPointer,

test/SILOptimizer/StaticVPrintFMandatoryOptTest.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Foundation
1414
// CHECK-LABEL: @${{.*}}testSimpleInterpolationyy
1515
func testSimpleInterpolation() {
1616
let x = "World"
17-
constant_vprintf("Hello \(5) \(x)")
17+
print("Hello \(5) \(x)")
1818
// Match the format string. For now we don't expect all allocations to be avoided
1919
// CHECK: string_literal utf8 "Hello %ld %s"
2020
}

test/stdlib/KeyPath.swift

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// RUN: %target-codesign %t/a.out
44
// RUN: %target-run %t/a.out
55
// REQUIRES: executable_test
6+
// UNSUPPORTED: freestanding
67

78
import StdlibUnittest
89

test/stdlib/Map.swift

+36-9
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,29 @@ print("testing...")
3939
// Test mapping a collection
4040
// CHECK-NEXT: [6, 9, 12, 15, 18, 21]
4141
let a = Array((2..<8).lazy.map { $0 * 3 })
42-
print(a)
42+
do {
43+
var output = ""
44+
var prefix = ""
45+
for x in a {
46+
output += "\(prefix)\(x)"
47+
prefix = ", "
48+
}
49+
print("[\(output)]")
50+
}
51+
4352

4453
// Test mapping a sequence
4554
let s = a.makeIterator().lazy.map { $0 / 3 }
4655
// CHECK-NEXT: <2, 3, 4, 5, 6, 7>
47-
print("<", terminator: "")
48-
var prefix = ""
49-
for x in s {
50-
print("\(prefix)\(x)", terminator: "")
51-
prefix = ", "
56+
do {
57+
var output = ""
58+
var prefix = ""
59+
for x in s {
60+
output += "\(prefix)\(x)"
61+
prefix = ", "
62+
}
63+
print("<\(output)>")
5264
}
53-
print(">")
5465

5566
//===--- Avoid creating gratuitously self-destructive sequences -----------===//
5667

@@ -94,11 +105,27 @@ struct IntRange : Sequence {
94105
// consuming it.
95106
let m1 = IntRange(start: 1, end: 5).lazy.map { $0 * 2 }
96107
// CHECK-NEXT: [2, 4, 6, 8]
97-
print(Array(m1))
108+
do {
109+
var output = ""
110+
var prefix = ""
111+
for x in m1 {
112+
output += "\(prefix)\(x)"
113+
prefix = ", "
114+
}
115+
print("[\(output)]")
116+
}
98117

99118
// A second iteration produces the same result.
100119
// CHECK-NEXT: [2, 4, 6, 8]
101-
print(Array(m1))
120+
do {
121+
var output = ""
122+
var prefix = ""
123+
for x in m1 {
124+
output += "\(prefix)\(x)"
125+
prefix = ", "
126+
}
127+
print("[\(output)]")
128+
}
102129

103130
// CHECK-NEXT: all done.
104131
print("all done.")

test/stdlib/Reduce.swift

+41-17
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,27 @@ let numberSum = numbers.reduce(0, { x, y in
2222
x + y
2323
})
2424
// CHECK-NEXT: 10
25-
print(numberSum)
25+
print("\(numberSum)")
2626

2727
let letters = "abracadabra"
2828
let letterCount = letters.reduce(into: [:]) { counts, letter in
2929
counts[letter, default: 0] += 1
3030
}
3131
// CHECK-NEXT: ["a", "b", "c", "d", "r"]
32-
print(letterCount.keys.sorted())
33-
print(letterCount["a"]!) // CHECK: 5
34-
print(letterCount["b"]!) // CHECK: 2
35-
print(letterCount["c"]!) // CHECK: 1
36-
print(letterCount["d"]!) // CHECK: 1
37-
print(letterCount["r"]!) // CHECK: 2
32+
do {
33+
var output = ""
34+
var prefix = ""
35+
for x in letterCount.keys.sorted() {
36+
output += "\(prefix)\"\(x)\""
37+
prefix = ", "
38+
}
39+
print("[\(output)]")
40+
}
41+
print("\(letterCount["a"]!)") // CHECK: 5
42+
print("\(letterCount["b"]!)") // CHECK: 2
43+
print("\(letterCount["c"]!)") // CHECK: 1
44+
print("\(letterCount["d"]!)") // CHECK: 1
45+
print("\(letterCount["r"]!)") // CHECK: 2
3846

3947

4048
// Test the two reduce methods with different levels of inference
@@ -43,36 +51,36 @@ let numbers2 = Array(2..<7)
4351
// Test reduce(_:_:)
4452
// CHECK-NEXT: 20
4553
let sum1 = numbers2.reduce(0) { (x: Int, y: Int) -> Int in x + y }
46-
print(sum1)
54+
print("\(sum1)")
4755

4856
// CHECK-NEXT: 20
4957
let sum2 = numbers2.reduce(0) { (x, y) in x + y }
50-
print(sum2)
58+
print("\(sum2)")
5159

5260
// CHECK-NEXT: 20
5361
let sum3 = numbers2.reduce(0) { $0 + $1 }
54-
print(sum3)
62+
print("\(sum3)")
5563

5664
// CHECK-NEXT: 20
5765
let sum4 = numbers2.reduce(0, +)
58-
print(sum4)
66+
print("\(sum4)")
5967

6068
// Test reduce(into:_:)
6169
// CHECK-NEXT: 20
6270
let sum5 = numbers2.reduce(into: 0) { (x: inout Int, y: Int) in x += y }
63-
print(sum5)
71+
print("\(sum5)")
6472

6573
// CHECK-NEXT: 20
6674
let sum6 = numbers2.reduce(into: 0) { x, y in x += y }
67-
print(sum6)
75+
print("\(sum6)")
6876

6977
// CHECK-NEXT: 20
7078
let sum7 = numbers2.reduce(into: 0) { $0 += $1 }
71-
print(sum7)
79+
print("\(sum7)")
7280

7381
// CHECK-NEXT: 20
7482
let sum8 = numbers2.reduce(into: 0, +=)
75-
print(sum8)
83+
print("\(sum8)")
7684

7785

7886
// Test that the initial value remains unmodified
@@ -81,9 +89,25 @@ let result = numbers2.reduce(into: original) { acc, x in
8189
acc.append(x)
8290
}
8391
// CHECK-NEXT: [0, 1]
84-
print(original)
92+
do {
93+
var output = ""
94+
var prefix = ""
95+
for x in original {
96+
output += "\(prefix)\(x)"
97+
prefix = ", "
98+
}
99+
print("[\(output)]")
100+
}
85101
// CHECK-NEXT: [0, 1, 2, 3, 4, 5, 6]
86-
print(result)
102+
do {
103+
var output = ""
104+
var prefix = ""
105+
for x in result {
106+
output += "\(prefix)\(x)"
107+
prefix = ", "
108+
}
109+
print("[\(output)]")
110+
}
87111

88112

89113
// CHECK: all done.

test/stdlib/StaticPrint.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ let PrintTests = TestSuite("StaticPrint")
1818
// CHECK: string_literal utf8 ", world"
1919
// CHECK: string_literal utf8 "hello, world 5"
2020
PrintTests.test("StringInterpolation") {
21-
constant_vprintf("")
22-
constant_vprintf("hello")
23-
constant_vprintf("\(5)")
21+
print("")
22+
print("hello")
23+
print("\(5)")
2424
let x = 5
25-
constant_vprintf("\(x)")
25+
print("\(x)")
2626
let y = ", world"
27-
constant_vprintf("\(y)")
28-
constant_vprintf("hello \(y)\(x)")
27+
print("\(y)")
28+
print("hello \(y)\(x)")
2929
}
3030

3131
runAllTests()

0 commit comments

Comments
 (0)