Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Ruby: generate overlay discard predicates #19719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 25, 2025

Conversation

nickrolfe
Copy link
Contributor

Stacked on top of #19684

@github-actions github-actions bot added the Ruby label Jun 10, 2025
@nickrolfe nickrolfe force-pushed the nickrolfe/ruby-overlay-extraction branch from fb89f22 to 576f0d2 Compare June 11, 2025 10:27
@nickrolfe nickrolfe force-pushed the nickrolfe/ruby-discard-predicates branch from b74138e to 8a33cb4 Compare June 11, 2025 10:29
@nickrolfe nickrolfe marked this pull request as ready for review June 11, 2025 10:31
@Copilot Copilot AI review requested due to automatic review settings June 11, 2025 10:31
@nickrolfe nickrolfe requested a review from a team as a code owner June 11, 2025 10:31
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for overlay-based discard predicates in the Ruby Tree-sitter extractor, enabling selective filtering of AST nodes and files when running on overlay databases.

  • Introduces OverlayAnnotation and PragmaAnnotation in the QL generator to emit overlay/pragma directives.
  • Extends ql_gen to produce new predicates (isOverlay, getRawFile, discardFile, discardableAstNode, discardAstNode).
  • Updates the code generator to conditionally include overlay predicates and adds static definitions in the Ruby QLL.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
shared/tree-sitter-extractor/src/generator/ql_gen.rs Added functions to generate overlay discard predicates.
shared/tree-sitter-extractor/src/generator/ql.rs Added TopLevel::Predicate, PragmaAnnotation, OverlayAnnotation, and Expression::Negation.
shared/tree-sitter-extractor/src/generator/mod.rs Renamed add_metadata_relation to overlay_support and wired in overlay predicates.
ruby/ql/lib/codeql/ruby/ast/internal/TreeSitter.qll Added overlay/pragma annotations and new predicates for Ruby and ERB modules.
Comments suppressed due to low confidence (2)

shared/tree-sitter-extractor/src/generator/ql.rs:271

  • Add a space or newline after the closing doc comment for readability, e.g.: write!(f, "/** {} */ ", qldoc)?; so that overlay[...] is clearly separated.
write!(f, "/** {} */", qldoc)?;

shared/tree-sitter-extractor/src/generator/mod.rs:63

  • Consider adding unit or integration tests to verify that overlay_support correctly causes the metadata relation and overlay predicates to be written.
if overlay_support {

Copy link
Contributor

@jbj jbj left a comment

Choose a reason for hiding this comment

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

I have only some cosmetic comments

@nickrolfe nickrolfe force-pushed the nickrolfe/ruby-discard-predicates branch from 8a33cb4 to acf0fe3 Compare June 11, 2025 11:39
@nickrolfe nickrolfe force-pushed the nickrolfe/ruby-overlay-extraction branch from 576f0d2 to 4029b89 Compare June 11, 2025 11:45
@nickrolfe nickrolfe requested review from a team as code owners June 11, 2025 11:45
@nickrolfe nickrolfe force-pushed the nickrolfe/ruby-discard-predicates branch from acf0fe3 to 4875dcb Compare June 11, 2025 11:45
@nickrolfe
Copy link
Contributor Author

@aibaars if you assume the generated QL is correct, would you be able to review the generator changes?

jbj
jbj previously approved these changes Jun 13, 2025
Copy link
Contributor

@jbj jbj left a comment

Choose a reason for hiding this comment

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

The QL changes LGTM now, assuming you've tested that it works in practice.

@nickrolfe nickrolfe force-pushed the nickrolfe/ruby-discard-predicates branch from 4875dcb to 3d78f35 Compare June 17, 2025 08:47
hvitved
hvitved previously approved these changes Jun 17, 2025
aibaars
aibaars previously approved these changes Jun 19, 2025
Copy link
Contributor

@aibaars aibaars left a comment

Choose a reason for hiding this comment

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

Looks good to me. Is there any harm in generating these predicates for other tree-sitter based extractors as well? if not , we might just as well include them by default.

@jbj
Copy link
Contributor

jbj commented Jun 19, 2025

I think @kaspersv determined that pragma[nomagic] isn't needed on any of the predicates. It was only needed in an early compiler prototype. Is that right, Kasper?

Is there any harm in generating these predicates for other tree-sitter based extractors as well?

I don't think there is, but it looks like if overlay_support also guards the addition of the databaseMetadata relation to the dbscheme, so I presume all languages will need a dbscheme upgrade if overlay is universally enabled. That seems like a lot to add to this PR.

@nickrolfe
Copy link
Contributor Author

Is there any harm in generating these predicates for other tree-sitter based extractors as well?

I don't think there is, but it looks like if overlay_support also guards the addition of the databaseMetadata relation to the dbscheme, so I presume all languages will need a dbscheme upgrade if overlay is universally enabled. That seems like a lot to add to this PR.

Right. I don't think we normally bother with upgrade scripts for QL-for-QL, but I would prefer not to deal with updating its dbscheme nonetheless, for now.

@kaspersv
Copy link
Contributor

I think @kaspersv determined that pragma[nomagic] isn't needed on any of the predicates. It was only needed in an early compiler prototype. Is that right, Kasper?

Yes, that is correct. In theory they should no longer affect the semantics of overlay analysis and a DCA experiment for Java showed no impact of removing them on overlay analysis accuracy (without nomagic annotations vs baseline).

Which reminds me, I should update the Incremental CodeQL docs to remove the nomagic pragmas in examples.

@nickrolfe nickrolfe force-pushed the nickrolfe/ruby-overlay-extraction branch from 4029b89 to fa24ee6 Compare June 19, 2025 14:57
@nickrolfe nickrolfe force-pushed the nickrolfe/ruby-discard-predicates branch from 3d78f35 to c9e7be7 Compare June 19, 2025 15:20
@nickrolfe nickrolfe added the no-change-note-required This PR does not need a change note label Jun 19, 2025
@nickrolfe
Copy link
Contributor Author

I've removed the pragma[nomagic] annotations.

Looks good to me. Is there any harm in generating these predicates for other tree-sitter based extractors as well? if not , we might just as well include them by default.

As discussed in the extractor PR, I can do a quick follow-up PR that adds the databaseMetadata relation to the QL-for-QL dbscheme, and I can make this simplification at the same time.

@nickrolfe nickrolfe force-pushed the nickrolfe/ruby-overlay-extraction branch from fa24ee6 to 665df4b Compare June 19, 2025 15:34
@nickrolfe nickrolfe force-pushed the nickrolfe/ruby-discard-predicates branch from c9e7be7 to c968a95 Compare June 19, 2025 15:36
Base automatically changed from nickrolfe/ruby-overlay-extraction to main June 25, 2025 10:39
@nickrolfe nickrolfe dismissed stale reviews from aibaars, hvitved, and jbj June 25, 2025 10:39

The base branch was changed.

@@ -5,6 +5,10 @@

import codeql.Locations as L

/** Holds if the database is an overlay. */
overlay[local]
private predicate isOverlay() { databaseMetadata("isOverlay", "true") }

Check warning

Code scanning / CodeQL

Dead code Warning

This code is never used, and it's not publicly exported.
@@ -48,6 +52,30 @@
final override string getAPrimaryQlClass() { result = "ReservedWord" }
}

