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>.

llvm-svn: 149199
This commit is contained in:
Douglas Gregor
2012-01-29 18:15:03 +00:00
parent 279a6c3747
commit 07f4357b44
15 changed files with 157 additions and 3 deletions

View File

@@ -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<Module *> &Modules);
private:
/// \brief Retrieve a module with the given name, which may be part of the
/// given framework.

View File

@@ -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<std::pair<IdentifierInfo*, SourceLocation> > ModuleIdPath;
typedef llvm::ArrayRef<std::pair<IdentifierInfo*, SourceLocation> >
ModuleIdPath;
/// \brief Abstract interface for a module loader.
///

View File

@@ -211,6 +211,10 @@ public:
/// \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(); }
};
}

View File

@@ -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,

View File

@@ -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<const IdentifierInfo *, clang::Module *>::iterator Known

View File

@@ -953,3 +953,58 @@ HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir) {
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());
}
}

View File

@@ -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();

View File

@@ -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<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;

View File

@@ -0,0 +1,2 @@
int *getFrameworkVersion();

View File

@@ -0,0 +1,2 @@
int *getA();

View File

@@ -0,0 +1 @@
int *getAExtensions();

View File

@@ -0,0 +1,7 @@
module LibA {
header "a.h"
module Extensions {
header "a_extensions.h"
}
}

View File

@@ -0,0 +1,4 @@
module nested {
header "nested.h"
}

View File

@@ -0,0 +1 @@
int *getNested();

View File

@@ -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)