Skip to content

Commit 85d59d2

Browse files
don't use Clang modules in the "only re-export public symbols" check (swiftlang#66610)
rdar://110399757
1 parent e69c301 commit 85d59d2

File tree

4 files changed

+48
-10
lines changed

4 files changed

+48
-10
lines changed

lib/SymbolGraphGen/SymbolGraph.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,10 @@ bool SymbolGraph::isImplicitlyPrivate(const Decl *D,
718718

719719
// Symbols from exported-imported modules should only be included if they
720720
// were originally public.
721-
if (Walker.isFromExportedImportedModule(D) &&
721+
// We force compiler-equality here to ensure that the presence of an underlying
722+
// Clang module does not prevent internal Swift symbols from being emitted when
723+
// MinimumAccessLevel is set to `internal` or below.
724+
if (Walker.isFromExportedImportedModule(D, /*countUnderlyingClangModule*/false) &&
722725
VD->getFormalAccess() < AccessLevel::Public) {
723726
return true;
724727
}

lib/SymbolGraphGen/SymbolGraphASTWalker.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,17 @@ namespace {
2828
///
2929
/// This does a by-name comparison to consider a module's underlying Clang module to be equivalent
3030
/// to the wrapping module of the same name.
31-
bool areModulesEqual(const ModuleDecl *lhs, const ModuleDecl *rhs) {
32-
return lhs->getNameStr() == rhs->getNameStr();
31+
///
32+
/// If the `isClangEqual` argument is set to `false`, the modules must also be from the same
33+
/// compiler, i.e. a Swift module and its underlying Clang module would be considered not equal.
34+
bool areModulesEqual(const ModuleDecl *lhs, const ModuleDecl *rhs, bool isClangEqual = true) {
35+
if (lhs->getNameStr() != rhs->getNameStr())
36+
return false;
37+
38+
if (!isClangEqual && (lhs->isNonSwiftModule() != rhs->isNonSwiftModule()))
39+
return false;
40+
41+
return true;
3342
}
3443

3544
} // anonymous namespace
@@ -303,9 +312,9 @@ bool SymbolGraphASTWalker::isConsideredExportedImported(const Decl *D) const {
303312
return false;
304313
}
305314

306-
bool SymbolGraphASTWalker::isFromExportedImportedModule(const Decl* D) const {
315+
bool SymbolGraphASTWalker::isFromExportedImportedModule(const Decl* D, bool countUnderlyingClangModule) const {
307316
auto *M = D->getModuleContext();
308-
return isQualifiedExportedImport(D) || isExportedImportedModule(M);
317+
return isQualifiedExportedImport(D) || isExportedImportedModule(M, countUnderlyingClangModule);
309318
}
310319

311320
bool SymbolGraphASTWalker::isQualifiedExportedImport(const Decl *D) const {
@@ -314,9 +323,9 @@ bool SymbolGraphASTWalker::isQualifiedExportedImport(const Decl *D) const {
314323
});
315324
}
316325

317-
bool SymbolGraphASTWalker::isExportedImportedModule(const ModuleDecl *M) const {
318-
return llvm::any_of(ExportedImportedModules, [&M](const auto *MD) {
319-
return areModulesEqual(M, MD->getModuleContext());
326+
bool SymbolGraphASTWalker::isExportedImportedModule(const ModuleDecl *M, bool countUnderlyingClangModule) const {
327+
return llvm::any_of(ExportedImportedModules, [&M, countUnderlyingClangModule](const auto *MD) {
328+
return areModulesEqual(M, MD->getModuleContext(), /*isClangEqual*/countUnderlyingClangModule);
320329
});
321330
}
322331

lib/SymbolGraphGen/SymbolGraphASTWalker.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,19 @@ struct SymbolGraphASTWalker : public SourceEntityWalker {
103103
virtual bool isConsideredExportedImported(const Decl *D) const;
104104

105105
/// Returns whether the given declaration comes from an `@_exported import` module.
106-
virtual bool isFromExportedImportedModule(const Decl *D) const;
106+
///
107+
/// If `countUnderlyingClangModule` is `false`, decls from Clang modules will not be considered
108+
/// re-exported unless the Clang module was itself directly re-exported.
109+
virtual bool isFromExportedImportedModule(const Decl *D, bool countUnderlyingClangModule = true) const;
107110

108111
/// Returns whether the given declaration was imported via an `@_exported import <type>` declaration.
109112
virtual bool isQualifiedExportedImport(const Decl *D) const;
110113

111114
/// Returns whether the given module is an `@_exported import` module.
112-
virtual bool isExportedImportedModule(const ModuleDecl *M) const;
115+
///
116+
/// If `countUnderlyingClangModule` is `false`, Clang modules will not be considered re-exported
117+
/// unless the Clang module itself was directly re-exported.
118+
virtual bool isExportedImportedModule(const ModuleDecl *M, bool countUnderlyingClangModule = true) const;
113119

114120
/// Returns whether the given module is the main module, or is an `@_exported import` module.
115121
virtual bool isOurModule(const ModuleDecl *M) const;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: cp -r %S/Inputs/EmitWhileBuilding/EmitWhileBuilding.framework %t
3+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -enable-objc-interop -emit-module-path %t/EmitWhileBuilding.framework/Modules/EmitWhileBuilding.swiftmodule/%target-swiftmodule-name -import-underlying-module -F %t -module-name EmitWhileBuilding -disable-objc-attr-requires-foundation-module %s %S/Inputs/EmitWhileBuilding/Extra.swift -emit-symbol-graph -emit-symbol-graph-dir %t -symbol-graph-minimum-access-level internal
4+
// RUN: %FileCheck %s --input-file %t/EmitWhileBuilding.symbols.json
5+
// RUN: %{python} -c 'import os.path; import sys; sys.exit(1 if os.path.exists(sys.argv[1]) else 0)' %t/[email protected]
6+
7+
// RUN: %target-swift-symbolgraph-extract -sdk %clang-importer-sdk -module-name EmitWhileBuilding -F %t -output-dir %t -pretty-print -v -minimum-access-level internal
8+
// RUN: %FileCheck %s --input-file %t/EmitWhileBuilding.symbols.json
9+
// RUN: %{python} -c 'import os.path; import sys; sys.exit(1 if os.path.exists(sys.argv[1]) else 0)' %t/[email protected]
10+
11+
// REQUIRES: objc_interop
12+
13+
// Ensure that having an underlying Clang module does not override the
14+
// `-symbol-graph-minimum-access-level` flag (rdar://110399757)
15+
16+
// CHECK: "s:17EmitWhileBuilding9innerFuncSSyF"
17+
18+
internal func innerFunc() -> String { "sup" }
19+
20+
public func someFunc() -> String { innerFunc() }

0 commit comments

Comments
 (0)