We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 129ec84 commit b302633Copy full SHA for b302633
clang/bindings/python/clang/cindex.py
@@ -2701,6 +2701,10 @@ def spelling(self):
2701
"""Retrieve the spelling of this Type."""
2702
return _CXString.from_result(conf.lib.clang_getTypeSpelling(self))
2703
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
+
2708
def __eq__(self, other):
2709
if type(other) != type(self):
2710
return False
@@ -3955,6 +3959,7 @@ def set_property(self, property, value):
3955
3959
("clang_getTypedefDeclUnderlyingType", [Cursor], Type),
3956
3960
("clang_getTypedefName", [Type], _CXString),
3957
3961
("clang_getTypeKindSpelling", [c_uint], _CXString),
3962
+ ("clang_getTypePrettyPrinted", [Type, PrintingPolicy], _CXString),
3958
3963
("clang_getTypeSpelling", [Type], _CXString),
3964
("clang_hashCursor", [Cursor], c_uint),
3965
("clang_isAttribute", [CursorKind], bool),
clang/bindings/python/tests/cindex/test_type.py
@@ -1,6 +1,14 @@
1
import os
2
3
-from clang.cindex import Config, CursorKind, RefQualifierKind, TranslationUnit, TypeKind
+from clang.cindex import (
4
+ Config,
5
+ CursorKind,
6
+ PrintingPolicy,
7
+ PrintingPolicyProperty,
8
+ RefQualifierKind,
9
+ TranslationUnit,
10
+ TypeKind,
11
+)
12
13
if "CLANG_LIBRARY_PATH" in os.environ:
14
Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
@@ -517,3 +525,12 @@ class Template {
517
525
# Variable without a template argument.
518
526
cursor = get_cursor(tu, "bar")
519
527
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
@@ -1182,6 +1182,8 @@ libclang
1182
--------
1183
- Add ``clang_isBeforeInTranslationUnit``. Given two source locations, it determines
1184
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.
1187
1188
Static Analyzer
1189
---------------
@@ -1322,10 +1324,13 @@ Sanitizers
1322
1324
Python Binding Changes
1323
1325
----------------------
1326
- Fixed an issue that led to crashes when calling ``Type.get_exception_specification_kind``.
-- Added bindings for ``clang_getCursorPrettyPrinted`` and related functions,
- 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.
+- Added ``Cursor.pretty_printed``, a binding for ``clang_getCursorPrettyPrinted``,
+ 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.
1334
1335
OpenMP Support
1336
--------------
clang/include/clang-c/Index.h
@@ -4182,6 +4182,14 @@ CINDEX_LINKAGE void clang_PrintingPolicy_dispose(CXPrintingPolicy Policy);
4182
CINDEX_LINKAGE CXString clang_getCursorPrettyPrinted(CXCursor Cursor,
4183
CXPrintingPolicy Policy);
4184
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
4193
/**
4194
* Retrieve the display name for the entity referenced by this cursor.
4195
*
clang/tools/libclang/CXType.cpp
@@ -313,6 +313,20 @@ CXString clang_getTypeSpelling(CXType CT) {
313
return cxstring::createDup(OS.str());
314
}
315
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
330
CXType clang_getTypedefDeclUnderlyingType(CXCursor C) {
331
using namespace cxcursor;
332
CXTranslationUnit TU = cxcursor::getCursorTU(C);
clang/tools/libclang/libclang.map
@@ -436,6 +436,7 @@ LLVM_19 {
436
437
LLVM_20 {
438
global:
439
+ clang_getTypePrettyPrinted;
440
clang_isBeforeInTranslationUnit;
441
};
442
0 commit comments