[clang] Add comments and macros support to Python bindings (PR #81684)
https://github.com/dnmiller updated https://github.com/llvm/llvm-project/pull/81684 >From 0617712f89ef0193f6112680e25bd7b40002d6fd Mon Sep 17 00:00:00 2001 From: "Daniel N. Miller (APD)" Date: Tue, 13 Feb 2024 15:12:05 -0800 Subject: [PATCH] Add comments and macros support to Python bindings --- clang/bindings/python/clang/cindex.py | 349 ++ 1 file changed, 349 insertions(+) diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py index 44a34ca196274c..49b4a1f3a765a1 100644 --- a/clang/bindings/python/clang/cindex.py +++ b/clang/bindings/python/clang/cindex.py @@ -1378,6 +1378,7 @@ def __repr__(self): # A code completion overload candidate. CursorKind.OVERLOAD_CANDIDATE = CursorKind(700) + ### Template Argument Kinds ### class TemplateArgumentKind(BaseEnumeration): """ @@ -1396,6 +1397,7 @@ class TemplateArgumentKind(BaseEnumeration): TemplateArgumentKind.NULLPTR = TemplateArgumentKind(3) TemplateArgumentKind.INTEGRAL = TemplateArgumentKind(4) + ### Exception Specification Kinds ### class ExceptionSpecificationKind(BaseEnumeration): """ @@ -1907,6 +1909,11 @@ def raw_comment(self): """Returns the raw comment text associated with that Cursor""" return conf.lib.clang_Cursor_getRawCommentText(self) +@property +def parsed_comment(self): +"""Returns the parsed comment text associaetd with that Cursor""" +return conf.lib.clang_Cursor_getParsedComment(self) + def get_arguments(self): """Return an iterator for accessing the arguments of this cursor.""" num_args = conf.lib.clang_Cursor_getNumArguments(self) @@ -1994,6 +2001,18 @@ def get_bitfield_width(self): """ return conf.lib.clang_getFieldDeclBitWidth(self) +def is_macro_function(self): +""" +Check if the field is a macro function. +""" +return conf.lib.clang_Cursor_isMacroFunctionLike(self) + +def is_macro_builtin(self): +""" +Check if the field is a macro function. +""" +return conf.lib.clang_Cursor_isMacroBuiltin(self) + @staticmethod def from_result(res, fn, args): assert isinstance(res, Cursor) @@ -2243,6 +2262,74 @@ def __repr__(self): TypeKind.EXTVECTOR = TypeKind(176) TypeKind.ATOMIC = TypeKind(177) +### Comment Kinds ### + + +class CommentKind(BaseEnumeration): +""" +Describes the kind of comment. +""" + +# The unique kind objects, indexed by id. +_kinds = [] +_name_map = None + +def __repr__(self): +return "CommentKind.%s" % (self.name,) + + +CommentKind.NULL = CommentKind(0) +CommentKind.TEXT = CommentKind(1) +CommentKind.INLINECOMMAND = CommentKind(2) +CommentKind.HTMLSTARTTAG = CommentKind(3) +CommentKind.HTMLENDTAG = CommentKind(4) +CommentKind.PARAGRAPH = CommentKind(5) +CommentKind.BLOCKCOMMAND = CommentKind(6) +CommentKind.PARAMCOMMAND = CommentKind(7) +CommentKind.TPARAMCOMMAND = CommentKind(8) +CommentKind.VERBATIMBLOCKCOMMAND = CommentKind(9) +CommentKind.VERBATIMBLOCKLINE = CommentKind(10) +CommentKind.VERBATIMLINE = CommentKind(11) +CommentKind.FULLCOMMENT = CommentKind(12) + + +class CommentInlineCommandRenderKind(BaseEnumeration): +""" +Describes the kind of rendering mode of an inline command. +""" + +# The unique kind objects, indexed by id. +_kinds = [] +_name_map = None + +def __repr__(self): +return "CommentInlineCommandRenderKind.%s" % (self.name,) + + +CommentInlineCommandRenderKind.NORMAL = CommentInlineCommandRenderKind(0) +CommentInlineCommandRenderKind.BOLD = CommentInlineCommandRenderKind(1) +CommentInlineCommandRenderKind.MONOSPACED = CommentInlineCommandRenderKind(2) +CommentInlineCommandRenderKind.EMPHASIZE = CommentInlineCommandRenderKind(3) + + +class CommentParamPassDirection(BaseEnumeration): +""" +Describes the kind of parameter passing direction for \\param +or \\arg command +""" + +# The unique kind objects, indexed by id. +_kinds = [] +_name_map = None + +def __repr__(self): +return "CommentParamPassDirection.%s" % (self.name,) + + +CommentParamPassDirection.IN = CommentParamPassDirection(0) +CommentParamPassDirection.OUT = CommentParamPassDirection(1) +CommentParamPassDirection.INOU = CommentParamPassDirection(2) + class RefQualifierKind(BaseEnumeration): """Describes a specific ref-qualifier of a type.""" @@ -3574,6 +3661,187 @@ def write_main_file_to_stdout(self): callbacks["cursor_visit"] = CFUNCTYPE(c_int, Cursor, Cursor, py_object) callbacks["fields_visit"] = CFUNCTYPE(c_int, Cursor, py_object) + +class CXTranslationUnitImpl(Structure): +pass # opaque structure + + +CXTranslationUnit = POINTER(CXTranslationUnitImpl) + + +class Comment(Structure): +_fields_ = [("ASTNode", c_void_p), ("TranslationUnit", CXTranslationUnit)] + +def get_text(self): +return conf.lib.clang_TextComment_getText(self) + +
[clang] Add comments and macros support to Python bindings (PR #81684)
github-actions[bot] wrote: :warning: Python code formatter, darker found issues in your code. :warning: You can test this locally with the following command: ``bash darker --check --diff -r fd3a0c185f177351207783fc2a604dac086cdaf7...497478f08570f1333966c0843d713268bc9ba5c7 clang/bindings/python/clang/cindex.py `` View the diff from darker here. ``diff --- cindex.py 2024-02-13 23:12:05.00 + +++ cindex.py 2024-02-13 23:17:41.291558 + @@ -2260,21 +2260,22 @@ TypeKind.EXTVECTOR = TypeKind(176) TypeKind.ATOMIC = TypeKind(177) ### Comment Kinds ### + class CommentKind(BaseEnumeration): """ Describes the kind of comment. """ # The unique kind objects, indexed by id. _kinds = [] _name_map = None def __repr__(self): -return 'CommentKind.%s' % (self.name,) +return "CommentKind.%s" % (self.name,) CommentKind.NULL = CommentKind(0) CommentKind.TEXT = CommentKind(1) CommentKind.INLINECOMMAND = CommentKind(2) @@ -2292,16 +2293,17 @@ class CommentInlineCommandRenderKind(BaseEnumeration): """ Describes the kind of rendering mode of an inline command. """ + # The unique kind objects, indexed by id. _kinds = [] _name_map = None def __repr__(self): -return 'CommentInlineCommandRenderKind.%s' % (self.name,) +return "CommentInlineCommandRenderKind.%s" % (self.name,) CommentInlineCommandRenderKind.NORMAL = CommentInlineCommandRenderKind(0) CommentInlineCommandRenderKind.BOLD = CommentInlineCommandRenderKind(1) CommentInlineCommandRenderKind.MONOSPACED = CommentInlineCommandRenderKind(2) @@ -2311,22 +2313,22 @@ class CommentParamPassDirection(BaseEnumeration): """ Describes the kind of parameter passing direction for \\param or \\arg command """ + # The unique kind objects, indexed by id. _kinds = [] _name_map = None def __repr__(self): -return 'CommentParamPassDirection.%s' % (self.name,) +return "CommentParamPassDirection.%s" % (self.name,) CommentParamPassDirection.IN = CommentParamPassDirection(0) CommentParamPassDirection.OUT = CommentParamPassDirection(1) CommentParamPassDirection.INOU = CommentParamPassDirection(2) - class RefQualifierKind(BaseEnumeration): """Describes a specific ref-qualifier of a type.""" @@ -3655,14 +3657,17 @@ None, c_object_p, POINTER(SourceLocation), c_uint, py_object ) callbacks["cursor_visit"] = CFUNCTYPE(c_int, Cursor, Cursor, py_object) callbacks["fields_visit"] = CFUNCTYPE(c_int, Cursor, py_object) + class CXTranslationUnitImpl(Structure): -pass # opaque structure +pass # opaque structure + CXTranslationUnit = POINTER(CXTranslationUnitImpl) + class Comment(Structure): _fields_ = [("ASTNode", c_void_p), ("TranslationUnit", CXTranslationUnit)] def get_text(self): @@ -3678,10 +3683,11 @@ """Get number of child nodes.""" return conf.lib.clang_Comment_getNumChildren(self) def get_children(self): """Return an iterator for accessing the children of this comment.""" + def cast_child(child): if child.kind == CommentKind.INLINECOMMAND: child.__class__ = InlineCommand if child.kind == CommentKind.HTMLSTARTTAG: child.__class__ = HTMLComment @@ -3690,23 +3696,25 @@ if child.kind == CommentKind.BLOCKCOMMAND: child.__class__ = BlockCommandComment if child.kind == CommentKind.PARAMCOMMAND: child.__class__ = ParamCommandComment if child.kind == CommentKind.TPARAMCOMMAND: -child.__class__ = TParamCommandComment +child.__class__ = TParamCommandComment if child.kind == CommentKind.VERBATIMBLOCKLINE: child.__class__ = VerbatimBlockLineComment if child.kind == CommentKind.VERBATIMLINE: child.__class__ = VerbatimLineComment if child.kind == CommentKind.FULLCOMMENT: child.__class__ = FullComment # if child.kind == CommentKind.PARAGRAPH: # if child.kind == CommentKind.VERBATIMBLOCKCOMMAND: return child -return (cast_child(conf.lib.clang_Comment_getChild(self, i)) -for i in range(self.num_children())) +return ( +cast_child(conf.lib.clang_Comment_getChild(self, i)) +for i in range(self.num_children()) +) def is_whitespace(self): """Check if all paragraph nodes are space or empty.""" return conf.lib.clang_Comment_isWhitespace(self) @@ -3727,12 +3735,14 @@ def get_num_args(self): conf.lib.clang_InlineCommandComment_getNumArgs(self) def get_args(self): -return (conf.lib.clang_InlineCommandComment_getArgText(self, i) -for i in
[clang] Add comments and macros support to Python bindings (PR #81684)
llvmbot wrote: @llvm/pr-subscribers-clang Author: Dan Miller (dnmiller) Changes The Python bindings currently do not expose the comment parsing engine and are missing a few macro utility functions. This adds bindings for the comments. --- Full diff: https://github.com/llvm/llvm-project/pull/81684.diff 1 Files Affected: - (modified) clang/bindings/python/clang/cindex.py (+284) ``diff diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py index 44a34ca196274c..542bc0172a56fa 100644 --- a/clang/bindings/python/clang/cindex.py +++ b/clang/bindings/python/clang/cindex.py @@ -1907,6 +1907,11 @@ def raw_comment(self): """Returns the raw comment text associated with that Cursor""" return conf.lib.clang_Cursor_getRawCommentText(self) +@property +def parsed_comment(self): +"""Returns the parsed comment text associaetd with that Cursor""" +return conf.lib.clang_Cursor_getParsedComment(self) + def get_arguments(self): """Return an iterator for accessing the arguments of this cursor.""" num_args = conf.lib.clang_Cursor_getNumArguments(self) @@ -1994,6 +1999,18 @@ def get_bitfield_width(self): """ return conf.lib.clang_getFieldDeclBitWidth(self) +def is_macro_function(self): +""" +Check if the field is a macro function. +""" +return conf.lib.clang_Cursor_isMacroFunctionLike(self) + +def is_macro_builtin(self): +""" +Check if the field is a macro function. +""" +return conf.lib.clang_Cursor_isMacroBuiltin(self) + @staticmethod def from_result(res, fn, args): assert isinstance(res, Cursor) @@ -2243,6 +2260,72 @@ def __repr__(self): TypeKind.EXTVECTOR = TypeKind(176) TypeKind.ATOMIC = TypeKind(177) +### Comment Kinds ### + +class CommentKind(BaseEnumeration): +""" +Describes the kind of comment. +""" + +# The unique kind objects, indexed by id. +_kinds = [] +_name_map = None + +def __repr__(self): +return 'CommentKind.%s' % (self.name,) + + +CommentKind.NULL = CommentKind(0) +CommentKind.TEXT = CommentKind(1) +CommentKind.INLINECOMMAND = CommentKind(2) +CommentKind.HTMLSTARTTAG = CommentKind(3) +CommentKind.HTMLENDTAG = CommentKind(4) +CommentKind.PARAGRAPH = CommentKind(5) +CommentKind.BLOCKCOMMAND = CommentKind(6) +CommentKind.PARAMCOMMAND = CommentKind(7) +CommentKind.TPARAMCOMMAND = CommentKind(8) +CommentKind.VERBATIMBLOCKCOMMAND = CommentKind(9) +CommentKind.VERBATIMBLOCKLINE = CommentKind(10) +CommentKind.VERBATIMLINE = CommentKind(11) +CommentKind.FULLCOMMENT = CommentKind(12) + + +class CommentInlineCommandRenderKind(BaseEnumeration): +""" +Describes the kind of rendering mode of an inline command. +""" +# The unique kind objects, indexed by id. +_kinds = [] +_name_map = None + +def __repr__(self): +return 'CommentInlineCommandRenderKind.%s' % (self.name,) + + +CommentInlineCommandRenderKind.NORMAL = CommentInlineCommandRenderKind(0) +CommentInlineCommandRenderKind.BOLD = CommentInlineCommandRenderKind(1) +CommentInlineCommandRenderKind.MONOSPACED = CommentInlineCommandRenderKind(2) +CommentInlineCommandRenderKind.EMPHASIZE = CommentInlineCommandRenderKind(3) + + +class CommentParamPassDirection(BaseEnumeration): +""" +Describes the kind of parameter passing direction for \\param +or \\arg command +""" +# The unique kind objects, indexed by id. +_kinds = [] +_name_map = None + +def __repr__(self): +return 'CommentParamPassDirection.%s' % (self.name,) + + +CommentParamPassDirection.IN = CommentParamPassDirection(0) +CommentParamPassDirection.OUT = CommentParamPassDirection(1) +CommentParamPassDirection.INOU = CommentParamPassDirection(2) + + class RefQualifierKind(BaseEnumeration): """Describes a specific ref-qualifier of a type.""" @@ -3574,6 +3657,171 @@ def write_main_file_to_stdout(self): callbacks["cursor_visit"] = CFUNCTYPE(c_int, Cursor, Cursor, py_object) callbacks["fields_visit"] = CFUNCTYPE(c_int, Cursor, py_object) +class CXTranslationUnitImpl(Structure): +pass # opaque structure + +CXTranslationUnit = POINTER(CXTranslationUnitImpl) + +class Comment(Structure): +_fields_ = [("ASTNode", c_void_p), ("TranslationUnit", CXTranslationUnit)] + +def get_text(self): +return conf.lib.clang_TextComment_getText(self) + +@property +def kind(self): +"""Return the kind of this comment.""" +kind_id = conf.lib.clang_Comment_getKind(self) +return CommentKind.from_id(kind_id) + +def num_children(self): +"""Get number of child nodes.""" +return conf.lib.clang_Comment_getNumChildren(self) + +def get_children(self): +"""Return an iterator for accessing the children of this comment.""" +def cast_child(child): +if child.kind == CommentKind.INLINECOMMAND: +
[clang] Add comments and macros support to Python bindings (PR #81684)
https://github.com/dnmiller created https://github.com/llvm/llvm-project/pull/81684 The Python bindings currently do not expose the comment parsing engine and are missing a few macro utility functions. This adds bindings for the comments. >From 497478f08570f1333966c0843d713268bc9ba5c7 Mon Sep 17 00:00:00 2001 From: "Daniel N. Miller (APD)" Date: Tue, 13 Feb 2024 15:12:05 -0800 Subject: [PATCH] Add comments and macros support to Python bindings --- clang/bindings/python/clang/cindex.py | 284 ++ 1 file changed, 284 insertions(+) diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py index 44a34ca196274c..542bc0172a56fa 100644 --- a/clang/bindings/python/clang/cindex.py +++ b/clang/bindings/python/clang/cindex.py @@ -1907,6 +1907,11 @@ def raw_comment(self): """Returns the raw comment text associated with that Cursor""" return conf.lib.clang_Cursor_getRawCommentText(self) +@property +def parsed_comment(self): +"""Returns the parsed comment text associaetd with that Cursor""" +return conf.lib.clang_Cursor_getParsedComment(self) + def get_arguments(self): """Return an iterator for accessing the arguments of this cursor.""" num_args = conf.lib.clang_Cursor_getNumArguments(self) @@ -1994,6 +1999,18 @@ def get_bitfield_width(self): """ return conf.lib.clang_getFieldDeclBitWidth(self) +def is_macro_function(self): +""" +Check if the field is a macro function. +""" +return conf.lib.clang_Cursor_isMacroFunctionLike(self) + +def is_macro_builtin(self): +""" +Check if the field is a macro function. +""" +return conf.lib.clang_Cursor_isMacroBuiltin(self) + @staticmethod def from_result(res, fn, args): assert isinstance(res, Cursor) @@ -2243,6 +2260,72 @@ def __repr__(self): TypeKind.EXTVECTOR = TypeKind(176) TypeKind.ATOMIC = TypeKind(177) +### Comment Kinds ### + +class CommentKind(BaseEnumeration): +""" +Describes the kind of comment. +""" + +# The unique kind objects, indexed by id. +_kinds = [] +_name_map = None + +def __repr__(self): +return 'CommentKind.%s' % (self.name,) + + +CommentKind.NULL = CommentKind(0) +CommentKind.TEXT = CommentKind(1) +CommentKind.INLINECOMMAND = CommentKind(2) +CommentKind.HTMLSTARTTAG = CommentKind(3) +CommentKind.HTMLENDTAG = CommentKind(4) +CommentKind.PARAGRAPH = CommentKind(5) +CommentKind.BLOCKCOMMAND = CommentKind(6) +CommentKind.PARAMCOMMAND = CommentKind(7) +CommentKind.TPARAMCOMMAND = CommentKind(8) +CommentKind.VERBATIMBLOCKCOMMAND = CommentKind(9) +CommentKind.VERBATIMBLOCKLINE = CommentKind(10) +CommentKind.VERBATIMLINE = CommentKind(11) +CommentKind.FULLCOMMENT = CommentKind(12) + + +class CommentInlineCommandRenderKind(BaseEnumeration): +""" +Describes the kind of rendering mode of an inline command. +""" +# The unique kind objects, indexed by id. +_kinds = [] +_name_map = None + +def __repr__(self): +return 'CommentInlineCommandRenderKind.%s' % (self.name,) + + +CommentInlineCommandRenderKind.NORMAL = CommentInlineCommandRenderKind(0) +CommentInlineCommandRenderKind.BOLD = CommentInlineCommandRenderKind(1) +CommentInlineCommandRenderKind.MONOSPACED = CommentInlineCommandRenderKind(2) +CommentInlineCommandRenderKind.EMPHASIZE = CommentInlineCommandRenderKind(3) + + +class CommentParamPassDirection(BaseEnumeration): +""" +Describes the kind of parameter passing direction for \\param +or \\arg command +""" +# The unique kind objects, indexed by id. +_kinds = [] +_name_map = None + +def __repr__(self): +return 'CommentParamPassDirection.%s' % (self.name,) + + +CommentParamPassDirection.IN = CommentParamPassDirection(0) +CommentParamPassDirection.OUT = CommentParamPassDirection(1) +CommentParamPassDirection.INOU = CommentParamPassDirection(2) + + class RefQualifierKind(BaseEnumeration): """Describes a specific ref-qualifier of a type.""" @@ -3574,6 +3657,171 @@ def write_main_file_to_stdout(self): callbacks["cursor_visit"] = CFUNCTYPE(c_int, Cursor, Cursor, py_object) callbacks["fields_visit"] = CFUNCTYPE(c_int, Cursor, py_object) +class CXTranslationUnitImpl(Structure): +pass # opaque structure + +CXTranslationUnit = POINTER(CXTranslationUnitImpl) + +class Comment(Structure): +_fields_ = [("ASTNode", c_void_p), ("TranslationUnit", CXTranslationUnit)] + +def get_text(self): +return conf.lib.clang_TextComment_getText(self) + +@property +def kind(self): +"""Return the kind of this comment.""" +kind_id = conf.lib.clang_Comment_getKind(self) +return CommentKind.from_id(kind_id) + +def num_children(self): +"""Get number of child nodes.""" +return conf.lib.clang_Comment_getNumChildren(self) + +def get_children(self): +"""Return
[clang] Add comments and macros support to Python bindings (PR #81684)
github-actions[bot] wrote: 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](https://llvm.org/docs/GitHub.html). You can also ask questions in a comment on this PR, on the [LLVM Discord](https://discord.com/invite/xS7Z362) or on the [forums](https://discourse.llvm.org/). https://github.com/llvm/llvm-project/pull/81684 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits