我正在尝试通过C API使用Clang,索引要详细.问题是某些类型不是在编写时返回,而是在编译时返回.例如,"Stream&"变为"int&","byte"变为"int".
一些测试库:
// 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);
Run Code Online (Sandbox Code Playgroud)
我正在使用代码获取方法信息:
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);
}
Run Code Online (Sandbox Code Playgroud)
输出:
[105:10 4689] access = [CX_CXXPublic] kind = [CXIdxEntity_CXXInstanceMethod](21)name = [setFirmwareNameAndVersion] is_container = [0] 3 args:
kind = [CXCursor_ParmDecl(10)],type = [const char*],拼写= [name]
kind = [CXCursor_ParmDecl(10)],type = [int],spelling = [major]
kind = [CXCursor_ParmDecl(10)],type = [int],spelling = [minor]返回void
所以你可以看到byte函数参数被描述为int.我怎样才能获得实际的拼写?
是byte通过 typedef 声明的,还是通过 #define 声明的?
当我声明这些类型时:
typedef int MyType_t;
#define MyType2_t int
class Foo
{
public:
bool bar( MyType_t a, MyType2_t b );
};
Run Code Online (Sandbox Code Playgroud)
然后打印我从中得到的类型名称,clang_GetTypeSpelling这就是我得到的:
bool Foo_bar( MyType_t a, int b )
Run Code Online (Sandbox Code Playgroud)
Libclang 可能无法打印 #define 名称,因为int在构建解析树时预处理器已经将其替换为 #define 名称。
| 归档时间: |
|
| 查看次数: |
624 次 |
| 最近记录: |