From c6c08462ee3e8fc3d9cf9a69bb51175be49d5d3c Mon Sep 17 00:00:00 2001 From: Jannick Kremer Date: Thu, 24 Apr 2025 11:15:50 +0200 Subject: [PATCH] [libclang/python] Add equality comparison operators for File (#130383) This covers the `File` interface changes added by #120590 --------- Co-authored-by: Mathias Stearn Co-authored-by: Vlad Serebrennikov --- clang/bindings/python/clang/cindex.py | 9 ++++ .../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 | 54 ++++++++++++++++++- clang/docs/ReleaseNotes.rst | 1 + 6 files changed, 71 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/clang/cindex.py b/clang/bindings/python/clang/cindex.py index 8dc79f28a090..a5227df093e7 100644 --- a/clang/bindings/python/clang/cindex.py +++ b/clang/bindings/python/clang/cindex.py @@ -3499,6 +3499,14 @@ class File(ClangObject): def __repr__(self): return "" % (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 @@ FUNCTION_LIST: list[LibFunc] = [ ("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 000000000000..2739d724db3b --- /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 000000000000..2739d724db3b --- /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 000000000000..21778bc0b17e --- /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 14a3084ee2b4..a8c1dbf55854 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 @@ class TestFile(unittest.TestCase): self.assertEqual(str(file), "t.c") self.assertEqual(file.name, "t.c") self.assertEqual(repr(file), "") + + 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 cf90218c562e..03ee627e1db7 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 --------------