Tobias R
Tobias R

Reputation: 938

How can I generate a Warning when using certain RTL/VCL or Delphi language elements

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?

Upvotes: 3

Views: 227

Answers (4)

Jerry Gagnon
Jerry Gagnon

Reputation: 1161

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

Upvotes: 1

Rudy Velthuis
Rudy Velthuis

Reputation: 28806

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.

Upvotes: 2

Disillusioned
Disillusioned

Reputation: 14832

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.

Upvotes: 1

Johan
Johan

Reputation: 76547

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.

Upvotes: 1

Related Questions