Skip to content

Commit c9e7be7

Browse files
committed
Ruby: generate overlay discard predicates
1 parent fa24ee6 commit c9e7be7

File tree

4 files changed

+287
-2
lines changed

4 files changed

+287
-2
lines changed

ruby/ql/lib/codeql/ruby/ast/internal/TreeSitter.qll

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
import codeql.Locations as L
77

8+
/** Holds if the database is an overlay. */
9+
overlay[local]
10+
private predicate isOverlay() { databaseMetadata("isOverlay", "true") }
11+
812
module Ruby {
913
/** The base class for all AST nodes */
1014
class AstNode extends @ruby_ast_node {
@@ -48,6 +52,30 @@ module Ruby {
4852
final override string getAPrimaryQlClass() { result = "ReservedWord" }
4953
}
5054

55+
/** Gets the file containing the given `node`. */
56+
overlay[local]
57+
private @file getNodeFile(@ruby_ast_node node) {
58+
exists(@location_default loc | ruby_ast_node_location(node, loc) |
59+
locations_default(loc, result, _, _, _, _)
60+
)
61+
}
62+
63+
/** Holds if `file` was extracted as part of the overlay database. */
64+
overlay[local]
65+
private predicate discardFile(@file file) { isOverlay() and file = getNodeFile(_) }
66+
67+
/** Holds if `node` is in the `file` and is part of the overlay base database. */
68+
overlay[local]
69+
private predicate discardableAstNode(@file file, @ruby_ast_node node) {
70+
not isOverlay() and file = getNodeFile(node)
71+
}
72+
73+
/** 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. */
74+
overlay[discard_entity]
75+
private predicate discardAstNode(@ruby_ast_node node) {
76+
exists(@file file | discardableAstNode(file, node) and discardFile(file))
77+
}
78+
5179
class UnderscoreArg extends @ruby_underscore_arg, AstNode { }
5280

5381
class UnderscoreCallOperator extends @ruby_underscore_call_operator, AstNode { }
@@ -1970,6 +1998,30 @@ module Erb {
19701998
final override string getAPrimaryQlClass() { result = "ReservedWord" }
19711999
}
19722000

2001+
/** Gets the file containing the given `node`. */
2002+
overlay[local]
2003+
private @file getNodeFile(@erb_ast_node node) {
2004+
exists(@location_default loc | erb_ast_node_location(node, loc) |
2005+
locations_default(loc, result, _, _, _, _)
2006+
)
2007+
}
2008+
2009+
/** Holds if `file` was extracted as part of the overlay database. */
2010+
overlay[local]
2011+
private predicate discardFile(@file file) { isOverlay() and file = getNodeFile(_) }
2012+
2013+
/** Holds if `node` is in the `file` and is part of the overlay base database. */
2014+
overlay[local]
2015+
private predicate discardableAstNode(@file file, @erb_ast_node node) {
2016+
not isOverlay() and file = getNodeFile(node)
2017+
}
2018+
2019+
/** 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. */
2020+
overlay[discard_entity]
2021+
private predicate discardAstNode(@erb_ast_node node) {
2022+
exists(@file file | discardableAstNode(file, node) and discardFile(file))
2023+
}
2024+
19732025
/** A class representing `code` tokens. */
19742026
class Code extends @erb_token_code, Token {
19752027
/** Gets the name of the primary QL class for this element. */

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub fn generate(
1717
languages: Vec<language::Language>,
1818
dbscheme_path: PathBuf,
1919
ql_library_path: PathBuf,
20-
add_metadata_relation: bool,
20+
overlay_support: bool,
2121
) -> std::io::Result<()> {
2222
let dbscheme_file = File::create(dbscheme_path).map_err(|e| {
2323
tracing::error!("Failed to create dbscheme file: {}", e);
@@ -35,7 +35,7 @@ pub fn generate(
3535

3636
// Eventually all languages will have the metadata relation (for overlay support), at which
3737
// point this could be moved to prefix.dbscheme.
38-
if add_metadata_relation {
38+
if overlay_support {
3939
writeln!(dbscheme_writer, "/*- Database metadata -*/",)?;
4040
dbscheme::write(
4141
&mut dbscheme_writer,
@@ -60,6 +60,15 @@ pub fn generate(
6060
})],
6161
)?;
6262

63+
if overlay_support {
64+
ql::write(
65+
&mut ql_writer,
66+
&[ql::TopLevel::Predicate(
67+
ql_gen::create_is_overlay_predicate(),
68+
)],
69+
)?;
70+
}
71+
6372
for language in languages {
6473
let prefix = node_types::to_snake_case(&language.name);
6574
let ast_node_name = format!("{}_ast_node", &prefix);
@@ -103,6 +112,22 @@ pub fn generate(
103112
ql::TopLevel::Class(ql_gen::create_token_class(&token_name, &tokeninfo_name)),
104113
ql::TopLevel::Class(ql_gen::create_reserved_word_class(&reserved_word_name)),
105114
];
115+
116+
if overlay_support {
117+
body.push(ql::TopLevel::Predicate(
118+
ql_gen::create_get_node_file_predicate(&ast_node_name, &node_location_table_name),
119+
));
120+
body.push(ql::TopLevel::Predicate(
121+
ql_gen::create_discard_file_predicate(),
122+
));
123+
body.push(ql::TopLevel::Predicate(
124+
ql_gen::create_discardable_ast_node_predicate(&ast_node_name),
125+
));
126+
body.push(ql::TopLevel::Predicate(
127+
ql_gen::create_discard_ast_node_predicate(&ast_node_name),
128+
));
129+
}
130+
106131
body.append(&mut ql_gen::convert_nodes(&nodes));
107132
ql::write(
108133
&mut ql_writer,

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub enum TopLevel<'a> {
66
Class(Class<'a>),
77
Import(Import<'a>),
88
Module(Module<'a>),
9+
Predicate(Predicate<'a>),
910
}
1011

1112
impl fmt::Display for TopLevel<'_> {
@@ -14,6 +15,7 @@ impl fmt::Display for TopLevel<'_> {
1415
TopLevel::Import(imp) => write!(f, "{}", imp),
1516
TopLevel::Class(cls) => write!(f, "{}", cls),
1617
TopLevel::Module(m) => write!(f, "{}", m),
18+
TopLevel::Predicate(pred) => write!(f, "{}", pred),
1719
}
1820
}
1921
}
@@ -68,10 +70,12 @@ impl fmt::Display for Class<'_> {
6870
qldoc: None,
6971
name: self.name,
7072
overridden: false,
73+
is_private: false,
7174
is_final: false,
7275
return_type: None,
7376
formal_parameters: vec![],
7477
body: charpred.clone(),
78+
overlay: None,
7579
}
7680
)?;
7781
}
@@ -150,6 +154,7 @@ pub enum Expression<'a> {
150154
expr: Box<Expression<'a>>,
151155
second_expr: Option<Box<Expression<'a>>>,
152156
},
157+
Negation(Box<Expression<'a>>),
153158
}
154159

155160
impl fmt::Display for Expression<'_> {
@@ -231,26 +236,46 @@ impl fmt::Display for Expression<'_> {
231236
}
232237
write!(f, ")")
233238
}
239+
Expression::Negation(e) => write!(f, "not ({})", e),
234240
}
235241
}
236242
}
237243

