AST: Clarify SourceFile's existing missing imports record.

In anticipation of adding a new kind of missing import record to `SourceFile`,
clarify the purpose of the existing "missing imports" record with more specific
naming and documentation.
This commit is contained in:
Allan Shortlidge
2024-08-01 10:53:53 -07:00
parent 5424ddd051
commit fff4bebc44
6 changed files with 30 additions and 18 deletions

View File

@@ -273,8 +273,8 @@ public:
ModuleDecl::ImportFilter filter) const {} ModuleDecl::ImportFilter filter) const {}
/// Lists modules that are not imported from this file and used in API. /// Lists modules that are not imported from this file and used in API.
virtual void virtual void getImplicitImportsForModuleInterface(
getMissingImportedModules(SmallVectorImpl<ImportedModule> &imports) const {} SmallVectorImpl<ImportedModule> &imports) const {}
/// \see ModuleDecl::getImportedModulesForLookup /// \see ModuleDecl::getImportedModulesForLookup
virtual void getImportedModulesForLookup( virtual void getImportedModulesForLookup(

View File

@@ -1034,8 +1034,8 @@ public:
ImportFilter filter = ImportFilterKind::Exported) const; ImportFilter filter = ImportFilterKind::Exported) const;
/// Lists modules that are not imported from a file and used in API. /// Lists modules that are not imported from a file and used in API.
void void getImplicitImportsForModuleInterface(
getMissingImportedModules(SmallVectorImpl<ImportedModule> &imports) const; SmallVectorImpl<ImportedModule> &imports) const;
/// Looks up which modules are imported by this module, ignoring any that /// Looks up which modules are imported by this module, ignoring any that
/// won't contain top-level decls. /// won't contain top-level decls.

View File

@@ -159,6 +159,10 @@ private:
/// The highest access level of declarations referencing each import. /// The highest access level of declarations referencing each import.
llvm::DenseMap<const ModuleDecl *, AccessLevel> ImportsUseAccessLevel; llvm::DenseMap<const ModuleDecl *, AccessLevel> ImportsUseAccessLevel;
/// Imports that should be printed in the module interface even though they
/// were not written in the source file.
llvm::SmallDenseSet<ImportedModule> ImplicitImportsForModuleInterface;
/// A unique identifier representing this file; used to mark private decls /// A unique identifier representing this file; used to mark private decls
/// within the file to keep them from conflicting with other files in the /// within the file to keep them from conflicting with other files in the
/// same module. /// same module.
@@ -510,12 +514,20 @@ public:
SWIFT_DEBUG_DUMPER(dumpSeparatelyImportedOverlays()); SWIFT_DEBUG_DUMPER(dumpSeparatelyImportedOverlays());
llvm::SmallDenseSet<ImportedModule> MissingImportedModules; /// Record an import that should be printed in the module interface even
/// though it was not written in the source file. These imports are needed in
void addMissingImportedModule(ImportedModule module) const { /// Swift 5 mode to preserve the integrity of swiftinterface files when code
const_cast<SourceFile *>(this)->MissingImportedModules.insert(module); /// publicly use declarations from modules that were \c `@_implementationOnly`
/// imported in other source files.
void addImplicitImportForModuleInterface(ImportedModule module) {
ImplicitImportsForModuleInterface.insert(module);
} }
/// Gather implicit imports that should printed in swiftinterfaces for
/// compatibility with code in some Swift 5 modules.
void getImplicitImportsForModuleInterface(
SmallVectorImpl<ImportedModule> &imports) const override;
/// Record the source range info for a parsed \c #if clause. /// Record the source range info for a parsed \c #if clause.
void recordIfConfigClauseRangeInfo(const IfConfigClauseRangeInfo &range); void recordIfConfigClauseRangeInfo(const IfConfigClauseRangeInfo &range);
@@ -527,10 +539,10 @@ public:
ArrayRef<IfConfigClauseRangeInfo> ArrayRef<IfConfigClauseRangeInfo>
getIfConfigClausesWithin(SourceRange outer) const; getIfConfigClausesWithin(SourceRange outer) const;
void getMissingImportedModules( /// Record visible declarations for use in code completion and refactoring.
SmallVectorImpl<ImportedModule> &imports) const override;
void cacheVisibleDecls(SmallVectorImpl<ValueDecl *> &&globals) const; void cacheVisibleDecls(SmallVectorImpl<ValueDecl *> &&globals) const;
/// Retrieve visible declarations for use in code completion and refactoring.
const SmallVectorImpl<ValueDecl *> &getCachedVisibleDecls() const; const SmallVectorImpl<ValueDecl *> &getCachedVisibleDecls() const;
virtual void lookupValue(DeclName name, NLKind lookupKind, virtual void lookupValue(DeclName name, NLKind lookupKind,

View File

@@ -1720,9 +1720,9 @@ void ModuleDecl::getImportedModules(SmallVectorImpl<ImportedModule> &modules,
FORWARD(getImportedModules, (modules, filter)); FORWARD(getImportedModules, (modules, filter));
} }
void ModuleDecl::getMissingImportedModules( void ModuleDecl::getImplicitImportsForModuleInterface(
SmallVectorImpl<ImportedModule> &imports) const { SmallVectorImpl<ImportedModule> &imports) const {
FORWARD(getMissingImportedModules, (imports)); FORWARD(getImplicitImportsForModuleInterface, (imports));
} }
const llvm::DenseMap<const clang::Module *, ModuleDecl *> & const llvm::DenseMap<const clang::Module *, ModuleDecl *> &
@@ -1811,9 +1811,9 @@ SourceFile::getImportedModules(SmallVectorImpl<ImportedModule> &modules,
} }
} }
void SourceFile::getMissingImportedModules( void SourceFile::getImplicitImportsForModuleInterface(
SmallVectorImpl<ImportedModule> &modules) const { SmallVectorImpl<ImportedModule> &modules) const {
for (auto module : MissingImportedModules) for (auto module : ImplicitImportsForModuleInterface)
modules.push_back(module); modules.push_back(module);
} }

