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 9426fdd commit a96fbe3Copy full SHA for a96fbe3
clang/bindings/python/clang/cindex.py
@@ -2701,6 +2701,11 @@ 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
2709
def __eq__(self, other):
2710
if type(other) != type(self):
2711
return False
@@ -3955,6 +3960,7 @@ def set_property(self, property, value):
3955
3960
("clang_getTypedefDeclUnderlyingType", [Cursor], Type),
3956
3961
("clang_getTypedefName", [Type], _CXString),
3957
3962
("clang_getTypeKindSpelling", [c_uint], _CXString),
3963
+ ("clang_getTypePrettyPrinted", [Type, PrintingPolicy], _CXString),
3958
3964
("clang_getTypeSpelling", [Type], _CXString),
3959
3965
("clang_hashCursor", [Cursor], c_uint),
3966
("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
@@ -1158,6 +1158,8 @@ libclang
1158
--------
1159
- Add ``clang_isBeforeInTranslationUnit``. Given two source locations, it determines
1160
whether the first one comes strictly before the second in the source code.
1161
+- Add ``clang_getTypePrettyPrinted``. It allows controlling the PrintingPolicy used
1162
+ to pretty-print a type.
1163
1164
Static Analyzer
1165
---------------
clang/include/clang-c/Index.h
@@ -4182,6 +4182,13 @@ 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, CXPrintingPolicy cxPolicy);
4191
4192
/**
4193
* Retrieve the display name for the entity referenced by this cursor.
4194
*
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
@@ -437,6 +437,7 @@ LLVM_19 {
437
LLVM_20 {
438
global:
439
clang_isBeforeInTranslationUnit;
440
+ clang_getTypePrettyPrinted;
441
};
442
443
# Example of how to add a new symbol version entry. If you do add a new symbol
0 commit comments