[PATCH] D74790: [Sema][CodeComplete] Handle symlinks for include code completion

David Goldman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 18 13:58:31 PST 2020


dgoldman updated this revision to Diff 245256.
dgoldman added a comment.

- Add test case


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D74790/new/

https://reviews.llvm.org/D74790

Files:
  clang/lib/Sema/SemaCodeComplete.cpp
  clang/test/CodeCompletion/included-symlinks.cpp


Index: clang/test/CodeCompletion/included-symlinks.cpp
===================================================================
--- /dev/null
+++ clang/test/CodeCompletion/included-symlinks.cpp
@@ -0,0 +1,15 @@
+// RUN: rm -rf %t && mkdir -p %t/real/myproj && mkdir -p %t/links
+// RUN: touch %t/real/foo.h && ln -s %t/real/foo.h %t/links/foo.h
+// RUN: touch %t/real/foobar.h && ln -s %t/real/foobar.h %t/links/foobar.h
+// RUN: touch %t/real/myproj/test.h && ln -s %t/real/myproj %t/links/myproj
+
+// Suggest symlinked header files.
+#include "foo.h"
+// RUN: %clang -fsyntax-only -I%t/links -Xclang -code-completion-at=%s:7:13 %s | FileCheck -check-prefix=CHECK-1 %s
+// CHECK-1: foo.h"
+// CHECK-1: foobar.h"
+
+// Suggest symlinked folder.
+#include "mypr"
+// RUN: %clang -fsyntax-only -I%t/links -Xclang -code-completion-at=%s:13:13 %s | FileCheck -check-prefix=CHECK-2 %s
+// CHECK-2: myproj/
Index: clang/lib/Sema/SemaCodeComplete.cpp
===================================================================
--- clang/lib/Sema/SemaCodeComplete.cpp
+++ clang/lib/Sema/SemaCodeComplete.cpp
@@ -8776,7 +8776,19 @@
       if (++Count == 2500) // If we happen to hit a huge directory,
         break;             // bail out early so we're not too slow.
       StringRef Filename = llvm::sys::path::filename(It->path());
-      switch (It->type()) {
+
+      // We need to manually resolve symlinks since the directory_iterator
+      // doesn't do it for us. Alternatively we could use a heuristic such as
+      // file extension, but this should be okay as long as there aren't many
+      // symlinks.
+      auto Type = It->type();
+      if (Type == llvm::sys::fs::file_type::symlink_file) {
+        auto FileStatus = FS.status(It->path());
+        if (FileStatus) {
+          Type = FileStatus->getType();
+        }
+      }
+      switch (Type) {
       case llvm::sys::fs::file_type::directory_file:
         // All entries in a framework directory must have a ".framework" suffix,
         // but the suffix does not appear in the source code's include/import.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D74790.245256.patch
Type: text/x-patch
Size: 2070 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200218/f4f3db53/attachment.bin>


More information about the cfe-commits mailing list