[clang] [LifetimeSafety] Add support for iterator arithmetic (PR #195442)

Utkarsh Saxena via cfe-commits cfe-commits at lists.llvm.org
Sun May 3 11:24:23 PDT 2026


================
@@ -542,6 +542,23 @@ void FactsGenerator::VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *OCE) {
     }
   }
 
+  if (OCE->getNumArgs() < 3 && isPropagatingIteratorOp(OCE->getOperator())) {
+    const Expr *IteratorArg = nullptr;
+    for (const Expr *Arg : OCE->arguments()) {
+      if (isIteratorType(Arg->getType()->getAsCXXRecordDecl())) {
+        IteratorArg = Arg;
+        break;
+      }
+    }
+
+    if (IteratorArg) {
+      flow(getOriginsList(*OCE),
+           getRValueOrigins(IteratorArg, getOriginsList(*IteratorArg)),
----------------
usx95 wrote:

Can you add tests for the following cases:

- `next = (it + 1) + 2`
- `next = it + 1 + 2 + 3`
- `it = it + 1`
- `it = ++it + 1 + 2`
- `it = v.begin() + 1`
- `it = std::begin(v) + 1`



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


More information about the cfe-commits mailing list