Useful reference about disabling specific compiler warnings


Earlier today while working on an IDE plugin, I got the following compiler warnings:

[dcc32 Warning] W1029 Duplicate constructor ‘TLineDifference.CreateEqual’ with identical parameters will be inacessible [sic] from C++

[dcc32 Warning] W1029 Duplicate constructor ‘TLineDifference.CreateAdded’ with identical parameters will be inacessible [sic] from C++

This is caused by a record type having two or more constructors taking the same parameters.  (For interest, the type TLineDifference here represents a single difference in two text files, ie one of many calculated when diff-ing source code, for example.  The type of difference – a line being added, removed or equal – could be a parameter but when using the type in code, it is more readable and clear-in-intent for it to be part of the constructor name.)

In the past I’ve dealt with C++ compatibility warnings, in code that is not intended for C++ consumption, by making sure the “Project Options > Delphi Compiler > Output – C/C++ > C++ Output file generation” option was set to “DCUs only”.  But in this case, it didn’t have any effect.  And since the code in question is (a) internal to the BPL and (b) is intended for Delphi use only I wanted to disable the warning.

Enter this very useful blog post on Delphi’s Hidden Hints and Warnings.

For each warning code (eg W1029) there is a corresponding name you can use either on the command line or in a {$WARN} directive to enable or disable it, restore it to its default on/off state, or promote it to an error.  For example, using the name for W1029 gives:

{$WARN DUPLICATE_CTOR_DTOR OFF}

The page has a table of code to names – very useful – and some other miscellaneous information.

Some side notes:

  • You have to use the name not the warning code.  I would prefer to write something like {$WARN W1029 OFF} because it’s then self-documenting what warning it is that is being blocked, but neither this nor variations of this syntax work.  You must use the name not code.
  • This specific warning, W1029, has to be turned off in the project file not in the unit that generates the warning (in fact, the warning doesn’t specific a file or line number.)
  • The blog has a number of other interesting articles on Delphi topics, such as using generics on enumerated types which do not have RTTI.
  • In a ‘it’s a small world’ coincidence, while writing this from Estonia, I believe I knew the blog author Marc ten or more years ago as a friend-of-a-friend when I lived in the same town in Australia.  I don’t think either of us knew the other used Delphi.  It is a small world indeed.

Discussions about this page on Google+