Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions clang/bindings/python/clang/cindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -2593,6 +2593,19 @@ def get_canonical(self):
"""
return Type.from_result(conf.lib.clang_getCanonicalType(self), (self,))

def get_fully_qualified_name(self, policy, with_global_ns_prefix=False):
"""
Get the fully qualified name for a type.

This includes full qualification of all template parameters.

policy - This PrintingPolicy can further refine the type formatting
with_global_ns_prefix - If true, prepend '::' to qualified names
"""
return _CXString.from_result(
conf.lib.clang_getFullyQualifiedName(self, policy, with_global_ns_prefix)
)

def is_const_qualified(self):
"""Determine whether a Type has the "const" qualifier set.

Expand Down Expand Up @@ -4022,6 +4035,7 @@ def set_property(self, property, value):
("clang_getTypeSpelling", [Type], _CXString),
("clang_hashCursor", [Cursor], c_uint),
("clang_isAttribute", [CursorKind], bool),
("clang_getFullyQualifiedName", [Type, PrintingPolicy, c_uint], _CXString),
("clang_isConstQualifiedType", [Type], bool),
("clang_isCursorDefinition", [Cursor], bool),
("clang_isDeclaration", [CursorKind], bool),
Expand Down
19 changes: 19 additions & 0 deletions clang/bindings/python/tests/cindex/test_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,25 @@ def test_pretty(self):
pp.set_property(PrintingPolicyProperty.SuppressTagKeyword, False)
self.assertEqual(f.type.get_canonical().pretty_printed(pp), "struct X")

def test_fully_qualified_name(self):
source = """
namespace home {
class Bar {
};
class Foo {
public:
void setIt(Bar*);
};
}
class A : public home::Foo {
};
"""
tu = get_tu(source, lang="cpp")
arg = next(get_cursor(tu, "setIt").get_arguments())
pp = PrintingPolicy.create(arg)
self.assertEqual(arg.type.get_fully_qualified_name(pp), "home::Bar *")
self.assertEqual(arg.type.get_fully_qualified_name(pp, True), "::home::Bar *")

def test_base_classes(self):
source = """
class A { int a; };
Expand Down
4 changes: 4 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,8 @@ libclang
--------
- Added ``clang_visitCXXMethods``, which allows visiting the methods
of a class.
- Added ``clang_getFullyQualifiedName``, which provides fully qualified type names as
instructed by a PrintingPolicy.

- Fixed a buffer overflow in ``CXString`` implementation. The fix may result in
increased memory allocation.
Expand Down Expand Up @@ -645,6 +647,8 @@ Python Binding Changes
the cursor is a specialization of.
- Added ``Type.get_methods``, a binding for ``clang_visitCXXMethods``, which
allows visiting the methods of a class.
- Added ``Type.get_fully_qualified_name``, which provides fully qualified type names as
instructed by a PrintingPolicy.

OpenMP Support
--------------
Expand Down
12 changes: 12 additions & 0 deletions clang/include/clang-c/Index.h
Original file line number Diff line number Diff line change
Expand Up @@ -4223,6 +4223,18 @@ CINDEX_LINKAGE CXString clang_getCursorPrettyPrinted(CXCursor Cursor,
CINDEX_LINKAGE CXString clang_getTypePrettyPrinted(CXType CT,
CXPrintingPolicy cxPolicy);

/**
* Get the fully qualified name for a type.
*
* This includes full qualification of all template parameters.
*
* Policy - Further refine the type formatting
* WithGlobalNsPrefix - If non-zero, function will prepend a '::' to qualified
* names
*/
CINDEX_LINKAGE CXString clang_getFullyQualifiedName(
CXType CT, CXPrintingPolicy Policy, unsigned WithGlobalNsPrefix);

/**
* Retrieve the display name for the entity referenced by this cursor.
*
Expand Down
17 changes: 17 additions & 0 deletions clang/tools/libclang/CXType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "clang/AST/DeclObjC.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/Expr.h"
#include "clang/AST/QualTypeNames.h"
#include "clang/AST/RecordLayout.h"
#include "clang/AST/Type.h"
#include "clang/Basic/AddressSpaces.h"
Expand Down Expand Up @@ -328,6 +329,22 @@ CXString clang_getTypePrettyPrinted(CXType CT, CXPrintingPolicy cxPolicy) {
return cxstring::createDup(OS.str());
}

CXString clang_getFullyQualifiedName(CXType CT, CXPrintingPolicy cxPolicy,
unsigned int WithGlobalNsPrefix) {
const QualType T = GetQualType(CT);
if (T.isNull())
return cxstring::createEmpty();
const CXTranslationUnit TU = GetTU(CT);
const ASTContext &Ctx = cxtu::getASTUnit(TU)->getASTContext();
const PrintingPolicy *UserPolicy = static_cast<PrintingPolicy *>(cxPolicy);
const bool WithGlobalNs = (WithGlobalNsPrefix != 0);

const std::string Str =
TypeName::getFullyQualifiedName(T, Ctx, *UserPolicy, WithGlobalNs);

return cxstring::createDup(Str);
}

CXType clang_getTypedefDeclUnderlyingType(CXCursor C) {
using namespace cxcursor;
CXTranslationUnit TU = cxcursor::getCursorTU(C);
Expand Down
5 changes: 5 additions & 0 deletions clang/tools/libclang/libclang.map
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,11 @@ LLVM_20 {
clang_visitCXXMethods;
};

LLVM_21 {
global:
clang_getFullyQualifiedName;
};

# Example of how to add a new symbol version entry. If you do add a new symbol
# version, please update the example to depend on the version you added.
# LLVM_X {
Expand Down