Skip to content

Commit 9b75217

Browse files
Merge pull request swiftlang#30670 from ravikandhadai/oslog-test-simplification
[stdlib/private][os log] Simplify the OSLogPrototype private module and eliminate the dependency on os overlay.
2 parents 1f904ca + b7e8f76 commit 9b75217

11 files changed

+933
-1263
lines changed

stdlib/private/OSLog/CMakeLists.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
add_swift_target_library(swiftOSLogPrototype
1+
add_swift_target_library(swiftOSLogTestHelper
22
IS_SDK_OVERLAY
33
SHARED
44

5-
OSLog.swift
5+
OSLogTestHelper.swift
66
OSLogMessage.swift
77
OSLogIntegerFormatting.swift
88
OSLogStringAlignment.swift
@@ -11,10 +11,10 @@ add_swift_target_library(swiftOSLogPrototype
1111
OSLogNSObjectType.swift
1212
OSLogFloatingPointTypes.swift
1313

14-
SWIFT_MODULE_DEPENDS_IOS Darwin os ObjectiveC
15-
SWIFT_MODULE_DEPENDS_OSX Darwin os ObjectiveC
16-
SWIFT_MODULE_DEPENDS_TVOS Darwin os ObjectiveC
17-
SWIFT_MODULE_DEPENDS_WATCHOS Darwin os ObjectiveC
14+
SWIFT_MODULE_DEPENDS_IOS Darwin ObjectiveC
15+
SWIFT_MODULE_DEPENDS_OSX Darwin ObjectiveC
16+
SWIFT_MODULE_DEPENDS_TVOS Darwin ObjectiveC
17+
SWIFT_MODULE_DEPENDS_WATCHOS Darwin ObjectiveC
1818
TARGET_SDKS ALL_APPLE_PLATFORMS
1919
SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
2020
INSTALL_IN_COMPONENT never_install

stdlib/private/OSLog/OSLog.swift

Lines changed: 0 additions & 198 deletions
This file was deleted.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
//===----------------- OSLogTestHelper.swift ----------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
// This file contains test helpers for testing the compiler diagnostics and optimizations
14+
// of the new swift APIs for os log that accept string interpolations.
15+
16+
// Some functions defined in this file are marked @_optimize(none) to prevent inlining
17+
// of string internals (such as String._StringGuts) which will interfere with
18+
// constant evaluation and folding. Note that these functions will be inlined,
19+
// constant evaluated, folded and optimized in the context of a caller. TODO:
20+
// @_optimize(none) can be removed if (non-mandatory) inlining optimizations can be moved
21+
// after serialization.
22+
23+
/// A function that acts like a check for whether logging is enabled in `_osLogTestHelper`.
24+
@inline(never)
25+
@usableFromInline
26+
internal func isLoggingEnabled() -> Bool { true }
27+
28+
/// A closure that does nothing. Meant to be used as the default assertion of
29+
/// `_osLogTestHelper`.
30+
public let _noopClosure = { (x : String, y : UnsafeBufferPointer<UInt8>) in return }
31+
32+
/// A test helper that constructs a byte buffer and a format string from an
33+
/// instance of `OSLogMessage` using the same logic as the new os log APIs,
34+
/// and applies a given `assertion` to the constructed format string and
35+
/// byte buffer. This function should be used only in tests.
36+
/// - Parameters:
37+
/// - message: An instance of `OSLogMessage` created from string interpolation
38+
/// - assertion: A closure that takes a format string and a pointer to a
39+
/// byte buffer and asserts a condition.
40+
@_transparent
41+
@_optimize(none)
42+
public // @testable
43+
func _osLogTestHelper(
44+
_ message: OSLogMessage,
45+
assertion: (String, UnsafeBufferPointer<UInt8>) -> Void = _noopClosure
46+
) {
47+
// Compute static constants first so that they can be folded by
48+
// OSLogOptimization pass.
49+
let formatString = message.interpolation.formatString
50+
let preamble = message.interpolation.preamble
51+
let argumentCount = message.interpolation.argumentCount
52+
let bufferSize = message.bufferSize
53+
let argumentClosures = message.interpolation.arguments.argumentClosures
54+
let formatStringPointer = _getGlobalStringTablePointer(formatString)
55+
56+
// Code that will execute at runtime.
57+
if (!isLoggingEnabled()) {
58+
return
59+
}
60+
61+
// Allocate a byte buffer to store the arguments. The buffer could be stack
62+
// allocated as it is local to this function and also its size is a
63+
// compile-time constant.
64+
let bufferMemory = UnsafeMutablePointer<UInt8>.allocate(capacity: bufferSize)
65+
// Array of references to auxiliary storage created during serialization of
66+
// strings. This array can be stack allocated.
67+
var stringStorageObjects: [AnyObject] = []
68+
69+
var currentBufferPosition = bufferMemory
70+
serialize(preamble, at: &currentBufferPosition)
71+
serialize(argumentCount, at: &currentBufferPosition)
72+
argumentClosures.forEach { $0(&currentBufferPosition, &stringStorageObjects) }
73+
74+
_os_log_impl_test(
75+
assertion,
76+
formatString,
77+
formatStringPointer,
78+
bufferMemory,
79+
UInt32(bufferSize))
80+
81+
// The following operation extends the lifetime of argumentClosures,
82+
// stringStorageObjects, and also of the objects stored in them, till this
83+
// point. This is necessary because the assertion is passed internal pointers
84+
// to the objects/strings stored in these arrays, as in the actual os log
85+
// implementation.
86+
_fixLifetime(argumentClosures)
87+
_fixLifetime(stringStorageObjects)
88+
bufferMemory.deallocate()
89+
}
90+
91+
/// A function that pretends to be _os_log_impl.
92+
@inline(never)
93+
@usableFromInline
94+
internal func _os_log_impl_test(
95+
_ assertion: (String, UnsafeBufferPointer<UInt8>) -> Void,
96+
_ formatString: String,
97+
_ formatStringPointer: UnsafePointer<CChar>,
98+
_ bufferMemory: UnsafeMutablePointer<UInt8>,
99+
_ bufferSize: UInt32
100+
) {
101+
assertion(
102+
formatString,
103+
UnsafeBufferPointer(
104+
start: UnsafePointer(bufferMemory),
105+
count: Int(bufferSize)))
106+
}

0 commit comments

Comments
 (0)