Howto extract C++ functions with Clang?

Hi guys,

I want to use Clang to extract all C++ function signatures, from a C++ header file and transfer it to a easy to parse/work format (the best would be XML or something [I knew about GCCXML but it does not help me because it could not manage namespaces]).

E.g. I have a function in my header file like:
int makeSometing(int a, int b);

now I want to run Clang and get a output file which I can simply parse to extract all function signatures.

Best regards, Devel

Hi guys,

I want to use Clang to extract all C++ function signatures, from a C++ header file and transfer it to a easy to parse/work format (the best would be XML or something [I knew about GCCXML but it does not help me because it could not manage namespaces]).

I'm fairly certain that GCC-XML handles namespaces; it doesn't keep as much source information as Clang does, but it handles all of C++.

E.g. I have a function in my header file like:
int makeSometing(int a, int b);

now I want to run Clang and get a output file which I can simply parse to extract all function signatures.

You can write an ASTConsumer that prints all of the functions; see, e.g., the PrintFunctionNames example.

Alternatively, you can use the C interface to Clang's ASTs, libclang: http://clang.llvm.org/doxygen/group__CINDEX.html

  - Doug

There really isn't anything quite like that at the moment, but it
shouldn't be too hard to write... it should be a very straightforward
RecursiveASTVisitor (there's an example of a very simple one in
lib/Frontend/BoostConAction.cpp.) See examples/PrintFunctionNames for
how to write your own little AST consuming utility. I'd say the hard
part is actually figuring out what exactly the output should look
like.

Please feel free to ask any questions if you need help, either here or
on the IRC channel (#llvm on irc.oftc.net). And I think other people
would be interested in this work, so if you get something usable,
please send it here.

-Eli