Simplify jump-to-def query

The expected output format is a tuple (a, b, k) where `a` and `b` are any
`AstNode` subclass and `k` is a string indicating the kind of
definition (e.g. variable, method, ...).

By ensuring that every value in `DefLoc` is a subclass of `Expr` (itself
a subclass of `AstNode`) we can simplify the query by removing all the
use of `getLocation()`.
This commit is contained in:
Harry Maclean
2021-08-04 17:56:59 +01:00
parent 19e135fb6f
commit 95e2b8a4a4
2 changed files with 22 additions and 34 deletions

View File

@@ -15,34 +15,18 @@ import ruby
import codeql_ruby.ast.internal.Module
import codeql_ruby.dataflow.SSA
from DefLoc loc, Location src, Location target, string kind
from DefLoc loc, Expr src, Expr target, string kind
where
(
exists(ConstantReadAccess read, ConstantWriteAccess write | ConstantDefLoc(read, write) = loc |
src = read.getLocation() and
target = write.getLocation() and
kind = "constant"
)
or
exists(MethodCall call, Method meth | LocalMethodLoc(call, meth) = loc |
src = call.getLocation() and
target = meth.getLocation() and
kind = "method"
)
or
exists(VariableReadAccess read, Ssa::WriteDefinition write |
LocalVariableLoc(read, write) = loc
|
src = read.getLocation() and
target = write.getLocation() and
kind = "variable"
)
)
ConstantDefLoc(src, target) = loc and kind = "constant"
or
LocalMethodLoc(src, target) = loc and kind = "method"
or
LocalVariableLoc(src, target) = loc and kind = "variable"
select src, target, kind
/**
* Definition location info for different identifiers.
* Each branch holds two values that have a `getLocation()` predicate.
* Each branch holds two values that are subclasses of `Expr`.
* The first is the "source" - some usage of an identifier.
* The second is the "target" - the definition of that identifier.
*/
@@ -55,8 +39,12 @@ newtype DefLoc =
call.getReceiver() instanceof Self
} or
/** A local variable. */
LocalVariableLoc(VariableReadAccess read, Ssa::WriteDefinition write) {
read = write.getARead().getExpr() and not read.getLocation() = write.getLocation()
LocalVariableLoc(VariableReadAccess read, VariableWriteAccess write) {
exists(Ssa::WriteDefinition w |
write = w.getWriteAccess() and
read = w.getARead().getExpr() and
not read.getLocation() = write.getLocation()
)
}
/**
@@ -72,7 +60,7 @@ newtype DefLoc =
* end
* ```
*
* the constant `Bar` has the fully qualified name `Foo::Bar::Baz`.
* the constant `Baz` has the fully qualified name `Foo::Bar::Baz`.
*/
string constantQualifiedName(ConstantWriteAccess w) {
not exists(ConstantWriteAccess w2 | w2.getAChild() = w) and result = w.getName()

View File

@@ -1,8 +1,8 @@
| Definitions.rb:4:7:4:9 | Definitions.rb@4:7:4:9 | Definitions.rb:7:5:9:7 | Definitions.rb@7:5:9:7 | method |
| Definitions.rb:8:7:8:7 | Definitions.rb@8:7:8:7 | Definitions.rb:7:11:7:11 | Definitions.rb@7:11:7:11 | variable |
| Definitions.rb:12:7:12:7 | Definitions.rb@12:7:12:7 | Definitions.rb:3:5:5:7 | Definitions.rb@3:5:5:7 | method |
| Definitions.rb:20:7:20:7 | Definitions.rb@20:7:20:7 | Definitions.rb:1:1:15:3 | Definitions.rb@1:1:15:3 | constant |
| Definitions.rb:20:7:20:10 | Definitions.rb@20:7:20:10 | Definitions.rb:2:3:14:5 | Definitions.rb@2:3:14:5 | constant |
| Definitions.rb:20:18:20:18 | Definitions.rb@20:18:20:18 | Definitions.rb:19:11:19:11 | Definitions.rb@19:11:19:11 | variable |
| Definitions.rb:26:1:26:1 | Definitions.rb@26:1:26:1 | Definitions.rb:17:1:24:3 | Definitions.rb@17:1:24:3 | constant |
| Definitions.rb:26:1:26:4 | Definitions.rb@26:1:26:4 | Definitions.rb:18:3:23:5 | Definitions.rb@18:3:23:5 | constant |
| Definitions.rb:4:7:4:9 | call to g | Definitions.rb:7:5:9:7 | g | method |
| Definitions.rb:8:7:8:7 | x | Definitions.rb:7:11:7:11 | x | variable |
| Definitions.rb:12:7:12:7 | call to f | Definitions.rb:3:5:5:7 | f | method |
| Definitions.rb:20:7:20:7 | A | Definitions.rb:1:1:15:3 | A | constant |
| Definitions.rb:20:7:20:10 | B | Definitions.rb:2:3:14:5 | B | constant |
| Definitions.rb:20:18:20:18 | y | Definitions.rb:19:11:19:11 | y | variable |
| Definitions.rb:26:1:26:1 | C | Definitions.rb:17:1:24:3 | C | constant |
| Definitions.rb:26:1:26:4 | D | Definitions.rb:18:3:23:5 | D | constant |