[clang] [libclang/python] Add equality comparison operators for File (PR #130383)
Jannick Kremer via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 23 19:47:11 PDT 2025
https://github.com/DeinAlptraum updated https://github.com/llvm/llvm-project/pull/130383
>From 1e6ed0266fb849f14d6b952dcd84e277ed70aa58 Mon Sep 17 00:00:00 2001
From: Mathias Stearn <redbeard0531 at gmail.com>
Date: Thu, 19 Dec 2024 16:22:04 +0100
Subject: [PATCH 01/10] [libclang/python] Add equality comparison operators for
File
---
clang/bindings/python/clang/cindex.py | 7 +++++++
clang/bindings/python/tests/cindex/test_file.py | 15 +++++++++++++++
clang/docs/ReleaseNotes.rst | 1 +
3 files changed, 23 insertions(+)
diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py
index 8dc79f28a090a..16dd1e6895f48 100644
--- a/clang/bindings/python/clang/cindex.py
+++ b/clang/bindings/python/clang/cindex.py
@@ -3499,6 +3499,12 @@ def __str__(self):
def __repr__(self):
return "<File: %s>" % (self.name)
+ def __eq__(self, other) -> bool:
+ return isinstance(other, File) and bool(conf.lib.clang_File_isEqual(self, other))
+
+ def __ne__(self, other) -> bool:
+ return not self.__eq__(other)
+
@staticmethod
def from_result(res, arg):
assert isinstance(res, c_object_p)
@@ -3986,6 +3992,7 @@ def set_property(self, property, value):
("clang_getFile", [TranslationUnit, c_interop_string], c_object_p),
("clang_getFileName", [File], _CXString),
("clang_getFileTime", [File], c_uint),
+ ("clang_File_isEqual", [File, File], bool),
("clang_getIBOutletCollectionType", [Cursor], Type),
("clang_getIncludedFile", [Cursor], c_object_p),
(
diff --git a/clang/bindings/python/tests/cindex/test_file.py b/clang/bindings/python/tests/cindex/test_file.py
index 14a3084ee2b47..0e8431054e951 100644
--- a/clang/bindings/python/tests/cindex/test_file.py
+++ b/clang/bindings/python/tests/cindex/test_file.py
@@ -16,3 +16,18 @@ 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):
+ 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")
+ self.assertEqual(file1, file1)
+ self.assertNotEqual(file1, file2)
+ self.assertNotEqual(file1, "t.c")
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index cf90218c562e2..03ee627e1db71 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -747,6 +747,7 @@ Python Binding Changes
allows visiting the methods of a class.
- Added ``Type.get_fully_qualified_name``, which provides fully qualified type names as
instructed by a PrintingPolicy.
+- Add equality comparison operators for ``File`` type
OpenMP Support
--------------
>From 4b72ede9e1b89ffc44be3ec352d0a82c7abe7fd4 Mon Sep 17 00:00:00 2001
From: Jannick Kremer <jannick.kremer at mailbox.org>
Date: Thu, 20 Mar 2025 12:11:20 +0900
Subject: [PATCH 02/10] Use existing test input files instead of in-memory
files
---
.../bindings/python/tests/cindex/test_file.py | 21 +++++++++----------
1 file changed, 10 insertions(+), 11 deletions(-)
diff --git a/clang/bindings/python/tests/cindex/test_file.py b/clang/bindings/python/tests/cindex/test_file.py
index 0e8431054e951..4e056d3e22115 100644
--- a/clang/bindings/python/tests/cindex/test_file.py
+++ b/clang/bindings/python/tests/cindex/test_file.py
@@ -1,12 +1,13 @@
import os
-from clang.cindex import Config, File, Index
+from clang.cindex import Config, File, Index, TranslationUnit
if "CLANG_LIBRARY_PATH" in os.environ:
Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
import unittest
+kInputsDir = os.path.join(os.path.dirname(__file__), "INPUTS")
class TestFile(unittest.TestCase):
def test_file(self):
@@ -18,16 +19,14 @@ def test_file(self):
self.assertEqual(repr(file), "<File: t.c>")
def test_file_eq(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")
+ path = os.path.join(kInputsDir, "hello.cpp")
+ header_path = os.path.join(kInputsDir, "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")
>From d1da5faf71585dcdb83c206b323bb439e4ac037c Mon Sep 17 00:00:00 2001
From: Jannick Kremer <jannick.kremer at mailbox.org>
Date: Thu, 20 Mar 2025 13:27:04 +0900
Subject: [PATCH 03/10] Fix formatting
---
clang/bindings/python/clang/cindex.py | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py
index 16dd1e6895f48..a5227df093e73 100644
--- a/clang/bindings/python/clang/cindex.py
+++ b/clang/bindings/python/clang/cindex.py
@@ -3500,7 +3500,9 @@ def __repr__(self):
return "<File: %s>" % (self.name)
def __eq__(self, other) -> bool:
- return isinstance(other, File) and bool(conf.lib.clang_File_isEqual(self, other))
+ return isinstance(other, File) and bool(
+ conf.lib.clang_File_isEqual(self, other)
+ )
def __ne__(self, other) -> bool:
return not self.__eq__(other)
>From c02dcc34187fc8164419e3d5e04dd9681385242b Mon Sep 17 00:00:00 2001
From: Jannick Kremer <jannick.kremer at mailbox.org>
Date: Sat, 22 Mar 2025 11:50:59 +0900
Subject: [PATCH 04/10] Adress review comments
---
.../bindings/python/tests/cindex/test_file.py | 20 ++++++++++++++++---
1 file changed, 17 insertions(+), 3 deletions(-)
diff --git a/clang/bindings/python/tests/cindex/test_file.py b/clang/bindings/python/tests/cindex/test_file.py
index 4e056d3e22115..3a70f0a7f2dc9 100644
--- a/clang/bindings/python/tests/cindex/test_file.py
+++ b/clang/bindings/python/tests/cindex/test_file.py
@@ -7,7 +7,7 @@
import unittest
-kInputsDir = os.path.join(os.path.dirname(__file__), "INPUTS")
+inputs_dir = os.path.join(os.path.dirname(__file__), "INPUTS")
class TestFile(unittest.TestCase):
def test_file(self):
@@ -19,8 +19,8 @@ def test_file(self):
self.assertEqual(repr(file), "<File: t.c>")
def test_file_eq(self):
- path = os.path.join(kInputsDir, "hello.cpp")
- header_path = os.path.join(kInputsDir, "header3.h")
+ 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)
@@ -30,3 +30,17 @@ def test_file_eq(self):
self.assertEqual(file2, file2_2)
self.assertNotEqual(file1, file2)
self.assertNotEqual(file1, "t.c")
+
+ # FIXME: this test shouldn't pass
+ 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")
+ self.assertEqual(file1, file2) # These files are not supposed to be equal
>From 1a439b75a3c16e4c369d16b168ec2949a8e2d805 Mon Sep 17 00:00:00 2001
From: Jannick Kremer <jannick.kremer at mailbox.org>
Date: Sat, 22 Mar 2025 04:11:05 +0100
Subject: [PATCH 05/10] Update clang/bindings/python/tests/cindex/test_file.py
Co-authored-by: Vlad Serebrennikov <serebrennikov.vladislav at gmail.com>
---
clang/bindings/python/tests/cindex/test_file.py | 1 -
1 file changed, 1 deletion(-)
diff --git a/clang/bindings/python/tests/cindex/test_file.py b/clang/bindings/python/tests/cindex/test_file.py
index 3a70f0a7f2dc9..5e8563ab7087a 100644
--- a/clang/bindings/python/tests/cindex/test_file.py
+++ b/clang/bindings/python/tests/cindex/test_file.py
@@ -31,7 +31,6 @@ def test_file_eq(self):
self.assertNotEqual(file1, file2)
self.assertNotEqual(file1, "t.c")
- # FIXME: this test shouldn't pass
def test_file_eq_failing(self):
index = Index.create()
tu = index.parse(
>From acfa2de78fbca0bb417fd5b5ea0ef20473496e85 Mon Sep 17 00:00:00 2001
From: Jannick Kremer <jannick.kremer at mailbox.org>
Date: Sat, 22 Mar 2025 04:11:14 +0100
Subject: [PATCH 06/10] Update clang/bindings/python/tests/cindex/test_file.py
Co-authored-by: Vlad Serebrennikov <serebrennikov.vladislav at gmail.com>
---
clang/bindings/python/tests/cindex/test_file.py | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/clang/bindings/python/tests/cindex/test_file.py b/clang/bindings/python/tests/cindex/test_file.py
index 5e8563ab7087a..58881801f7963 100644
--- a/clang/bindings/python/tests/cindex/test_file.py
+++ b/clang/bindings/python/tests/cindex/test_file.py
@@ -42,4 +42,5 @@ def test_file_eq_failing(self):
)
file1 = File.from_name(tu, "t.c")
file2 = File.from_name(tu, "s.c")
- self.assertEqual(file1, file2) # These files are not supposed to be equal
+ # FIXME: These files are not supposed to be equal
+ self.assertEqual(file1, file2)
>From 40acc19d025a21fd598408c934b6e7a35af10f44 Mon Sep 17 00:00:00 2001
From: Jannick Kremer <jannick.kremer at mailbox.org>
Date: Sat, 22 Mar 2025 20:00:11 +0900
Subject: [PATCH 07/10] Add example test
---
.../bindings/python/tests/cindex/INPUTS/a.inc | 1 +
.../bindings/python/tests/cindex/INPUTS/b.inc | 1 +
.../python/tests/cindex/INPUTS/testfile.c | 6 +++
.../bindings/python/tests/cindex/test_file.py | 45 ++++++++++++++++++-
4 files changed, 52 insertions(+), 1 deletion(-)
create mode 100644 clang/bindings/python/tests/cindex/INPUTS/a.inc
create mode 100644 clang/bindings/python/tests/cindex/INPUTS/b.inc
create mode 100644 clang/bindings/python/tests/cindex/INPUTS/testfile.c
diff --git a/clang/bindings/python/tests/cindex/INPUTS/a.inc b/clang/bindings/python/tests/cindex/INPUTS/a.inc
new file mode 100644
index 0000000000000..3fde4e20239e0
--- /dev/null
+++ b/clang/bindings/python/tests/cindex/INPUTS/a.inc
@@ -0,0 +1 @@
+1,2,3
\ No newline at end of file
diff --git a/clang/bindings/python/tests/cindex/INPUTS/b.inc b/clang/bindings/python/tests/cindex/INPUTS/b.inc
new file mode 100644
index 0000000000000..3fde4e20239e0
--- /dev/null
+++ b/clang/bindings/python/tests/cindex/INPUTS/b.inc
@@ -0,0 +1 @@
+1,2,3
\ No newline at end of file
diff --git a/clang/bindings/python/tests/cindex/INPUTS/testfile.c b/clang/bindings/python/tests/cindex/INPUTS/testfile.c
new file mode 100644
index 0000000000000..23dc5165e2b1f
--- /dev/null
+++ b/clang/bindings/python/tests/cindex/INPUTS/testfile.c
@@ -0,0 +1,6 @@
+int a[] = {
+ #include "a.inc"
+};
+int b[] = {
+ #include "b.inc"
+};
diff --git a/clang/bindings/python/tests/cindex/test_file.py b/clang/bindings/python/tests/cindex/test_file.py
index 58881801f7963..1f470b5718add 100644
--- a/clang/bindings/python/tests/cindex/test_file.py
+++ b/clang/bindings/python/tests/cindex/test_file.py
@@ -43,4 +43,47 @@ def test_file_eq_failing(self):
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)
+ 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, "hello.cpp")
+ tu = TranslationUnit.from_source(path)
+ 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)
>From aff6bfe2f2c77449e3e3445a90ab80e102e1c803 Mon Sep 17 00:00:00 2001
From: Jannick Kremer <jannick.kremer at mailbox.org>
Date: Sat, 22 Mar 2025 20:15:26 +0900
Subject: [PATCH 08/10] Fix test error
---
clang/bindings/python/tests/cindex/test_file.py | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/bindings/python/tests/cindex/test_file.py b/clang/bindings/python/tests/cindex/test_file.py
index 1f470b5718add..0b90fb7f68836 100644
--- a/clang/bindings/python/tests/cindex/test_file.py
+++ b/clang/bindings/python/tests/cindex/test_file.py
@@ -78,9 +78,9 @@ def test_file_eq_failing_3(self):
self.assertEqual(file1, file3)
def test_file_eq_failing_4(self):
- path = os.path.join(inputs_dir, "hello.cpp")
+ path = os.path.join(inputs_dir, "testfile.c")
tu = TranslationUnit.from_source(path)
- file1 = File.from_name(tu, "t.c")
+ file1 = File.from_name(tu, "testfile.c")
file2 = File.from_name(tu, "a.c")
file3 = File.from_name(tu, "b.c")
# FIXME: These files are not supposed to be equal
>From f0bc4276d2ee15d7871b86715ea7b8cc82ee5480 Mon Sep 17 00:00:00 2001
From: Jannick Kremer <jannick.kremer at mailbox.org>
Date: Sat, 22 Mar 2025 20:35:57 +0900
Subject: [PATCH 09/10] Fix test again
---
clang/bindings/python/tests/cindex/test_file.py | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/clang/bindings/python/tests/cindex/test_file.py b/clang/bindings/python/tests/cindex/test_file.py
index 0b90fb7f68836..21ff478e43b18 100644
--- a/clang/bindings/python/tests/cindex/test_file.py
+++ b/clang/bindings/python/tests/cindex/test_file.py
@@ -79,10 +79,13 @@ def test_file_eq_failing_3(self):
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)
- file1 = File.from_name(tu, "testfile.c")
- file2 = File.from_name(tu, "a.c")
- file3 = File.from_name(tu, "b.c")
+ 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)
>From bb4e33e0a81275938dd9de89e0580bbd4d50afe4 Mon Sep 17 00:00:00 2001
From: Jannick Kremer <jannick.kremer at mailbox.org>
Date: Thu, 24 Apr 2025 11:35:32 +0900
Subject: [PATCH 10/10] Add proper tests for on-disk and in-memory files
---
.../bindings/python/tests/cindex/test_file.py | 98 ++++++++-----------
1 file changed, 39 insertions(+), 59 deletions(-)
diff --git a/clang/bindings/python/tests/cindex/test_file.py b/clang/bindings/python/tests/cindex/test_file.py
index 21ff478e43b18..1ce721e56ec72 100644
--- a/clang/bindings/python/tests/cindex/test_file.py
+++ b/clang/bindings/python/tests/cindex/test_file.py
@@ -19,74 +19,54 @@ def test_file(self):
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")
+ 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)
- 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)
+ print(tu.spelling, tu.cursor.spelling)
+ main_file = File.from_name(tu, path)
+ a_file = File.from_name(tu, path_a)
+ a_file2 = File.from_name(tu, path_a)
+ b_file = File.from_name(tu, path_b)
- 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)
+ self.assertEqual(a_file, a_file2)
+ self.assertNotEqual(a_file, b_file)
+ self.assertNotEqual(main_file, a_file)
+ self.assertNotEqual(main_file, b_file)
+ self.assertNotEqual(main_file, "t.c")
- def test_file_eq_failing_3(self):
- index = Index.create()
- tu = index.parse(
- "t.c",
+ def test_file_eq_in_memory(self):
+ tu = TranslationUnit.from_source(
+ "testfile.c",
unsaved_files=[
- ("t.c", '#include "a.c"\n#include "b.c";'),
- ("a.c", "int a = 729;"),
- ("b.c", "int b = 729;"),
+ (
+ "testfile.c",
+ """
+int a[] = {
+ #include "a.inc"
+};
+int b[] = {
+ #include "b.inc"
+};
+""",
+ ),
+ ("a.inc", "1,2,3"),
+ ("b.inc", "1,2,3"),
],
)
- 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)
+ main_file = File.from_name(tu, path)
+ a_file = File.from_name(tu, path_a)
+ a_file2 = File.from_name(tu, path_a)
+ b_file = File.from_name(tu, path_b)
+
+ self.assertEqual(a_file, a_file2)
+ self.assertNotEqual(a_file, b_file)
+ self.assertNotEqual(main_file, a_file)
+ self.assertNotEqual(main_file, b_file)
+ self.assertNotEqual(main_file, "a.inc")
More information about the cfe-commits
mailing list