From a1ce7446eef921bcb53300aa76e7e10f9bac4c69 Mon Sep 17 00:00:00 2001 From: tignear Date: Mon, 26 Oct 2026 03:46:32 +0900 Subject: [PATCH] fix(sv): reject pragmas without a name A bare `pragma was accepted by preprocessing even though IEEE 1800-2023 22.11 (Syntax 22-8) requires a pragma name. Reject active pragma directives whose line contains only whitespace or comments. Preserve named unknown pragmas and inactive conditional branches. Cover EOF, whitespace, comments, a following module, inactive branches, and error propagation through public preprocessing and parsing APIs. The official sv-tests pragma-invalid case now passes in both state modes because it receives the intended diagnostic. Update only its two expectation rows; all 90 preprocessing cases now pass in each mode. Validation: celox-sv-syntax tests (126 passed), analyzer syntax tests (7 passed), Clippy for both crates with all targets and warnings denied, formatting and diff checks, and the official pragma-invalid case in both state modes with the release sv_tests driver. Change-Id: I0046a3ff7118b6fbf823c326ede552932d3af5a0 --- conformance/sv-tests/README.md | 10 +++---- conformance/sv-tests/expected.tsv | 4 +-- crates/celox-sv-analyzer/src/syntax.rs | 9 ++++++ crates/celox-sv-syntax/src/preprocess.rs | 35 ++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 7 deletions(-) diff --git a/conformance/sv-tests/README.md b/conformance/sv-tests/README.md index 42bd159f1b2..fb91aa70ce2 100644 --- a/conformance/sv-tests/README.md +++ b/conformance/sv-tests/README.md @@ -37,11 +37,11 @@ command-line defines apply to each file. | `skip` | the official runner skipped an incompatible test or unsupported mode | All 90 preprocessing cases at the pinned revision now execute in both state -modes, leaving no skipped expectation rows. Each mode reports 89 passes and -one failure: `chapter-22/22.11--pragma-invalid.sv` expects a bare `` `pragma `` -to be rejected, but the preprocessor currently accepts it. This failure remains -visible in the baseline. Upstream preprocessing verdicts check process success -and expected failure; they do not compare the emitted text against a golden file. +modes, leaving no skipped expectation rows. Each mode reports 90 passes, +including `chapter-22/22.11--pragma-invalid.sv`: a bare `` `pragma `` is rejected +because its name is required by IEEE 1800-2023 22.11. Upstream preprocessing +verdicts check process success and expected failure; they do not compare the +emitted text against a golden file. The official runner’s process exit code is separate from the case verdict. The wrapper reads its `PASS`/`FAIL` log messages; it does not reconstruct their diff --git a/conformance/sv-tests/expected.tsv b/conformance/sv-tests/expected.tsv index 6e6ddef53fa..50c15b87ad4 100644 --- a/conformance/sv-tests/expected.tsv +++ b/conformance/sv-tests/expected.tsv @@ -831,8 +831,8 @@ chapter-22/22.11--pragma-basic.sv 2state pass chapter-22/22.11--pragma-basic.sv 4state pass chapter-22/22.11--pragma-complex.sv 2state pass chapter-22/22.11--pragma-complex.sv 4state pass -chapter-22/22.11--pragma-invalid.sv 2state fail -chapter-22/22.11--pragma-invalid.sv 4state fail +chapter-22/22.11--pragma-invalid.sv 2state pass +chapter-22/22.11--pragma-invalid.sv 4state pass chapter-22/22.11--pragma-nested.sv 2state pass chapter-22/22.11--pragma-nested.sv 4state pass chapter-22/22.11--pragma-number-multi.sv 2state pass diff --git a/crates/celox-sv-analyzer/src/syntax.rs b/crates/celox-sv-analyzer/src/syntax.rs index 9fde61ea7ba..7c792a9eba9 100644 --- a/crates/celox-sv-analyzer/src/syntax.rs +++ b/crates/celox-sv-analyzer/src/syntax.rs @@ -189,6 +189,15 @@ mod tests { } } + #[test] + fn public_preprocessing_and_parsing_reject_nameless_pragmas() { + let code = "`pragma\nmodule M; endmodule"; + let path = Path::new("pragma.sv"); + assert!(preprocess_source(code, path, &[], &[]).is_err()); + assert!(parse_source(code, path).is_err()); + assert!(crate::ParsedSource::parse(code, path).is_err()); + } + #[test] fn command_macros_have_values_without_leaking_source_definitions() { let path = Path::new("macros.sv"); diff --git a/crates/celox-sv-syntax/src/preprocess.rs b/crates/celox-sv-syntax/src/preprocess.rs index 560bf9734e5..e619d16855f 100644 --- a/crates/celox-sv-syntax/src/preprocess.rs +++ b/crates/celox-sv-syntax/src/preprocess.rs @@ -431,6 +431,16 @@ impl Preprocessor { } name if passthrough(name) => { if active { + // IEEE 1800-2023 22.11 requires a pragma name, even + // when the implementation does not recognize it. + if name == "pragma" + && tokens + .iter() + .take_while(|token| token.kind != TokenKind::Newline) + .all(Token::is_trivia) + { + return Err(self.error(origin, "expected a name after `pragma")); + } output.extend([token, name_token]); } } @@ -1117,6 +1127,31 @@ mod tests { .join(" ") } + #[test] + fn pragmas_require_a_name_on_the_directive_line() { + for code in [ + "`pragma", + "`pragma \t", + "`pragma // comment\nmodule M; endmodule", + "`pragma /* comment */\nmodule M; endmodule", + "`pragma\nmodule M; endmodule", + ] { + let error = Preprocessor::default() + .preprocess(code, "pragma.sv") + .unwrap_err(); + assert!(error.to_string().contains("expected a name after `pragma")); + } + for code in [ + "`pragma unknown", + "`pragma /* comment */ unknown value=1\nmodule M; endmodule", + "`ifdef UNDEFINED\n`pragma\n`endif\nmodule M; endmodule", + ] { + Preprocessor::default() + .preprocess(code, "pragma.sv") + .unwrap(); + } + } + #[test] fn macro_generated_includes_end_at_each_invocation() { let mut pp = Preprocessor::new(Options {