By Dave Hillier


2008-10-25 08:14:04 8 Comments

I've recently been trying to create units tests for some legacy code.

I've been taking the approach of using the linker to show me which functions cause link errors, greping the source to find the definition and creating a stub from that.

Is there an easier way? Is there some kind of C++ parser that can give me class definitions, in some easy to use form, from which I can generate stubs?

9 comments

@linuxbuild 2012-09-22 19:20:54

The abi-compliance-checker tool can be used as a parser of C/C++ header files:

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

VER.xml input file is the following:

<version>
  1.0
</version>

<headers>
  /path1/to/header(s)/
  /path2/to/header(s)/
   ...
</headers>

The output api.xml file contains function signatures and other information from header files in the structured form:

...
<symbol>
    <id>37348</id>
    <mangled>_ZN7MWidget11qt_metacallEN11QMetaObject4CallEiPPv</mangled>
    <short>qt_metacall</short>
    <class>13749</class>
    <header>mwidget.h</header>
    <line>45</line>
    <return>44</return>
    <spec>virtual</spec>
    <parameters>
        <param>
            <name>p1</name>
            <type>4078</type>
            <algn>4</algn>
            <pos>0</pos>
        </param>
        <param>
            <name>p2</name>
            <type>44</type>
            <algn>4</algn>
            <pos>1</pos>
        </param>
        <param>
            <name>p3</name>
            <type>3905</type>
            <algn>8</algn>
            <pos>2</pos>
        </param>
    </parameters>
</symbol>
...

See also information about api-sanity-checker tool, that can generate basic unit test cases for every function in the API through the analysis of declarations in header files.

@tully 2008-12-13 01:54:52

Eclipse CDT project provide an advanced C++ parser. The interface is quite easy. The following code snippet can give enough hint.

ITranslationUnit tu = CoreModelUtil.findTranslationUnit(file);
ICElement[] elements = tu.getChildren();

IStructure structure = (IStructure) element;
IMethodDeclaration[] methods = structure.getMethods();
IField[] field = structure.getFields();

@philant 2008-12-08 12:28:59

Did you look at Mockcpp, AMOP and mockpp ? You could see how they parses C++ - if none of them fit your needs.

@zvrba 2008-10-25 16:45:56

If you're on aplatform that uses DWARF debugging format (mostly UNIX), you can use libdwarf to parse debugging information and extract information about everything (function prototypes, class definitions, etc). Much more structured and easier to parse than C++.

@Dave Hillier 2008-10-25 11:45:46

http://clang.llvm.org/ looks promising but is incomplete.

http://www.boost.org/doc/libs/1_36_0/libs/python/pyste/index.html uses GCCXML to generate wrappers for C++ code to interface python. This proves that GCCXML has been used for a similar concept.

@anatoly techtonik 2015-01-27 13:47:42

The answer might be outdated since 2008. See eli.thegreenplace.net/2011/07/03/parsing-c-in-python-with-cl‌​an for more modern overview of this capability of clang.

@CesarB 2008-10-25 12:09:25

doxygen can usually parse enough of C++ to create documentation for the code. It also has a XML output option.

@Ferruccio 2008-10-25 11:25:39

If you're on the Windows platform, you might want to have a look at the Microsoft Phoenix project. It's a new compiler framework that lets you hook into any stage of the compilation process.

@Dwight Holman 2008-10-25 09:31:15

Gcc XML is used in some projects, such as automatic FFI for Common Lisp. It ties into the G++ compiler to generate XML representing the source. From there, any XML processing tool could help you reach your goal.

@PaweĊ‚ Hajdan 2008-10-25 09:15:54

You may want to investigate http://os.inf.tu-dresden.de/vfiasco/related.html#parsing. But C++ parsing is hard.

On the other hand, maybe ctags or something similar can extract class definitions...

You may also try to write your own simple (?) parser to generate class stubs from header files...

I tried to give you some pointers. As you see, the problem is not easy. But hopefully you can automate at least some part of it.

@Dave Hillier 2008-10-25 11:07:50

Good answer but seems fairly old. Anyone know of more up to date sources?

Related Questions

Sponsored Content

30 Answered Questions

[SOLVED] How do you parse and process HTML/XML in PHP?

34 Answered Questions

[SOLVED] What's the difference between a mock & stub?

  • 2010-08-11 14:19:57
  • never_had_a_name
  • 258759 View
  • 781 Score
  • 34 Answer
  • Tags:   testing mocking stub

47 Answered Questions

21 Answered Questions

[SOLVED] What is the "-->" operator in C++?

24 Answered Questions

[SOLVED] Image Processing: Algorithm Improvement for 'Coca-Cola Can' Recognition

35 Answered Questions

25 Answered Questions

[SOLVED] How do I parse a string to a float or int in Python?

31 Answered Questions

[SOLVED] Unit Testing C Code

1 Answered Questions

[SOLVED] The Definitive C++ Book Guide and List

  • 2008-12-23 05:23:56
  • grepsedawk
  • 2110431 View
  • 4249 Score
  • 1 Answer
  • Tags:   c++ c++-faq

16 Answered Questions

[SOLVED] Parse JSON in JavaScript?

Sponsored Content