问题
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?
回答1:
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.
回答2:
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.
回答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.
回答4:
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.
回答5:
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++.
回答6:
doxygen can usually parse enough of C++ to create documentation for the code. It also has a XML output option.
回答7:
Did you look at Mockcpp, AMOP and mockpp ? You could see how they parses C++ - if none of them fit your needs.
回答8:
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();
回答9:
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.
来源:https://stackoverflow.com/questions/236086/parsing-c-to-generate-unit-test-stubs