Skip to content

Commit e6d061a

Browse files
[libclang/python] Add python bindings for PrintingPolicy (#120494)
This allows changing the way pretty-printed code is formatted.
1 parent 3caa68a commit e6d061a

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

clang/bindings/python/clang/cindex.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,6 +1770,16 @@ def spelling(self):
17701770

17711771
return self._spelling
17721772

1773+
def pretty_printed(self, policy):
1774+
"""
1775+
Pretty print declarations.
1776+
Parameters:
1777+
policy -- The policy to control the entities being printed.
1778+
"""
1779+
return _CXString.from_result(
1780+
conf.lib.clang_getCursorPrettyPrinted(self, policy)
1781+
)
1782+
17731783
@property
17741784
def displayname(self):
17751785
"""
@@ -3699,6 +3709,72 @@ def write_main_file_to_stdout(self):
36993709
conf.lib.clang_CXRewriter_writeMainFileToStdOut(self)
37003710

37013711

3712+
class PrintingPolicyProperty(BaseEnumeration):
3713+
3714+
"""
3715+
A PrintingPolicyProperty identifies a property of a PrintingPolicy.
3716+
"""
3717+
3718+
Indentation = 0
3719+
SuppressSpecifiers = 1
3720+
SuppressTagKeyword = 2
3721+
IncludeTagDefinition = 3
3722+
SuppressScope = 4
3723+
SuppressUnwrittenScope = 5
3724+
SuppressInitializers = 6
3725+
ConstantArraySizeAsWritten = 7
3726+
AnonymousTagLocations = 8
3727+
SuppressStrongLifetime = 9
3728+
SuppressLifetimeQualifiers = 10
3729+
SuppressTemplateArgsInCXXConstructors = 11
3730+
Bool = 12
3731+
Restrict = 13
3732+
Alignof = 14
3733+
UnderscoreAlignof = 15
3734+
UseVoidForZeroParams = 16
3735+
TerseOutput = 17
3736+
PolishForDeclaration = 18
3737+
Half = 19
3738+
MSWChar = 20
3739+
IncludeNewlines = 21
3740+
MSVCFormatting = 22
3741+
ConstantsAsWritten = 23
3742+
SuppressImplicitBase = 24
3743+
FullyQualifiedName = 25
3744+
3745+
3746+
class PrintingPolicy(ClangObject):
3747+
"""
3748+
The PrintingPolicy is a wrapper class around clang::PrintingPolicy
3749+
3750+
It allows specifying how declarations, expressions, and types should be
3751+
pretty-printed.
3752+
"""
3753+
3754+
@staticmethod
3755+
def create(cursor):
3756+
"""
3757+
Creates a new PrintingPolicy
3758+
Parameters:
3759+
cursor -- Any cursor for a translation unit.
3760+
"""
3761+
return PrintingPolicy(conf.lib.clang_getCursorPrintingPolicy(cursor))
3762+
3763+
def __init__(self, ptr):
3764+
ClangObject.__init__(self, ptr)
3765+
3766+
def __del__(self):
3767+
conf.lib.clang_PrintingPolicy_dispose(self)
3768+
3769+
def get_property(self, property):
3770+
"""Get a property value for the given printing policy."""
3771+
return conf.lib.clang_PrintingPolicy_getProperty(self, property.value)
3772+
3773+
def set_property(self, property, value):
3774+
"""Set a property value for the given printing policy."""
3775+
conf.lib.clang_PrintingPolicy_setProperty(self, property.value, value)
3776+
3777+
37023778
# Now comes the plumbing to hook up the C library.
37033779

37043780
# Register callback types
@@ -3801,6 +3877,8 @@ def write_main_file_to_stdout(self):
38013877
("clang_getCursorExtent", [Cursor], SourceRange),
38023878
("clang_getCursorLexicalParent", [Cursor], Cursor),
38033879
("clang_getCursorLocation", [Cursor], SourceLocation),
3880+
("clang_getCursorPrettyPrinted", [Cursor, PrintingPolicy], _CXString),
3881+
("clang_getCursorPrintingPolicy", [Cursor], c_object_p),
38043882
("clang_getCursorReferenced", [Cursor], Cursor),
38053883
("clang_getCursorReferenceNameRange", [Cursor, c_uint, c_uint], SourceRange),
38063884
("clang_getCursorResultType", [Cursor], Type),
@@ -3924,6 +4002,9 @@ def write_main_file_to_stdout(self):
39244002
("clang_Cursor_isAnonymousRecordDecl", [Cursor], bool),
39254003
("clang_Cursor_isBitField", [Cursor], bool),
39264004
("clang_Location_isInSystemHeader", [SourceLocation], bool),
4005+
("clang_PrintingPolicy_dispose", [PrintingPolicy]),
4006+
("clang_PrintingPolicy_getProperty", [PrintingPolicy, c_int], c_uint),
4007+
("clang_PrintingPolicy_setProperty", [PrintingPolicy, c_int, c_uint]),
39274008
("clang_Type_getAlignOf", [Type], c_longlong),
39284009
("clang_Type_getClassType", [Type], Type),
39294010
("clang_Type_getNumTemplateArguments", [Type], c_int),
@@ -4104,6 +4185,8 @@ def function_exists(self, name: str) -> bool:
41044185
"FixIt",
41054186
"Index",
41064187
"LinkageKind",
4188+
"PrintingPolicy",
4189+
"PrintingPolicyProperty",
41074190
"RefQualifierKind",
41084191
"SourceLocation",
41094192
"SourceRange",

clang/bindings/python/tests/cindex/test_cursor.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
BinaryOperator,
66
Config,
77
CursorKind,
8+
PrintingPolicy,
9+
PrintingPolicyProperty,
810
StorageClass,
911
TemplateArgumentKind,
1012
TranslationUnit,
@@ -981,3 +983,15 @@ def test_from_result_null(self):
981983
def test_from_cursor_result_null(self):
982984
tu = get_tu("")
983985
self.assertEqual(tu.cursor.semantic_parent, None)
986+
987+
def test_pretty_print(self):
988+
tu = get_tu("struct X { int x; }; void f(bool x) { }", lang="cpp")
989+
f = get_cursor(tu, "f")
990+
991+
self.assertEqual(f.displayname, "f(bool)")
992+
pp = PrintingPolicy.create(f)
993+
self.assertEqual(pp.get_property(PrintingPolicyProperty.Bool), True)
994+
self.assertEqual(f.pretty_printed(pp), "void f(bool x) {\n}\n")
995+
pp.set_property(PrintingPolicyProperty.Bool, False)
996+
self.assertEqual(pp.get_property(PrintingPolicyProperty.Bool), False)
997+
self.assertEqual(f.pretty_printed(pp), "void f(_Bool x) {\n}\n")

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,6 +1298,8 @@ Sanitizers
12981298
Python Binding Changes
12991299
----------------------
13001300
- Fixed an issue that led to crashes when calling ``Type.get_exception_specification_kind``.
1301+
- Added bindings for ``clang_getCursorPrettyPrinted`` and related functions,
1302+
which allow changing the formatting of pretty-printed code.
13011303
- Added binding for ``clang_Cursor_isAnonymousRecordDecl``, which allows checking if
13021304
a declaration is an anonymous union or anonymous struct.
13031305

0 commit comments

Comments
 (0)