|
| 1 | +/** |
| 2 | + * @name Definitions |
| 3 | + * @description Jump to definition helper query. |
| 4 | + * @kind definitions |
| 5 | + * @id rb/jump-to-definition |
| 6 | + */ |
| 7 | + |
| 8 | +/* |
| 9 | + * TODO: |
| 10 | + * - should `Foo.new` point to `Foo#initialize`? |
| 11 | + */ |
| 12 | + |
| 13 | +import ruby |
| 14 | +import codeql.ruby.ast.internal.Module |
| 15 | +import codeql.ruby.dataflow.SSA |
| 16 | +import codeql.ruby.dataflow.internal.DataFlowDispatch |
| 17 | + |
| 18 | +from DefLoc loc, Expr src, Expr target, string kind |
| 19 | +where |
| 20 | + ConstantDefLoc(src, target) = loc and kind = "constant" |
| 21 | + or |
| 22 | + MethodLoc(src, target) = loc and kind = "method" |
| 23 | + or |
| 24 | + LocalVariableLoc(src, target) = loc and kind = "variable" |
| 25 | + or |
| 26 | + InstanceVariableLoc(src, target) = loc and kind = "instance variable" |
| 27 | + or |
| 28 | + ClassVariableLoc(src, target) = loc and kind = "class variable" |
| 29 | +select src, target, kind |
| 30 | + |
| 31 | +/** |
| 32 | + * Definition location info for different identifiers. |
| 33 | + * Each branch holds two values that are subclasses of `Expr`. |
| 34 | + * The first is the "source" - some usage of an identifier. |
| 35 | + * The second is the "target" - the definition of that identifier. |
| 36 | + */ |
| 37 | +newtype DefLoc = |
| 38 | + /** A constant, module or class. */ |
| 39 | + ConstantDefLoc(ConstantReadAccess read, ConstantWriteAccess write) { write = definitionOf(read) } or |
| 40 | + /** A method call. */ |
| 41 | + MethodLoc(MethodCall call, Method meth) { |
| 42 | + exists(DataFlowCall c | c.getExpr() = call and c.getTarget() = meth) |
| 43 | + } or |
| 44 | + /** A local variable. */ |
| 45 | + LocalVariableLoc(VariableReadAccess read, VariableWriteAccess write) { |
| 46 | + exists(Ssa::WriteDefinition w | |
| 47 | + write = w.getWriteAccess() and |
| 48 | + read = w.getARead().getExpr() and |
| 49 | + not read.isSynthesized() |
| 50 | + ) |
| 51 | + } or |
| 52 | + /** An instance variable */ |
| 53 | + InstanceVariableLoc(InstanceVariableReadAccess read, InstanceVariableWriteAccess write) { |
| 54 | + /* |
| 55 | + * We consider instance variables to be "defined" in the initialize method of their enclosing class. |
| 56 | + * If that method doesn't exist, we won't provide any jump-to-def information for the instance variable. |
| 57 | + */ |
| 58 | + |
| 59 | + exists(Method m | |
| 60 | + m.getAChild+() = write and |
| 61 | + m.getName() = "initialize" and |
| 62 | + write.getVariable() = read.getVariable() |
| 63 | + ) |
| 64 | + } or |
| 65 | + /** A class variable */ |
| 66 | + ClassVariableLoc(ClassVariableReadAccess read, ClassVariableWriteAccess write) { |
| 67 | + read.getVariable() = write.getVariable() and |
| 68 | + not exists(MethodBase m | m.getAChild+() = write) |
| 69 | + } |
| 70 | + |
| 71 | +/** |
| 72 | + * Gets the fully qualified name for a constant, based on the context in which it is defined. |
| 73 | + * |
| 74 | + * For example, given |
| 75 | + * ```ruby |
| 76 | + * module Foo |
| 77 | + * module Bar |
| 78 | + * class Baz |
| 79 | + * end |
| 80 | + * end |
| 81 | + * end |
| 82 | + * ``` |
| 83 | + * |
| 84 | + * the constant `Baz` has the fully qualified name `Foo::Bar::Baz`. |
| 85 | + */ |
| 86 | +string constantQualifiedName(ConstantWriteAccess w) { |
| 87 | + /* get the qualified name for the parent module, then append w */ |
| 88 | + exists(ConstantWriteAccess parent | parent = w.getEnclosingModule() | |
| 89 | + result = constantQualifiedName(parent) + "::" + w.getName() |
| 90 | + ) |
| 91 | + or |
| 92 | + /* base case - there's no parent module */ |
| 93 | + not exists(ConstantWriteAccess parent | parent = w.getEnclosingModule()) and |
| 94 | + result = w.getName() |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Gets the constant write that defines the given constant. |
| 99 | + * Modules often don't have a unique definition, as they are opened multiple times in different |
| 100 | + * files. In these cases we arbitrarily pick the definition with the lexicographically least |
| 101 | + * location. |
| 102 | + */ |
| 103 | +ConstantWriteAccess definitionOf(ConstantReadAccess r) { |
| 104 | + result = |
| 105 | + min(ConstantWriteAccess w | |
| 106 | + constantQualifiedName(w) = resolveConstant(r) |
| 107 | + | |
| 108 | + w order by w.getLocation().toString() |
| 109 | + ) |
| 110 | +} |
0 commit comments