[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:52 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);
+
+struct FunctionExitResult {
+  cir::FuncOp func;
+  FunctionExitKind kind = FunctionExitKind::Unknown;
+};
+
+/// analyzeFunctionExits over the definitions `filter` accepts (all if null), in
+/// parallel when allowed: `filter` may run concurrently. In module order.
+/// Each result is also an analysis remark in the "function-exit" category.
+llvm::SmallVector<FunctionExitResult>
+analyzeFunctionExits(mlir::ModuleOp module,
+                     llvm::function_ref<bool(cir::FuncOp)> filter = nullptr);
----------------
xlauko wrote:

Nothing in this PR passes `filter`. Drop it until a caller needs it? Then `kinds` doesn't need `std::optional`.

https://github.com/llvm/llvm-project/pull/239774


More information about the cfe-commits mailing list