Source: https://bugs.webkit.org/show_bug.cgi?id=38045

Given the following code:

$ cat test.cpp
struct Foo
{
    char __attribute__((aligned(4))) c[sizeof(int)];
};

char junk;
Foo f;

int main()
{
    int *i = reinterpret_cast<int *>(&f.c);
    *i = 0;
}

When compiled for ARM produces the warning:
$ arm-none-linux-gnueabi-g++ -Wcast-align -O3 -fsyntax-only /tmp/test.cpp
test.cpp:11: warning: cast from 'char (*)[4]' to 'int*' increases required
alignment of target type

Note that we requested that Foo::c be aligned to 4 bytes, which is the required
alignment for int. The assembly dump of the build confirms that the alignment
was honoured:

junk:
        .space  1
        .space  3
        .type   f, %object
        .size   f, 4
f:
        .space  4

According to Dirk Müller, the following code is at fault (I have no idea where
it's from):
      /* Warn about possible alignment problems.  */
      if (STRICT_ALIGNMENT && warn_cast_align
          && (complain & tf_warning)
·         && !VOID_TYPE_P (type)
·         && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
·         && COMPLETE_TYPE_P (TREE_TYPE (type))
·         && COMPLETE_TYPE_P (TREE_TYPE (intype))
·         && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
·       warning (OPT_Wcast_align, "cast from %qT to %qT "
                 "increases required alignment of target type", intype, type);

As it only verifies the aligment of the type in question (char*), not of the
variable in question (&f.c).


-- 
           Summary: warning about increased alignment during casting printed
                    even though variable is properly aligned
           Product: gcc
           Version: 4.4.1
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: thiago at kde dot org
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: arm-none-linux-gnueabi


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43976

Reply via email to