Skip to content

Commit 2236af8

Browse files
chqrliebvdberg
authored andcommitted
Analyser: detect and report unused recursive functions
1 parent 676313c commit 2236af8

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

analyser/module_analyser_call.c2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ fn QualType Analyser.analyseCallExpr(Analyser* ma, Expr** e_ptr) {
5656
*/
5757

5858
FunctionDecl* fd = ft.getDecl();
59-
fd.asDecl().setUsed();
59+
if (fd != ma.curFunction) fd.asDecl().setUsed();
6060
if (fd.hasAttrNoReturn()) call.setNoreturn();
6161

6262
if (fd.isTemplate()) {
@@ -702,7 +702,7 @@ fn QualType Analyser.analysePureCallExpr(Analyser* ma, Expr* e) {
702702
}
703703

704704
FunctionDecl* fd = ft.getDecl();
705-
fd.asDecl().setUsed();
705+
if (fd != ma.curFunction) fd.asDecl().setUsed();
706706

707707
if (!fd.hasAttrPure()) {
708708
ma.error(e.getLoc(), "only pure functions can be called in global initializers");

analyser/module_analyser_expr.c2

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,9 @@ fn Decl* Analyser.analyseIdentifier(Analyser* ma, Expr** e_ptr, u32 side) {
154154
e.setType(qt);
155155
i.setDecl(d);
156156

157-
if (side & RHS || side == 0) d.setUsed();
158-
else {
157+
if (side & RHS || side == 0) {
158+
if (d != (Decl*)ma.curFunction) d.setUsed();
159+
} else {
159160
// parameters are used if LHS|RHS
160161
if (d.isVarDecl()) {
161162
VarDecl* vd = (VarDecl*)d;

test/functions/unused_recursive.c2

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module test;
2+
3+
fn i32 fib(i32 n) { // @warning{unused function 'test.fib'}
4+
return n <= 2 ? 1 : fib(n - 1) + fib(n - 2);
5+
}
6+
7+
public fn i32 main() {
8+
return 1;
9+
}

0 commit comments

Comments
 (0)