Skip to content

Commit 03b0f55

Browse files
epistaxDeinAlptraum
andauthoredApr 15, 2025
[cindex] Add support for calling getFullyQualifiedName to the Python binding. (#135420)
We're coming from llvm 11. There was a change made back in 15f3cd6 that changed how type spelling works. Previous we were given namespaces of the types within the spelling, and this was necessary in our use-case. There is a more appropriate function available but it was not being exposed over the Python bindings. This PR is intended to make make this already-existing functionality accessible. --------- Co-authored-by: Jannick Kremer <[email protected]>
1 parent 81499ed commit 03b0f55

File tree

6 files changed

+71
-0
lines changed

6 files changed

+71
-0
lines changed
 

‎clang/bindings/python/clang/cindex.py

+14
Original file line numberDiff line numberDiff line change
@@ -2593,6 +2593,19 @@ def get_canonical(self):
25932593
"""
25942594
return Type.from_result(conf.lib.clang_getCanonicalType(self), (self,))
25952595

2596+
def get_fully_qualified_name(self, policy, with_global_ns_prefix=False):
2597+
"""
2598+
Get the fully qualified name for a type.
2599+
2600+
This includes full qualification of all template parameters.
2601+
2602+
policy - This PrintingPolicy can further refine the type formatting
2603+
with_global_ns_prefix - If true, prepend '::' to qualified names
2604+
"""
2605+
return _CXString.from_result(
2606+
conf.lib.clang_getFullyQualifiedName(self, policy, with_global_ns_prefix)
2607+
)
2608+
25962609
def is_const_qualified(self):
25972610
"""Determine whether a Type has the "const" qualifier set.
25982611
@@ -4022,6 +4035,7 @@ def set_property(self, property, value):
40224035
("clang_getTypeSpelling", [Type], _CXString),
40234036
("clang_hashCursor", [Cursor], c_uint),
40244037
("clang_isAttribute", [CursorKind], bool),
4038+
("clang_getFullyQualifiedName", [Type, PrintingPolicy, c_uint], _CXString),
40254039
("clang_isConstQualifiedType", [Type], bool),
40264040
("clang_isCursorDefinition", [Cursor], bool),
40274041
("clang_isDeclaration", [CursorKind], bool),

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

+19
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,25 @@ def test_pretty(self):
535535
pp.set_property(PrintingPolicyProperty.SuppressTagKeyword, False)
536536
self.assertEqual(f.type.get_canonical().pretty_printed(pp), "struct X")
537537

538+
def test_fully_qualified_name(self):
539+
source = """
540+
namespace home {
541+
class Bar {
542+
};
543+
class Foo {
544+
public:
545+
void setIt(Bar*);
546+
};
547+
}
548+
class A : public home::Foo {
549+
};
550+
"""
551+
tu = get_tu(source, lang="cpp")
552+
arg = next(get_cursor(tu, "setIt").get_arguments())
553+
pp = PrintingPolicy.create(arg)
554+
self.assertEqual(arg.type.get_fully_qualified_name(pp), "home::Bar *")
555+
self.assertEqual(arg.type.get_fully_qualified_name(pp, True), "::home::Bar *")
556+
538557
def test_base_classes(self):
539558
source = """
540559
class A { int a; };

‎clang/docs/ReleaseNotes.rst

+4
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,8 @@ libclang
607607
--------
608608
- Added ``clang_visitCXXMethods``, which allows visiting the methods
609609
of a class.
610+
- Added ``clang_getFullyQualifiedName``, which provides fully qualified type names as
611+
instructed by a PrintingPolicy.
610612

611613
- Fixed a buffer overflow in ``CXString`` implementation. The fix may result in
612614
increased memory allocation.
@@ -661,6 +663,8 @@ Python Binding Changes
661663
the cursor is a specialization of.
662664
- Added ``Type.get_methods``, a binding for ``clang_visitCXXMethods``, which
663665
allows visiting the methods of a class.
666+
- Added ``Type.get_fully_qualified_name``, which provides fully qualified type names as
667+
instructed by a PrintingPolicy.
664668

665669
OpenMP Support
666670
--------------

‎clang/include/clang-c/Index.h

+12
Original file line numberDiff line numberDiff line change
@@ -4223,6 +4223,18 @@ CINDEX_LINKAGE CXString clang_getCursorPrettyPrinted(CXCursor Cursor,
42234223
CINDEX_LINKAGE CXString clang_getTypePrettyPrinted(CXType CT,
42244224
CXPrintingPolicy cxPolicy);
42254225

4226+
/**
4227+
* Get the fully qualified name for a type.
4228+
*
4229+
* This includes full qualification of all template parameters.
4230+
*
4231+
* Policy - Further refine the type formatting
4232+
* WithGlobalNsPrefix - If non-zero, function will prepend a '::' to qualified
4233+
* names
4234+
*/
4235+
CINDEX_LINKAGE CXString clang_getFullyQualifiedName(
4236+
CXType CT, CXPrintingPolicy Policy, unsigned WithGlobalNsPrefix);
4237+
42264238
/**
42274239
* Retrieve the display name for the entity referenced by this cursor.
42284240
*

‎clang/tools/libclang/CXType.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "clang/AST/DeclObjC.h"
2020
#include "clang/AST/DeclTemplate.h"
2121
#include "clang/AST/Expr.h"
22+
#include "clang/AST/QualTypeNames.h"
2223
#include "clang/AST/RecordLayout.h"
2324
#include "clang/AST/Type.h"
2425
#include "clang/Basic/AddressSpaces.h"
@@ -328,6 +329,22 @@ CXString clang_getTypePrettyPrinted(CXType CT, CXPrintingPolicy cxPolicy) {
328329
return cxstring::createDup(OS.str());
329330
}
330331

332+
CXString clang_getFullyQualifiedName(CXType CT, CXPrintingPolicy cxPolicy,
333+
unsigned int WithGlobalNsPrefix) {
334+
const QualType T = GetQualType(CT);
335+
if (T.isNull())
336+
return cxstring::createEmpty();
337+
const CXTranslationUnit TU = GetTU(CT);
338+
const ASTContext &Ctx = cxtu::getASTUnit(TU)->getASTContext();
339+
const PrintingPolicy *UserPolicy = static_cast<PrintingPolicy *>(cxPolicy);
340+
const bool WithGlobalNs = (WithGlobalNsPrefix != 0);
341+
342+
const std::string Str =
343+
TypeName::getFullyQualifiedName(T, Ctx, *UserPolicy, WithGlobalNs);
344+
345+
return cxstring::createDup(Str);
346+
}
347+
331348
CXType clang_getTypedefDeclUnderlyingType(CXCursor C) {
332349
using namespace cxcursor;
333350
CXTranslationUnit TU = cxcursor::getCursorTU(C);

‎clang/tools/libclang/libclang.map

+5
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,11 @@ LLVM_20 {
438438
clang_visitCXXMethods;
439439
};
440440

441+
LLVM_21 {
442+
global:
443+
clang_getFullyQualifiedName;
444+
};
445+
441446
# Example of how to add a new symbol version entry. If you do add a new symbol
442447
# version, please update the example to depend on the version you added.
443448
# LLVM_X {

0 commit comments

Comments
 (0)
Please sign in to comment.