|
| 1 | +/** |
| 2 | + * Provides classes and predicates related to jump-to-definition links |
| 3 | + * in the code viewer. |
| 4 | + */ |
| 5 | + |
| 6 | +private import codeql.rust.elements.Variable |
| 7 | +private import codeql.rust.elements.Locatable |
| 8 | +private import codeql.rust.elements.FormatArgsExpr |
| 9 | +private import codeql.rust.elements.FormatArgsArg |
| 10 | +private import codeql.rust.elements.Format |
| 11 | +private import codeql.rust.elements.MacroCall |
| 12 | +private import codeql.rust.elements.NamedFormatArgument |
| 13 | +private import codeql.rust.elements.PositionalFormatArgument |
| 14 | +private import codeql.Locations |
| 15 | + |
| 16 | +/** An element with an associated definition. */ |
| 17 | +abstract class Use extends Locatable { |
| 18 | + /** Gets the definition associated with this element. */ |
| 19 | + abstract Definition getDefinition(); |
| 20 | + |
| 21 | + /** |
| 22 | + * Gets the type of use. |
| 23 | + */ |
| 24 | + abstract string getUseType(); |
| 25 | +} |
| 26 | + |
| 27 | +cached |
| 28 | +private module Cached { |
| 29 | + cached |
| 30 | + newtype TDef = |
| 31 | + TVariable(Variable v) or |
| 32 | + TFormatArgsArgName(Name name) { name = any(FormatArgsArg a).getName() } or |
| 33 | + TFormatArgsArgIndex(Expr e) { e = any(FormatArgsArg a).getExpr() } |
| 34 | + |
| 35 | + /** |
| 36 | + * Gets an element, of kind `kind`, that element `use` uses, if any. |
| 37 | + */ |
| 38 | + cached |
| 39 | + Definition definitionOf(Use use, string kind) { |
| 40 | + result = use.getDefinition() and |
| 41 | + kind = use.getUseType() and |
| 42 | + not result.getLocation() = any(MacroCall m).getLocation() |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +predicate definitionOf = Cached::definitionOf/2; |
| 47 | + |
| 48 | +/** A definition */ |
| 49 | +class Definition extends Cached::TDef { |
| 50 | + /** Gets the location of this variable. */ |
| 51 | + Location getLocation() { |
| 52 | + result = this.asVariable().getLocation() or |
| 53 | + result = this.asName().getLocation() or |
| 54 | + result = this.asExpr().getLocation() |
| 55 | + } |
| 56 | + |
| 57 | + /** Gets this definition as a `Variable` */ |
| 58 | + Variable asVariable() { this = Cached::TVariable(result) } |
| 59 | + |
| 60 | + /** Gets this definition as a `Name` */ |
| 61 | + Name asName() { this = Cached::TFormatArgsArgName(result) } |
| 62 | + |
| 63 | + /** Gets this definition as an `Expr` */ |
| 64 | + Expr asExpr() { this = Cached::TFormatArgsArgIndex(result) } |
| 65 | + |
| 66 | + /** Gets the string representation of this element. */ |
| 67 | + string toString() { |
| 68 | + result = this.asExpr().toString() or |
| 69 | + result = this.asVariable().toString() or |
| 70 | + result = this.asName().getText() |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +private class LocalVariableUse extends Use instanceof VariableAccess { |
| 75 | + private Variable def; |
| 76 | + |
| 77 | + LocalVariableUse() { this = def.getAnAccess() } |
| 78 | + |
| 79 | + override Definition getDefinition() { result.asVariable() = def } |
| 80 | + |
| 81 | + override string getUseType() { result = "local variable" } |
| 82 | +} |
| 83 | + |
| 84 | +private class NamedFormatArgumentUse extends Use instanceof NamedFormatArgument { |
| 85 | + private Name def; |
| 86 | + |
| 87 | + NamedFormatArgumentUse() { |
| 88 | + exists(FormatArgsExpr parent | |
| 89 | + parent = this.getParent().getParent() and |
| 90 | + parent.getAnArg().getName() = def and |
| 91 | + this.getName() = def.getText() |
| 92 | + ) |
| 93 | + } |
| 94 | + |
| 95 | + override Definition getDefinition() { result.asName() = def } |
| 96 | + |
| 97 | + override string getUseType() { result = "format argument" } |
| 98 | +} |
| 99 | + |
| 100 | +private class PositionalFormatUse extends Use instanceof Format { |
| 101 | + PositionalFormatUse() { not exists(this.getArgumentRef()) } |
| 102 | + |
| 103 | + override Definition getDefinition() { |
| 104 | + exists(FormatArgsExpr parent, int index | parent.getFormat(_) = this | |
| 105 | + this = rank[index + 1](PositionalFormatUse f, int i | parent.getFormat(i) = f | f order by i) and |
| 106 | + result.asExpr() = parent.getArg(index).getExpr() |
| 107 | + ) |
| 108 | + } |
| 109 | + |
| 110 | + override string getUseType() { result = "format argument" } |
| 111 | +} |
| 112 | + |
| 113 | +private class PositionalFormatArgumentUse extends Use instanceof PositionalFormatArgument { |
| 114 | + private Expr def; |
| 115 | + |
| 116 | + PositionalFormatArgumentUse() { |
| 117 | + exists(FormatArgsExpr parent | |
| 118 | + parent = this.getParent().getParent() and |
| 119 | + def = parent.getArg(this.getIndex()).getExpr() |
| 120 | + ) |
| 121 | + } |
| 122 | + |
| 123 | + override Definition getDefinition() { result.asExpr() = def } |
| 124 | + |
| 125 | + override string getUseType() { result = "format argument" } |
| 126 | +} |
0 commit comments