Skip to content

Commit 88e42c6

Browse files
authored
[libclang/python] Fix bugs in custom enum implementation and add tests (#95381)
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
1 parent 32cd703 commit 88e42c6

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

clang/bindings/python/clang/cindex.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ def name(self):
649649

650650
@classmethod
651651
def from_id(cls, id):
652-
if id >= len(cls._kinds) or cls._kinds[id] is None:
652+
if id < 0 or id >= len(cls._kinds) or cls._kinds[id] is None:
653653
raise ValueError("Unknown template argument kind %d" % id)
654654
return cls._kinds[id]
655655

@@ -1336,7 +1336,7 @@ def __repr__(self):
13361336
CursorKind.OMP_TEAMS_DISTRIBUTE_DIRECTIVE = CursorKind(271)
13371337

13381338
# OpenMP teams distribute simd directive.
1339-
CursorKind.OMP_TEAMS_DISTRIBUTE_DIRECTIVE = CursorKind(272)
1339+
CursorKind.OMP_TEAMS_DISTRIBUTE_SIMD_DIRECTIVE = CursorKind(272)
13401340

13411341
# OpenMP teams distribute parallel for simd directive.
13421342
CursorKind.OMP_TEAMS_DISTRIBUTE_PARALLEL_FOR_SIMD_DIRECTIVE = CursorKind(273)
@@ -2215,7 +2215,7 @@ def name(self):
22152215

22162216
@staticmethod
22172217
def from_id(id):
2218-
if id >= len(StorageClass._kinds) or not StorageClass._kinds[id]:
2218+
if id < 0 or id >= len(StorageClass._kinds) or not StorageClass._kinds[id]:
22192219
raise ValueError("Unknown storage class %d" % id)
22202220
return StorageClass._kinds[id]
22212221

@@ -2395,7 +2395,7 @@ def __repr__(self):
23952395
TypeKind.OCLRESERVEID = TypeKind(160)
23962396

23972397
TypeKind.OBJCOBJECT = TypeKind(161)
2398-
TypeKind.OBJCCLASS = TypeKind(162)
2398+
TypeKind.OBJCTYPEPARAM = TypeKind(162)
23992399
TypeKind.ATTRIBUTED = TypeKind(163)
24002400

24012401
TypeKind.OCLINTELSUBGROUPAVCMCEPAYLOAD = TypeKind(164)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import unittest
2+
3+
from clang.cindex import (
4+
CursorKind,
5+
TemplateArgumentKind,
6+
ExceptionSpecificationKind,
7+
AvailabilityKind,
8+
AccessSpecifier,
9+
TypeKind,
10+
RefQualifierKind,
11+
LinkageKind,
12+
TLSKind,
13+
StorageClass,
14+
)
15+
16+
17+
class TestCursorKind(unittest.TestCase):
18+
enums = [
19+
CursorKind,
20+
TemplateArgumentKind,
21+
ExceptionSpecificationKind,
22+
AvailabilityKind,
23+
AccessSpecifier,
24+
TypeKind,
25+
RefQualifierKind,
26+
LinkageKind,
27+
TLSKind,
28+
StorageClass,
29+
]
30+
31+
def test_from_id(self):
32+
"""Check that kinds can be constructed from valid IDs"""
33+
for enum in self.enums:
34+
self.assertEqual(enum.from_id(2), enum._kinds[2])
35+
with self.assertRaises(ValueError):
36+
enum.from_id(len(enum._kinds))
37+
with self.assertRaises(ValueError):
38+
enum.from_id(-1)
39+
40+
def test_unique_kinds(self):
41+
"""Check that no kind name has been used multiple times"""
42+
for enum in self.enums:
43+
for id in range(len(enum._kinds)):
44+
try:
45+
enum.from_id(id).name
46+
except ValueError:
47+
pass

clang/docs/ReleaseNotes.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,14 @@ Clang Frontend Potentially Breaking Changes
135135
- The ``hasTypeLoc`` AST matcher will no longer match a ``classTemplateSpecializationDecl``;
136136
existing uses should switch to ``templateArgumentLoc`` or ``hasAnyTemplateArgumentLoc`` instead.
137137

138+
Clang Python Bindings Potentially Breaking Changes
139+
--------------------------------------------------
140+
- Renamed ``CursorKind`` variant 272 from ``OMP_TEAMS_DISTRIBUTE_DIRECTIVE``
141+
to ``OMP_TEAMS_DISTRIBUTE_SIMD_DIRECTIVE``. The previous name was incorrect, it was a duplicate
142+
of variant 271.
143+
- Renamed ``TypeKind`` variant 162 from ``OBJCCLASS`` to ``OBJCTYPEPARAM``.
144+
The previous name was incorrect, it was a duplicate of variant 28.
145+
138146
What's New in Clang |release|?
139147
==============================
140148
Some of the major new features and improvements to Clang are listed

0 commit comments

Comments
 (0)