是否有工具可以添加“覆盖"?现有 C++ 代码的标识符 [英] Is there a tool to add the "override" identifier to existing C++ code
问题描述
任务
我正在研究如何最好地添加 C++0x 的 override
已在大量 C++ 代码中覆盖的所有现有方法的标识符,无需手动执行.
(我们有很多、几十万行的代码,手动完成是完全不可能的.)
目前的想法
我们的编码标准规定,我们应该为派生类中的所有隐式虚拟方法添加 virtual
关键字,即使完全没有必要(以帮助理解).
因此,如果我要自己编写添加脚本,我会编写一个脚本来读取我们所有的头文件,找到所有以 virtual 开头的函数,并在以下分号之前插入 override
.然后在支持override
的编译器上编译,修复基类中的所有错误.
但我真的宁愿不使用这种本土的方式,因为:
- 这显然会很乏味且容易出错.
- 并不是每个人都记得每次都添加 virtual 关键字,因此此方法会遗漏一些现有的覆盖
有现成的工具吗?
那么,是否已经有一种工具可以解析 C++ 代码、检测覆盖的现有方法并将 override
附加到它们的声明中?
(我知道诸如 PC-lint 之类的静态分析工具会警告看起来像它们的函数应该是覆盖.我所追求的是实际上会修改我们的代码的东西,以便将来在编译时检测到覆盖中的错误,而不是稍后在静态分析中)
(如果有人想指出 C++03 不支持覆盖"...在实践中,我会添加一个宏,而不是实际的覆盖"标识符,以使用我们的不支持此功能的旧编译器上的代码.因此,在添加标识符后,我将运行一个单独的脚本以将其替换为我们将要使用的任何宏...)
提前致谢...
LLVM 项目正在开发一个名为cpp11-migrate"的工具;目前具有以下特点:
- 将循环转换为基于范围的 for 循环
- 将空指针常量(如
NULL
或0
)转换为 C++11nullptr
- 用
auto
类型说明符替换变量声明中的类型说明符 - 将
override
说明符添加到适用的成员函数
此工具在此处有文档记录,应作为 clang 3.3 的一部分发布.但是,您可以立即下载源代码并自行构建.
编辑
更多信息:
- C++11 迁移器的状态 - 一篇博文,日期为 2013 年 4 月 15 日
- cpp11-migrate 用户手册
编辑 2:2013-09-07
cpp11-migrate"已更名为clang-modernize".对于 Windows 用户,它现在包含在新的 LLVM 快照构建中.
编辑 3:2020-10-07
clang-modernize"已将 bee 重命名为Clang-Tidy".>
The task
I am trying to work out how best to add C++0x's override
identifier to all existing methods that are already overrides in a large body of C++ code, without doing it manually.
(We have many, many hundreds of thousands of lines of code, and doing it manually would be a complete non-starter.)
Current idea
Our coding standards say that we should add the virtual
keyword against all implicitly virtual methods in derived classes, even though strictly unnecessary (to aid comprehension).
So if I were to script the addition myself, I'd write a script that read all our headers, found all functions beginning with virtual, and insert override
before the following semi-colon. Then compile it on a compiler that supports override
, and fix all the errors in base classes.
But I'd really much rather not use this home-grown way, as:
- it's obviously going to be tedious and error-prone.
- not everyone has remembered, every time, to add the virtual keyword, so this method would miss out some existing overrides
Is there an existing tool?
So, is there already a tool that parses C++ code, detects existing methods that overrides, and appends override
to their declarations?
(I am aware of static analysis tools such as PC-lint that warn about functions that look like they should be overrides. What I'm after is something that would actually munge our code, so that future errors in overrides will be detected at compiler-time, rather than later on in static analysis)
(In case anyone is tempted to point out that C++03 doesn't support 'override'... In practice, I'd be adding a macro, rather than the actual "override" identifier, to use our code on older compilers that don't support this feature. So after the identifier was added, I'd run a separate script to replace it with whatever macro we're going to use...)
Thanks in advance...
There is a tool under development by the LLVM project called "cpp11-migrate" which currently has the following features:
- convert loops to range-based for loops
- convert null pointer constants (like
NULL
or0
) to C++11nullptr
- replace the type specifier in variable declarations with the
auto
type specifier - add the
override
specifier to applicable member functions
This tool is documented here and should be released as part of clang 3.3. However, you can download the source and build it yourself today.
Edit
Some more info:
- Status of the C++11 Migrator - a blog post, dated 2013-04-15
- cpp11-migrate User’s Manual
Edit 2: 2013-09-07
"cpp11-migrate" has been renamed to "clang-modernize". For windows users, it is now included in the new LLVM Snapshot Builds.
Edit 3: 2020-10-07
"clang-modernize" has bee renamed to "Clang-Tidy".
这篇关于是否有工具可以添加“覆盖"?现有 C++ 代码的标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!