public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/43976]  New: warning about increased alignment during casting printed even though variable is properly aligned
@ 2010-05-03 14:40 thiago at kde dot org
  2010-05-04 17:27 ` [Bug middle-end/43976] " pinskia at gcc dot gnu dot org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: thiago at kde dot org @ 2010-05-03 14:40 UTC (permalink / raw)
  To: gcc-bugs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 2039 bytes --]

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug middle-end/43976] warning about increased alignment during casting printed even though variable is properly aligned
  2010-05-03 14:40 [Bug c++/43976] New: warning about increased alignment during casting printed even though variable is properly aligned thiago at kde dot org
@ 2010-05-04 17:27 ` pinskia at gcc dot gnu dot org
  2010-05-04 19:40 ` thiago at kde dot org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2010-05-04 17:27 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2010-05-04 17:27 -------
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.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|c++                         |middle-end


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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug middle-end/43976] warning about increased alignment during casting printed even though variable is properly aligned
  2010-05-03 14:40 [Bug c++/43976] New: warning about increased alignment during casting printed even though variable is properly aligned thiago at kde dot org
  2010-05-04 17:27 ` [Bug middle-end/43976] " pinskia at gcc dot gnu dot org
@ 2010-05-04 19:40 ` thiago at kde dot org
  2010-05-05 19:18 ` hp at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: thiago at kde dot org @ 2010-05-04 19:40 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from thiago at kde dot org  2010-05-04 19:40 -------
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.


-- 


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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug middle-end/43976] warning about increased alignment during casting printed even though variable is properly aligned
  2010-05-03 14:40 [Bug c++/43976] New: warning about increased alignment during casting printed even though variable is properly aligned thiago at kde dot org
  2010-05-04 17:27 ` [Bug middle-end/43976] " pinskia at gcc dot gnu dot org
  2010-05-04 19:40 ` thiago at kde dot org
@ 2010-05-05 19:18 ` hp at gcc dot gnu dot org
  2010-05-05 19:40 ` thiago at kde dot org
  2010-09-08 10:02 ` ibolton at gcc dot gnu dot org
  4 siblings, 0 replies; 6+ messages in thread
From: hp at gcc dot gnu dot org @ 2010-05-05 19:18 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from hp at gcc dot gnu dot org  2010-05-05 19:17 -------
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).


-- 


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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug middle-end/43976] warning about increased alignment during casting printed even though variable is properly aligned
  2010-05-03 14:40 [Bug c++/43976] New: warning about increased alignment during casting printed even though variable is properly aligned thiago at kde dot org
                   ` (2 preceding siblings ...)
  2010-05-05 19:18 ` hp at gcc dot gnu dot org
@ 2010-05-05 19:40 ` thiago at kde dot org
  2010-09-08 10:02 ` ibolton at gcc dot gnu dot org
  4 siblings, 0 replies; 6+ messages in thread
From: thiago at kde dot org @ 2010-05-05 19:40 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from thiago at kde dot org  2010-05-05 19:39 -------
(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]


-- 


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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug middle-end/43976] warning about increased alignment during casting printed even though variable is properly aligned
  2010-05-03 14:40 [Bug c++/43976] New: warning about increased alignment during casting printed even though variable is properly aligned thiago at kde dot org
                   ` (3 preceding siblings ...)
  2010-05-05 19:40 ` thiago at kde dot org
@ 2010-09-08 10:02 ` ibolton at gcc dot gnu dot org
  4 siblings, 0 replies; 6+ messages in thread
From: ibolton at gcc dot gnu dot org @ 2010-09-08 10:02 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from ibolton at gcc dot gnu dot org  2010-09-08 10:02 -------
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.


-- 

ibolton at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|0                           |1
      Known to fail|                            |4.4.5 4.5.2 4.6.0
   Last reconfirmed|0000-00-00 00:00:00         |2010-09-08 10:02:04
               date|                            |


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


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2010-09-08 10:02 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-05-03 14:40 [Bug c++/43976] New: warning about increased alignment during casting printed even though variable is properly aligned thiago at kde dot org
2010-05-04 17:27 ` [Bug middle-end/43976] " pinskia at gcc dot gnu dot org
2010-05-04 19:40 ` thiago at kde dot org
2010-05-05 19:18 ` hp at gcc dot gnu dot org
2010-05-05 19:40 ` thiago at kde dot org
2010-09-08 10:02 ` ibolton at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).