From 155dcc56585fd5c900dd1a69c151c0c4c49b0c65 Mon Sep 17 00:00:00 2001 From: Krzysztof Bieganski Date: Tue, 27 Aug 2024 15:39:36 +0200 Subject: [PATCH] Fix empty `foreach` in `if` in constraints (#5408) Signed-off-by: Krzysztof Bieganski --- src/V3Randomize.cpp | 4 +++- test_regress/t/t_constraint_foreach.v | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/V3Randomize.cpp b/src/V3Randomize.cpp index 4a6f6160a..95e2d01c2 100644 --- a/src/V3Randomize.cpp +++ b/src/V3Randomize.cpp @@ -695,11 +695,13 @@ class ConstraintExprVisitor final : public VNVisitor { AstNode* const cstmtp = new AstText{fl, "ret += \" \" + "}; cstmtp->addNext(itemp); cstmtp->addNext(new AstText{fl, ";"}); - AstNode* const exprsp = new AstText{fl, "([&]{ std::string ret = \"(bvand\";"}; + AstNode* const exprsp = new AstText{fl, "([&]{ std::string ret;"}; exprsp->addNext(new AstBegin{ fl, "", new AstForeach{fl, nodep->arrayp()->unlinkFrBack(), new AstCStmt{fl, cstmtp}}, false, true}); + exprsp->addNext( + new AstText{fl, "return ret.empty() ? \"#b1\" : \"(bvand \" + ret + \")\";"}); exprsp->addNext(new AstText{fl, "return ret + \")\"; })()"}); AstNodeExpr* const newp = new AstCExpr{fl, exprsp}; newp->dtypeSetString(); diff --git a/test_regress/t/t_constraint_foreach.v b/test_regress/t/t_constraint_foreach.v index 6bd36c704..35ec42c69 100644 --- a/test_regress/t/t_constraint_foreach.v +++ b/test_regress/t/t_constraint_foreach.v @@ -32,8 +32,16 @@ endclass class D; rand bit posit; rand int x; + int o[$]; // empty + int p[$] = {1}; int q[$] = {0, 0, 0, 0, 0}; constraint fore { + if (posit == 1) { + foreach(o[i]) o[i] > 0; + } + if (posit == 1) { + foreach(p[i]) p[i] > 0; + } if (posit == 1) { x < 7; foreach(q[i])