Skip to content

Commit 46c4c2b

Browse files
authored
Merge pull request #30733 from allevato/rt-inline-ns
Wrap (de)mangling namespaces in an inline namespace.
2 parents 00d5839 + 5b1daa9 commit 46c4c2b

File tree

17 files changed

+90
-2
lines changed

17 files changed

+90
-2
lines changed

include/swift/AST/ASTDemangler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@
2626
#include "llvm/ADT/StringRef.h"
2727
#include "swift/AST/Types.h"
2828
#include "swift/Demangling/Demangler.h"
29+
#include "swift/Demangling/NamespaceMacros.h"
2930
#include "swift/Demangling/TypeDecoder.h"
3031

3132
namespace swift {
3233

3334
class TypeDecl;
3435

3536
namespace Demangle {
37+
SWIFT_BEGIN_INLINE_NAMESPACE
3638

3739
Type getTypeForMangling(ASTContext &ctx,
3840
llvm::StringRef mangling);
@@ -178,6 +180,7 @@ class ASTBuilder {
178180
Demangle::Node::Kind kind);
179181
};
180182

183+
SWIFT_END_INLINE_NAMESPACE
181184
} // namespace Demangle
182185

183186
} // namespace swift

include/swift/Basic/Mangler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define SWIFT_BASIC_MANGLER_H
1515

1616
#include "swift/Demangling/ManglingUtils.h"
17+
#include "swift/Demangling/NamespaceMacros.h"
1718
#include "swift/Basic/Debug.h"
1819
#include "swift/Basic/LLVM.h"
1920
#include "llvm/ADT/DenseMap.h"
@@ -24,6 +25,7 @@
2425

2526
namespace swift {
2627
namespace Mangle {
28+
SWIFT_BEGIN_INLINE_NAMESPACE
2729

2830
void printManglingStats();
2931

@@ -204,6 +206,7 @@ class Mangler {
204206
}
205207
};
206208

209+
SWIFT_END_INLINE_NAMESPACE
207210
} // end namespace Mangle
208211
} // end namespace swift
209212

include/swift/Demangling/Demangle.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@
2525
#include <cstdint>
2626
#include "llvm/ADT/StringRef.h"
2727
#include "swift/Runtime/Config.h"
28+
#include "swift/Demangling/NamespaceMacros.h"
2829

2930
namespace llvm {
3031
class raw_ostream;
3132
}
3233

3334
namespace swift {
3435
namespace Demangle {
36+
SWIFT_BEGIN_INLINE_NAMESPACE
3537

3638
enum class SymbolicReferenceKind : uint8_t;
3739

@@ -621,6 +623,7 @@ bool isFunctionAttr(Node::Kind kind);
621623
/// contain symbolic references.
622624
llvm::StringRef makeSymbolicMangledNameStringRef(const char *base);
623625

626+
SWIFT_END_INLINE_NAMESPACE
624627
} // end namespace Demangle
625628
} // end namespace swift
626629

include/swift/Demangling/Demangler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define SWIFT_DEMANGLING_DEMANGLER_H
2121

2222
#include "swift/Demangling/Demangle.h"
23+
#include "swift/Demangling/NamespaceMacros.h"
2324

2425
//#define NODE_FACTORY_DEBUGGING
2526

@@ -28,6 +29,7 @@ using llvm::StringRef;
2829

2930
namespace swift {
3031
namespace Demangle {
32+
SWIFT_BEGIN_INLINE_NAMESPACE
3133

3234
class CharVector;
3335

@@ -622,6 +624,7 @@ template <size_t Size> class StackAllocatedDemangler : public Demangler {
622624

623625
NodePointer demangleOldSymbolAsNode(StringRef MangledName,
624626
NodeFactory &Factory);
627+
SWIFT_END_INLINE_NAMESPACE
625628
} // end namespace Demangle
626629
} // end namespace swift
627630

include/swift/Demangling/ManglingUtils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
#define SWIFT_DEMANGLING_MANGLINGUTILS_H
1515

1616
#include "llvm/ADT/StringRef.h"
17+
#include "swift/Demangling/NamespaceMacros.h"
1718
#include "swift/Demangling/Punycode.h"
1819

1920
namespace swift {
2021
namespace Mangle {
22+
SWIFT_BEGIN_INLINE_NAMESPACE
2123

2224
using llvm::StringRef;
2325

@@ -311,6 +313,7 @@ class SubstitutionMerging {
311313
}
312314
};
313315

316+
SWIFT_END_INLINE_NAMESPACE
314317
} // end namespace Mangle
315318
} // end namespace swift
316319

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===--- NamespaceMacros.h - Macros for inline namespaces -------*- C++ -*-===//
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+
// Macros that conditionally define an inline namespace so that symbols used in
14+
// multiple places (such as in the compiler and in the runtime library) can be
15+
// given distinct mangled names in different contexts without affecting client
16+
// usage in source.
17+
//
18+
//===----------------------------------------------------------------------===//
19+
20+
#ifndef SWIFT_DEMANGLING_NAMESPACE_MACROS_H
21+
#define SWIFT_DEMANGLING_NAMESPACE_MACROS_H
22+
23+
#if defined(__cplusplus)
24+
25+
#if defined(SWIFT_INLINE_NAMESPACE)
26+
#define SWIFT_BEGIN_INLINE_NAMESPACE inline namespace SWIFT_INLINE_NAMESPACE {
27+
#define SWIFT_END_INLINE_NAMESPACE }
28+
#else
29+
#define SWIFT_BEGIN_INLINE_NAMESPACE
30+
#define SWIFT_END_INLINE_NAMESPACE
31+
#endif
32+
33+
#endif
34+
35+
#endif // SWIFT_DEMANGLING_NAMESPACE_MACROS_H

