How to analyze a C++ header file with a clang tool?

Hi
I have seen the examples in clang/tools/, and even built a few simple tools myself.
But all of them work on source files.
For example, I am not sure if I can use clang-check on a header file.

How do I write a clang tool that will allow me to read a header file and …say… print the names of all the top level declarations?
This is easy for a source file but I can not figure out how to handle headers.

Manasij Mukherjee

Is there a difference? Just pass the header file to the tool instead. Clang (whichever libraries you use) won't make a difference, as far as I know.

an easy workaround could be to create a source file which contains only include directives of the desired header files.

In article <lmmfo6$at5$1@ger.gmane.org>,
    Jacob Carlborg <doob@me.com> writes:

> Hi
> I have seen the examples in clang/tools/, and even built a few simple
> tools myself.
> But all of them work on source files.
> For example, I am not sure if I can use clang-check on a header file.

Is there a difference? Just pass the header file to the tool instead.

Many tools use a compilation database to specify the set of defined
symbols, include order, etc., all of which are still relevant to
analyzing a header file.

Unfortunately the compilation database is typically generated from a
build system that says how source files are compiled into object
files, so it won't have instructions for how to compile a header file.

However, the compilation database is just a JSON file with a simple
syntax, so you should be able to construct command lines for a header
file just as you would for a source file. I haven't tried this, so I
don't know how/if it will work.

I see. I don't use a compilation database for my tool. I'm using libclang and the tool is intended to process header files. Works perfectly fine.

In article <lmnqj7$cql$1@ger.gmane.org>,
    Jacob Carlborg <doob@me.com> writes:

I see. I don't use a compilation database for my tool. I'm using
libclang and the tool is intended to process header files. Works
perfectly fine.

Yes, I don't think this is a limitation of the library so much as some
of the tooling classes expect a compilation database specified as a
command-line argument. While preparing for my C++ Now! 2014
presentation, I discovered that even in this case it may be possible
to specify the actual compile command on the command-line to the tool
and separate it from other arguments with --. However, this depends
on specific support for this from the tool and not every tool may
support this (although if they immitated remove-cstr-calls, then they
would have it).

Sorry, this turned out to be a rather trivial problem.

I forgot to force C++ as the language being parsed.

So, seeing errors like <unknown type ‘namespace’>, I sort of panicked!