xref: /llvm-project/clang/test/Sema/attr-noinline.cpp (revision 8995ccc4460ed8a90dcc9bd023743a8f59458f50)
1 // RUN: %clang_cc1 -verify -fsyntax-only %s -Wno-c++17-extensions
2 
3 int bar();
4 
5 // expected-note@+1 2 {{conflicting attribute is here}}
always_inline_fn(void)6 [[gnu::always_inline]] void always_inline_fn(void) { }
7 // expected-note@+1 2 {{conflicting attribute is here}}
flatten_fn(void)8 [[gnu::flatten]] void flatten_fn(void) { }
noinline_fn(void)9 [[gnu::noinline]] void noinline_fn(void) { }
10 
foo()11 void foo() {
12   [[clang::noinline]] bar();
13   [[clang::noinline(0)]] bar(); // expected-error {{'noinline' attribute takes no arguments}}
14   int x;
15   [[clang::noinline]] x = 0; // expected-warning {{'noinline' attribute is ignored because there exists no call expression inside the statement}}
16   [[clang::noinline]] { asm("nop"); } // expected-warning {{'noinline' attribute is ignored because there exists no call expression inside the statement}}
17   [[clang::noinline]] label: x = 1; // expected-warning {{'noinline' attribute only applies to functions and statements}}
18 
19 
20   [[clang::noinline]] always_inline_fn(); // expected-warning {{statement attribute 'noinline' has higher precedence than function attribute 'always_inline'}}
21   [[clang::noinline]] flatten_fn(); // expected-warning {{statement attribute 'noinline' has higher precedence than function attribute 'flatten'}}
22   [[clang::noinline]] noinline_fn();
23 
24   [[gnu::noinline]] bar(); // expected-warning {{attribute is ignored on this statement as it only applies to functions; use '[[clang::noinline]]' on statements}}
25   __attribute__((noinline)) bar(); // expected-warning {{attribute is ignored on this statement as it only applies to functions; use '[[clang::noinline]]' on statements}}
26 }
27 
ms_noi_check()28 void ms_noi_check() {
29   [[msvc::noinline]] bar();
30   [[msvc::noinline(0)]] bar(); // expected-error {{'noinline' attribute takes no arguments}}
31   int x;
32   [[msvc::noinline]] x = 0; // expected-warning {{'noinline' attribute is ignored because there exists no call expression inside the statement}}
33   [[msvc::noinline]] { asm("nop"); } // expected-warning {{'noinline' attribute is ignored because there exists no call expression inside the statement}}
34   [[msvc::noinline]] label: x = 1; // expected-warning {{'noinline' attribute only applies to functions and statements}}
35 
36   [[msvc::noinline]] always_inline_fn(); // expected-warning {{statement attribute 'noinline' has higher precedence than function attribute 'always_inline'}}
37   [[msvc::noinline]] flatten_fn(); // expected-warning {{statement attribute 'noinline' has higher precedence than function attribute 'flatten'}}
38   [[msvc::noinline]] noinline_fn();
39 }
40 
41 [[clang::noinline]] static int i = bar(); // expected-warning {{'noinline' attribute only applies to functions and statements}}
42 [[msvc::noinline]] static int j = bar(); // expected-warning {{'noinline' attribute only applies to functions and statements}}
43 
44 // This used to crash the compiler.
45 template<int D>
foo(int x)46 int foo(int x) {
47   [[clang::noinline]] return foo<D-1>(x + 1);
48 }
49 
50 template<int D>
51 [[clang::always_inline]]
dependent(int x)52 int dependent(int x){ return x + D;} // #DEP
53 [[clang::always_inline]]
non_dependent(int x)54 int non_dependent(int x){return x;} // #NO_DEP
55 
56 template<int D> [[clang::always_inline]]
baz(int x)57 int baz(int x) { // #BAZ
58   // expected-warning@+2{{statement attribute 'noinline' has higher precedence than function attribute 'always_inline'}}
59   // expected-note@#NO_DEP{{conflicting attribute is here}}
60   [[clang::noinline]] non_dependent(x);
61   if constexpr (D>0) {
62     // expected-warning@+6{{statement attribute 'noinline' has higher precedence than function attribute 'always_inline'}}
63     // expected-note@#NO_DEP{{conflicting attribute is here}}
64     // expected-warning@+4 3{{statement attribute 'noinline' has higher precedence than function attribute 'always_inline'}}
65     // expected-note@#BAZ 3{{conflicting attribute is here}}
66     // expected-note@#BAZ_INST 3{{in instantiation}}
67     // expected-note@+1 3{{in instantiation}}
68     [[clang::noinline]] return non_dependent(x), baz<D-1>(x + 1);
69   }
70   return x;
71 }
72 
73 // We can't suppress if there is a variadic involved.
74 template<int ... D>
variadic_baz(int x)75 int variadic_baz(int x) {
76   // Diagnoses NO_DEP 2x, once during phase 1, the second during instantiation.
77   // Dianoses DEP 3x, once per variadic expansion.
78   // expected-warning@+5 2{{statement attribute 'noinline' has higher precedence than function attribute 'always_inline'}}
79   // expected-note@#NO_DEP 2{{conflicting attribute is here}}
80   // expected-warning@+3 3{{statement attribute 'noinline' has higher precedence than function attribute 'always_inline'}}
81   // expected-note@#DEP 3{{conflicting attribute is here}}
82   // expected-note@#VARIADIC_INST{{in instantiation}}
83   [[clang::noinline]] return non_dependent(x) + (dependent<D>(x) + ...);
84 }
85 
86 template<int D> [[clang::always_inline]]
qux(int x)87 int qux(int x) { // #QUX
88   // expected-warning@+2{{statement attribute 'noinline' has higher precedence than function attribute 'always_inline'}}
89   // expected-note@#NO_DEP{{conflicting attribute is here}}
90   [[msvc::noinline]] non_dependent(x);
91   if constexpr (D>0) {
92     // expected-warning@+6{{statement attribute 'noinline' has higher precedence than function attribute 'always_inline'}}
93     // expected-note@#NO_DEP{{conflicting attribute is here}}
94     // expected-warning@+4 3{{statement attribute 'noinline' has higher precedence than function attribute 'always_inline'}}
95     // expected-note@#QUX 3{{conflicting attribute is here}}
96     // expected-note@#QUX_INST 3{{in instantiation}}
97     // expected-note@+1 3{{in instantiation}}
98     [[msvc::noinline]] return non_dependent(x), qux<D-1>(x + 1);
99   }
100   return x;
101 }
102 
103 // We can't suppress if there is a variadic involved.
104 template<int ... D>
variadic_qux(int x)105 int variadic_qux(int x) {
106   // Diagnoses NO_DEP 2x, once during phase 1, the second during instantiation.
107   // Dianoses DEP 3x, once per variadic expansion.
108   // expected-warning@+5 2{{statement attribute 'noinline' has higher precedence than function attribute 'always_inline'}}
109   // expected-note@#NO_DEP 2{{conflicting attribute is here}}
110   // expected-warning@+3 3{{statement attribute 'noinline' has higher precedence than function attribute 'always_inline'}}
111   // expected-note@#DEP 3{{conflicting attribute is here}}
112   // expected-note@#QUX_VARIADIC_INST{{in instantiation}}
113   [[msvc::noinline]] return non_dependent(x) + (dependent<D>(x) + ...);
114 }
115 
use()116 void use() {
117   baz<3>(0); // #BAZ_INST
118   variadic_baz<0, 1, 2>(0); // #VARIADIC_INST
119   qux<3>(0); // #QUX_INST
120   variadic_qux<0, 1, 2>(0); // #QUX_VARIADIC_INST
121 }
122