include/swift/Demangling/Punycode.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@
2828
#define SWIFT_DEMANGLING_PUNYCODE_H
2929

3030
#include "llvm/ADT/StringRef.h"
31+
#include "swift/Demangling/NamespaceMacros.h"
3132
#include <vector>
3233
#include <cstdint>
3334

3435
namespace swift {
3536
namespace Punycode {
37+
SWIFT_BEGIN_INLINE_NAMESPACE
3638

3739
using llvm::StringRef;
3840

@@ -58,6 +60,7 @@ bool encodePunycodeUTF8(StringRef InputUTF8, std::string &OutPunycode,
5860

5961
bool decodePunycodeUTF8(StringRef InputPunycode, std::string &OutUTF8);
6062

63+
SWIFT_END_INLINE_NAMESPACE
6164
} // end namespace Punycode
6265
} // end namespace swift
6366

include/swift/Demangling/TypeDecoder.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "swift/ABI/MetadataValues.h"
2222
#include "swift/Demangling/Demangler.h"
23+
#include "swift/Demangling/NamespaceMacros.h"
2324
#include "swift/Basic/LLVM.h"
2425
#include "swift/Runtime/Unreachable.h"
2526
#include "swift/Strings.h"
@@ -28,6 +29,7 @@
2829

2930
namespace swift {
3031
namespace Demangle {
32+
SWIFT_BEGIN_INLINE_NAMESPACE
3133

3234
enum class ImplMetatypeRepresentation {
3335
Thin,
@@ -1152,6 +1154,7 @@ decodeMangledType(BuilderType &Builder,
11521154
}
11531155

11541156

1157+
SWIFT_END_INLINE_NAMESPACE
11551158
} // end namespace Demangle
11561159
} // end namespace swift
11571160

include/swift/SILOptimizer/Utils/SpecializationMangler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414
#define SWIFT_SILOPTIMIZER_UTILS_SPECIALIZATIONMANGLER_H
1515

1616
#include "swift/Demangling/Demangler.h"
17+
#include "swift/Demangling/NamespaceMacros.h"
1718
#include "swift/Basic/NullablePtr.h"
1819
#include "swift/AST/ASTMangler.h"
1920
#include "swift/SIL/SILLinkage.h"
2021
#include "swift/SIL/SILFunction.h"
2122

2223
namespace swift {
2324
namespace Mangle {
25+
SWIFT_BEGIN_INLINE_NAMESPACE
2426

2527
enum class SpecializationKind : uint8_t {
2628
Generic,
@@ -169,6 +171,7 @@ class FunctionSignatureSpecializationMangler : public SpecializationMangler {
169171
void mangleReturnValue(ReturnValueModifierIntBase RetMod);
170172
};
171173

174+
SWIFT_END_INLINE_NAMESPACE
172175
} // end namespace Mangle
173176
} // end namespace swift
174177

lib/Demangling/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,12 @@ add_swift_host_library(swiftDemangling STATIC
1111
target_compile_definitions(swiftDemangling PRIVATE
1212
LLVM_DISABLE_ABI_BREAKING_CHECKS_ENFORCING=1)
1313

14+
# NOTE: Runtime libraries that depend on swiftDemangling should define
15+
# SWIFT_INLINE_NAMESPACE to specify the identifier that will be used for an
16+
# inline namespace that will be added around the symbols defined by this
17+
# library. This keeps the demangling symbols in those libraries distinct
18+
# from those in the compiler, which prevents ODR violations in certain
19+
# contexts; for example, on platforms that support statically linking the
20+
# Swift standard library, it allows this to happen safely when the binary
21+
# also links in compiler libraries that may not match exactly with the
22+
# runtime version.

lib/Demangling/Context.cpp

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

1717
#include "swift/Demangling/Demangler.h"
1818
#include "swift/Demangling/ManglingMacros.h"
19+
#include "swift/Demangling/NamespaceMacros.h"
1920

2021
namespace swift {
2122
namespace Demangle {
23+
SWIFT_BEGIN_INLINE_NAMESPACE
2224

2325
//////////////////////////////////
2426
// Context member functions //
@@ -248,5 +250,6 @@ std::string demangleTypeAsString(const char *MangledName,
248250
Options);
249251
}
250252

253+
SWIFT_END_INLINE_NAMESPACE
251254
} // namespace Demangle
252255
} // namespace swift

lib/Demangling/RemanglerBase.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
#define SWIFT_DEMANGLING_BASEREMANGLER_H
1919

2020
#include "swift/Demangling/Demangler.h"
21+
#include "swift/Demangling/NamespaceMacros.h"
2122
#include <unordered_map>
2223

2324
using namespace swift::Demangle;
2425
using llvm::StringRef;
2526

2627
namespace swift {
2728
namespace Demangle {
29+
SWIFT_BEGIN_INLINE_NAMESPACE
2830

2931
// An entry in the remangler's substitution map.
3032
class SubstitutionEntry {
@@ -163,6 +165,7 @@ class RemanglerBase {
163165
}
164166
};
165167

168+
SWIFT_END_INLINE_NAMESPACE
166169
} // end namespace Demangle
167170
} // end namespace swift
168171

lib/SwiftRemoteMirror/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
add_swift_host_library(swiftRemoteMirror STATIC
22
${SWIFT_SOURCE_DIR}/stdlib/public/SwiftRemoteMirror/SwiftRemoteMirror.cpp)
3+
target_link_libraries(swiftRemoteMirror PRIVATE
4+
swiftDemangling)
5+
36
if(CMAKE_SYSTEM_NAME STREQUAL Windows)
47
target_compile_definitions(swiftRemoteMirror PRIVATE _LIB)
58
endif()

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,13 @@ function(_add_swift_target_library_single target name)
884884
endif()
885885
_set_target_prefix_and_suffix("${target}" "${libkind}" "${SWIFTLIB_SINGLE_SDK}")
886886

887+
# Target libraries that include libDemangling must define the name to use for
888+
# the inline namespace to distinguish symbols from those built for the
889+
# compiler, in order to avoid possible ODR violations if both are statically
890+
# linked into the same binary.
891+
target_compile_definitions("${target}" PRIVATE
892+
SWIFT_INLINE_NAMESPACE=__runtime)
893+
887894
if("${SWIFTLIB_SINGLE_SDK}" STREQUAL "WINDOWS")
888895
swift_windows_include_for_arch(${SWIFTLIB_SINGLE_ARCHITECTURE} SWIFTLIB_INCLUDE)
889896
target_include_directories("${target}" SYSTEM PRIVATE

unittests/Reflection/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ if(("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "${SWIFT_PRIMARY_VARIANT_SDK}") AND
55
TypeRef.cpp)
66
target_include_directories(SwiftReflectionTests BEFORE PRIVATE
77
${SWIFT_SOURCE_DIR}/stdlib/include)
8+
target_compile_definitions(SwiftReflectionTests PRIVATE
9+
SWIFT_INLINE_NAMESPACE=__runtime)
810
target_link_libraries(SwiftReflectionTests
911
PRIVATE
1012
swiftReflection${SWIFT_PRIMARY_VARIANT_SUFFIX})

unittests/runtime/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ if(("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "${SWIFT_PRIMARY_VARIANT_SDK}") AND
8383
# with swiftCore_EXPORTS to permit exporting the stdlib interfaces.
8484
target_compile_definitions(SwiftRuntimeTests
8585
PRIVATE
86-
swiftCore_EXPORTS)
86+
swiftCore_EXPORTS
87+
SWIFT_INLINE_NAMESPACE=__runtime)
8788

8889
target_include_directories(SwiftRuntimeTests BEFORE PRIVATE
8990
${SWIFT_SOURCE_DIR}/stdlib/include)

unittests/runtime/LongTests/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ if(("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "${SWIFT_PRIMARY_VARIANT_SDK}") AND
4747
# interfaces.
4848
target_compile_definitions(SwiftRuntimeLongTests
4949
PRIVATE
50-
swiftCore_EXPORTS)
50+
swiftCore_EXPORTS
51+
SWIFT_INLINE_NAMESPACE=__runtime)
5152

5253
# FIXME: cross-compile for all variants.
5354
target_link_libraries(SwiftRuntimeLongTests

0 commit comments

Comments
 (0)