Skip to content

Commit cf461be

Browse files
authored
Merge pull request swiftlang#81414 from hamishknight/ref-nt
[Frontend] Avoid storing StringRef values in `ModuleAliasMap`
2 parents 0fd55d7 + b53c411 commit cf461be

File tree

5 files changed

+15
-12
lines changed

5 files changed

+15
-12
lines changed

include/swift/AST/ASTContext.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ class ASTContext final {
620620
/// For example, if '-module-alias Foo=X -module-alias Bar=Y' input is passed in, the aliases Foo and Bar are
621621
/// the names of the imported or referenced modules in source files in the main module, and X and Y
622622
/// are the real (physical) module names on disk.
623-
void setModuleAliases(const llvm::StringMap<StringRef> &aliasMap);
623+
void setModuleAliases(const llvm::StringMap<std::string> &aliasMap);
624624

625625
/// Adds a given alias to the map of Identifiers between module aliases and
626626
/// their actual names.

include/swift/Frontend/FrontendOptions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class FrontendOptions {
5858
std::string ImplicitObjCPCHPath;
5959

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

6363
/// The name of the module that the frontend is building.
6464
std::string ModuleName;

lib/AST/ASTContext.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2153,15 +2153,14 @@ void ASTContext::addModuleInterfaceChecker(
21532153
getImpl().InterfaceChecker = std::move(checker);
21542154
}
21552155

2156-
void ASTContext::setModuleAliases(const llvm::StringMap<StringRef> &aliasMap) {
2156+
void ASTContext::setModuleAliases(
2157+
const llvm::StringMap<std::string> &aliasMap) {
21572158
// This setter should be called only once after ASTContext has been initialized
21582159
assert(ModuleAliasMap.empty());
2159-
2160-
for (auto k: aliasMap.keys()) {
2161-
auto v = aliasMap.lookup(k);
2162-
if (!v.empty()) {
2163-
addModuleAlias(k, v);
2164-
}
2160+
2161+
for (auto &entry : aliasMap) {
2162+
if (!entry.getValue().empty())
2163+
addModuleAlias(entry.getKey(), entry.getValue());
21652164
}
21662165
}
21672166

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -984,13 +984,12 @@ bool ModuleAliasesConverter::computeModuleAliases(std::vector<std::string> args,
984984

985985
// First, add the real name as a key to prevent it from being
986986
// used as an alias
987-
if (!options.ModuleAliasMap.insert({rhs, StringRef()}).second) {
987+
if (!options.ModuleAliasMap.insert({rhs, ""}).second) {
988988
diags.diagnose(SourceLoc(), diag::error_module_alias_duplicate, rhs);
989989
return false;
990990
}
991991
// Next, add the alias as a key and the real name as a value to the map
992-
auto underlyingName = options.ModuleAliasMap.find(rhs)->first();
993-
if (!options.ModuleAliasMap.insert({lhs, underlyingName}).second) {
992+
if (!options.ModuleAliasMap.insert({lhs, rhs.str()}).second) {
994993
diags.diagnose(SourceLoc(), diag::error_module_alias_duplicate, lhs);
995994
return false;
996995
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// RUN: env DYLD_INSERT_LIBRARIES=/usr/lib/libgmalloc.dylib %sourcekitd-test -req=active-regions %s -- %s -module-name B -module-alias A=B
2+
// Make sure we don't crash.
3+
4+
// guardmalloc is incompatible with ASAN
5+
// REQUIRES: no_asan

0 commit comments

Comments
 (0)