Skip to content

[libclang/python] Fix bugs in custom enum implementation and add tests #95381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 14, 2024

Conversation

DeinAlptraum
Copy link
Contributor

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

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the clang Clang issues not falling into any other category label Jun 13, 2024
@llvmbot
Copy link
Member

llvmbot commented Jun 13, 2024

@llvm/pr-subscribers-clang

Author: Jannick Kremer (DeinAlptraum)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/95381.diff

2 Files Affected:

  • (modified) clang/bindings/python/clang/cindex.py (+4-4)
  • (added) clang/bindings/python/tests/cindex/test_enums.py (+55)
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..425c4a8274a17
--- /dev/null
+++ b/clang/bindings/python/tests/cindex/test_enums.py
@@ -0,0 +1,55 @@
+import unittest
+
+from clang.cindex import (
+    CursorKind,
+    TemplateArgumentKind,
+    ExceptionSpecificationKind,
+    AvailabilityKind,
+    AccessSpecifier,
+    TypeKind,
+    RefQualifierKind,
+    LinkageKind,
+    TLSKind,
+    StorageClass,
+)
+
+
+def get_all_kinds(enum):
+    """Return all CursorKind enumeration instances."""
+    return [x for x in enum._kinds if not x is None]
+
+
+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)

@DeinAlptraum
Copy link
Contributor Author

@Endilll I separated the fixes for the enum bugs from the strict typing PR, and added tests that cover these cases. I checked that the tests fail before the fixes and succeed now.

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
Also simplify the enum test
@Endilll
Copy link
Contributor

Endilll commented Jun 13, 2024

Let me know when you think this is good to go. I'll merge it for you.

@DeinAlptraum
Copy link
Contributor Author

These were all the bugs I'd found, so I think it is

@Endilll
Copy link
Contributor

Endilll commented Jun 13, 2024

This depends on #95210 to pass the CI. Let's merge that first.

@DeinAlptraum
Copy link
Contributor Author

DeinAlptraum commented Jun 13, 2024

@Endilll I merged the Python 3.8 CI update into this PR and the CI run was successful (though it only ran on 3.8 for some reason), so can this be merged?

@Endilll
Copy link
Contributor

Endilll commented Jun 13, 2024

Let's wait for CI to pass.

@Endilll Endilll merged commit 88e42c6 into llvm:main Jun 14, 2024
9 checks passed
Copy link

@DeinAlptraum Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested
by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as
the builds can include changes from many authors. It is not uncommon for your
change to be included in a build that fails due to someone else's changes, or
infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself.
This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@DeinAlptraum DeinAlptraum deleted the fix-enum-bugs branch June 14, 2024 09:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants