如何在 gnatcheck 报告中添加 -Wuninitialized?

How to add -Wuninitialized in a gnatcheck report?

我想在我的 gnatcheck 报告 中包含未初始化的变量,但此警告的格式与以下格式不兼容:

+RWarnings:xxxx(带有 xxxx 不同的警告开关)

我曾尝试像其他人一样编写此编程规则:+RWuninitialized 但它不起作用。

并且 gnatcheck 的开关 -Wuninitialized 不存在。

用于将编译器检查结果添加到 Gnatcheck 输出的 documentation 表示警告(经过一些编辑)

To record compiler warnings (see Warning Message Control section in GNAT User’s Guide), use the Warnings rule with a parameter that is a valid static_string_expression argument of the GNAT pragma Warnings (see “Pragma Warnings” in the GNAT Reference Manual). Note that [the]in case of gnatcheck s parameter, that corresponds to the GNAT -gnatws option, disables all the specific warnings, but [does] not suppresses the warning mode, and [the] e parameter, corresponding to -gnatwe that means "treat warnings as errors", does not have any effect.

如果你去查找 GNAT Reference Manual 中的“pragma Warnings”,你会发现它会将你发送给编译器:

The string is a list of letters specifying which warnings are to be activated and which deactivated. The code for these letters is the same as the string used in the command line switch controlling warnings [-gnatw]. For a brief summary, use the gnatmake command with no arguments, which will generate usage information containing the list of warnings switches supported.

按照这个建议,好像没有-gnatwx给你-Wuninitialized的效果了。但是,如果您打开所有警告

project Checks is
   for Source_Files use ("checks.adb");
   package Check is
   for Default_Switches ("ada") use
     (
      "-rules",
      "+RWarnings:.e"
     );
   end Check;
end Checks;

和运行它在

procedure Checks (Input : Integer; Result : out Integer) is
   X : Integer;
   Y : Integer;
   Z : Integer;
begin
   if (Y > 0) = True then
      Result := X;
   end if;
end Checks;

你得到

checks.adb:1:19: warning: formal parameter "Input" is not referenced
checks.adb:2:04: warning: variable "X" is read but never assigned
checks.adb:3:04: warning: variable "Y" is read but never assigned
checks.adb:4:04: warning: variable "Z" is never read and never assigned
checks.adb:6:15: warning: comparison with True is redundant

我认为第 2、3(和 4)行的警告与“未初始化”的意思相同。

然后您可以关闭不需要的警告;例如,"+RWarnings:.eF” 将“关闭未引用正式的警告”,并抑制第 1 行的警告。