I’m using libclang to parse some code, and I want to find calls to a specific function and the types of its arguments.
For example, let’s say the code is:
void foo(int a, ...) {}
enum test {
ENUM_VAL1,
ENUM_VAL2
};
int main() {
enum test e = ENUM_VAL1;
int a = 1;
foo(a, e);
}
In this case, I want to find function “foo” and see that it has two arguments, the first is an integer, and the second is an “enum test”.
Running the following code:
static enum CXChildVisitResult visitFuncCalls(CXCursor current_cursor,
CXCursor parent,
CXClientData client_data) {
if (clang_getCursorKind(current_cursor) != CXCursor_CallExpr) {
return CXChildVisit_Recurse;
}
static const char *FUNCTION_NAME = "foo";
const CXString spelling = clang_getCursorSpelling(current_cursor);
if (strcmp(clang_getCString(spelling), FUNCTION_NAME) != 0) {
return CXChildVisit_Recurse;
}
clang_disposeString(spelling);
for (int i = 0; i < clang_Cursor_getNumArguments(current_cursor); i++) {
CXCursor argument = clang_Cursor_getArgument(current_cursor, i);
CXType argument_type = clang_getCursorType(argument);
CXString argument_type_spelling = clang_getTypeSpelling(argument_type);
printf("Argument %d: %sn", i, clang_getCString(argument_type_spelling));
clang_disposeString(argument_type_spelling);
}
return CXChildVisit_Continue;
}
int main() {
const CXTranslationUnit unit = clang_parseTranslationUnit(
index, "file.c", NULL, 0, NULL, 0, CXTranslationUnit_None);
const CXCursor cursor = clang_getTranslationUnitCursor(unit);
clang_visitChildren(cursor, visitFuncCalls, NULL /* client_data*/);
}
I get:
Argument 0: int
Argument 1: unsigned int
So, basically, the compiler is ignoring the fact that this type is an enum, and shows it as an unsigned int. Is there a way to know this argument is an enum?