Skip to content

Commit bb0542e

Browse files
committed
[cindex] Add API to query the class methods of a type
1 parent 293dbea commit bb0542e

File tree

6 files changed

+89
-0
lines changed

6 files changed

+89
-0
lines changed

clang/bindings/python/clang/cindex.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2710,6 +2710,21 @@ def visitor(base, children):
27102710
conf.lib.clang_visitCXXBaseClasses(self, fields_visit_callback(visitor), bases)
27112711
return iter(bases)
27122712

2713+
def get_methods(self):
2714+
"""Return an iterator for accessing the methods of this type."""
2715+
2716+
def visitor(method, children):
2717+
assert method != conf.lib.clang_getNullCursor()
2718+
2719+
# Create reference to TU so it isn't GC'd before Cursor.
2720+
method._tu = self._tu
2721+
methods.append(method)
2722+
return 1 # continue
2723+
2724+
methods: list[Cursor] = []
2725+
conf.lib.clang_visitCXXMethods(self, fields_visit_callback(visitor), methods)
2726+
return iter(methods)
2727+
27132728
def get_exception_specification_kind(self):
27142729
"""
27152730
Return the kind of the exception specification; a value from
@@ -4017,6 +4032,7 @@ def set_property(self, property, value):
40174032
),
40184033
("clang_visitChildren", [Cursor, cursor_visit_callback, py_object], c_uint),
40194034
("clang_visitCXXBaseClasses", [Type, fields_visit_callback, py_object], c_uint),
4035+
("clang_visitCXXMethods", [Type, fields_visit_callback, py_object], c_uint),
40204036
("clang_Cursor_getNumArguments", [Cursor], c_int),
40214037
("clang_Cursor_getArgument", [Cursor, c_uint], Cursor),
40224038
("clang_Cursor_getNumTemplateArguments", [Cursor], c_int),

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,3 +559,21 @@ class Template : public A, public B, virtual C {
559559
self.assertEqual(bases[1].get_base_offsetof(cursor_type_decl), 96)
560560
self.assertTrue(bases[2].is_virtual_base())
561561
self.assertEqual(bases[2].get_base_offsetof(cursor_type_decl), 128)
562+
563+
def test_class_methods(self):
564+
source = """
565+
template <typename T>
566+
class Template { void Foo(); };
567+
typedef Template<int> instance;
568+
instance bar;
569+
"""
570+
tu = get_tu(source, lang="cpp", flags=["--target=x86_64-linux-gnu"])
571+
cursor = get_cursor(tu, "instance")
572+
cursor_type = cursor.underlying_typedef_type
573+
self.assertEqual(cursor.kind, CursorKind.TYPEDEF_DECL)
574+
methods = list(cursor_type.get_methods())
575+
self.assertEqual(len(methods), 4)
576+
self.assertEqual(methods[0].kind, CursorKind.CXX_METHOD)
577+
self.assertEqual(methods[1].kind, CursorKind.CONSTRUCTOR)
578+
self.assertEqual(methods[2].kind, CursorKind.CONSTRUCTOR)
579+
self.assertEqual(methods[3].kind, CursorKind.CONSTRUCTOR)

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,6 +1241,8 @@ libclang
12411241
of a class.
12421242
- Added ``clang_getOffsetOfBase``, which allows computing the offset of a base
12431243
class in a class's layout.
1244+
- Added ``clang_visitCXXMethods``, which allows visiting the methods
1245+
of a class.
12441246

12451247
Static Analyzer
12461248
---------------
@@ -1394,6 +1396,8 @@ Python Binding Changes
13941396
allows visiting the base classes of a class.
13951397
- Added ``Cursor.get_base_offsetof``, a binding for ``clang_getOffsetOfBase``,
13961398
which allows computing the offset of a base class in a class's layout.
1399+
- Added ``Type.get_methods``, a binding for ``clang_visitCXXMethods``, which
1400+
allows visiting the methods of a class.
13971401

13981402
OpenMP Support
13991403
--------------

clang/include/clang-c/Index.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6680,6 +6680,29 @@ CINDEX_LINKAGE unsigned clang_visitCXXBaseClasses(CXType T,
66806680
CXFieldVisitor visitor,
66816681
CXClientData client_data);
66826682

6683+
/**
6684+
* Visit the class methods of a type.
6685+
*
6686+
* This function visits all the methods of the given cursor,
6687+
* invoking the given \p visitor function with the cursors of each
6688+
* visited method. The traversal may be ended prematurely, if
6689+
* the visitor returns \c CXFieldVisit_Break.
6690+
*
6691+
* \param T the record type whose field may be visited.
6692+
*
6693+
* \param visitor the visitor function that will be invoked for each
6694+
* field of \p T.
6695+
*
6696+
* \param client_data pointer data supplied by the client, which will
6697+
* be passed to the visitor each time it is invoked.
6698+
*
6699+
* \returns a non-zero value if the traversal was terminated
6700+
* prematurely by the visitor returning \c CXFieldVisit_Break.
6701+
*/
6702+
CINDEX_LINKAGE unsigned clang_visitCXXMethods(CXType T,
6703+
CXFieldVisitor visitor,
6704+
CXClientData client_data);
6705+
66836706
/**
66846707
* Describes the kind of binary operators.
66856708
*/

clang/tools/libclang/CIndexCXX.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,33 @@ unsigned clang_visitCXXBaseClasses(CXType PT, CXFieldVisitor visitor,
5454
return true;
5555
}
5656

57+
unsigned clang_visitCXXMethods(CXType PT, CXFieldVisitor visitor,
58+
CXClientData client_data) {
59+
CXCursor PC = clang_getTypeDeclaration(PT);
60+
if (clang_isInvalid(PC.kind))
61+
return false;
62+
const CXXRecordDecl *RD =
63+
dyn_cast_if_present<CXXRecordDecl>(cxcursor::getCursorDecl(PC));
64+
if (!RD || RD->isInvalidDecl())
65+
return false;
66+
RD = RD->getDefinition();
67+
if (!RD || RD->isInvalidDecl())
68+
return false;
69+
70+
for (auto Method : RD->methods()) {
71+
// Callback to the client.
72+
switch (
73+
visitor(cxcursor::MakeCXCursor(Method, getCursorTU(PC)),
74+
client_data)) {
75+
case CXVisit_Break:
76+
return true;
77+
case CXVisit_Continue:
78+
break;
79+
}
80+
}
81+
return true;
82+
}
83+
5784
enum CX_CXXAccessSpecifier clang_getCXXAccessSpecifier(CXCursor C) {
5885
AccessSpecifier spec = AS_none;
5986

clang/tools/libclang/libclang.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ LLVM_20 {
440440
clang_getTypePrettyPrinted;
441441
clang_isBeforeInTranslationUnit;
442442
clang_visitCXXBaseClasses;
443+
clang_visitCXXMethods;
443444
};
444445

445446
# Example of how to add a new symbol version entry. If you do add a new symbol

0 commit comments

Comments
 (0)