11

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?

Paweł Hajdan
  • 18,074
  • 9
  • 49
  • 65
Dave Hillier
  • 18,105
  • 9
  • 43
  • 87

9 Answers9

5

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.

Paweł Hajdan
  • 18,074
  • 9
  • 49
  • 65
4

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.

Dwight Holman
  • 1,570
  • 14
  • 15
3

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.

linuxbuild
  • 15,843
  • 6
  • 60
  • 87
2

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.

Dave Hillier
  • 18,105
  • 9
  • 43
  • 87
  • The answer might be outdated since 2008. See http://eli.thegreenplace.net/2011/07/03/parsing-c-in-python-with-clan for more modern overview of this capability of clang. – anatoly techtonik Jan 27 '15 at 13:47
2

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++.

zvrba
  • 24,186
  • 3
  • 55
  • 65
1

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

CesarB
  • 43,947
  • 7
  • 63
  • 86
1

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

philant
  • 34,748
  • 11
  • 69
  • 112
1

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();

0

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.

Ferruccio
  • 98,941
  • 38
  • 226
  • 299