Skip to content

Commit 13a9c0b

Browse files
committed
[libclang/python] Add python bindings for PrintingPolicy
This allows changing the way pretty-printed code is formatted.
1 parent 07e053f commit 13a9c0b

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

clang/bindings/python/clang/cindex.py

+83
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
"""
@@ -3685,6 +3695,72 @@ def write_main_file_to_stdout(self):
36853695
conf.lib.clang_CXRewriter_writeMainFileToStdOut(self)
36863696

36873697

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

36903766
# Register callback types
@@ -3787,6 +3863,8 @@ def write_main_file_to_stdout(self):
37873863
("clang_getCursorExtent", [Cursor], SourceRange),
37883864
("clang_getCursorLexicalParent", [Cursor], Cursor),
37893865
("clang_getCursorLocation", [Cursor], SourceLocation),
3866+
("clang_getCursorPrettyPrinted", [Cursor, PrintingPolicy], _CXString),
3867+
("clang_getCursorPrintingPolicy", [Cursor], c_object_p),
37903868
("clang_getCursorReferenced", [Cursor], Cursor),
37913869
("clang_getCursorReferenceNameRange", [Cursor, c_uint, c_uint], SourceRange),
37923870
("clang_getCursorResultType", [Cursor], Type),
@@ -3909,6 +3987,9 @@ def write_main_file_to_stdout(self):
39093987
("clang_Cursor_getRawCommentText", [Cursor], _CXString),
39103988
("clang_Cursor_getOffsetOfField", [Cursor], c_longlong),
39113989
("clang_Location_isInSystemHeader", [SourceLocation], bool),
3990+
("clang_PrintingPolicy_dispose", [PrintingPolicy]),
3991+
("clang_PrintingPolicy_getProperty", [PrintingPolicy, c_int], c_uint),
3992+
("clang_PrintingPolicy_setProperty", [PrintingPolicy, c_int, c_uint]),
39123993
("clang_Type_getAlignOf", [Type], c_longlong),
39133994
("clang_Type_getClassType", [Type], Type),
39143995
("clang_Type_getNumTemplateArguments", [Type], c_int),
@@ -4089,6 +4170,8 @@ def function_exists(self, name: str) -> bool:
40894170
"FixIt",
40904171
"Index",
40914172
"LinkageKind",
4173+
"PrintingPolicy",
4174+
"PrintingPolicyProperty",
40924175
"RefQualifierKind",
40934176
"SourceLocation",
40944177
"SourceRange",

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

+14
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

+2
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,8 @@ Sanitizers
883883
Python Binding Changes
884884
----------------------
885885
- Fixed an issue that led to crashes when calling ``Type.get_exception_specification_kind``.
886+
- Added bindings for ``clang_getCursorPrettyPrinted`` and related functions,
887+
which allow changing the formatting of pretty-printed code.
886888

887889
OpenMP Support
888890
--------------

0 commit comments

Comments
 (0)