解析 C++ 生成单元测试存根

发布于 2024-07-08 02:04:40 字数 159 浏览 10 评论 0原文

我最近一直在尝试为一些遗留代码创建单元测试。

我一直采用使用链接器的方法来向我显示哪些函数会导致链接错误,对源进行grep以查找定义并从中创建存根。

有更容易的方法吗? 是否有某种 C++ 解析器可以以某种易于使用的形式为我提供类定义,我可以从中生成存根?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

以歌曲疗慰 2024-07-15 02:04:40

您可能需要调查http://os.inf.tu-dresden。 de/vfiasco/lated.html#parsing。 但是C++解析很难。

另一方面,也许 ctags 或类似的东西可以提取类定义...

您也可以尝试编写自己的简单(?)解析器来从头文件生成类存根...

我试图给您一些指示。 正如你所看到的,这个问题并不容易解决。 但希望你至少可以自动化其中的一部分。

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.

故事还在继续 2024-07-15 02:04:40

Gcc XML 用于一些项目,例如 Common Lisp 的自动 FFI。
它与 G++ 编译器绑定以生成表示源代码的 XML。 从这里开始,任何 XML 处理工具都可以帮助您实现目标。

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.

旧时模样 2024-07-15 02:04:40

abi-compliance-checker工具可以用作C/C++头的解析器文件:

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

VER.xml 输入文件如下:

<version>
  1.0
</version>

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

输出 api.xml 文件包含来自结构化形式的头文件的函数签名和其他信息:

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

另请参阅有关 <一个 href="https://github.com/lvc/api-sanity-checker" rel="nofollow noreferrer">api-sanity-checker 工具,可以为API 通过分析头文件中的声明。

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.

这样的小城市 2024-07-15 02:04:40

http://clang.llvm.org/ 看起来很有希望,但并不完整。

http://www.boost.org/doc/ libs/1_36_0/libs/python/pyste/index.html 使用 GCCXML 为 C++ 代码生成包装器以连接 python。 这证明 GCCXML 已用于类似的概念。

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.

帝王念 2024-07-15 02:04:40

如果您使用的平台使用 DWARF 调试格式(主要是 UNIX),则可以使用 libdwarf 来解析调试信息并提取有关所有内容的信息(函数原型、类定义等)。 比 C++ 更结构化且更易于解析。

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

辞取 2024-07-15 02:04:40

doxygen 通常可以解析足够的 C++ 来为代码创建文档。 它还具有 XML 输出选项。

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

执手闯天涯 2024-07-15 02:04:40

您是否看过 MockcppAMOPmockpp ? 你可以看看他们如何解析 C++ - 如果它们都不符合你的需求。

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

逆光下的微笑 2024-07-15 02:04:40

Eclipse CDT 项目提供了一个高级的 C++ 解析器。 界面非常简单。
下面的代码片段可以给出足够的提示。

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

IStructure 结构 = (IStructure) 元素;
IMethodDeclaration[] 方法 = Structure.getMethods();
IField[] 字段 = Structure.getFields();

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

等待圉鍢 2024-07-15 02:04:40

如果您使用的是 Windows 平台,您可能需要查看 Microsoft Phoenix 项目 。 它是一个新的编译器框架,可让您连接到编译过程的任何阶段。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文