[clang] c6c0846 - [libclang/python] Add equality comparison operators for File (#130383)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 24 02:15:53 PDT 2025
Author: Jannick Kremer
Date: 2025-04-24T11:15:50+02:00
New Revision: c6c08462ee3e8fc3d9cf9a69bb51175be49d5d3c
URL: https://github.com/llvm/llvm-project/commit/c6c08462ee3e8fc3d9cf9a69bb51175be49d5d3c
DIFF: https://github.com/llvm/llvm-project/commit/c6c08462ee3e8fc3d9cf9a69bb51175be49d5d3c.diff
LOG: [libclang/python] Add equality comparison operators for File (#130383)
This covers the `File` interface changes added by #120590
---------
Co-authored-by: Mathias Stearn <redbeard0531 at gmail.com>
Co-authored-by: Vlad Serebrennikov <serebrennikov.vladislav at gmail.com>
Added:
clang/bindings/python/tests/cindex/INPUTS/a.inc
clang/bindings/python/tests/cindex/INPUTS/b.inc
clang/bindings/python/tests/cindex/INPUTS/testfile.c
Modified:
clang/bindings/python/clang/cindex.py
clang/bindings/python/tests/cindex/test_file.py
clang/docs/ReleaseNotes.rst
Removed:
################################################################################
diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py
index 8dc79f28a090a..a5227df093e73 100644
--- a/clang/bindings/python/clang/cindex.py
+++ b/clang/bindings/python/clang/cindex.py
@@ -3499,6 +3499,14 @@ 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 +3994,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/INPUTS/a.inc b/clang/bindings/python/tests/cindex/INPUTS/a.inc
new file mode 100644
index 0000000000000..2739d724db3b7
--- /dev/null
+++ b/clang/bindings/python/tests/cindex/INPUTS/a.inc
@@ -0,0 +1 @@
+1, 2, 3
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..2739d724db3b7
--- /dev/null
+++ b/clang/bindings/python/tests/cindex/INPUTS/b.inc
@@ -0,0 +1 @@
+1, 2, 3
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..21778bc0b17e7
--- /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 14a3084ee2b47..a8c1dbf558543 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
+inputs_dir = os.path.join(os.path.dirname(__file__), "INPUTS")
class TestFile(unittest.TestCase):
def test_file(self):
@@ -16,3 +17,54 @@ 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, "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)
+ 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, "t.c")
+
+ def test_file_eq_in_memory(self):
+ tu = TranslationUnit.from_source(
+ "testfile.c",
+ unsaved_files=[
+ (
+ "testfile.c",
+ """
+int a[] = {
+ #include "a.inc"
+};
+int b[] = {
+ #include "b.inc"
+};
+""",
+ ),
+ ("a.inc", "1,2,3"),
+ ("b.inc", "1,2,3"),
+ ],
+ )
+
+ 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)
+ 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")
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
--------------
More information about the cfe-commits
mailing list