]> granicus.if.org Git - clang/commitdiff
[clang.py] Implement Type.__eq__ and Type.__ne__
authorGregory Szorc <gregory.szorc@gmail.com>
Mon, 20 Feb 2012 17:44:49 +0000 (17:44 +0000)
committerGregory Szorc <gregory.szorc@gmail.com>
Mon, 20 Feb 2012 17:44:49 +0000 (17:44 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@150969 91177308-0d34-0410-b5e6-96231b3b80d8

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

index d01d1db31a165f5cc392e359eb9be0286d9dd080..e2ef3c366542dfd4b5f9874efc6aec92c52dc7bb 100644 (file)
@@ -1245,6 +1245,15 @@ class Type(Structure):
         """
         return Type_get_array_size(self)
 
+    def __eq__(self, other):
+        if type(other) != type(self):
+            return False
+
+        return Type_equal(self, other)
+
+    def __ne__(self, other):
+        return not self.__eq__(other)
+
 ## CIndex Objects ##
 
 # CIndex objects (derived from ClangObject) are essentially lightweight
@@ -1936,6 +1945,10 @@ Type_get_array_size = lib.clang_getArraySize
 Type_get_array_size.argtype = [Type]
 Type_get_array_size.restype = c_longlong
 
+Type_equal = lib.clang_equalTypes
+Type_equal.argtypes = [Type, Type]
+Type_equal.restype = bool
+
 # Index Functions
 Index_create = lib.clang_createIndex
 Index_create.argtypes = [c_int, c_int]
index c4869fdd5a4bf615fb9672aefed27d9f37d8b69a..b07ef643bdc703a17d279994c8e3305d52bf45d9 100644 (file)
@@ -109,6 +109,31 @@ def testConstantArray():
     else:
         assert False, "Didn't find teststruct??"
 
+def test_equal():
+    """Ensure equivalence operators work on Type."""
+    source = 'int a; int b; void *v;'
+    tu = get_tu(source)
+
+    a, b, v = None, None, None
+
+    for cursor in tu.cursor.get_children():
+        if cursor.spelling == 'a':
+            a = cursor
+        elif cursor.spelling == 'b':
+            b = cursor
+        elif cursor.spelling == 'v':
+            v = cursor
+
+    assert a is not None
+    assert b is not None
+    assert v is not None
+
+    assert a.type == b.type
+    assert a.type != v.type
+
+    assert a.type != None
+    assert a.type != 'foo'
+
 def test_is_pod():
     tu = get_tu('int i; void f();')
     i, f = None, None