Skip to content

[SymbolGraphGen] distinguish between headers of the same name in different modules #82112

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 1 commit into from
Jun 9, 2025
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
17 changes: 16 additions & 1 deletion lib/SymbolGraphGen/SymbolGraphASTWalker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ using namespace symbolgraphgen;

namespace {

/// Get the fully-qualified module name of the given `ModuleDecl` as a `std::string`.
///
/// For example, if `M` is a submodule of `ParentModule` named `SubModule`,
/// this function would return `"ParentModule.SubModule"`.
std::string getFullModuleName(const ModuleDecl *M) {
if (!M) return {};

std::string S;
llvm::raw_string_ostream OS(S);

M->getReverseFullModuleName().printForward(OS);

return S;
}

/// Compare the two \c ModuleDecl instances to see whether they are the same.
///
/// This does a by-name comparison to consider a module's underlying Clang module to be equivalent
Expand All @@ -36,7 +51,7 @@ namespace {
/// If the `isClangEqual` argument is set to `false`, the modules must also be from the same
/// compiler, i.e. a Swift module and its underlying Clang module would be considered not equal.
bool areModulesEqual(const ModuleDecl *lhs, const ModuleDecl *rhs, bool isClangEqual = true) {
if (lhs->getNameStr() != rhs->getNameStr())
if (getFullModuleName(lhs) != getFullModuleName(rhs))
return false;

if (!isClangEqual && (lhs->isNonSwiftModule() != rhs->isNonSwiftModule()))
Expand Down
53 changes: 53 additions & 0 deletions test/SymbolGraph/ClangImporter/HeaderNameCollision.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// RUN: %empty-directory(%t)
// RUN: split-file %s %t

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

// REQUIRES: objc_interop

// Ensure that extensions to a dependency's types, declared in a header with the same name as the
// dependency's type's header, correctly get sorted into an extension header rather than the module
// itself.

// MODULE: "symbols": []
// EXTENSION: "precise": "c:objc(cs)DependencyClass(im)addNumber:to:"

//--- Dependency.framework/Modules/module.modulemap
framework module Dependency {
umbrella header "Dependency.h"
export *
module * { export * }
}

//--- Dependency.framework/Headers/Dependency.h
#import <Dependency/DependencyClass.h>

//--- Dependency.framework/Headers/DependencyClass.h
@import Foundation;

@class DependencyClass;

@interface DependencyClass : NSObject

@end

//--- HeaderCollision.framework/Modules/module.modulemap
framework module HeaderCollision {
umbrella header "HeaderCollision.h"
export *
module * { export * }
}

//--- HeaderCollision.framework/Headers/HeaderCollision.h
#import <HeaderCollision/DependencyClass.h>

//--- HeaderCollision.framework/Headers/DependencyClass.h
#import <Dependency/DependencyClass.h>

@interface DependencyClass (HeaderCollisionClassAdditions)

- (NSInteger)addNumber:(NSInteger)x to:(NSInteger)y;

@end