Skip to content

Commit 20179aa

Browse files
authored
Merge pull request #81417 from hamishknight/ref-nt-6.2
[6.2] [Frontend] Avoid storing StringRef values in `ModuleAliasMap`
2 parents 8c2ee95 + 54a6d75 commit 20179aa

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
@@ -2151,15 +2151,14 @@ void ASTContext::addModuleInterfaceChecker(
21512151
getImpl().InterfaceChecker = std::move(checker);
21522152
}
21532153

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

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

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

981981
// First, add the real name as a key to prevent it from being
982982
// used as an alias
983-
if (!options.ModuleAliasMap.insert({rhs, StringRef()}).second) {
983+
if (!options.ModuleAliasMap.insert({rhs, ""}).second) {
984984
diags.diagnose(SourceLoc(), diag::error_module_alias_duplicate, rhs);
985985
return false;
986986
}
987987
// Next, add the alias as a key and the real name as a value to the map
988-
auto underlyingName = options.ModuleAliasMap.find(rhs)->first();
989-
if (!options.ModuleAliasMap.insert({lhs, underlyingName}).second) {
988+
if (!options.ModuleAliasMap.insert({lhs, rhs.str()}).second) {
990989
diags.diagnose(SourceLoc(), diag::error_module_alias_duplicate, lhs);
991990
return false;
992991
}
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)