Skip to content

Commit d2bc66e

Browse files
committed
QL: switch to shared YAML extractor
1 parent 6d7e95a commit d2bc66e

File tree

16 files changed

+197
-381
lines changed

16 files changed

+197
-381
lines changed

ql/Cargo.lock

Lines changed: 0 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ql/extractor/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ edition = "2018"
1010
tree-sitter = ">= 0.20, < 0.21"
1111
tree-sitter-ql = { git = "https://github.com/tree-sitter/tree-sitter-ql.git", rev = "d08db734f8dc52f6bc04db53a966603122bc6985"}
1212
tree-sitter-ql-dbscheme = { git = "https://github.com/erik-krogh/tree-sitter-ql-dbscheme.git", rev = "63e1344353f63931e88bfbc2faa2e78e1421b213"}
13-
tree-sitter-ql-yaml = {git = "https://github.com/erik-krogh/tree-sitter-ql.git", rev = "cf704bf3671e1ae148e173464fb65a4d2bbf5f99"}
1413
tree-sitter-blame = {path = "../buramu/tree-sitter-blame"}
1514
tree-sitter-json = {git = "https://github.com/tausbn/tree-sitter-json.git", rev = "745663ee997f1576fe1e7187e6347e0db36ec7a9"}
1615
clap = { version = "4.2", features = ["derive"] }

ql/extractor/src/autobuilder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub fn run(_: Options) -> std::io::Result<()> {
1515

1616
autobuilder::Autobuilder::new("ql", PathBuf::from(database))
1717
.include_extensions(&[".ql", ".qll", ".dbscheme", ".json", ".jsonc", ".jsonl"])
18-
.include_globs(&["**/qlpack.yml", "deprecated.blame"])
18+
.include_globs(&["deprecated.blame"])
1919
.size_limit("10m")
2020
.run()
2121
}

ql/extractor/src/extractor.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,6 @@ pub fn run(options: Options) -> std::io::Result<()> {
4242
node_types: tree_sitter_ql_dbscheme::NODE_TYPES,
4343
file_extensions: vec!["dbscheme".into()],
4444
},
45-
simple::LanguageSpec {
46-
prefix: "yaml",
47-
ts_language: tree_sitter_ql_yaml::language(),
48-
node_types: tree_sitter_ql_yaml::NODE_TYPES,
49-
file_extensions: vec!["yml".into()],
50-
},
5145
simple::LanguageSpec {
5246
prefix: "json",
5347
ts_language: tree_sitter_json::language(),

ql/extractor/src/generator.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ pub fn run(options: Options) -> std::io::Result<()> {
3131
name: "Dbscheme".to_owned(),
3232
node_types: tree_sitter_ql_dbscheme::NODE_TYPES,
3333
},
34-
Language {
35-
name: "Yaml".to_owned(),
36-
node_types: tree_sitter_ql_yaml::NODE_TYPES,
37-
},
3834
Language {
3935
name: "Blame".to_owned(),
4036
node_types: tree_sitter_blame::NODE_TYPES,

ql/ql/src/codeql_ql/ast/Ast.qll

Lines changed: 35 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ class AstNode extends TAstNode {
3838
result = node.getLocation()
3939
)
4040
or
41-
result = toGenerateYaml(this).getLocation()
42-
or
4341
result = toDbscheme(this).getLocation()
4442
}
4543

@@ -2573,126 +2571,49 @@ class BindingSet extends Annotation {
25732571
* Classes modeling YAML AST nodes.
25742572
*/
25752573
module YAML {
2576-
/** A node in a YAML file */
2577-
class YamlNode extends TYamlNode, AstNode {
2578-
/** Holds if the predicate is a root node (has no parent) */
2579-
predicate isRoot() { not exists(this.getParent()) }
2580-
2581-
override AstNode getParent() { toGenerateYaml(result) = toGenerateYaml(this).getParent() }
2582-
}
2574+
private import codeql.yaml.Yaml as LibYaml
25832575

2584-
/** DEPRECATED: Alias for YamlNode */
2585-
deprecated class YAMLNode = YamlNode;
2576+
private module YamlSig implements LibYaml::InputSig {
2577+
import codeql.Locations
25862578

2587-
/** A YAML comment. */
2588-
class YamlComment extends TYamlComment, YamlNode {
2589-
Yaml::Comment yamlcomment;
2579+
class LocatableBase extends @yaml_locatable {
2580+
Location getLocation() { yaml_locations(this, result) }
25902581

2591-
YamlComment() { this = TYamlComment(yamlcomment) }
2592-
2593-
override string getAPrimaryQlClass() { result = "YamlComment" }
2594-
}
2582+
string toString() { none() }
2583+
}
25952584

2596-
/** DEPRECATED: Alias for YamlComment */
2597-
deprecated class YAMLComment = YamlComment;
2585+
class NodeBase extends LocatableBase, @yaml_node {
2586+
NodeBase getChildNode(int i) { yaml(result, _, this, i, _, _) }
25982587

2599-
/** A YAML entry. */
2600-
class YamlEntry extends TYamlEntry, YamlNode {
2601-
Yaml::Entry yamle;
2588+
string getTag() { yaml(this, _, _, _, result, _) }
26022589

2603-
YamlEntry() { this = TYamlEntry(yamle) }
2590+
string getAnchor() { yaml_anchors(this, result) }
26042591

2605-
/** Gets the key of this YAML entry. */
2606-
YamlKey getKey() {
2607-
exists(Yaml::Keyvaluepair pair |
2608-
pair.getParent() = yamle and
2609-
result = TYamlKey(pair.getKey())
2610-
)
2592+
override string toString() { yaml(this, _, _, _, _, result) }
26112593
}
26122594

2613-
YamlListItem getListItem() { toGenerateYaml(result).getParent() = yamle }
2595+
class ScalarNodeBase extends NodeBase, @yaml_scalar_node {
2596+
int getStyle() { yaml_scalars(this, result, _) }
26142597

2615-
/** Gets the value of this YAML entry. */
2616-
YamlValue getValue() {
2617-
exists(Yaml::Keyvaluepair pair |
2618-
pair.getParent() = yamle and
2619-
result = TYamlValue(pair.getValue())
2620-
)
2598+
string getValue() { yaml_scalars(this, _, result) }
26212599
}
26222600

2623-
override string getAPrimaryQlClass() { result = "YamlEntry" }
2624-
}
2625-
2626-
/** DEPRECATED: Alias for YamlEntry */
2627-
deprecated class YAMLEntry = YamlEntry;
2628-
2629-
/** A YAML key. */
2630-
class YamlKey extends TYamlKey, YamlNode {
2631-
Yaml::Key yamlkey;
2601+
class CollectionNodeBase extends NodeBase, @yaml_collection_node { }
26322602

2633-
YamlKey() { this = TYamlKey(yamlkey) }
2603+
class MappingNodeBase extends CollectionNodeBase, @yaml_mapping_node { }
26342604

2635-
/**
2636-
* Gets the value of this YAML key.
2637-
*/
2638-
YamlValue getValue() {
2639-
exists(Yaml::Keyvaluepair pair |
2640-
pair.getKey() = yamlkey and result = TYamlValue(pair.getValue())
2641-
)
2642-
}
2643-
2644-
override string getAPrimaryQlClass() { result = "YamlKey" }
2605+
class SequenceNodeBase extends CollectionNodeBase, @yaml_sequence_node { }
26452606

2646-
/** Gets the value of this YAML value. */
2647-
string getNamePart(int i) {
2648-
i = 0 and result = yamlkey.getChild(0).(Yaml::SimpleId).getValue()
2649-
or
2650-
exists(YamlKey child |
2651-
child = TYamlKey(yamlkey.getChild(1)) and
2652-
result = child.getNamePart(i - 1)
2653-
)
2607+
class AliasNodeBase extends NodeBase, @yaml_alias_node {
2608+
string getTarget() { yaml_aliases(this, result) }
26542609
}
26552610

2656-
/**
2657-
* Gets all the name parts of this YAML key concatenated with `/`.
2658-
* Dashes are replaced with `/` (because we don't have that information in the generated AST).
2659-
*/
2660-
string getQualifiedName() {
2661-
result = concat(string part, int i | part = this.getNamePart(i) | part, "/" order by i)
2611+
class ParseErrorBase extends LocatableBase, @yaml_error {
2612+
string getMessage() { yaml_errors(this, result) }
26622613
}
26632614
}
26642615

2665-
/** DEPRECATED: Alias for YamlKey */
2666-
deprecated class YAMLKey = YamlKey;
2667-
2668-
/** A YAML list item. */
2669-
class YamlListItem extends TYamlListitem, YamlNode {
2670-
Yaml::Listitem yamllistitem;
2671-
2672-
YamlListItem() { this = TYamlListitem(yamllistitem) }
2673-
2674-
/**
2675-
* Gets the value of this YAML list item.
2676-
*/
2677-
YamlValue getValue() { result = TYamlValue(yamllistitem.getChild()) }
2678-
2679-
override string getAPrimaryQlClass() { result = "YamlListItem" }
2680-
}
2681-
2682-
/** DEPRECATED: Alias for YamlListItem */
2683-
deprecated class YAMLListItem = YamlListItem;
2684-
2685-
/** A YAML value. */
2686-
class YamlValue extends TYamlValue, YamlNode {
2687-
Yaml::Value yamlvalue;
2688-
2689-
YamlValue() { this = TYamlValue(yamlvalue) }
2690-
2691-
override string getAPrimaryQlClass() { result = "YamlValue" }
2692-
2693-
/** Gets the value of this YAML value. */
2694-
string getValue() { result = yamlvalue.getValue() }
2695-
}
2616+
import LibYaml::Make<YamlSig>
26962617

26972618
// to not expose the entire `File` API on `QlPack`.
26982619
private newtype TQLPack = MKQlPack(File file) { file.getBaseName() = "qlpack.yml" }
@@ -2705,15 +2626,16 @@ module YAML {
27052626

27062627
QLPack() { this = MKQlPack(file) }
27072628

2708-
private string getProperty(string name) {
2709-
exists(YamlEntry entry |
2710-
entry.isRoot() and
2711-
entry.getKey().getQualifiedName() = name and
2712-
result = entry.getValue().getValue().trim() and
2713-
entry.getLocation().getFile() = file
2629+
private YamlValue get(string name) {
2630+
exists(YamlMapping m |
2631+
m instanceof YamlDocument and
2632+
m.getFile() = file and
2633+
result = m.lookup(name)
27142634
)
27152635
}
27162636

2637+
private string getProperty(string name) { result = this.get(name).(YamlScalar).getValue() }
2638+
27172639
/** Gets the name of this qlpack */
27182640
string getName() { result = this.getProperty("name") }
27192641

@@ -2728,32 +2650,12 @@ module YAML {
27282650
/** Gets the file that this `QLPack` represents. */
27292651
File getFile() { result = file }
27302652

2731-
private predicate isADependency(YamlEntry entry) {
2732-
exists(YamlEntry deps |
2733-
deps.getLocation().getFile() = file and entry.getLocation().getFile() = file
2734-
|
2735-
deps.isRoot() and
2736-
deps.getKey().getQualifiedName() = ["dependencies", "libraryPathDependencies"] and
2737-
entry.getLocation().getStartLine() = 1 + deps.getLocation().getStartLine() and
2738-
entry.getLocation().getStartColumn() > deps.getLocation().getStartColumn()
2739-
)
2740-
or
2741-
exists(YamlEntry prev | this.isADependency(prev) |
2742-
prev.getLocation().getFile() = file and
2743-
entry.getLocation().getFile() = file and
2744-
entry.getLocation().getStartLine() = 1 + prev.getLocation().getStartLine() and
2745-
entry.getLocation().getStartColumn() = prev.getLocation().getStartColumn()
2746-
)
2747-
}
2748-
27492653
predicate hasDependency(string name, string version) {
2750-
exists(YamlEntry entry | this.isADependency(entry) |
2751-
entry.getKey().getQualifiedName().trim() = name and
2752-
entry.getValue().getValue() = version
2753-
or
2754-
name = entry.getListItem().getValue().getValue().trim() and
2755-
version = "\"*\""
2756-
)
2654+
version = this.get("dependencies").(YamlMapping).lookup(name).(YamlScalar).getValue()
2655+
or
2656+
name =
2657+
this.get("libraryPathDependencies").(YamlCollection).getAChild().(YamlScalar).getValue() and
2658+
version = "\"*\""
27572659
or
27582660
name = this.getProperty("libraryPathDependencies") and
27592661
version = "\"*\""

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,6 @@ newtype TAstNode =
6161
TPredicateExpr(QL::PredicateExpr pe) or
6262
TAnnotation(QL::Annotation annot) or
6363
TAnnotationArg(QL::AnnotArg arg) or
64-
TYamlComment(Yaml::Comment yc) or
65-
TYamlEntry(Yaml::Entry ye) or
66-
TYamlKey(Yaml::Key yk) or
67-
TYamlListitem(Yaml::Listitem yli) or
68-
TYamlValue(Yaml::Value yv) or
6964
TBuiltinClassless(string ret, string name, string args) { isBuiltinClassless(ret, name, args) } or
7065
TBuiltinMember(string qual, string ret, string name, string args) {
7166
isBuiltinMember(qual, ret, name, args)
@@ -87,15 +82,10 @@ class TCall = TPredicateCall or TMemberCall or TNoneCall or TAnyCall;
8782

8883
class TTypeRef = TImport or TModuleExpr or TType;
8984

90-
class TYamlNode = TYamlComment or TYamlEntry or TYamlKey or TYamlListitem or TYamlValue;
91-
9285
class TSignatureExpr = TPredicateExpr or TType or TModuleExpr;
9386

9487
class TComment = TQLDoc or TBlockComment or TLineComment;
9588

96-
/** DEPRECATED: Alias for TYamlNode */
97-
deprecated class TYAMLNode = TYamlNode;
98-
9989
private QL::AstNode toQLFormula(AST::AstNode n) {
10090
n = TConjunction(result) or
10191
n = TDisjunction(result) or
@@ -125,14 +115,6 @@ private QL::AstNode toQLExpr(AST::AstNode n) {
125115
n = TDontCare(result)
126116
}
127117

128-
Yaml::AstNode toGenerateYaml(AST::AstNode n) {
129-
n = TYamlComment(result) or
130-
n = TYamlEntry(result) or
131-
n = TYamlKey(result) or
132-
n = TYamlListitem(result) or
133-
n = TYamlValue(result)
134-
}
135-
136118
Dbscheme::AstNode toDbscheme(AST::AstNode n) { n = TDBRelation(result) }
137119

138120
/**

0 commit comments

Comments
 (0)