warn-infinite-recursion.cpp
2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// RUN: %clang_cc1 %s -fsyntax-only -verify -Winfinite-recursion
void a() { // expected-warning{{call itself}}
a();
}
void b(int x) { // expected-warning{{call itself}}
if (x)
b(x);
else
b(x+1);
}
void c(int x) {
if (x)
c(5);
}
void d(int x) { // expected-warning{{call itself}}
if (x)
++x;
return d(x);
}
// Doesn't warn on mutually recursive functions
void e();
void f();
void e() { f(); }
void f() { e(); }
void g() { // expected-warning{{call itself}}
while (true)
g();
g();
}
void h(int x) {
while (x < 5) {
h(x+1);
}
}
void i(int x) { // expected-warning{{call itself}}
while (x < 5) {
--x;
}
i(0);
}
int j() { // expected-warning{{call itself}}
return 5 + j();
}
// Don't warn on infinite loops
void k() {
while(true) {
k();
}
}
void l() {
while (true) {}
l();
}
void m() {
static int count = 5;
if (count >0) {
count--;
l();
}
while (true) {}
}
class S {
static void a();
void b();
};
void S::a() { // expected-warning{{call itself}}
return a();
}
void S::b() { // expected-warning{{call itself}}
int i = 0;
do {
++i;
b();
} while (i > 5);
}
template<class member>
struct T {
member m;
void a() { return a(); } // expected-warning{{call itself}}
static void b() { return b(); } // expected-warning{{call itself}}
};
void test_T() {
T<int> foo;
foo.a(); // expected-note{{in instantiation}}
foo.b(); // expected-note{{in instantiation}}
}
class U {
U* u;
void Fun() { // expected-warning{{call itself}}
u->Fun();
}
};
// No warnings on templated functions
// sum<0>() is instantiated, does recursively call itself, but never runs.
template <int value>
int sum() {
return value + sum<value/2>();
}
template<>
int sum<1>() { return 1; }
template<int x, int y>
int calculate_value() {
if (x != y)
return sum<x - y>(); // This instantiates sum<0>() even if never called.
else
return 0;
}
int value = calculate_value<1,1>();
void DoSomethingHere();
// DoStuff<0,0>() is instantiated, but never called.
template<int First, int Last>
int DoStuff() {
if (First + 1 == Last) {
// This branch gets removed during <0, 0> instantiation in so CFG for this
// function goes straight to the else branch.
DoSomethingHere();
} else {
DoStuff<First, (First + Last)/2>();
DoStuff<(First + Last)/2, Last>();
}
return 0;
}
int stuff = DoStuff<0, 1>();
template<int x>
struct Wrapper {
static int run() {
// Similar to the above, Wrapper<0>::run() will discard the if statement.
if (x == 1)
return 0;
return Wrapper<x/2>::run();
}
static int run2() { // expected-warning{{call itself}}
return run2();
}
};
template <int x>
int test_wrapper() {
if (x != 0)
return Wrapper<x>::run() +
Wrapper<x>::run2(); // expected-note{{instantiation}}
return 0;
}
int wrapper_sum = test_wrapper<2>(); // expected-note{{instantiation}}