Improve packed record layout support with -fdump-ada-spec
authorEric Botcazou <ebotcazou@adacore.com>
Thu, 1 Jul 2021 16:06:46 +0000 (18:06 +0200)
committerEric Botcazou <ebotcazou@adacore.com>
Thu, 1 Jul 2021 16:13:12 +0000 (18:13 +0200)
We cannot fully support packed record layout in -fdump-ada-spec, as packing
in C and Ada does not behave the same, so we issue a warning.  But simple
cases are OK and can actually be handled without much work.

gcc/c-family/
* c-ada-spec.c (packed_layout): New global variable.
(dump_ada_declaration): Set it upon seeing a packed record type.
Do not put the "aliased" keyword if it is set.
(dump_ada_structure): Add Pack aspect if it is set and clear it.
gcc/testsuite/
* c-c++-common/dump-ada-spec-14.c: Adjust dg-warning directive.

gcc/c-family/c-ada-spec.c
gcc/testsuite/c-c++-common/dump-ada-spec-14.c

index ea52be6..827bcc7 100644 (file)
@@ -2038,6 +2038,7 @@ is_float128 (tree node)
 }
 
 static bool bitfield_used = false;
+static bool packed_layout = false;
 
 /* Recursively dump in BUFFER Ada declarations corresponding to NODE of type
    TYPE.  SPC is the indentation level.  LIMITED_ACCESS indicates whether NODE
@@ -2851,14 +2852,14 @@ dump_ada_declaration (pretty_printer *buffer, tree t, tree type, int spc)
                return 1;
              }
 
-           /* ??? Packed record layout is not supported.  */
+           /* Packed record layout is not fully supported.  */
            if (TYPE_PACKED (TREE_TYPE (t)))
              {
-               warning_at (DECL_SOURCE_LOCATION (t), 0,
-                           "unsupported record layout");
+               warning_at (DECL_SOURCE_LOCATION (t), 0, "packed layout");
                pp_string (buffer, "pragma Compile_Time_Warning (True, ");
-               pp_string (buffer, "\"probably incorrect record layout\");");
+               pp_string (buffer, "\"packed layout may be incorrect\");");
                newline_and_indent (buffer, spc);
+               packed_layout = true;
              }
 
            if (orig && TYPE_NAME (orig))
@@ -2951,7 +2952,8 @@ dump_ada_declaration (pretty_printer *buffer, tree t, tree type, int spc)
 
          pp_string (buffer, " : ");
 
-         if (TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != POINTER_TYPE)
+         if (TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != POINTER_TYPE
+             && !packed_layout)
            pp_string (buffer, "aliased ");
 
          if (TYPE_NAME (TREE_TYPE (t)))
@@ -3185,7 +3187,8 @@ dump_ada_declaration (pretty_printer *buffer, tree t, tree type, int spc)
          if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
              && (TYPE_NAME (TREE_TYPE (t))
                  || (TREE_CODE (TREE_TYPE (t)) != INTEGER_TYPE
-                     && TREE_CODE (TREE_TYPE (t)) != ENUMERAL_TYPE)))
+                     && TREE_CODE (TREE_TYPE (t)) != ENUMERAL_TYPE))
+             && !packed_layout)
            pp_string (buffer, "aliased ");
 
          if (TREE_READONLY (t) && TREE_CODE (t) != FIELD_DECL)
@@ -3352,7 +3355,7 @@ dump_ada_structure (pretty_printer *buffer, tree node, tree type, bool nested,
       pp_string (buffer, "Unchecked_Union => True");
     }
 
-  if (bitfield_used)
+  if (bitfield_used || packed_layout)
     {
       char buf[32];
       pp_comma (buffer);
@@ -3363,6 +3366,7 @@ dump_ada_structure (pretty_printer *buffer, tree node, tree type, bool nested,
       sprintf (buf, "Alignment => %d", TYPE_ALIGN (node) / BITS_PER_UNIT);
       pp_string (buffer, buf);
       bitfield_used = false;
+      packed_layout = false;
     }
 
   if (nested)
index bfdec61..291eea8 100644 (file)
@@ -1,7 +1,7 @@
 /* { dg-do compile } */
 /* { dg-options "-fdump-ada-spec" } */
 
-struct __attribute__((packed)) S /* { dg-warning "unsupported record layout" } */
+struct __attribute__((packed)) S /* { dg-warning "packed layout" } */
 {
   char c;
   int  t;