[PATCH] D58062: Support framework import/include auto-completion

David Goldman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 11 09:20:02 PST 2019


dgoldman created this revision.
dgoldman added a reviewer: sammccall.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Frameworks filesystem representations:

  UIKit.framework/Headers/%header%

Framework import format:

  #import <UIKit/%header%>

Thus the completion code must map the input format of <UIKit/> to
the path of UIKit.framework/Headers as well as strip the
".framework" suffix when auto-completing the framework name.


Repository:
  rC Clang

https://reviews.llvm.org/D58062

Files:
  lib/Sema/SemaCodeComplete.cpp


Index: lib/Sema/SemaCodeComplete.cpp
===================================================================
--- lib/Sema/SemaCodeComplete.cpp
+++ lib/Sema/SemaCodeComplete.cpp
@@ -8371,10 +8371,31 @@
   };
 
   // Helper: scans IncludeDir for nice files, and adds results for each.
-  auto AddFilesFromIncludeDir = [&](StringRef IncludeDir, bool IsSystem) {
+  auto AddFilesFromIncludeDir = [&](StringRef IncludeDir, bool IsSystem,
+                                    bool isFramework) {
+    bool stripFrameworkSuffix = false;
     llvm::SmallString<128> Dir = IncludeDir;
-    if (!NativeRelDir.empty())
+    if (isFramework) {
+      if (NativeRelDir.empty()) {
+        stripFrameworkSuffix = true;
+      } else {
+        auto Begin = llvm::sys::path::begin(NativeRelDir);
+        auto End = llvm::sys::path::end(NativeRelDir);
+
+        if (Begin != End) {
+          SmallString<32> Framework = *Begin;
+          Framework += ".framework";
+          llvm::sys::path::append(Dir, Framework, "Headers");
+          llvm::sys::path::append(Dir, ++Begin, End);
+        } else {
+          SmallString<32> Framework(NativeRelDir);
+          Framework += ".framework";
+          llvm::sys::path::append(Dir, Framework, "Headers");
+        }
+      }
+    } else if (!NativeRelDir.empty()) {
       llvm::sys::path::append(Dir, NativeRelDir);
+    }
 
     std::error_code EC;
     unsigned Count = 0;
@@ -8385,7 +8406,12 @@
       StringRef Filename = llvm::sys::path::filename(It->path());
       switch (It->type()) {
       case llvm::sys::fs::file_type::directory_file:
-        AddCompletion(Filename, /*IsDirectory=*/true);
+        if (stripFrameworkSuffix && Filename.endswith(".framework")) {
+          auto FrameworkName = Filename.substr(0, Filename.size() - 10);
+          AddCompletion(FrameworkName, /*IsDirectory=*/true);
+        } else {
+          AddCompletion(Filename, /*IsDirectory=*/true);
+        }
         break;
       case llvm::sys::fs::file_type::regular_file:
         // Only files that really look like headers. (Except in system dirs).
@@ -8413,10 +8439,12 @@
       // header maps are not (currently) enumerable.
       break;
     case DirectoryLookup::LT_NormalDir:
-      AddFilesFromIncludeDir(IncludeDir.getDir()->getName(), IsSystem);
+      AddFilesFromIncludeDir(IncludeDir.getDir()->getName(), IsSystem,
+                             /*IsFramework*/false);
       break;
     case DirectoryLookup::LT_Framework:
-      AddFilesFromIncludeDir(IncludeDir.getFrameworkDir()->getName(), IsSystem);
+      AddFilesFromIncludeDir(IncludeDir.getFrameworkDir()->getName(), IsSystem,
+                             /*IsFramework*/true);
       break;
     }
   };
@@ -8430,7 +8458,7 @@
     // The current directory is on the include path for "quoted" includes.
     auto *CurFile = PP.getCurrentFileLexer()->getFileEntry();
     if (CurFile && CurFile->getDir())
-      AddFilesFromIncludeDir(CurFile->getDir()->getName(), false);
+      AddFilesFromIncludeDir(CurFile->getDir()->getName(), false, false);
     for (const auto &D : make_range(S.quoted_dir_begin(), S.quoted_dir_end()))
       AddFilesFromDirLookup(D, false);
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D58062.186276.patch
Type: text/x-patch
Size: 3177 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190211/fc341614/attachment-0001.bin>


More information about the cfe-commits mailing list