================
@@ -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
_______________________________________________
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to