https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111086

            Bug ID: 111086
           Summary: Possibly incorrect warning: arm-none-eabi-gcc:packed
                    attribute causes inefficient alignment for <field>
           Product: gcc
           Version: 9.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tonyjosinew at gmail dot com
  Target Milestone: ---

Toolchain and compilation info
------------------------------

The exact version of GCC, the system type:

arm-none-eabi-gcc (15:9-2019-q4-0ubuntu1) 9.2.1 20191025 (release)
[ARM/arm-9-branch revision 277599]


The options given when GCC was configured/built:

-c -Wpacked


The complete command line that triggers the bug:

arm-none-eabi-gcc test.c -c -Wpacked

The compiler output (error messages, warnings, etc.);

test.c:22:14: warning: packed attribute causes inefficient alignment for 'ch1'
[-Wattributes]
   22 |     uint32_t ch1;
      |              ^~~
test.c:23:14: warning: packed attribute causes inefficient alignment for 'ch2'
[-Wattributes]
   23 |     uint16_t ch2;
      |              ^~~

Description:
------------
When the following struct is compiled in the above listed compiler:

struct Struct_test 
{
    uint32_t ch1;
    uint16_t ch2;
    uint32_t ch3;
}
__attribute__( ( packed ) );

this "warning: packed attribute causes inefficient alignment for <struct field
name>" is triggered. 

As per the manual -Wpacked means: Warn when the packed attribute has no effect
on struct layout. But in this case the packed attribute does have an impact on
the struct layout as it removes the padding between ch2 and ch3, but still it
causes a warning. Though the warning is listed as associated with:
-Wattributes. It doesn't show up if -Wpacked is not given as a compile option.

Is this a valid warning? I'm not fully sure what is meant by the warning, from
the looks of it seems like the compiler is trying to warn that with the packed
attribute the fields that are aligned relative to the struct can be aligned
inefficiently as the alignment of the entire struct is now 1 because of
packing.

Reply via email to