Skip to content

Commit 663a6fb

Browse files
authored
Introduce SWIFT_STDLIB_HAS_TYPE_PRINTING flag to remove the ability to print types at runtime (to save codesize) (#40649)
1 parent 4405620 commit 663a6fb

File tree

11 files changed

+43
-3
lines changed

11 files changed

+43
-3
lines changed

lib/Demangling/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ add_swift_host_library(swiftDemangling STATIC
1010
Remangler.cpp)
1111
target_compile_definitions(swiftDemangling PRIVATE
1212
LLVM_DISABLE_ABI_BREAKING_CHECKS_ENFORCING=1
13-
SWIFT_SUPPORT_OLD_MANGLING=1)
13+
SWIFT_SUPPORT_OLD_MANGLING=1
14+
SWIFT_STDLIB_HAS_TYPE_PRINTING=1)
1415

1516
# NOTE: Runtime libraries that depend on swiftDemangling should define
1617
# SWIFT_INLINE_NAMESPACE to specify the identifier that will be used for an

lib/Demangling/NodePrinter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
//
1515
//===----------------------------------------------------------------------===//
1616

17+
#if SWIFT_STDLIB_HAS_TYPE_PRINTING
18+
1719
#include "swift/Basic/STLExtras.h"
1820
#include "swift/Demangling/Demangle.h"
1921
#include "swift/AST/Ownership.h"
@@ -3093,3 +3095,5 @@ std::string Demangle::nodeToString(NodePointer root,
30933095

30943096
return NodePrinter(options).printRoot(root);
30953097
}
3098+
3099+
#endif

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,10 @@ function(_add_target_variant_c_compile_flags)
364364
list(APPEND result "-DSWIFT_STDLIB_SHORT_MANGLING_LOOKUPS")
365365
endif()
366366

367+
if(SWIFT_STDLIB_HAS_TYPE_PRINTING)
368+
list(APPEND result "-DSWIFT_STDLIB_HAS_TYPE_PRINTING")
369+
endif()
370+
367371
if(SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING)
368372
list(APPEND result "-DSWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING")
369373
endif()

stdlib/cmake/modules/StdlibOptions.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ option(SWIFT_STDLIB_SHORT_MANGLING_LOOKUPS
88
"Build stdlib with fast-path context descriptor lookups based on well-known short manglings."
99
TRUE)
1010

11+
option(SWIFT_STDLIB_HAS_TYPE_PRINTING
12+
"Build stdlib with support for printing user-friendly type name as strings at runtime"
13+
TRUE)
14+
1115
option(SWIFT_STDLIB_BUILD_PRIVATE
1216
"Build private part of the Standard Library."
1317
TRUE)

stdlib/public/CMakeLists.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,21 @@ if(SWIFT_BUILD_STDLIB OR SWIFT_BUILD_REMOTE_MIRROR)
7979
"${SWIFT_SOURCE_DIR}/lib/Demangling/Remangler.cpp"
8080
"${SWIFT_SOURCE_DIR}/lib/Demangling/NodeDumper.cpp")
8181

82+
set(swift_demangling_cflags)
83+
8284
# The old mangling support is only needed on platforms with ObjC.
8385
if(SWIFT_STDLIB_ENABLE_OBJC_INTEROP)
8486
list(APPEND swiftDemanglingSources
8587
"${SWIFT_SOURCE_DIR}/lib/Demangling/OldDemangler.cpp"
8688
"${SWIFT_SOURCE_DIR}/lib/Demangling/OldRemangler.cpp"
8789
)
88-
set(swift_demangling_cflags -DSWIFT_SUPPORT_OLD_MANGLING=1)
90+
list(APPEND swift_demangling_cflags -DSWIFT_SUPPORT_OLD_MANGLING=1)
8991
else()
90-
set(swift_demangling_cflags -DSWIFT_SUPPORT_OLD_MANGLING=0)
92+
list(APPEND swift_demangling_cflags -DSWIFT_SUPPORT_OLD_MANGLING=0)
93+
endif()
94+
95+
if(SWIFT_STDLIB_HAS_TYPE_PRINTING)
96+
list(APPEND swift_demangling_cflags -DSWIFT_STDLIB_HAS_TYPE_PRINTING)
9197
endif()
9298

