[clang] [libclang/python] Fix bugs in custom enum implementation and add tests (PR #95381)
Jannick Kremer via cfe-commits
cfe-commits at lists.llvm.org
Thu Jun 13 07:57:03 PDT 2024
https://github.com/DeinAlptraum updated https://github.com/llvm/llvm-project/pull/95381
>From a3da142b0db6581581ccb135800d77b09476f385 Mon Sep 17 00:00:00 2001
From: Jannick Kremer <jannick-kremer at gmx.de>
Date: Thu, 13 Jun 2024 10:43:52 +0100
Subject: [PATCH 1/2] [libclang/python] Fix bugs in custom enum implementation
and add tests
Do not allow initialization of enum from negative IDs (e.g. from_id(-1)
currently produces the last known variant)
Rename duplicate enums: CursorKind.OMP_TEAMS_DISTRIBUTE_DIRECTIVE
and TypeKind.OBJCCLASS
Add tests to cover these cases
---
clang/bindings/python/clang/cindex.py | 8 +--
.../python/tests/cindex/test_enums.py | 50 +++++++++++++++++++
2 files changed, 54 insertions(+), 4 deletions(-)
create mode 100644 clang/bindings/python/tests/cindex/test_enums.py
diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py
index 302d99dccd77b..b3d51e4d2a668 100644
--- a/clang/bindings/python/clang/cindex.py
+++ b/clang/bindings/python/clang/cindex.py
@@ -649,7 +649,7 @@ def name(self):
@classmethod
def from_id(cls, id):
- if id >= len(cls._kinds) or cls._kinds[id] is None:
+ if id < 0 or id >= len(cls._kinds) or cls._kinds[id] is None:
raise ValueError("Unknown template argument kind %d" % id)
return cls._kinds[id]
@@ -1336,7 +1336,7 @@ def __repr__(self):
CursorKind.OMP_TEAMS_DISTRIBUTE_DIRECTIVE = CursorKind(271)
# OpenMP teams distribute simd directive.
-CursorKind.OMP_TEAMS_DISTRIBUTE_DIRECTIVE = CursorKind(272)
+CursorKind.OMP_TEAMS_DISTRIBUTE_SIMD_DIRECTIVE = CursorKind(272)
# OpenMP teams distribute parallel for simd directive.
CursorKind.OMP_TEAMS_DISTRIBUTE_PARALLEL_FOR_SIMD_DIRECTIVE = CursorKind(273)
@@ -2215,7 +2215,7 @@ def name(self):
@staticmethod
def from_id(id):
- if id >= len(StorageClass._kinds) or not StorageClass._kinds[id]:
+ if id < 0 or id >= len(StorageClass._kinds) or not StorageClass._kinds[id]:
raise ValueError("Unknown storage class %d" % id)
return StorageClass._kinds[id]
@@ -2395,7 +2395,7 @@ def __repr__(self):
TypeKind.OCLRESERVEID = TypeKind(160)
TypeKind.OBJCOBJECT = TypeKind(161)
-TypeKind.OBJCCLASS = TypeKind(162)
+TypeKind.OBJCTYPEPARAM = TypeKind(162)
TypeKind.ATTRIBUTED = TypeKind(163)
TypeKind.OCLINTELSUBGROUPAVCMCEPAYLOAD = TypeKind(164)
diff --git a/clang/bindings/python/tests/cindex/test_enums.py b/clang/bindings/python/tests/cindex/test_enums.py
new file mode 100644
index 0000000000000..985d71a4fdcfd
--- /dev/null
+++ b/clang/bindings/python/tests/cindex/test_enums.py
@@ -0,0 +1,50 @@
+import unittest
+
+from clang.cindex import (
+ CursorKind,
+ TemplateArgumentKind,
+ ExceptionSpecificationKind,
+ AvailabilityKind,
+ AccessSpecifier,
+ TypeKind,
+ RefQualifierKind,
+ LinkageKind,
+ TLSKind,
+ StorageClass,
+)
+
+
+class TestCursorKind(unittest.TestCase):
+ enums = [
+ CursorKind,
+ TemplateArgumentKind,
+ ExceptionSpecificationKind,
+ AvailabilityKind,
+ AccessSpecifier,
+ TypeKind,
+ RefQualifierKind,
+ LinkageKind,
+ TLSKind,
+ StorageClass,
+ ]
+
+ def test_from_id(self):
+ """Check that kinds can be constructed from valid IDs"""
+ for enum in self.enums:
+ self.assertEqual(enum.from_id(2), enum._kinds[2])
+ with self.assertRaises(ValueError):
+ enum.from_id(len(enum._kinds))
+ with self.assertRaises(ValueError):
+ enum.from_id(-1)
+
+ def test_unique_kinds(self):
+ """Check that no kind name has been used multiple times"""
+ for enum in self.enums:
+ seen_names = set()
+ for id in range(len(enum._kinds)):
+ try:
+ kind_name = enum.from_id(id).name
+ except ValueError:
+ continue
+ self.assertNotIn(kind_name, seen_names)
+ seen_names.add(id)
>From 67f43ab2c26129cc6611ba58e81f6fbe6c490dbd Mon Sep 17 00:00:00 2001
From: Jannick Kremer <jannick-kremer at gmx.de>
Date: Thu, 13 Jun 2024 14:18:03 +0100
Subject: [PATCH 2/2] [libclang/python] Add release notes
Also simplify the enum test
---
clang/bindings/python/tests/cindex/test_enums.py | 7 ++-----
clang/docs/ReleaseNotes.rst | 8 ++++++++
2 files changed, 10 insertions(+), 5 deletions(-)
diff --git a/clang/bindings/python/tests/cindex/test_enums.py b/clang/bindings/python/tests/cindex/test_enums.py
index 985d71a4fdcfd..6fc0e5ed77e3e 100644
--- a/clang/bindings/python/tests/cindex/test_enums.py
+++ b/clang/bindings/python/tests/cindex/test_enums.py
@@ -40,11 +40,8 @@ def test_from_id(self):
def test_unique_kinds(self):
"""Check that no kind name has been used multiple times"""
for enum in self.enums:
- seen_names = set()
for id in range(len(enum._kinds)):
try:
- kind_name = enum.from_id(id).name
+ enum.from_id(id).name
except ValueError:
- continue
- self.assertNotIn(kind_name, seen_names)
- seen_names.add(id)
+ pass
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 8c2f737836a9d..78a045239fe47 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -135,6 +135,14 @@ Clang Frontend Potentially Breaking Changes
- The ``hasTypeLoc`` AST matcher will no longer match a ``classTemplateSpecializationDecl``;
existing uses should switch to ``templateArgumentLoc`` or ``hasAnyTemplateArgumentLoc`` instead.
+Clang Python Bindings Potentially Breaking Changes
+--------------------------------------------------
+- Renamed ``CursorKind`` variant 272 from ``OMP_TEAMS_DISTRIBUTE_DIRECTIVE``
+ to ``OMP_TEAMS_DISTRIBUTE_SIMD_DIRECTIVE``. The previous name was incorrect, it was a duplicate
+ of variant 271.
+- Renamed ``TypeKind`` variant 162 from ``OBJCCLASS`` to ``OBJCTYPEPARAM``.
+ The previous name was incorrect, it was a duplicate of variant 28.
+
What's New in Clang |release|?
==============================
Some of the major new features and improvements to Clang are listed
More information about the cfe-commits
mailing list