使用某些 RTL/VCL 或 Delphi 语言元素时如何生成警告

发布于 2024-12-08 00:23:28 字数 368 浏览 6 评论 0原文

我想将某些元素、函数和类标记为已弃用,以便编译器生成警告。

我的目的是使源代码的某些方面现代化。尤其是当关注更多平台独立性时。

例如,通过将 WinAPI 和特殊 Windows 类(例如 TRegistry)标记为“已弃用”,它们可以被替换或至少移动并使其独立于平台。 至于语言元素,我特别想弃用 withlabelgoto 关键字。

我已经尝试通过在单元文件的底部插入 {$IF DECLARED(...)}{$MESSAGE WARN...} 来生成函数和类的警告,但这将核心单元显然失败了......

有人知道有一个工具可以做到这一点吗?

I would like to mark certain elements, functions and classes as deprecated, so that the compiler generates a warning.

My intention is to modernize certain aspects of the source. Especially when focusing on more platform independence.

E.g. By marking the WinAPI and special Windows classes (such as TRegistry) as "deprecated" they could be replaced or at least moved and made platform independent.
As for the language elements, I would especially like to deprecate the with, label and goto keywords.

I already tried to generate a warning for functions and classes by inserting a {$IF DECLARED(...)}{$MESSAGE WARN...} into the bottom of the unit file, but this will evidently fail with the core units...

Does somebody know a Tool that does this?

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

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

发布评论

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

评论(4

旧时模样 2024-12-15 00:23:28

您无法弃用 withgoto 等关键字。如果您想使代码现代化,只需将这些单词作为整个单词查找,如果找到它们,更改您的代码。

Win.* 核心单元中的标识符也是如此。您可以修改 RTL 并重新编译它,也可以从 此处并指定所有例程和类型平台,或者只是单位。我建议不要重新编译 RTL。你如何使用 JEDI 的单位,取决于你。

除了 Win.* 单元之外,大多数 RTL 实际上都是独立于平台的,甚至 withgoto 也可以在 64 位或 Mac 上工作。

我的建议:找到一种更好的方法来使代码现代化。不要试图否定 RTL 提供的功能。

There is no way you can deprecate keywords like with, goto, etc. If you want to modernize your code, simply look for these words as whole word and if you find them, change your code.

The same is true for identifiers in the Win.* core units. You could modify the RTL and recompile it, or you could download the JEDI API headers from here and designate all the routines and types platform, or just the units. I would recommend against recompiling the RTL. What you do with, say, units from JEDI, is up to you.

Except for the Win.* units, most of the RTL is actually platform independent, and even withand goto will work in 64 bit or on the Mac too.

My advice: find a better way to modernize your code. Don't try to deprecate what the RTL offers.

梦境 2024-12-15 00:23:28

作为警告的替代方案,您可以使用一个简单的应用程序来解析所有源代码并报告这些构造的使用情况。

通过一些调查,我相信您可以找到一些静态代码分析工具,它们甚至可以更进一步并提醒您某些代码气味。 (例如,不正确地使用 try..finally,吞掉异常)。

将应用程序或静态代码分析器作为构建过程中的一个步骤插入,您就会收到相当于编译时警告的信息。

As an alternative to warnings, you could go with a simple app that parses all source code and reports the use of these constructs.

With a little investigation, I'm sure you can find some static code analysis tools that will even go a bit further and alert you to certain code-smells. (E.g. incorrect use of try..finally, swallowed exceptions).

Plug the app or the static code analyser in as a step in your build process, and you have the equivalent to compile-time warnings.

白色秋天 2024-12-15 00:23:28

无论您多么讨厌 withlabelgoto 等核心语言功能,您都无法对其发出警告。
事实上,这些是保留字,您应该已经注意到这一点。

这些功能不存在于任何单元中,它们是 Pascal 语言的一个功能(自诞生以来一直如此)并深入编译器中。

您甚至无法重新定义它们,因为它们是保留字。

You cannot put a warning on core language features like with, label and goto no matter how much you hate them.
The fact that these are reserved words should have tipped you off on this.

These features are not in any unit, they are a feature of the Pascal language (and have been since its beginning) and build deep into the compiler.

You can't even redefine them because they are reserved words.

原来是傀儡 2024-12-15 00:23:28

首先,出于其他人所说的所有原因,我不建议您走您想要走的路线。但是,如果您觉得必须这样做,那么这里有一个方法可以完成您所要求的一些操作。

您可以为项目中的平台特定代码打开警告。至于其他你认为已弃用但实际上并未被 Delphi 弃用的东西,我有一个需要一些工作的解决方案。

创建一个单元来重新声明您想要弃用的类,如下所示:

unit PleaseDeprecateThisStuff;

uses
  Registry;

interface

type
  TRegistry = class (Registry.TRegistry)
  end deprecated;

implementation

end;

EDIT2:我觉得我应该进一步解释源代码。对于每个类,例如 TRegistry 示例,您需要在此单元中包含它的同名版本,并使其与另一个单元中的版本相同。确保将该单元包含在该单元的 use 子句中(废话),并在类型部分中添加类引用前缀,如 Registry.TRegistry 的示例。

包含您想要弃用的所有内容,然后将此单元包含在您想要“保护”的每个单元的接口 uses 子句的末尾处。

编辑:这个技巧只会对类、变量和常量有帮助......而不是保留字。

让我再次声明这是一个可怕的想法。 :p

First, I do NOT advise you go the route you want to go in, for all the reasons others have stated. BUT, if you feel you must, then here is a method that will do some of what you are asking for.

You can turn on warnings for platform specific code in your project. As for other things that you consider deprecated but are not actually deprecated by Delphi, I have a solution that will take some work.

Create a unit that redeclares the classes that you want deprecated, like the following:

unit PleaseDeprecateThisStuff;

uses
  Registry;

interface

type
  TRegistry = class (Registry.TRegistry)
  end deprecated;

implementation

end;

EDIT2: I feel I should explain the source further. For each class, like the TRegistry example, you'll need to include a same-name version of it in this unit and make it identical to the version in another unit. Make sure you include that unit in the uses clause of this unit (duh) AND prefix the class reference in the type section, like the example of Registry.TRegistry.

Include everything you want deprecated and then include this unit at the end of the interface uses clause of EVERY unit you want to "protect".

EDIT: This trick will only help with classes, variables, and constants... not reserved words.

Again, let me state this is a horrible idea. :p

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