Skip to content

Commit 61c74c0

Browse files
committed
c-family: Allow placement of attributes on function definitions.
This is to address Issue gcc-mirror#12. It is very unlikely to be acceptable upstream, because the situation with how GNU attributes appertain is already very complex. This is strictly an extension to allow us to consume them in the same position as clang does. gcc/c-family/ChangeLog: * c.opt: gcc/c/ChangeLog: * c-parser.cc (c_parser_declaration_or_fndef): gcc/ChangeLog: * config/darwin.cc (darwin_override_options): gcc/cp/ChangeLog: * parser.cc (cp_parser_init_declarator): Signed-off-by: Iain Sandoe <[email protected]>
1 parent 0710024 commit 61c74c0

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

gcc/c-family/c.opt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,6 +1805,10 @@ static-libmpxwrappers
18051805
Driver WarnRemoved
18061806
Removed in GCC 9. This switch has no effect.
18071807

1808+
fallow-extended-attribute-placement
1809+
C ObjC C++ ObjC++ LTO Var(flag_allow_ext_attr_placement) Init(0)
1810+
Allow placement of attributes on function definitions.
1811+
18081812
fcilkplus
18091813
C ObjC C++ ObjC++ LTO Undocumented Ignore
18101814
Removed in GCC 8. This switch has no effect.

gcc/c/c-parser.cc

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2691,15 +2691,28 @@ c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
26912691
d = d->declarator;
26922692
underspec_name = d->u.id.id;
26932693
}
2694+
tree postfix_attrs = NULL_TREE;
2695+
if (flag_allow_ext_attr_placement
2696+
&& c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2697+
{
2698+
postfix_attrs = c_parser_gnu_attributes (parser);
2699+
/* IF we have a function definition, and we're allowing it then
2700+
treat these attributes as if they had been prepended. */
2701+
if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2702+
{
2703+
all_prefix_attrs = chainon (all_prefix_attrs, postfix_attrs);
2704+
postfix_attrs = NULL_TREE;
2705+
}
2706+
}
26942707
if (c_parser_next_token_is (parser, CPP_EQ)
26952708
|| c_parser_next_token_is (parser, CPP_COMMA)
26962709
|| c_parser_next_token_is (parser, CPP_SEMICOLON)
26972710
|| c_parser_next_token_is_keyword (parser, RID_ASM)
26982711
|| c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
2712+
|| postfix_attrs
26992713
|| c_parser_next_token_is_keyword (parser, RID_IN))
27002714
{
27012715
tree asm_name = NULL_TREE;
2702-
tree postfix_attrs = NULL_TREE;
27032716
if (!diagnosed_no_specs && !specs->declspecs_seen_p)
27042717
{
27052718
diagnosed_no_specs = true;
@@ -2711,8 +2724,9 @@ c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
27112724
if (c_parser_next_token_is_keyword (parser, RID_ASM))
27122725
asm_name = c_parser_simple_asm_expr (parser);
27132726
if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2727+
postfix_attrs = c_parser_gnu_attributes (parser);
2728+
if (postfix_attrs)
27142729
{
2715-
postfix_attrs = c_parser_gnu_attributes (parser);
27162730
if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
27172731
{
27182732
/* This means there is an attribute specifier after

gcc/config/darwin.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3592,6 +3592,11 @@ darwin_override_options (void)
35923592

35933593
/* The c_dialect...() macros are not available to us here. */
35943594
darwin_running_cxx = (strstr (lang_hooks.name, "C++") != 0);
3595+
3596+
/* We need to consume attributes on function definitions from the the SDK
3597+
headers. */
3598+
if (!OPTION_SET_P (flag_allow_ext_attr_placement))
3599+
flag_allow_ext_attr_placement = true;
35953600
}
35963601

35973602
#if DARWIN_PPC

gcc/cp/parser.cc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23966,9 +23966,16 @@ cp_parser_init_declarator (cp_parser* parser,
2396623966
"an %<asm%> specification is not allowed "
2396723967
"on a function-definition");
2396823968
if (attributes)
23969-
error_at (attributes_start_token->location,
23970-
"attributes are not allowed "
23971-
"on a function-definition");
23969+
{
23970+
/* When we are allowing attributes in this position, then add
23971+
them to the prefix ones. */
23972+
if (flag_allow_ext_attr_placement)
23973+
prefix_attributes = chainon (prefix_attributes, attributes);
23974+
else
23975+
error_at (attributes_start_token->location,
23976+
"attributes are not allowed "
23977+
"on a function-definition");
23978+
}
2397223979
/* This is a function-definition. */
2397323980
*function_definition_p = true;
2397423981

0 commit comments

Comments
 (0)