Skip to content

Commit 62dcfb1

Browse files
committed
[libclang/C++] Fix clang_File_isEqual for in-memory files
Add tests for clang_File_isEqual (on-disk and in-memory)
1 parent f04bfbc commit 62dcfb1

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

clang/unittests/libclang/LibclangTest.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,3 +1410,52 @@ TEST_F(LibclangRewriteTest, RewriteRemove) {
14101410
ASSERT_EQ(clang_CXRewriter_overwriteChangedFiles(Rew), 0);
14111411
EXPECT_EQ(getFileContent(Filename), "int () { return 0; }");
14121412
}
1413+
1414+
TEST_F(LibclangParseTest, FileEqual) {
1415+
std::string AInc = "a.inc", BInc = "b.inc", Main = "main.cpp";
1416+
WriteFile(Main, "int a[] = {\n"
1417+
" #include \"a.inc\"\n"
1418+
"};\n"
1419+
"int b[] = {\n"
1420+
" #include \"b.inc\"\n"
1421+
"};");
1422+
WriteFile(AInc, "1,2,3");
1423+
WriteFile(BInc, "1,2,3");
1424+
1425+
ClangTU = clang_parseTranslationUnit(Index, Main.c_str(), nullptr, 0, nullptr,
1426+
0, TUFlags);
1427+
1428+
CXFile AFile = clang_getFile(ClangTU, AInc.c_str()),
1429+
AFile2 = clang_getFile(ClangTU, AInc.c_str()),
1430+
BFile = clang_getFile(ClangTU, BInc.c_str()),
1431+
MainFile = clang_getFile(ClangTU, Main.c_str());
1432+
1433+
ASSERT_FALSE(clang_File_isEqual(MainFile, AFile));
1434+
ASSERT_FALSE(clang_File_isEqual(AFile, BFile));
1435+
ASSERT_TRUE(clang_File_isEqual(AFile, AFile2));
1436+
}
1437+
1438+
TEST_F(LibclangParseTest, FileEqualInMemory) {
1439+
std::string AInc = "a.inc", BInc = "b.inc", Main = "main.cpp";
1440+
MapUnsavedFile(Main, "int a[] = {\n"
1441+
" #include \"a.inc\"\n"
1442+
"};\n"
1443+
"int b[] = {\n"
1444+
" #include \"b.inc\"\n"
1445+
"};");
1446+
MapUnsavedFile(AInc, "1,2,3");
1447+
MapUnsavedFile(BInc, "1,2,3");
1448+
1449+
ClangTU = clang_parseTranslationUnit(Index, UnsavedFiles[0].Filename, nullptr,
1450+
0, &UnsavedFiles.front(),
1451+
UnsavedFiles.size(), TUFlags);
1452+
1453+
CXFile AFile = clang_getFile(ClangTU, UnsavedFiles[1].Filename),
1454+
AFile2 = clang_getFile(ClangTU, UnsavedFiles[1].Filename),
1455+
BFile = clang_getFile(ClangTU, UnsavedFiles[2].Filename),
1456+
MainFile = clang_getFile(ClangTU, UnsavedFiles[0].Filename);
1457+
1458+
ASSERT_FALSE(clang_File_isEqual(MainFile, AFile));
1459+
ASSERT_FALSE(clang_File_isEqual(AFile, BFile));
1460+
ASSERT_TRUE(clang_File_isEqual(AFile, AFile2));
1461+
}

0 commit comments

Comments
 (0)