Skip to content

Commit 5a176d6

Browse files
authored
Merge pull request #19878 from github/nickrolfe/ql-overlay
Ruby/Rust/QL: simplify generation of overlay-related tables/predicates
2 parents ae36f94 + 8678264 commit 5a176d6

File tree

15 files changed

+14721
-59
lines changed

15 files changed

+14721
-59
lines changed

.github/workflows/ql-for-ql-dataset_measure.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ jobs:
5353
- name: Create database
5454
run: |
5555
"${CODEQL}" database create \
56-
--search-path "${{ github.workspace }}"
56+
--search-path "${{ github.workspace }}" \
5757
--threads 4 \
5858
--language ql --source-root "${{ github.workspace }}/repo" \
5959
"${{ runner.temp }}/database"

ql/extractor/src/generator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ pub fn run(options: Options) -> std::io::Result<()> {
3636
},
3737
];
3838

39-
generate(languages, options.dbscheme, options.library, false)
39+
generate(languages, options.dbscheme, options.library)
4040
}

ql/ql/src/codeql_ql/ast/internal/TreeSitter.qll

Lines changed: 100 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 QL {
913
/** The base class for all AST nodes */
1014
class AstNode extends @ql_ast_node {
@@ -48,6 +52,30 @@ module QL {
4852
final override string getAPrimaryQlClass() { result = "ReservedWord" }
4953
}
5054

55+
/** Gets the file containing the given `node`. */
56+
overlay[local]
57+
private @file getNodeFile(@ql_ast_node node) {
58+
exists(@location_default loc | ql_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, @ql_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(@ql_ast_node node) {
76+
exists(@file file | discardableAstNode(file, node) and discardFile(file))
77+
}
78+
5179
/** A class representing `add_expr` nodes. */
5280
class AddExpr extends @ql_add_expr, AstNode {
5381
/** Gets the name of the primary QL class for this element. */
@@ -1318,6 +1346,30 @@ module Dbscheme {
13181346
final override string getAPrimaryQlClass() { result = "ReservedWord" }
13191347
}
13201348

1349+
/** Gets the file containing the given `node`. */
1350+
overlay[local]
1351+
private @file getNodeFile(@dbscheme_ast_node node) {
1352+
exists(@location_default loc | dbscheme_ast_node_location(node, loc) |
1353+
locations_default(loc, result, _, _, _, _)
1354+
)
1355+
}
1356+
1357+
/** Holds if `file` was extracted as part of the overlay database. */
1358+
overlay[local]
1359+
private predicate discardFile(@file file) { isOverlay() and file = getNodeFile(_) }
1360+
1361+
/** Holds if `node` is in the `file` and is part of the overlay base database. */
1362+
overlay[local]
1363+
private predicate discardableAstNode(@file file, @dbscheme_ast_node node) {
1364+
not isOverlay() and file = getNodeFile(node)
1365+
}
1366+
1367+
/** 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. */
1368+
overlay[discard_entity]
1369+
private predicate discardAstNode(@dbscheme_ast_node node) {
1370+
exists(@file file | discardableAstNode(file, node) and discardFile(file))
1371+
}
1372+
13211373
/** A class representing `annotName` tokens. */
13221374
class AnnotName extends @dbscheme_token_annot_name, Token {
13231375
/** Gets the name of the primary QL class for this element. */
@@ -1654,6 +1706,30 @@ module Blame {
16541706
final override string getAPrimaryQlClass() { result = "ReservedWord" }
16551707
}
16561708

1709+
/** Gets the file containing the given `node`. */
1710+
overlay[local]
1711+
private @file getNodeFile(@blame_ast_node node) {
1712+
exists(@location_default loc | blame_ast_node_location(node, loc) |
1713+
locations_default(loc, result, _, _, _, _)
1714+
)
1715+
}
1716+
1717+
/** Holds if `file` was extracted as part of the overlay database. */
1718+
overlay[local]
1719+
private predicate discardFile(@file file) { isOverlay() and file = getNodeFile(_) }
1720+
1721+
/** Holds if `node` is in the `file` and is part of the overlay base database. */
1722+
overlay[local]
1723+
private predicate discardableAstNode(@file file, @blame_ast_node node) {
1724+
not isOverlay() and file = getNodeFile(node)
1725+
}
1726+
1727+
/** 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. */
1728+
overlay[discard_entity]
1729+
private predicate discardAstNode(@blame_ast_node node) {
1730+
exists(@file file | discardableAstNode(file, node) and discardFile(file))
1731+
}
1732+
16571733
/** A class representing `blame_entry` nodes. */
16581734
class BlameEntry extends @blame_blame_entry, AstNode {
16591735
/** Gets the name of the primary QL class for this element. */
@@ -1767,6 +1843,30 @@ module JSON {
17671843
final override string getAPrimaryQlClass() { result = "ReservedWord" }
17681844
}
17691845

1846+
/** Gets the file containing the given `node`. */
1847+
overlay[local]
1848+
private @file getNodeFile(@json_ast_node node) {
1849+
exists(@location_default loc | json_ast_node_location(node, loc) |
1850+
locations_default(loc, result, _, _, _, _)
1851+
)
1852+
}
1853+
1854+
/** Holds if `file` was extracted as part of the overlay database. */
1855+
overlay[local]
1856+
private predicate discardFile(@file file) { isOverlay() and file = getNodeFile(_) }
1857+
1858+
/** Holds if `node` is in the `file` and is part of the overlay base database. */
1859+
overlay[local]
1860+
private predicate discardableAstNode(@file file, @json_ast_node node) {
1861+
not isOverlay() and file = getNodeFile(node)
1862+
}
1863+
1864+
/** 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. */
1865+
overlay[discard_entity]
1866+
private predicate discardAstNode(@json_ast_node node) {
1867+
exists(@file file | discardableAstNode(file, node) and discardFile(file))
1868+
}
1869+
17701870
class UnderscoreValue extends @json_underscore_value, AstNode { }
17711871

17721872
/** A class representing `array` nodes. */

ql/ql/src/ql.dbscheme

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ yaml_locations(unique int locatable: @yaml_locatable ref,
108108

109109
@yaml_locatable = @yaml_node | @yaml_error;
110110

111+
/*- Database metadata -*/
112+
databaseMetadata(
113+
string metadataKey: string ref,
114+
string value: string ref
115+
);
116+
111117
/*- QL dbscheme -*/
112118
@ql_add_expr_left_type = @ql_add_expr | @ql_aggregate | @ql_call_or_unqual_agg_expr | @ql_comp_term | @ql_conjunction | @ql_disjunction | @ql_expr_annotation | @ql_if_term | @ql_implication | @ql_in_expr | @ql_instance_of | @ql_literal | @ql_mul_expr | @ql_negation | @ql_par_expr | @ql_prefix_cast | @ql_qualified_expr | @ql_quantified | @ql_range | @ql_set_literal | @ql_special_call | @ql_super_ref | @ql_unary_expr | @ql_variable
113119

ql/ql/src/ql.dbscheme.stats

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22436,5 +22436,41 @@
2243622436
</dep>
2243722437
</dependencies>
2243822438
</relation>
22439+
<relation>
22440+
<name>databaseMetadata</name>
22441+
<cardinality>1</cardinality>
22442+
<columnsizes>
22443+
<e>
22444+
<k>metadataKey</k>
22445+
<v>1</v>
22446+
</e>
22447+
<e>
22448+
<k>value</k>
22449+
<v>1</v>
22450+
</e>
22451+
</columnsizes>
22452+
<dependencies>
22453+
<dep>
22454+
<src>metadataKey</src>
22455+
<trg>value</trg>
22456+
<val>
22457+
<hist>
22458+
<budget>12</budget>
22459+
<bs/>
22460+
</hist>
22461+
</val>
22462+
</dep>
22463+
<dep>
22464+
<src>value</src>
22465+
<trg>metadataKey</trg>
22466+
<val>
22467+
<hist>
22468+
<budget>12</budget>
22469+
<bs/>
22470+
</hist>
22471+
</val>
22472+
</dep>
22473+
</dependencies>
22474+
</relation>
2243922475
</stats>
2244022476
</dbstats>

ruby/extractor/src/generator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ pub fn run(options: Options) -> std::io::Result<()> {
2828
},
2929
];
3030

31-
generate(languages, options.dbscheme, options.library, true)
31+
generate(languages, options.dbscheme, options.library)
3232
}

0 commit comments

Comments
 (0)