https://github.com/DeinAlptraum updated https://github.com/llvm/llvm-project/pull/160296
>From cfaead063da5bf716e65981929a65be69d22e7ba Mon Sep 17 00:00:00 2001 From: Jannick Kremer <[email protected]> Date: Tue, 23 Sep 2025 22:26:44 +0900 Subject: [PATCH 1/9] Use existing AvailabilityKind enum for code completion availability --- clang/bindings/python/clang/cindex.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py index 2b6ab00c88219..8bee77424d1f4 100644 --- a/clang/bindings/python/clang/cindex.py +++ b/clang/bindings/python/clang/cindex.py @@ -3051,6 +3051,16 @@ class _CXUnsavedFile(Structure): } +# Converting the new enum names (full upper-case, underscore separated) +# to the old ones (separated by capitalization), e.g. RESULT_TYPE -> ResultType +def _kind_to_old_name(kind: BaseEnumeration): + # Remove underscores + components = kind.name.split("_") + # Upper-camel case each split component + components = [component.lower().capitalize() for component in components] + return "".join(components) + + class CompletionChunk: class Kind: def __init__(self, name: str): @@ -3177,9 +3187,9 @@ def priority(self) -> int: return conf.lib.clang_getCompletionPriority(self.obj) # type: ignore [no-any-return] @property - def availability(self) -> CompletionChunk.Kind: + def availability(self) -> AvailabilityKind: res = conf.lib.clang_getCompletionAvailability(self.obj) - return availabilityKinds[res] + return AvailabilityKind.from_id(res) @property def briefComment(self) -> str: @@ -3191,20 +3201,12 @@ def __repr__(self) -> str: + " || Priority: " + str(self.priority) + " || Availability: " - + str(self.availability) + + _kind_to_old_name(self.availability) + " || Brief comment: " + str(self.briefComment) ) -availabilityKinds = { - 0: CompletionChunk.Kind("Available"), - 1: CompletionChunk.Kind("Deprecated"), - 2: CompletionChunk.Kind("NotAvailable"), - 3: CompletionChunk.Kind("NotAccessible"), -} - - class CodeCompletionResult(Structure): _fields_ = [("cursorKind", c_int), ("completionString", c_object_p)] >From 1b19b6d6d73804b23630518f6e55b37268f6f184 Mon Sep 17 00:00:00 2001 From: Jannick Kremer <[email protected]> Date: Wed, 24 Sep 2025 22:18:25 +0900 Subject: [PATCH 2/9] Turn comment into docstring --- clang/bindings/python/clang/cindex.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py index 8bee77424d1f4..989ba5ada2f0a 100644 --- a/clang/bindings/python/clang/cindex.py +++ b/clang/bindings/python/clang/cindex.py @@ -3051,9 +3051,11 @@ class _CXUnsavedFile(Structure): } -# Converting the new enum names (full upper-case, underscore separated) -# to the old ones (separated by capitalization), e.g. RESULT_TYPE -> ResultType def _kind_to_old_name(kind: BaseEnumeration): + """ + Converting the new enum names (full upper-case, underscore separated) + to the old ones (separated by capitalization), e.g. RESULT_TYPE -> ResultType + """ # Remove underscores components = kind.name.split("_") # Upper-camel case each split component >From 1aef182a29c0187929eb9003abf0241ba6f92b56 Mon Sep 17 00:00:00 2001 From: Jannick Kremer <[email protected]> Date: Wed, 24 Sep 2025 22:19:19 +0900 Subject: [PATCH 3/9] Change converter function name --- clang/bindings/python/clang/cindex.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py index 989ba5ada2f0a..75422b92f7f58 100644 --- a/clang/bindings/python/clang/cindex.py +++ b/clang/bindings/python/clang/cindex.py @@ -3203,7 +3203,7 @@ def __repr__(self) -> str: + " || Priority: " + str(self.priority) + " || Availability: " - + _kind_to_old_name(self.availability) + + _convert_screaming_caps_to_pascal_case(self.availability) + " || Brief comment: " + str(self.briefComment) ) >From 599ac2f4a072aa41bcd86253c9e7e07de68924b3 Mon Sep 17 00:00:00 2001 From: Jannick Kremer <[email protected]> Date: Wed, 24 Sep 2025 17:02:44 +0200 Subject: [PATCH 4/9] Update clang/bindings/python/clang/cindex.py Co-authored-by: Vlad Serebrennikov <[email protected]> --- clang/bindings/python/clang/cindex.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py index 75422b92f7f58..288007307552d 100644 --- a/clang/bindings/python/clang/cindex.py +++ b/clang/bindings/python/clang/cindex.py @@ -3051,7 +3051,7 @@ class _CXUnsavedFile(Structure): } -def _kind_to_old_name(kind: BaseEnumeration): +def _convert_screaming_caps_to_pascal_case(kind: BaseEnumeration): """ Converting the new enum names (full upper-case, underscore separated) to the old ones (separated by capitalization), e.g. RESULT_TYPE -> ResultType >From 706ca9749197754b5c0157bd3548dda3be3ed065 Mon Sep 17 00:00:00 2001 From: Jannick Kremer <[email protected]> Date: Sun, 4 Jan 2026 17:24:03 +0900 Subject: [PATCH 5/9] Remove unused CompletionString.Availability --- clang/bindings/python/clang/cindex.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py index 288007307552d..50471439fe9ac 100644 --- a/clang/bindings/python/clang/cindex.py +++ b/clang/bindings/python/clang/cindex.py @@ -3155,16 +3155,6 @@ def isKindResultType(self) -> bool: class CompletionString(ClangObject): - class Availability: - def __init__(self, name): - self.name = name - - def __str__(self): - return self.name - - def __repr__(self): - return "<Availability: %s>" % self - def __len__(self) -> int: return self.num_chunks >From 40836b4dd0a6fe56182b3cabe0e886f22e65cf64 Mon Sep 17 00:00:00 2001 From: Jannick Kremer <[email protected]> Date: Sun, 4 Jan 2026 17:27:23 +0900 Subject: [PATCH 6/9] Pass string instead of enum variant to converter --- clang/bindings/python/clang/cindex.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py index 50471439fe9ac..6a2e54aed5c53 100644 --- a/clang/bindings/python/clang/cindex.py +++ b/clang/bindings/python/clang/cindex.py @@ -3051,13 +3051,13 @@ class _CXUnsavedFile(Structure): } -def _convert_screaming_caps_to_pascal_case(kind: BaseEnumeration): +def _convert_screaming_caps_to_pascal_case(kind: str): """ Converting the new enum names (full upper-case, underscore separated) to the old ones (separated by capitalization), e.g. RESULT_TYPE -> ResultType """ # Remove underscores - components = kind.name.split("_") + components = kind.split("_") # Upper-camel case each split component components = [component.lower().capitalize() for component in components] return "".join(components) @@ -3193,7 +3193,7 @@ def __repr__(self) -> str: + " || Priority: " + str(self.priority) + " || Availability: " - + _convert_screaming_caps_to_pascal_case(self.availability) + + _convert_screaming_caps_to_pascal_case(self.availability.name) + " || Brief comment: " + str(self.briefComment) ) >From b65376d9d2554490b2e3f1d58563aeb9b2fa30a7 Mon Sep 17 00:00:00 2001 From: Jannick Kremer <[email protected]> Date: Mon, 5 Jan 2026 01:17:33 +0900 Subject: [PATCH 7/9] Add AvailabilityKindCompat --- clang/bindings/python/clang/cindex.py | 52 +++++++++++++------ .../tests/cindex/test_code_completion.py | 37 ++++++++++++- 2 files changed, 72 insertions(+), 17 deletions(-) diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py index 6a2e54aed5c53..a51023c2d9c5d 100644 --- a/clang/bindings/python/clang/cindex.py +++ b/clang/bindings/python/clang/cindex.py @@ -3051,18 +3051,6 @@ class _CXUnsavedFile(Structure): } -def _convert_screaming_caps_to_pascal_case(kind: str): - """ - Converting the new enum names (full upper-case, underscore separated) - to the old ones (separated by capitalization), e.g. RESULT_TYPE -> ResultType - """ - # Remove underscores - components = kind.split("_") - # Upper-camel case each split component - components = [component.lower().capitalize() for component in components] - return "".join(components) - - class CompletionChunk: class Kind: def __init__(self, name: str): @@ -3179,9 +3167,9 @@ def priority(self) -> int: return conf.lib.clang_getCompletionPriority(self.obj) # type: ignore [no-any-return] @property - def availability(self) -> AvailabilityKind: + def availability(self) -> AvailabilityKindCompat: res = conf.lib.clang_getCompletionAvailability(self.obj) - return AvailabilityKind.from_id(res) + return AvailabilityKindCompat.from_id(res) @property def briefComment(self) -> str: @@ -3193,11 +3181,45 @@ def __repr__(self) -> str: + " || Priority: " + str(self.priority) + " || Availability: " - + _convert_screaming_caps_to_pascal_case(self.availability.name) + + str(self.availability) + " || Brief comment: " + str(self.briefComment) ) +# AvailabilityKindCompat is an exact copy of AvailabilityKind, except for __str__ +# This is a temporary measure to keep the string representation the same +# until we unify the return of CompletionString.availability to be AvailabilityKind +# Note that deriving from AvailabilityKind directly is not possible +class AvailabilityKindCompat(BaseEnumeration): + """ + Describes the availability of an entity. + """ + + # Ensure AvailabilityKindCompat is comparable with AvailabilityKind + def __eq__(self, other: object) -> bool: + if isinstance(other, AvailabilityKind): + return self.value == other.value + else: + return NotImplemented + + def __str__(self) -> str: + """ + Returns the common enum names (full upper-case, underscore separated) + to the old format (separated by capitalization), e.g. NOT_ACCESSIBLE -> NotAccessible + This is a temporary measure and will be changed in a future release + to return the same format (full upper-case, underscore separated) like other Kinds + """ + # Remove underscores + components = self.name.split("_") + # Upper-camel case each split component + components = [component.lower().capitalize() for component in components] + return "".join(components) + + AVAILABLE = 0 + DEPRECATED = 1 + NOT_AVAILABLE = 2 + NOT_ACCESSIBLE = 3 + class CodeCompletionResult(Structure): _fields_ = [("cursorKind", c_int), ("completionString", c_object_p)] diff --git a/clang/bindings/python/tests/cindex/test_code_completion.py b/clang/bindings/python/tests/cindex/test_code_completion.py index 32b75eb1ae029..f2ebc7ab73fcd 100644 --- a/clang/bindings/python/tests/cindex/test_code_completion.py +++ b/clang/bindings/python/tests/cindex/test_code_completion.py @@ -1,5 +1,4 @@ -from clang.cindex import TranslationUnit - +from clang.cindex import TranslationUnit, AvailabilityKind, AvailabilityKindCompat import unittest from pathlib import Path @@ -137,3 +136,37 @@ class Q : public P { "{'void', ResultType} | {'~P', TypedText} | {'(', LeftParen} | {')', RightParen} || Priority: 79 || Availability: Available || Brief comment: ", ] self.check_completion_results(cr, expected) + + def test_availability_kind_compat_(self): + numKinds = len(AvailabilityKindCompat) + + # Compare with regular kind + for compatKind in AvailabilityKindCompat: + commonKind = AvailabilityKind.from_id(compatKind.value) + nextKindId = (compatKind.value+1) % numKinds + commonKindUnequal= AvailabilityKind.from_id(nextKindId) + self.assertEqual(commonKind, compatKind) + self.assertEqual(compatKind, commonKind) + self.assertNotEqual(commonKindUnequal, compatKind) + self.assertNotEqual(compatKind, commonKindUnequal) + + # Compare two compat kinds + for compatKind in AvailabilityKindCompat: + compatKind2 = AvailabilityKindCompat.from_id(compatKind.value) + nextKindId = (compatKind.value+1) % numKinds + compatKind2Unequal = AvailabilityKind.from_id(nextKindId) + self.assertEqual(compatKind, compatKind2) + self.assertEqual(compatKind2, compatKind) + self.assertNotEqual(compatKind2Unequal, compatKind) + self.assertNotEqual(compatKind, compatKind2Unequal) + + def test_compat_str(self): + kindStringMap = { + 0: "Available", + 1: "Deprecated", + 2: "NotAvailable", + 3: "NotAccessible", + } + for id, string in kindStringMap.items(): + kind = AvailabilityKindCompat.from_id(id) + self.assertEqual(str(kind), string) >From 9520237753e49515adc3601750476f4bc932ddac Mon Sep 17 00:00:00 2001 From: Jannick Kremer <[email protected]> Date: Mon, 5 Jan 2026 01:36:47 +0900 Subject: [PATCH 8/9] Add deprecation warning --- clang/bindings/python/clang/cindex.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py index a51023c2d9c5d..ebb3a4f538c5c 100644 --- a/clang/bindings/python/clang/cindex.py +++ b/clang/bindings/python/clang/cindex.py @@ -84,6 +84,7 @@ import os import sys from enum import Enum +import warnings from typing import ( Any, @@ -3186,6 +3187,7 @@ def __repr__(self) -> str: + str(self.briefComment) ) + # AvailabilityKindCompat is an exact copy of AvailabilityKind, except for __str__ # This is a temporary measure to keep the string representation the same # until we unify the return of CompletionString.availability to be AvailabilityKind @@ -3209,6 +3211,14 @@ def __str__(self) -> str: This is a temporary measure and will be changed in a future release to return the same format (full upper-case, underscore separated) like other Kinds """ + + warnings.warn( + "The CompletionString.availability's return value will be changed in " + "a future release to be unified with other kinds. As a result, its string " + "representations will change to full upper-case, prefixed with the class name " + "(e.g. NotAvailable -> AvailabilityKind.NOT_AVAILABLE).", + DeprecationWarning, + ) # Remove underscores components = self.name.split("_") # Upper-camel case each split component >From 0da816572055b850eb0306208620a6c92bc6f566 Mon Sep 17 00:00:00 2001 From: Jannick Kremer <[email protected]> Date: Mon, 5 Jan 2026 01:48:29 +0900 Subject: [PATCH 9/9] Fix enums test --- clang/bindings/python/tests/cindex/test_enums.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/clang/bindings/python/tests/cindex/test_enums.py b/clang/bindings/python/tests/cindex/test_enums.py index 0d3453e602edf..d290686b5ee71 100644 --- a/clang/bindings/python/tests/cindex/test_enums.py +++ b/clang/bindings/python/tests/cindex/test_enums.py @@ -4,6 +4,7 @@ from clang.cindex import ( AccessSpecifier, AvailabilityKind, + AvailabilityKindCompat, BinaryOperator, CursorKind, ExceptionSpecificationKind, @@ -22,7 +23,10 @@ class TestEnums(unittest.TestCase): + # Test all enum classes, except for AvailabilityKindCompat since it is + # just a copy of AvailabilityKind and has no corresponding C-class enums = BaseEnumeration.__subclasses__() + enums.remove(AvailabilityKindCompat) def test_from_id(self): """Check that kinds can be constructed from valid IDs""" _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
