Handling multiple files with libclang

What I've understood, libclang can only parse one file/translation unit at the time. I'm working on a tool where I would like to be able to pass multiple files on the command line and process them with libclang. Should I be calling "clang_parseTranslationUnit", or similar, once for each file to accomplish this?

If I'm supposed to call "clang_parseTranslationUnit" multiple times then my next question is: how do I handle the command line arguments? I know that "clang_parseTranslationUnit" handles the command line arguments but how do I get the rest of the files, the ones that weren't handle by libclang.

I could handle the command line arguments myself but that would basically mean to replicate what libclang already does. This would also mean that my tool could break if clang removes a flag or adds a new one.

What I've understood, libclang can only parse one file/translation unit
at the time. I'm working on a tool where I would like to be able to pass
multiple files on the command line and process them with libclang.
Should I be calling "clang_parseTranslationUnit", or similar, once for
each file to accomplish this?

Yes, exactly.

If I'm supposed to call "clang_parseTranslationUnit" multiple times then
my next question is: how do I handle the command line arguments? I know
that "clang_parseTranslationUnit" handles the command line arguments but
how do I get the rest of the files, the ones that weren't handle by
libclang.

Unfortunately, libclang just skips over all files beyond the first one, and doesn't even tell you that it's doing it. We would need to introduce some new API into libclang to parse a command line into its respective pieces to get this information back to the user.

Since you're building the tool yourself, you might consider just tweaking your command-line syntax to make this obvious for your tool where the source files are. For example, do something like:

  your-tool source1.c source2.cpp source3.cpp -- -Wall <command-line arguments>

  - Doug

Unfortunately, libclang just skips over all files beyond the first one, and doesn't even tell you that it's doing it. We would need to introduce some new API into libclang to parse a command line into its respective pieces to get this information back to the user.

That would be nice to have.

Since you're building the tool yourself, you might consider just tweaking your command-line syntax to make this obvious for your tool where the source files are. For example, do something like:

  your-tool source1.c source2.cpp source3.cpp -- -Wall<command-line arguments>

  - Doug

I've currently implemented it like this:

I'm iterating over all arguments. If an argument doesn't start with '-' and then previous argument doesn't start with '-' then I'm considering it to be an input file. I'm not sure if this covers all cases.