View File

@@ -87,7 +87,7 @@ static void printToolVersionAndFlagsComment(raw_ostream &out,
SmallVector<ImportedModule> imports; SmallVector<ImportedModule> imports;
M->getImportedModules(imports, filter); M->getImportedModules(imports, filter);
M->getMissingImportedModules(imports); M->getImplicitImportsForModuleInterface(imports);
for (ImportedModule import: imports) { for (ImportedModule import: imports) {
StringRef importedName = import.importedModule->getNameStr(); StringRef importedName = import.importedModule->getNameStr();
@@ -337,7 +337,7 @@ static void printImports(raw_ostream &out,
M->getImportedModules(allImports, allImportFilter); M->getImportedModules(allImports, allImportFilter);
if (Opts.PrintMissingImports) if (Opts.PrintMissingImports)
M->getMissingImportedModules(allImports); M->getImplicitImportsForModuleInterface(allImports);
ImportedModule::removeDuplicates(allImports); ImportedModule::removeDuplicates(allImports);
diagnoseScopedImports(ctx.Diags, allImports); diagnoseScopedImports(ctx.Diags, allImports);

View File

@@ -45,7 +45,7 @@ static bool addMissingImport(SourceLoc loc, const Decl *D,
// API. // API.
auto missingImport = ImportedModule(ImportPath::Access(), auto missingImport = ImportedModule(ImportPath::Access(),
const_cast<ModuleDecl *>(M)); const_cast<ModuleDecl *>(M));
SF->addMissingImportedModule(missingImport); SF->addImplicitImportForModuleInterface(missingImport);
ctx.Diags.diagnose(loc, diag::missing_import_inserted, M->getName()); ctx.Diags.diagnose(loc, diag::missing_import_inserted, M->getName());
return true; return true;
} }