Skip to content

Commit 82ef9d3

Browse files
committed
[interop][SwiftToCxx] do not emit unavaialble stubs for internal/private decls
only emit them for public ones
1 parent 74e87a9 commit 82ef9d3

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

lib/PrintAsClang/DeclAndTypePrinter.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2908,6 +2908,12 @@ bool DeclAndTypePrinter::shouldInclude(const ValueDecl *VD) {
29082908
!excludeForObjCImplementation(VD);
29092909
}
29102910

2911+
bool DeclAndTypePrinter::isVisible(const ValueDecl *vd) const {
2912+
return outputLang == OutputLanguageMode::Cxx
2913+
? cxx_translation::isVisibleToCxx(vd, minRequiredAccess)
2914+
: isVisibleToObjC(vd, minRequiredAccess);
2915+
}
2916+
29112917
void DeclAndTypePrinter::print(const Decl *D) {
29122918
getImpl().print(D);
29132919
}

lib/PrintAsClang/DeclAndTypePrinter.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ class DeclAndTypePrinter {
106106
/// the options the printer was constructed with.
107107
bool shouldInclude(const ValueDecl *VD);
108108

109+
/// Returns true if \p vd is visible given the current access level and thus
110+
/// can be included in the generated header.
111+
bool isVisible(const ValueDecl *vd) const;
112+
109113
void print(const Decl *D);
110114
void print(Type ty);
111115

lib/PrintAsClang/ModuleContentsWriter.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -820,18 +820,18 @@ class ModuleWriter {
820820
emissionScope.additionalUnrepresentableDeclarations)
821821
removedVDList.push_back(removedVD);
822822

823+
// Do not report internal/private decls as unavailable.
823824
// @objc declarations are emitted in the Objective-C section, so do not
824825
// report them as unavailable. Also skip underscored decls from the standard
825826
// library. Also skip structs from the standard library, they can cause
826827
// ambiguities because of the arithmetic types that conflict with types we
827828
// already have in `swift::` namespace. Also skip `Error` protocol from
828829
// stdlib, we have experimental support for it.
829-
// FIXME: Note unrepresented type aliases too.
830830
removedVDList.erase(
831831
llvm::remove_if(
832832
removedVDList,
833-
[](const ValueDecl *vd) {
834-
return vd->isObjC() ||
833+
[&](const ValueDecl *vd) {
834+
return !printer.isVisible(vd) || vd->isObjC() ||
835835
(vd->isStdlibDecl() && !vd->getName().isSpecial() &&
836836
vd->getBaseIdentifier().str().startswith("_")) ||
837837
(vd->isStdlibDecl() && isa<StructDecl>(vd)) ||
@@ -897,6 +897,7 @@ class ModuleWriter {
897897

898898
// FIXME: Emit an unavailable stub for a function / function overload set
899899
// / variable.
900+
// FIXME: Note unrepresented type aliases too.
900901
emitStubComment();
901902
}
902903
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend %s -typecheck -module-name Functions -clang-header-expose-decls=all-public -emit-clang-header-path %t/apis.h
3+
// RUN: %FileCheck %s < %t/apis.h
4+
5+
internal func takeFloat(_ x: Float) {}
6+
7+
private struct PrivateStruct { let x: Int }
8+
9+
class InternalClass {
10+
let x: Int
11+
init() { self.x = 0 }
12+
}
13+
14+
protocol InternalProto {}
15+
16+
// CHECK: namespace Functions SWIFT_PRIVATE_ATTR SWIFT_SYMBOL_MODULE("Functions") {
17+
// CHECK-EMPTY:
18+
// CHECK-EMPTY:
19+
// CHECK-NEXT: } // namespace Functions

0 commit comments

Comments
 (0)