Skip to content

Commit b74138e

Browse files
committed
Ruby: generate overlay discard predicates
1 parent fb89f22 commit b74138e

File tree

4 files changed

+337
-2
lines changed

4 files changed

+337
-2
lines changed

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

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

66
import codeql.Locations as L
77

8+
/** Holds if and only if the database is an overlay. */
9+
overlay[local]
10+
pragma[nomagic]
11+
predicate isOverlay() { databaseMetadata("isOverlay", "true") }
12+
813
module Ruby {
914
/** The base class for all AST nodes */
1015
class AstNode extends @ruby_ast_node {
@@ -48,6 +53,38 @@ module Ruby {
4853
final override string getAPrimaryQlClass() { result = "ReservedWord" }
4954
}
5055

56+
/** Gets the file path of the given node. */
57+
overlay[local]
58+
pragma[nomagic]
59+
string getRawFile(@ruby_ast_node node) {
60+
exists(@location_default loc, @file file |
61+
ruby_ast_node_location(node, loc) and
62+
locations_default(loc, file, _, _, _, _) and
63+
files(file, result)
64+
)
65+
}
66+
67+
/** Holds if the given file was extracted as part of the overlay database. */
68+
overlay[local]
69+
pragma[nomagic]
70+
predicate discardFile(string file) {
71+
isOverlay() and exists(@ruby_ast_node node | file = getRawFile(node))
72+
}
73+
74+
/** Holds if the given node is in the given file and is part of the overlay base database */
75+
overlay[local]
76+
pragma[nomagic]
77+
predicate discardableAstNode(string file, @ruby_ast_node node) {
78+
not isOverlay() and file = getRawFile(node)
79+
}
80+
81+
/** Holds if the given node should be discarded, because it is part of the overlay base and is in a file that was extracted as part of the overlay database. */
82+
overlay[discard_entity]
83+
pragma[nomagic]
84+
predicate discardAstNode(@ruby_ast_node node) {
85+
exists(string file | discardableAstNode(file, node) and discardFile(file))
86+
}
87+
5188
class UnderscoreArg extends @ruby_underscore_arg, AstNode { }
5289

5390
class UnderscoreCallOperator extends @ruby_underscore_call_operator, AstNode { }
@@ -1970,6 +2007,38 @@ module Erb {
19702007
final override string getAPrimaryQlClass() { result = "ReservedWord" }
19712008
}
19722009

2010+
/** Gets the file path of the given node. */
2011+
overlay[local]
2012+
pragma[nomagic]
2013+
string getRawFile(@erb_ast_node node) {
2014+
exists(@location_default loc, @file file |
2015+
erb_ast_node_location(node, loc) and
2016+
locations_default(loc, file, _, _, _, _) and
2017+
files(file, result)
2018+
)
2019+
}
2020+
2021+
/** Holds if the given file was extracted as part of the overlay database. */
2022+
overlay[local]
2023+
pragma[nomagic]
2024+
predicate discardFile(string file) {
2025+
isOverlay() and exists(@erb_ast_node node | file = getRawFile(node))
2026+
}
2027+
2028+
/** Holds if the given node is in the given file and is part of the overlay base database */
2029+
overlay[local]
2030+
pragma[nomagic]
2031+
predicate discardableAstNode(string file, @erb_ast_node node) {
2032+
not isOverlay() and file = getRawFile(node)
2033+
}
2034+
2035+
/** Holds if the given node should be discarded, because it is part of the overlay base and is in a file that was extracted as part of the overlay database. */
2036+
overlay[discard_entity]
2037+
pragma[nomagic]
2038+
predicate discardAstNode(@erb_ast_node node) {
2039+
exists(string file | discardableAstNode(file, node) and discardFile(file))
2040+
}
2041+
19732042
/** A class representing `code` tokens. */
19742043
class Code extends @erb_token_code, Token {
19752044
/** 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_raw_file_predicate(&ast_node_name, &node_location_table_name),
119+
));
120+
body.push(ql::TopLevel::Predicate(
121+
ql_gen::create_discard_file_predicate(&ast_node_name),
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: 34 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
}
@@ -72,6 +74,8 @@ impl fmt::Display for Class<'_> {
7274
return_type: None,
7375
formal_parameters: vec![],
7476
body: charpred.clone(),
77+
pragma: None,
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,10 +236,22 @@ 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 PragmaAnnotation {
246+
Nomagic,
247+
}
248+
249+
#[derive(Clone, Eq, PartialEq, Hash)]
250+
pub enum OverlayAnnotation {
251+
Local,
252+
DiscardEntity,
253+
}
254+
238255
#[derive(Clone, Eq, PartialEq, Hash)]
239256
pub struct Predicate<'a> {
240257
pub qldoc: Option<String>,
@@ -244,13 +261,30 @@ pub struct Predicate<'a> {
244261
pub return_type: Option<Type<'a>>,
245262
pub formal_parameters: Vec<FormalParameter<'a>>,
246263
pub body: Expression<'a>,
264+
pub pragma: Option<PragmaAnnotation>,
265+
pub overlay: Option<OverlayAnnotation>,
247266
}
248267

249268
impl fmt::Display for Predicate<'_> {
250269
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
251270
if let Some(qldoc) = &self.qldoc {
252271
write!(f, "/** {} */", qldoc)?;
253272
}
273+
if let Some(overlay_annotation) = &self.overlay {
274+
write!(f, "overlay[")?;
275+
match overlay_annotation {
276+
OverlayAnnotation::Local => write!(f, "local")?,
277+
OverlayAnnotation::DiscardEntity => write!(f, "discard_entity")?,
278+
}
279+
write!(f, "] ")?;
280+
}
281+
if let Some(pragma) = &self.pragma {
282+
write!(f, "pragma[")?;
283+
match pragma {
284+
PragmaAnnotation::Nomagic => write!(f, "nomagic")?,
285+
}
286+
write!(f, "] ")?;
287+
}
254288
if self.is_final {
255289
write!(f, "final ")?;
256290
}

0 commit comments

Comments
 (0)