Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,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.

Expand Down
2 changes: 1 addition & 1 deletion clang/tools/libclang/CIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
49 changes: 49 additions & 0 deletions clang/unittests/libclang/LibclangTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Loading