From 600eb7ec86eadb943bd091fd51ff23554e958e4e Mon Sep 17 00:00:00 2001 From: Wilson Snyder Date: Sun, 29 Mar 2026 20:33:06 -0400 Subject: [PATCH] Internals: Favor std::swap. No functional change. --- src/V3AstAttr.h | 6 +--- src/V3Width.cpp | 6 +--- src/V3WidthSel.cpp | 28 +++++-------------- ...ram_sel_range.py => t_param_sel_range1.py} | 0 ...param_sel_range.v => t_param_sel_range1.v} | 0 ...nge_bad.out => t_param_sel_range1_bad.out} | 4 +-- ...range_bad.py => t_param_sel_range1_bad.py} | 2 +- 7 files changed, 12 insertions(+), 34 deletions(-) rename test_regress/t/{t_param_sel_range.py => t_param_sel_range1.py} (100%) rename test_regress/t/{t_param_sel_range.v => t_param_sel_range1.v} (100%) rename test_regress/t/{t_param_sel_range_bad.out => t_param_sel_range1_bad.out} (65%) rename test_regress/t/{t_param_sel_range_bad.py => t_param_sel_range1_bad.py} (91%) diff --git a/src/V3AstAttr.h b/src/V3AstAttr.h index 91a7c5257..6a81c993c 100644 --- a/src/V3AstAttr.h +++ b/src/V3AstAttr.h @@ -1482,11 +1482,7 @@ public: ~VNumRange() = default; // MEMBERS void init(int hi, int lo, bool ascending) { - if (lo > hi) { - const int t = hi; - hi = lo; - lo = t; - } + if (lo > hi) std::swap(hi, lo); m_left = ascending ? lo : hi; m_right = ascending ? hi : lo; m_ranged = true; diff --git a/src/V3Width.cpp b/src/V3Width.cpp index 660b7e73a..464af84c3 100644 --- a/src/V3Width.cpp +++ b/src/V3Width.cpp @@ -1185,11 +1185,7 @@ class WidthVisitor final : public VNVisitor { if (const AstUnpackArrayDType* const adtypep = VN_CAST(fromDtp, UnpackArrayDType)) { frommsb = adtypep->hi(); fromlsb = adtypep->lo(); - if (fromlsb > frommsb) { - const int t = frommsb; - frommsb = fromlsb; - fromlsb = t; - } + if (fromlsb > frommsb) std::swap(frommsb, fromlsb); // However, if the lsb<0 we may go negative, so need more bits! if (fromlsb < 0) frommsb += -fromlsb; nodep->dtypeFrom(adtypep->subDTypep()); // Need to strip off array reference diff --git a/src/V3WidthSel.cpp b/src/V3WidthSel.cpp index fb0f6a53a..055755842 100644 --- a/src/V3WidthSel.cpp +++ b/src/V3WidthSel.cpp @@ -448,21 +448,15 @@ class WidthSelVisitor final : public VNVisitor { adtypep, "Array extraction with width miscomputed " << adtypep->width() << "/" << fromRange.elements()); - if (fromRange.ascending()) { - // Below code assumes descending bit range; just works out if we swap - const int x = msb; - msb = lsb; - lsb = x; - } + // Below code assumes descending bit range; just works out if we swap + if (fromRange.ascending()) std::swap(msb, lsb); if (lsb > msb) { nodep->v3warn( SELRANGE, "[" << msb << ":" << lsb << "] Slice range has ascending bit ordering, perhaps you wanted [" << lsb << ":" << msb << "]"); - const int x = msb; - msb = lsb; - lsb = x; + std::swap(msb, lsb); } const int elwidth = adtypep->width() / fromRange.elements(); AstSel* const newp @@ -477,21 +471,15 @@ class WidthSelVisitor final : public VNVisitor { nodep->replaceWith(newp); VL_DO_DANGLING(pushDeletep(nodep), nodep); } else if (VN_IS(ddtypep, BasicDType)) { - if (fromRange.ascending()) { - // Below code assumes descending bit range; just works out if we swap - const int x = msb; - msb = lsb; - lsb = x; - } + // Below code assumes descending bit range; just works out if we swap + if (fromRange.ascending()) std::swap(msb, lsb); if (lsb > msb) { nodep->v3warn( SELRANGE, "[" << msb << ":" << lsb << "] Slice range has ascending bit ordering, perhaps you wanted [" << lsb << ":" << msb << "]"); - const int x = msb; - msb = lsb; - lsb = x; + std::swap(msb, lsb); } AstSel* const newp = new AstSel{nodep->fileline(), fromp, newSubLsbOf(lsbp, fromRange), msb + 1 - lsb}; @@ -508,9 +496,7 @@ class WidthSelVisitor final : public VNVisitor { "[" << msb << ":" << lsb << "] Slice range has ascending bit ordering, perhaps you wanted [" << lsb << ":" << msb << "]"); - const int x = msb; - msb = lsb; - lsb = x; + std::swap(msb, lsb); } AstSel* const newp = new AstSel{nodep->fileline(), fromp, newSubLsbOf(lsbp, fromRange), msb + 1 - lsb}; diff --git a/test_regress/t/t_param_sel_range.py b/test_regress/t/t_param_sel_range1.py similarity index 100% rename from test_regress/t/t_param_sel_range.py rename to test_regress/t/t_param_sel_range1.py diff --git a/test_regress/t/t_param_sel_range.v b/test_regress/t/t_param_sel_range1.v similarity index 100% rename from test_regress/t/t_param_sel_range.v rename to test_regress/t/t_param_sel_range1.v diff --git a/test_regress/t/t_param_sel_range_bad.out b/test_regress/t/t_param_sel_range1_bad.out similarity index 65% rename from test_regress/t/t_param_sel_range_bad.out rename to test_regress/t/t_param_sel_range1_bad.out index a6842a905..35b88187a 100644 --- a/test_regress/t/t_param_sel_range_bad.out +++ b/test_regress/t/t_param_sel_range1_bad.out @@ -1,5 +1,5 @@ -%Warning-SELRANGE: t/t_param_sel_range.v:41:33: Selection index out of range: 7:7 outside 4:0 - : ... note: In instance 't.u2' +%Warning-SELRANGE: t/t_param_sel_range1.v:41:33: Selection index out of range: 7:7 outside 4:0 + : ... note: In instance 't.u2' 41 | else r_rst[i] <= r_rst[i-1]; | ^ ... For warning description see https://verilator.org/warn/SELRANGE?v=latest diff --git a/test_regress/t/t_param_sel_range_bad.py b/test_regress/t/t_param_sel_range1_bad.py similarity index 91% rename from test_regress/t/t_param_sel_range_bad.py rename to test_regress/t/t_param_sel_range1_bad.py index 50f394103..6e2f64a44 100755 --- a/test_regress/t/t_param_sel_range_bad.py +++ b/test_regress/t/t_param_sel_range1_bad.py @@ -10,7 +10,7 @@ import vltest_bootstrap test.scenarios('linter') -test.top_filename = "t/t_param_sel_range.v" +test.top_filename = "t/t_param_sel_range1.v" test.lint(fails=True, expect_filename=test.golden_filename)