Skip to content

Commit ae2ea7b

Browse files
DeinAlptraumRedBeard0531Endilll
authored andcommitted
[libclang/python] Add equality comparison operators for File (llvm#130383)
This covers the `File` interface changes added by llvm#120590 --------- Co-authored-by: Mathias Stearn <[email protected]> Co-authored-by: Vlad Serebrennikov <[email protected]>
1 parent 5df4a9d commit ae2ea7b

File tree

6 files changed

+71
-1
lines changed

6 files changed

+71
-1
lines changed

clang/bindings/python/clang/cindex.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3499,6 +3499,14 @@ def __str__(self):
34993499
def __repr__(self):
35003500
return "<File: %s>" % (self.name)
35013501

3502+
def __eq__(self, other) -> bool:
3503+
return isinstance(other, File) and bool(
3504+
conf.lib.clang_File_isEqual(self, other)
3505+
)
3506+
3507+
def __ne__(self, other) -> bool:
3508+
return not self.__eq__(other)
3509+
35023510
@staticmethod
35033511
def from_result(res, arg):
35043512
assert isinstance(res, c_object_p)
@@ -3986,6 +3994,7 @@ def set_property(self, property, value):
39863994
("clang_getFile", [TranslationUnit, c_interop_string], c_object_p),
39873995
("clang_getFileName", [File], _CXString),
39883996
("clang_getFileTime", [File], c_uint),
3997+
("clang_File_isEqual", [File, File], bool),
39893998
("clang_getIBOutletCollectionType", [Cursor], Type),
39903999
("clang_getIncludedFile", [Cursor], c_object_p),
39914000
(
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1, 2, 3
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1, 2, 3
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
int a[] = {
2+
#include "a.inc"
3+
};
4+
int b[] = {
5+
#include "b.inc"
6+
};

clang/bindings/python/tests/cindex/test_file.py

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import os
22

3-
from clang.cindex import Config, File, Index
3+
from clang.cindex import Config, File, Index, TranslationUnit
44

55
if "CLANG_LIBRARY_PATH" in os.environ:
66
Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
77

88
import unittest
99

10+
inputs_dir = os.path.join(os.path.dirname(__file__), "INPUTS")
1011

1112
class TestFile(unittest.TestCase):
1213
def test_file(self):
@@ -16,3 +17,54 @@ def test_file(self):
1617
self.assertEqual(str(file), "t.c")
1718
self.assertEqual(file.name, "t.c")
1819
self.assertEqual(repr(file), "<File: t.c>")
20+
21+
def test_file_eq(self):
22+
path = os.path.join(inputs_dir, "testfile.c")
23+
path_a = os.path.join(inputs_dir, "a.inc")
24+
path_b = os.path.join(inputs_dir, "b.inc")
25+
tu = TranslationUnit.from_source(path)
26+
main_file = File.from_name(tu, path)
27+
a_file = File.from_name(tu, path_a)
28+
a_file2 = File.from_name(tu, path_a)
29+
b_file = File.from_name(tu, path_b)
30+
31+
self.assertEqual(a_file, a_file2)
32+
self.assertNotEqual(a_file, b_file)
33+
self.assertNotEqual(main_file, a_file)
34+
self.assertNotEqual(main_file, b_file)
35+
self.assertNotEqual(main_file, "t.c")
36+
37+
def test_file_eq_in_memory(self):
38+
tu = TranslationUnit.from_source(
39+
"testfile.c",
40+
unsaved_files=[
41+
(
42+
"testfile.c",
43+
"""
44+
int a[] = {
45+
#include "a.inc"
46+
};
47+
int b[] = {
48+
#include "b.inc"
49+
};
50+
""",
51+
),
52+
("a.inc", "1,2,3"),
53+
("b.inc", "1,2,3"),
54+
],
55+
)
56+
57+
path = os.path.join(inputs_dir, "testfile.c")
58+
path_a = os.path.join(inputs_dir, "a.inc")
59+
path_b = os.path.join(inputs_dir, "b.inc")
60+
tu = TranslationUnit.from_source(path)
61+
main_file = File.from_name(tu, path)
62+
a_file = File.from_name(tu, path_a)
63+
a_file2 = File.from_name(tu, path_a)
64+
b_file = File.from_name(tu, path_b)
65+
66+
self.assertEqual(a_file, a_file2)
67+
self.assertNotEqual(a_file, b_file)
68+
self.assertNotEqual(main_file, a_file)
69+
self.assertNotEqual(main_file, b_file)
70+
self.assertNotEqual(main_file, "a.inc")

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,7 @@ Python Binding Changes
747747
allows visiting the methods of a class.
748748
- Added ``Type.get_fully_qualified_name``, which provides fully qualified type names as
749749
instructed by a PrintingPolicy.
750+
- Add equality comparison operators for ``File`` type
750751

751752
OpenMP Support
752753
--------------

0 commit comments

Comments
 (0)