244+
#[derive(Clone, Eq, PartialEq, Hash)]
245+
pub enum OverlayAnnotation {
246+
Local,
247+
DiscardEntity,
248+
}
249+
238250
#[derive(Clone, Eq, PartialEq, Hash)]
239251
pub struct Predicate<'a> {
240252
pub qldoc: Option<String>,
241253
pub name: &'a str,
242254
pub overridden: bool,
255+
pub is_private: bool,
243256
pub is_final: bool,
244257
pub return_type: Option<Type<'a>>,
245258
pub formal_parameters: Vec<FormalParameter<'a>>,
246259
pub body: Expression<'a>,
260+
pub overlay: Option<OverlayAnnotation>,
247261
}
248262

249263
impl fmt::Display for Predicate<'_> {
250264
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
251265
if let Some(qldoc) = &self.qldoc {
252266
write!(f, "/** {} */", qldoc)?;
253267
}
268+
if let Some(overlay_annotation) = &self.overlay {
269+
write!(f, "overlay[")?;
270+
match overlay_annotation {
271+
OverlayAnnotation::Local => write!(f, "local")?,
272+
OverlayAnnotation::DiscardEntity => write!(f, "discard_entity")?,
273+
}
274+
write!(f, "] ")?;
275+
}
276+
if self.is_private {
277+
write!(f, "private ")?;
278+
}
254279
if self.is_final {
255280
write!(f, "final ")?;
256281
}

0 commit comments

Comments
 (0)