Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tool for retrieving the list of functions and methods in a C++ code base

Tags:

c++

methods

list

Considering a C++ based source code i'm looking for a tool that can output a plain text list of methods with/without interfaces, this means 2 options, only the method's name or the complete interface's signature.

I would like to apply this to source code in C too.

Thanks.

like image 974
Ken Avatar asked Sep 10 '12 22:09

Ken


2 Answers

A reasonable solution cam be built easily using Doxygen's XML format and a little python script to parse it. Doxygens XML output is not very well documented, but seems pretty complete.

Here's my python script:

import lxml.etree
import glob

prefix = "/Code/stack_overflow_examples/list_functions_by_doxygen/"

for filename in glob.glob("xml/*.xml"):
    f = open( filename, "r" )
    xml = lxml.etree.parse(f)
    for x in xml.xpath('//memberdef[@kind="function"]'):
        srcfile = x.xpath('.//location/@file')[0].replace(prefix,'') 
        srcline = x.xpath('.//location/@line')[0]
        definition = x.xpath('.//definition/text()')[0] 
        args = x.xpath('.//argsstring/text()')[0]
        print( "%s:%s: %s%s" % ( srcfile, srcline, definition, args) )

When run on this file:

/**
 * This is a test function.
 */
int a_function( Baz & b )
{
  return 7;
}

void another_function( Boo & b )
{
}

class Foo
{
  private:
    int a_private_member_function();
  public:
    int a_public_member_function();
};

It generates this output:

test.cpp:16: int Foo::a_private_member_function()
test.cpp:18: int Foo::a_public_member_function()
test.cpp:5: int a_function(Baz &b)
test.cpp:10: void another_function(Boo &b)

You'll just need to make a couple of changes to the Doxyfile you use to generate the "docs". Here's the changes I used:

EXTRACT_ALL = YES
EXTRACT_PRIVATE = YES
EXTRACT_STATIC = YES
EXTRACT_LOCAL_METHODS = YES
EXTRACT_ANON_NSPACES = YES
CASE_SENSE_NAMES = YES

GENERATE_HTML = NO
GENERATE_LATEX = NO
GENERATE_XML = YES
like image 52
Michael Anderson Avatar answered Oct 06 '22 00:10

Michael Anderson


According to Jonathan Wakely's comment you can use ctags like that:

ctags -x --c-types=f --format=1 file.c

It would list your functions:

celsjusz2Fahrenheit   17 file.c double celsjusz2Fahrenheit(double celsjuszDegree)
celsjusz2Kelwin    21 file.c double celsjusz2Kelwin(double celsjuszDegree)
main               44 file.c int main()

If you want to list methods proceded by class name, You can use:

ctags -x --c++-types=f --extra=q --format=1 file.cc

it will print e.g:

S::f                9 file.cc void f()
S::f1              11 file.cc void f1()
S::f2              13 file.cc void f2()
celsjusz2Fahrenheit   17 file.cc double celsjusz2Fahrenheit(double celsjuszDegree)
celsjusz2Kelwin    21 file.cc double celsjusz2Kelwin(double celsjuszDegree)

EDIT (because of comments): Args means (copied from ctags --help):

  • -x - Print a tabular cross reference file to standard output.
  • --extra=q - adds extra flag, where q means "Include an extra class-qualified tag entry for each tag"
  • --format=1 - Force output of specified tag file format
like image 29
baziorek Avatar answered Oct 06 '22 00:10

baziorek