================
@@ -167,24 +171,242 @@ static QualType inferPossibleTypeFromCastExpr(const
CallExpr *CallE,
return QualType();
}
-QualType infer_alloc::inferPossibleType(const CallExpr *E,
- const ASTContext &Ctx,
- const CastExpr *CastE) {
+QualType infer_alloc::inferPossibleType(
+ const CallExpr *E, const ASTContext &Ctx, const CastExpr *CastE,
+ llvm::function_ref<QualType(const CallExpr *)> PlacementNewType) {
QualType AllocType;
- // First check arguments.
+ // First check arguments: a sizeof states what the allocation was sized for.
for (const Expr *Arg : E->arguments()) {
AllocType = inferPossibleTypeFromArithSizeofExpr(Arg);
if (AllocType.isNull())
AllocType = inferPossibleTypeFromVarInitSizeofExpr(Arg);
if (!AllocType.isNull())
break;
}
+ // Then check for a placement new into the result, which names the object
+ // constructed there and is stronger evidence than a cast of the result.
+ if (AllocType.isNull() && PlacementNewType)
+ AllocType = PlacementNewType(E);
// Then check later casts.
if (AllocType.isNull())
AllocType = inferPossibleTypeFromCastExpr(E, CastE);
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 parentheses, casts, temporaries, and the trivial copy or
+/// move a class-typed value may carry.
+const CallExpr *initializingCall(const Expr *Init) {
+ while (Init) {
+ Init = Init->IgnoreImplicit()->IgnoreParenCasts();
+ const auto *CE = dyn_cast<CXXConstructExpr>(Init);
+ // A non-trivial copy or move may not be elided (-fno-elide-constructors)
+ // and could replace the pointer.
+ if (!CE || CE->getNumArgs() != 1 ||
+ !CE->getConstructor()->isCopyOrMoveConstructor() ||
+ !CE->getConstructor()->isTrivial())
+ break;
+ Init = CE->getArg(0);
+ }
+ return dyn_cast_or_null<CallExpr>(Init);
+}
+
+/// Returns true if \p Init is a null pointer constant (nullptr, NULL, 0).
+bool isNullPointerInit(const Expr *Init) {
----------------
melver wrote:
I would drop this together with assignment-as-definition and only accept
`T *p = alloc(...)` / `auto res = alloc(...)`. `void *p; p = malloc(n);` and
`void *q = nullptr; if (c) q = malloc(n);` are not the idiom this PR is for,
and they are what makes "definition" need its own bookkeeping.
If it stays: Expr::isNullPointerConstant() already exists.
https://github.com/llvm/llvm-project/pull/241548
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits