views:

1070

answers:

9

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?

+3  A: 

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.

phjr
Good answer but seems fairly old. Anyone know of more up to date sources?
Dave Hillier
+4  A: 

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.

anonfunc
A: 

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
+2  A: 

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
+1  A: 

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

CesarB
A: 

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
A: 

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

philippe
A: 

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

A: 

See http://www.semanticdesigns.com/Products/FrontEnds/CppFrontEnd.html for a full-featured C++ parser that works with many dialects of C++. It builds ASTs and symbol tables, and can infer the type of any expression. It is built on top of the DMS Software Reengineering Toolkit, which enables you to build arbitrary C++ analysis or translation tools.

Ira Baxter