首页 > 解决方案 > Ada 标签比较导致编译器崩溃

问题描述

我不确定这是否是我的 gcc (4.8.5) 或 gprbuild (2.2.0) 版本中的错误,但是当我尝试编译项目时,有一个特定的函数,其主体导致编译器以STORAGE_ERROR. 当我构建时,-cargs -v我看到这gnatl -quiet ...是崩溃前最近输出的命令。

有问题的函数从属于特定类型层次结构的标记生成信息字符串。它看起来像:

function Tag_To_String (From : Ada.Tags.Tag) return String is (
    if From = A'Tag then "This is tag A"
    elsif From = B'Tag then "This is tag B"
    --  ...
    elsif From = Z'Tag then "This is tag Z"
    else "");

而如果我将该主体更改为:

function Tag_To_String (From : Ada.Tags.Tag) return String is ("");

我得到的错误正是:

gcc -c -gnat12 sourcefile.adb

raised STORAGE_ERROR : stack overflow or erroneous memory access
gprbuild: *** compilation phase failed

知道为什么 gcc 似乎无法编译这个特定的函数吗?

标签: gccsegmentation-faultadagnat

解决方案


以下程序使用 2018 版 GNAT 和 GPS 可以正常工作。

with Ada.text_IO; use Ada.Text_IO;
with Ada.Tags; use Ada.Tags;

procedure Tag_Main is
   package foo is
      type A is tagged private;
      type B is tagged private;
   private
      type A is tagged null record;
      type B is tagged null record;
   end foo;

   use Foo;

   function Tag_To_String(From : Ada.Tags.Tag) return String is(
      if From = A'Tag then
         "This is tag A"
      else
         "This is tag B"
      );

begin
   Put_Line(Tag_To_String(A'Tag));
   Put_Line(Tag_To_String(B'Tag));
end Tag_Main;

请注意,我已编辑代码以使用函数表达式。它仍然适用于 GNAT/GPS 2018 版本。这个版本是用 gprbuild -d -PD 编译的


推荐阅读