Skip to content

fix!: reject extra tokens on conditional directive lines - #228

Merged
exKAZUu merged 9 commits into
mainfrom
fix/ifdef-line-end
Nov 23, 2026
Merged

exKAZUu merged 9 commits into
mainfrom
fix/ifdef-line-end

Conversation

@exKAZUu

@exKAZUu exKAZUu commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Close #222

Why

In a plain block, preproc_ifdef and _try_opening_ifdef did not require the #ifdef/#ifndef line to end after the macro identifier, and #elifdef, #elifndef, #else, and #endif directives without an explicit _preproc_newline rule likewise accepted tokens joined on the same directive line without ERROR (including #ifdef E try { g(); } catch (...) {} and split try guards such as #ifdef E try { or #ifdef E } catch (...) {}). Adding a grammar-level newline or optional zero-width external token to preproc_ifdef_in_block either makes _preproc_newline valid in shared block states or alters GLR disambiguation on valid inputs such as Modules in preprocessor sections.

Requirements

  • required: Report tokens joined to #ifdef / #ifndef directive lines in blocks (including #ifdef E try { g(); } catch (...) {} and split try_statement opening/closing #ifdef lines) as an ERROR, while keeping valid inputs error-free (#222).
  • required: Full parses of valid C++ inputs yield error-free syntax trees without altering existing GLR disambiguations, and invalid inputs produce ERROR or MISSING nodes in linear time without crashing or hanging.
  • chosen: Validate directive line ends for #ifdef, #ifndef, #elifdef, #elifndef, #else, and #endif in the external scanner (src/cppTrivia.h, src/scanner.c) with a peek_units fast path, accept backslash-newline splices inside #ifdef/#ifndef/#elifdef/#elifndef macro names in both the scanner and _preproc_ifdef_name, and emit GNU_RAW_STRING_BODY when rejecting a malformed header during error recovery, because grammar-level newline tokens disturb shared block states and GLR tie-breaking, phase-two line splices inside conditional macro names are valid C++, and mergeableExternals would otherwise shift PREPROC_IFDEF error-free in error recovery.
  • chosen: Validate conditional directive headers and #endif line ends uniformly across all #if and #ifdef branches without evaluating #if 0 or other preprocessor constant expressions, because both @willbooster/tree-sitter-c and @willbooster/tree-sitter-cpp parse all conditional branches structurally as C++ (matching preproc_ifdef_in_if_block and preproc_ifdef_in_initializer).

Customer Summary

Malformed #ifdef, #ifndef, #elifdef, #elifndef, #else, and #endif lines that place extra C++ tokens on the same logical directive line now produce parse ERROR nodes instead of being silently accepted as valid conditional blocks or split try statements, while conditional macro names split across physical lines with backslash-newline splices parse as a single name identifier.

Technical Summary

  • grammar.js: Routes #ifdef, #ifndef, #elifdef, and #elifndef name fields through _preproc_ifdef_name, which pairs identifier with _spliced_preproc_identifier (aliased to identifier) so phase-two spliced macro names bind as a single name node.
  • src/cppTrivia.h: Adds peek_cpp_ifdef_header and peek_cpp_directive_line_end to validate plain ASCII directive tails directly from lexer->peek_units without advancing the lexer, falling back to scan_cpp_ifdef_header, scan_cpp_ifdef_word, scan_cpp_directive_line_end, and scan_cpp_directive_pragma_args across comments, line splices, UCNs, and same-logical-line _Pragma(...) operators while recording had_pragma.
  • src/scanner.c & src/initializerConditional.h: Checks #ifdef/#ifndef (scan_cpp_ifdef_directive), #elifdef/#elifndef (check_cpp_ifdef_line), and #else/#endif (check_cpp_empty_tail_line) before selecting directive symbols. When the slow path advances to the line end (at_line_end), scan_empty_initializer_at_line_end and !had_pragma && scan_cpp_conditional_marker continue lookahead from that newline and restore mark_end to the directive keyword end. On rejection in error_recovery, reject_cpp_directive returns GNU_RAW_STRING_BODY so tree-sitter wraps the malformed directive header in an ERROR node.

Testing

  • Ran bun run build/ci, script/tree-sitter test (326 corpus tests), cargo test --locked, bun wb test test/unit (41 test files, 161 tests), script/parse-examples (566/731 successful parses, unchanged from v43.0.0), and bun run verify (0 code-gauge errors or warnings in changed files).
  • Added isolated :error corpus cases in test/corpus/splitTryStatements.txt for joined #ifdef E try { ... }, split #ifdef opening try {, #elifdef, #else, #endif, and spliced #ifdef E\ followed by FOO;, plus unit assertions in test/unit/conditionalCpp.test.ts covering comments, same-line/wrapped/unspliced _Pragma(...), UCNs, spliced macro name spans, conditional marker selection with header _Pragma, and split #ifdef closing ERROR spans.
  • Verified 30-parse UTF-16 instruction counts remain below @willbooster/tree-sitter-cpp@34.0.1 (1,122.26M vs 1,136.32M, -1.24% on gcc/cp/parser.cc; 747.33M vs 769.59M, -2.89% on nlohmann/json v3.11.3 json.hpp).
  • Incremental parsing (Tree.edit) was not exercised because this fork maintains only full parsing.

