From e46ac5fb9dcbfd805bf1181dc6babcae0a5c9210 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sat, 7 Apr 2018 20:50:35 +0000 Subject: [PATCH] [libclang] Add clang_File_tryGetRealPathName Summary: 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 `. 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 "/usr/include/c++/7.3.0/string" which is more useful for the indexer in this case. Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D42893 llvm-svn: 329515 --- clang/include/clang-c/Index.h | 7 +++++++ clang/tools/libclang/CIndex.cpp | 8 ++++++++ clang/tools/libclang/libclang.exports | 1 + clang/unittests/libclang/LibclangTest.cpp | 15 +++++++++++++++ 4 files changed, 31 insertions(+) diff --git a/clang/include/clang-c/Index.h b/clang/include/clang-c/Index.h index cc43b6a53599..e8dcfdf03504 100644 --- a/clang/include/clang-c/Index.h +++ b/clang/include/clang-c/Index.h @@ -424,6 +424,13 @@ CINDEX_LINKAGE const char *clang_getFileContents(CXTranslationUnit tu, */ 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); + /** * @} */ diff --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp index 743057182962..480e00eb962b 100644 --- a/clang/tools/libclang/CIndex.cpp +++ b/clang/tools/libclang/CIndex.cpp @@ -4249,6 +4249,14 @@ int clang_File_isEqual(CXFile file1, CXFile file2) { return FEnt1->getUniqueID() == FEnt2->getUniqueID(); } +CXString clang_File_tryGetRealPathName(CXFile SFile) { + if (!SFile) + return cxstring::createNull(); + + FileEntry *FEnt = static_cast(SFile); + return cxstring::createRef(FEnt->tryGetRealPathName()); +} + //===----------------------------------------------------------------------===// // CXCursor Operations. //===----------------------------------------------------------------------===// diff --git a/clang/tools/libclang/libclang.exports b/clang/tools/libclang/libclang.exports index 67e4b4334b7e..222598dcd94e 100644 --- a/clang/tools/libclang/libclang.exports +++ b/clang/tools/libclang/libclang.exports @@ -46,6 +46,7 @@ clang_Cursor_isVariadic clang_Cursor_getModule clang_Cursor_getStorageClass clang_File_isEqual +clang_File_tryGetRealPathName clang_Module_getASTFile clang_Module_getParent clang_Module_getName diff --git a/clang/unittests/libclang/LibclangTest.cpp b/clang/unittests/libclang/LibclangTest.cpp index c44095d54283..17a506762f1f 100644 --- a/clang/unittests/libclang/LibclangTest.cpp +++ b/clang/unittests/libclang/LibclangTest.cpp @@ -482,6 +482,21 @@ public: } }; +TEST_F(LibclangReparseTest, FileName) { + std::string CppName = "main.cpp"; + WriteFile(CppName, "int main() {}"); + ClangTU = clang_parseTranslationUnit(Index, CppName.c_str(), nullptr, 0, + nullptr, 0, TUFlags); + CXFile cxf = clang_getFile(ClangTU, CppName.c_str()); + + CXString cxname = clang_getFileName(cxf); + ASSERT_TRUE(strstr(clang_getCString(cxname), CppName.c_str())); + clang_disposeString(cxname); + + cxname = clang_File_tryGetRealPathName(cxf); + ASSERT_TRUE(strstr(clang_getCString(cxname), CppName.c_str())); + clang_disposeString(cxname); +} TEST_F(LibclangReparseTest, Reparse) { const char *HeaderTop = "#ifndef H\n#define H\nstruct Foo { int bar;";