Skip to content

Commit b302633

Browse files
[libclang] Allow using PrintingPolicy with types (#122386)
This allows controlling pretty-printing of types the same way it works with cursors.
1 parent 129ec84 commit b302633

File tree

6 files changed

+55
-5
lines changed

6 files changed

+55
-5
lines changed

clang/bindings/python/clang/cindex.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2701,6 +2701,10 @@ def spelling(self):
27012701
"""Retrieve the spelling of this Type."""
27022702
return _CXString.from_result(conf.lib.clang_getTypeSpelling(self))
27032703

2704+
def pretty_printed(self, policy):
2705+
"""Pretty-prints this Type with the given PrintingPolicy"""
2706+
return _CXString.from_result(conf.lib.clang_getTypePrettyPrinted(self, policy))
2707+
27042708
def __eq__(self, other):
27052709
if type(other) != type(self):
27062710
return False
@@ -3955,6 +3959,7 @@ def set_property(self, property, value):
39553959
("clang_getTypedefDeclUnderlyingType", [Cursor], Type),
39563960
("clang_getTypedefName", [Type], _CXString),
39573961
("clang_getTypeKindSpelling", [c_uint], _CXString),
3962+
("clang_getTypePrettyPrinted", [Type, PrintingPolicy], _CXString),
39583963
("clang_getTypeSpelling", [Type], _CXString),
39593964
("clang_hashCursor", [Cursor], c_uint),
39603965
("clang_isAttribute", [CursorKind], bool),

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import os
22

3-
from clang.cindex import Config, CursorKind, RefQualifierKind, TranslationUnit, TypeKind
3+
from clang.cindex import (
4+
Config,
5+
CursorKind,
6+
PrintingPolicy,
7+
PrintingPolicyProperty,
8+
RefQualifierKind,
9+
TranslationUnit,
10+
TypeKind,
11+
)
412

513
if "CLANG_LIBRARY_PATH" in os.environ:
614
Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
@@ -517,3 +525,12 @@ class Template {
517525
# Variable without a template argument.
518526
cursor = get_cursor(tu, "bar")
519527
self.assertEqual(cursor.get_num_template_arguments(), -1)
528+
529+
def test_pretty(self):
530+
tu = get_tu("struct X {}; X x;", lang="cpp")
531+
f = get_cursor(tu, "x")
532+
533+
pp = PrintingPolicy.create(f)
534+
self.assertEqual(f.type.get_canonical().pretty_printed(pp), "X")
535+
pp.set_property(PrintingPolicyProperty.SuppressTagKeyword, False)
536+
self.assertEqual(f.type.get_canonical().pretty_printed(pp), "struct X")

clang/docs/ReleaseNotes.rst

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,8 @@ libclang
11821182
--------
11831183
- Add ``clang_isBeforeInTranslationUnit``. Given two source locations, it determines
11841184
whether the first one comes strictly before the second in the source code.
1185+
- Add ``clang_getTypePrettyPrinted``. It allows controlling the PrintingPolicy used
1186+
to pretty-print a type.
11851187

11861188
Static Analyzer
11871189
---------------
@@ -1322,10 +1324,13 @@ Sanitizers
13221324
Python Binding Changes
13231325
----------------------
13241326
- Fixed an issue that led to crashes when calling ``Type.get_exception_specification_kind``.
1325-
- Added bindings for ``clang_getCursorPrettyPrinted`` and related functions,
1326-
which allow changing the formatting of pretty-printed code.
1327-
- Added binding for ``clang_Cursor_isAnonymousRecordDecl``, which allows checking if
1328-
a declaration is an anonymous union or anonymous struct.
1327+
- Added ``Cursor.pretty_printed``, a binding for ``clang_getCursorPrettyPrinted``,
1328+
and related functions, which allow changing the formatting of pretty-printed code.
1329+
- Added ``Cursor.is_anonymous_record_decl``, a binding for
1330+
``clang_Cursor_isAnonymousRecordDecl``, which allows checking if a
1331+
declaration is an anonymous union or anonymous struct.
1332+
- Added ``Type.pretty_printed`, a binding for ``clang_getTypePrettyPrinted``,
1333+
which allows changing the formatting of pretty-printed types.
13291334

13301335
OpenMP Support
13311336
--------------

clang/include/clang-c/Index.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4182,6 +4182,14 @@ CINDEX_LINKAGE void clang_PrintingPolicy_dispose(CXPrintingPolicy Policy);
41824182
CINDEX_LINKAGE CXString clang_getCursorPrettyPrinted(CXCursor Cursor,
41834183
CXPrintingPolicy Policy);
41844184

4185+
/**
4186+
* Pretty-print the underlying type using a custom printing policy.
4187+
*
4188+
* If the type is invalid, an empty string is returned.
4189+
*/
4190+
CINDEX_LINKAGE CXString clang_getTypePrettyPrinted(CXType CT,
4191+
CXPrintingPolicy cxPolicy);
4192+
41854193
/**
41864194
* Retrieve the display name for the entity referenced by this cursor.
41874195
*

clang/tools/libclang/CXType.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,20 @@ CXString clang_getTypeSpelling(CXType CT) {
313313
return cxstring::createDup(OS.str());
314314
}
315315

316+
CXString clang_getTypePrettyPrinted(CXType CT, CXPrintingPolicy cxPolicy) {
317+
QualType T = GetQualType(CT);
318+
if (T.isNull())
319+
return cxstring::createEmpty();
320+
321+
SmallString<64> Str;
322+
llvm::raw_svector_ostream OS(Str);
323+
PrintingPolicy *UserPolicy = static_cast<PrintingPolicy *>(cxPolicy);
324+
325+
T.print(OS, *UserPolicy);
326+
327+
return cxstring::createDup(OS.str());
328+
}
329+
316330
CXType clang_getTypedefDeclUnderlyingType(CXCursor C) {
317331
using namespace cxcursor;
318332
CXTranslationUnit TU = cxcursor::getCursorTU(C);

clang/tools/libclang/libclang.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ LLVM_19 {
436436

437437
LLVM_20 {
438438
global:
439+
clang_getTypePrettyPrinted;
439440
clang_isBeforeInTranslationUnit;
440441
};
441442

0 commit comments

Comments
 (0)