12

我们应用程序的源代码有几十万行,几千个文件,而且有些地方很老——这个应用程序最初是在 1995 年或 1996 年编写的。在过去的几年里,我的团队已经大大提高了源代码的质量,但是仍然有一个问题特别困扰我:许多类在其头文件中完全定义了许多方法。

在某些情况下,我对在标头中内联声明的方法没有任何问题 - 结构的构造函数,内联可显着提高速度的简单方法(我们有一些像这样的数学函数)等。但是内联方法的自由使用并不明显原因是:

  • 凌乱
  • 很难找到方法的实现(尤其是在类树中搜索虚函数,只发现一个类在标题中声明了它的版本......)
  • 可能会增加编译后的代码大小
  • 可能会导致我们的链接器出现问题,这对于大型代码库来说是出了名的不稳定。说句公道话,在过去的几年里它变得更好了,但并不完美。

最后一个原因现在可能给我们带来了问题,这是一个很好的理由去检查代码库并将大多数定义移动到源文件中。

我们的代码库很大。 有没有一个自动化工具可以为我们做(大部分)这件事?

笔记:

  • 我们使用Embarcadero RAD Studio 2010。也就是说,C++的方言包括VCL和其他扩展等。
  • 一些标头是独立的,但大多数都与相应的 .cpp 文件配对,就像您通常那样。除了扩展名之外,文件名是相同的,即,如果在 Xh 中定义了方法,则可以将它们移动到 X.cpp。这也意味着该工具不必处理整个项目的解析——它可能只解析单独的 .cpp/.h 文件对,忽略包含等,只要它能够可靠地识别定义了主体的方法在类声明中并移动它。
4

4 回答 4

6

您可以尝试Lazy C++。我没有使用过它,但我相信它是一个命令行工具,可以做你想做的事。

于 2012-01-13T16:04:36.097 回答
3

If the code is working then I would vote against any major automated rewrite.
Lots of work could be involved fixing it up.

Small iterative improvements over time is a better technique as you will be able to test each change in isolation (and add unit tests). Anyway your major complaint about not being able to find the code is not a real problem and is already solved. There are already tools that will index your code base so your editor will jump to the correct function definition without you having to search for it. Take a look at ctags or the equivalent for your editor.

  • Messy

    Subjective

  • Makes it hard to find the implementation of a method (especially searching through a tree of classes for a virtual function, only to find one class had its version declared in the header...)

    There are already tools available for finding the function. ctags will make a file that allows you to jump directly to the function from any decent editor (vim/emacs). I am sure your editor if nto one of these has the equivalent tool.

  • Probably increases the compiled code size

    Unlikely. The compiler will choose to inline or not based on internal metrics not weather it is marked inline in the source.

  • Probably causes issues for our linker, which is notoriously flaky for large codebases. To be fair, it has got much better in the past few years, but it's not perfect.

    Unlikely. If your linker is flakey then it is flakey it is not going to make much difference where the functions are defined as this has no bearing on if they are inlined anyway.

于 2012-01-13T17:02:23.633 回答
1

XE2 包括一个新的静态分析器。试一试 C++Builer 的新版本可能是值得的。

于 2012-01-14T16:56:52.377 回答
1

你有很多问题需要解决:

  • 如何理想地重新组合源文件和头文件
  • 如何自动化代码修改以执行此操作

在这两种情况下,您都需要一个具有全名解析的强大 C++ 解析器来准确地确定依赖关系。

然后,您需要能够可靠地修改 C++ 源代码的机器。

我们的DMS Software Reengineering Toolkit及其C++ 前端可用于此目的。DMS 已用于大规模 C++ 代码重构;请参阅http://www.semdesigns.com/Company/Publications/并追踪第一篇论文“案例研究:通过自动程序转换重新设计 C++ 组件模型”。(您可以从那里下载这篇论文的旧版本,但发表的版本更好)。AFAIK,DMS 是唯一用于大规模转换 C++ 的工具。

这个关于重组代码的 SO 讨论直接解决了分组问题。

于 2012-01-13T20:43:50.163 回答