[cfe-commits] r149199 - in /cfe/trunk: include/clang/Lex/ include/clang/Sema/ lib/Frontend/ lib/Lex/ lib/Parse/ lib/Sema/ test/Index/ test/Index/Inputs/Frameworks/ test/Index/Inputs/Frameworks/Framework.framework/ test/Index/Inputs/Frameworks/Framework.framework/Headers/ test/Index/Inputs/Headers/ test/Index/Inputs/Headers/nested/

Douglas Gregor dgregor at apple.com
Sun Jan 29 10:15:03 PST 2012


Author: dgregor
Date: Sun Jan 29 12:15:03 2012
New Revision: 149199

URL: http://llvm.org/viewvc/llvm-project?rev=149199&view=rev
Log:
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>.


Added:
    cfe/trunk/test/Index/Inputs/Frameworks/
    cfe/trunk/test/Index/Inputs/Frameworks/Framework.framework/
    cfe/trunk/test/Index/Inputs/Frameworks/Framework.framework/Headers/
    cfe/trunk/test/Index/Inputs/Frameworks/Framework.framework/Headers/Framework.h   (with props)
    cfe/trunk/test/Index/Inputs/Headers/
    cfe/trunk/test/Index/Inputs/Headers/a.h   (with props)
    cfe/trunk/test/Index/Inputs/Headers/a_extensions.h   (with props)
    cfe/trunk/test/Index/Inputs/Headers/module.map
    cfe/trunk/test/Index/Inputs/Headers/nested/
    cfe/trunk/test/Index/Inputs/Headers/nested/module.map
    cfe/trunk/test/Index/Inputs/Headers/nested/nested.h   (with props)
    cfe/trunk/test/Index/complete-modules.m
Modified:
    cfe/trunk/include/clang/Lex/HeaderSearch.h
    cfe/trunk/include/clang/Lex/ModuleLoader.h
    cfe/trunk/include/clang/Lex/ModuleMap.h
    cfe/trunk/include/clang/Sema/Sema.h
    cfe/trunk/lib/Frontend/CompilerInstance.cpp
    cfe/trunk/lib/Lex/HeaderSearch.cpp
    cfe/trunk/lib/Parse/Parser.cpp
    cfe/trunk/lib/Sema/SemaCodeComplete.cpp

Modified: cfe/trunk/include/clang/Lex/HeaderSearch.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/HeaderSearch.h?rev=149199&r1=149198&r2=149199&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/HeaderSearch.h (original)
+++ cfe/trunk/include/clang/Lex/HeaderSearch.h Sun Jan 29 12:15:03 2012
@@ -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.

Modified: cfe/trunk/include/clang/Lex/ModuleLoader.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/ModuleLoader.h?rev=149199&r1=149198&r2=149199&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/ModuleLoader.h (original)
+++ cfe/trunk/include/clang/Lex/ModuleLoader.h Sun Jan 29 12:15:03 2012
@@ -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.
 ///

Modified: cfe/trunk/include/clang/Lex/ModuleMap.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/ModuleMap.h?rev=149199&r1=149198&r2=149199&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/ModuleMap.h (original)
+++ cfe/trunk/include/clang/Lex/ModuleMap.h Sun Jan 29 12:15:03 2012
@@ -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(); }
 };
   
 }

Modified: cfe/trunk/include/clang/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=149199&r1=149198&r2=149199&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/Sema.h (original)
+++ cfe/trunk/include/clang/Sema/Sema.h Sun Jan 29 12:15:03 2012
@@ -6159,6 +6159,7 @@
     PCC_LocalDeclarationSpecifiers
   };
 
+  void CodeCompleteModuleImport(SourceLocation ImportLoc, ModuleIdPath Path);
   void CodeCompleteOrdinaryName(Scope *S,
                                 ParserCompletionContext CompletionContext);
   void CodeCompleteDeclSpec(Scope *S, DeclSpec &DS,

Modified: cfe/trunk/lib/Frontend/CompilerInstance.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CompilerInstance.cpp?rev=149199&r1=149198&r2=149199&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/CompilerInstance.cpp (original)
+++ cfe/trunk/lib/Frontend/CompilerInstance.cpp Sun Jan 29 12:15:03 2012
@@ -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

Modified: cfe/trunk/lib/Lex/HeaderSearch.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/HeaderSearch.cpp?rev=149199&r1=149198&r2=149199&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/HeaderSearch.cpp (original)
+++ cfe/trunk/lib/Lex/HeaderSearch.cpp Sun Jan 29 12:15:03 2012
@@ -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());
+  }
+}
+

Modified: cfe/trunk/lib/Parse/Parser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/Parser.cpp?rev=149199&r1=149198&r2=149199&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/Parser.cpp (original)
+++ cfe/trunk/lib/Parse/Parser.cpp Sun Jan 29 12:15:03 2012
@@ -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();

