Files
lucisv/crates/vlog_formatter/tests/cases/directive_split.cases
T
lucioandClaude Fable 5 b77b61e7de directive-split: recover conditional-split constructs as verbatim items
An `ifdef/`else region whose arms are each an incomplete fragment of one
declaration (alternative heads sharing a body; alternative struct bodies
sharing a closing) no longer refuses: the strict parser recovers the whole
construct as a verbatim item, exact source bytes, the constraint/covergroup
precedent. The trigger requires an unmatched open bracket inside a region
genuinely open at item scope — `Parser::open_conds` counts only at the
single ItemKind::Directive producer, so opaque verbatim walks cannot
perturb it, and a recovered block hands back the nesting it swallows.

Corpus: 800 -> 813 / 820 (97.6% -> 99.1%), +13 files (12x
riscv_core_setting.sv, riscv_instr_pkg.sv), pass set a strict superset.
verify.rs untouched. Three dev passes, two adversarial QA rejections
(stray-`endif laundering; open_conds pollution via opaque bodies), QA
round 5 PASS. 26+12 new tests; workspace 1672 passed / 0 failed.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-08-26 01:26:28 -07:00

321 lines
5.5 KiB
Plaintext

# alternative declaration heads sharing one body: the whole construct is one
# verbatim block, so it comes back byte for byte
# corpus/ibex/vendor/google_riscv-dv/target/rv32imc/riscv_core_setting.sv:98
`ifdef DSIM
privileged_reg_t implemented_csr[] = {
`else
const privileged_reg_t implemented_csr[] = {
`endif
MVENDORID, // Vendor ID
MARCHID // Architecture ID
};
parameter int AFTER = 1;
----
`ifdef DSIM
privileged_reg_t implemented_csr[] = {
`else
const privileged_reg_t implemented_csr[] = {
`endif
MVENDORID, // Vendor ID
MARCHID // Architecture ID
};
parameter int AFTER = 1;
====
# alternative struct bodies sharing one closing brace, inside a package
# corpus/ibex/vendor/google_riscv-dv/src/riscv_instr_pkg.sv:1236
package p;
`ifdef _VCP
typedef struct packed {
bit l;
`else
typedef struct{
rand bit l;
`endif
} pmp_cfg_reg_t;
parameter int AFTER = 1;
endpackage
----
package p;
`ifdef _VCP
typedef struct packed {
bit l;
`else
typedef struct{
rand bit l;
`endif
} pmp_cfg_reg_t;
parameter int AFTER = 1;
endpackage
====
# the block's *relative* layout is what survives: only the first line is
# re-indented to the item's level, the rest is dedented by the block's start
# column and indented back -- the fixed-point trick `constraint` uses
module m;
`ifdef A
logic [3:0] x[] = {
`else
const logic [3:0] x[] = {
`endif
4'h1,
4'h2
};
assign y = 1;
endmodule
----
module m;
`ifdef A
logic [3:0] x[] = {
`else
const logic [3:0] x[] = {
`endif
4'h1,
4'h2
};
assign y = 1;
endmodule
====
# `ifndef and an `elsif chain: three rival spellings of one head, one body.
# Only the first arm's `{` is counted, so the scan does not go looking for
# two more closing braces.
`ifndef A
int x[] = {
`elsif B
const int x[] = {
`else
static int x[] = {
`endif
1
};
----
`ifndef A
int x[] = {
`elsif B
const int x[] = {
`else
static int x[] = {
`endif
1
};
====
# a *balanced* region inside the shared tail is ordinary conditional text and
# does not end the block early -- the item after it is still its own item
`ifdef A
int x[] = {
`else
const int x[] = {
`endif
1,
`ifdef B
2,
`endif
3
};
parameter int AFTER=1;
----
`ifdef A
int x[] = {
`else
const int x[] = {
`endif
1,
`ifdef B
2,
`endif
3
};
parameter int AFTER = 1;
====
# comments: the one before the region leads the `ifdef, the ones written on
# the directives and inside the arms are part of the verbatim bytes, and the
# one after the terminating `;` stays outside the block and trails it
// head
`ifdef A // which
int x[] = { // open
`else // other
const int x[] = {
`endif
// why
1
}; // done
int y;
----
// head
`ifdef A // which
int x[] = { // open
`else // other
const int x[] = {
`endif
// why
1
}; // done
int y;
====
# NEGATIVE: a region whose arms are each a whole item is not a split -- both
# arms are parsed and laid out normally, as they always were
`ifdef A
wire x;
`else
wire y ;
`endif
----
`ifdef A
wire x;
`else
wire y;
`endif
====
# NEGATIVE: a bracket that opens *and closes* inside one arm leaves nothing
# open, so the region is ordinary text around ordinary items
`ifdef A
int x[] = {1, 2};
`else
int x[] = {3, 4};
`endif
----
`ifdef A
int x [] = {1, 2};
`else
int x [] = {3, 4};
`endif
====
# a genuine split nested INSIDE a legitimately-open outer region: the guard
# added for QA's stray-`endif finding is "a region is live", not "exactly one
# region is live", so this still recovers
`ifdef OUTER
`ifdef DSIM
privileged_reg_t csr[] = {
`else
const privileged_reg_t csr[] = {
`endif
MVENDORID
};
`endif
parameter int AFTER = 1;
----
`ifdef OUTER
`ifdef DSIM
privileged_reg_t csr[] = {
`else
const privileged_reg_t csr[] = {
`endif
MVENDORID
};
`endif
parameter int AFTER = 1;
====
# the same, shape B, inside a package inside an outer region
package p;
`ifdef OUTER
`ifdef _VCP
typedef struct packed {
bit l;
`else
typedef struct{
rand bit l;
`endif
} pmp_cfg_reg_t;
`endif
parameter int AFTER = 1;
endpackage
----
package p;
`ifdef OUTER
`ifdef _VCP
typedef struct packed {
bit l;
`else
typedef struct{
rand bit l;
`endif
} pmp_cfg_reg_t;
`endif
parameter int AFTER = 1;
endpackage
====
# NEGATIVE: a stray `endif closes nothing, so the declaration next to it is
# not a split -- and, since neither is a parse error on its own, both stay
# ordinary items (lucisv does not validate directive nesting, and did not
# before this work either)
wire a;
`endif
int x[] = {1};
----
wire a;
`endif
int x [] = {1};
====
# over-suppression guard: directives written inside an opaque `constraint`
# body no longer touch item-scope nesting, but the region around the body
# still counts, so a genuine split after it still recovers
class c;
constraint co {
`ifdef X
x < 10;
`endif
}
endclass
`ifdef DSIM
int y[] = {
`else
const int y[] = {
`endif
1
};
parameter int AFTER = 1;
----
class c;
constraint co {
`ifdef X
x < 10;
`endif
}
endclass
`ifdef DSIM
int y[] = {
`else
const int y[] = {
`endif
1
};
parameter int AFTER = 1;
====
# under-suppression guard: an earlier verbatim item (a constraint) inside the
# very region the split sits in -- the outer `ifdef must still be counted
`ifdef OUTER
class c;
constraint co {x < 10;}
endclass
`ifdef DSIM
int y[] = {
`else
const int y[] = {
`endif
1
};
`endif
parameter int AFTER = 1;
----
`ifdef OUTER
class c;
constraint co {x < 10;}
endclass
`ifdef DSIM
int y[] = {
`else
const int y[] = {
`endif
1
};
`endif
parameter int AFTER = 1;