Skip to content

Commit c885dc6

Browse files
[SymbolGraphGen] consider modules not equal if they're not from the same compiler (#58421)
* consider modules not equal if they're not from the same compiler rdar://92263972 * add swift-symbolgraph-extract command and checks to the test
1 parent 23f39db commit c885dc6

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

lib/SymbolGraphGen/SymbolGraphASTWalker.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@
2121
using namespace swift;
2222
using namespace symbolgraphgen;
2323

24+
namespace {
25+
26+
/// Compare the two \c ModuleDecl instances to see whether they are the same.
27+
///
28+
/// Pass \c true to the \c ignoreUnderlying argument to consider two modules the same even if
29+
/// one is a Swift module and the other a non-Swift module. This allows a Swift module and its
30+
/// underlying Clang module to compare as equal.
31+
bool areModulesEqual(const ModuleDecl *lhs, const ModuleDecl *rhs, bool ignoreUnderlying = false) {
32+
return lhs->getNameStr() == rhs->getNameStr()
33+
&& (ignoreUnderlying || lhs->isNonSwiftModule() == rhs->isNonSwiftModule());
34+
}
35+
36+
} // anonymous namespace
37+
2438
SymbolGraphASTWalker::SymbolGraphASTWalker(ModuleDecl &M,
2539
const SmallPtrSet<ModuleDecl *, 4> ExportedImportedModules,
2640
const SymbolGraphOptions &Options)
@@ -45,10 +59,10 @@ SymbolGraph *SymbolGraphASTWalker::getModuleSymbolGraph(const Decl *D) {
4559
}
4660
}
4761

48-
if (this->M.getNameStr().equals(M->getNameStr())) {
62+
if (areModulesEqual(&this->M, M, true)) {
4963
return &MainGraph;
5064
} else if (MainGraph.DeclaringModule.hasValue() &&
51-
MainGraph.DeclaringModule.getValue()->getNameStr().equals(M->getNameStr())) {
65+
areModulesEqual(MainGraph.DeclaringModule.getValue(), M, true)) {
5266
// Cross-import overlay modules already appear as "extensions" of their declaring module; we
5367
// should put actual extensions of that module into the main graph
5468
return &MainGraph;
@@ -220,6 +234,6 @@ bool SymbolGraphASTWalker::isFromExportedImportedModule(const Decl* D) const {
220234
auto *M = D->getModuleContext();
221235

222236
return llvm::any_of(ExportedImportedModules, [&M](const auto *MD) {
223-
return M->getNameStr().equals(MD->getModuleContext()->getNameStr());
237+
return areModulesEqual(M, MD->getModuleContext());
224238
});
225239
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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
4+
// RUN: %FileCheck %s --input-file %t/EmitWhileBuilding.symbols.json --check-prefix BASE
5+
// RUN: %FileCheck %s --input-file %t/[email protected] --check-prefix EXTENSION
6+
7+
// RUN: %target-swift-symbolgraph-extract -sdk %clang-importer-sdk -module-name EmitWhileBuilding -F %t -output-dir %t -pretty-print -v
8+
// RUN: %FileCheck %s --input-file %t/EmitWhileBuilding.symbols.json --check-prefix BASE
9+
// RUN: %FileCheck %s --input-file %t/[email protected] --check-prefix EXTENSION
10+
11+
// REQUIRES: objc_interop
12+
13+
// ensure that the symbol `String.Foo.bar` does not appear in the base module's symbol graph
14+
15+
// BASE-NOT: "s:SS17EmitWhileBuildingE3FooO3baryA2CmF",
16+
// EXTENSION: "s:SS17EmitWhileBuildingE3FooO3baryA2CmF",
17+
18+
public extension String {
19+
enum Foo {
20+
case bar
21+
}
22+
}
23+

0 commit comments

Comments
 (0)