Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions clang/bindings/python/clang/cindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -3470,6 +3470,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)
Expand Down Expand Up @@ -3956,6 +3964,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),
(
Expand Down
1 change: 1 addition & 0 deletions clang/bindings/python/tests/cindex/INPUTS/a.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1,2,3
1 change: 1 addition & 0 deletions clang/bindings/python/tests/cindex/INPUTS/b.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1,2,3
6 changes: 6 additions & 0 deletions clang/bindings/python/tests/cindex/INPUTS/testfile.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
int a[] = {
#include "a.inc"
};
int b[] = {
#include "b.inc"
};
76 changes: 75 additions & 1 deletion clang/bindings/python/tests/cindex/test_file.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -16,3 +17,76 @@ 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, "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)
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(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)

def test_file_eq_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.assertNotEqual(file1, file2)

def test_file_eq_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.assertNotEqual(file2, file3)
self.assertNotEqual(file1, file2)
self.assertNotEqual(file1, file3)

def test_file_eq_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.assertNotEqual(file2, file3)
self.assertNotEqual(file1, file2)
self.assertNotEqual(file1, file3)
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------------
Expand Down
2 changes: 1 addition & 1 deletion clang/tools/libclang/CIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5170,7 +5170,7 @@ int clang_File_isEqual(CXFile file1, CXFile file2) {

FileEntryRef FEnt1 = *cxfile::getFileEntryRef(file1);
FileEntryRef FEnt2 = *cxfile::getFileEntryRef(file2);
return FEnt1.getUniqueID() == FEnt2.getUniqueID();
return FEnt1 == FEnt2;
}

CXString clang_File_tryGetRealPathName(CXFile SFile) {
Expand Down
Loading