@@ -253,8 +253,9 @@ bool ArgsToFrontendOptionsConverter::convert(
253253 Opts.ModuleLinkName = A->getValue ();
254254
255255 // This must be called after computing module name, module abi name,
256- // and module link name.
257- if (computeModuleAliases ())
256+ // and module link name. If computing module aliases is unsuccessful,
257+ // return early.
258+ if (!computeModuleAliases ())
258259 return true ;
259260
260261 if (const Arg *A = Args.getLastArg (OPT_access_notes_path))
@@ -521,58 +522,7 @@ bool ArgsToFrontendOptionsConverter::setUpImmediateArgs() {
521522
522523bool ArgsToFrontendOptionsConverter::computeModuleAliases () {
523524 auto list = Args.getAllArgValues (options::OPT_module_alias);
524- if (!list.empty ()) {
525- auto validate = [this ](StringRef value, bool allowModuleName) -> bool
526- {
527- if (!allowModuleName) {
528- if (value == Opts.ModuleName ||
529- value == Opts.ModuleABIName ||
530- value == Opts.ModuleLinkName ) {
531- Diags.diagnose (SourceLoc (), diag::error_module_alias_forbidden_name, value);
532- return false ;
533- }
534- }
535- if (value == STDLIB_NAME) {
536- Diags.diagnose (SourceLoc (), diag::error_module_alias_forbidden_name, value);
537- return false ;
538- }
539- if (!Lexer::isIdentifier (value)) {
540- Diags.diagnose (SourceLoc (), diag::error_bad_module_name, value, false );
541- return false ;
542- }
543- return true ;
544- };
545-
546- for (auto item: list) {
547- auto str = StringRef (item);
548- // splits to an alias and the underlying name
549- auto pair = str.split (' =' );
550- auto lhs = pair.first ;
551- auto rhs = pair.second ;
552-
553- if (rhs.empty ()) { // '=' is missing
554- Diags.diagnose (SourceLoc (), diag::error_module_alias_invalid_format, str);
555- return true ;
556- }
557- if (!validate (lhs, false ) || !validate (rhs, true )) {
558- return true ;
559- }
560-
561- // First, add the underlying name as a key to prevent it from being
562- // used as an alias
563- if (!Opts.ModuleAliasMap .insert ({rhs, StringRef ()}).second ) {
564- Diags.diagnose (SourceLoc (), diag::error_module_alias_duplicate, rhs);
565- return true ;
566- }
567- // Next, add the alias as a key and the underlying name as a value to the map
568- auto underlyingName = Opts.ModuleAliasMap .find (rhs)->first ();
569- if (!Opts.ModuleAliasMap .insert ({lhs, underlyingName}).second ) {
570- Diags.diagnose (SourceLoc (), diag::error_module_alias_duplicate, lhs);
571- return true ;
572- }
573- }
574- }
575- return false ;
525+ return ModuleAliasesConverter::computeModuleAliases (list, Opts, Diags);
576526}
577527
578528bool ArgsToFrontendOptionsConverter::computeModuleName () {
@@ -753,3 +703,64 @@ void ArgsToFrontendOptionsConverter::computeLLVMArgs() {
753703 Opts.LLVMArgs .push_back (A->getValue ());
754704 }
755705}
706+
707+ bool ModuleAliasesConverter::computeModuleAliases (std::vector<std::string> args,
708+ FrontendOptions &options,
709+ DiagnosticEngine &diags) {
710+ if (!args.empty ()) {
711+ // ModuleAliasMap should initially be empty as setting
712+ // it should be called only once
713+ options.ModuleAliasMap .clear ();
714+
715+ auto validate = [&options, &diags](StringRef value, bool allowModuleName) -> bool
716+ {
717+ if (!allowModuleName) {
718+ if (value == options.ModuleName ||
719+ value == options.ModuleABIName ||
720+ value == options.ModuleLinkName ) {
721+ diags.diagnose (SourceLoc (), diag::error_module_alias_forbidden_name, value);
722+ return false ;
723+ }
724+ }
725+ if (value == STDLIB_NAME) {
726+ diags.diagnose (SourceLoc (), diag::error_module_alias_forbidden_name, value);
727+ return false ;
728+ }
729+ if (!Lexer::isIdentifier (value)) {
730+ diags.diagnose (SourceLoc (), diag::error_bad_module_name, value, false );
731+ return false ;
732+ }
733+ return true ;
734+ };
735+
736+ for (auto item: args) {
737+ auto str = StringRef (item);
738+ // splits to an alias and the underlying name
739+ auto pair = str.split (' =' );
740+ auto lhs = pair.first ;
741+ auto rhs = pair.second ;
742+
743+ if (rhs.empty ()) { // '=' is missing
744+ diags.diagnose (SourceLoc (), diag::error_module_alias_invalid_format, str);
745+ return false ;
746+ }
747+ if (!validate (lhs, false ) || !validate (rhs, true )) {
748+ return false ;
749+ }
750+
751+ // First, add the underlying name as a key to prevent it from being
752+ // used as an alias
753+ if (!options.ModuleAliasMap .insert ({rhs, StringRef ()}).second ) {
754+ diags.diagnose (SourceLoc (), diag::error_module_alias_duplicate, rhs);
755+ return false ;
756+ }
757+ // Next, add the alias as a key and the underlying name as a value to the map
758+ auto underlyingName = options.ModuleAliasMap .find (rhs)->first ();
759+ if (!options.ModuleAliasMap .insert ({lhs, underlyingName}).second ) {
760+ diags.diagnose (SourceLoc (), diag::error_module_alias_duplicate, lhs);
761+ return false ;
762+ }
763+ }
764+ }
765+ return true ;
766+ }
0 commit comments