Skip to content

Commit a3da142

Browse files
committed
[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
1 parent 24c6579 commit a3da142

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
seen_names = set()
44+
for id in range(len(enum._kinds)):
45+
try:
46+
kind_name = enum.from_id(id).name
47+
except ValueError:
48+
continue
49+
self.assertNotIn(kind_name, seen_names)
50+
seen_names.add(id)

0 commit comments

Comments
 (0)