[cfe-dev] Parsing functions returning _Bool / stdbool with libclang
Mattias Holm
lorrden at openorbit.org
Wed Jan 7 23:02:42 PST 2015
I have been using libclang (3.5 on Linux) for extracting information in
headers. When parsing the following C99 code:
---
#include <stdbool.h>
bool foo(int a, int b);
_Bool bar(int a, int b);
---
I manage to visit only the second function's arguments. When running my
test app, I get the following output (code is below):
visiting function: foo
type: int (int, int)
rettype: int
visiting function: bar
type: _Bool (int, int)
rettype: _Bool
arg: a type: int
arg: b type: int
As can be seen, firstly the type of foo is wrong, secondly, the argument
visitor is never invoked
I find this rather strange and I am wondering if there is something
obviously wrong with my libclang based setup (i.e the main function), or
if this is a known issue in libclang. I tried to search bugzilla, but
found nothing similar.
Cheers,
Mattias
My code...
---
#include "clang-c/Index.h"
#include <stdio.h>
#include <stdlib.h>
enum CXChildVisitResult
visitFuncParam(CXCursor Cursor, CXCursor Parent, CXClientData
ClientData)
{
CXType ArgTyp = clang_getCursorType(Cursor);
const char *ArgName =
clang_getCString(clang_getCursorSpelling(Cursor));
printf("\targ: %s type: %s\n",
ArgName, clang_getCString(clang_getTypeSpelling(ArgTyp)));
return CXChildVisit_Continue;
}
enum CXChildVisitResult
visitFuncDecl(CXCursor Cursor, CXCursor Parent, CXClientData ClientData)
{
CXType FuncTyp = clang_getCursorType(Cursor);
const char *FuncName =
clang_getCString(clang_getCursorSpelling(Cursor));
CXType FuncRetTyp = clang_getResultType(FuncTyp);
printf("visiting function: %s\n", FuncName);
printf("\ttype: %s\n",
clang_getCString(clang_getTypeSpelling(FuncTyp)));
printf("\trettype: %s\n",
clang_getCString(clang_getTypeSpelling(FuncRetTyp)));
clang_visitChildren(Cursor, visitFuncParam, NULL);
return CXChildVisit_Continue;
}
enum CXChildVisitResult
visitDecls(CXCursor Cursor, CXCursor Parent, CXClientData ClientData)
{
CXSourceLocation SL = clang_getCursorLocation(Cursor);
if (clang_Location_isFromMainFile(SL)) {
if (clang_getCursorKind(Cursor) == CXCursor_FunctionDecl) {
visitFuncDecl(Cursor, Parent, NULL);
}
}
return CXChildVisit_Continue;
}
int
main(int argc, const char *argv[argc])
{
CXIndex Index = clang_createIndex(0, 0);
CXTranslationUnit TU =
clang_parseTranslationUnit(Index, 0, argv, argc, 0, 0,
0);//CXTranslationUnit_SkipFunctionBodies);
CXCursor C = clang_getTranslationUnitCursor(TU);
clang_visitChildren(C, visitDecls, NULL);
clang_disposeTranslationUnit(TU);
clang_disposeIndex(Index);
return 0;
}
---
More information about the cfe-dev
mailing list