[clang] [LifetimeSafety] Add support for `new`/`delete` (PR #192504)

via cfe-commits cfe-commits at lists.llvm.org
Sun Apr 19 07:22:55 PDT 2026


================
@@ -615,6 +629,55 @@ void FactsGenerator::VisitArraySubscriptExpr(const ArraySubscriptExpr *ASE) {
       Dst->getOuterOriginID(), Src->getOuterOriginID(), /*Kill=*/true));
 }
 
+void FactsGenerator::VisitCXXNewExpr(const CXXNewExpr *NE) {
+  OriginList *NewList = getOriginsList(*NE);
+
+  // Check if we have a placement new where the second argument is void*, to
+  // avoid flowing from std::nothrow and the placement parameter amount is 1,
+  // that is to mostly limit to standard library placement new
+  if (NE->getNumPlacementArgs() == 1) {
+    if (const auto *Arg = NE->getOperatorNew()
+                              ->getParamDecl(1)
+                              ->getType()
+                              ->getAs<PointerType>();
+        Arg && Arg->isVoidPointerType()) {
+      OriginList *PlacementList = getOriginsList(*NE->getPlacementArg(0));
+      CurrentBlockFacts.push_back(FactMgr.createFact<OriginFlowFact>(
+          NewList->getOuterOriginID(), PlacementList->getOuterOriginID(),
+          true));
+    }
+  } else {
+    const Loan *L = createLoan(FactMgr, NE);
+    CurrentBlockFacts.push_back(
+        FactMgr.createFact<IssueFact>(L->getID(), NewList->getOuterOriginID()));
+  }
+
+  NewList = NewList->peelOuterOrigin();
+
+  if (!NewList)
+    return;
+
+  if (auto *CE = NE->getConstructExpr(); CE) {
+    if (OriginList *ArgList = getOriginsList(*CE); ArgList)
+      flow(NewList, ArgList, true);
+  } else if (const Expr *E = NE->getInitializer(); E) {
+    if (const auto *ILE = dyn_cast<InitListExpr>(E); NE->isArray() && ILE) {
+      if (OriginList *InitList = getOriginsList(*ILE); InitList)
+        flow(NewList, InitList, true);
+    } else if (OriginList *ArgList = getOriginsList(*E); ArgList) {
+      flow(NewList, ArgList, true);
+    }
+  }
+}
+
+void FactsGenerator::VisitCXXDeleteExpr(const CXXDeleteExpr *DE) {
+  OriginList *List = getOriginsList(*DE->getArgument()->IgnoreImpCasts());
----------------
NeKon69 wrote:

Yes, this test case does not warn.

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


More information about the cfe-commits mailing list