[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
Tue Dec 1 12:47:51 PST 2026
================
@@ -0,0 +1,68 @@
+//===- CIRFunctionExitAnalysis.h - How control leaves a function ----------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file declares an analysis that classifies how control can leave a
+// function: through an explicit return, by reaching the end of the body, or
+// not at all. It is the CIR counterpart of Sema's CFG-based CheckFallThrough
+// behind -Wreturn-type, and runs on CIRGen output, where the terminator emitted
+// at the end of a body is marked `implicit`.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef CLANG_CIR_DIALECT_ANALYSIS_CIRFUNCTIONEXITANALYSIS_H
+#define CLANG_CIR_DIALECT_ANALYSIS_CIRFUNCTIONEXITANALYSIS_H
+
+#include "clang/CIR/Dialect/IR/CIRDialect.h"
+#include "llvm/ADT/STLFunctionalExtras.h"
+#include "llvm/ADT/SmallVector.h"
+
+namespace mlir {
+class ModuleOp;
+} // namespace mlir
+
+namespace cir {
+
+/// How control can leave a function. Mirrors Sema's ControlFlowKind.
+enum class FunctionExitKind {
+ /// Not analyzed, e.g. no body.
+ Unknown,
+ /// Returns; never reaches the end of the body.
+ ReturnOnly,
+ /// Neither returns nor reaches the end.
+ NoReturn,
+ /// Reaches the end on some paths only.
+ MayReachEnd,
+ /// Leaves only by reaching the end.
+ MustReachEnd,
+};
+
+llvm::StringRef stringifyFunctionExitKind(FunctionExitKind kind);
+
+/// Classify how control can leave `func`.
+FunctionExitKind analyzeFunctionExits(cir::FuncOp func);
+
+/// A cheap, conservative check of whether control may reach the end of
+/// `func`'s body. When false, analyzeFunctionExits(func) is ReturnOnly or
+/// NoReturn.
+bool mayReachEnd(cir::FuncOp func);
----------------
xlauko wrote:
The only caller of `mayReachEnd` is the `EXPENSIVE_CHECKS` assert, so the pre-check the description mentions never runs and is untested in normal builds. Could it move to the Sema PR, along with `mayComplete`, `mayReachPast` and `mayCompleteWhenEntered`?
https://github.com/llvm/llvm-project/pull/239774
More information about the cfe-commits
mailing list