diff --git a/Changes b/Changes index 8ff7323eb..6abef0bd2 100644 --- a/Changes +++ b/Changes @@ -149,6 +149,7 @@ Verilator 5.051 devel * Fix crash on forward-referencing parameters (#8175). [Geza Lore, Testorrent USA, Inc.] * Fix crash on empty `#()` parameter list on class instance (#8182). [Geza Lore, Testorrent USA, Inc.] * Fix crash on multiple missing type parameters (#8184). [Geza Lore, Testorrent USA, Inc.] +* Fix internal on backslash quoted strings (#8198). [Henry Hsieh] Verilator 5.050 2026-07-01 diff --git a/src/V3File.cpp b/src/V3File.cpp index f6f063c47..18f6eb050 100644 --- a/src/V3File.cpp +++ b/src/V3File.cpp @@ -768,6 +768,8 @@ void V3OutFormatter::putns(const AstNode* nodep, const char* strg) { if (wordstart && m_lang == LA_VERILOG && !notstart && tokenStart(cp)) indentInc(); if (wordstart && m_lang == LA_VERILOG && tokenEnd(cp)) indentDec(); } + const bool wasInBackslash = m_inBackslash; + m_inBackslash = false; switch (*cp) { case '\n': ++m_lineno; @@ -785,7 +787,11 @@ void V3OutFormatter::putns(const AstNode* nodep, const char* strg) { case '\t': wordstart = true; break; case '"': wordstart = false; - m_inStringLiteral = !m_inStringLiteral; + if (!wasInBackslash) m_inStringLiteral = !m_inStringLiteral; + break; + case '\\': + wordstart = false; + if (!wasInBackslash) m_inBackslash = true; break; case '/': if (m_lang == LA_C || m_lang == LA_VERILOG) { diff --git a/src/V3File.h b/src/V3File.h index 66b3d60f3..954b0773c 100644 --- a/src/V3File.h +++ b/src/V3File.h @@ -118,7 +118,8 @@ private: int m_sourceLastLineno = 0; int m_sourceLastFilenameno = 0; bool m_prependIndent = true; - bool m_inStringLiteral = false; + bool m_inStringLiteral = false; // Inside a quote + bool m_inBackslash = false; // Inside a backslash int m_indentLevel = 0; // Current {} indentation std::stack m_parenVec; // Stack of columns where last ( was int m_bracketLevel = 0; // Indenting = { block, indicates number of {'s seen. diff --git a/test_regress/t/t_display_brace.v b/test_regress/t/t_display_brace.v index 16f8b5b47..c191378a1 100644 --- a/test_regress/t/t_display_brace.v +++ b/test_regress/t/t_display_brace.v @@ -28,12 +28,25 @@ class ahb_seq_item; endclass - module top; + class c; + string m_buf; + + // Issue #8198 + function void f(string s); + m_buf = "\""; + if (s[0] == "\"") m_buf = "\\\""; + endfunction + endclass + initial begin ahb_seq_item tr; + automatic c o = new(); + o.f("a"); + $display("%0s", o.m_buf); + tr = new(); tr.data = '{'h11, 'h22, 'h33, 'h44, 'h55, 'h66}; $display(" tr(bytes, LE) @0x10: %0s", tr.to_string());