|
| 1 | +/** |
| 2 | + * @name Call Graph From To for rust |
| 3 | + * @description Displays calls on reachable paths from a source function to a target function, showing transitive call graph connectivity. |
| 4 | + * @id rust/tools/call-graph-from-to |
| 5 | + * @kind problem |
| 6 | + * @problem.severity recommendation |
| 7 | + * @tags call-graph |
| 8 | + */ |
| 9 | + |
| 10 | +import rust |
| 11 | +import ExternalPredicates |
| 12 | + |
| 13 | +/** |
| 14 | + * Gets a single source function name from the comma-separated list. |
| 15 | + */ |
| 16 | +string getSourceFunctionName() { |
| 17 | + exists(string s | sourceFunction(s) | result = s.splitAt(",").trim()) |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Gets a single target function name from the comma-separated list. |
| 22 | + */ |
| 23 | +string getTargetFunctionName() { |
| 24 | + exists(string s | targetFunction(s) | result = s.splitAt(",").trim()) |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * Holds if function `caller` directly calls function `callee` by name. |
| 29 | + */ |
| 30 | +predicate calls(Function caller_, Function callee_) { |
| 31 | + exists(CallExpr c | |
| 32 | + c.getEnclosingCallable() = caller_ and |
| 33 | + c.getResolvedTarget().(Function).getName() = callee_.getName() |
| 34 | + ) |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Gets the name of the called function. |
| 39 | + */ |
| 40 | +string getCalleeName(CallExpr call) { |
| 41 | + if exists(call.getResolvedTarget().(Function).getName()) |
| 42 | + then result = call.getResolvedTarget().(Function).getName() |
| 43 | + else result = call.toString() |
| 44 | +} |
| 45 | + |
| 46 | +from CallExpr call, Function caller |
| 47 | +where |
| 48 | + call.getEnclosingCallable() = caller and |
| 49 | + exists(Function source, Function target | |
| 50 | + source.getName() = getSourceFunctionName() and |
| 51 | + target.getName() = getTargetFunctionName() and |
| 52 | + calls*(source, caller) and |
| 53 | + exists(Function callee | |
| 54 | + call.getResolvedTarget().(Function).getName() = callee.getName() and |
| 55 | + calls*(callee, target) |
| 56 | + ) |
| 57 | + ) |
| 58 | +select call, "Reachable call from `" + caller.getName() + "` to `" + getCalleeName(call) + "`" |
0 commit comments