views:

247

answers:

1

I'm porting some code from one framework library to another, which requires me to comment out large sections of code just to get it to compile, and then carefully restore them. In the meantime, I get tons of warnings because of all the commented-out code. So I decided to write a quick include file to suppress them:

{$MESSAGE WARN 'Warnings suppressed for unported unit with commented-out code'}
{$WARN NO_RETVAL OFF}
{$WARN USE_BEFORE_DEF OFF}

I included this in all the units that are giving me tons of warnings, but now when I compile, the compiler's returning the message as follows:

[DCC Warning] unfinished.inc(1): W1054 Warnings suppressed for unported unit with commented-out code

I'd like it to tell me that the warning is coming from the unit doing the including, not the include file itself. Is there any way to do that, or to work the name of the unit into the message, without actually moving the $MESSAGE directive out of the include file and into every single unit where I did this?

+13  A: 

Here is a trick, which uses the fact that the compiler hint for "unused private methods" contains the name of the unit, the class is declared in, even if the class with the private method is declared in an include file.

This requires that the include-directive for your include file appears in a part of the unit, where declarations are allowed, so either after the uses clause in the interface section or after the uses clause of the implementation section.

interface

uses Foo, Bar, Socks;

{$include Unfinished.inc}

Your include file should look like this:

{$MESSAGE WARN 'Warnings suppressed for unported unit with commented-out code, Unit name see next line'}
type
  TUnit_With_Suppressed_Warnings = class
  private
    procedure UnitWithSuppressedWarnings; virtual; abstract;
  end;

{$WARN NO_RETVAL OFF}
{$WARN USE_BEFORE_DEF OFF}

Now, you will get the following two messages for every unit that includes that include file:

[DCC Warning] unfinished.inc(1): W1054 Warnings suppressed for unported unit with commented-out code, Unit name see next line
[DCC Hint] actualunitname.pas(5): The private symbol UnitWithSuppressedWarnings was declared but never used.

Notice that the name of the actual unit appears in the second line.

Remark: You will not see the second line, if compiler hints are switched off.

NineBerry
I like it! It's a bit of a hack, but it manages to find a way to do what I thought couldn't be done.
Mason Wheeler
Genius, deleted my answer :)
Mihai Limbășan
Very creative indeed!
François