Skip to content

Commit 8f2e784

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

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

clang/bindings/python/clang/cindex.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,6 +1770,14 @@ 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(conf.lib.clang_getCursorPrettyPrinted(self, policy))
1780+
17731781
@property
17741782
def displayname(self):
17751783
"""
@@ -3685,6 +3693,69 @@ def write_main_file_to_stdout(self):
36853693
conf.lib.clang_CXRewriter_writeMainFileToStdOut(self)
36863694

36873695

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

36903761
# Register callback types
@@ -3787,6 +3858,8 @@ def write_main_file_to_stdout(self):
37873858
("clang_getCursorExtent", [Cursor], SourceRange),
37883859
("clang_getCursorLexicalParent", [Cursor], Cursor),
37893860
("clang_getCursorLocation", [Cursor], SourceLocation),
3861+
("clang_getCursorPrettyPrinted", [Cursor, PrintingPolicy], _CXString),
3862+
("clang_getCursorPrintingPolicy", [Cursor], c_object_p),
37903863
("clang_getCursorReferenced", [Cursor], Cursor),
37913864
("clang_getCursorReferenceNameRange", [Cursor, c_uint, c_uint], SourceRange),
37923865
("clang_getCursorResultType", [Cursor], Type),
@@ -3909,6 +3982,9 @@ def write_main_file_to_stdout(self):
39093982
("clang_Cursor_getRawCommentText", [Cursor], _CXString),
39103983
("clang_Cursor_getOffsetOfField", [Cursor], c_longlong),
39113984
("clang_Location_isInSystemHeader", [SourceLocation], bool),
3985+
("clang_PrintingPolicy_dispose", [PrintingPolicy]),
3986+
("clang_PrintingPolicy_getProperty", [PrintingPolicy, c_int], c_uint),
3987+
("clang_PrintingPolicy_setProperty", [PrintingPolicy, c_int, c_uint]),
39123988
("clang_Type_getAlignOf", [Type], c_longlong),
39133989
("clang_Type_getClassType", [Type], Type),
39143990
("clang_Type_getNumTemplateArguments", [Type], c_int),
@@ -4089,6 +4165,8 @@ def function_exists(self, name: str) -> bool:
40894165
"FixIt",
40904166
"Index",
40914167
"LinkageKind",
4168+
"PrintingPolicy",
4169+
"PrintingPolicyProperty",
40924170
"RefQualifierKind",
40934171
"SourceLocation",
40944172
"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)