From 9a310d307fd915b144c2351541ddc91771cda2de Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 22 Mar 2026 11:17:30 -1000 Subject: [PATCH] touch ups --- esphome/cpp_generator.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/esphome/cpp_generator.py b/esphome/cpp_generator.py index 5a6452f35a..a1e4c2ac59 100644 --- a/esphome/cpp_generator.py +++ b/esphome/cpp_generator.py @@ -585,20 +585,25 @@ def Pvariable(id_: ID, rhs: SafeExpType, type_: "MockObj" = None) -> "MockObj": if is_new: # For 'new' allocations, use placement new into static storage # to avoid heap fragmentation on embedded devices. - storage_name = f"{id_.id}_storage_" the_type = id_.type + storage_type = MockObj(f"esphome::PlacementStorage<{the_type}>") + storage_id = ID(f"{id_.id}_storage_", type=storage_type) CORE.add_global( - RawStatement( - f"static esphome::PlacementStorage<{the_type}> {storage_name};" - ) + VariableDeclarationExpression(storage_type, "", storage_id, static=True) ) CORE.add_global( - RawStatement(f"static {the_type} *const {id_.id} = {storage_name}.get();") + AssignmentExpression( + f"static {the_type}", + "*const ", + id_, + MockObj(f"{storage_id.id}.get()"), + ) ) - # Strip "new " prefix to get "Type(args)" for placement new - rhs_str = str(rhs) - CORE.add(RawStatement(f"new({id_.id}) {rhs_str[4:]};")) + # Extract args from the CallExpression and rebuild as placement new + call_expr = rhs.base # CallExpression("new Type", args...) + placement_new = CallExpression(f"new({id_.id}) {the_type}", *call_expr.args) + CORE.add(ExpressionStatement(placement_new)) else: decl = VariableDeclarationExpression(id_.type, "*", id_, static=True) CORE.add_global(decl)