Notes

  • Pre-existing code-gauge error in untouched src/pragma.h (scan_preproc_arg, synced from @willbooster/tree-sitter-c) is left unchanged; if vendored headers from @willbooster/tree-sitter-c should not be flagged here, src/pragma.h could be excluded in code-gauge.config.json.

Check in the external scanner that `#ifdef`, `#ifndef`, `#elifdef`,
`#elifndef`, `#else`, and `#endif` directive headers carry no extra
tokens before the end of the logical line, and emit `GNU_RAW_STRING_BODY`
during error recovery on malformed headers so `mergeableExternals`
cannot shift them without an `ERROR` node.

Co-authored-by: WillBooster (Antigravity) <agent@willbooster.com>
@exKAZUu exKAZUu self-assigned this Nov 23, 2026
exKAZUu and others added 8 commits November 23, 2026 13:57
Continue lexing `#ifdef`, `#ifndef`, `#elifdef`, and `#elifndef` macro
identifiers across backslash-newline splices in both `scan_cpp_ifdef_word_tail`
and `_preproc_ifdef_name` so phase-two spliced names remain single identifiers
while separate trailing tokens on the same logical line are still rejected.

Co-authored-by: WillBooster (Antigravity) <agent@willbooster.com>
…`#ifdef` headers

Record whether `scan_cpp_ifdef_header` or `scan_cpp_directive_line_end`
consumed a `_Pragma(...)` operator on the `#ifdef`/`#ifndef` line and skip
`scan_cpp_conditional_marker` when it did, matching `conditional_macro_body`
so headers carrying `_Pragma` extras remain standalone `preproc_ifdef` blocks.

Co-authored-by: WillBooster (Antigravity) <agent@willbooster.com>
Give each malformed `#ifdef`, `#elifdef`, `#else`, and `#endif` fixture in
`test/corpus/splitTryStatements.txt` its own `:error` corpus case so an error
in one function cannot mask a regression in another.

Co-authored-by: WillBooster (Antigravity) <agent@willbooster.com>
…lpers

Pass a scratch `bool` at callers that do not inspect `had_pragma` so
`scan_cpp_ifdef_header` and `scan_cpp_directive_line_end` avoid nullable
pointer branches and stay within the cognitive-complexity budget.

Co-authored-by: WillBooster (Antigravity) <agent@willbooster.com>
Keep `*ascii` set across trailing backslash-newline splices in
`scan_cpp_ifdef_word_tail` unless another identifier character or UCN
continues the word, so `_Pragma\` followed by `("...")` on the next physical
line is still recognized as a pragma extra on `#ifdef`/`#ifndef`/`#elifdef`/`#elifndef` headers.

Co-authored-by: WillBooster (Antigravity) <agent@willbooster.com>
…ective error tests

Add a semicolon to `FOO;` in the spliced `#ifdef` corpus case so it only fails
because of the joined directive token, and assert the `ERROR` node spanning the
closing `#ifdef` directly in `test/unit/conditionalCpp.test.ts`.

Co-authored-by: WillBooster (Antigravity) <agent@willbooster.com>
Verify in `test/unit/conditionalCpp.test.ts` that `#ifdef FOO\` followed by
`BAR` binds `FOO\\\nBAR` as the `preproc_ifdef` `name` field without producing
a `macro_specifier` on the guarded declaration.

Co-authored-by: WillBooster (Antigravity) <agent@willbooster.com>
…he logical line

Validate `_Pragma(...)` arguments on `#ifdef`, `#ifndef`, `#elifdef`,
`#elifndef`, `#else`, and `#endif` headers with `skip_cpp_ifdef_trivia`
instead of `scan_pragma_spacing_with` so unspliced newlines inside `_Pragma(...)`
are rejected as incomplete directive headers while spliced continuations and
block comments remain valid.

Co-authored-by: WillBooster (Antigravity) <agent@willbooster.com>
@exKAZUu
exKAZUu merged commit d448c03 into main Nov 23, 2026
12 of 13 checks passed
@exKAZUu
exKAZUu deleted the fix/ifdef-line-end branch November 23, 2026 06:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: report tokens joined to an #ifdef line in a block as an error

1 participant