|
9 | 9 | import codeql.actions.Ast |
10 | 10 | private import codeql.actions.Cfg |
11 | 11 | import codeql.actions.controlflow.BasicBlocks |
| 12 | +import ExternalPredicates |
12 | 13 |
|
13 | 14 | /** |
14 | | - * Configuration for PrintCFG that outputs all CFG nodes and edges. |
| 15 | + * Gets a single source file from the comma-separated list. |
| 16 | + */ |
| 17 | +string getSelectedSourceFile() { |
| 18 | + exists(string s | selectedSourceFiles(s) | result = s.splitAt(",").trim()) |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Holds if this CFG node should be included in output. |
| 23 | + * |
| 24 | + * Unfiltered, this query emits every node in the database, which for a real repository is |
| 25 | + * unbounded rather than merely large. Matching is done on the location's own strings because |
| 26 | + * the actions library does not expose a `File` type the way the other languages' libraries do. |
| 27 | + */ |
| 28 | +predicate shouldPrintNode(Node node) { |
| 29 | + exists(string selectedFile | selectedFile = getSelectedSourceFile() | |
| 30 | + // Match by exact relative path from source root |
| 31 | + node.getLocation().getFile().getRelativePath() = selectedFile |
| 32 | + or |
| 33 | + // Match by file name if no path separators |
| 34 | + not selectedFile.matches("%/%") and |
| 35 | + node.getLocation().getFile().getBaseName() = selectedFile |
| 36 | + or |
| 37 | + // Match by ending path component |
| 38 | + exists(string absolute | absolute = node.getLocation().getFile().getAbsolutePath() | |
| 39 | + absolute = selectedFile |
| 40 | + or |
| 41 | + absolute.length() > selectedFile.length() and |
| 42 | + absolute.suffix(absolute.length() - selectedFile.length() - 1) = "/" + selectedFile |
| 43 | + ) |
| 44 | + ) |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Configuration for PrintCFG that outputs filtered CFG nodes and edges. |
15 | 49 | */ |
16 | 50 | query predicate nodes(Node node, string property, string value) { |
| 51 | + shouldPrintNode(node) and |
17 | 52 | property = "semmle.label" and |
18 | 53 | value = node.toString() |
19 | 54 | } |
20 | 55 |
|
21 | | -query predicate edges(Node pred, Node succ) { pred.getASuccessor() = succ } |
| 56 | +query predicate edges(Node pred, Node succ) { |
| 57 | + shouldPrintNode(pred) and |
| 58 | + shouldPrintNode(succ) and |
| 59 | + pred.getASuccessor() = succ |
| 60 | +} |
0 commit comments