-
Notifications
You must be signed in to change notification settings - Fork 12.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[libclang] Allow using PrintingPolicy with types #122386
Open
efriedma-quic
wants to merge
1
commit into
llvm:main
Choose a base branch
from
efriedma-quic:printing-policy-type-cindex
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
+48
−1
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
llvmbot
added
clang
Clang issues not falling into any other category
clang:as-a-library
libclang and C++ API
labels
Jan 9, 2025
@llvm/pr-subscribers-clang Author: Eli Friedman (efriedma-quic) ChangesThis allows controlling pretty-printing of types the same way it works with cursors. Full diff: https://github.com/llvm/llvm-project/pull/122386.diff 6 Files Affected:
diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py
index 7caf0bbfd722af..e01285e27fcdf9 100644
--- a/clang/bindings/python/clang/cindex.py
+++ b/clang/bindings/python/clang/cindex.py
@@ -2701,6 +2701,11 @@ def spelling(self):
"""Retrieve the spelling of this Type."""
return _CXString.from_result(conf.lib.clang_getTypeSpelling(self))
+ def pretty_printed(self, policy):
+ """Pretty-prints this Type with the given PrintingPolicy"""
+ return _CXString.from_result(conf.lib.clang_getTypePrettyPrinted(self, policy))
+
+
def __eq__(self, other):
if type(other) != type(self):
return False
@@ -3955,6 +3960,7 @@ def set_property(self, property, value):
("clang_getTypedefDeclUnderlyingType", [Cursor], Type),
("clang_getTypedefName", [Type], _CXString),
("clang_getTypeKindSpelling", [c_uint], _CXString),
+ ("clang_getTypePrettyPrinted", [Type, PrintingPolicy], _CXString),
("clang_getTypeSpelling", [Type], _CXString),
("clang_hashCursor", [Cursor], c_uint),
("clang_isAttribute", [CursorKind], bool),
diff --git a/clang/bindings/python/tests/cindex/test_type.py b/clang/bindings/python/tests/cindex/test_type.py
index e1d8c2aad1c3a4..da27ba5bda3ee0 100644
--- a/clang/bindings/python/tests/cindex/test_type.py
+++ b/clang/bindings/python/tests/cindex/test_type.py
@@ -1,6 +1,14 @@
import os
-from clang.cindex import Config, CursorKind, RefQualifierKind, TranslationUnit, TypeKind
+from clang.cindex import (
+ Config,
+ CursorKind,
+ PrintingPolicy,
+ PrintingPolicyProperty,
+ RefQualifierKind,
+ TranslationUnit,
+ TypeKind
+)
if "CLANG_LIBRARY_PATH" in os.environ:
Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
@@ -517,3 +525,12 @@ class Template {
# Variable without a template argument.
cursor = get_cursor(tu, "bar")
self.assertEqual(cursor.get_num_template_arguments(), -1)
+
+ def test_pretty(self):
+ tu = get_tu("struct X {}; X x;", lang="cpp")
+ f = get_cursor(tu, "x")
+
+ pp = PrintingPolicy.create(f)
+ self.assertEqual(f.type.get_canonical().pretty_printed(pp), "X")
+ pp.set_property(PrintingPolicyProperty.SuppressTagKeyword, False)
+ self.assertEqual(f.type.get_canonical().pretty_printed(pp), "struct X")
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 07a1a4195427d8..d0479a94bd67ce 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -1158,6 +1158,8 @@ libclang
--------
- Add ``clang_isBeforeInTranslationUnit``. Given two source locations, it determines
whether the first one comes strictly before the second in the source code.
+- Add ``clang_getTypePrettyPrinted``. It allows controlling the PrintingPolicy used
+ to pretty-print a type.
Static Analyzer
---------------
diff --git a/clang/include/clang-c/Index.h b/clang/include/clang-c/Index.h
index 63d266dc60ec73..e1f3759bb53e85 100644
--- a/clang/include/clang-c/Index.h
+++ b/clang/include/clang-c/Index.h
@@ -4182,6 +4182,13 @@ CINDEX_LINKAGE void clang_PrintingPolicy_dispose(CXPrintingPolicy Policy);
CINDEX_LINKAGE CXString clang_getCursorPrettyPrinted(CXCursor Cursor,
CXPrintingPolicy Policy);
+/**
+ * Pretty-print the underlying type using a custom printing policy.
+ *
+ * If the type is invalid, an empty string is returned.
+ */
+CINDEX_LINKAGE CXString clang_getTypePrettyPrinted(CXType CT, CXPrintingPolicy cxPolicy);
+
/**
* Retrieve the display name for the entity referenced by this cursor.
*
diff --git a/clang/tools/libclang/CXType.cpp b/clang/tools/libclang/CXType.cpp
index b4df12405cf356..f97023c429bfae 100644
--- a/clang/tools/libclang/CXType.cpp
+++ b/clang/tools/libclang/CXType.cpp
@@ -313,6 +313,20 @@ CXString clang_getTypeSpelling(CXType CT) {
return cxstring::createDup(OS.str());
}
+CXString clang_getTypePrettyPrinted(CXType CT, CXPrintingPolicy cxPolicy) {
+ QualType T = GetQualType(CT);
+ if (T.isNull())
+ return cxstring::createEmpty();
+
+ SmallString<64> Str;
+ llvm::raw_svector_ostream OS(Str);
+ PrintingPolicy *UserPolicy = static_cast<PrintingPolicy *>(cxPolicy);
+
+ T.print(OS, *UserPolicy);
+
+ return cxstring::createDup(OS.str());
+}
+
CXType clang_getTypedefDeclUnderlyingType(CXCursor C) {
using namespace cxcursor;
CXTranslationUnit TU = cxcursor::getCursorTU(C);
diff --git a/clang/tools/libclang/libclang.map b/clang/tools/libclang/libclang.map
index 25d8ba57d32514..c5f0be746ffe12 100644
--- a/clang/tools/libclang/libclang.map
+++ b/clang/tools/libclang/libclang.map
@@ -437,6 +437,7 @@ LLVM_19 {
LLVM_20 {
global:
clang_isBeforeInTranslationUnit;
+ clang_getTypePrettyPrinted;
};
# Example of how to add a new symbol version entry. If you do add a new symbol
|
✅ With the latest revision this PR passed the Python code formatter. |
✅ With the latest revision this PR passed the C/C++ code formatter. |
This allows controlling pretty-printing of types the same way it works with cursors.
efriedma-quic
force-pushed
the
printing-policy-type-cindex
branch
from
January 9, 2025 23:06
a96fbe3
to
0557238
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This allows controlling pretty-printing of types the same way it works with cursors.