Bug 48138 - __attribute__((aligned)) should give an error when applied to a typedef or template parameter, at least in C++0x mode.
Summary: __attribute__((aligned)) should give an error when applied to a typedef or te...
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.6.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-03-15 18:08 UTC by Jeffrey Yasskin
Modified: 2024-04-03 23:50 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2011-06-20 02:20:52


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jeffrey Yasskin 2011-03-15 18:08:39 UTC
In the C++0x draft, [dcl.align] says:

"An alignment-specifier may be applied to a variable or to a class
data member, but it shall not be applied to a bit-field, a function
parameter, the formal parameter of a catch clause (15.3), or a
variable declared with the register storage class specifier.  An
alignment-specifier may also be applied to the declaration of a class
or enumeration type."

This does not allow its use on a typedef or next to a typename template parameter.  It might make sense for gcc to support that as an extension, but gcc's current behavior is not what people expect that extension to do:

$ cat test.cc
#include <iostream>

#define ALIGNED(x) __attribute__((aligned(x)))

struct Char15 {
  char x[15];
}  ALIGNED(8);

template<typename T>
void print_type_alignment(const T&) {
  struct { char c; T t; } s;
  std::cout << sizeof(T) << ' ' << ((char*)&s.t - (char*)&s.c) << '\n';
}

int main() {
  typedef char unaligned[15];
  typedef char aligned[15] ALIGNED(8);
  unaligned x[10];
  aligned y[10];
  Char15 c15[10];
  std::cout << sizeof(unaligned) << ' ' << sizeof(x) << '\n';
  std::cout << sizeof(aligned) << ' ' << sizeof(y) << '\n';
  std::cout << sizeof(Char15) << ' ' << sizeof(c15) << '\n';
  
  aligned z ALIGNED(8);
  print_type_alignment<unaligned ALIGNED(8)>(z);
}

$ g++-mp-4.6 -std=gnu++0x test.cc && ./a.out
15 150
15 152
16 160
15 1


Note that the alignment on the typedef applies to the final variable, not the defined type, which means that interior members of arrays of the defined type have an unexpected alignment. This has been reported several times before (PR43798, PR47557, PR12742, PR42098), but the core problem seems to be that alignments on typedefs aren't supported.

__attribute__((aligned)) on template arguments seems to have no effect at all.





$ g++-mp-4.6 -v
Using built-in specs.
COLLECT_GCC=g++-mp-4.6
COLLECT_LTO_WRAPPER=/opt/local/libexec/gcc/x86_64-apple-darwin10/4.6.0/lto-wrapper
Target: x86_64-apple-darwin10
Configured with: ../gcc-4.6-20110305/configure --prefix=/opt/local --build=x86_64-apple-darwin10 --enable-languages=c,c++,objc,obj-c++ --libdir=/opt/local/lib/gcc46 --includedir=/opt/local/include/gcc46 --infodir=/opt/local/share/info --mandir=/opt/local/share/man --datarootdir=/opt/local/share/gcc-4.6 --with-local-prefix=/opt/local --with-system-zlib --disable-nls --program-suffix=-mp-4.6 --with-gxx-include-dir=/opt/local/include/gcc46/c++/ --with-gmp=/opt/local --with-mpfr=/opt/local --with-mpc=/opt/local --enable-stage1-checking --disable-multilib --enable-fully-dynamic-string
Thread model: posix
gcc version 4.6.0 20110305 (experimental) (GCC)
Comment 1 Jason Merrill 2011-06-20 14:40:27 UTC
Author: jason
Date: Mon Jun 20 14:40:19 2011
New Revision: 175215

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=175215
Log:
	PR c++/48138
	* tree.c (strip_typedefs): Use build_aligned_type.

Added:
    trunk/gcc/testsuite/g++.dg/ext/attr-aligned01.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/tree.c
    trunk/gcc/testsuite/ChangeLog
Comment 2 Jason Merrill 2011-06-21 02:24:12 UTC
Author: jason
Date: Tue Jun 21 02:24:09 2011
New Revision: 175236

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=175236
Log:
	PR c++/48138
	* pt.c (canonicalize_type_argument): New.
	(convert_template_argument, unify): Use it.

Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/pt.c
    trunk/gcc/testsuite/g++.dg/ext/attr-aligned01.C
Comment 3 Jason Merrill 2011-06-21 02:25:34 UTC
We now warn about ignored attributes on type arguments to class templates, but not yet for functions.  Unassigning for now.