@@ -749,54 +749,66 @@ static bool moduleHasAnyImportsMatchingFlag(ModuleDecl *mod, ImportFlags flag) {
749
749
return false ;
750
750
}
751
751
752
- // / Finds all import declarations for a single module that inconsistently match
752
+ // / Finds all import declarations for a single file that inconsistently match
753
753
// / \c predicate and passes each pair of inconsistent imports to \c diagnose.
754
754
template <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 ;
759
764
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)
763
767
continue ;
764
768
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)
768
773
continue ;
769
774
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);
773
779
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
+ }
779
785
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
+ }
784
791
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
+ }
790
796
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;
796
804
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);
800
812
}
801
813
}
802
814
@@ -830,7 +842,7 @@ CheckInconsistentImplementationOnlyImportsRequest::evaluate(
830
842
return decl->getAttrs ().hasAttribute <ImplementationOnlyAttr>();
831
843
};
832
844
833
- findInconsistentImports (mod, predicate, diagnose);
845
+ findInconsistentImportsAcrossModule (mod, predicate, diagnose);
834
846
return {};
835
847
}
836
848
@@ -855,7 +867,7 @@ CheckInconsistentWeakLinkedImportsRequest::evaluate(Evaluator &evaluator,
855
867
return decl->getAttrs ().hasAttribute <WeakLinkedAttr>();
856
868
};
857
869
858
- findInconsistentImports (mod, predicate, diagnose);
870
+ findInconsistentImportsAcrossModule (mod, predicate, diagnose);
859
871
return {};
860
872
}
861
873
0 commit comments