[libclang/C++] Fix clang_File_isEqual for in-memory files (#135773)

Add tests for `clang_File_isEqual` (on-disk and in-memory)
This commit is contained in:
Jannick Kremer 2025-04-23 18:41:29 +02:00 committed by GitHub
parent ea5449ddd5
commit d7215c0ee2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 52 additions and 1 deletions

View File

@ -683,6 +683,8 @@ clang-format
libclang
--------
- Fixed a bug in ``clang_File_isEqual`` that sometimes led to different
in-memory files to be considered as equal.
- Added ``clang_visitCXXMethods``, which allows visiting the methods
of a class.
- Added ``clang_getFullyQualifiedName``, which provides fully qualified type names as

View File

@ -5170,7 +5170,7 @@ int clang_File_isEqual(CXFile file1, CXFile file2) {
FileEntryRef FEnt1 = *cxfile::getFileEntryRef(file1);
FileEntryRef FEnt2 = *cxfile::getFileEntryRef(file2);
return FEnt1.getUniqueID() == FEnt2.getUniqueID();
return FEnt1 == FEnt2;
}
CXString clang_File_tryGetRealPathName(CXFile SFile) {

View File

@ -1410,3 +1410,52 @@ TEST_F(LibclangRewriteTest, RewriteRemove) {
ASSERT_EQ(clang_CXRewriter_overwriteChangedFiles(Rew), 0);
EXPECT_EQ(getFileContent(Filename), "int () { return 0; }");
}
TEST_F(LibclangParseTest, FileEqual) {
std::string AInc = "a.inc", BInc = "b.inc", Main = "main.cpp";
WriteFile(Main, "int a[] = {\n"
" #include \"a.inc\"\n"
"};\n"
"int b[] = {\n"
" #include \"b.inc\"\n"
"};");
WriteFile(AInc, "1,2,3");
WriteFile(BInc, "1,2,3");
ClangTU = clang_parseTranslationUnit(Index, Main.c_str(), nullptr, 0, nullptr,
0, TUFlags);
CXFile AFile = clang_getFile(ClangTU, AInc.c_str()),
AFile2 = clang_getFile(ClangTU, AInc.c_str()),
BFile = clang_getFile(ClangTU, BInc.c_str()),
MainFile = clang_getFile(ClangTU, Main.c_str());
ASSERT_FALSE(clang_File_isEqual(MainFile, AFile));
ASSERT_FALSE(clang_File_isEqual(AFile, BFile));
ASSERT_TRUE(clang_File_isEqual(AFile, AFile2));
}
TEST_F(LibclangParseTest, FileEqualInMemory) {
std::string AInc = "a.inc", BInc = "b.inc", Main = "main.cpp";
MapUnsavedFile(Main, "int a[] = {\n"
" #include \"a.inc\"\n"
"};\n"
"int b[] = {\n"
" #include \"b.inc\"\n"
"};");
MapUnsavedFile(AInc, "1,2,3");
MapUnsavedFile(BInc, "1,2,3");
ClangTU = clang_parseTranslationUnit(Index, UnsavedFiles[0].Filename, nullptr,
0, &UnsavedFiles.front(),
UnsavedFiles.size(), TUFlags);
CXFile AFile = clang_getFile(ClangTU, UnsavedFiles[1].Filename),
AFile2 = clang_getFile(ClangTU, UnsavedFiles[1].Filename),
BFile = clang_getFile(ClangTU, UnsavedFiles[2].Filename),
MainFile = clang_getFile(ClangTU, UnsavedFiles[0].Filename);
ASSERT_FALSE(clang_File_isEqual(MainFile, AFile));
ASSERT_FALSE(clang_File_isEqual(AFile, BFile));
ASSERT_TRUE(clang_File_isEqual(AFile, AFile2));
}