Debian Codegraph @

gcc/testsuite/g++.dg/ext/attr-const-pure.C

raw
1 // Bug c++/83503 - bogus -Wattributes for const and pure on function template
2 // specialization
3 // { dg-do compile }
4 // { dg-options "-O -Wall -fdump-tree-optimized" }
5
6 int global;
7
8 template <class T>
9 int __attribute__ ((pure))
10 f (T);
11
12 template <>
13 int __attribute__ ((const)) f<int> (int); // { dg-bogus "ignoring attribute .const." }
14
15 void f_pure_primary_elim ();
16 void f_pure_primary_keep ();
17 void f_const_spec_elim ();
18
19 void call_pure_primary_elim (double x)
20 {
21 // Only the first call to f(x) must be emitted, the second one
22 // is expected to be eliminated because the primary template
23 // is pure.
24 int i0 = f (x);
25 int i1 = f (x);
26 if (i0 != i1)
27 f_pure_primary_elim ();
28 }
29
30 void call_pure_primary_keep (const char *s)
31 {
32 // Both calls to f(x) must be emitted because the primary is
33 // pure and may read global.
34 int i0 = f (s);
35 global = 123;
36 int i1 = f (s);
37 if (i0 != i1)
38 f_pure_primary_keep ();
39 }
40
41 void call_const_spec_elim (int i)
42 {
43 // Only the first call to f(x) must be emitted, the second
44 // one is expected to be eliminated again, this time because
45 // unlike the pure primary, the specialization is const.
46 int i0 = f (i);
47 global = 123;
48 int i1 = f (i);
49 if (i0 != i1)
50 f_const_spec_elim ();
51 }
52
53 template <class T>
54 int __attribute__ ((const))
55 g (T);
56
57 template <>
58 int __attribute__ ((pure)) g<int> (int); // { dg-bogus "ignoring attribute .const." }
59
60 template <class T>
61 int __attribute__ ((const))
62 h (T);
63
64 template <class T>
65 int __attribute__ ((pure))
66 h (const T*);
67
68 template <>
69 int h<int> (int);
70
71 template <>
72 int h<int*> (int*);
73
74 extern void h_const_primary_elim ();
75 extern void h_pure_cstptr_elim ();
76 extern void h_cstptr_keep ();
77 extern void h_int_keep ();
78 extern void h_intptr_keep ();
79
80 void call_const_primary_elim (double x)
81 {
82 // Only the first call to h(x) must be emitted, the second one
83 // is expected to be eliminated.
84 int i0 = h (x);
85 int i1 = h (x);
86
87 if (i0 != i1) // must be folded into false
88 h_const_primary_elim (); // must be eliminated
89 }
90
91 void call_pure_cstptr_elim (const void *p)
92 {
93 // Only the first call to h(x) must be emitted, the second one
94 // is expected to be eliminated. This verifies that h<const
95 // void*>*() is treated as const in this context.
96 int i0 = h (p);
97 int i1 = h (p);
98
99 if (i0 != i1) // must be folded into false
100 h_pure_cstptr_elim (); // must be eliminated
101 }
102
103 void call_cstptr_keep (const void *p)
104 {
105 // Because of the store to the global, both calls to h(p) must
106 // be emitted. This verifies that h<const void*>*() is not
107 // treated as const.
108 int i0 = h (p);
109 global = 123;
110 int i1 = h (p);
111
112 if (i0 != i1) // must not be folded
113 h_cstptr_keep (); // must be emitted
114 }
115
116 void call_int_keep (int i)
117 {
118 // Both calls to h(i) must be emitted.
119 int i0 = h (i);
120 int i1 = h (i);
121
122 if (i0 != i1) // must not be folded
123 h_int_keep (); // must be emitted
124 }
125
126 void call_intptr_keep (int *ip)
127 {
128 // Both calls to h(ip) must be emitted.
129 int i0 = h (ip);
130 int i1 = h (ip);
131
132 if (i0 != i1) // must not be folded
133 h_intptr_keep (); // must be emitted
134 }
135
136 // { dg-final { scan-tree-dump-not "f_pure_primary_elim" "optimized" } }
137 // { dg-final { scan-tree-dump-not "f_const_primary_elim" "optimized" } }
138 // { dg-final { scan-tree-dump-not "f_const_spec_elim" "optimized" } }
139 // { dg-final { scan-tree-dump-not "h_pure_cstptr_elim" "optimized" } }
140
141 // { dg-final { scan-tree-dump-times "f_pure_primary_keep" 1 "optimized" } }
142 // { dg-final { scan-tree-dump-times "h_cstptr_keep" 1 "optimized" } }
143 // { dg-final { scan-tree-dump-times "h_int_keep" 1 "optimized" } }
144 // { dg-final { scan-tree-dump-times "h_intptr_keep" 1 "optimized" } }