Modified: cfe/trunk/lib/Sema/SemaCodeComplete.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaCodeComplete.cpp?rev=149199&r1=149198&r2=149199&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaCodeComplete.cpp (original)
+++ cfe/trunk/lib/Sema/SemaCodeComplete.cpp Sun Jan 29 12:15:03 2012
@@ -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;

Added: cfe/trunk/test/Index/Inputs/Frameworks/Framework.framework/Headers/Framework.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/Inputs/Frameworks/Framework.framework/Headers/Framework.h?rev=149199&view=auto
==============================================================================
--- cfe/trunk/test/Index/Inputs/Frameworks/Framework.framework/Headers/Framework.h (added)
+++ cfe/trunk/test/Index/Inputs/Frameworks/Framework.framework/Headers/Framework.h Sun Jan 29 12:15:03 2012
@@ -0,0 +1,2 @@
+int *getFrameworkVersion();
+

Propchange: cfe/trunk/test/Index/Inputs/Frameworks/Framework.framework/Headers/Framework.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/trunk/test/Index/Inputs/Frameworks/Framework.framework/Headers/Framework.h
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cfe/trunk/test/Index/Inputs/Frameworks/Framework.framework/Headers/Framework.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: cfe/trunk/test/Index/Inputs/Headers/a.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/Inputs/Headers/a.h?rev=149199&view=auto
==============================================================================
--- cfe/trunk/test/Index/Inputs/Headers/a.h (added)
+++ cfe/trunk/test/Index/Inputs/Headers/a.h Sun Jan 29 12:15:03 2012
@@ -0,0 +1,2 @@
+int *getA();
+

Propchange: cfe/trunk/test/Index/Inputs/Headers/a.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/trunk/test/Index/Inputs/Headers/a.h
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cfe/trunk/test/Index/Inputs/Headers/a.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: cfe/trunk/test/Index/Inputs/Headers/a_extensions.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/Inputs/Headers/a_extensions.h?rev=149199&view=auto
==============================================================================
--- cfe/trunk/test/Index/Inputs/Headers/a_extensions.h (added)
+++ cfe/trunk/test/Index/Inputs/Headers/a_extensions.h Sun Jan 29 12:15:03 2012
@@ -0,0 +1 @@
+int *getAExtensions();

Propchange: cfe/trunk/test/Index/Inputs/Headers/a_extensions.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/trunk/test/Index/Inputs/Headers/a_extensions.h
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cfe/trunk/test/Index/Inputs/Headers/a_extensions.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: cfe/trunk/test/Index/Inputs/Headers/module.map
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/Inputs/Headers/module.map?rev=149199&view=auto
==============================================================================
--- cfe/trunk/test/Index/Inputs/Headers/module.map (added)
+++ cfe/trunk/test/Index/Inputs/Headers/module.map Sun Jan 29 12:15:03 2012
@@ -0,0 +1,7 @@
+module LibA {
+  header "a.h"
+  module Extensions { 
+    header "a_extensions.h"
+  }
+}
+

Added: cfe/trunk/test/Index/Inputs/Headers/nested/module.map
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/Inputs/Headers/nested/module.map?rev=149199&view=auto
==============================================================================
--- cfe/trunk/test/Index/Inputs/Headers/nested/module.map (added)
+++ cfe/trunk/test/Index/Inputs/Headers/nested/module.map Sun Jan 29 12:15:03 2012
@@ -0,0 +1,4 @@
+module nested {
+  header "nested.h"
+}
+

Added: cfe/trunk/test/Index/Inputs/Headers/nested/nested.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/Inputs/Headers/nested/nested.h?rev=149199&view=auto
==============================================================================
--- cfe/trunk/test/Index/Inputs/Headers/nested/nested.h (added)
+++ cfe/trunk/test/Index/Inputs/Headers/nested/nested.h Sun Jan 29 12:15:03 2012
@@ -0,0 +1 @@
+int *getNested();

Propchange: cfe/trunk/test/Index/Inputs/Headers/nested/nested.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/trunk/test/Index/Inputs/Headers/nested/nested.h
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cfe/trunk/test/Index/Inputs/Headers/nested/nested.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: cfe/trunk/test/Index/complete-modules.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/complete-modules.m?rev=149199&view=auto
==============================================================================
--- cfe/trunk/test/Index/complete-modules.m (added)
+++ cfe/trunk/test/Index/complete-modules.m Sun Jan 29 12:15:03 2012
@@ -0,0 +1,14 @@
+// Note: the run lines follow their respective tests, since line/column
+// matter in this test.
+
+ at 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)
+





More information about the cfe-commits mailing list