[llvm-branch-commits] [clang] [Clang][OBT] Add overflow handler diagnostics (PR #235047)

Ambrose Leeb via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Oct 23 11:46:28 PDT 2026


================
@@ -672,6 +673,131 @@ void Sema::NoteOverflowCheckedOps(Expr *E) {
     Handler->setReferenced();
 }
 
+namespace {
+/// Walks a function body, including nested lambda and block bodies, and
+/// diagnoses overflow-checked operations with no visible overflow handler.
+class OverflowHandlerDiagnoser {
+  Sema &S;
+  const IdentifierInfo *Name;
+  /// Handlers visible in the current function body, innermost last. The first
+  /// entry is the function-level handler and may be null.
+  SmallVector<LabelDecl *, 4> Handlers;
+  /// Matches the %select in warn_overflow_behavior_unreachable_handler.
+  enum NestedBodyKind { NB_Lambda, NB_Block };
+  /// Innermost handler of an enclosing function, when walking a nested body.
+  const LabelDecl *EnclosingHandler = nullptr;
+  NestedBodyKind NestedKind = NB_Lambda;
+  /// Set while walking the operands of a diagnosed operation, so that one
+  /// expression yields one warning.
+  bool InDiagnosedOp = false;
+
+public:
+  OverflowHandlerDiagnoser(Sema &S)
+      : S(S), Name(getOverflowHandlerName(S.Context)) {}
+
+  void visitBody(const Decl *D, const Stmt *Body) {
+    Handlers.push_back(findFunctionOverflowHandler(S.Context, D));
+    visit(Body);
+    Handlers.pop_back();
+  }
+
+private:
+  void visitNestedBody(const Decl *D, const Stmt *Body, NestedBodyKind Kind) {
+    const LabelDecl *Outer = Handlers.back();
+    SaveAndRestore SaveHandlers(Handlers, {});
+    SaveAndRestore SaveEnclosing(EnclosingHandler,
+                                 Outer ? Outer : EnclosingHandler);
+    SaveAndRestore SaveKind(NestedKind, Kind);
+    SaveAndRestore SaveInDiagnosedOp(InDiagnosedOp, false);
+    visitBody(D, Body);
+  }
+
+  void visit(const Stmt *St) {
----------------
Sirraide wrote:

It’s a bit weird to me that this isn’t using DynamicRecursiveASTVisitor. Is there a reason for that?

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


More information about the llvm-branch-commits mailing list