Merge pull request #20275 from hvitved/python/fix-print-ast-yaml

Python: Only include relevant YAML in `printAst.ql`
This commit is contained in:
Tom Hvitved
2025-08-25 14:22:14 +02:00
committed by GitHub
2 changed files with 20 additions and 2 deletions

View File

@@ -25,4 +25,9 @@ class PrintAstConfigurationOverride extends PrintAstConfiguration {
super.shouldPrint(e, l) and super.shouldPrint(e, l) and
l.getFile() = getFileBySourceArchiveName(selectedSourceFile()) l.getFile() = getFileBySourceArchiveName(selectedSourceFile())
} }
override predicate shouldPrintYaml(YamlNode y, Location l) {
super.shouldPrintYaml(y, l) and
l.getFile() = getFileBySourceArchiveName(selectedSourceFile())
}
} }

View File

@@ -26,12 +26,22 @@ class PrintAstConfiguration extends TPrintAstConfiguration {
* By default it checks whether the `AstNode` `e` belongs to `Location` `l`. * By default it checks whether the `AstNode` `e` belongs to `Location` `l`.
*/ */
predicate shouldPrint(AstNode e, Location l) { l = e.getLocation() } predicate shouldPrint(AstNode e, Location l) { l = e.getLocation() }
/**
* Controls whether the `YamlNode` should be considered for AST printing.
* By default it checks whether the `YamlNode` `y` belongs to `Location` `l`.
*/
predicate shouldPrintYaml(YamlNode y, Location l) { l = y.getLocation() }
} }
private predicate shouldPrint(AstNode e, Location l) { private predicate shouldPrint(AstNode e, Location l) {
exists(PrintAstConfiguration config | config.shouldPrint(e, l)) exists(PrintAstConfiguration config | config.shouldPrint(e, l))
} }
private predicate shouldPrintYaml(YamlNode y, Location l) {
exists(PrintAstConfiguration config | config.shouldPrintYaml(y, l))
}
/** Holds if the given element does not need to be rendered in the AST. */ /** Holds if the given element does not need to be rendered in the AST. */
private predicate isNotNeeded(AstNode el) { private predicate isNotNeeded(AstNode el) {
el.isArtificial() el.isArtificial()
@@ -55,8 +65,11 @@ private newtype TPrintAstNode =
not list = any(Module mod).getBody() and not list = any(Module mod).getBody() and
not forall(AstNode child | child = list.getAnItem() | isNotNeeded(child)) not forall(AstNode child | child = list.getAnItem() | isNotNeeded(child))
} or } or
TYamlNode(YamlNode node) or TYamlNode(YamlNode node) { shouldPrintYaml(node, _) } or
TYamlMappingNode(YamlMapping mapping, int i) { exists(mapping.getKeyNode(i)) } TYamlMappingNode(YamlMapping mapping, int i) {
shouldPrintYaml(mapping, _) and
exists(mapping.getKeyNode(i))
}
/** /**
* A node in the output tree. * A node in the output tree.