Hi! The following patch on top of the PR110345 P2552R3 series: https://gcc.gnu.org/pipermail/gcc-patches/2024-August/661904.html https://gcc.gnu.org/pipermail/gcc-patches/2024-August/661905.html https://gcc.gnu.org/pipermail/gcc-patches/2024-August/661906.html https://gcc.gnu.org/pipermail/gcc-patches/2024-September/662330.html https://gcc.gnu.org/pipermail/gcc-patches/2024-September/662331.html https://gcc.gnu.org/pipermail/gcc-patches/2024-September/662333.html https://gcc.gnu.org/pipermail/gcc-patches/2024-September/662334.html https://gcc.gnu.org/pipermail/gcc-patches/2024-September/662336.html https://gcc.gnu.org/pipermail/gcc-patches/2024-September/662379.html https://gcc.gnu.org/pipermail/gcc-patches/2024-September/662380.html https://gcc.gnu.org/pipermail/gcc-patches/2024-September/662381.html emits pedantic pedwarns for alignas appertaining to incorrect entities. As the middle-end and attribute exclusions look for "aligned" attribute, the patch transforms alignas into "internal "::aligned attribute (didn't use [[aligned (x)]] so that people can't type it that way).
Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-09-06 Jakub Jelinek <ja...@redhat.com> PR c++/110345 gcc/c-family/ * c-common.h (attr_aligned_exclusions): Declare. (handle_aligned_attribute): Likewise. * c-attribs.cc (handle_aligned_attribute): No longer static. (attr_aligned_exclusions): Use extern instead of static. gcc/cp/ * cp-tree.h (enum cp_tree_index): Add CPTI_INTERNAL_IDENTIFIER. (internal_identifier): Define. (internal_attribute_table): Declare. * parser.cc (cp_parser_exception_declaration): Error on alignas on exception declaration. (cp_parser_std_attribute_spec): Turn alignas into internal ns aligned attribute rather than gnu. * decl.cc (initialize_predefined_identifiers): Initialize internal_identifier. * tree.cc (handle_alignas_attribute): New function. (internal_attributes): New variable. (internal_attribute_table): Likewise. * cp-objcp-common.h (cp_objcp_attribute_table): Add internal_attribute_table entry. gcc/testsuite/ * g++.dg/cpp0x/alignas1.C: Add dg-options "". * g++.dg/cpp0x/alignas2.C: Likewise. * g++.dg/cpp0x/alignas7.C: Likewise. * g++.dg/cpp0x/alignas21.C: New test. * g++.dg/ext/bitfield9.C: Expect a warning. * g++.dg/cpp2a/is-layout-compatible3.C: Add dg-options -pedantic. Expect a warning. --- gcc/c-family/c-common.h.jj 2024-09-06 13:43:37.311307920 +0200 +++ gcc/c-family/c-common.h 2024-09-06 15:33:47.497616169 +0200 @@ -1645,8 +1645,10 @@ extern int parse_tm_stmt_attr (tree, int extern int tm_attr_to_mask (tree); extern tree tm_mask_to_attr (int); extern tree find_tm_attribute (tree); +extern const struct attribute_spec::exclusions attr_aligned_exclusions[]; extern const struct attribute_spec::exclusions attr_cold_hot_exclusions[]; extern const struct attribute_spec::exclusions attr_noreturn_exclusions[]; +extern tree handle_aligned_attribute (tree *, tree, tree, int, bool *); extern tree handle_noreturn_attribute (tree *, tree, tree, int, bool *); extern tree handle_musttail_attribute (tree *, tree, tree, int, bool *); extern bool has_attribute (location_t, tree, tree, tree (*)(tree)); --- gcc/c-family/c-attribs.cc.jj 2024-09-06 13:43:37.300308064 +0200 +++ gcc/c-family/c-attribs.cc 2024-09-06 16:00:55.864465359 +0200 @@ -100,7 +100,6 @@ static tree handle_destructor_attribute static tree handle_mode_attribute (tree *, tree, tree, int, bool *); static tree handle_section_attribute (tree *, tree, tree, int, bool *); static tree handle_special_var_sec_attribute (tree *, tree, tree, int, bool *); -static tree handle_aligned_attribute (tree *, tree, tree, int, bool *); static tree handle_warn_if_not_aligned_attribute (tree *, tree, tree, int, bool *); static tree handle_strict_flex_array_attribute (tree *, tree, tree, @@ -192,7 +191,7 @@ static tree handle_null_terminated_strin { name, function, type, variable } /* Define attributes that are mutually exclusive with one another. */ -static const struct attribute_spec::exclusions attr_aligned_exclusions[] = +extern const struct attribute_spec::exclusions attr_aligned_exclusions[] = { /* Attribute name exclusion applies to: function, type, variable */ @@ -2806,7 +2805,7 @@ common_handle_aligned_attribute (tree *n /* Handle a "aligned" attribute; arguments as in struct attribute_spec.handler. */ -static tree +tree handle_aligned_attribute (tree *node, tree name, tree args, int flags, bool *no_add_attrs) { --- gcc/cp/cp-tree.h.jj 2024-09-06 13:58:09.428940803 +0200 +++ gcc/cp/cp-tree.h 2024-09-06 15:51:23.012904756 +0200 @@ -192,6 +192,7 @@ enum cp_tree_index CPTI_HEAP_VEC_UNINIT_IDENTIFIER, CPTI_HEAP_VEC_IDENTIFIER, CPTI_OMP_IDENTIFIER, + CPTI_INTERNAL_IDENTIFIER, CPTI_LANG_NAME_C, CPTI_LANG_NAME_CPLUSPLUS, @@ -345,6 +346,7 @@ extern GTY(()) tree cp_global_trees[CPTI #define heap_vec_uninit_identifier cp_global_trees[CPTI_HEAP_VEC_UNINIT_IDENTIFIER] #define heap_vec_identifier cp_global_trees[CPTI_HEAP_VEC_IDENTIFIER] #define omp_identifier cp_global_trees[CPTI_OMP_IDENTIFIER] +#define internal_identifier cp_global_trees[CPTI_INTERNAL_IDENTIFIER] #define lang_name_c cp_global_trees[CPTI_LANG_NAME_C] #define lang_name_cplusplus cp_global_trees[CPTI_LANG_NAME_CPLUSPLUS] @@ -8132,6 +8134,7 @@ extern bool is_byte_access_type (tree) extern bool is_byte_access_type_not_plain_char (tree); extern const struct scoped_attribute_specs cxx_gnu_attribute_table; extern const struct scoped_attribute_specs std_attribute_table; +extern const struct scoped_attribute_specs internal_attribute_table; extern tree make_ptrmem_cst (tree, tree); extern tree cp_build_type_attribute_variant (tree, tree); extern tree cp_build_reference_type (tree, bool); --- gcc/cp/parser.cc.jj 2024-09-06 14:29:48.392322139 +0200 +++ gcc/cp/parser.cc 2024-09-06 17:10:18.507455975 +0200 @@ -29681,7 +29681,15 @@ cp_parser_exception_declaration (cp_pars tree decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, &type_specifiers.attributes); if (decl != error_mark_node && type_specifiers.attributes) - cplus_decl_attributes (&decl, type_specifiers.attributes, 0); + { + cplus_decl_attributes (&decl, type_specifiers.attributes, 0); + if (pedantic && lookup_attribute ("internal ", "aligned", + DECL_ATTRIBUTES (decl))) + pedwarn (type_specifiers.locations[ds_attribute] + ? type_specifiers.locations[ds_attribute] + : input_location, OPT_Wpedantic, + "%<alignas%> on exception declaration"); + } return decl; } @@ -31131,7 +31139,7 @@ cp_parser_std_attribute_spec (cp_parser /* Build the C++-11 representation of an 'aligned' attribute. */ attributes - = build_tree_list (build_tree_list (gnu_identifier, + = build_tree_list (build_tree_list (internal_identifier, aligned_identifier), alignas_expr); } --- gcc/cp/decl.cc.jj 2024-09-06 14:29:32.619526092 +0200 +++ gcc/cp/decl.cc 2024-09-06 15:51:53.437509531 +0200 @@ -4812,6 +4812,7 @@ initialize_predefined_identifiers (void) {"heap [] uninit", &heap_vec_uninit_identifier, cik_normal}, {"heap []", &heap_vec_identifier, cik_normal}, {"omp", &omp_identifier, cik_normal}, + {"internal ", &internal_identifier, cik_normal}, {NULL, NULL, cik_normal} }; --- gcc/cp/tree.cc.jj 2024-09-06 14:30:05.278103789 +0200 +++ gcc/cp/tree.cc 2024-09-06 17:16:47.490434844 +0200 @@ -5127,6 +5127,35 @@ handle_maybe_unused_attribute (tree *nod return ret; } +/* The C++11 alignment specifier. It mostly maps to GNU aligned attribute, + but we need to do some extra pedantic checking. */ + +static tree +handle_alignas_attribute (tree *node, tree name, tree args, int flags, + bool *no_add_attrs) +{ + tree t = *node; + tree ret = handle_aligned_attribute (node, name, args, flags, no_add_attrs); + if (pedantic) + { + if (TREE_CODE (*node) == FUNCTION_DECL) + pedwarn (input_location, OPT_Wattributes, + "%<alignas%> on function declaration"); + else if (TREE_CODE (*node) == ENUMERAL_TYPE) + pedwarn (input_location, OPT_Wattributes, + "%<alignas%> on enumerated type"); + else if (TYPE_P (*node) && t != *node) + pedwarn (input_location, OPT_Wattributes, + "%<alignas%> on a type other than class"); + else if (TREE_CODE (*node) == FIELD_DECL && DECL_C_BIT_FIELD (*node)) + pedwarn (input_location, OPT_Wattributes, "%<alignas%> on bit-field"); + else if (TREE_CODE (t) == TYPE_DECL) + pedwarn (input_location, OPT_Wattributes, + "%<alignas%> on a type alias"); + } + return ret; +} + /* Table of valid C++ attributes. */ static const attribute_spec cxx_gnu_attributes[] = { @@ -5177,6 +5206,18 @@ const scoped_attribute_specs std_attribu nullptr, { std_attributes } }; +/* Table of internal attributes. */ +static const attribute_spec internal_attributes[] = +{ + { "aligned", 0, 1, false, false, false, false, + handle_alignas_attribute, attr_aligned_exclusions } +}; + +const scoped_attribute_specs internal_attribute_table = +{ + "internal ", { internal_attributes } +}; + /* Handle an "init_priority" attribute; arguments as in struct attribute_spec.handler. */ static tree --- gcc/cp/cp-objcp-common.h.jj 2024-09-06 13:43:37.712302692 +0200 +++ gcc/cp/cp-objcp-common.h 2024-09-06 15:47:33.425887161 +0200 @@ -129,7 +129,8 @@ static const scoped_attribute_specs *con &cxx_gnu_attribute_table, &c_common_gnu_attribute_table, &c_common_clang_attribute_table, - &c_common_format_attribute_table + &c_common_format_attribute_table, + &internal_attribute_table }; #undef LANG_HOOKS_ATTRIBUTE_TABLE --- gcc/testsuite/g++.dg/cpp0x/alignas1.C.jj 2020-01-12 11:54:37.052403768 +0100 +++ gcc/testsuite/g++.dg/cpp0x/alignas1.C 2024-09-06 17:22:09.014284498 +0200 @@ -1,5 +1,6 @@ // PR c++/65734 // { dg-do compile { target c++11 } } +// { dg-options "" } template <class T> struct A { --- gcc/testsuite/g++.dg/cpp0x/alignas2.C.jj 2020-01-12 11:54:37.052403768 +0100 +++ gcc/testsuite/g++.dg/cpp0x/alignas2.C 2024-09-06 17:22:36.848925193 +0200 @@ -1,5 +1,6 @@ // PR c++/65734 // { dg-do compile { target c++11 } } +// { dg-options "" } template <typename T> struct BVector --- gcc/testsuite/g++.dg/cpp0x/alignas7.C.jj 2020-01-12 11:54:37.052403768 +0100 +++ gcc/testsuite/g++.dg/cpp0x/alignas7.C 2024-09-06 17:23:02.934588031 +0200 @@ -1,5 +1,6 @@ // PR c++/71513 // { dg-do compile { target c++11 } } +// { dg-options "" } template < int N, typename T > struct A --- gcc/testsuite/g++.dg/cpp0x/alignas21.C.jj 2024-09-06 14:30:31.548763706 +0200 +++ gcc/testsuite/g++.dg/cpp0x/alignas21.C 2024-09-06 17:18:20.721231386 +0200 @@ -0,0 +1,157 @@ +// C++ 26 P2552R3 - On the ignorability of standard attributes +// { dg-do compile { target c++11 } } + +int arr[2]; +struct S { int a, b; }; +S arr2[2]; + +void +foo (int n) +{ + alignas (int) int x1; + alignas ("foobar") int x2; // { dg-error "'alignas' argument has non-integral type 'const char \\\[7\\\]'" } + alignas (0) int x3; // { dg-warning "requested alignment '0' is not a positive power of 2" } + alignas ("foo", "bar", "baz") int x4; // { dg-error "'alignas' argument has non-integral type 'const char \\\[4\\\]'" } + // { dg-error "expected '\\\)' before ',' token" "" { target *-*-* } .-1 } + // { dg-error "expected declaration before ',' token" "" { target *-*-* } .-2 } + // { dg-error "expected primary-expression before ',' token" "" { target *-*-* } .-3 } + alignas (0, 1, 2) int x5; // { dg-error "expected '\\\)' before ',' token" } + // { dg-error "expected declaration before ',' token" "" { target *-*-* } .-1 } + // { dg-error "expected primary-expression before ',' token" "" { target *-*-* } .-2 } + + auto a = [] alignas (int) () {}; // { dg-error "'alignas' on function declaration" } + auto b = [] constexpr alignas (int) {}; // { dg-error "'alignas' on a type other than class" } + // { dg-error "parameter declaration before lambda declaration specifiers only optional with" "" { target c++20_down } .-1 } + // { dg-error "'constexpr' lambda only available with" "" { target c++14_down } .-2 } + auto c = [] noexcept alignas (int) {}; // { dg-error "'alignas' on a type other than class" } + // { dg-error "parameter declaration before lambda exception specification only optional with" "" { target c++20_down } .-1 } + auto d = [] () alignas (int) {}; // { dg-error "'alignas' on a type other than class" } + auto e = new int [n] alignas (int); // { dg-warning "attributes ignored on outermost array type in new expression" } + auto e2 = new int [n] alignas (int) [42]; // { dg-warning "attributes ignored on outermost array type in new expression" } + auto f = new int [n][42] alignas (int); // { dg-error "'alignas' on a type other than class" } + alignas (int); // { dg-warning "attributes at the beginning of statement are ignored" } + alignas (int) {} // { dg-warning "attributes at the beginning of statement are ignored" } + alignas (int) if (true) {} // { dg-warning "attributes at the beginning of statement are ignored" } + alignas (int) while (false) {} // { dg-warning "attributes at the beginning of statement are ignored" } + alignas (int) goto lab; // { dg-warning "attributes at the beginning of statement are ignored" } + alignas (int) lab:; // { dg-error "alignment may not be specified for 'lab'" } + alignas (int) try {} catch (int) {} // { dg-warning "attributes at the beginning of statement are ignored" } + if (alignas (int) int x = 0) {} + switch (n) + { + alignas (int) case 1: // { dg-error "alignment may not be specified for" } + alignas (int) break; // { dg-warning "attributes at the beginning of statement are ignored" } + alignas (int) default: // { dg-error "alignment may not be specified for" } + break; + } + for (alignas (int) auto a : arr) {} + for (alignas (int) auto [a, b] : arr2) {} // { dg-error "structured bindings only available with" "" { target c++14_down } } + alignas (int) asm (""); // { dg-warning "attributes ignored on 'asm' declaration" } + try {} catch (alignas (int) int x) {} // { dg-error "'alignas' on exception declaration" } + try {} catch (alignas (int) int) {} // { dg-error "'alignas' on exception declaration" } + try {} catch (int alignas (int) x) {} // { dg-warning "attribute ignored" } + try {} catch (int alignas (int)) {} // { dg-warning "attribute ignored" } + try {} catch (int x alignas (int)) {} // { dg-error "'alignas' on exception declaration" } +} + +alignas (int) int bar (); // { dg-error "'alignas' on function declaration" } +using foobar alignas (int) = int; // { dg-error "'alignas' on a type alias" } +alignas (int) int a; +alignas (int) auto [b, c] = arr; // { dg-error "structured bindings only available with" "" { target c++14_down } } +alignas (int); // { dg-warning "attribute ignored" } +inline alignas (int) void baz () {} // { dg-warning "attribute ignored" } + // { dg-error "standard attributes in middle of decl-specifiers" "" { target *-*-* } .-1 } +constexpr alignas (int) int qux () { return 0; } // { dg-warning "attribute ignored" } + // { dg-error "standard attributes in middle of decl-specifiers" "" { target *-*-* } .-1 } +int alignas (int) d; // { dg-warning "attribute ignored" } +int const alignas (int) e = 1; // { dg-warning "attribute ignored" } +struct A {} alignas (int); // { dg-warning "attribute ignored in declaration of 'struct A'" } +struct A alignas (int); // { dg-warning "attribute ignored" } +struct A alignas (int) a1; // { dg-warning "attribute ignored" } +A alignas (int) a2; // { dg-warning "attribute ignored" } +enum B { B0 } alignas (int); // { dg-warning "attribute ignored in declaration of 'enum B'" } +enum B alignas (int); // { dg-warning "attribute ignored" } +enum B alignas (int) b1; // { dg-warning "attribute ignored" } +B alignas (int) b2; // { dg-warning "attribute ignored" } +struct alignas (int) C {}; +int f alignas (int); +struct alignas (int) Y; +int g[2] alignas (int); // { dg-error "'alignas' on a type other than class" } +int g2 alignas (int) [2]; +int corge () alignas (int); // { dg-error "'alignas' on a type other than class" } +int *alignas (int) h; // { dg-error "'alignas' on a type other than class" } +int & alignas (int) i = f; // { dg-error "'alignas' on a type other than class" } +int && alignas (int) j = 0; // { dg-error "'alignas' on a type other than class" } +int S::* alignas (int) k; // { dg-error "'alignas' on a type other than class" } +auto l = sizeof (int [2] alignas (int)); // { dg-error "'alignas' on a type other than class" } +int freddy (alignas (int) int a, // { dg-error "alignment may not be specified for 'a'" } + alignas (int) int, // { dg-error "alignment may not be specified for '<anonymous>'" } + alignas (int) int c = 0, // { dg-error "alignment may not be specified for 'c'" } + alignas (int) int = 0); // { dg-error "alignment may not be specified for '<anonymous>'" } +void +corge (alignas (int) int a, // { dg-error "alignment may not be specified for 'a'" } + alignas (int) int, // { dg-error "alignment may not be specified for '<anonymous>'" } + alignas (int) int c = 0, // { dg-error "alignment may not be specified for 'c'" } + alignas (int) int = 0) // { dg-error "alignment may not be specified for '<anonymous>'" } +{ +} +alignas (int) void +garply () // { dg-error "'alignas' on function declaration" } +{ +} +int grault (int alignas (int) a, // { dg-warning "attribute ignored" } + int alignas (int), // { dg-warning "attribute ignored" } + int alignas (int) c = 0, // { dg-warning "attribute ignored" } + int alignas (int) = 0); // { dg-warning "attribute ignored" } +void +waldo (int alignas (int) a, // { dg-warning "attribute ignored" } + int alignas (int), // { dg-warning "attribute ignored" } + int alignas (int) c = 0, // { dg-warning "attribute ignored" } + int alignas (int) = 0) // { dg-warning "attribute ignored" } +{ +} +int plugh (int a alignas (int), // { dg-error "alignment may not be specified for 'a'" } + int b alignas (int) = 0); // { dg-error "alignment may not be specified for 'b'" } +void +thud (int a alignas (int), // { dg-error "alignment may not be specified for 'a'" } + int b alignas (int) = 0) // { dg-error "alignment may not be specified for 'b'" } +{ +} +enum alignas (int) D { D0 }; // { dg-error "'alignas' on enumerated type" } +enum class alignas (int) E { E0 }; // { dg-error "'alignas' on enumerated type" } +enum F {}; +enum alignas (int) F; // { dg-warning "type attributes ignored after type is already defined" } +enum G { + G0 alignas (int), // { dg-error "alignment may not be specified for 'G0'" } + G1 alignas (int) = 2 // { dg-error "alignment may not be specified for 'G1'" } +}; +namespace alignas (int) H { using H0 = int; } // { dg-error "expected identifier before 'alignas'" } + // { dg-error "H' does not name a type" "" { target *-*-* } .-1 } +namespace alignas (int) {} // { dg-error "expected identifier before 'alignas'" } + // { dg-error "expected unqualified-id before '\\\{' token" "" { target *-*-* } .-1 } +alignas (int) using namespace H; + // { dg-error "'H' is not a namespace-name" "" { target *-*-* } .-1 } +struct alignas (int) I +{ + alignas (int); // { dg-error "declaration does not declare anything" } + alignas (int) int i; + alignas (int) int foo (); // { dg-error "'alignas' on function declaration" } + alignas (int) int bar () { return 1; } // { dg-error "'alignas' on function declaration" } + alignas (int) int : 0; // { dg-error "'alignas' on bit-field" } + alignas (int) int i2 : 5; // { dg-error "'alignas' on bit-field" } + alignas (int) static int i3; + static int i4; +}; +alignas (int) int I::i4 = 0; +struct J : alignas (int) C {}; // { dg-warning "attributes on base specifiers are ignored" } +#if __cpp_concepts >= 201907L +template <typename T> +concept K alignas (int) = requires { true; }; // { dg-error "alignment may not be specified for 'K'" "" { target c++20 } } +#endif +typedef int L alignas (int); // { dg-error "'alignas' on a type alias" } +template <typename T> +struct M {}; +template <> +struct alignas (int) M<int> { int m; }; +typedef int N[2] alignas (int); // { dg-error "'alignas' on a type other than class" } +typedef int O alignas (int) [2]; // { dg-error "'alignas' on a type alias" } --- gcc/testsuite/g++.dg/ext/bitfield9.C.jj 2020-01-14 20:02:46.829609145 +0100 +++ gcc/testsuite/g++.dg/ext/bitfield9.C 2024-09-06 20:25:54.024691945 +0200 @@ -3,7 +3,7 @@ struct S { - char a:4; + char a:4; // { dg-warning "alignas' on bit-field" "" { target *-*-* } .+1 } char b:8 alignas(int); // { dg-warning "ISO C\\+\\+ allows bit-field attributes only before the ':' token" } char c:8 [[gnu::aligned(8)]]; // { dg-warning "ISO C\\+\\+ allows bit-field attributes only before the ':' token" } // { dg-error "two consecutive '\\\[' shall only introduce an attribute before '\\\[' token" "" { target *-*-* } .-1 } --- gcc/testsuite/g++.dg/cpp2a/is-layout-compatible3.C.jj 2022-11-17 22:10:05.809657315 +0100 +++ gcc/testsuite/g++.dg/cpp2a/is-layout-compatible3.C 2024-09-06 20:27:54.773223783 +0200 @@ -1,5 +1,6 @@ // P0466R5 // { dg-do compile { target c++20 } } +// { dg-options "-pedantic" } namespace std { @@ -34,7 +35,7 @@ union N { J v; }; union O { int a; int b; }; union P { int a : 1; int b : 12; }; enum Q : int { Q1, Q2 }; -enum alignas(16) R : int { R1, R2 }; +enum alignas(16) R : int { R1, R2 }; // { dg-warning "'alignas' on enumerated type" } struct U { [[no_unique_address]] S a1; [[no_unique_address]] S a2; [[no_unique_address]] S a3; }; struct V { [[no_unique_address]] S b1; [[no_unique_address]] T b2; [[no_unique_address]] S b3; }; struct alignas(16) A : public I {}; Jakub