Repository navigation
fix!: reject extra tokens on conditional directive lines - #228
Merged
Merged
Conversation
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>
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Close #222
Why
In a plain block,
preproc_ifdefand_try_opening_ifdefdid not require the#ifdef/#ifndefline to end after the macro identifier, and#elifdef,#elifndef,#else, and#endifdirectives without an explicit_preproc_newlinerule likewise accepted tokens joined on the same directive line withoutERROR(including#ifdef E try { g(); } catch (...) {}and splittryguards such as#ifdef E try {or#ifdef E } catch (...) {}). Adding a grammar-level newline or optional zero-width external token topreproc_ifdef_in_blockeither makes_preproc_newlinevalid in shared block states or alters GLR disambiguation on valid inputs such asModules in preprocessor sections.Requirements
required: Report tokens joined to#ifdef/#ifndefdirective lines in blocks (including#ifdef E try { g(); } catch (...) {}and splittry_statementopening/closing#ifdeflines) as anERROR, 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 produceERRORorMISSINGnodes in linear time without crashing or hanging.chosen: Validate directive line ends for#ifdef,#ifndef,#elifdef,#elifndef,#else, and#endifin the external scanner (src/cppTrivia.h,src/scanner.c) with apeek_unitsfast path, accept backslash-newline splices inside#ifdef/#ifndef/#elifdef/#elifndefmacro names in both the scanner and_preproc_ifdef_name, and emitGNU_RAW_STRING_BODYwhen 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++, andmergeableExternalswould otherwise shiftPREPROC_IFDEFerror-free in error recovery.chosen: Validate conditional directive headers and#endifline ends uniformly across all#ifand#ifdefbranches without evaluating#if 0or other preprocessor constant expressions, because both@willbooster/tree-sitter-cand@willbooster/tree-sitter-cppparse all conditional branches structurally as C++ (matchingpreproc_ifdef_in_if_blockandpreproc_ifdef_in_initializer).Customer Summary
Malformed
#ifdef,#ifndef,#elifdef,#elifndef,#else, and#endiflines that place extra C++ tokens on the same logical directive line now produce parseERRORnodes instead of being silently accepted as valid conditional blocks or splittrystatements, while conditional macro names split across physical lines with backslash-newline splices parse as a singlenameidentifier.Technical Summary
grammar.js: Routes#ifdef,#ifndef,#elifdef, and#elifndefnamefields through_preproc_ifdef_name, which pairsidentifierwith_spliced_preproc_identifier(aliased toidentifier) so phase-two spliced macro names bind as a singlenamenode.src/cppTrivia.h: Addspeek_cpp_ifdef_headerandpeek_cpp_directive_line_endto validate plain ASCII directive tails directly fromlexer->peek_unitswithout advancing the lexer, falling back toscan_cpp_ifdef_header,scan_cpp_ifdef_word,scan_cpp_directive_line_end, andscan_cpp_directive_pragma_argsacross comments, line splices, UCNs, and same-logical-line_Pragma(...)operators while recordinghad_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_endand!had_pragma && scan_cpp_conditional_markercontinue lookahead from that newline and restoremark_endto the directive keyword end. On rejection inerror_recovery,reject_cpp_directivereturnsGNU_RAW_STRING_BODYso tree-sitter wraps the malformed directive header in anERRORnode.Testing
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 fromv43.0.0), andbun run verify(0code-gaugeerrors or warnings in changed files).:errorcorpus cases intest/corpus/splitTryStatements.txtfor joined#ifdef E try { ... }, split#ifdefopeningtry {,#elifdef,#else,#endif, and spliced#ifdef E\followed byFOO;, plus unit assertions intest/unit/conditionalCpp.test.tscovering comments, same-line/wrapped/unspliced_Pragma(...), UCNs, spliced macronamespans, conditional marker selection with header_Pragma, and split#ifdefclosingERRORspans.@willbooster/tree-sitter-cpp@34.0.1(1,122.26Mvs1,136.32M,-1.24%ongcc/cp/parser.cc;747.33Mvs769.59M,-2.89%onnlohmann/jsonv3.11.3json.hpp).Tree.edit) was not exercised because this fork maintains only full parsing.Notes
code-gaugeerror in untouchedsrc/pragma.h(scan_preproc_arg, synced from@willbooster/tree-sitter-c) is left unchanged; if vendored headers from@willbooster/tree-sitter-cshould not be flagged here,src/pragma.hcould be excluded incode-gauge.config.json.