Skip to content

Commit c98e292

Browse files
authored
Merge pull request #24642 from rintaro/5.1-ide-complete-import-swiftmodule
[5.1][CodeCompletion] Complete Swift only module name after 'import'
2 parents a311f42 + 2a8eaaf commit c98e292

File tree

15 files changed

+437
-151
lines changed

15 files changed

+437
-151
lines changed

include/swift/AST/ASTContext.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -892,9 +892,9 @@ class ASTContext final {
892892
return getIdentifier(getSwiftName(kind));
893893
}
894894

895-
/// Collect visible clang modules from the ClangModuleLoader. These modules are
896-
/// not necessarily loaded.
897-
void getVisibleTopLevelClangModules(SmallVectorImpl<clang::Module*> &Modules) const;
895+
/// Populate \p names with visible top level module names.
896+
/// This guarantees that resulted \p names doesn't have duplicated names.
897+
void getVisibleTopLevelModuleNames(SmallVectorImpl<Identifier> &names) const;
898898

899899
private:
900900
/// Register the given generic signature builder to be used as the canonical

include/swift/AST/ModuleLoader.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ class ModuleLoader {
8282
public:
8383
virtual ~ModuleLoader() = default;
8484

85+
/// Collect visible module names.
86+
///
87+
/// Append visible module names to \p names. Note that names are possibly
88+
/// duplicated, and not guaranteed to be ordered in any way.
89+
virtual void collectVisibleTopLevelModuleNames(
90+
SmallVectorImpl<Identifier> &names) const = 0;
91+
8592
/// Check whether the module with a given name can be imported without
8693
/// importing it.
8794
///

include/swift/ClangImporter/ClangImporter.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ class ClangImporter final : public ClangModuleLoader {
119119
static std::shared_ptr<clang::DependencyCollector>
120120
createDependencyCollector(bool TrackSystemDeps);
121121

122+
/// Append visible module names to \p names. Note that names are possibly
123+
/// duplicated, and not guaranteed to be ordered in any way.
124+
void collectVisibleTopLevelModuleNames(
125+
SmallVectorImpl<Identifier> &names) const override;
126+
122127
/// Check whether the module with a given name can be imported without
123128
/// importing it.
124129
///
@@ -336,7 +341,7 @@ class ClangImporter final : public ClangModuleLoader {
336341
/// Calling this function does not load the module.
337342
void collectSubModuleNames(
338343
ArrayRef<std::pair<Identifier, SourceLoc>> path,
339-
std::vector<std::string> &names);
344+
std::vector<std::string> &names) const;
340345

341346
/// Given a Clang module, decide whether this module is imported already.
342347
static bool isModuleImported(const clang::Module *M);

include/swift/DWARFImporter/DWARFImporter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ class DWARFImporter final : public ClangModuleLoader {
6666

6767
~DWARFImporter();
6868

69+
void collectVisibleTopLevelModuleNames(
70+
SmallVectorImpl<Identifier> &names) const override;
71+
6972
/// Check whether the module with a given name can be imported without
7073
/// importing it.
7174
///

include/swift/Frontend/ParseableInterfaceModuleLoader.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ class ParseableInterfaceModuleLoader : public SerializedModuleLoaderBase {
154154
RemarkOnRebuildFromInterface));
155155
}
156156

157+
/// Append visible module names to \p names. Note that names are possibly
158+
/// duplicated, and not guaranteed to be ordered in any way.
159+
void collectVisibleTopLevelModuleNames(
160+
SmallVectorImpl<Identifier> &names) const override;
161+
157162
/// Unconditionally build \p InPath (a swiftinterface file) to \p OutPath (as
158163
/// a swiftmodule file).
159164
///

include/swift/Sema/SourceLoader.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ class SourceLoader : public ModuleLoader {
4848
SourceLoader &operator=(const SourceLoader &) = delete;
4949
SourceLoader &operator=(SourceLoader &&) = delete;
5050

51+
/// Append visible module names to \p names. Note that names are possibly
52+
/// duplicated, and not guaranteed to be ordered in any way.
53+
void collectVisibleTopLevelModuleNames(
54+
SmallVectorImpl<Identifier> &names) const override;
55+
5156
/// Check whether the module with a given name can be imported without
5257
/// importing it.
5358
///

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ class SerializedModuleLoaderBase : public ModuleLoader {
4444
SerializedModuleLoaderBase(ASTContext &ctx, DependencyTracker *tracker,
4545
ModuleLoadingMode LoadMode);
4646

47+
void collectVisibleTopLevelModuleNamesImpl(SmallVectorImpl<Identifier> &names,
48+
StringRef extension) const;
49+
4750
using AccessPathElem = std::pair<Identifier, SourceLoc>;
4851
bool findModule(AccessPathElem moduleID,
4952
std::unique_ptr<llvm::MemoryBuffer> *moduleBuffer,
@@ -169,6 +172,11 @@ class SerializedModuleLoader : public SerializedModuleLoaderBase {
169172
public:
170173
virtual ~SerializedModuleLoader();
171174

175+
/// Append visible module names to \p names. Note that names are possibly
176+
/// duplicated, and not guaranteed to be ordered in any way.
177+
void collectVisibleTopLevelModuleNames(
178+
SmallVectorImpl<Identifier> &names) const override;
179+
172180
/// Create a new importer that can load serialized Swift modules
173181
/// into the given ASTContext.
174182
static std::unique_ptr<SerializedModuleLoader>
@@ -220,6 +228,9 @@ class MemoryBufferSerializedModuleLoader : public SerializedModuleLoaderBase {
220228
MemoryBuffers[AccessPath] = std::move(input);
221229
}
222230

231+
void collectVisibleTopLevelModuleNames(
232+
SmallVectorImpl<Identifier> &names) const override {}
233+
223234
/// Create a new importer that can load serialized Swift modules
224235
/// into the given ASTContext.
225236
static std::unique_ptr<MemoryBufferSerializedModuleLoader>

lib/AST/ASTContext.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@
4646
#include "swift/Syntax/SyntaxArena.h"
4747
#include "swift/Strings.h"
4848
#include "swift/Subsystems.h"
49-
#include "clang/AST/Attr.h"
50-
#include "clang/AST/DeclObjC.h"
51-
#include "clang/Lex/HeaderSearch.h"
52-
#include "clang/Lex/Preprocessor.h"
5349
#include "llvm/ADT/DenseMap.h"
5450
#include "llvm/ADT/Statistic.h"
5551
#include "llvm/ADT/StringMap.h"
@@ -1502,12 +1498,6 @@ ModuleDecl *ASTContext::getLoadedModule(Identifier ModuleName) const {
15021498
return LoadedModules.lookup(ModuleName);
15031499
}
15041500

1505-
void ASTContext::getVisibleTopLevelClangModules(
1506-
SmallVectorImpl<clang::Module*> &Modules) const {
1507-
getClangModuleLoader()->getClangPreprocessor().getHeaderSearchInfo().
1508-
collectAllModules(Modules);
1509-
}
1510-
15111501
static AllocationArena getArena(GenericSignature *genericSig) {
15121502
if (!genericSig)
15131503
return AllocationArena::Permanent;
@@ -1749,6 +1739,19 @@ void ProtocolDecl::setDefaultAssociatedConformanceWitness(
17491739
(void)pair;
17501740
}
17511741

1742+
void ASTContext::getVisibleTopLevelModuleNames(
1743+
SmallVectorImpl<Identifier> &names) const {
1744+
names.clear();
1745+
for (auto &importer : getImpl().ModuleLoaders)
1746+
importer->collectVisibleTopLevelModuleNames(names);
1747+
1748+
// Sort and unique.
1749+
std::sort(names.begin(), names.end(), [](Identifier LHS, Identifier RHS) {
1750+
return LHS.str().compare_lower(RHS.str()) < 0;
1751+
});
1752+
names.erase(std::unique(names.begin(), names.end()), names.end());
1753+
}
1754+
17521755
bool ASTContext::canImportModule(std::pair<Identifier, SourceLoc> ModulePath) {
17531756
// If this module has already been successfully imported, it is importable.
17541757
if (getLoadedModule(ModulePath) != nullptr)

lib/ClangImporter/ClangImporter.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1494,9 +1494,22 @@ ClangImporter::emitBridgingPCH(StringRef headerPath,
14941494
return false;
14951495
}
14961496

1497+
void ClangImporter::collectVisibleTopLevelModuleNames(
1498+
SmallVectorImpl<Identifier> &names) const {
1499+
SmallVector<clang::Module *, 32> Modules;
1500+
Impl.getClangPreprocessor().getHeaderSearchInfo().collectAllModules(Modules);
1501+
for (auto &M : Modules) {
1502+
if (!M->isAvailable())
1503+
continue;
1504+
1505+
names.push_back(
1506+
Impl.SwiftContext.getIdentifier(M->getTopLevelModuleName()));
1507+
}
1508+
}
1509+
14971510
void ClangImporter::collectSubModuleNames(
14981511
ArrayRef<std::pair<Identifier, SourceLoc>> path,
1499-
std::vector<std::string> &names) {
1512+
std::vector<std::string> &names) const {
15001513
auto &clangHeaderSearch = Impl.getClangPreprocessor().getHeaderSearchInfo();
15011514

15021515
// Look up the top-level module first.

lib/DWARFImporter/DWARFImporter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ DWARFImporter::DWARFImporter(ASTContext &ctx,
142142

143143
DWARFImporter::~DWARFImporter() { delete &Impl; }
144144

145+
void DWARFImporter::collectVisibleTopLevelModuleNames(
146+
SmallVectorImpl<Identifier> &names) const {}
147+
145148
bool DWARFImporter::canImportModule(std::pair<Identifier, SourceLoc> named) {
146149
return false;
147150
}

lib/Frontend/ParseableInterfaceModuleLoader.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,3 +1377,10 @@ bool ParseableInterfaceModuleLoader::buildSwiftModuleFromSwiftInterface(
13771377
return builder.buildSwiftModule(OutPath, /*shouldSerializeDeps*/true,
13781378
/*ModuleBuffer*/nullptr);
13791379
}
1380+
1381+
void ParseableInterfaceModuleLoader::collectVisibleTopLevelModuleNames(
1382+
SmallVectorImpl<Identifier> &names) const {
1383+
collectVisibleTopLevelModuleNamesImpl(
1384+
names,
1385+
file_types::getExtension(file_types::TY_SwiftParseableInterfaceFile));
1386+
}

lib/IDE/CodeCompletion.cpp

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "swift/Parse/CodeCompletionCallbacks.h"
3434
#include "swift/Sema/IDETypeChecking.h"
3535
#include "swift/Syntax/SyntaxKind.h"
36+
#include "swift/Strings.h"
3637
#include "swift/Subsystems.h"
3738
#include "clang/AST/ASTContext.h"
3839
#include "clang/AST/Attr.h"
@@ -1755,35 +1756,21 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
17551756
}
17561757

17571758
void addImportModuleNames() {
1758-
// FIXME: Add user-defined swift modules
1759-
SmallVector<StringRef, 20> ModuleNames;
1760-
1761-
// Collect clang module names.
1762-
{
1763-
SmallVector<clang::Module*, 20> ClangModules;
1764-
Ctx.getVisibleTopLevelClangModules(ClangModules);
1765-
for (auto *M : ClangModules) {
1766-
if (!M->isAvailable())
1767-
continue;
1768-
if (M->getTopLevelModuleName().startswith("_"))
1769-
continue;
1770-
if (M->getTopLevelModuleName() == Ctx.SwiftShimsModuleName.str())
1771-
continue;
1772-
1773-
ModuleNames.push_back(M->getTopLevelModuleName());
1774-
}
1775-
}
1776-
1777-
std::sort(ModuleNames.begin(), ModuleNames.end(),
1778-
[](StringRef LHS, StringRef RHS) {
1779-
return LHS.compare_lower(RHS) < 0;
1780-
});
1759+
SmallVector<Identifier, 0> ModuleNames;
1760+
Ctx.getVisibleTopLevelModuleNames(ModuleNames);
17811761

17821762
llvm::StringSet<> ImportedModules;
17831763
collectImportedModules(ImportedModules);
17841764

1765+
auto mainModuleName = CurrDeclContext->getParentModule()->getName();
17851766
for (auto ModuleName : ModuleNames) {
1786-
auto MD = ModuleDecl::create(Ctx.getIdentifier(ModuleName), Ctx);
1767+
if (ModuleName.str().startswith("_") ||
1768+
ModuleName == mainModuleName ||
1769+
ModuleName == Ctx.SwiftShimsModuleName ||
1770+
ModuleName.str() == SWIFT_ONONE_SUPPORT)
1771+
continue;
1772+
1773+
auto MD = ModuleDecl::create(ModuleName, Ctx);
17871774
CodeCompletionResultBuilder Builder(
17881775
Sink,
17891776
CodeCompletionResult::ResultKind::Declaration,

lib/Sema/SourceLoader.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ class SkipNonTransparentFunctions : public DelayedParsingCallbacks {
7070

7171
} // unnamed namespace
7272

73+
void SourceLoader::collectVisibleTopLevelModuleNames(
74+
SmallVectorImpl<Identifier> &names) const {
75+
// TODO: Implement?
76+
}
77+
7378
bool SourceLoader::canImportModule(std::pair<Identifier, SourceLoc> ID) {
7479
// Search the memory buffers to see if we can find this file on disk.
7580
FileOrError inputFileOrError = findModule(Ctx, ID.first.str(),

0 commit comments

Comments
 (0)