Skip to content

Commit 7719a3d

Browse files
committed
Add a module alias option to frontend
Validate input and set up the module alias map rdar://83316886
1 parent d7281a1 commit 7719a3d

File tree

8 files changed

+63
-1
lines changed

8 files changed

+63
-1
lines changed

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ ERROR(error_stdlib_module_name,none,
150150
"module name \"%0\" is reserved for the standard library"
151151
"%select{|; use -module-name flag to specify an alternate name}1",
152152
(StringRef, bool))
153-
153+
ERROR(error_bad_module_alias,none,
154+
"invalid module alias \"%0\" for an imported module; make sure the input format is correct (-module-alias alias=underlying_name), where the alias maps to an imported module, and the names do not contain special characters", (StringRef))
154155
ERROR(error_stdlib_not_found,Fatal,
155156
"unable to load standard library for target '%0'", (StringRef))
156157

include/swift/Basic/LangOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "llvm/Support/Regex.h"
3232
#include "llvm/Support/VersionTuple.h"
3333
#include "llvm/Support/raw_ostream.h"
34+
#include <map>
3435
#include <string>
3536
#include <vector>
3637

@@ -196,6 +197,9 @@ namespace swift {
196197
/// Enable features useful for running in the debugger.
197198
bool DebuggerSupport = false;
198199

200+
/// A map of aliases and underlying names of imported modules.
201+
std::map<StringRef, StringRef> ModuleAliasMap;
202+
199203
/// Enable the MemoryBufferSerializedModuleImporter.
200204
/// Only used by lldb-moduleimport-test.
201205
bool EnableMemoryBufferImporter = false;

include/swift/Frontend/Frontend.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,14 @@ class CompilerInvocation {
300300
return FrontendOpts.ParseStdlib;
301301
}
302302

303+
void setModuleAliasMap(std::map<StringRef, StringRef> ModAliasMap) {
304+
FrontendOpts.ModuleAliasMap = ModAliasMap;
305+
}
306+
307+
std::map<StringRef, StringRef> getModuleAliasMap() const {
308+
return FrontendOpts.ModuleAliasMap;
309+
}
310+
303311
void setModuleName(StringRef Name) {
304312
FrontendOpts.ModuleName = Name.str();
305313
IRGenOpts.ModuleName = Name.str();

include/swift/Frontend/FrontendOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "swift/Frontend/InputFile.h"
2020
#include "llvm/ADT/Hashing.h"
2121

22+
#include <map>
2223
#include <string>
2324
#include <vector>
2425

@@ -48,6 +49,9 @@ class FrontendOptions {
4849
/// An Objective-C header to import and make implicitly visible.
4950
std::string ImplicitObjCHeaderPath;
5051

52+
/// The map of aliases and underlying names of imported modules.
53+
std::map<StringRef, StringRef> ModuleAliasMap;
54+
5155
/// The name of the module that the frontend is building.
5256
std::string ModuleName;
5357

include/swift/Option/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,10 @@ def module_name : Separate<["-"], "module-name">,
430430
def module_name_EQ : Joined<["-"], "module-name=">, Flags<[FrontendOption]>,
431431
Alias<module_name>;
432432

433+
def module_alias : Separate<["-"], "module-alias">,
434+
Flags<[FrontendOption]>,
435+
HelpText<"Alias and underlying name 'alias=underlying_name' of the module to be imported">;
436+
433437
def module_link_name : Separate<["-"], "module-link-name">,
434438
Flags<[FrontendOption, ModuleInterfaceOption]>,
435439
HelpText<"Library to link against when using this module">;

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,11 @@ bool ArgsToFrontendOptionsConverter::convert(
244244
if (const Arg *A = Args.getLastArg(OPT_module_link_name))
245245
Opts.ModuleLinkName = A->getValue();
246246

247+
// This must be called after computing module name, module abi name,
248+
// and module link name.
249+
if (computeModuleAliases())
250+
return true;
251+
247252
if (const Arg *A = Args.getLastArg(OPT_access_notes_path))
248253
Opts.AccessNotesPath = A->getValue();
249254

@@ -498,6 +503,40 @@ bool ArgsToFrontendOptionsConverter::setUpImmediateArgs() {
498503
return false;
499504
}
500505

506+
bool ArgsToFrontendOptionsConverter::computeModuleAliases() {
507+
auto list = Args.getAllArgValues(options::OPT_module_alias);
508+
if (!list.empty()) {
509+
for (auto val: list) {
510+
auto i = val.find("=");
511+
if (i == std::string::npos) {
512+
Diags.diagnose(SourceLoc(), diag::error_bad_module_alias, val);
513+
return true;
514+
}
515+
516+
std::string *lhs = new std::string(val.substr(0, i));
517+
std::string *rhs = new std::string(val.substr(i+1, val.length()));
518+
auto modAlias = StringRef(*lhs);
519+
auto modValue = StringRef(*rhs);
520+
521+
if (modAlias.empty() ||
522+
modValue.empty() ||
523+
modAlias == Opts.ModuleName ||
524+
modAlias == Opts.ModuleABIName ||
525+
modAlias == Opts.ModuleLinkName ||
526+
modValue == Opts.ModuleName ||
527+
modValue == Opts.ModuleABIName ||
528+
modValue == Opts.ModuleLinkName ||
529+
!Lexer::isIdentifier(modAlias) ||
530+
!Lexer::isIdentifier(modValue)) {
531+
Diags.diagnose(SourceLoc(), diag::error_bad_module_alias, val);
532+
return true;
533+
}
534+
Opts.ModuleAliasMap.insert({ modAlias, modValue });
535+
}
536+
}
537+
return false;
538+
}
539+
501540
bool ArgsToFrontendOptionsConverter::computeModuleName() {
502541
const Arg *A = Args.getLastArg(options::OPT_module_name);
503542
if (A) {

lib/Frontend/ArgsToFrontendOptionsConverter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class ArgsToFrontendOptionsConverter {
3737
void computeDebugTimeOptions();
3838
bool computeFallbackModuleName();
3939
bool computeModuleName();
40+
bool computeModuleAliases();
4041
bool computeMainAndSupplementaryOutputFilenames();
4142
void computeDumpScopeMapLocations();
4243
void computeHelpOptions();

lib/Frontend/Frontend.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ bool CompilerInstance::setUpASTContextIfNeeded() {
211211
// and single file builds.
212212
Invocation.getLangOptions().RecordRequestReferences
213213
= !isWholeModuleCompilation();
214+
Invocation.getLangOptions().ModuleAliasMap = Invocation.getFrontendOptions().ModuleAliasMap;
214215

215216
Context.reset(ASTContext::get(
216217
Invocation.getLangOptions(), Invocation.getTypeCheckerOptions(),

0 commit comments

Comments
 (0)