了解 {$WARN XXXX OFF} 编译器指令

Understanding the {$WARN XXXX OFF} compiler directive

如果我试图不加区别地使编译器消息静音,编译器命令行参数 -vmXXXX 对整个项目都非常适用。但是,如果我试图在源代码中有选择地使消息静音,我必须使用 {$WARN XXXX OFF} 编译器指令。

问题是...我很难理解该编译器指令的应用规则。在下面的 1 个程序和 2 个单元的示例中,如果您一次取消注释 {$WARN XXXX OFF} 命令,将会发生的情况不是我所期望的(结果写在每个编译器指令的注释中)。

是否有人可以准确解释该编译器指令的工作原理?

计划:

program Project1;
//{$WARN 4055 OFF} // will silence the unit2 hint but not the unit1 hint
uses
 Unit1;
//{$WARN 4055 OFF} // will silence neither the unit2 hint nor the unit1 hint
begin
 if convert_it(0)=nil then
  halt(1);
end.

第一单元:

unit Unit1;
//{$WARN 4055 OFF} // will silence the unit2 hint but not the unit1 hint
interface
//{$WARN 4055 OFF} // will silence the unit2 hint but not the unit1 hint
function convert_it(avalue:longint):pointer;
//{$WARN 4055 OFF} // will silence the unit2 hint but not the unit1 hint
implementation
//{$WARN 4055 OFF} // will silence the unit2 hint but not the unit1 hint
uses
 unit2;
//{$WARN 4055 OFF} // will silence the unit1 hint but not the unit2 hint
function convert_it(avalue:longint):pointer;
begin
 result:=pointer(do_it(avalue));
end;

end.

第二单元:

unit Unit2;
//{$WARN 4055 OFF} // will silence the unit2 hint but not the unit1 hint
interface
//{$WARN 4055 OFF} // will silence the unit2 hint but not the unit1 hint
function do_it(avalue:longint):longint;
//{$WARN 4055 OFF} // will silence the unit2 hint but not the unit1 hint
implementation
//{$WARN 4055 OFF} // will silence the unit2 hint but not the unit1 hint
function do_it(avalue:longint):longint;
var
 p:pointer;
begin
 p:=pointer(avalue);
 if p=nil then
  result:=avalue+1
 else
  result:=avalue-1;
end;

end.

来自manual

Local directives can occur more than once in a unit or program, If they have a command line counterpart, the command line argument is restored as the default for each compiled file. The local directives influence the compiler’s behaviour from the moment they’re encountered until the moment another switch annihilates their behaviour, or the end of the current unit or program is reached.

因此您使用的指令从它们的声明到 a 单元结束有效,在 use 子句之后,在恢复默认值。

对于每个单元,对于每个第 n 个指令

  • 程序
    • 1:指令被激活,在unit1中仍然有效,因为use,在unit2中仍然有效因为使用。但是在 unit2(一个单元结束)之后默认值被恢复,所以你在 unit1.
    • 中得到了消息
    • 2:指令在使用后激活,所以不管单位是什么都没有效果。
  • Unit1
    • 1:该指令已激活,由于 use,在 unit2 中仍然有效。但是在 unit2(一个单元结束)之后默认值被恢复,所以你在 unit1.
    • 中得到了消息
    • 2:同上
    • 3:同上
    • 4:同上
    • 5: 该指令仅在 unit1 实现部分激活。
  • Unit2,所有情况:指令仅针对 unit2.
  • 激活

到达此处的棘手之处在于它遵循 use.