Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know written var type with Clang using C API instead of actual?

Tags:

c++

c

clang

I'm trying to use Clang via C API, indexing to be detailed. The problem is that some types are returned not as they are written, but as they are for compiler. For example "Stream &" becomes "int &" and "byte" becomes "int.

Some test lib:

// TODO make it a subclass of a generic Serial/Stream base class
class FirmataClass
{
public:
    FirmataClass(Stream &s);

    void setFirmwareNameAndVersion(const char *name, byte major, byte minor);

I'm using the code to get method information:

void showMethodInfo(const CXIdxDeclInfo *info) {
    int numArgs = clang_Cursor_getNumArguments(info->cursor);
    fprintf(stderr, "  %i args:\n", numArgs);

    for (int i=0; i<numArgs; i++) {
        CXCursor argCursor = clang_Cursor_getArgument(info->cursor, i);
        CXString name = clang_getCursorDisplayName(argCursor);
        CXString spelling = clang_getCursorSpelling(argCursor);

        CXType type = clang_getCursorType(argCursor);
        CXString typeSpelling = clang_getTypeSpelling(type);

        CXCursorKind kind = clang_getCursorKind(argCursor);

        fprintf(stderr, "  kind=[%s (%i)], type=[%s], spelling=[%s]\n",
            cursor_kinds[kind], kind, clang_getCString(typeSpelling),
            clang_getCString(spelling));

        clang_disposeString(name);
        clang_disposeString(spelling);
        clang_disposeString(typeSpelling);
    }

    // return type
    CXType returnType = clang_getCursorResultType(info->cursor);
    CXString returnTypeSpelling = clang_getTypeSpelling(returnType);

    fprintf(stderr, " returns %s\n", clang_getCString(returnTypeSpelling));
    clang_disposeString(returnTypeSpelling);
}

Output:

[105:10 4689] access=[CX_CXXPublic] kind=[CXIdxEntity_CXXInstanceMethod] (21) name=[setFirmwareNameAndVersion] is_container=[0] 3 args:
kind=[CXCursor_ParmDecl (10)], type=[const char *], spelling=[name]
kind=[CXCursor_ParmDecl (10)], type=[int], spelling=[major]
kind=[CXCursor_ParmDecl (10)], type=[int], spelling=[minor] returns void

So you can see that byte function arguments are described as int. How can i get actual spelling?

like image 301
4ntoine Avatar asked Nov 11 '13 12:11

4ntoine


1 Answers

Is byte declared via a typedef, or a #define?

When I declare these types:

typedef int MyType_t;
#define MyType2_t int

class Foo
{
public:
    bool bar( MyType_t a, MyType2_t b );
};

And then print the type names I get from clang_GetTypeSpelling this is what I get:

bool Foo_bar(  MyType_t a, int b )

Libclang presumably can't print the #defined name because the preprocessor has already replaced it with int by the time the parse tree is built.

like image 83
Joe Ludwig Avatar answered Oct 02 '22 22:10

Joe Ludwig