Skip to content

[6.2] [Frontend] Avoid storing StringRef values in ModuleAliasMap #81417

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
May 10, 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
2 changes: 1 addition & 1 deletion include/swift/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ class ASTContext final {
/// For example, if '-module-alias Foo=X -module-alias Bar=Y' input is passed in, the aliases Foo and Bar are
/// the names of the imported or referenced modules in source files in the main module, and X and Y
/// are the real (physical) module names on disk.
void setModuleAliases(const llvm::StringMap<StringRef> &aliasMap);
void setModuleAliases(const llvm::StringMap<std::string> &aliasMap);

/// Adds a given alias to the map of Identifiers between module aliases and
/// their actual names.
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Frontend/FrontendOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class FrontendOptions {
std::string ImplicitObjCPCHPath;

/// The map of aliases and real names of imported or referenced modules.
llvm::StringMap<StringRef> ModuleAliasMap;
llvm::StringMap<std::string> ModuleAliasMap;

/// The name of the module that the frontend is building.
std::string ModuleName;
Expand Down
13 changes: 6 additions & 7 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2151,15 +2151,14 @@ void ASTContext::addModuleInterfaceChecker(
getImpl().InterfaceChecker = std::move(checker);
}

void ASTContext::setModuleAliases(const llvm::StringMap<StringRef> &aliasMap) {
void ASTContext::setModuleAliases(
const llvm::StringMap<std::string> &aliasMap) {
// This setter should be called only once after ASTContext has been initialized
assert(ModuleAliasMap.empty());

for (auto k: aliasMap.keys()) {
auto v = aliasMap.lookup(k);
if (!v.empty()) {
addModuleAlias(k, v);
}

for (auto &entry : aliasMap) {
if (!entry.getValue().empty())
addModuleAlias(entry.getKey(), entry.getValue());
}
}

Expand Down
5 changes: 2 additions & 3 deletions lib/Frontend/ArgsToFrontendOptionsConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -980,13 +980,12 @@ bool ModuleAliasesConverter::computeModuleAliases(std::vector<std::string> args,

// First, add the real name as a key to prevent it from being
// used as an alias
if (!options.ModuleAliasMap.insert({rhs, StringRef()}).second) {
if (!options.ModuleAliasMap.insert({rhs, ""}).second) {
diags.diagnose(SourceLoc(), diag::error_module_alias_duplicate, rhs);
return false;
}
// Next, add the alias as a key and the real name as a value to the map
auto underlyingName = options.ModuleAliasMap.find(rhs)->first();
if (!options.ModuleAliasMap.insert({lhs, underlyingName}).second) {
if (!options.ModuleAliasMap.insert({lhs, rhs.str()}).second) {
diags.diagnose(SourceLoc(), diag::error_module_alias_duplicate, lhs);
return false;
}
Expand Down
5 changes: 5 additions & 0 deletions test/SourceKit/Misc/rdar148130166.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// RUN: env DYLD_INSERT_LIBRARIES=/usr/lib/libgmalloc.dylib %sourcekitd-test -req=active-regions %s -- %s -module-name B -module-alias A=B
// Make sure we don't crash.

// guardmalloc is incompatible with ASAN
// REQUIRES: no_asan