[clang] [libclang/python] Add equality comparison operators for File (PR #130383)
Jannick Kremer via cfe-commits
cfe-commits at lists.llvm.org
Fri Mar 21 20:11:14 PDT 2025
https://github.com/DeinAlptraum updated https://github.com/llvm/llvm-project/pull/130383
>From e8bf3b6f08f0e0030ea36fe8c42fcde166ad27e3 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 1/5] [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 e881bf131d6c4..e1a931b883480 100644
--- a/clang/bindings/python/clang/cindex.py
+++ b/clang/bindings/python/clang/cindex.py
@@ -3470,6 +3470,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)
@@ -3956,6 +3962,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 8182bccdd2da8..cc0495d29a677 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -527,6 +527,7 @@ Python Binding Changes
----------------------
- Added ``Type.get_methods``, a binding for ``clang_visitCXXMethods``, which
allows visiting the methods of a class.
+- Add equality comparison operators for ``File`` type
OpenMP Support
--------------
>From 2656a319de0897fb8224b76c240f26fe502ce3c1 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 2/5] 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 d2fcbda9fbf9327afc177777da73a2aeede16d71 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 3/5] 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 e1a931b883480..e13c80523ba1f 100644
--- a/clang/bindings/python/clang/cindex.py
+++ b/clang/bindings/python/clang/cindex.py
@@ -3471,7 +3471,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 b8a4f30520d089eea068849e930964bdfc95c078 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 4/5] Adress review comments
---
.../bindings/python/tests/cindex/test_file.py | 20 ++++++++++++++++---
clang/docs/ReleaseNotes.rst | 2 +-
2 files changed, 18 insertions(+), 4 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
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index cc0495d29a677..a8bad875114e0 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -527,7 +527,7 @@ Python Binding Changes
----------------------
- Added ``Type.get_methods``, a binding for ``clang_visitCXXMethods``, which
allows visiting the methods of a class.
-- Add equality comparison operators for ``File`` type
+- Add equality comparison operators for ``File`` type.
OpenMP Support
--------------
>From 0726a2729482d1e8249063c405a8982d44eda446 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 5/5] 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(
More information about the cfe-commits
mailing list