Skip to content

[SymbolGraphGen] consider modules not equal if they're not from the same compiler #58421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions lib/SymbolGraphGen/SymbolGraphASTWalker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@
using namespace swift;
using namespace symbolgraphgen;

namespace {

/// Compare the two \c ModuleDecl instances to see whether they are the same.
///
/// Pass \c true to the \c ignoreUnderlying argument to consider two modules the same even if
/// one is a Swift module and the other a non-Swift module. This allows a Swift module and its
/// underlying Clang module to compare as equal.
bool areModulesEqual(const ModuleDecl *lhs, const ModuleDecl *rhs, bool ignoreUnderlying = false) {
return lhs->getNameStr() == rhs->getNameStr()
&& (ignoreUnderlying || lhs->isNonSwiftModule() == rhs->isNonSwiftModule());
}

} // anonymous namespace

SymbolGraphASTWalker::SymbolGraphASTWalker(ModuleDecl &M,
const SmallPtrSet<ModuleDecl *, 4> ExportedImportedModules,
const SymbolGraphOptions &Options)
Expand All @@ -45,10 +59,10 @@ SymbolGraph *SymbolGraphASTWalker::getModuleSymbolGraph(const Decl *D) {
}
}

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

return llvm::any_of(ExportedImportedModules, [&M](const auto *MD) {
return M->getNameStr().equals(MD->getModuleContext()->getNameStr());
return areModulesEqual(M, MD->getModuleContext());
});
}
23 changes: 23 additions & 0 deletions test/SymbolGraph/ClangImporter/ForeignExtensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: %empty-directory(%t)
// RUN: cp -r %S/Inputs/EmitWhileBuilding/EmitWhileBuilding.framework %t
// 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also have a test that checks the behavior when using swift-symbolgraph-extract, since the behaviors regarding @_export import are different?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense! I'll write up a test and push it today.

// RUN: %FileCheck %s --input-file %t/EmitWhileBuilding.symbols.json --check-prefix BASE
// RUN: %FileCheck %s --input-file %t/[email protected] --check-prefix EXTENSION

// RUN: %target-swift-symbolgraph-extract -sdk %clang-importer-sdk -module-name EmitWhileBuilding -F %t -output-dir %t -pretty-print -v
// RUN: %FileCheck %s --input-file %t/EmitWhileBuilding.symbols.json --check-prefix BASE
// RUN: %FileCheck %s --input-file %t/[email protected] --check-prefix EXTENSION

// REQUIRES: objc_interop

// ensure that the symbol `String.Foo.bar` does not appear in the base module's symbol graph

// BASE-NOT: "s:SS17EmitWhileBuildingE3FooO3baryA2CmF",
// EXTENSION: "s:SS17EmitWhileBuildingE3FooO3baryA2CmF",

public extension String {
enum Foo {
case bar
}
}