r329515 - [libclang] Add clang_File_tryGetRealPathName
Fangrui Song via cfe-commits
cfe-commits at lists.llvm.org
Sat Apr 7 13:50:35 PDT 2018
Author: maskray
Date: Sat Apr 7 13:50:35 2018
New Revision: 329515
URL: http://llvm.org/viewvc/llvm-project?rev=329515&view=rev
Log:
[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 <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 "/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
Modified:
cfe/trunk/include/clang-c/Index.h
cfe/trunk/tools/libclang/CIndex.cpp
cfe/trunk/tools/libclang/libclang.exports
cfe/trunk/unittests/libclang/LibclangTest.cpp
Modified: cfe/trunk/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=329515&r1=329514&r2=329515&view=diff
==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Sat Apr 7 13:50:35 2018
@@ -425,6 +425,13 @@ CINDEX_LINKAGE const char *clang_getFile
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);
+
+/**
* @}
*/
Modified: cfe/trunk/tools/libclang/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndex.cpp?rev=329515&r1=329514&r2=329515&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndex.cpp (original)
+++ cfe/trunk/tools/libclang/CIndex.cpp Sat Apr 7 13:50:35 2018
@@ -4249,6 +4249,14 @@ int clang_File_isEqual(CXFile file1, CXF
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.
//===----------------------------------------------------------------------===//
Modified: cfe/trunk/tools/libclang/libclang.exports
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/libclang.exports?rev=329515&r1=329514&r2=329515&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/libclang.exports (original)
+++ cfe/trunk/tools/libclang/libclang.exports Sat Apr 7 13:50:35 2018
@@ -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
Modified: cfe/trunk/unittests/libclang/LibclangTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/libclang/LibclangTest.cpp?rev=329515&r1=329514&r2=329515&view=diff
==============================================================================
--- cfe/trunk/unittests/libclang/LibclangTest.cpp (original)
+++ cfe/trunk/unittests/libclang/LibclangTest.cpp Sat Apr 7 13:50:35 2018
@@ -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;";
More information about the cfe-commits
mailing list