@@ -38,9 +38,6 @@ along with GCC; see the file COPYING3. If not see
searched. */
static const struct attribute_spec *attribute_tables[4];
-/* Hashtable mapping names (represented as substrings) to attribute specs. */
-static htab_t attribute_hash;
-
/* Substring representation. */
struct substring
@@ -49,6 +46,30 @@ struct substring
int length;
};
+/* Scoped attribute name representation. */
+
+struct scoped_attributes
+{
+ const char *ns;
+ unsigned len;
+ struct attribute_spec *attributes;
+ htab_t attribute_hash;
+};
+
+struct scoped_attributes_table
+{
+ unsigned len;
+ struct scoped_attributes *scoped;
+};
+
+/* The table of scope attributes. */
+static struct scoped_attributes_table *attributes_table;
+
+static struct scoped_attributes* set_attributes_namespace (const struct attribute_spec *,
+ unsigned, const char*);
+static struct scoped_attributes* find_attribute_namespace (const char*);
+static void register_scoped_attribute (const struct attribute_spec *,
+ struct scoped_attributes *);
static bool attributes_initialized = false;
/* Default empty table of attributes. */
@@ -102,6 +123,116 @@ eq_attr (const void *p, const void *q)
return (!strncmp (spec->name, str->str, str->length) && !spec->name[str->length]);
}
+/* Set attributes ATTRIBUTES into the namespace NS. */
+
+static struct scoped_attributes*
+set_attributes_namespace (const struct attribute_spec * attributes,
+ unsigned attributes_length,
+ const char* ns)
+{
+ unsigned i;
+ struct scoped_attributes *iter = NULL;
+ bool found_attrs = false;
+
+ /* See if we already have attributes in the namespace NS. */
+ for (i = 0; attributes_table != NULL && i < attributes_table->len; ++i)
+ {
+ iter = &attributes_table->scoped[i];
+ if (strcmp (iter->ns, ns) == 0)
+ {
+ found_attrs = true;
+ break;
+ }
+ }
+
+ if (found_attrs)
+ {
+ /* We already have attributes in the namespace NS. Add
+ ATTRIBUTES to that namespace. */
+
+ unsigned j;
+
+ gcc_assert (strcmp (iter->ns, ns) == 0);
+
+ iter->attributes = XRESIZEVEC (struct attribute_spec,
+ iter->attributes,
+ iter->len + attributes_length);
+
+ for (i = iter->len, j = 0;
+ i < iter->len + attributes_length;
+ ++i, ++j)
+ memcpy (&iter->attributes[i], &attributes[j],
+ sizeof (struct attribute_spec));
+ iter->len += attributes_length;
+ }
+ else
+ {
+ /* We don't have any namespace NS yet. Create one and add
+ ATTRIBUTES into it. */
+
+ if (attributes_table == NULL)
+ attributes_table = XCNEW (struct scoped_attributes_table);
+
+ attributes_table->scoped = XRESIZEVEC (struct scoped_attributes,
+ attributes_table->scoped,
+ attributes_table->len + 1);
+ iter = &attributes_table->scoped[attributes_table->len];
+ memset (iter, 0, sizeof (struct scoped_attributes));
+ iter->ns = ns;
+ iter->len = attributes_length;
+ iter->attributes = XCNEWVEC (struct attribute_spec,
+ attributes_length);
+ memcpy (iter->attributes,
+ attributes,
+ attributes_length * sizeof (struct attribute_spec));
+ attributes_table->len++;
+ }
+
+ return iter;
+}
+
+/* Return the namespace which name is NS, NULL if none exist. */
+
+static struct scoped_attributes*
+find_attribute_namespace (const char* ns)
+{
+ unsigned i;
+ struct scoped_attributes * attrs = NULL;
+
+ for (i = 0; i < attributes_table->len; ++i)
+ if (ns == attributes_table->scoped->ns
+ || (attributes_table->scoped->ns != NULL
+ && ns != NULL
+ && !strcmp (attributes_table->scoped[i].ns, ns)))
+ {
+ attrs = &attributes_table->scoped[i];
+ break;
+ }
+
+ if (attrs != NULL && attrs->attribute_hash == 0)
+ attrs->attribute_hash =
+ htab_create (200, hash_attr, eq_attr, NULL);
+
+ return attrs;
+}
+
+/* Return the length of the NULL terminated array of struct
+ attribute_spec. */
+
+static unsigned
+attributes_array_length (const struct attribute_spec *attributes_array)
+{
+ unsigned i;
+
+ if (attributes_array == NULL || attributes_array[0].name == NULL)
+ return 0;
+
+ for (i = 0; attributes_array[i].name != NULL; ++i)
+ ;
+
+ return i;
+}
+
/* Initialize attribute tables, and make some sanity checks
if --enable-checking. */
@@ -109,7 +240,6 @@ void
init_attributes (void)
{
size_t i;
- int k;
if (attributes_initialized)
return;
@@ -181,12 +311,12 @@ init_attributes (void)
}
#endif
- attribute_hash = htab_create (200, hash_attr, eq_attr, NULL);
- for (i = 0; i < ARRAY_SIZE (attribute_tables); i++)
- for (k = 0; attribute_tables[i][k].name != NULL; k++)
- {
- register_attribute (&attribute_tables[i][k]);
- }
+ for (i = 0; i < ARRAY_SIZE (attribute_tables); ++i)
+ /* Put all the GNU attributes into the "gnu" namespace. */
+ register_scoped_attributes (attribute_tables[i],
+ attributes_array_length (attribute_tables[i]),
+ "gnu");
+
invoke_plugin_callbacks (PLUGIN_ATTRIBUTES, NULL);
attributes_initialized = true;
}
@@ -196,9 +326,23 @@ init_attributes (void)
void
register_attribute (const struct attribute_spec *attr)
{
+ register_scoped_attribute (attr, find_attribute_namespace ("gnu"));
+}
+
+/* Insert a single attribute ATTR into a namespace of attributes. */
+
+static void
+register_scoped_attribute (const struct attribute_spec *attr,
+ struct scoped_attributes *name_space)
+{
struct substring str;
void **slot;
+ gcc_assert (attr != NULL && name_space != NULL);
+
+ if (name_space->attribute_hash == NULL)
+ name_space->attribute_hash = htab_create (200, hash_attr, eq_attr, NULL);
+
str.str = attr->name;
str.length = strlen (str.str);
@@ -206,27 +350,68 @@ register_attribute (const struct attribute_spec *attr)
in the form '__text__'. */
gcc_assert (str.length > 0 && str.str[0] != '_');
- slot = htab_find_slot_with_hash (attribute_hash, &str,
+ slot = htab_find_slot_with_hash (name_space->attribute_hash, &str,
substring_hash (str.str, str.length),
INSERT);
gcc_assert (!*slot || attr->name[0] == '*');
*slot = (void *) CONST_CAST (struct attribute_spec *, attr);
}
-/* Return the spec for the attribute named NAME. */
+/* Insert an array of attributes ATTRS into a namespace. ATTRS_LEN is
+ the length of the ATTRS array. NAME_SPACE is the name of attribute
+ namespace. The function returns the namespace into which the
+ attributes have been registered. */
+
+struct scoped_attributes*
+register_scoped_attributes (const struct attribute_spec *attrs,
+ unsigned attrs_len,
+ const char *name_space)
+{
+ unsigned i;
+ struct scoped_attributes *ns = set_attributes_namespace (attrs,
+ attrs_len,
+ name_space);
+ if (ns == NULL)
+ return NULL;
+
+ for (i = 0; i < attrs_len; ++i)
+ register_scoped_attribute (&attrs[i], ns);
+
+ return ns;
+}
+
+/* Return the spec for the scoped attribute with namespace NS and
+ name NAME. */
const struct attribute_spec *
-lookup_attribute_spec (const_tree name)
+lookup_scoped_attribute_spec (const_tree ns, const_tree name)
{
struct substring attr;
+ struct scoped_attributes *attrs;
+
+ const char *ns_str = (ns != NULL_TREE) ? IDENTIFIER_POINTER (ns): NULL;
+
+ attrs = find_attribute_namespace (ns_str);
+
+ if (attrs == NULL)
+ return NULL;
attr.str = IDENTIFIER_POINTER (name);
attr.length = IDENTIFIER_LENGTH (name);
extract_attribute_substring (&attr);
return (const struct attribute_spec *)
- htab_find_with_hash (attribute_hash, &attr,
+ htab_find_with_hash (attrs->attribute_hash, &attr,
substring_hash (attr.str, attr.length));
}
+
+/* Return the spec for the attribute named NAME. */
+
+const struct attribute_spec *
+lookup_attribute_spec (const_tree name)
+{
+ return lookup_scoped_attribute_spec (get_identifier ("gnu"), name);
+}
+
/* Process the attributes listed in ATTRIBUTES and install them in *NODE,
which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL,
@@ -302,10 +487,11 @@ decl_attributes (tree *node, tree attributes, int flags)
for (a = attributes; a; a = TREE_CHAIN (a))
{
- tree name = TREE_PURPOSE (a);
+ tree ns = get_attribute_namespace (a);
+ tree name = get_attribute_name (a);
tree args = TREE_VALUE (a);
tree *anode = node;
- const struct attribute_spec *spec = lookup_attribute_spec (name);
+ const struct attribute_spec *spec = lookup_scoped_attribute_spec (ns, name);
bool no_add_attrs = 0;
int fn_ptr_quals = 0;
tree fn_ptr_tmp = NULL_TREE;
@@ -313,8 +499,15 @@ decl_attributes (tree *node, tree attributes, int flags)
if (spec == NULL)
{
if (!(flags & (int) ATTR_FLAG_BUILT_IN))
- warning (OPT_Wattributes, "%qE attribute directive ignored",
- name);
+ {
+ if (ns == NULL_TREE || !cxx_11_attribute_p (a))
+ warning (OPT_Wattributes, "%qE attribute directive ignored",
+ name);
+ else
+ warning (OPT_Wattributes,
+ "%<%E::%E%> scoped attribute directive ignored",
+ ns, name);
+ }
continue;
}
else if (list_length (args) < spec->min_length
@@ -406,9 +599,15 @@ decl_attributes (tree *node, tree attributes, int flags)
}
if (spec->handler != NULL)
- returned_attrs = chainon ((*spec->handler) (anode, name, args,
- flags, &no_add_attrs),
- returned_attrs);
+ {
+ int cxx_11_flag =
+ cxx_11_attribute_p (a) ? ATTR_FLAG_CXX_11 : 0;
+
+ returned_attrs = chainon ((*spec->handler) (anode, name, args,
+ flags|cxx_11_flag,
+ &no_add_attrs),
+ returned_attrs);
+ }
/* Layout the decl in case anything changed. */
if (spec->type_required && DECL_P (*node)
@@ -488,6 +687,56 @@ decl_attributes (tree *node, tree attributes, int flags)
return returned_attrs;
}
+/* Return TRUE iff ATTR has been parsed by the front-end as a C++-11
+ generalized attribute.
+
+ When G++ parses a C++11 generalized attribute, it is represented as
+ a TREE_LIST which TREE_PURPOSE is itself a TREE_LIST. TREE_PURPOSE
+ (TREE_PURPOSE (ATTR)) is the namespace of the attribute, and the
+ TREE_VALUE (TREE_PURPOSE (ATTR)) is its non-qualified name. Please
+ use get_attribute_namespace and get_attribute_name to retrieve the
+ namespace and name of the attribute, as these accessors work with
+ GNU attributes as well. */
+
+bool
+cxx_11_attribute_p (const_tree attr)
+{
+ if (attr == NULL_TREE
+ || TREE_CODE (attr) != TREE_LIST)
+ return false;
+
+ return (TREE_CODE (TREE_PURPOSE (attr)) == TREE_LIST);
+}
+
+/* Return the name of the attribute ATTR. This accessor works on GNU
+ and C++11 generalized (scoped) attributes.
+
+ Please read the comments of cxx_11_attribute_p to understand the
+ format of attributes. */
+
+tree
+get_attribute_name (const_tree attr)
+{
+ if (cxx_11_attribute_p (attr))
+ return TREE_VALUE (TREE_PURPOSE (attr));
+ return TREE_PURPOSE (attr);
+}
+
+/* Return the namespace of the attribute ATTR. This accessor works on
+ GNU and C++11 generalized (scoped) attributes. On GNU attributes,
+ it returns an identifier tree for the string "gnu".
+
+ Please read the comments of cxx_11_attribute_p to understand the
+ format of attributes. */
+
+tree
+get_attribute_namespace (const_tree attr)
+{
+ if (cxx_11_attribute_p (attr))
+ return TREE_PURPOSE (TREE_PURPOSE (attr));
+ return get_identifier ("gnu");
+}
+
/* Subroutine of set_method_tm_attributes. Apply TM attribute ATTR
to the method FNDECL. */
@@ -484,6 +484,7 @@ const struct c_common_resword c_common_reswords[] =
{ "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
{ "__volatile", RID_VOLATILE, 0 },
{ "__volatile__", RID_VOLATILE, 0 },
+ { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
{ "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
{ "asm", RID_ASM, D_ASM },
{ "auto", RID_AUTO, 0 },
@@ -6990,6 +6991,84 @@ check_user_alignment (const_tree align, bool allow_zero)
}
return i;
}
+/*
+ If in c++-11, check if the c++-11 alignment constraint with respect
+ to fundamental alignment (in [dcl.align]) are satisfied. If not in
+ c++-11 mode, does nothing.
+
+ [dcl.align]2/ says:
+
+ [* if the constant expression evaluates to a fundamental alignment,
+ the alignment requirement of the declared entity shall be the
+ specified fundamental alignment.
+
+ * if the constant expression evaluates to an extended alignment
+ and the implementation supports that alignment in the context
+ of the declaration, the alignment of the declared entity shall
+ be that alignment
+
+ * if the constant expression evaluates to an extended alignment
+ and the implementation does not support that alignment in the
+ context of the declaration, the program is ill-formed]. */
+
+static bool
+check_cxx_fundamental_alignment_constraints (tree node,
+ unsigned align_log,
+ int flags)
+{
+ bool alignment_too_large_p = false;
+
+ if (!(flags & ATTR_FLAG_CXX_11)
+ || (node == NULL_TREE || node == error_mark_node))
+ return true;
+
+ if (cxx_fundamental_alignment_p ((1U << align_log)))
+ return true;
+
+ if (DECL_P (node))
+ {
+ if (TREE_STATIC (node))
+ {
+ /* For file scope variables and static members, the target
+ supports alignments that are at most
+ MAX_OFILE_ALIGNMENT. */
+ if ((1U << align_log) > MAX_OFILE_ALIGNMENT)
+ alignment_too_large_p = true;
+ }
+ else
+ {
+#ifdef BIGGEST_FIELD_ALIGNMENT
+#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
+#else
+#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
+#endif
+ /* For non-static members, the target supports either
+ alignments that at most either BIGGEST_FIELD_ALIGNMENT
+ if it is defined or BIGGEST_ALIGNMENT. */
+ if (TREE_CODE (node) == FIELD_DECL
+ && decl_function_context
+ && (1U << align_log) > MAX_TARGET_FIELD_ALIGNMENT)
+ alignment_too_large_p = true;
+#undef MAX_TARGET_FIELD_ALIGNMENT
+ /* For stack variables, the target supports at most
+ MAX_STACK_ALIGNMENT. */
+ else if (decl_function_context (node) != NULL
+ && (1U << align_log) > MAX_STACK_ALIGNMENT)
+ alignment_too_large_p = true;
+ }
+ }
+ else if (TYPE_P (node))
+ {
+ /* Let's be liberal for types. */
+ if ((1U << align_log) > BIGGEST_ALIGNMENT)
+ alignment_too_large_p = true;
+ }
+
+ if (alignment_too_large_p)
+ error ("requested alignment is too large");
+
+ return !alignment_too_large_p;
+}
/* Handle a "aligned" attribute; arguments as in
struct attribute_spec.handler. */
@@ -7014,7 +7093,8 @@ handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
else if (TYPE_P (*node))
type = node, is_type = 1;
- if ((i = check_user_alignment (align_expr, false)) == -1)
+ if ((i = check_user_alignment (align_expr, false)) == -1
+ || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
*no_add_attrs = true;
else if (is_type)
{
@@ -7044,6 +7124,13 @@ handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
error ("alignment may not be specified for %q+D", decl);
*no_add_attrs = true;
}
+ else if (flags & ATTR_FLAG_CXX_11
+ && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
+ /* C++-11 [dcl.align/4]:
+ [When multiple alignment-specifiers are specified for an
+ entity, the alignment requirement shall be set to the
+ strictest specified alignment]. */
+ *no_add_attrs = true;
else if (TREE_CODE (decl) == FUNCTION_DECL
&& DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
{
@@ -11008,4 +11095,23 @@ convert_vector_to_pointer_for_subscript (location_t loc,
}
}
+/* Return true iff ALIGN is an integral constant that is a fundamental
+ alignment, as defined by [basic.align] in the c++-11
+ specifications.
+
+ That is:
+
+ [A fundamental alignment is represented by an alignment less than or
+ equal to the greatest alignment supported by the implementation
+ in all contexts, which is equal to
+ alignof(std::max_align_t)]. */
+
+bool
+cxx_fundamental_alignment_p (unsigned align)
+{
+
+ return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
+ TYPE_ALIGN (long_double_type_node)));
+}
+
#include "gt-c-family-c-common.h"
@@ -787,6 +787,7 @@ extern bool keyword_begins_type_specifier (enum rid);
extern bool keyword_is_storage_class_specifier (enum rid);
extern bool keyword_is_type_qualifier (enum rid);
extern bool keyword_is_decl_specifier (enum rid);
+extern bool cxx_fundamental_alignment_p (unsigned);
#define c_sizeof(LOC, T) c_sizeof_or_alignof_type (LOC, T, true, 1)
#define c_alignof(LOC, T) c_sizeof_or_alignof_type (LOC, T, false, 1)
@@ -411,7 +411,9 @@ typedef enum cpp0x_warn_str
/* user defined literals */
CPP0X_USER_DEFINED_LITERALS,
/* delegating constructors */
- CPP0X_DELEGATING_CTORS
+ CPP0X_DELEGATING_CTORS,
+ /* Generalized attributes */
+ CPP0X_GENERALIZED_ATTRIBUTES
} cpp0x_warn_str;
/* The various kinds of operation used by composite_pointer_type. */
@@ -5816,6 +5818,7 @@ extern int comp_cv_qualification (const_tree, const_tree);
extern int comp_cv_qual_signature (tree, tree);
extern tree cxx_sizeof_or_alignof_expr (tree, enum tree_code, bool);
extern tree cxx_sizeof_or_alignof_type (tree, enum tree_code, bool);
+extern tree cxx_alignas_expr (tree, tsubst_flags_t);
extern tree cxx_sizeof_nowarn (tree);
extern tree is_bitfield_expr_with_lowered_type (const_tree);
extern tree unlowered_expr_type (const_tree);
@@ -1091,7 +1091,7 @@ grokbitfield (const cp_declarator *declarator,
static bool
is_late_template_attribute (tree attr, tree decl)
{
- tree name = TREE_PURPOSE (attr);
+ tree name = get_attribute_name (attr);
tree args = TREE_VALUE (attr);
const struct attribute_spec *spec = lookup_attribute_spec (name);
tree arg;
@@ -3368,6 +3368,11 @@ maybe_warn_cpp0x (cpp0x_warn_str str)
"delegating constructors "
"only available with -std=c++11 or -std=gnu++11");
break;
+ case CPP0X_GENERALIZED_ATTRIBUTES:
+ pedwarn (input_location, 0,
+ "generalized attributes "
+ "only available with -std=c++11 or -std=gnu++11");
+ break;
default:
gcc_unreachable ();
}
@@ -2103,9 +2103,29 @@ static tree cp_parser_asm_clobber_list
(cp_parser *);
static tree cp_parser_asm_label_list
(cp_parser *);
+static bool cp_next_tokens_can_be_attribute_p
+ (cp_parser *);
+static bool cp_next_tokens_can_be_gnu_attribute_p
+ (cp_parser *);
+static bool cp_next_tokens_can_be_std_attribute_p
+ (cp_parser *);
+static bool cp_nth_tokens_can_be_std_attribute_p
+ (cp_parser *, size_t);
+static bool cp_nth_tokens_can_be_gnu_attribute_p
+ (cp_parser *, size_t);
+static bool cp_nth_tokens_can_be_attribute_p
+ (cp_parser *, size_t);
static tree cp_parser_attributes_opt
(cp_parser *);
-static tree cp_parser_attribute_list
+static tree cp_parser_gnu_attributes_opt
+ (cp_parser *);
+static tree cp_parser_gnu_attribute_list
+ (cp_parser *);
+static tree cp_parser_std_attribute
+ (cp_parser *);
+static tree cp_parser_std_attribute_spec
+ (cp_parser *);
+static tree cp_parser_std_attribute_spec_seq
(cp_parser *);
static bool cp_parser_extension_opt
(cp_parser *, int *);
@@ -5609,6 +5629,13 @@ cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
switch (token->type)
{
case CPP_OPEN_SQUARE:
+ if (cp_next_tokens_can_be_std_attribute_p (parser))
+ {
+ cp_parser_error (parser,
+ "two consecutive %<[%> shall "
+ "only introduce an attribute");
+ return error_mark_node;
+ }
postfix_expression
= cp_parser_postfix_open_square_expression (parser,
postfix_expression,
@@ -8859,7 +8886,7 @@ cp_parser_label_for_labeled_statement (cp_parser* parser)
lab: __attribute__ ((unused)) int i;
we want the attribute to attach to "i", not "lab". */
if (label != NULL_TREE
- && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
+ && cp_next_tokens_can_be_gnu_attribute_p (parser))
{
tree attrs;
@@ -10299,8 +10326,7 @@ cp_parser_block_declaration (cp_parser *parser,
else if (cxx_dialect >= cxx0x
&& token2->type == CPP_NAME
&& ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
- || (cp_lexer_peek_nth_token (parser->lexer, 3)->keyword
- == RID_ATTRIBUTE)))
+ || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
cp_parser_alias_declaration (parser);
/* Otherwise, it's a using-declaration. */
else
@@ -10589,7 +10615,7 @@ cp_parser_decl_specifier_seq (cp_parser* parser,
if (!start_token)
start_token = token;
/* Handle attributes. */
- if (token->keyword == RID_ATTRIBUTE)
+ if (cp_next_tokens_can_be_attribute_p (parser))
{
/* Parse the attributes. */
decl_specs->attributes
@@ -15498,7 +15524,7 @@ cp_parser_init_declarator (cp_parser* parser,
*attributes_start_token = NULL;
cp_declarator *declarator;
tree prefix_attributes;
- tree attributes;
+ tree attributes = NULL;
tree asm_specification;
tree initializer;
tree decl = NULL_TREE;
@@ -15564,22 +15590,20 @@ cp_parser_init_declarator (cp_parser* parser,
decl_specifiers->type
= maybe_update_decl_type (decl_specifiers->type, scope);
- /* If we're allowing GNU extensions, look for an asm-specification
- and attributes. */
+ /* If we're allowing GNU extensions, look for an
+ asm-specification. */
if (cp_parser_allow_gnu_extensions_p (parser))
{
/* Look for an asm-specification. */
asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
asm_specification = cp_parser_asm_specification_opt (parser);
- /* And attributes. */
- attributes_start_token = cp_lexer_peek_token (parser->lexer);
- attributes = cp_parser_attributes_opt (parser);
}
else
- {
- asm_specification = NULL_TREE;
- attributes = NULL_TREE;
- }
+ asm_specification = NULL_TREE;
+
+ /* Look for attributes. */
+ attributes_start_token = cp_lexer_peek_token (parser->lexer);
+ attributes = cp_parser_attributes_opt (parser);
/* Peek at the next token. */
token = cp_lexer_peek_token (parser->lexer);
@@ -16179,7 +16203,8 @@ cp_parser_direct_declarator (cp_parser* parser,
break;
}
else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
- && token->type == CPP_OPEN_SQUARE)
+ && token->type == CPP_OPEN_SQUARE
+ && !cp_next_tokens_can_be_attribute_p (parser))
{
/* Parse an array-declarator. */
tree bounds;
@@ -16921,7 +16946,7 @@ cp_parser_type_specifier_seq (cp_parser* parser,
bool is_cv_qualifier;
/* Check for attributes first. */
- if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
+ if (cp_next_tokens_can_be_attribute_p (parser))
{
type_specifier_seq->attributes =
chainon (type_specifier_seq->attributes,
@@ -20104,6 +20129,81 @@ cp_parser_asm_label_list (cp_parser* parser)
return nreverse (labels);
}
+/* Return TRUE iff the next tokens in the stream are possibly the
+ beginning of a GNU extension attribute. */
+
+static bool
+cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
+{
+ return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
+}
+
+/* Return TRUE iff the next tokens in the stream are possibly the
+ beginning of a standard C++-11 attribute. */
+
+static bool
+cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
+{
+ return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
+}
+
+/* Return TRUE iff the next Nth tokens in the stream are possibly the
+ beginning of a standard C++-11 attribute. */
+
+static bool
+cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
+{
+ cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
+
+ return (cxx_dialect >= cxx0x
+ && token->type == CPP_OPEN_SQUARE
+ && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
+ && token->type == CPP_OPEN_SQUARE);
+}
+
+/* Return TRUE iff the next Nth tokens in the stream are possibly the
+ beginning of a GNU extension attribute. */
+
+static bool
+cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
+{
+ cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
+
+ return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
+}
+
+/* Return true iff the next tokens can be the beginning of either a
+ GNU attribute list, or a standard C++11 attribute sequence. */
+
+static bool
+cp_next_tokens_can_be_attribute_p (cp_parser *parser)
+{
+ return (cp_next_tokens_can_be_gnu_attribute_p (parser)
+ || cp_next_tokens_can_be_std_attribute_p (parser));
+}
+
+/* Return true iff the next Nth tokens can be the beginning of either
+ a GNU attribute list, or a standard C++11 attribute sequence. */
+
+static bool
+cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
+{
+ return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
+ || cp_nth_tokens_can_be_std_attribute_p (parser, n));
+}
+
+/* Parse either a standard C++-11 attribute-specifier-seq, or a series
+ of GNU attributes, or return NULL. */
+
+static tree
+cp_parser_attributes_opt (cp_parser *parser)
+{
+
+ if (cp_next_tokens_can_be_gnu_attribute_p (parser))
+ return cp_parser_gnu_attributes_opt (parser);
+ return cp_parser_std_attribute_spec_seq (parser);
+}
+
/* Parse an (optional) series of attributes.
attributes:
@@ -20112,10 +20212,10 @@ cp_parser_asm_label_list (cp_parser* parser)
attribute:
__attribute__ (( attribute-list [opt] ))
- The return value is as for cp_parser_attribute_list. */
+ The return value is as for cp_parser_gnu_attribute_list. */
static tree
-cp_parser_attributes_opt (cp_parser* parser)
+cp_parser_gnu_attributes_opt (cp_parser* parser)
{
tree attributes = NULL_TREE;
@@ -20141,7 +20241,7 @@ cp_parser_attributes_opt (cp_parser* parser)
token = cp_lexer_peek_token (parser->lexer);
if (token->type != CPP_CLOSE_PAREN)
/* Parse the attribute-list. */
- attribute_list = cp_parser_attribute_list (parser);
+ attribute_list = cp_parser_gnu_attribute_list (parser);
else
/* If the next token is a `)', then there is no attribute
list. */
@@ -20162,7 +20262,7 @@ cp_parser_attributes_opt (cp_parser* parser)
return attributes;
}
-/* Parse an attribute-list.
+/* Parse a GNU attribute-list.
attribute-list:
attribute
@@ -20180,7 +20280,7 @@ cp_parser_attributes_opt (cp_parser* parser)
the arguments, if any. */
static tree
-cp_parser_attribute_list (cp_parser* parser)
+cp_parser_gnu_attribute_list (cp_parser* parser)
{
tree attribute_list = NULL_TREE;
bool save_translate_strings_p = parser->translate_strings_p;
@@ -20259,6 +20359,275 @@ cp_parser_attribute_list (cp_parser* parser)
return nreverse (attribute_list);
}
+/* Parse a standard C++11 generalized attribute.
+
+ The returned representation is a TREE_LIST which TREE_PURPOSE is
+ the scoped name of the attribute, and the TREE_VALUE is its
+ arguments list.
+
+ Note that the scoped name of the attribute is itself a TREE_LIST
+ which TREE_PURPOSE is the namespace of the attribute, and
+ TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
+ by cp_parser_gnu_attribute_list -- that doesn't have any namespace
+ and which TREE_PURPOSE is directly the attribute name.
+
+ Clients of the attribute code should use get_attribute_namespace
+ and get_attribute_name to get the actual namespace and name of
+ attributes, regardless of their being GNU or C++11 generalized
+ attributes.
+
+ attribute:
+ attribute-token attribute-argument-clause [opt]
+
+ attribute-token:
+ identifier
+ attribute-scoped-token
+
+ attribute-scoped-token:
+ attribute-namespace :: identifier
+
+ attribute-namespace:
+ identifier
+
+ attribute-argument-clause:
+ ( balanced-token-seq )
+
+ balanced-token-seq:
+ balanced-token [opt]
+ balanced-token-seq balanced-token
+
+ balanced-token:
+ ( balanced-token-seq )
+ [ balanced-token-seq ]
+ { balanced-token-seq }. */
+
+static tree
+cp_parser_std_attribute (cp_parser *parser)
+{
+ tree attribute, attr_ns = NULL_TREE, attr_id, arguments;
+ cp_token *token;
+
+ /* First, parse name of the the attribute, a.k.a
+ attribute-token. */
+
+ token = cp_lexer_peek_token (parser->lexer);
+ if (token->type == CPP_NAME)
+ attr_id = token->u.value;
+ else if (token->type == CPP_KEYWORD)
+ attr_id = ridpointers[(int) token->keyword];
+ else
+ return NULL_TREE;
+ cp_lexer_consume_token (parser->lexer);
+
+ token = cp_lexer_peek_token (parser->lexer);
+ if (token->type == CPP_SCOPE)
+ {
+ /* We are seeing a scoped attribute token. */
+
+ cp_lexer_consume_token (parser->lexer);
+ attr_ns = attr_id;
+
+ token = cp_lexer_consume_token (parser->lexer);
+ if (token->type == CPP_NAME)
+ attr_id = token->u.value;
+ else if (token->type == CPP_KEYWORD)
+ attr_id = ridpointers[(int) token->keyword];
+ else
+ {
+ error_at (token->location,
+ "expected an identifier for the attribute name");
+ return error_mark_node;
+ }
+ attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
+ NULL_TREE);
+ token = cp_lexer_peek_token (parser->lexer);
+ }
+ else
+ attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
+ NULL_TREE);
+
+ /* Now parse the optional argument close of the attribute. */
+
+ if (token->type != CPP_OPEN_PAREN)
+ return attribute;
+
+ {
+ VEC(tree, gc) *vec;
+ int attr_flag = attribute_takes_identifier_p (attr_id)
+ ? id_attr
+ : normal_attr;
+
+ vec = cp_parser_parenthesized_expression_list
+ (parser, attr_flag, /*cast_p=*/false,
+ /*allow_expansion_p=*/true,
+ /*non_constant_p=*/NULL);
+ if (vec == NULL)
+ arguments = error_mark_node;
+ else
+ {
+ arguments = build_tree_list_vec (vec);
+ release_tree_vector (vec);
+ }
+
+ if (arguments == error_mark_node)
+ attribute = error_mark_node;
+ else
+ TREE_VALUE (attribute) = arguments;
+ }
+
+ return attribute;
+}
+
+/* Parse a list of standard C++-11 attributes.
+
+ attribute-list:
+ attribute [opt]
+ attribute-list , attribute[opt]
+ attribute ...
+ attribute-list , attribute ...
+*/
+
+static tree
+cp_parser_std_attribute_list (cp_parser *parser)
+{
+ tree attributes = NULL_TREE, attribute = NULL_TREE;
+ cp_token *token = NULL;
+
+ while (true)
+ {
+ attribute = cp_parser_std_attribute (parser);
+ if (attribute == error_mark_node)
+ break;
+ if (attribute != NULL_TREE)
+ {
+ TREE_CHAIN (attribute) = attributes;
+ attributes = attribute;
+ }
+ token = cp_lexer_peek_token (parser->lexer);
+ if (token->type != CPP_COMMA)
+ break;
+ cp_lexer_consume_token (parser->lexer);
+ }
+ attributes = nreverse (attributes);
+ return attributes;
+}
+
+/* Parse a standard C++-11 attribute specifier.
+
+ attribute-specifier:
+ [ [ attribute-list ] ]
+ alignment-specifier
+
+ alignment-specifier:
+ alignas ( type-id ... [opt] )
+ alignas ( alignment-expression ... [opt] ). */
+
+static tree
+cp_parser_std_attribute_spec (cp_parser *parser)
+{
+ tree attributes = NULL_TREE;
+ cp_token *token = cp_lexer_peek_token (parser->lexer);
+
+ if (token->type == CPP_OPEN_SQUARE
+ && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
+ {
+ cp_lexer_consume_token (parser->lexer);
+ maybe_warn_cpp0x (CPP0X_GENERALIZED_ATTRIBUTES);
+ cp_lexer_consume_token (parser->lexer);
+
+ attributes = cp_parser_std_attribute_list (parser);
+
+ if (cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE) == NULL
+ || cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE) == NULL)
+ cp_parser_skip_to_end_of_statement (parser);
+ }
+ else
+ {
+ tree alignas_expr;
+ bool complain = !cp_parser_uncommitted_to_tentative_parse_p (parser);
+
+ /* Look for an alignment-specifier. */
+
+ token = cp_lexer_peek_token (parser->lexer);
+
+ if (token->type != CPP_KEYWORD
+ || token->keyword != RID_ALIGNAS)
+ return NULL_TREE;
+
+ cp_lexer_consume_token (parser->lexer);
+ maybe_warn_cpp0x (CPP0X_GENERALIZED_ATTRIBUTES);
+
+ if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
+ {
+ cp_parser_error (parser, "expected %<(%>");
+ return error_mark_node;
+ }
+
+ cp_parser_parse_tentatively (parser);
+ alignas_expr = cp_parser_type_id (parser);
+
+ if (!cp_parser_parse_definitely (parser))
+ {
+ gcc_assert (alignas_expr == error_mark_node
+ || alignas_expr == NULL_TREE);
+
+ alignas_expr =
+ cp_parser_assignment_expression (parser, /*cast_p=*/false,
+ /**cp_id_kind=*/NULL);
+ if (alignas_expr == NULL_TREE
+ || alignas_expr == error_mark_node)
+ return alignas_expr;
+ }
+
+ if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
+ {
+ cp_parser_error (parser, "expected %<)%>");
+ return error_mark_node;
+ }
+
+ alignas_expr =
+ cxx_alignas_expr (alignas_expr,
+ complain ? tf_warning_or_error : tf_none);
+ alignas_expr = fold_non_dependent_expr (alignas_expr);
+
+ /* Build the C++-11 representation of an 'aligned'
+ attribute. */
+ attributes =
+ build_tree_list (build_tree_list (get_identifier ("gnu"),
+ get_identifier ("aligned")),
+ build_tree_list (NULL_TREE, alignas_expr));
+ }
+
+ return attributes;
+}
+
+/* Parse a standard C++-11 attribute-specifier-seq.
+
+ attribute-specifier-seq:
+ attribute-specifier-seq [opt] attribute-specifier
+ */
+
+static tree
+cp_parser_std_attribute_spec_seq (cp_parser *parser)
+{
+ tree attr_specs = NULL;
+
+ while (true)
+ {
+ tree attr_spec = cp_parser_std_attribute_spec (parser);
+ if (attr_spec == NULL_TREE)
+ break;
+ if (attr_spec == error_mark_node)
+ return error_mark_node;
+
+ TREE_CHAIN (attr_spec) = attr_specs;
+ attr_specs = attr_spec;
+ }
+
+ attr_specs = nreverse (attr_specs);
+ return attr_specs;
+}
+
/* Parse an optional `__extension__' keyword. Returns TRUE if it is
present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
current value of the PEDANTIC flag, regardless of whether or not
@@ -1701,6 +1701,66 @@ cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
else
return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
}
+
+/* Build a representation of an expression 'alignas(E).' Return the
+ folded integer value of E if it is an integral constant expression
+ that resolves to a valid alignment. If E depends on a template
+ parameter, return a syntactic representation tree of kind
+ ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
+ expression is ill formed, or NULL_TREE if E is NULL_TREE. */
+
+tree
+cxx_alignas_expr (tree e, tsubst_flags_t complain)
+{
+ if (e == NULL_TREE || e == error_mark_node)
+ return e;
+
+ if (processing_template_decl)
+ {
+ /* If E is a constant, fold it and return it right away.
+ Otherwise, build an ALIGNOF_EXPR that will be substituted
+ into, later at template instantiation time. */
+ tree cst = TYPE_P (e) ? NULL_TREE : cxx_constant_value (e);
+ if (cst != NULL && cst != error_mark_node)
+ return cst;
+ return cxx_alignof_expr (e, complain);
+ }
+
+ if (TYPE_P (e))
+ {
+ e = mark_type_use (e);
+
+ /*
+ [dcl.align]/3
+
+ When the alignment-specifier is of the form
+ alignas(type-id ), it shall have the same effect as
+ alignas( alignof(type-id )). */
+
+ return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR,
+ complain & tf_error
+ || complain & tf_warning);
+ }
+
+ /* If we reach this point, it means the alignas expression if of
+ the form "alignas(assignment-expression)", so we should follow
+ what is stated by [dcl.align]/2. */
+
+ e = mark_rvalue_use (e);
+
+ /* [dcl.align]/2 says:
+
+ [the assignment-expression shall be an integral constant
+ expression]. */
+ e = cxx_constant_value (e);
+ if (e == NULL_TREE
+ || e == error_mark_node
+ || TREE_CODE (e) != INTEGER_CST)
+ return error_mark_node;
+
+ return e;
+}
+
/* EXPR is being used in a context that is not a function call.
Enforce:
@@ -23,6 +23,7 @@ along with GCC; see the file COPYING3. If not see
#include "gcc-plugin.h"
struct attribute_spec;
+struct scoped_attributes;
extern void add_new_plugin (const char *);
extern void parse_plugin_arg_opt (const char *);
@@ -64,5 +65,7 @@ invoke_plugin_callbacks (int event ATTRIBUTE_UNUSED,
/* In attribs.c. */
extern void register_attribute (const struct attribute_spec *attr);
+extern struct scoped_attributes* register_scoped_attributes (const struct attribute_spec *,
+ unsigned, const char *);
#endif /* PLUGIN_H */
new file mode 100644
@@ -0,0 +1,3 @@
+// { dg-do compile { target c++11 } }
+
+int **** [[gnu::format(printf, 1, 2)]] foo(const char *, ...);
new file mode 100644
@@ -0,0 +1,9 @@
+// PR c++/12795
+// { dg-do compile { target c++11 } }
+// { dg-require-alias "" }
+
+void foo()
+{
+ extern void bar () [[gnu::__alias__ ("BAR")]]; // { dg-warning "ignored" }
+ bar ();
+}
new file mode 100644
@@ -0,0 +1,17 @@
+// { dg-do compile { target c++11 } }
+// PR c++/13791
+
+template <typename T> struct O {
+ struct [[gnu::packed]] I {
+ int i;
+ char c;
+ };
+
+ I* foo();
+};
+
+template <typename T>
+typename O<T>::I*
+O<T>::foo() { return 0; }
+
+template class O<int>;
new file mode 100644
@@ -0,0 +1,17 @@
+// { dg-do compile { target c++11 } }
+// PR c++/13791
+
+template <typename T> struct O {
+ struct [[gnu::packed]] I {
+ int i;
+ char c;
+ };
+
+ I* foo();
+};
+
+template <typename T>
+typename O<T>::I*
+O<T>::foo() { return 0; }
+
+template class O<int>;
new file mode 100644
@@ -0,0 +1,5 @@
+// { dg-do compile { target c++11 } }
+// PR c++/13854
+
+extern char *rindex (__const char *__s, int __c) throw () [[gnu::__pure__]];
+extern char *rindex (__const char *__s, int __c) throw () [[gnu::__pure__]];
new file mode 100644
@@ -0,0 +1,14 @@
+// { dg-do compile { target c++11 } }
+// PR c++/13170
+// The bogus attribute is ignored, but was in TYPE_ATTRIBUTES during
+// parsing of the class, causing some variants to have it and some not.
+
+struct [[gnu::bogus]] A // { dg-warning "ignored" "" }
+{
+ virtual ~A();
+ void foo(const A&);
+ void bar(const A&);
+};
+
+void A::foo(const A&) {}
+void A::bar(const A& a) { foo(a); }
new file mode 100644
@@ -0,0 +1,9 @@
+// { dg-do compile { target c++11 } }
+// PR c++/15317
+
+struct A
+{
+ A(char);
+};
+A::A([[gnu::unused]] char i2)
+{}
new file mode 100644
@@ -0,0 +1,8 @@
+// { dg-do compile { target c++11 } }
+// Origin: <rguenth at tat dot physik dot uni-tuebingen dot de>
+// PR c++/10479: use of non dependent expressions in attributes in templates
+
+template <int i>
+struct foo2 {
+ float bar [[gnu::aligned(__alignof__(double))]];
+};
new file mode 100644
@@ -0,0 +1,19 @@
+// { dg-do compile { target c++11 } }
+// Origin: Benjamin Kosnik <bkoz at gcc dot gnu dot org>
+// PR c++/17743: Attributes applied to typedefs.
+
+struct A {
+ typedef char layout_type[sizeof(double)]
+ [[gnu::aligned(__alignof__(double)]]); // { dg-error "expected" }
+ layout_type data;
+};
+
+struct B {
+ typedef char layout_type[sizeof(double)];
+ layout_type data [[gnu::aligned(__alignof__(double))]];
+};
+
+template<bool> struct StaticAssert;
+template<> struct StaticAssert<true> {};
+
+StaticAssert<__alignof__(A) == __alignof__(B)> a1;// { dg-error "incomplete type and cannot be defined" }
new file mode 100644
@@ -0,0 +1,19 @@
+// { dg-do compile { target c++11 } }
+// Origin: Benjamin Kosnik <bkoz at gcc dot gnu dot org>
+// PR c++/17743: Attributes applied to typedefs.
+
+struct A {
+ typedef char layout_type[sizeof(double)]
+ [[gnu::aligned(__alignof__(double))]];
+ layout_type data;
+};
+
+struct B {
+ typedef char layout_type[sizeof(double)];
+ layout_type data [[gnu::aligned(__alignof__(double))]];
+};
+
+template<bool> struct StaticAssert;
+template<> struct StaticAssert<true> {};
+
+StaticAssert<__alignof__(A) == __alignof__(B)> a1;
new file mode 100644
@@ -0,0 +1,10 @@
+// PR c++/17542
+// Test that we warn when an attribute preceding the class-key is ignored.
+// { dg-do compile { target c++11 } }
+
+[[gnu::packed]] struct A // { dg-warning "attribute" }
+{
+ char c;
+ int x;
+ void f();
+};
new file mode 100644
@@ -0,0 +1,11 @@
+// PR c++/19739
+// { dg-do compile { target c++11 } }
+
+void Dummy() [[ , ]];
+void Dummy() {}
+
+int main (int argc, char **argv)
+{
+ Dummy();
+ return 0;
+}
new file mode 100644
@@ -0,0 +1,11 @@
+// { dg-do compile { target c++11 } }
+
+struct [[gnu::packed]] A
+{
+ void f () const;
+};
+
+void
+A::f () const
+{
+}
new file mode 100644
@@ -0,0 +1,22 @@
+// { dg-do compile { target c++11 } }
+// { dg-options "-g" }
+// Origin: <jan at etpmod dot phys dot tue dot nl>
+// PR c++/19508: avoid attributes for template parameters
+
+template <typename T>
+struct BVector
+{
+ typedef T T2;
+ typedef T value_type [[gnu::aligned(8)]]; // { dg-bogus "attribute" "attribute" }
+ typedef T2 value_type2 [[gnu::aligned(8)]]; // { dg-bogus "attribute" "attribute" }
+ value_type v;
+};
+BVector<int> m;
+
+template <template <class> class T>
+struct BV2
+{
+ typedef T<float> value_type [[gnu::aligned(8)]]; // { dg-bogus "attribute" "attribute" }
+ value_type v;
+};
+BV2<BVector> m2;
new file mode 100644
@@ -0,0 +1,18 @@
+// PR c++/20763
+// { dg-do compile { target c++11 } }
+
+typedef void *voidp;
+
+struct S
+{
+ char a;
+ voidp [[gnu::aligned (16)]] b;
+};
+
+struct T
+{
+ char a;
+ void * [[gnu::aligned (16)]] b;
+};
+
+int f[sizeof (struct S) != sizeof (struct T) ? -1 : 1];
new file mode 100644
@@ -0,0 +1,7 @@
+// PR c++/27648
+// { dg-do compile { target c++11 } }
+
+void f()
+{
+ static_cast<float *[[gnu::unused]]>(0); // { dg-error "expected|invalid" }
+}
new file mode 100644
@@ -0,0 +1,11 @@
+// PR c++/28112
+// { dg-do compile { target c++11 } }
+
+int i [[gnu::init_priority(;)]]; // { dg-error "before" }
+int j [[gnu::vector_size(;)]]; // { dg-error "before" }
+int k [[gnu::visibility(;)]]; // { dg-error "before" }
+struct A {} [[gnu::aligned(;)]]; // { dg-error "before" }
+struct B {} [[gnu::mode(;)]]; // { dg-error "before" }
+void foo() [[gnu::alias(;)]]; // { dg-error "before" }
+void bar() [[gnu::nonnull(;)]]; // { dg-error "before" }
+void baz() [[gnu::section(;)]]; // { dg-error "before" }
new file mode 100644
@@ -0,0 +1,4 @@
+// PR c++/28387
+// { dg-do compile { target c++11 } }
+
+enum [[gnu::unused]] E; // { dg-error "without previous declaration" }
new file mode 100644
@@ -0,0 +1,12 @@
+// PR c++/28559
+// { dg-do compile { target c++11 } }
+
+template<typename T> struct A
+{
+ struct B;
+};
+
+struct C
+{
+ template<typename T> friend struct [[gnu::packed]] A<T>::B; // { dg-warning "uninstantiated" }
+};
new file mode 100644
@@ -0,0 +1,15 @@
+// PR c++/28659
+// The attribute was causing us to get confused in merge_types when
+// combining the template type with an uninstantiated version.
+// { dg-do compile { target c++11 } }
+
+template<class T>
+struct [[gnu::aligned(1)]] A
+{
+ A& operator=(const A &t);
+};
+
+template<class T>
+A<T>& A<T>::operator=(const A<T> &t)
+{
+}
new file mode 100644
@@ -0,0 +1,6 @@
+//PR c++/29980
+// { dg-do compile { target c++11 } }
+
+struct A { typedef int X; }; // { dg-message "previous declaration" }
+
+struct [[gnu::unused]] A::X; // { dg-error "typedef-name" }
new file mode 100644
@@ -0,0 +1,13 @@
+// PR c++/28558
+// { dg-options "" }
+// { dg-do compile { target c++11 } }
+
+struct A
+{
+ A(int) { }
+};
+
+int main()
+{
+ A a = (A [[gnu::unused]])0; // { dg-warning "attribute" }
+}
new file mode 100644
@@ -0,0 +1,10 @@
+// PR c++/33506
+// { dg-do compile { target c++11 } }
+
+extern int f1 (char *) [[gnu::warn_unused_result]];
+extern int f2 (char *) throw () [[gnu::warn_unused_result]];
+extern int f2 (char *) throw ();
+
+extern int f3 (char *) [[gnu::nonnull (1)]];
+extern int f4 (char *) throw () [[gnu::nonnull (1)]];
+extern int f4 (char *) throw ();
new file mode 100644
@@ -0,0 +1,21 @@
+// Test that attributes work in a variety of situations.
+// { dg-options -O }
+// { dg-do run { target c++11 } }
+
+#define attrib [[gnu::mode (QI)]]
+
+attrib signed int a; // attributes before type are broken
+static attrib unsigned int b;
+
+int foo(attrib int o) // attribute arguments are broken
+{
+ return (sizeof (a) != 1
+ || sizeof (b) != 1
+ || sizeof (o) != 1
+ || sizeof ((attrib signed int) b) != 1);
+}
+
+int main ()
+{
+ return foo (42);
+}
new file mode 100644
@@ -0,0 +1,8 @@
+// { dg-do compile { target c++11 } }
+// PR c++/35074
+template<typename T> struct A
+{
+ void foo() const;
+} [[gnu::aligned(4)]];
+
+template<typename T> void A<T>::foo() const {}
new file mode 100644
@@ -0,0 +1,16 @@
+// PR c++/35097
+// { dg-do compile { target c++11 } }
+
+template<int> struct A;
+
+template<> struct A<0>
+{
+ typedef int X [[gnu::aligned(4)]];
+};
+
+template<typename T> void foo(const A<0>::X&, T);
+
+void bar()
+{
+ foo(A<0>::X(), 0);
+}
new file mode 100644
@@ -0,0 +1,37 @@
+// PR c++/35315
+// { dg-do compile { target c++11 } }
+
+typedef union { int i; } U [[gnu::transparent_union]];
+
+static void foo(U) {}
+static void foo(int) {}
+
+void bar()
+{
+ foo(0);
+}
+
+typedef union U1 { int i; } U2 [[gnu::transparent_union]]; // { dg-warning "ignored" }
+
+static void foo2(U1) {} // { dg-error "previously defined" }
+static void foo2(U2) {} // { dg-error "redefinition" }
+
+void bar2(U1 u1, U2 u2)
+{
+ foo2(u1);
+ foo2(u2);
+}
+
+// PR c++/36410
+struct A
+{
+ typedef union
+ {
+ int i;
+ } B [[gnu::transparent_union]];
+};
+
+void foo(A::B b)
+{
+ b.i;
+}
new file mode 100644
@@ -0,0 +1,19 @@
+// PR c++/35546
+// { dg-do compile { target c++11 } }
+// { dg-options "-g" }
+
+template <int N>
+struct T
+{
+ void foo (char const * ...) [[gnu::format (printf,2,3)]];
+};
+
+template struct T<3>;
+
+template <typename T>
+struct U
+{
+ typedef T [[gnu::mode (SI)]] V;
+};
+
+U<int>::V v;
new file mode 100644
@@ -0,0 +1,19 @@
+// PR c/37171
+// { dg-do compile { target c++11 } }
+// { dg-options "-O2 -fdump-tree-optimized" }
+
+unsigned int f1 () [[gnu::const]];
+unsigned int f2 () [[gnu::__const]];
+unsigned int f3 () [[gnu::__const__]];
+
+unsigned int f4 ()
+{
+ return f1 () + f1 () + f1 () + f1 ()
+ + f2 () + f2 () + f2 () + f2 ()
+ + f3 () + f3 () + f3 () + f3 ();
+}
+
+// { dg-final { scan-tree-dump-times "= f1 \\(\\)" 1 "optimized" } }
+// { dg-final { scan-tree-dump-times "= f2 \\(\\)" 1 "optimized" } }
+// { dg-final { scan-tree-dump-times "= f3 \\(\\)" 1 "optimized" } }
+// { dg-final { cleanup-tree-dump "optimized" } }
new file mode 100644
@@ -0,0 +1,20 @@
+// { dg-do compile { target i?86-*-* x86_64-*-* } }
+// { dg-options "-O3 -msse2 -std=c++11" }
+// { dg-require-effective-target sse2 }
+
+// You can make NON-template typedefs with a large alignment.
+typedef double AlignedDoubleType [[gnu::aligned(16)]];
+
+template <typename RealType>
+RealType f(const RealType* p)
+{
+ // But if you use a template parameter it complains.
+ typedef RealType AlignedRealType [[gnu::aligned(16)]];
+
+ return p[0];
+}
+
+double f2(const double* p)
+{
+ return f<double>(p);
+}
new file mode 100644
@@ -0,0 +1,21 @@
+// PR c++/43031
+// { dg-options "-std=c++11 -pedantic" }
+// { dg-do compile { target { { i?86-*-* x86_64-*-* } && ia32 } } }
+
+class T;
+class L { };
+class P : public L
+{
+ typedef void ([[gnu::__stdcall__]] T::*F) (L*);
+ void f(bool aAdd);
+};
+class T
+{
+public:
+ virtual void [[gnu::__stdcall__]] A(L *listener) = 0;
+ virtual void [[gnu::__stdcall__]] R(L *listener) = 0;
+};
+void P::f(bool aAdd)
+{
+ F addRemoveEventListener = (aAdd ? &T::A : &T::R);
+}
new file mode 100644
@@ -0,0 +1,15 @@
+// PR c++/43093
+// { dg-options "-std=c++11 -pedantic" }
+// { dg-do compile { target { { i?86-*-* x86_64-*-* } && ia32 } } }
+
+struct S {
+ int x;
+ S(const S &s) {}
+};
+
+S [[gnu::__stdcall__]] getS();
+
+void test()
+{
+ S s = getS();
+}
new file mode 100644
@@ -0,0 +1,12 @@
+// { dg-do compile { target c++11 } }
+// PR c++/36625
+
+template <int N>
+struct A {
+ struct S { short f[3]; } [[gnu::aligned (N)]];
+};
+
+int main ()
+{
+ A<4>::S s;
+}
new file mode 100644
@@ -0,0 +1,10 @@
+// PR debug/43370
+// { dg-do compile { target c++11 } }
+// { dg-options "-g" }
+
+int fragile_block(void) {
+ typedef [[gnu::aligned (16)]] struct {
+ int i;
+ } XmmUint16;
+ return 0;
+}
new file mode 100644
@@ -0,0 +1,32 @@
+// { dg-do compile { target c++11 } }
+// Test for syntax support of various attribute permutations.
+
+int
+[[gnu::noreturn]]
+[[gnu::unused]]
+one(void); // OK
+
+[[gnu::noreturn]]
+[[gnu::unused]]
+int
+two(void); // OK
+
+int
+[[gnu::unused]]
+three (void)
+[[gnu::noreturn]]; // OK
+
+[[gnu::unused]]
+int
+four (void)
+[[gnu::noreturn]]; // OK
+
+int
+five(void)
+[[gnu::noreturn]]
+[[gnu::unused]]; // OK
+
+[[gnu::noreturn]]
+int
+[[gnu::unused]] // parse error before '__attribute__' in C++
+six (void); // OK in C
new file mode 100644
@@ -0,0 +1,5 @@
+// PR c++/46803
+// { dg-do compile { target c++11 } }
+
+int strftime(char *, int, const char *, const struct tm *)
+ [[gnu::__bounded__(__string__,1,2)]]; // { dg-warning "ignored" }
new file mode 100644
@@ -0,0 +1,20 @@
+// PR c++/45267
+// { dg-do compile { target c++11 } }
+// { dg-options "-O" }
+
+template<typename T> struct Vector {
+ Vector(long long x);
+ inline Vector<T> operator<<(int x) const [[gnu::always_inline]];
+};
+long long bar (long long);
+template<> inline Vector<int> Vector<int>::operator<<(int x) const {
+ return bar(x);
+}
+bool b;
+int main() {
+ Vector<int> a(1);
+ if ((a << 2), b) {
+ a << 2;
+ throw 1;
+ }
+}
new file mode 100644
@@ -0,0 +1,13 @@
+// { dg-options "-std=c++11 -pedantic" }
+// { dg-do compile { target { i?86-*-* && ilp32 } } }
+
+struct A {
+ [[gnu::fastcall]]
+ void f();
+};
+
+int main()
+{
+ typedef void (A::*FP)();
+ FP fp[] = {&A::f}; // { dg-error "cannot convert" }
+}
new file mode 100644
@@ -0,0 +1,6 @@
+// { dg-do compile { target c++11 } }
+template <class T> struct A { };
+
+template
+[[gnu::packed]]
+struct A<int>; // { dg-warning "attribute" }
new file mode 100644
@@ -0,0 +1,4 @@
+// PR c++/52671
+// { dg-do compile { target c++11 } }
+[[gnu::deprecated]] enum E { E0 }; // { dg-warning "attribute ignored in declaration of" }
+// { dg-message "must follow the" "" { target *-*-* } 3 }
new file mode 100644
@@ -0,0 +1,4 @@
+// PR c++/52906
+// { dg-do compile { target c++11 } }
+
+[[gnu::deprecated]]; // { dg-error "does not declare anything" }
new file mode 100644
@@ -0,0 +1,5 @@
+// PR c++/40821
+// { dg-do compile { target c++11 } }
+
+struct [[gnu::aligned(8)] S1 { int i; }; // { dg-error "" }
+struct [aligned(8) S2 { int i; }; // { dg-error "" }
new file mode 100644
@@ -0,0 +1,13 @@
+// { dg-do compile { target c++11 } }
+// Example taken from dcl.attr.grammar:
+
+int p[10];
+void f()
+{
+ int x = 42, y[5];
+ /* Here, the '[[gnu::' should have introduced an attribute, ont a
+ lambda invocation an array subscripting expression. */
+ int(p[[gnu::x] { return x; }()]); // { dg-error "expected|consecutive" }
+ /* Likewise, the '[[gnu::' is invalid here. */
+ y[[gnu::] { return 2; }()] = 2; // { dg-error "expected|consecutive" }
+}
new file mode 100644
@@ -0,0 +1,17 @@
+// { dg-do compile { target c++11 } }
+
+typedef char layout_type;
+
+struct A {
+ layout_type member alignas (double);
+};
+
+static_assert (sizeof (A) == sizeof (double),
+ "size of struct A must be sizeof (double)");
+
+struct alignas (alignof (long double)) B {
+ layout_type member;
+};
+
+static_assert (sizeof (B) == sizeof (long double),
+ "size of struct A must be sizeof (double double)");
new file mode 100644
@@ -0,0 +1,17 @@
+// { dg-do compile { target c++11 } }
+
+typedef char layout_type;
+
+template<class> struct A {
+ layout_type member alignas (double);
+};
+
+static_assert (sizeof (A<int>) == sizeof (double),
+ "size of struct A must be sizeof (double)");
+
+template<class> struct alignas (alignof (long double)) B {
+ layout_type member;
+};
+
+static_assert (sizeof (B<int>) == sizeof (long double),
+ "size of struct A must be sizeof (double double)");
new file mode 100644
@@ -0,0 +1,23 @@
+// { dg-do compile { target c++11 } }
+//
+// There were two related problems here, depending on the vintage. At
+// one time:
+//
+// typedef struct A { ... } A [[gnu::aligned (16)]];
+//
+// would cause original_types to go into an infinite loop. At other
+// times, the attributes applied to an explicit typedef would be lost
+// (check_b3 would have a negative size).
+
+// First check that the declaration is accepted and has an effect.
+typedef struct A { int i; } A [[gnu::aligned (16)]];
+int check_A[__alignof__ (A) >= 16 ? 1 : -1];
+
+// Check that the alignment is only applied to the typedef.
+struct B { int i; };
+struct B b1;
+typedef struct B B [[gnu::aligned (16)]];
+struct B b2;
+B b3;
+int check_b1[__alignof__ (b1) == __alignof__ (b2) ? 1 : -1];
+int check_b3[__alignof__ (b3) >= 16 ? 1 : -1];
new file mode 100644
@@ -0,0 +1,22 @@
+// { dg-do compile { target c++11 } }
+
+typedef char layout_type;
+
+template<class> struct A {
+ layout_type member alignas (double) alignas (int);
+};
+
+// Here, the spec says that A<int> should have the stricter alignment,
+// so that would be the alignment of 'double', not 'int'.
+static_assert (sizeof (A<int>) == sizeof (double),
+ "size of struct A must be sizeof (double)");
+
+template<class> struct alignas (1) alignas (alignof (long double)) B {
+ layout_type member;
+};
+
+// Similarly, the B<int> should have the stricter alignment, so that would
+// so that would be the alignment of 'long double', not '1'.
+static_assert (sizeof (B<int>) == sizeof (long double),
+ "size of struct A must be sizeof (double double)");
+
new file mode 100644
@@ -0,0 +1,9 @@
+// { dg-do compile { target c++11 } }
+
+typedef char layout_type;
+struct A
+{
+ layout_type member [[gnu::aligned (16)]];
+};
+
+static_assert (sizeof (A) == 16, "Alignment should be 16");
new file mode 100644
@@ -0,0 +1,20 @@
+// Copyright (C) 2002 Free Software Foundation.
+//
+// Test that the nothrow attribute is working correctly.
+//
+// Written by Richard Henderson, 26 May 2002.
+
+// { dg-do link { target c++11} }
+extern void foo() [[gnu::nothrow]];
+extern void link_error();
+
+int main()
+{
+ try {
+ foo();
+ } catch (...) {
+ link_error();
+ }
+}
+
+void foo() { }
new file mode 100644
@@ -0,0 +1,4 @@
+// { dg-do compile { target c++11 } }
+// { dg-options "-Wunused-parameter" }
+
+void f (int i [[gnu::__unused__]]) {}
new file mode 100644
@@ -0,0 +1,11 @@
+// PR 8656
+// { dg-options "-std=c++11 -pedantic" }
+// { dg-do compile { target { { i?86-*-* x86_64-*-* } && ia32 } } }
+
+extern int * ([[gnu::stdcall]] *fooPtr)( void);
+int * [[gnu::stdcall]] myFn01( void) { return 0; }
+
+void snafu( void)
+{
+ fooPtr = myFn01;
+}
new file mode 100644
@@ -0,0 +1,12 @@
+// { dg-do compile { target c++11 } }
+
+class [[gnu::unused]] C;
+struct [[gnu::unused]] S;
+union [[gnu::unused]] U;
+enum e {};
+enum [[gnu::unused]] e; // { dg-warning "already defined" }
+
+struct [[gnu::unused]] B *p; // { dg-warning "attributes" }
+
+template <class T> struct A { };
+struct [[gnu::unused]] A<int>; // { dg-warning "attributes" }
@@ -4388,7 +4388,7 @@ comp_type_attributes (const_tree type1, const_tree type2)
const struct attribute_spec *as;
const_tree attr;
- as = lookup_attribute_spec (TREE_PURPOSE (a));
+ as = lookup_attribute_spec (get_attribute_name (a));
if (!as || as->affects_type_identity == false)
continue;
@@ -4402,7 +4402,7 @@ comp_type_attributes (const_tree type1, const_tree type2)
{
const struct attribute_spec *as;
- as = lookup_attribute_spec (TREE_PURPOSE (a));
+ as = lookup_attribute_spec (get_attribute_name (a));
if (!as || as->affects_type_identity == false)
continue;
@@ -5325,11 +5325,11 @@ private_lookup_attribute (const char *attr_name, size_t attr_len, tree list)
{
while (list)
{
- size_t ident_len = IDENTIFIER_LENGTH (TREE_PURPOSE (list));
+ size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
if (ident_len == attr_len)
{
- if (strcmp (attr_name, IDENTIFIER_POINTER (TREE_PURPOSE (list))) == 0)
+ if (strcmp (attr_name, IDENTIFIER_POINTER (get_attribute_name (list))) == 0)
break;
}
/* TODO: If we made sure that attributes were stored in the
@@ -5337,7 +5337,7 @@ private_lookup_attribute (const char *attr_name, size_t attr_len, tree list)
to '__text__') then we could avoid the following case. */
else if (ident_len == attr_len + 4)
{
- const char *p = IDENTIFIER_POINTER (TREE_PURPOSE (list));
+ const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
if (p[0] == '_' && p[1] == '_'
&& p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
&& strncmp (attr_name, p + 2, attr_len) == 0)
@@ -5367,10 +5367,10 @@ lookup_ident_attribute (tree attr_identifier, tree list)
while (list)
{
- gcc_checking_assert (TREE_CODE (TREE_PURPOSE (list)) == IDENTIFIER_NODE);
+ gcc_checking_assert (TREE_CODE (get_attribute_name (list)) == IDENTIFIER_NODE);
/* Identifiers can be compared directly for equality. */
- if (attr_identifier == TREE_PURPOSE (list))
+ if (attr_identifier == get_attribute_name (list))
break;
/* If they are not equal, they may still be one in the form
@@ -5380,11 +5380,11 @@ lookup_ident_attribute (tree attr_identifier, tree list)
the fact that we're comparing identifiers. :-) */
{
size_t attr_len = IDENTIFIER_LENGTH (attr_identifier);
- size_t ident_len = IDENTIFIER_LENGTH (TREE_PURPOSE (list));
+ size_t ident_len = IDENTIFIER_LENGTH (get_attribute_name (list));
if (ident_len == attr_len + 4)
{
- const char *p = IDENTIFIER_POINTER (TREE_PURPOSE (list));
+ const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
const char *q = IDENTIFIER_POINTER (attr_identifier);
if (p[0] == '_' && p[1] == '_'
&& p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
@@ -5393,7 +5393,7 @@ lookup_ident_attribute (tree attr_identifier, tree list)
}
else if (ident_len + 4 == attr_len)
{
- const char *p = IDENTIFIER_POINTER (TREE_PURPOSE (list));
+ const char *p = IDENTIFIER_POINTER (get_attribute_name (list));
const char *q = IDENTIFIER_POINTER (attr_identifier);
if (q[0] == '_' && q[1] == '_'
&& q[attr_len - 2] == '_' && q[attr_len - 1] == '_'
@@ -5423,7 +5423,7 @@ remove_attribute (const char *attr_name, tree list)
tree l = *p;
/* TODO: If we were storing attributes in normalized form, here
we could use a simple strcmp(). */
- if (private_is_attribute_p (attr_name, attr_len, TREE_PURPOSE (l)))
+ if (private_is_attribute_p (attr_name, attr_len, get_attribute_name (l)))
*p = TREE_CHAIN (l);
else
p = &TREE_CHAIN (l);
@@ -5460,9 +5460,9 @@ merge_attributes (tree a1, tree a2)
for (; a2 != 0; a2 = TREE_CHAIN (a2))
{
tree a;
- for (a = lookup_ident_attribute (TREE_PURPOSE (a2), attributes);
+ for (a = lookup_ident_attribute (get_attribute_name (a2), attributes);
a != NULL_TREE && !attribute_value_equal (a, a2);
- a = lookup_ident_attribute (TREE_PURPOSE (a2), TREE_CHAIN (a)))
+ a = lookup_ident_attribute (get_attribute_name (a2), TREE_CHAIN (a)))
;
if (a == NULL_TREE)
{
@@ -6384,7 +6384,7 @@ attribute_hash_list (const_tree list, hashval_t hashcode)
for (tail = list; tail; tail = TREE_CHAIN (tail))
/* ??? Do we want to add in TREE_VALUE too? */
hashcode = iterative_hash_object
- (IDENTIFIER_HASH_VALUE (TREE_PURPOSE (tail)), hashcode);
+ (IDENTIFIER_HASH_VALUE (get_attribute_name (tail)), hashcode);
return hashcode;
}
@@ -6421,7 +6421,7 @@ attribute_list_contained (const_tree l1, const_tree l2)
/* Maybe the lists are similar. */
for (t1 = l1, t2 = l2;
t1 != 0 && t2 != 0
- && TREE_PURPOSE (t1) == TREE_PURPOSE (t2)
+ && get_attribute_name (t1) == get_attribute_name (t2)
&& TREE_VALUE (t1) == TREE_VALUE (t2);
t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
;
@@ -6436,9 +6436,9 @@ attribute_list_contained (const_tree l1, const_tree l2)
/* This CONST_CAST is okay because lookup_attribute does not
modify its argument and the return value is assigned to a
const_tree. */
- for (attr = lookup_ident_attribute (TREE_PURPOSE (t2), CONST_CAST_TREE(l1));
+ for (attr = lookup_ident_attribute (get_attribute_name (t2), CONST_CAST_TREE(l1));
attr != NULL_TREE && !attribute_value_equal (t2, attr);
- attr = lookup_ident_attribute (TREE_PURPOSE (t2), TREE_CHAIN (attr)))
+ attr = lookup_ident_attribute (get_attribute_name (t2), TREE_CHAIN (attr)))
;
if (attr == NULL_TREE)
@@ -4585,7 +4585,10 @@ enum attribute_flags
/* The attributes are being applied by default to a library function whose
name indicates known behavior, and should be silently ignored if they
are not in fact compatible with the function type. */
- ATTR_FLAG_BUILT_IN = 16
+ ATTR_FLAG_BUILT_IN = 16,
+ /* A given attribute has been parsed as a C++-11 generalized
+ attribute. */
+ ATTR_FLAG_CXX_11 = 32
};
/* Default versions of target-overridable functions. */
@@ -5671,6 +5674,8 @@ extern bool must_pass_in_stack_var_size_or_pad (enum machine_mode, const_tree);
/* In attribs.c. */
extern const struct attribute_spec *lookup_attribute_spec (const_tree);
+extern const struct attribute_spec *lookup_scoped_attribute_spec (const_tree,
+ const_tree);
extern void init_attributes (void);
@@ -5684,6 +5689,12 @@ extern void init_attributes (void);
a decl attribute to the declaration rather than to its type). */
extern tree decl_attributes (tree *, tree, int);
+extern bool cxx_11_attribute_p (const_tree);
+
+extern tree get_attribute_name (const_tree);
+
+extern tree get_attribute_namespace (const_tree);
+
extern void apply_tm_attr (tree, tree);
/* In stor-layout.c */