From af65a85a1e11fb0e5332bb91024dd020de49dd79 Mon Sep 17 00:00:00 2001 From: Wilson Snyder Date: Thu, 26 Feb 2026 18:12:12 -0500 Subject: [PATCH] Fix function locals in display %p --- src/V3Common.cpp | 17 ++++++++++++----- src/V3Task.cpp | 1 + test_regress/t/t_class_format.v | 13 ++++++++++++- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/V3Common.cpp b/src/V3Common.cpp index b86a2146b..1d0deb257 100644 --- a/src/V3Common.cpp +++ b/src/V3Common.cpp @@ -107,7 +107,7 @@ static void makeToStringMiddle(AstClass* nodep) { funcp->isConst(true); funcp->isStatic(false); funcp->protect(false); - funcp->addStmtsp(new AstCStmt{nodep->fileline(), "std::string out;"}); + AstNodeStmt* stmtsp = nullptr; std::string comma; for (AstNode* itemp = nodep->membersp(); itemp; itemp = itemp->nextp()) { if (const auto* const varp = VN_CAST(itemp, Var)) { @@ -122,7 +122,7 @@ static void makeToStringMiddle(AstClass* nodep) { stmt += V3Common::makeToStringCall(itemp->dtypep(), itemp->nameProtect()); stmt += ";"; nodep->user1(true); // So what we extend dumps this - funcp->addStmtsp(new AstCStmt{nodep->fileline(), stmt}); + stmtsp = AstNode::addNextNull(stmtsp, new AstCStmt{nodep->fileline(), stmt}); } } } @@ -133,11 +133,18 @@ static void makeToStringMiddle(AstClass* nodep) { stmt += EmitCUtil::prefixNameProtect(nodep->extendsp()->dtypep()); stmt += "::to_string_middle();"; nodep->user1(true); // So what we extend dumps this - funcp->addStmtsp(new AstCStmt{nodep->fileline(), stmt}); + stmtsp = AstNode::addNextNull(stmtsp, new AstCStmt{nodep->fileline(), stmt}); } - AstCExpr* const exprp = new AstCExpr{nodep->fileline(), "out"}; - exprp->dtypeSetString(); + AstNodeExpr* exprp; + if (stmtsp) { + funcp->addStmtsp(new AstCStmt{nodep->fileline(), "std::string out;"}); + funcp->addStmtsp(stmtsp); + exprp = new AstCExpr{nodep->fileline(), "out"}; + exprp->dtypeSetString(); + } else { // Nothing to print, return "" + exprp = new AstConst{nodep->fileline(), AstConst::String{}, ""}; + } funcp->addStmtsp(new AstCReturn{nodep->fileline(), exprp}); nodep->addStmtsp(funcp); diff --git a/src/V3Task.cpp b/src/V3Task.cpp index 9a04fa5e5..387ff5de1 100644 --- a/src/V3Task.cpp +++ b/src/V3Task.cpp @@ -451,6 +451,7 @@ class TaskVisitor final : public VNVisitor { = new AstVar{invarp->fileline(), VVarType::BLOCKTEMP, name, invarp}; newvarp->funcLocal(false); newvarp->propagateAttrFrom(invarp); + newvarp->isInternal(true); m_modp->addStmtsp(newvarp); AstVarScope* const newvscp = new AstVarScope{newvarp->fileline(), m_scopep, newvarp}; m_scopep->addVarsp(newvscp); diff --git a/test_regress/t/t_class_format.v b/test_regress/t/t_class_format.v index a67e334fe..76e14b879 100644 --- a/test_regress/t/t_class_format.v +++ b/test_regress/t/t_class_format.v @@ -18,6 +18,14 @@ endclass class Base; int m_in_base; + virtual function string fs(input string add = "default"); + static string s_f = "foo"; + fs = {s_f, add}; + endfunction + virtual function string other(input string add = "default"); + string other = "other"; + other = {other, fs(add)}; + endfunction endclass class ClsA extends Base; @@ -54,8 +62,11 @@ module t; c.debug(); ca = new; - $display("'%p'", ca); + if (ca.fs("-s") !== "foo-s") $stop; // So not optimized away + if (ca.other("-o") !== "otherfoo-o") $stop; // So not optimized away + cb = new; + $display("'%p'", ca); ca.m_b = cb; $display("'%p'", ca); cb.m_a = ca; // Circular