================
@@ -185,6 +190,244 @@ QualType infer_alloc::inferPossibleType(const CallExpr *E,
return AllocType;
}
+namespace {
+/// Strips the wrappers a placement argument may have around the expression
+/// that produced the storage: parentheses, casts, and non-static member
+/// accesses of a by-value result (e.g. the pointer member of a size-returning
+/// allocation result). Anything that follows a pointer into different storage
+/// (`->`, `*`) or names a different object (`&`) stops the walk.
+const Expr *stripToStorageSource(const Expr *E) {
+ while (true) {
+ E = E->IgnoreParenCasts();
+ const auto *ME = dyn_cast<MemberExpr>(E);
+ if (!ME || ME->isArrow() || !isa<FieldDecl>(ME->getMemberDecl()))
+ return E;
+ E = ME->getBase();
+ }
+}
+
+/// Finds the allocation call an initializer or assigned value consists of,
+/// looking through casts and the temporaries and trivial copies a class-typed
+/// value may carry.
+const CallExpr *initializingCall(const Expr *Init) {
+ while (Init) {
+ Init = Init->IgnoreParenCasts();
+ if (const auto *EWC = dyn_cast<ExprWithCleanups>(Init)) {
+ Init = EWC->getSubExpr();
+ } else if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(Init)) {
+ Init = MTE->getSubExpr();
+ } else if (const auto *BTE = dyn_cast<CXXBindTemporaryExpr>(Init)) {
+ Init = BTE->getSubExpr();
+ } else if (const auto *CE = dyn_cast<CXXConstructExpr>(Init);
+ CE && CE->getNumArgs() == 1 &&
+ CE->getConstructor()->isCopyOrMoveConstructor() &&
+ CE->getConstructor()->isTrivial()) {
+ // A non-trivial copy or move may not be elided (-fno-elide-constructors)
+ // and could replace the pointer.
+ Init = CE->getArg(0);
+ } else if (const auto *FCE = dyn_cast<CXXFunctionalCastExpr>(Init)) {
+ Init = FCE->getSubExpr();
+ } else {
+ break;
+ }
+ }
+ return dyn_cast_or_null<CallExpr>(Init);
+}
+
+/// Records, per allocation call, the type placement-constructed into its
+/// result.
+///
+/// Placement arguments that name a variable are resolved with a lexical
+/// def-use chain: a use `new (p) T` is matched to the nearest preceding
+/// definition of `p` (its initializer or an assignment `p = <call>`) in
+/// traversal order, provided that definition lexically dominates the use,
+/// i.e. it is in the same control-flow context as the use or an enclosing
+/// one. A definition in a branch that does not enclose the use, a later
+/// definition that can reach the use around a loop, or any other write to
+/// the variable (compound assignment, increment, address-of) makes the use
+/// ambiguous and nothing is inferred for it.
+class PlacementNewCollector : public ConstDynamicRecursiveASTVisitor {
----------------
melver wrote:
Suggest replacing the write detection (VisitBinaryOperator,
VisitUnaryOperator, VisitCallExpr, VisitCXXConstructExpr,
VisitCXXMemberCallExpr, writtenVar, noteWrite, noteReferenceArgs) with read
detection. A variable qualifies if its initializer is the allocation call and
every DeclRefExpr to it is read through an lvalue-to-rvalue conversion.
Traversal is pre-order, so the cast is seen before the DeclRefExpr below it.
Untested sketch:
```
bool VisitVarDecl(const VarDecl *VD) override {
if (VD->hasLocalStorage() && !isa<ParmVarDecl>(VD))
if (const CallExpr *CE = initializingCall(VD->getInit()))
Defs.try_emplace(VD, CE);
return true;
}
// A read of the variable or of one of its fields.
bool VisitImplicitCastExpr(const ImplicitCastExpr *ICE) override {
if (ICE->getCastKind() == CK_LValueToRValue)
if (const auto *DRE =
dyn_cast<DeclRefExpr>(stripFields(ICE->getSubExpr())))
Reads.insert(DRE);
return true;
}
// Any other reference may modify the variable.
bool VisitDeclRefExpr(const DeclRefExpr *DRE) override {
if (!Reads.contains(DRE))
if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
Defs.erase(VD);
return true;
}
llvm::DenseMap<const VarDecl *, const CallExpr *> Defs;
llvm::SmallPtrSet<const DeclRefExpr *, 16> Reads;
```
where stripFields() removes parentheses and non-arrow FieldDecl member
accesses (the part of stripToStorageSource() without the casts).
VisitCXXNewExpr and finish() stay as they are.
Assignment, compound assignment, ++/--, &, reference binding in any form,
member and operator calls, and by-reference captures all leave the
DeclRefExpr without an lvalue-to-rvalue conversion, so they disqualify the
variable without being listed. It is stricter than now in a few places
(`peek(t)` by const reference, `u.size()`, `(void)p`), which I think is fine.
https://github.com/llvm/llvm-project/pull/241548
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits