[clang] [libclang/python] Add equality comparison operators for File (PR #130383)
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Wed Mar 26 06:55:10 PDT 2025
================
@@ -16,3 +17,76 @@ def test_file(self):
self.assertEqual(str(file), "t.c")
self.assertEqual(file.name, "t.c")
self.assertEqual(repr(file), "<File: t.c>")
+
+ def test_file_eq(self):
+ path = os.path.join(inputs_dir, "hello.cpp")
+ header_path = os.path.join(inputs_dir, "header3.h")
+ tu = TranslationUnit.from_source(path)
+ file1 = File.from_name(tu, path)
+ file2 = File.from_name(tu, header_path)
+ file2_2 = File.from_name(tu, header_path)
+
+ self.assertEqual(file1, file1)
+ self.assertEqual(file2, file2_2)
+ self.assertNotEqual(file1, file2)
+ self.assertNotEqual(file1, "t.c")
+
+ def test_file_eq_failing(self):
+ index = Index.create()
+ tu = index.parse(
+ "t.c",
+ unsaved_files=[
+ ("t.c", "int a = 729;"),
+ ("s.c", "int a = 729;"),
+ ],
+ )
+ file1 = File.from_name(tu, "t.c")
+ file2 = File.from_name(tu, "s.c")
+ # FIXME: These files are not supposed to be equal
+ self.assertEqual(file1, file2)
+
+ def test_file_eq_failing_2(self):
+ index = Index.create()
+ tu = index.parse(
+ "t.c",
+ unsaved_files=[
+ ("t.c", "int a = 729;"),
+ ("s.c", "int a = 728;"),
+ ],
+ )
+ file1 = File.from_name(tu, "t.c")
+ file2 = File.from_name(tu, "s.c")
+ # FIXME: These files are not supposed to be equal
+ self.assertEqual(file1, file2)
+
+ def test_file_eq_failing_3(self):
+ index = Index.create()
+ tu = index.parse(
+ "t.c",
+ unsaved_files=[
+ ("t.c", '#include "a.c"\n#include "b.c";'),
+ ("a.c", "int a = 729;"),
+ ("b.c", "int b = 729;"),
+ ],
+ )
+ file1 = File.from_name(tu, "t.c")
+ file2 = File.from_name(tu, "a.c")
+ file3 = File.from_name(tu, "b.c")
+ # FIXME: These files are not supposed to be equal
+ self.assertEqual(file2, file3)
+ self.assertEqual(file1, file2)
+ self.assertEqual(file1, file3)
+
+ def test_file_eq_failing_4(self):
+ path = os.path.join(inputs_dir, "testfile.c")
+ path_a = os.path.join(inputs_dir, "a.inc")
+ path_b = os.path.join(inputs_dir, "b.inc")
+ tu = TranslationUnit.from_source(path)
+ print(tu.spelling, tu.cursor.spelling)
+ file1 = File.from_name(tu, path)
+ file2 = File.from_name(tu, path_a)
+ file3 = File.from_name(tu, path_b)
+ # FIXME: These files are not supposed to be equal
+ self.assertEqual(file2, file3)
+ self.assertEqual(file1, file2)
+ self.assertEqual(file1, file3)
----------------
AaronBallman wrote:
I think this might be a bug here: https://github.com/llvm/llvm-project/blob/1b07e865a1f9da64c75cc409a969b108b201fe80/clang/tools/libclang/CIndex.cpp#L5173
For starters, `FileEntryRef` has an equality operator: https://github.com/llvm/llvm-project/blob/1b07e865a1f9da64c75cc409a969b108b201fe80/clang/include/clang/Basic/FileEntry.h#L89 so it's worth seeing if using that gives the correct behavior.
But also, you can use `FileManager::getNoncachedStatValue` to get a `vfs::Status` object on which you can call `equivalent()` to test for equivalency, so it's worth seeing if that gives the correct behavior.
https://github.com/llvm/llvm-project/pull/130383
More information about the cfe-commits
mailing list