From: Douglas Gregor Date: Sun, 29 Jan 2012 18:15:03 +0000 (+0000) Subject: Implement code completion support for module import declarations, e.g., X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c5b2e58840748145d1706c1d1481369d1863fabf;p=clang Implement code completion support for module import declarations, e.g., @import or @import std. Addresses . 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 d1cd8af49a..c5969489f5 100644 --- a/include/clang/Lex/HeaderSearch.h +++ b/include/clang/Lex/HeaderSearch.h @@ -391,7 +391,6 @@ public: /// \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 @@ public: /// \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 &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 b2fb722b94..36d03c0aa2 100644 --- a/include/clang/Lex/ModuleLoader.h +++ b/include/clang/Lex/ModuleLoader.h @@ -24,7 +24,8 @@ class IdentifierInfo; /// \brief A sequence of identifier/location pairs used to describe a particular /// module or submodule, e.g., std.vector. -typedef llvm::ArrayRef > ModuleIdPath; +typedef llvm::ArrayRef > + ModuleIdPath; /// \brief Abstract interface for a module loader. /// diff --git a/include/clang/Lex/ModuleMap.h b/include/clang/Lex/ModuleMap.h index 64358c83e5..3e794f5c00 100644 --- a/include/clang/Lex/ModuleMap.h +++ b/include/clang/Lex/ModuleMap.h @@ -211,6 +211,10 @@ public: /// \brief Dump the contents of the module map, for debugging purposes. void dump(); + + typedef llvm::StringMap::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 e5e86772c5..a221df4323 100644 --- a/include/clang/Sema/Sema.h +++ b/include/clang/Sema/Sema.h @@ -6159,6 +6159,7 @@ public: 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 7f22247e57..cc8d3b48a2 100644 --- a/lib/Frontend/CompilerInstance.cpp +++ b/lib/Frontend/CompilerInstance.cpp @@ -1102,7 +1102,6 @@ Module *CompilerInstance::loadModule(SourceLocation ImportLoc, 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::iterator Known diff --git a/lib/Lex/HeaderSearch.cpp b/lib/Lex/HeaderSearch.cpp index 7c981f852b..e56eb6c915 100644 --- a/lib/Lex/HeaderSearch.cpp +++ b/lib/Lex/HeaderSearch.cpp @@ -953,3 +953,58 @@ HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir) { return LMM_InvalidModuleMap; } +void HeaderSearch::collectAllModules(llvm::SmallVectorImpl &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 5afb0fdf3c..9ec9f9e8bb 100644 --- a/lib/Parse/Parser.cpp +++ b/lib/Parse/Parser.cpp @@ -1595,6 +1595,13 @@ Parser::DeclGroupPtrTy Parser::ParseModuleImport(SourceLocation AtLoc) { // 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 e4c2bdd031..cdad740851 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 @@ static void MaybeAddOverrideCalls(Sema &S, DeclContext *InContext, } } +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 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 0000000000..3e3830a2ba --- /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 0000000000..33c3417676 --- /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 0000000000..2155787549 --- /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 0000000000..e875210662 --- /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 0000000000..bd239f1fc1 --- /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 0000000000..01716d12ec --- /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 0000000000..a8737216e2 --- /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) +