[cfe-dev] Parsing functions returning _Bool / stdbool with libclang

Richard Smith richard at metafoo.co.uk
Thu Jan 8 17:40:50 PST 2015


On Wed, Jan 7, 2015 at 11:02 PM, Mattias Holm <lorrden at openorbit.org> wrote:

> 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.


What's probably happening is:

1) You turned off displaying of diagnostics and didn't specify anything
else to do with them, so they're silently discarded, and
2) <stdbool.h> is not found.

The return type of 'int' is invented because lookup of 'bool' failed, and
we don't bother to build the parameters for the invalid declaration of
'foo'.


> 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);
>

Try setting the second parameter (displayDiagnostics) to 1 here to see the
problem.

  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;
> }
> ---
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150108/7ac963e5/attachment.html>


More information about the cfe-dev mailing list