[PATCH] D42893: [libclang] Add clang_File_tryGetRealPathName

Fangrui Song via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Feb 4 11:14:45 PST 2018


MaskRay created this revision.
Herald added a subscriber: cfe-commits.

clang_getFileName() may return a path relative to WorkingDir.
On Arch Linux, during clang_indexTranslationUnit(), clang_getFileName() on
CXIdxIncludedIncludedFileInfo::file may return
"/../lib64/gcc/x86_64-pc-linux-gnu/7.3.0/../../../../include/c++/7.3.0/string",
for `#include <string>`.

I presume WorkingDir is somehow changed to /usr/lib or /usr/include and
clang_getFileName() returns a path relative to WorkingDir.

clang_File_tryGetRealPathName() returns a better file name more useful for
the indexer in this case.


Repository:
  rC Clang

https://reviews.llvm.org/D42893

Files:
  include/clang-c/Index.h
  tools/libclang/CIndex.cpp
  tools/libclang/libclang.exports


Index: tools/libclang/libclang.exports
===================================================================
--- tools/libclang/libclang.exports
+++ tools/libclang/libclang.exports
@@ -46,6 +46,7 @@
 clang_Cursor_getModule
 clang_Cursor_getStorageClass
 clang_File_isEqual
+clang_File_tryGetRealPathName
 clang_Module_getASTFile
 clang_Module_getParent
 clang_Module_getName
Index: tools/libclang/CIndex.cpp
===================================================================
--- tools/libclang/CIndex.cpp
+++ tools/libclang/CIndex.cpp
@@ -4249,6 +4249,14 @@
   return FEnt1->getUniqueID() == FEnt2->getUniqueID();
 }
 
+CXString clang_File_tryGetRealPathName(CXFile SFile) {
+  if (!SFile)
+    return cxstring::createNull();
+
+  FileEntry *FEnt = static_cast<FileEntry *>(SFile);
+  return cxstring::createRef(FEnt->tryGetRealPathName());
+}
+
 //===----------------------------------------------------------------------===//
 // CXCursor Operations.
 //===----------------------------------------------------------------------===//
Index: include/clang-c/Index.h
===================================================================
--- include/clang-c/Index.h
+++ include/clang-c/Index.h
@@ -424,6 +424,13 @@
  */
 CINDEX_LINKAGE int clang_File_isEqual(CXFile file1, CXFile file2);
 
+/**
+ * \brief Returns the real path name of \c file.
+ *
+ * An empty string may be returned. Use \c clang_getFileName() in that case.
+ */
+CINDEX_LINKAGE CXString clang_File_tryGetRealPathName(CXFile file);
+
 /**
  * @}
  */


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D42893.132772.patch
Type: text/x-patch
Size: 1515 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180204/247ce409/attachment.bin>


More information about the cfe-commits mailing list