Module Aliasing: do not allow module real names to appear in source files / only allow aliases

Resolves rdar://83592084
This commit is contained in:
elsh
2021-10-28 02:36:57 -07:00
parent 710c6b858b
commit 10a96dff57
6 changed files with 182 additions and 6 deletions

View File

@@ -1638,9 +1638,14 @@ void ASTContext::addModuleInterfaceChecker(
void ASTContext::setModuleAliases(const llvm::StringMap<StringRef> &aliasMap) {
for (auto k: aliasMap.keys()) {
auto val = aliasMap.lookup(k);
if (!val.empty()) {
ModuleAliasMap[getIdentifier(k)] = getIdentifier(val);
auto v = aliasMap.lookup(k);
if (!v.empty()) {
auto key = getIdentifier(k);
auto val = getIdentifier(v);
// key is a module alias, val is its corresponding real name
ModuleAliasMap[key] = std::make_pair(val, true);
// add an entry with an alias as key for an easier lookup later
ModuleAliasMap[val] = std::make_pair(key, false);
}
}
}
@@ -1648,11 +1653,22 @@ void ASTContext::setModuleAliases(const llvm::StringMap<StringRef> &aliasMap) {
Identifier ASTContext::getRealModuleName(Identifier key) const {
auto found = ModuleAliasMap.find(key);
if (found != ModuleAliasMap.end()) {
return found->second;
auto realOrAlias = found->second;
if (realOrAlias.second) // check if it's a real name given the key (alias)
return realOrAlias.first;
}
return key;
}
std::pair<Identifier, bool> ASTContext::getRealModuleNameOrAlias(Identifier key) const {
auto found = ModuleAliasMap.find(key);
if (found != ModuleAliasMap.end()) {
return found->second;
}
// No module aliasing is used for the given key
return std::make_pair(key, true);
}
Optional<ModuleDependencies> ASTContext::getModuleDependencies(
StringRef moduleName, bool isUnderlyingClangModule,
ModuleDependenciesCache &cache, InterfaceSubContextDelegate &delegate,