Implement code completion support for module import declarations, e.g.,
@import <complete with module names here>
or
@import std.<complete with submodule names here>
Addresses <rdar://problem/10710117>.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@149199 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/Lex/HeaderSearch.h b/include/clang/Lex/HeaderSearch.h
index d1cd8af..c596948 100644
--- a/include/clang/Lex/HeaderSearch.h
+++ b/include/clang/Lex/HeaderSearch.h
@@ -391,7 +391,6 @@
/// \param File The header that we wish to map to a module.
Module *findModuleForHeader(const FileEntry *File);
-
/// \brief Read the contents of the given module map file.
///
/// \param File The module map file.
@@ -401,6 +400,11 @@
/// \returns true if an error occurred, false otherwise.
bool loadModuleMapFile(const FileEntry *File);
+ /// \brief Collect the set of all known, top-level modules.
+ ///
+ /// \param Modules Will be filled with the set of known, top-level modules.
+ void collectAllModules(llvm::SmallVectorImpl<Module *> &Modules);
+
private:
/// \brief Retrieve a module with the given name, which may be part of the
/// given framework.
diff --git a/include/clang/Lex/ModuleLoader.h b/include/clang/Lex/ModuleLoader.h
index b2fb722..36d03c0 100644
--- a/include/clang/Lex/ModuleLoader.h
+++ b/include/clang/Lex/ModuleLoader.h
@@ -24,7 +24,8 @@
/// \brief A sequence of identifier/location pairs used to describe a particular
/// module or submodule, e.g., std.vector.
-typedef llvm::ArrayRef<std::pair<IdentifierInfo*, SourceLocation> > ModuleIdPath;
+typedef llvm::ArrayRef<std::pair<IdentifierInfo*, SourceLocation> >
+ ModuleIdPath;
/// \brief Abstract interface for a module loader.
///
diff --git a/include/clang/Lex/ModuleMap.h b/include/clang/Lex/ModuleMap.h
index 64358c8..3e794f5 100644
--- a/include/clang/Lex/ModuleMap.h
+++ b/include/clang/Lex/ModuleMap.h
@@ -211,6 +211,10 @@
/// \brief Dump the contents of the module map, for debugging purposes.
void dump();
+
+ typedef llvm::StringMap<Module *>::const_iterator module_iterator;
+ module_iterator module_begin() const { return Modules.begin(); }
+ module_iterator module_end() const { return Modules.end(); }
};
}
diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h
index e5e8677..a221df4 100644
--- a/include/clang/Sema/Sema.h
+++ b/include/clang/Sema/Sema.h
@@ -6159,6 +6159,7 @@
PCC_LocalDeclarationSpecifiers
};
+ void CodeCompleteModuleImport(SourceLocation ImportLoc, ModuleIdPath Path);
void CodeCompleteOrdinaryName(Scope *S,
ParserCompletionContext CompletionContext);
void CodeCompleteDeclSpec(Scope *S, DeclSpec &DS,
diff --git a/lib/Frontend/CompilerInstance.cpp b/lib/Frontend/CompilerInstance.cpp
index 7f22247..cc8d3b4 100644
--- a/lib/Frontend/CompilerInstance.cpp
+++ b/lib/Frontend/CompilerInstance.cpp
@@ -1102,7 +1102,6 @@
SourceLocation ModuleNameLoc = Path[0].second;
clang::Module *Module = 0;
- const FileEntry *ModuleFile = 0;
// If we don't already have information on this module, load the module now.
llvm::DenseMap<const IdentifierInfo *, clang::Module *>::iterator Known
diff --git a/lib/Lex/HeaderSearch.cpp b/lib/Lex/HeaderSearch.cpp
index 7c981f8..e56eb6c 100644
--- a/lib/Lex/HeaderSearch.cpp
+++ b/lib/Lex/HeaderSearch.cpp
@@ -953,3 +953,58 @@
return LMM_InvalidModuleMap;
}
+void HeaderSearch::collectAllModules(llvm::SmallVectorImpl<Module *> &Modules) {
+ Modules.clear();
+
+ // Load module maps for each of the header search directories.
+ for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
+ if (SearchDirs[Idx].isFramework()) {
+ llvm::error_code EC;
+ llvm::SmallString<128> DirNative;
+ llvm::sys::path::native(SearchDirs[Idx].getFrameworkDir()->getName(),
+ DirNative);
+
+ // Search each of the ".framework" directories to load them as modules.
+ bool IsSystem = SearchDirs[Idx].getDirCharacteristic() != SrcMgr::C_User;
+ for (llvm::sys::fs::directory_iterator Dir(DirNative.str(), EC), DirEnd;
+ Dir != DirEnd && !EC; Dir.increment(EC)) {
+ if (llvm::sys::path::extension(Dir->path()) != ".framework")
+ continue;
+
+ const DirectoryEntry *FrameworkDir = FileMgr.getDirectory(Dir->path());
+ if (!FrameworkDir)
+ continue;
+
+ // Load this framework module.
+ loadFrameworkModule(llvm::sys::path::stem(Dir->path()), FrameworkDir,
+ IsSystem);
+ }
+ continue;
+ }
+
+ // FIXME: Deal with header maps.
+ if (SearchDirs[Idx].isHeaderMap())
+ continue;
+
+ // Try to load a module map file for the search directory.
+ loadModuleMapFile(SearchDirs[Idx].getDir());
+
+ // Try to load module map files for immediate subdirectories of this search
+ // directory.
+ llvm::error_code EC;
+ llvm::SmallString<128> DirNative;
+ llvm::sys::path::native(SearchDirs[Idx].getDir()->getName(), DirNative);
+ for (llvm::sys::fs::directory_iterator Dir(DirNative.str(), EC), DirEnd;
+ Dir != DirEnd && !EC; Dir.increment(EC)) {
+ loadModuleMapFile(Dir->path());
+ }
+ }
+
+ // Populate the list of modules.
+ for (ModuleMap::module_iterator M = ModMap.module_begin(),
+ MEnd = ModMap.module_end();
+ M != MEnd; ++M) {
+ Modules.push_back(M->getValue());
+ }
+}
+
diff --git a/lib/Parse/Parser.cpp b/lib/Parse/Parser.cpp
index 5afb0fd..9ec9f9e 100644
--- a/lib/Parse/Parser.cpp
+++ b/lib/Parse/Parser.cpp
@@ -1595,6 +1595,13 @@
// Parse the module path.
do {
if (!Tok.is(tok::identifier)) {
+ if (Tok.is(tok::code_completion)) {
+ Actions.CodeCompleteModuleImport(ImportLoc, Path);
+ ConsumeCodeCompletionToken();
+ SkipUntil(tok::semi);
+ return DeclGroupPtrTy();
+ }
+
Diag(Tok, diag::err_module_expected_ident);
SkipUntil(tok::semi);
return DeclGroupPtrTy();
diff --git a/lib/Sema/SemaCodeComplete.cpp b/lib/Sema/SemaCodeComplete.cpp
index e4c2bdd..cdad740 100644
--- a/lib/Sema/SemaCodeComplete.cpp
+++ b/lib/Sema/SemaCodeComplete.cpp
@@ -20,6 +20,7 @@
#include "clang/AST/DeclObjC.h"
#include "clang/AST/ExprCXX.h"
#include "clang/AST/ExprObjC.h"
+#include "clang/Lex/HeaderSearch.h"
#include "clang/Lex/MacroInfo.h"
#include "clang/Lex/Preprocessor.h"
#include "llvm/ADT/DenseSet.h"
@@ -3010,6 +3011,57 @@
}
}
+void Sema::CodeCompleteModuleImport(SourceLocation ImportLoc,
+ ModuleIdPath Path) {
+ typedef CodeCompletionResult Result;
+ ResultBuilder Results(*this, CodeCompleter->getAllocator(),
+ CodeCompletionContext::CCC_Other);
+ Results.EnterNewScope();
+
+ CodeCompletionAllocator &Allocator = Results.getAllocator();
+ CodeCompletionBuilder Builder(Allocator);
+ typedef CodeCompletionResult Result;
+ if (Path.empty()) {
+ // Enumerate all top-level modules.
+ llvm::SmallVector<Module *, 8> Modules;
+ PP.getHeaderSearchInfo().collectAllModules(Modules);
+ for (unsigned I = 0, N = Modules.size(); I != N; ++I) {
+ Builder.AddTypedTextChunk(
+ Builder.getAllocator().CopyString(Modules[I]->Name));
+ Results.AddResult(Result(Builder.TakeString(),
+ CCP_Declaration,
+ CXCursor_NotImplemented,
+ Modules[I]->isAvailable()
+ ? CXAvailability_Available
+ : CXAvailability_NotAvailable));
+ }
+ } else {
+ // Load the named module.
+ Module *Mod = PP.getModuleLoader().loadModule(ImportLoc, Path,
+ Module::AllVisible,
+ /*IsInclusionDirective=*/false);
+ // Enumerate submodules.
+ if (Mod) {
+ for (Module::submodule_iterator Sub = Mod->submodule_begin(),
+ SubEnd = Mod->submodule_end();
+ Sub != SubEnd; ++Sub) {
+
+ Builder.AddTypedTextChunk(
+ Builder.getAllocator().CopyString((*Sub)->Name));
+ Results.AddResult(Result(Builder.TakeString(),
+ CCP_Declaration,
+ CXCursor_NotImplemented,
+ (*Sub)->isAvailable()
+ ? CXAvailability_Available
+ : CXAvailability_NotAvailable));
+ }
+ }
+ }
+ Results.ExitScope();
+ HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
+ Results.data(),Results.size());
+}
+
void Sema::CodeCompleteOrdinaryName(Scope *S,
ParserCompletionContext CompletionContext) {
typedef CodeCompletionResult Result;
diff --git a/test/Index/Inputs/Frameworks/Framework.framework/Headers/Framework.h b/test/Index/Inputs/Frameworks/Framework.framework/Headers/Framework.h
new file mode 100644
index 0000000..3e3830a
--- /dev/null
+++ b/test/Index/Inputs/Frameworks/Framework.framework/Headers/Framework.h
@@ -0,0 +1,2 @@
+int *getFrameworkVersion();
+
diff --git a/test/Index/Inputs/Headers/a.h b/test/Index/Inputs/Headers/a.h
new file mode 100644
index 0000000..33c3417
--- /dev/null
+++ b/test/Index/Inputs/Headers/a.h
@@ -0,0 +1,2 @@
+int *getA();
+
diff --git a/test/Index/Inputs/Headers/a_extensions.h b/test/Index/Inputs/Headers/a_extensions.h
new file mode 100644
index 0000000..2155787
--- /dev/null
+++ b/test/Index/Inputs/Headers/a_extensions.h
@@ -0,0 +1 @@
+int *getAExtensions();
diff --git a/test/Index/Inputs/Headers/module.map b/test/Index/Inputs/Headers/module.map
new file mode 100644
index 0000000..e875210
--- /dev/null
+++ b/test/Index/Inputs/Headers/module.map
@@ -0,0 +1,7 @@
+module LibA {
+ header "a.h"
+ module Extensions {
+ header "a_extensions.h"
+ }
+}
+
diff --git a/test/Index/Inputs/Headers/nested/module.map b/test/Index/Inputs/Headers/nested/module.map
new file mode 100644
index 0000000..bd239f1
--- /dev/null
+++ b/test/Index/Inputs/Headers/nested/module.map
@@ -0,0 +1,4 @@
+module nested {
+ header "nested.h"
+}
+
diff --git a/test/Index/Inputs/Headers/nested/nested.h b/test/Index/Inputs/Headers/nested/nested.h
new file mode 100644
index 0000000..01716d1
--- /dev/null
+++ b/test/Index/Inputs/Headers/nested/nested.h
@@ -0,0 +1 @@
+int *getNested();
diff --git a/test/Index/complete-modules.m b/test/Index/complete-modules.m
new file mode 100644
index 0000000..a873721
--- /dev/null
+++ b/test/Index/complete-modules.m
@@ -0,0 +1,14 @@
+// Note: the run lines follow their respective tests, since line/column
+// matter in this test.
+
+@import LibA.Extensions;
+
+// RUN: rm -rf %t
+// RUN: c-index-test -code-completion-at=%s:4:9 -fmodule-cache-path %t -fmodules -F %S/Inputs/Frameworks -I %S/Inputs/Headers %s | FileCheck -check-prefix=CHECK-TOP-LEVEL %s
+// CHECK-TOP-LEVEL: NotImplemented:{TypedText Framework} (50)
+// CHECK-TOP-LEVEL: NotImplemented:{TypedText LibA} (50)
+// CHECK-TOP-LEVEL: NotImplemented:{TypedText nested} (50)
+
+// RUN: c-index-test -code-completion-at=%s:4:14 -fmodule-cache-path %t -fmodules -F %S/Inputs/Frameworks -I %S/Inputs/Headers %s | FileCheck -check-prefix=CHECK-LIBA %s
+// CHECK-LIBA: NotImplemented:{TypedText Extensions} (50)
+