/** Gets the file containing the given `node`. */
overlay[local]
private @file getNodeFile(@ruby_ast_node node) {

Check warning

Code scanning / CodeQL

Dead code Warning

This code is never used, and it's not publicly exported.

/** Holds if `file` was extracted as part of the overlay database. */
overlay[local]
private predicate discardFile(@file file) { isOverlay() and file = getNodeFile(_) }

Check warning

Code scanning / CodeQL

Dead code Warning

This code is never used, and it's not publicly exported.

/** Holds if `node` is in the `file` and is part of the overlay base database. */
overlay[local]
private predicate discardableAstNode(@file file, @ruby_ast_node node) {

Check warning

Code scanning / CodeQL

Dead code Warning

This code is never used, and it's not publicly exported.

/** Holds if `node` should be discarded, because it is part of the overlay base and is in a file that was also extracted as part of the overlay database. */
overlay[discard_entity]
private predicate discardAstNode(@ruby_ast_node node) {

Check warning

Code scanning / CodeQL

Dead code Warning

This code is never used, and it's not publicly exported.
@@ -1970,6 +1998,30 @@
final override string getAPrimaryQlClass() { result = "ReservedWord" }
}

/** Gets the file containing the given `node`. */
overlay[local]
private @file getNodeFile(@erb_ast_node node) {

Check warning

Code scanning / CodeQL

Dead code Warning

This code is never used, and it's not publicly exported.

/** Holds if `file` was extracted as part of the overlay database. */
overlay[local]
private predicate discardFile(@file file) { isOverlay() and file = getNodeFile(_) }

Check warning

Code scanning / CodeQL

Dead code Warning

This code is never used, and it's not publicly exported.

/** Holds if `node` is in the `file` and is part of the overlay base database. */
overlay[local]
private predicate discardableAstNode(@file file, @erb_ast_node node) {

Check warning

Code scanning / CodeQL

Dead code Warning

This code is never used, and it's not publicly exported.

/** Holds if `node` should be discarded, because it is part of the overlay base and is in a file that was also extracted as part of the overlay database. */
overlay[discard_entity]
private predicate discardAstNode(@erb_ast_node node) {

Check warning

Code scanning / CodeQL

Dead code Warning

This code is never used, and it's not publicly exported.
@nickrolfe nickrolfe force-pushed the nickrolfe/ruby-discard-predicates branch from e36a625 to a9ddf00 Compare June 25, 2025 10:48
@nickrolfe nickrolfe merged commit 1e68a7e into main Jun 25, 2025
51 checks passed
@nickrolfe nickrolfe deleted the nickrolfe/ruby-discard-predicates branch June 25, 2025 11:18
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 Ruby
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants