[clang] [LifetimeSafety] Add placement new support (PR #194030)
Utkarsh Saxena via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 29 07:17:35 PDT 2026
================
@@ -626,9 +632,31 @@ void FactsGenerator::VisitArraySubscriptExpr(const ArraySubscriptExpr *ASE) {
void FactsGenerator::VisitCXXNewExpr(const CXXNewExpr *NE) {
OriginList *NewList = getOriginsList(*NE);
- const Loan *L = createLoan(FactMgr, NE);
- CurrentBlockFacts.push_back(
- FactMgr.createFact<IssueFact>(L->getID(), NewList->getOuterOriginID()));
+ // Check if we have a placement new where the second argument is void*, to
+ // avoid flowing from non-pointer parameters, such as std::nothrow.
+ // And that the placement parameter num 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()) {
+ const Expr *PlacementArg = NE->getPlacementArg(0)->IgnoreImpCasts();
----------------
usx95 wrote:
I am not a big fan of making this work without a general solution of writing to aliases of origins. (e.g. `placement_new_member_call_from_dead_scope`)
Let's keep it simple for now and flow from PlacementList to NewList
(See another thread which talks about an alias analysis required to solve this)
https://github.com/llvm/llvm-project/pull/194030
More information about the cfe-commits
mailing list