On Mon, Mar 17, 2025 at 06:38:34PM +0100, Jakub Jelinek wrote: > Anyway, below is a patch which accepts all kinds of > ordering and mixing of standard and GNU attributes at the start of > statements (for C++).
Bootstraps/regtests (on x86_64-linux and i686-linux) found a regression on attr-fallthrough-2.c, we no longer for C++ attr-fallthrough-2.c:24:3: error: expected primary-expression before 'case' but attr-fallthrough-2.c:24:9: warning: 'unused' attribute ignored [-Wattributes] Here is updated patch which adjusts that testcase. 2025-03-18 Jakub Jelinek <ja...@redhat.com> PR c/116545 gcc/ * doc/extend.texi (musttail statement attribute): Document that musttail GNU attribute can be used as well. gcc/c-family/ * c-attribs.cc (c_common_clang_attributes): Add musttail. gcc/c/ * c-parser.cc (c_parser_declaration_or_fndef): Parse __attribute__((musttail)) return. (c_parser_handle_musttail): Diagnose attribute arguments. (c_parser_statement_after_labels): Parse __attribute__((musttail)) return. gcc/cp/ * parser.cc (cp_parser_statement): Call cp_parser_attributes_opt rather than cp_parser_std_attribute_spec_seq. (cp_parser_jump_statement): Diagnose gnu::musttail attributes with no arguments. gcc/testsuite/ * c-c++-common/attr-fallthrough-2.c: Adjust expected diagnostics for C++. * c-c++-common/musttail15.c: New test. * c-c++-common/musttail16.c: New test. * c-c++-common/musttail17.c: New test. * c-c++-common/musttail18.c: New test. * c-c++-common/musttail19.c: New test. * c-c++-common/musttail20.c: New test. * c-c++-common/musttail21.c: New test. * c-c++-common/musttail22.c: New test. * c-c++-common/musttail23.c: New test. * c-c++-common/musttail24.c: New test. * g++.dg/musttail7.C: New test. * g++.dg/musttail8.C: New test. * g++.dg/musttail12.C: New test. * g++.dg/musttail13.C: New test. * g++.dg/musttail14.C: New test. * g++.dg/ext/pr116545.C: New test. --- gcc/doc/extend.texi.jj 2025-03-14 15:29:29.954920202 +0100 +++ gcc/doc/extend.texi 2025-03-17 17:20:13.609053907 +0100 @@ -10241,18 +10241,22 @@ have to optimize it to just @code{return @cindex @code{musttail} statement attribute @item musttail -The @code{gnu::musttail} or @code{clang::musttail} attribute -can be applied to a @code{return} statement with a return-value expression -that is a function call. It asserts that the call must be a tail call that -does not allocate extra stack space, so it is safe to use tail recursion -to implement long running loops. +The @code{gnu::musttail} or @code{clang::musttail} standard attribute +or @code{musttail} GNU attribute can be applied to a @code{return} statement +with a return-value expression that is a function call. It asserts that the +call must be a tail call that does not allocate extra stack space, so it is +safe to use tail recursion to implement long running loops. @smallexample [[gnu::musttail]] return foo(); @end smallexample +@smallexample +__attribute__((musttail)) return bar(); +@end smallexample + If the compiler cannot generate a @code{musttail} tail call it will report -an error. On some targets tail calls may never be supported. +an error. On some targets tail calls may never be supported. Tail calls cannot reference locals in memory, which may affect builds without optimization when passing small structures, or passing or returning large structures. Enabling @option{-O1} or @option{-O2} can --- gcc/c-family/c-attribs.cc.jj 2025-03-14 15:29:01.974290407 +0100 +++ gcc/c-family/c-attribs.cc 2025-03-17 17:20:13.610053894 +0100 @@ -651,7 +651,9 @@ const struct scoped_attribute_specs c_co /* Attributes also recognized in the clang:: namespace. */ const struct attribute_spec c_common_clang_attributes[] = { { "flag_enum", 0, 0, false, true, false, false, - handle_flag_enum_attribute, NULL } + handle_flag_enum_attribute, NULL }, + { "musttail", 0, 0, false, false, false, + false, handle_musttail_attribute, NULL } }; const struct scoped_attribute_specs c_common_clang_attribute_table = --- gcc/c/c-parser.cc.jj 2025-03-17 17:15:32.504951278 +0100 +++ gcc/c/c-parser.cc 2025-03-17 17:20:13.613053852 +0100 @@ -1820,6 +1820,7 @@ static void c_parser_objc_at_dynamic_dec static bool c_parser_objc_diagnose_bad_element_prefix (c_parser *, struct c_declspecs *); static location_t c_parser_parse_rtl_body (c_parser *, char *); +static tree c_parser_handle_musttail (c_parser *, tree, attr_state &); #if ENABLE_ANALYZER @@ -2519,6 +2520,32 @@ c_parser_declaration_or_fndef (c_parser c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false); return result; } + else if (specs->typespec_kind == ctsk_none + && nested + /* Only parse __attribute__((musttail)) when called from + c_parser_compound_statement_nostart. This certainly isn't + a declaration in that case, but we don't do tentative parsing + of GNU attributes right now. */ + && fallthru_attr_p + && c_parser_next_token_is_keyword (parser, RID_RETURN)) + { + attr_state astate = {}; + specs->attrs = c_parser_handle_musttail (parser, specs->attrs, astate); + if (astate.musttail_p) + { + if (specs->attrs) + { + auto_urlify_attributes sentinel; + warning_at (c_parser_peek_token (parser)->location, + OPT_Wattributes, + "attribute %<musttail%> mixed with other attributes " + "on %<return%> statement"); + } + c_parser_statement_after_labels (parser, NULL, NULL_TREE, NULL, + astate); + return result; + } + } /* Provide better error recovery. Note that a type name here is usually better diagnosed as a redeclaration. */ @@ -7373,8 +7400,12 @@ c_parser_handle_musttail (c_parser *pars { if (c_parser_next_token_is_keyword (parser, RID_RETURN)) { - if (lookup_attribute ("gnu", "musttail", std_attrs)) + if (tree a = lookup_attribute ("gnu", "musttail", std_attrs)) { + for (; a; a = lookup_attribute ("gnu", "musttail", TREE_CHAIN (a))) + if (TREE_VALUE (a)) + error ("%qs attribute does not take any arguments", + "musttail"); std_attrs = remove_attribute ("gnu", "musttail", std_attrs); attr.musttail_p = true; } @@ -8237,7 +8268,8 @@ c_parser_statement_after_labels (c_parse case RID_ATTRIBUTE: { /* Allow '__attribute__((fallthrough));' or - '__attribute__((assume(cond)));'. */ + '__attribute__((assume(cond)));' or + '__attribute__((musttail))) return'. */ tree attrs = c_parser_gnu_attributes (parser); bool has_assume = lookup_attribute ("assume", attrs); if (has_assume) @@ -8252,6 +8284,20 @@ c_parser_statement_after_labels (c_parse has_assume = false; } } + gcc_assert (!astate.musttail_p); + attrs = c_parser_handle_musttail (parser, attrs, astate); + if (astate.musttail_p) + { + if (attrs) + { + auto_urlify_attributes sentinel; + warning_at (c_parser_peek_token (parser)->location, + OPT_Wattributes, + "attribute %<musttail%> mixed with other " + "attributes on %<return%> statement"); + } + goto restart; + } if (attribute_fallthrough_p (attrs)) { if (c_parser_next_token_is (parser, CPP_SEMICOLON)) --- gcc/cp/parser.cc.jj 2025-03-14 15:29:02.208287311 +0100 +++ gcc/cp/parser.cc 2025-03-17 18:02:45.867724310 +0100 @@ -12992,7 +12992,7 @@ cp_parser_statement (cp_parser* parser, c++11 attributes, or a nested objc-message-expression. So let's parse the c++11 attributes tentatively. */ cp_parser_parse_tentatively (parser); - std_attrs = cp_parser_std_attribute_spec_seq (parser); + std_attrs = cp_parser_attributes_opt (parser); if (std_attrs) attrs_loc = make_location (attrs_loc, attrs_loc, parser->lexer); if (c_dialect_objc ()) @@ -15332,8 +15332,13 @@ cp_parser_jump_statement (cp_parser* par if (keyword == RID_RETURN) { bool musttail_p = false; - if (lookup_attribute ("gnu", "musttail", std_attrs)) + if (tree a = lookup_attribute ("gnu", "musttail", std_attrs)) { + for (; a; a = lookup_attribute ("gnu", "musttail", + TREE_CHAIN (a))) + if (TREE_VALUE (a)) + error ("%qs attribute does not take any arguments", + "musttail"); musttail_p = true; std_attrs = remove_attribute ("gnu", "musttail", std_attrs); } --- gcc/testsuite/c-c++-common/attr-fallthrough-2.c.jj 2020-11-10 23:29:45.645246191 +0100 +++ gcc/testsuite/c-c++-common/attr-fallthrough-2.c 2025-03-18 08:43:51.018207925 +0100 @@ -21,8 +21,8 @@ fn (int i) case 3: bar (1); __attribute__((fallthrough)) /* { dg-warning "not followed" "" { target c } } */ - case 4: /* { dg-error "expected" } */ - bar (1); + case 4: /* { dg-error "expected" "" { target c } } */ + bar (1); /* { dg-warning "'fallthrough' attribute ignored" "" { target c++ } .-1 } */ __attribute__((fallthrough)) 1; /* { dg-error "expected" "" { target c } .-1 } */ /* { dg-warning "not followed" "" { target *-*-* } .-2 } */ --- gcc/testsuite/c-c++-common/musttail15.c.jj 2025-03-17 17:20:13.620053754 +0100 +++ gcc/testsuite/c-c++-common/musttail15.c 2025-03-17 17:20:13.620053754 +0100 @@ -0,0 +1,14 @@ +/* { dg-do compile { target { musttail && { c || c++11 } } } } */ +/* { dg-additional-options "-fdelayed-branch" { target sparc*-*-* } } */ + +int __attribute__((noinline,noclone,noipa)) +callee (int i) +{ + return i * i; +} + +int __attribute__((noinline,noclone,noipa)) +caller (int i) +{ + __attribute__((musttail)) return callee (i + 1); +} --- gcc/testsuite/c-c++-common/musttail16.c.jj 2025-03-17 17:20:13.620053754 +0100 +++ gcc/testsuite/c-c++-common/musttail16.c 2025-03-17 17:20:13.620053754 +0100 @@ -0,0 +1,33 @@ +/* { dg-do compile { target { musttail && { c || c++11 } } } } */ + +struct box { char field[256]; int i; }; + +int __attribute__((noinline,noclone,noipa)) +test_2_callee (int i, struct box b) +{ + if (b.field[0]) + return 5; + return i * i; +} + +int __attribute__((noinline,noclone,noipa)) +test_2_caller (int i) +{ + struct box b; + __attribute__((musttail)) return test_2_callee (i + 1, b); /* { dg-error "cannot tail-call: " } */ +} + +extern void setjmp (void); +void +test_3 (void) +{ + __attribute__((musttail)) return setjmp (); /* { dg-error "cannot tail-call: " } */ +} + +extern float f7(void); + +int +test_6 (void) +{ + __attribute__((musttail)) return f7(); /* { dg-error "cannot tail-call: " } */ +} --- gcc/testsuite/c-c++-common/musttail17.c.jj 2025-03-17 17:20:13.620053754 +0100 +++ gcc/testsuite/c-c++-common/musttail17.c 2025-03-17 17:20:13.620053754 +0100 @@ -0,0 +1,17 @@ +/* { dg-do compile { target { musttail && { c || c++11 } } } } */ + +struct box { char field[64]; int i; }; + +struct box __attribute__((noinline,noclone,noipa)) +returns_struct (int i) +{ + struct box b; + b.i = i * i; + return b; +} + +int __attribute__((noinline,noclone)) +test_1 (int i) +{ + __attribute__((musttail)) return returns_struct (i * 5).i; /* { dg-error "cannot tail-call: " } */ +} --- gcc/testsuite/c-c++-common/musttail18.c.jj 2025-03-17 17:20:13.620053754 +0100 +++ gcc/testsuite/c-c++-common/musttail18.c 2025-03-17 17:20:13.620053754 +0100 @@ -0,0 +1,14 @@ +/* { dg-do compile { target { musttail && { c || c++11 } } } } */ +/* { dg-additional-options "-fdelayed-branch" { target sparc*-*-* } } */ + +void __attribute__((noipa)) f() {} + +void f2() +{ + __attribute__((__musttail__)) return f2(); +} + +void f3() +{ + __attribute__((__musttail__)) return f(); +} --- gcc/testsuite/c-c++-common/musttail19.c.jj 2025-03-17 17:20:13.620053754 +0100 +++ gcc/testsuite/c-c++-common/musttail19.c 2025-03-17 17:20:13.620053754 +0100 @@ -0,0 +1,17 @@ +/* { dg-do compile { target { musttail && { c || c++11 } } } } */ + +float f1(void); + +int f2(void) +{ + __attribute__((musttail)) return f1 (); /* { dg-error "changed after call" } */ +} + + +int f3(int *); + +int f4(void) +{ + int x; + __attribute__((musttail)) return f3(&x); /* { dg-error "\(refers to locals|other reasons\)" } */ +} --- gcc/testsuite/c-c++-common/musttail20.c.jj 2025-03-17 17:20:13.620053754 +0100 +++ gcc/testsuite/c-c++-common/musttail20.c 2025-03-17 17:20:13.620053754 +0100 @@ -0,0 +1,15 @@ +/* { dg-do compile { target { struct_musttail && { c || c++11 } } } } */ +/* { dg-additional-options "-fdelayed-branch" { target sparc*-*-* } } */ + +struct str +{ + int a, b; +}; +struct str +cstruct (int x) +{ + if (x < 10) + L: + __attribute__((musttail)) return cstruct (x + 1); /* { dg-warning "'musttail' attribute ignored" "" { target c } } */ + return ((struct str){ x, 0 }); +} --- gcc/testsuite/c-c++-common/musttail21.c.jj 2025-03-17 17:20:13.620053754 +0100 +++ gcc/testsuite/c-c++-common/musttail21.c 2025-03-17 17:20:13.620053754 +0100 @@ -0,0 +1,5 @@ +/* { dg-do compile { target { c || c++11 } } } */ +void f(void) +{ + __attribute__((musttail)) return; /* { dg-error "cannot tail-call.*return value must be a call" } */ +} --- gcc/testsuite/c-c++-common/musttail22.c.jj 2025-03-17 17:20:13.620053754 +0100 +++ gcc/testsuite/c-c++-common/musttail22.c 2025-03-17 17:20:13.620053754 +0100 @@ -0,0 +1,90 @@ +/* PR tree-optimization/118430 */ +/* { dg-do compile { target musttail } } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* { dg-final { scan-tree-dump-times " \[^\n\r]* = bar \\\(\[^\n\r]*\\\); \\\[tail call\\\] \\\[must tail call\\\]" 1 "optimized" } } */ +/* { dg-final { scan-tree-dump-times " \[^\n\r]* = freddy \\\(\[^\n\r]*\\\); \\\[tail call\\\] \\\[must tail call\\\]" 1 "optimized" } } */ +/* { dg-final { scan-tree-dump-not " (?:bar|freddy) \\\(\[^\n\r]*\\\); \\\[tail call\\\]" "optimized" } } */ + +__attribute__ ((noipa)) void +foo (int x) +{ + (void) x; +} + +__attribute__ ((noinline)) int +bar (int x) +{ + foo (x); + return 1; +} + +__attribute__ ((noinline)) int +baz (int *x) +{ + foo (*x); + return 2; +} + +__attribute__((noipa)) int +qux (int x) +{ + { + int v; + foo (x); + baz (&v); + } + __attribute__((musttail)) + return bar (x); +} + +__attribute__((noipa)) int +corge (int x) +{ + { + int v; + foo (x); + baz (&v); + } + return bar (x) + 1; +} + +__attribute__ ((noinline)) float +freddy (int x) +{ + foo (x); + return 1.75f; +} + +__attribute__((noipa)) float +garply (int x) +{ + { + int v; + foo (x); + baz (&v); + } + __attribute__((musttail)) + return freddy (x); +} + +__attribute__((noipa)) float +quux (int x) +{ + { + int v; + foo (x); + baz (&v); + } + return freddy (x) + 0.25f; +} + +int v; + +int +main () +{ + qux (v); + corge (v); + garply (v); + quux (v); +} --- gcc/testsuite/c-c++-common/musttail23.c.jj 2025-03-17 17:20:13.620053754 +0100 +++ gcc/testsuite/c-c++-common/musttail23.c 2025-03-17 17:20:13.620053754 +0100 @@ -0,0 +1,45 @@ +/* { dg-do compile } */ +/* { dg-options "-W -Wall" } */ + +void bar (void); + +void +foo (int x) +{ + __attribute__((musttail)); /* { dg-warning "empty declaration" "" { target c } } */ + /* { dg-warning "attributes at the beginning of statement are ignored" "" { target c++ } .-1 } */ + if (x == 1) + __attribute__((musttail (1))) return bar (); /* { dg-error "'musttail' attribute does not take any arguments" } */ + if (x == 2) + __attribute__((musttail (1, "", 3))) return bar (); /* { dg-error "'musttail' attribute does not take any arguments" } */ + if (x == 3) + [[gnu::musttail (1)]] return bar (); /* { dg-error "'musttail' attribute does not take any arguments" } */ + /* { dg-error "expected" "" { target c } .-1 } */ + if (x == 4) + [[gnu::musttail (1, "", 3)]] return bar (); /* { dg-error "'musttail' attribute does not take any arguments" } */ + /* { dg-error "expected" "" { target c } .-1 } */ + if (x == 3) + [[clang::musttail (1)]] return bar (); /* { dg-error "'musttail' attribute does not take any arguments" } */ + /* { dg-error "expected" "" { target c } .-1 } */ + if (x == 4) + [[clang::musttail (1, "", 3)]] return bar (); /* { dg-error "'musttail' attribute does not take any arguments" } */ + /* { dg-error "expected" "" { target c } .-1 } */ + if (x == 5) + __attribute__((fallthrough, musttail)) return bar (); /* { dg-warning "attribute 'musttail' mixed with other attributes on 'return' statement" "" { target c } } */ + /* { dg-warning "attributes at the beginning of statement are ignored" "" { target c++ } .-1 } */ + + if (x == 6) + [[fallthrough]] [[gnu::musttail]] return bar (); /* { dg-warning "'fallthrough' attribute ignored" "" { target c } } */ + /* { dg-warning "attributes at the beginning of statement are ignored" "" { target c++ } .-1 } */ + if (x == 7) + [[clang::musttail, fallthrough]] return bar (); /* { dg-warning "'fallthrough' attribute ignored" "" { target c } } */ + /* { dg-warning "attributes at the beginning of statement are ignored" "" { target c++ } .-1 } */ + if (x == 8) + __attribute__((musttail, musttail)) return bar (); + if (x == 9) + [[gnu::musttail, gnu::musttail]] return bar (); + if (x == 10) + [[clang::musttail]] [[clang::musttail]] return bar (); + if (x == 11) + [[clang::musttail]] [[gnu::musttail]] return bar (); +} --- gcc/testsuite/c-c++-common/musttail24.c.jj 2025-03-17 17:20:13.620053754 +0100 +++ gcc/testsuite/c-c++-common/musttail24.c 2025-03-17 17:20:13.620053754 +0100 @@ -0,0 +1,21 @@ +/* { dg-do compile } */ +/* { dg-options "" } */ + +#if !__has_attribute (musttail) +#error missing musttail attribute +#endif +#ifdef __cplusplus +#if !__has_cpp_attribute (gnu::musttail) +#error missing gnu::musttail attribute +#endif +#if !__has_cpp_attribute (clang::musttail) +#error missing clang::musttail attribute +#endif +#else +#if !__has_c_attribute (gnu::musttail) +#error missing gnu::musttail attribute +#endif +#if !__has_c_attribute (clang::musttail) +#error missing clang::musttail attribute +#endif +#endif --- gcc/testsuite/g++.dg/musttail7.C.jj 2025-03-17 17:20:13.621053740 +0100 +++ gcc/testsuite/g++.dg/musttail7.C 2025-03-17 17:20:13.621053740 +0100 @@ -0,0 +1,60 @@ +/* { dg-do compile { target { struct_musttail } } } */ +/* { dg-require-effective-target external_musttail } */ +/* A lot of architectures will not build this due to PR115606 and PR115607 */ +/* { dg-options "-std=gnu++11" } */ +/* { dg-additional-options "-fdelayed-branch" { target sparc*-*-* } } */ + +class Foo { +public: + int a, b; + Foo(int a, int b) : a(a), b(b) {} +}; + +Foo __attribute__((noinline,noclone,noipa)) +callee (int i) +{ + return Foo(i, i+1); +} + +Foo __attribute__((noinline,noclone,noipa)) +caller (int i) +{ + __attribute__((__musttail__)) return callee (i + 1); +} + +template<typename T> +T __attribute__((noinline,noclone,noipa)) foo (T i) +{ + return i + 1; +} + +int +caller2 (int k) +{ + __attribute__((__musttail__)) return foo<int>(1); +} + +template<typename T> +T caller3 (T v) +{ + __attribute__((__musttail__)) return foo<T>(v); +} + +int call3(int i) +{ + __attribute__((__musttail__)) return caller3<int>(i + 1); +} + +struct Bar { + int a; + Bar(int a) : a(a) {} + Bar operator+(Bar o) { return Bar(a + o.a); } +}; + +#if __OPTIMIZE__ >= 1 +Bar +caller4 (Bar k) +{ + __attribute__((__musttail__)) return caller3<Bar>(Bar(99)); +} +#endif --- gcc/testsuite/g++.dg/musttail8.C.jj 2025-03-17 17:20:13.621053740 +0100 +++ gcc/testsuite/g++.dg/musttail8.C 2025-03-17 17:20:13.621053740 +0100 @@ -0,0 +1,10 @@ +/* { dg-do compile { target { musttail } } } */ +/* { dg-options "-std=gnu++11" } */ +/* { dg-additional-options "-fdelayed-branch" { target sparc*-*-* } } */ + +extern void foo(); + +void f() noexcept +{ + __attribute__((musttail)) return foo(); /* { dg-error "call may throw exception that does not propagate" } */ +} --- gcc/testsuite/g++.dg/musttail12.C.jj 2025-03-17 17:20:13.621053740 +0100 +++ gcc/testsuite/g++.dg/musttail12.C 2025-03-17 17:20:13.621053740 +0100 @@ -0,0 +1,40 @@ +/* { dg-do compile { target { musttail } } } */ +/* { dg-options "-std=gnu++11" } */ +/* { dg-additional-options "-fdelayed-branch" { target sparc*-*-* } } */ + +template <class T> T f(); + +double g() { __attribute__((musttail)) return f<int>(); } /* { dg-error "cannot tail-call" } */ + +template <class T> +__attribute__((noinline, noclone, noipa)) +T g1() { __attribute__((musttail)) return f<T>(); } /* { dg-error "target is not able" "" { target { ! external_musttail } } } */ + +template <class T> +__attribute__((noinline, noclone, noipa)) +T g2() { __attribute__((musttail)) return f<T>(); } /* { dg-error "target is not able" "" { target { ! external_musttail } } } */ + +template <class T> +__attribute__((noinline, noclone, noipa)) +/* Would work with -O1. */ +T g3() { __attribute__((musttail)) return f<T>(); } /* { dg-error "cannot tail-call" } */ + +template <class T> +__attribute__((noinline, noclone, noipa)) +T g4() { __attribute__((musttail)) return f<double>(); } /* { dg-error "cannot tail-call" } */ + +class C +{ + double x; +public: + C(double x) : x(x) {} + ~C() { asm("":::"memory"); } +}; + +int main() +{ + g1<int>(); + g2<double>(); + g3<C>(); + g4<int>(); +} --- gcc/testsuite/g++.dg/musttail13.C.jj 2025-03-17 17:20:13.621053740 +0100 +++ gcc/testsuite/g++.dg/musttail13.C 2025-03-17 17:20:13.621053740 +0100 @@ -0,0 +1,33 @@ +/* { dg-do compile { target { musttail } } } */ +/* { dg-options "-std=gnu++11" } */ +/* { dg-additional-options "-fdelayed-branch" { target sparc*-*-* } } */ + +template <class T> T f(); + +class C +{ + double x; +public: + C(double x) : x(x) {} + ~C() { asm("":::"memory"); } + operator int() { return x; } +}; + +template <class T> +__attribute__((noinline, noclone, noipa)) +T g5() { __attribute__((musttail)) return f<C> (); } /* { dg-error "cannot tail-call" } */ + +C h(); + +__attribute__((noinline, noclone, noipa)) +int g6() { __attribute__((__musttail__)) return h (); } /* { dg-error "cannot tail-call" } */ + +__attribute__((noinline, noclone, noipa)) +C g7() { __attribute__((musttail)) return h (); } /* { dg-error "cannot tail-call" } */ + +int main() +{ + g5<int> (); + g6 (); + g7 (); +} --- gcc/testsuite/g++.dg/musttail14.C.jj 2025-03-17 18:21:50.512946874 +0100 +++ gcc/testsuite/g++.dg/musttail14.C 2025-03-17 18:28:18.965583376 +0100 @@ -0,0 +1,65 @@ +// PR tree-optimization/118430 +// { dg-do compile { target musttail } } +// { dg-options "-O2 -fdump-tree-optimized" } +// { dg-final { scan-tree-dump-times " \[^\n\r]* = foo \\\(\[^\n\r]*\\\); \\\[tail call\\\] \\\[must tail call\\\]" 1 "optimized" } } */ +// { dg-final { scan-tree-dump-times " \[^\n\r]* = bar \\\(\[^\n\r]*\\\); \\\[tail call\\\] \\\[must tail call\\\]" 1 "optimized" } } */ +// { dg-final { scan-tree-dump-times " \[^\n\r]* = baz \\\(\[^\n\r]*\\\); \\\[tail call\\\] \\\[must tail call\\\]" 1 "optimized" } } */ +// { dg-final { scan-tree-dump-times " \[^\n\r]* = qux \\\(\[^\n\r]*\\\); \\\[tail call\\\] \\\[must tail call\\\]" 1 "optimized" } } */ +// { dg-final { scan-tree-dump-times " \[^\n\r]* = corge \\\(\[^\n\r]*\\\); \\\[tail call\\\] \\\[must tail call\\\]" 1 "optimized" } } */ +// { dg-final { scan-tree-dump-times " \[^\n\r]* = freddy \\\(\[^\n\r]*\\\); \\\[tail call\\\] \\\[must tail call\\\]" 1 "optimized" } } */ + +__attribute__ ((noipa)) int +foo (int x) +{ + return x; +} + +__attribute__ ((noipa)) int +bar (int x) +{ + return x; +} + +__attribute__ ((noipa)) int +baz (int x) +{ + return x; +} + +__attribute__ ((noipa)) int +qux (int x) +{ + return x; +} + +__attribute__ ((noipa)) int +corge (int x) +{ + return x; +} + +__attribute__ ((noipa)) int +freddy (int x) +{ + return x; +} + +int +garply (int x) +{ + switch (x) + { + case 0: + [[]] __attribute__(()) [[]] __attribute__((musttail)) [[]] return foo (42); + case 1: + [[]] __attribute__(()) [[gnu::musttail]] __attribute__(()) [[]] return bar (43); + case 2: + __attribute__(()) [[]] __attribute__((musttail)) [[]] __attribute__(()) return baz (44); + case 3: + __attribute__(()) [[gnu::musttail]] __attribute__(()) [[]] __attribute__(()) return qux (45); + case 4: + [[]] __attribute__(()) [[clang::musttail]] __attribute__(()) [[]] return corge (46); + default: + __attribute__(()) [[clang::musttail]] __attribute__(()) [[]] __attribute__(()) return freddy (47); + } +} --- gcc/testsuite/g++.dg/ext/pr116545.C.jj 2025-03-17 18:12:12.208918044 +0100 +++ gcc/testsuite/g++.dg/ext/pr116545.C 2025-03-17 18:17:57.430159619 +0100 @@ -0,0 +1,39 @@ +// PR c/116545 +// { dg-do compile } +// { dg-options "-Wall" } + +void +foo (bool x) +{ + [[]] __attribute__(()) [[]] __attribute__((assume (true))) [[]]; + [[]] __attribute__(()) [[assume (true)]] __attribute__(()) [[]]; + __attribute__(()) [[]] __attribute__((assume (true))) [[]] __attribute__(()); + __attribute__(()) [[assume (true)]] __attribute__(()) [[]] __attribute__(()); + if (__attribute__((assume (true))); x) // { dg-warning "init-statement in selection statements only available with" "" { target c++14_down } } + ; +} + +void +bar (int x) +{ + switch (x) + { + case 1: + ++x; + [[]] __attribute__(()) [[]] __attribute__((fallthrough)) [[]]; + case 2: + ++x; + [[]] __attribute__(()) [[fallthrough]] __attribute__(()) [[]]; + case 3: + ++x; + __attribute__(()) [[]] __attribute__((__fallthrough__)) [[]] __attribute__(()); + case 4: + ++x; + __attribute__(()) [[fallthrough]] __attribute__(()) [[]] __attribute__(()); + case 5: + ++x; + break; + default: + break; + } +} Jakub