5

I'm looking for a c++ parser which is able to extract all the functions and methods with its signatures. Is there something like this?

I had a look at gccxml there I have the problem, that it is not able to use namespaces and its not fine when only a header file is present.

linuxbuild
  • 15,843
  • 6
  • 60
  • 87
develhevel
  • 3,161
  • 4
  • 21
  • 27
  • +1, by the way for last 2 year as a part of my side project, I have written one parser, which extracts method/function signature from `class, namespace, internal class` etc. I want to make it commercialize sometime. – iammilind May 13 '11 at 13:39
  • @iammilind: i focus on making money materialize. No definite results yet – sehe May 13 '11 at 13:42
  • @sehe, means ? you have also written something like that ? Well what I am talking about is not just a parser. It's a project for automatic garbage collector which includes this part of extracting signatures of pointers, classes, namespaces, function/methods and so on. – iammilind May 13 '11 at 13:44
  • What are you talking about? Of course gccxml does handle and understand namespaces and what do you mean with "not fine when only a header file is present"? – Nordic Mainframe May 13 '11 at 14:19

4 Answers4

4

Most obvious options:

  1. ctags
  2. cscope

Just a sample of the GCC man page:

-fdump-noaddr -fdump-unnumbered -fdump-translation-unit[-n] -fdump-class-hierarchy[-n] -fdump-ipa-all -fdump-ipa-cgraph -fdump-ipa-inline
           -fdump-statistics -fdump-tree-all -fdump-tree-original[-n] -fdump-tree-optimized[-n] -fdump-tree-cfg -fdump-tree-vcg -fdump-tree-alias -fdump-tree-ch -fdump-tree-ssa[-n] -fdump-tree-pre[-n] -fdump-tree-ccp[-n] -fdump-tree-dce[-n]
           -fdump-tree-gimple[-raw] -fdump-tree-mudflap[-n] -fdump-tree-dom[-n] -fdump-tree-dse[-n] -fdump-tree-phiopt[-n] -fdump-tree-forwprop[-n] -fdump-tree-copyrename[-n] -fdump-tree-nrv -fdump-tree-vect -fdump-tree-sink -fdump-tree-sra[-n]
           -fdump-tree-fre[-n] -fdump-tree-vrp

Also there is a gccxml backend

sehe
  • 374,641
  • 47
  • 450
  • 633
1

The Clang compiler obviously has the functionality to do this, if I remember correctly there's even an API to access the code tree generated by the parser.

Milan
  • 3,342
  • 3
  • 31
  • 40
  • 1
    There is. CLang generates an AST (Abstract Syntax Tree) of the code and provides a `ConsumerAST` base class for visiting it. – Matthieu M. May 13 '11 at 14:44
  • I rather thought of libclang (http://llvm.org/devmtg/2010-11/Gregor-libclang.pdf) but that ought to work too – Milan May 13 '11 at 14:50
  • libclang is the C-API. If you have access to C++, you'll have more complete information directly interfacing as some features take time to be ported to the C-API. For simple things as declarations, it should be perfectly suitable, though perhaps not as suitable since C-code does take some amount of effort. – Matthieu M. May 13 '11 at 14:52
1

You can use the -dump option of the abi-compliance-checker tool to parse signatures of functions and methods from your header file(s):

abi-compliance-checker -lib NAME -dump DESC.xml -headers-only -stdout > api.dump

XML-descriptor (DESC.xml) is the following:

<version>
    VERSION
</version>

<headers>
    /path(s)/to/headers/
</headers>

The tool works as following:

  1. Call GCC with -fdump-translation-unit and a set of automagically generated -I... options on the headers specified in the input XML-descriptor;
  2. Parse the AST dump generated by the GCC;
  3. Generate function signatures and type definitions in the Data::Dumper or XML format (if additional -xml option is provided).

The sample signature of int BZ2_bzRead ( int *bzerror, BZFILE *b, void *buf, int len ) function from bzlib.h header looks like:

'228' => {
   'Header' => 'bzlib.h',
   'Line' => '160',
   'Param' => {
                '0' => {
                         'algn' => '4',
                         'name' => 'bzerror',
                         'type' => '30'
                       },
                '1' => {
                         'algn' => '4',
                         'name' => 'b',
                         'type' => '16'
                       },
                '2' => {
                         'algn' => '4',
                         'name' => 'buf',
                         'type' => '68'
                       },
                '3' => {
                         'algn' => '4',
                         'name' => 'len',
                         'type' => '41'
                       }
              },
   'Return' => '41',
   'ShortName' => 'BZ2_bzRead'
 },
linuxbuild
  • 15,843
  • 6
  • 60
  • 87
0

you could try compiling your code with the save-temps flag set on gcc, this makes gcc output the files with macro unfolding and full signatures. these are the .ii files.

Martin Kristiansen
  • 9,875
  • 10
  • 51
  • 83
  • this makes nearly the same output as in the header file, but i need a parsed output that is easy to extract for other programms – develhevel May 13 '11 at 13:45