[clang] [LifetimeSafety] Introduce buildOriginFlowChain for use-after-scope (PR #199345)

Utkarsh Saxena via cfe-commits cfe-commits at lists.llvm.org
Sun May 24 10:00:24 PDT 2026


================
@@ -498,6 +513,43 @@ class LifetimeChecker {
       }
     }
   }
+
+  /// Retrieve a list of reliable expressions from OriginIFlowChain that
+  /// can be used for Sema warnings.
+  ///
+  /// Although the AST node corresponding to Origin can be either a
+  /// `const Expr *` or a `const ValueDecl *`, `buildOriginFlowChain` only
+  /// collects Origins from RHS expressions. Therefore, we do not need to
+  /// handle non-expression cases here.
+  llvm::SmallVector<const Expr *>
+  buildExprOrDeclChain(llvm::ArrayRef<OriginID> OriginFlowChain) {
+    llvm::SmallVector<const Expr *> rs;
+    const SourceManager &SM = AST.getSourceManager();
+
+    auto InsertOrReplace = [&rs, &SM](const Expr *NewNode) {
+      if (!NewNode)
+        return;
+      SourceLocation NewLocation = NewNode->getExprLoc();
+      if (NewLocation.isInvalid())
+        return;
+
+      const Expr *LastNode = rs.back();
+      SourceLocation LastLocation = LastNode->getExprLoc();
+      if (SM.getSpellingLineNumber(LastLocation) ==
+          SM.getSpellingLineNumber(NewLocation))
+        rs.back() = NewNode;
+      else
+        rs.push_back(NewNode);
+    };
----------------
usx95 wrote:

For this method, let's focus only on retrieving expr from origins and not omit certain expressions for diagnostics purposes keeping this very simple and boilerplate. This filtering can be done at printing time.

We should not use line numbers but source ranges to allow highlighting subexpressions with different ranges on the same line.

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


More information about the cfe-commits mailing list