[cfe-dev] [libclang] Parsing an empty file.

Jacob Carlborg doob at me.com
Mon Jun 11 00:23:23 PDT 2012

On 2012-06-11 02:35, Cyril Roelandt wrote:
> Hi,
> I've tried using libclang with the following code :

> I would have thought nothing would be printed, since the file is empty.
> Trying to understand this behaviour, I noticed that dumping the AST of
> empty.c gave similar results :
> $ clang -cc1 -ast-dump empty.c
> typedef __int128 __int128_t;
> typedef unsigned __int128 __uint128_t;
> struct __va_list_tag {
>       unsigned int gp_offset;
>       unsigned int fp_offset;
>       void *overflow_arg_area;
>       void *reg_save_area;
> };
> typedef struct __va_list_tag __va_list_tag;
> typedef __va_list_tag __builtin_va_list[1];
> Is there any way to only analyze the code that is actually written by
> the programmer ?

I don't know if there's a better way but what I do is I compare the file 
information for a declaration (or similar) with the input file. If 
they're the same I process the declaration, otherwise I just skip the 

The C++ AIP has a function for this but there doesn't seem be one in 
libclang so I rolled my own implementation.

This is an example in pseudo code:

foreach (cursor ; topLevel)
     if (cursor.location.spelling.file == inputFile)

/Jacob Carlborg

More information about the cfe-dev mailing list