Skip to content

Commit 699e057

Browse files
committed
TBDGen: avoid emitting symbols with OBJC_CLASS and OBJC_METACLASS prefixes
rdar://60057489
1 parent 3f69d6b commit 699e057

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/TBDGen/TBDGen.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,28 @@ using namespace llvm::yaml;
5656
using StringSet = llvm::StringSet<>;
5757
using SymbolKind = llvm::MachO::SymbolKind;
5858

59+
static constexpr StringLiteral ObjC2ClassNamePrefix = "_OBJC_CLASS_$_";
60+
static constexpr StringLiteral ObjC2MetaClassNamePrefix = "_OBJC_METACLASS_$_";
61+
5962
static bool isGlobalOrStaticVar(VarDecl *VD) {
6063
return VD->isStatic() || VD->getDeclContext()->isModuleScopeContext();
6164
}
6265

66+
// If a symbol is implied, we don't need to emit it explictly into the tbd file.
67+
// e.g. When a symbol is in the `objc-classes` section in a tbd file, a additional
68+
// symbol with `_OBJC_CLASS_$_` is implied.
69+
static bool isSymbolImplied(StringRef name) {
70+
return name.startswith(ObjC2ClassNamePrefix) ||
71+
name.startswith(ObjC2MetaClassNamePrefix);
72+
}
73+
6374
void TBDGenVisitor::addSymbolInternal(StringRef name,
6475
llvm::MachO::SymbolKind kind,
6576
bool isLinkerDirective) {
6677
if (!isLinkerDirective && Opts.LinkerDirectivesOnly)
6778
return;
68-
Symbols.addSymbol(kind, name, Targets);
79+
if (!isSymbolImplied(name))
80+
Symbols.addSymbol(kind, name, Targets);
6981
if (StringSymbols && kind == SymbolKind::GlobalSymbol) {
7082
auto isNewValue = StringSymbols->insert(name).second;
7183
(void)isNewValue;

test/TBD/implied_objc_symbols.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// REQUIRES: VENDOR=apple
2+
// RUN: %empty-directory(%t)
3+
4+
// RUN: echo "import Foundation" > %t/main.swift
5+
// RUN: echo "@objc public class ObjCOnly {}" >> %t/main.swift
6+
// RUN: %target-swift-frontend -emit-ir -o/dev/null -parse-as-library -module-name test -import-objc-header %S/Inputs/objc_class_header.h -validate-tbd-against-ir=missing %t/main.swift -disable-objc-attr-requires-foundation-module -emit-tbd -emit-tbd-path %t/main.tbd
7+
8+
// RUN: %FileCheck %s < %t/main.tbd
9+
10+
// CHECK: __TtC4test8ObjCOnly
11+
// CHECK-NOT: _OBJC_CLASS_
12+
// CHECK-NOT: _OBJC_METACLASS_

0 commit comments

Comments
 (0)