Question

I have a CXCursor that marks the place where the declaration of a function in C++. I know how to get the method name, or the USR... but how can I get the Class Name (that the method is a part of)

The code that I am parsing with libclang is:

Number3D* ParseObjectFace::RetFaceVertex(){
    // some code... 
}

When I try to print the cursor information I use:

clang_getCString(clang_getCursorUSR(cr));
//output "c:@C@ParseObjectFace@F@RetFaceVertex#"
clang_getCString(clang_getCursorDisplayName(cr));
//output "RetFaceVertex()"

How can I get "ParseObjectFace" (the class name)?

Was it helpful?

Solution

You can use clang_getCursorSemanticParent to retrieve the "semantic parent" of a cursor. As quoted from the documentation:

The semantic parent of a cursor is the cursor that semantically contains the given cursor. [...] In the out-of-line definition of C::f, the semantic parent is the the class C, of which this function is a member.

In your example, something like the following should work:

// Retrieve the semantic parent (the class in this case)
CXCursor parent = clang_getCursorSemanticParent (cr);

clang_getCString (clang_getCursorDisplayName (parent));
// Should yield "ParseObjectFace"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top