Skip to content

Commit ec604ef

Browse files
committed
[libclang/python] Add python bindings for PrintingPolicy
1 parent 07e053f commit ec604ef

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

clang/bindings/python/clang/cindex.py

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

36903765
# Register callback types
@@ -3787,6 +3862,8 @@ def write_main_file_to_stdout(self):
37873862
("clang_getCursorExtent", [Cursor], SourceRange),
37883863
("clang_getCursorLexicalParent", [Cursor], Cursor),
37893864
("clang_getCursorLocation", [Cursor], SourceLocation),
3865+
("clang_getCursorPrettyPrinted", [Cursor, PrintingPolicy], _CXString),
3866+
("clang_getCursorPrintingPolicy", [Cursor], c_object_p),
37903867
("clang_getCursorReferenced", [Cursor], Cursor),
37913868
("clang_getCursorReferenceNameRange", [Cursor, c_uint, c_uint], SourceRange),
37923869
("clang_getCursorResultType", [Cursor], Type),
@@ -3909,6 +3986,9 @@ def write_main_file_to_stdout(self):
39093986
("clang_Cursor_getRawCommentText", [Cursor], _CXString),
39103987
("clang_Cursor_getOffsetOfField", [Cursor], c_longlong),
39113988
("clang_Location_isInSystemHeader", [SourceLocation], bool),
3989+
("clang_PrintingPolicy_dispose", [PrintingPolicy]),
3990+
("clang_PrintingPolicy_getProperty", [PrintingPolicy, c_int], c_uint),
3991+
("clang_PrintingPolicy_setProperty", [PrintingPolicy, c_int, c_uint]),
39123992
("clang_Type_getAlignOf", [Type], c_longlong),
39133993
("clang_Type_getClassType", [Type], Type),
39143994
("clang_Type_getNumTemplateArguments", [Type], c_int),
@@ -4089,6 +4169,8 @@ def function_exists(self, name: str) -> bool:
40894169
"FixIt",
40904170
"Index",
40914171
"LinkageKind",
4172+
"PrintingPolicy",
4173+
"PrintingPolicyProperty",
40924174
"RefQualifierKind",
40934175
"SourceLocation",
40944176
"SourceRange",

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

Lines changed: 12 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,13 @@ 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(f.pretty_printed(pp), "void f(bool x) {\n}\n")
994+
pp.set_property(PrintingPolicyProperty.Bool, False)
995+
self.assertEqual(f.pretty_printed(pp), "void f(_Bool x) {\n}\n")

0 commit comments

Comments
 (0)