Skip to content

unified: Add side table for extracting interpreted string values - #23271

Open
tausbn wants to merge 6 commits into
mainfrom
tausbn/yeast-post-extract-side-tables
Open

tausbn wants to merge 6 commits into
mainfrom
tausbn/yeast-post-extract-side-tables

Conversation

@tausbn

@tausbn tausbn commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Adds support for specifying a piece of code that runs after normal extraction has taken place, with access to things like the trap_writer. This enables us to easily add "side tables" to an analysis, by explicitly tracking the tuples we want to emit inside the user context, and then at the end calling add_tuple for each of them.

This mechanism is very flexible -- the current implementation just adds a simple table for specifying interpreted string values for Swift strings, but in general you can stuff anything that add_tuple understands into these tables.

One wrinkle that one should be aware of when reviewing this: because we allow nodes to appear in multiple places in the output AST -- e.g. when we store an OutputId in the context and later use it, as we do in Swift -- there is in general not a 1-1 correspondence between OutputIds and TRAP IDs. So, when we output the AST, we also record for each OutputId the list of TRAP IDs that it corresponds to. We can't simply reuse the TRAP IDs, because these entities may have different parents and indices.

It may be desirable at some point to enforce that the result of desugaring is an actual tree, but for now I expect the difference to be negligible. Almost all OutputIds will in fact correspond to a single TRAP ID.


Can be reviewed commit-by-commit.

@tausbn tausbn added the no-change-note-required This PR does not need a change note label Nov 26, 2026
Expose desugaring entry points that use a caller-provided context so
language integrations can inspect context-owned data after translation.

The context remains isolated through YEAST's existing clone-on-rule
behavior and no longer needs to be `Send + Sync`.

This also makes it so that we can use `Rc<RefCell<...>>` instead of
`Arc<Mutex<...>>` and thus avoid a bunch of pointless locking.
Return the TRAP labels allocated for each reachable YEAST `OutputId`. An
output node may occur in multiple structural positions, so retain every
occurrence label.

Separate output-AST extraction from parsing and desugaring so language
pipelines can run work after labels have been assigned.
Keep each language's parser, desugarer, context type, schema, and
optional post-extraction callback behind one private pipeline object.

This type-erases the context once without duplicating parser or
desugarer state, while retaining explicit parse and desugar entry points
for extractor tests.
Restore the TRAP writer to its pre-attempt state when a matching
language extractor returns an error. This prevents partial AST or
post-extraction tuples from leaking into later successful language
output.

Extend the driver test with a write-then-fail attempt followed by a
successful fallback.
Add a generated side-table relation and façade getter for
extractor-provided string values.
Collect Swift string values in the per-file YEAST context and emit them
after AST extraction for every structural occurrence label.

For now "interpreted" just means "quotes stripped". At a later point, we
should replace this with code that evaluates strings in the same way
that Swift does.
@tausbn
tausbn force-pushed the tausbn/yeast-post-extract-side-tables branch from 6be870c to 3f37bb2 Compare November 26, 2026 22:51
@tausbn
tausbn marked this pull request as ready for review November 27, 2026 11:32
@tausbn
tausbn requested review from a team as code owners November 27, 2026 11:32
Copilot AI adaptive review requested due to automatic review settings November 27, 2026 11:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The one-to-many OutputId mapping lacks an extraction-level regression verifying that a literal reused under two parents produces distinct TRAP labels, correct parent/index information, and interpreted-value tuples for both occurrences.

1 open issue
What changed in this PR

Adds post-extraction hooks to the shared desugaring pipeline, enabling side tables populated from language-specific context. Swift uses this mechanism to expose string values through the unified QL library.

Changes:

  • Track every TRAP label associated with an output node, including reused nodes.
  • Add Swift string-value collection, schema generation, and QL accessors.
  • Update desugaring callers and restore TRAP output after failed extraction attempts.
File Description
unified/​ql/​lib/​unified.dbscheme Adds the string-value relation.
unified/​ql/​lib/​codeql/​unified/​internal/​FacadeAst.qll Reads string values from the new relation.
unified/​extractor/​tests/​swift_syntax_pipeline.rs Uses the language-level desugaring API.
unified/​extractor/​tests/​location_tests.rs Updates parsing and desugaring calls.
unified/​extractor/​tests/​corpus_tests.rs Updates corpus helpers to the new API.
unified/​extractor/​string_values.dbscheme Defines the side-table schema fragment.
unified/​extractor/​src/​languages/​swift/​swift.rs Collects string values and emits side-table tuples.
unified/​extractor/​src/​generator.rs Appends the side-table schema during generation.
unified/​extractor/​BUILD.bazel Includes the schema fragment as compile-time data.
shared/​yeast/​src/​lib.rs Adds desugaring with caller-provided context.
shared/​tree-sitter-extractor/​src/​trap.rs Makes TRAP writers cloneable for rollback.
shared/​tree-sitter-extractor/​src/​extractor/​mod.rs Returns output-node-to-TRAP-label mappings.
shared/​tree-sitter-extractor/​src/​extractor/​driver.rs Restores failed output and tests fallback extraction.
shared/​tree-sitter-extractor/​src/​extractor/​desugaring.rs Introduces the pipeline builder and post-extraction hook.

Review effort: Balanced (adaptive)

let recurse = self.enter_node(node);
if recurse {
let label = self.stack.last().expect("entered node must be on stack").0;
self.output_labels.entry(id).or_default().push(label);

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-change-note-required This PR does not need a change note Unified

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants