Skip to content

Commit 88059d9

Browse files
authored
Merge pull request #20275 from hvitved/python/fix-print-ast-yaml
Python: Only include relevant YAML in `printAst.ql`
2 parents 4be995d + bf7e3da commit 88059d9

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

python/ql/lib/printAst.ql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,9 @@ class PrintAstConfigurationOverride extends PrintAstConfiguration {
2525
super.shouldPrint(e, l) and
2626
l.getFile() = getFileBySourceArchiveName(selectedSourceFile())
2727
}
28+
29+
override predicate shouldPrintYaml(YamlNode y, Location l) {
30+
super.shouldPrintYaml(y, l) and
31+
l.getFile() = getFileBySourceArchiveName(selectedSourceFile())
32+
}
2833
}

python/ql/lib/semmle/python/PrintAst.qll

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,22 @@ class PrintAstConfiguration extends TPrintAstConfiguration {
2626
* By default it checks whether the `AstNode` `e` belongs to `Location` `l`.
2727
*/
2828
predicate shouldPrint(AstNode e, Location l) { l = e.getLocation() }
29+
30+
/**
31+
* Controls whether the `YamlNode` should be considered for AST printing.
32+
* By default it checks whether the `YamlNode` `y` belongs to `Location` `l`.
33+
*/
34+
predicate shouldPrintYaml(YamlNode y, Location l) { l = y.getLocation() }
2935
}
3036

3137
private predicate shouldPrint(AstNode e, Location l) {
3238
exists(PrintAstConfiguration config | config.shouldPrint(e, l))
3339
}
3440

41+
private predicate shouldPrintYaml(YamlNode y, Location l) {
42+
exists(PrintAstConfiguration config | config.shouldPrintYaml(y, l))
43+
}
44+
3545
/** Holds if the given element does not need to be rendered in the AST. */
3646
private predicate isNotNeeded(AstNode el) {
3747
el.isArtificial()
@@ -55,8 +65,11 @@ private newtype TPrintAstNode =
5565
not list = any(Module mod).getBody() and
5666
not forall(AstNode child | child = list.getAnItem() | isNotNeeded(child))
5767
} or
58-
TYamlNode(YamlNode node) or
59-
TYamlMappingNode(YamlMapping mapping, int i) { exists(mapping.getKeyNode(i)) }
68+
TYamlNode(YamlNode node) { shouldPrintYaml(node, _) } or
69+
TYamlMappingNode(YamlMapping mapping, int i) {
70+
shouldPrintYaml(mapping, _) and
71+
exists(mapping.getKeyNode(i))
72+
}
6073

6174
/**
6275
* A node in the output tree.

0 commit comments

Comments
 (0)