[clang] [CIR] -Wreturn-type (1/2): Add an analysis classifying how control reaches a function's end (PR #239774)
Henrich Lauko via cfe-commits
cfe-commits at lists.llvm.org
Fri Dec 4 04:53:37 PST 2026
================
@@ -0,0 +1,316 @@
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fcxx-exceptions -fexceptions -fclangir -emit-cir -clangir-disable-passes -Wno-return-type %s -o %t.cir
+// RUN: cir-opt %t.cir -test-cir-function-exit-analysis --remarks-filter=function-exit -o /dev/null 2>&1 | FileCheck %s
+
+// At -O1 the implicit terminator is cir.unreachable rather than cir.trap.
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fcxx-exceptions -fexceptions -O1 -fclangir -emit-cir -clangir-disable-passes -Wno-return-type %s -o %t-O1.cir
+// RUN: cir-opt %t-O1.cir -test-cir-function-exit-analysis --remarks-filter=function-exit -o /dev/null 2>&1 | FileCheck %s
+
+// The warning annotations are what Sema's CFG-based check reports
+// for the same code, so both classifications are pinned to each other.
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fcxx-exceptions -fexceptions -fsyntax-only -Wreturn-type -verify %s
+
+[[noreturn]] void die();
+int g();
+
+// CHECK-DAG: function-exit.cpp:[[@LINE+1]]:1: remark: [Analysis] function-exit | Category:function-exit | Kind=must_reach_end, Remark="control leaves only by reaching the end of the body"
+int must_reach_end(int x) {
+ x++;
+} // expected-warning {{non-void function does not return a value}}
+
+// CHECK-DAG: function-exit.cpp:[[@LINE+1]]:1: remark: [Analysis] function-exit | Category:function-exit | Kind=may_reach_end, Remark="control reaches the end of the body on some paths"
+int may_reach_end(int x) {
+ if (x)
+ return 1;
+} // expected-warning {{non-void function does not return a value in all control paths}}
+
+// CHECK-DAG: function-exit.cpp:[[@LINE+1]]:1: remark: [Analysis] function-exit | Category:function-exit | Kind=return_only, Remark="control returns but never reaches the end of the body"
+int return_only(int x) {
+ if (x)
+ return 1;
+ return 0;
+}
+
+// CHECK-DAG: function-exit.cpp:[[@LINE+1]]:1: remark: [Analysis] function-exit | Category:function-exit | Kind=return_only, Remark="control returns but never reaches the end of the body"
+int noreturn_call(int x) {
+ if (x)
+ return 1;
+ die();
+}
+
+// CHECK-DAG: function-exit.cpp:[[@LINE+1]]:1: remark: [Analysis] function-exit | Category:function-exit | Kind=no_return, Remark="control never returns or reaches the end of the body"
+int only_noreturn_call() {
+ die();
+}
+
+// CHECK-DAG: function-exit.cpp:[[@LINE+1]]:1: remark: [Analysis] function-exit | Category:function-exit | Kind=return_only, Remark="control returns but never reaches the end of the body"
+int user_unreachable(int x) {
+ if (x)
+ return 1;
+ __builtin_unreachable();
+}
+
+// CHECK-DAG: function-exit.cpp:[[@LINE+1]]:1: remark: [Analysis] function-exit | Category:function-exit | Kind=return_only, Remark="control returns but never reaches the end of the body"
+int user_trap(int x) {
+ if (x)
+ return 1;
+ __builtin_trap();
+}
+
+// CHECK-DAG: function-exit.cpp:[[@LINE+1]]:1: remark: [Analysis] function-exit | Category:function-exit | Kind=may_reach_end, Remark="control reaches the end of the body on some paths"
+int noreturn_on_some_path(int x) {
+ if (x)
+ die();
+} // expected-warning {{non-void function does not return a value in all control paths}}
+
+// CHECK-DAG: function-exit.cpp:[[@LINE+1]]:1: remark: [Analysis] function-exit | Category:function-exit | Kind=return_only, Remark="control returns but never reaches the end of the body"
+int constant_condition() {
+ if (true)
+ return 1;
+}
+
+// CHECK-DAG: function-exit.cpp:[[@LINE+1]]:1: remark: [Analysis] function-exit | Category:function-exit | Kind=no_return, Remark="control never returns or reaches the end of the body"
+int infinite_loop() {
+ while (true) {
+ }
+}
+
+// CHECK-DAG: function-exit.cpp:[[@LINE+1]]:1: remark: [Analysis] function-exit | Category:function-exit | Kind=must_reach_end, Remark="control leaves only by reaching the end of the body"
+int infinite_loop_with_break(int x) {
+ while (true) {
+ if (x)
+ break;
+ }
+} // expected-warning {{non-void function does not return a value}}
+
+// CHECK-DAG: function-exit.cpp:[[@LINE+1]]:1: remark: [Analysis] function-exit | Category:function-exit | Kind=return_only, Remark="control returns but never reaches the end of the body"
+int for_ever(int x) {
+ for (;;) {
+ if (x)
+ return 1;
+ }
+}
+
+// Constant comparisons in loop conditions fold.
+
+// CHECK-DAG: function-exit.cpp:[[@LINE+1]]:1: remark: [Analysis] function-exit | Category:function-exit | Kind=return_only, Remark="control returns but never reaches the end of the body"
+int while_const_cmp(int x) {
+ while (1 == 1) {
+ if (x)
+ return 1;
+ }
+}
+
+// CHECK-DAG: function-exit.cpp:[[@LINE+1]]:1: remark: [Analysis] function-exit | Category:function-exit | Kind=return_only, Remark="control returns but never reaches the end of the body"
+int for_sizeof_cmp(int x) {
+ for (; sizeof(int) == 4;) {
+ if (x)
+ return 1;
+ }
+}
+
+// CHECK-DAG: function-exit.cpp:[[@LINE+1]]:1: remark: [Analysis] function-exit | Category:function-exit | Kind=return_only, Remark="control returns but never reaches the end of the body"
+int do_while_const_cmp(int x) {
+ do {
+ if (x)
+ return 1;
+ } while (2 > 1);
+}
+
+// CHECK-DAG: function-exit.cpp:[[@LINE+1]]:1: remark: [Analysis] function-exit | Category:function-exit | Kind=may_reach_end, Remark="control reaches the end of the body on some paths"
+int while_may_not_run(int x) {
+ while (x)
+ return 1;
+} // expected-warning {{non-void function does not return a value in all control paths}}
+
+// CHECK-DAG: function-exit.cpp:[[@LINE+1]]:1: remark: [Analysis] function-exit | Category:function-exit | Kind=return_only, Remark="control returns but never reaches the end of the body"
+int do_while_runs_once(int x) {
+ do {
+ return 1;
+ } while (x);
+}
+
+// CHECK-DAG: function-exit.cpp:[[@LINE+1]]:1: remark: [Analysis] function-exit | Category:function-exit | Kind=may_reach_end, Remark="control reaches the end of the body on some paths"
+int continue_reaches_exit(int x) {
+ for (int i = 0; i < x; ++i) {
+ if (i == 3)
+ continue;
+ return i;
+ }
+} // expected-warning {{non-void function does not return a value in all control paths}}
+
+// CHECK-DAG: function-exit.cpp:[[@LINE+1]]:1: remark: [Analysis] function-exit | Category:function-exit | Kind=return_only, Remark="control returns but never reaches the end of the body"
+int switch_with_default(int x) {
+ switch (x) {
+ case 0:
+ return 0;
+ default:
+ return 1;
+ }
+}
+
+// CHECK-DAG: function-exit.cpp:[[@LINE+1]]:1: remark: [Analysis] function-exit | Category:function-exit | Kind=may_reach_end, Remark="control reaches the end of the body on some paths"
+int switch_without_default(int x) {
+ switch (x) {
+ case 0:
+ return 0;
+ case 1:
+ return 1;
+ }
+} // expected-warning {{non-void function does not return a value in all control paths}}
+
+enum E { A, B };
+
+// CHECK-DAG: function-exit.cpp:[[@LINE+1]]:1: remark: [Analysis] function-exit | Category:function-exit | Kind=return_only, Remark="control returns but never reaches the end of the body"
+int switch_covers_enum(E e) {
+ switch (e) {
+ case A:
+ return 0;
+ case B:
+ return 1;
+ }
+}
+
+// CHECK-DAG: function-exit.cpp:[[@LINE+1]]:1: remark: [Analysis] function-exit | Category:function-exit | Kind=may_reach_end, Remark="control reaches the end of the body on some paths"
+int switch_break(int x) {
+ switch (x) {
+ case 0:
+ break;
+ default:
+ return 1;
+ }
+} // expected-warning {{non-void function does not return a value in all control paths}}
+
+// A switch dispatches to cases nested anywhere in its body, even after dead
+// code.
+// CHECK-DAG: function-exit.cpp:[[@LINE+1]]:1: remark: [Analysis] function-exit | Category:function-exit | Kind=may_reach_end, Remark="control reaches the end of the body on some paths"
+int switch_nested_case_after_dead_code(int x) {
+ switch (x) {
+ return 0;
+ {
+ case 1:
+ return 1;
+ }
+ }
+} // expected-warning {{non-void function does not return a value in all control paths}}
+
+// CHECK-DAG: function-exit.cpp:[[@LINE+1]]:1: remark: [Analysis] function-exit | Category:function-exit | Kind=return_only, Remark="control returns but never reaches the end of the body"
+int switch_nested_default_after_dead_code(int x) {
+ switch (x) {
+ return 5;
+ {
+ default:
+ return 1;
+ }
+ }
+}
+
+// CHECK-DAG: function-exit.cpp:[[@LINE+1]]:1: remark: [Analysis] function-exit | Category:function-exit | Kind=may_reach_end, Remark="control reaches the end of the body on some paths"
+int switch_case_in_loop(int x, int n) {
+ switch (x) {
+ case 0:
+ do {
+ n--;
+ case 1:
+ n--;
+ } while (n > 0);
+ return n;
+ }
+} // expected-warning {{non-void function does not return a value in all control paths}}
+
+// CHECK-DAG: function-exit.cpp:[[@LINE+1]]:1: remark: [Analysis] function-exit | Category:function-exit | Kind=return_only, Remark="control returns but never reaches the end of the body"
+int switch_fallthrough(int x) {
+ switch (x) {
+ case 0:
+ x++;
+ [[fallthrough]];
+ default:
+ return x;
+ }
+}
+
+// CHECK-DAG: function-exit.cpp:[[@LINE+1]]:1: remark: [Analysis] function-exit | Category:function-exit | Kind=return_only, Remark="control returns but never reaches the end of the body"
----------------
xlauko wrote:
Nit: matching `Kind=return_only` alone would check the property under test and keep the CHECK lines short.
https://github.com/llvm/llvm-project/pull/239774
More information about the cfe-commits
mailing list