@@ -749,54 +749,66 @@ static bool moduleHasAnyImportsMatchingFlag(ModuleDecl *mod, ImportFlags flag) {
749749 return false ;
750750}
751751
752- // / Finds all import declarations for a single module that inconsistently match
752+ // / Finds all import declarations for a single file that inconsistently match
753753// / \c predicate and passes each pair of inconsistent imports to \c diagnose.
754754template <typename Pred, typename Diag>
755- static void findInconsistentImports (ModuleDecl *mod, Pred predicate,
756- Diag diagnose) {
757- llvm::DenseMap<ModuleDecl *, const ImportDecl *> matchingImports;
758- llvm::DenseMap<ModuleDecl *, std::vector<const ImportDecl *>> otherImports;
755+ static void findInconsistentImportsAcrossFile (
756+ const SourceFile *SF, Pred predicate, Diag diagnose,
757+ llvm::DenseMap<ModuleDecl *, const ImportDecl *> &matchingImports,
758+ llvm::DenseMap<ModuleDecl *, std::vector<const ImportDecl *>> &otherImports) {
759+
760+ for (auto *topLevelDecl : SF->getTopLevelDecls ()) {
761+ auto *nextImport = dyn_cast<ImportDecl>(topLevelDecl);
762+ if (!nextImport)
763+ continue ;
759764
760- for (const FileUnit *file : mod->getFiles ()) {
761- auto *SF = dyn_cast<SourceFile>(file);
762- if (!SF)
765+ ModuleDecl *module = nextImport->getModule ();
766+ if (!module )
763767 continue ;
764768
765- for (auto *topLevelDecl : SF->getTopLevelDecls ()) {
766- auto *nextImport = dyn_cast<ImportDecl>(topLevelDecl);
767- if (!nextImport)
769+ if (predicate (nextImport)) {
770+ // We found a matching import.
771+ bool isNew = matchingImports.insert ({module , nextImport}).second ;
772+ if (!isNew)
768773 continue ;
769774
770- ModuleDecl *module = nextImport->getModule ();
771- if (!module )
772- continue ;
775+ auto seenOtherImportPosition = otherImports.find (module );
776+ if (seenOtherImportPosition != otherImports.end ()) {
777+ for (auto *seenOtherImport : seenOtherImportPosition->getSecond ())
778+ diagnose (seenOtherImport, nextImport);
773779
774- if ( predicate (nextImport)) {
775- // We found a matching import.
776- bool isNew = matchingImports. insert ({ module , nextImport}). second ;
777- if (!isNew)
778- continue ;
780+ // We're done with these; keep the map small if possible.
781+ otherImports. erase (seenOtherImportPosition);
782+ }
783+ continue ;
784+ }
779785
780- auto seenOtherImportPosition = otherImports.find (module );
781- if (seenOtherImportPosition != otherImports.end ()) {
782- for (auto *seenOtherImport : seenOtherImportPosition->getSecond ())
783- diagnose (seenOtherImport, nextImport);
786+ // We saw a non-matching import. Is that in conflict with what we've seen?
787+ if (auto *seenMatchingImport = matchingImports.lookup (module )) {
788+ diagnose (nextImport, seenMatchingImport);
789+ continue ;
790+ }
784791
785- // We're done with these; keep the map small if possible.
786- otherImports.erase (seenOtherImportPosition);
787- }
788- continue ;
789- }
792+ // Otherwise, record it for later.
793+ otherImports[module ].push_back (nextImport);
794+ }
795+ }
790796
791- // We saw a non-matching import. Is that in conflict with what we've seen?
792- if (auto *seenMatchingImport = matchingImports.lookup (module )) {
793- diagnose (nextImport, seenMatchingImport);
794- continue ;
795- }
797+ // / Finds all import declarations for a single module that inconsistently match
798+ // / \c predicate and passes each pair of inconsistent imports to \c diagnose.
799+ template <typename Pred, typename Diag>
800+ static void findInconsistentImportsAcrossModule (ModuleDecl *mod, Pred predicate,
801+ Diag diagnose) {
802+ llvm::DenseMap<ModuleDecl *, const ImportDecl *> matchingImports;
803+ llvm::DenseMap<ModuleDecl *, std::vector<const ImportDecl *>> otherImports;
796804
797- // Otherwise, record it for later.
798- otherImports[module ].push_back (nextImport);
799- }
805+ for (const FileUnit *file : mod->getFiles ()) {
806+ auto *SF = dyn_cast<SourceFile>(file);
807+ if (!SF)
808+ continue ;
809+
810+ findInconsistentImportsAcrossFile (SF, predicate, diagnose,
811+ matchingImports, otherImports);
800812 }
801813}
802814
@@ -830,7 +842,7 @@ CheckInconsistentImplementationOnlyImportsRequest::evaluate(
830842 return decl->getAttrs ().hasAttribute <ImplementationOnlyAttr>();
831843 };
832844
833- findInconsistentImports (mod, predicate, diagnose);
845+ findInconsistentImportsAcrossModule (mod, predicate, diagnose);
834846 return {};
835847}
836848
@@ -855,7 +867,7 @@ CheckInconsistentWeakLinkedImportsRequest::evaluate(Evaluator &evaluator,
855867 return decl->getAttrs ().hasAttribute <WeakLinkedAttr>();
856868 };
857869
858- findInconsistentImports (mod, predicate, diagnose);
870+ findInconsistentImportsAcrossModule (mod, predicate, diagnose);
859871 return {};
860872}
861873
0 commit comments