diff --git a/src/V3Ast.cpp b/src/V3Ast.cpp index 973814442..995fd1b5c 100644 --- a/src/V3Ast.cpp +++ b/src/V3Ast.cpp @@ -97,18 +97,17 @@ AstNode* AstNode::abovep() const { string AstNode::encodeName(const string& namein) { // Encode signal name raw from parser, then not called again on same signal string out; - for (string::const_iterator pos = namein.begin(); pos != namein.end(); ++pos) { + out.reserve(namein.size()); + for (auto pos = namein.begin(); pos != namein.end(); ++pos) { if ((pos == namein.begin()) ? std::isalpha(pos[0]) // digits can't lead identifiers : std::isalnum(pos[0])) { out += pos[0]; } else if (pos[0] == '_') { + out += pos[0]; + if (pos + 1 == namein.end()) break; if (pos[1] == '_') { - out += "_"; - out += "__05F"; // hex(_) = 0x5F ++pos; - if (pos == namein.end()) break; - } else { - out += pos[0]; + out += "__05F"; // hex(_) = 0x5F } } else { // Need the leading 0 so this will never collide with diff --git a/test_regress/t/t_var_escape.out b/test_regress/t/t_var_escape.out index 48b83ad17..7517b1831 100644 --- a/test_regress/t/t_var_escape.out +++ b/test_regress/t/t_var_escape.out @@ -11,9 +11,11 @@ $timescale 1ps $end $var wire 1 # clk $end $var wire 32 ( cyc [31:0] $end $var wire 1 $ escaped_normal $end - $var wire 1 $ double__underscore $end - $var wire 1 $ 9num $end - $var wire 1 $ bra[ket]slash/dash-colon:9backslash\done $end + $var wire 1 % double__underscore $end + $var wire 1 $ underscore_at_the_end_ $end + $var wire 1 $ double__underscore_at_the_end__ $end + $var wire 1 & 9num $end + $var wire 1 ' bra[ket]slash/dash-colon:9backslash\done $end $var wire 1 $ wire $end $var wire 1 $ check_alias $end $var wire 1 $ check:alias $end diff --git a/test_regress/t/t_var_escape.v b/test_regress/t/t_var_escape.v index bc2b38ea5..910c61332 100644 --- a/test_regress/t/t_var_escape.v +++ b/test_regress/t/t_var_escape.v @@ -21,6 +21,15 @@ module t (/*AUTOARG*/ output double__underscore ; wire double__underscore = cyc[0]; + wire underscore_at_the_end_ = cyc[0]; + wire double__underscore_at_the_end__ = cyc[0]; + + // Only underscores, ignored in trace + wire _ = cyc[0]; + wire __ = cyc[0]; + wire ___ = cyc[0]; + wire ____ = cyc[0]; + // C doesn't allow leading non-alpha, so must escape output \9num ; wire \9num = cyc[0]; @@ -45,7 +54,12 @@ module t (/*AUTOARG*/ cyc <= cyc + 1; if (escaped_normal != cyc[0]) $stop; if (\escaped_normal != cyc[0]) $stop; - if (double__underscore != cyc[0]) $stop; + if (underscore_at_the_end_ != cyc[0]) $stop; + if (double__underscore_at_the_end__ != cyc[0]) $stop; + if (_ != cyc[0]) $stop; + if (__ != cyc[0]) $stop; + if (___ != cyc[0]) $stop; + if (____ != cyc[0]) $stop; if (\9num != cyc[0]) $stop; if (\bra[ket]slash/dash-colon:9backslash\done != cyc[0]) $stop; if (\wire != cyc[0]) $stop;