================
@@ -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 {
----------------
melver wrote:

The LLVM codebase tends to prefer `static` functions rather than wrapping into 
anon namespace.

https://github.com/llvm/llvm-project/pull/241548
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to