From e93b44670fba860c4e7bceea19d72a4c693a4d71 Mon Sep 17 00:00:00 2001 From: Arthur Baars Date: Wed, 31 May 2023 16:08:01 +0200 Subject: [PATCH] Ruby: printCfg: only show graph for selected CfgScope --- .../ql/lib/ide-contextual-queries/printCfg.ql | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/ruby/ql/lib/ide-contextual-queries/printCfg.ql b/ruby/ql/lib/ide-contextual-queries/printCfg.ql index 9c42fe91361..108d5a0bceb 100644 --- a/ruby/ql/lib/ide-contextual-queries/printCfg.ql +++ b/ruby/ql/lib/ide-contextual-queries/printCfg.ql @@ -9,14 +9,45 @@ private import codeql.ruby.controlflow.internal.ControlFlowGraphImplShared::TestOutput private import codeql.IDEContextual +private import codeql.Locations +private import codeql.ruby.controlflow.ControlFlowGraph /** * Gets the source file to generate a CFG from. */ external string selectedSourceFile(); +external string selectedSourceLine(); + +external string selectedSourceColumn(); + +bindingset[file, line, column] +private CfgScope smallestEnclosingScope(File file, int line, int column) { + result = + min(Location loc, CfgScope scope | + loc = scope.getLocation() and + ( + loc.getStartLine() < line + or + loc.getStartLine() = line and loc.getStartColumn() <= column + ) and + ( + loc.getEndLine() > line + or + loc.getEndLine() = line and loc.getEndColumn() >= column + ) and + loc.getFile() = file + | + scope + order by + loc.getStartLine() desc, loc.getStartColumn() desc, loc.getEndLine(), loc.getEndColumn() + ) +} + class MyRelevantNode extends RelevantNode { MyRelevantNode() { - this.getScope().getLocation().getFile() = getFileBySourceArchiveName(selectedSourceFile()) + this.getScope() = + smallestEnclosingScope(getFileBySourceArchiveName(selectedSourceFile()), + selectedSourceLine().toInt(), selectedSourceColumn().toInt()) } }