Bug 43976 - warning about increased alignment during casting printed even though variable is properly aligned
Summary: warning about increased alignment during casting printed even though variable...
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.4.1
: P3 minor
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-05-03 14:40 UTC by Thiago Macieira
Modified: 2010-09-08 10:02 UTC (History)
4 users (show)

See Also:
Host: i686-pc-linux-gnu
Target: arm-none-linux-gnueabi
Build:
Known to work:
Known to fail: 4.4.5 4.5.2 4.6.0
Last reconfirmed: 2010-09-08 10:02:04


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Thiago Macieira 2010-05-03 14:40:14 UTC
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).
Comment 1 Andrew Pinski 2010-05-04 17:27:16 UTC
I think the warning is correct as the resulting type of &f.c is a pointer to an array and that array is of type char[4] which has an alignment of 1. Yes f.c has an alignment of 4 but the array type has an alignment of 1.
Comment 2 Thiago Macieira 2010-05-04 19:40:00 UTC
Well, technically f.c has a type of 'char (__attribute__((aligned(4)))) [4]'.

Anyway, the point is that the variable is properly aligned, so the warning is superfluous.

Or, in other words, we need a way to tell GCC "I know this is properly aligned, so don't tell me there's a problem". The -Wcast-align warning is very useful to catch real mistakes, but there's no way to weed out the false positives.
Comment 3 Hans-Peter Nilsson 2010-05-05 19:17:13 UTC
The struct Foo looks like an odd way to try and express

union Foo
{
  int i;
  char c[sizeof (int)];
};

but that doesn't work either, for my 4.3.1 arm-linux build.
Neither does moving the attribute just before the ";" (either ones) or before "char" (the original or the above). Neither in C (changing reinterpret_cast to a C cast).
Comment 4 Thiago Macieira 2010-05-05 19:39:37 UTC
(In reply to comment #3)
> The struct Foo looks like an odd way to try and express
> 
> union Foo
> {
>   int i;
>   char c[sizeof (int)];
> };

Well, that is the objective, but you can't replace int with a non-POD type in C++98. C++0x relaxes the unions and that's exactly what is intended: delayed construction.

I used int as an example, but the objective is to have:

template <typename T>
struct Buffer
{
    char __attribute__((aligned(__alignof__(T)))) buf[sizeof(T)];
};

[the WebKit WTF::Vector code is a bit more complex than that to get around the fact that gcc 4.2 can't do aligned(__alignof__(T)), so it partially specialises alignments of 1, 2, 4, 8, 16, and 64]
Comment 5 Ian Bolton 2010-09-08 10:02:04 UTC
Confirmed on latest 4.4, 4.5 and 4.6 (trunk).

Related GCC documentation on alignment of structure fields is here:
http://gcc.gnu.org/onlinedocs/gcc-4.5.0/gcc/Variable-Attributes.html#Variable-Attributes

In the short-term, one workaround is to write the code as follows:

#include <stdio.h>

struct Foo
{
    char c[sizeof(int)];
} __attribute__((aligned(4)));

char junk;
Foo f;

int main()
{
    int *i = reinterpret_cast<int *>(&f);
    *i = 0x44434241;
    printf("%c %c %c %c", f.c[0], f.c[1], f.c[2], f.c[3]);
}

By aligning the structure Foo to 4 bytes, you can successfully cast a Foo* to an int* and then initialise all four chars in one go.  (Without the type attribute for the struct Foo, you still get the warning.)  My example prints "A B C D".

FYI: I have tracked down the alleged offending code mentioned in an earlier comment to build_c_cast() in c-typeck.c.