9399
add_swift_target_library(swiftDemangling OBJECT_LIBRARY

stdlib/public/runtime/Casting.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ extern "C" const void *swift_dynamicCastObjCProtocolConditional(
7676
Protocol * const *protocols);
7777
#endif
7878

79+
#if SWIFT_STDLIB_HAS_TYPE_PRINTING
80+
7981
// Build a user-comprehensible name for a type.
8082
static void _buildNameForMetadata(const Metadata *type,
8183
bool qualified,
@@ -124,6 +126,13 @@ std::string swift::nameForMetadata(const Metadata *type,
124126
return result;
125127
}
126128

129+
#else // SWIFT_STDLIB_HAS_TYPE_PRINTING
130+
131+
std::string swift::nameForMetadata(const Metadata *type, bool qualified) {
132+
return "<<< type printer not available >>>";
133+
}
134+
135+
#endif // SWIFT_STDLIB_HAS_TYPE_PRINTING
127136

128137
/// Used as part of cache key for `TypeNameCache`.
129138
enum class TypeNameKind {

test/stdlib/TypeName.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
// REQUIRES: executable_test
1010

11+
// Freestanding/minimal runtime does not support printing type names at runtime.
12+
// UNSUPPORTED: freestanding
13+
1114
import StdlibUnittest
1215

1316
let TypeNameTests = TestSuite("TypeName")

test/stdlib/TypeNameInterpolation.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
// REQUIRES: executable_test
1313

14+
// Freestanding/minimal runtime does not support printing type names at runtime.
15+
// UNSUPPORTED: freestanding
16+
1417
import StdlibUnittest
1518

1619
let TypeNameTests = TestSuite("TypeName")

utils/build-presets.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2540,6 +2540,7 @@ swift-stdlib-enable-prespecialization=0
25402540
swift-stdlib-passthrough-metadata-allocator=1
25412541
swift-stdlib-short-mangling-lookups=0
25422542
swift-stdlib-experimental-hermetic-seal-at-link=1
2543+
swift-stdlib-has-type-printing=0
25432544

25442545
[preset: stdlib_S_standalone_minimal_macho_x86_64,build]
25452546
mixin-preset=

utils/build-script-impl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ KNOWN_SETTINGS=(
224224
swift-stdlib-passthrough-metadata-allocator "0" "whether stdlib should be built without a custom implementation of MetadataAllocator, relying on malloc+free instead"
225225
swift-stdlib-short-mangling-lookups "1" "whether to build stdlib with fast-path context descriptor lookups based on well-known short manglings"
226226
swift-stdlib-experimental-hermetic-seal-at-link "0" "whether stdlib should be built with -experimental-hermetic-seal-at-link"
227+
swift-stdlib-has-type-printing "1" "whether stdlib should support printing user-friendly type name as strings at runtime"
227228
swift-disable-dead-stripping "0" "turns off Darwin-specific dead stripping for Swift host tools"
228229
common-swift-flags "" "Flags used for Swift targets other than the stdlib, like the corelibs"
229230
swift-enable-experimental-string-processing "1" "whether to build experimental string processing feature"
@@ -2013,6 +2014,7 @@ for host in "${ALL_HOSTS[@]}"; do
20132014
-DSWIFT_STDLIB_ENABLE_LTO:STRING="${SWIFT_STDLIB_LTO}"
20142015
-DSWIFT_STDLIB_PASSTHROUGH_METADATA_ALLOCATOR:BOOL=$(true_false "${SWIFT_STDLIB_PASSTHROUGH_METADATA_ALLOCATOR}")
20152016
-DSWIFT_STDLIB_SHORT_MANGLING_LOOKUPS:BOOL=$(true_false "${SWIFT_STDLIB_SHORT_MANGLING_LOOKUPS}")
2017+
-DSWIFT_STDLIB_HAS_TYPE_PRINTING:BOOL=$(true_false "${SWIFT_STDLIB_HAS_TYPE_PRINTING}")
20162018
-DSWIFT_STDLIB_EXPERIMENTAL_HERMETIC_SEAL_AT_LINK:BOOL=$(true_false "${SWIFT_STDLIB_EXPERIMENTAL_HERMETIC_SEAL_AT_LINK}")
20172019
-DSWIFT_NATIVE_LLVM_TOOLS_PATH:STRING="${native_llvm_tools_path}"
20182020
-DSWIFT_NATIVE_CLANG_TOOLS_PATH:STRING="${native_clang_tools_path}"

validation-test/stdlib/SetAnyHashableExtensions.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// RUN: %target-run-simple-swift
22
// REQUIRES: executable_test
33

4+
// Freestanding/minimal runtime does not support printing type names at runtime.
5+
// UNSUPPORTED: freestanding
6+
47
import StdlibUnittest
58

69
class TestHashableBase : Hashable {

0 commit comments

Comments
 (0)