Ruby: Make module graph queries avoid relying on evalaution order.

This commit is contained in:
Alex Eyers-Taylor
2025-03-25 16:13:44 +00:00
parent ffb25b7aac
commit 0ccbe3ef7f
4 changed files with 66 additions and 44 deletions

View File

@@ -1,25 +1,21 @@
#-----| BasicObject
#-----| Class
#-----| super -> Module
#-----| EsotericInstanceMethods
#-----| MyStruct
#-----| Struct
#-----| UnresolvedNamespace
#-----| BasicObject
#-----| Complex
#-----| super -> Numeric
#-----| EsotericInstanceMethods
#-----| FalseClass
#-----| super -> Object
#-----| Float
#-----| super -> Numeric
#-----| MyStruct
#-----| NilClass
#-----| super -> Object
@@ -31,11 +27,15 @@
#-----| Rational
#-----| super -> Numeric
#-----| Struct
#-----| Symbol
#-----| TrueClass
#-----| super -> Object
#-----| UnresolvedNamespace
#-----| UnresolvedNamespace::X1
#-----| UnresolvedNamespace::X1::X2

View File

@@ -5,22 +5,33 @@
import codeql.ruby.AST
int locationModuleRank(Module node) {
node =
rank[result](Module m, Location l |
l = m.getLocation()
|
m
order by
l.getFile().getBaseName(), l.getFile().getAbsolutePath(), l.getStartLine(),
l.getStartColumn(), l.getEndLine(), l.getEndColumn(), m.toString()
)
}
int stringModuleRank(Module node) {
node = rank[result](Module m | not exists(locationModuleRank(m)) | m order by m.toString())
}
int moduleRank(Module node) {
result = locationModuleRank(node) + max(stringModuleRank(_))
or
result = stringModuleRank(node)
}
query predicate nodes(Module node, string key, string value) {
key = "semmle.label" and value = node.toString()
or
key = "semmle.order" and
value =
any(int i |
node =
rank[i](Module m, Location l |
l = m.getLocation()
|
m
order by
l.getFile().getBaseName(), l.getFile().getAbsolutePath(), l.getStartLine(),
l.getStartColumn(), l.getEndLine(), l.getEndColumn(), m.toString()
)
).toString()
value = moduleRank(node).toString()
}
Module getATarget(Module source, string value) {

View File

@@ -1,25 +1,21 @@
#-----| BasicObject
#-----| Class
#-----| -> Module
#-----| EsotericInstanceMethods
#-----| MyStruct
#-----| Struct
#-----| UnresolvedNamespace
#-----| BasicObject
#-----| Complex
#-----| -> Numeric
#-----| EsotericInstanceMethods
#-----| FalseClass
#-----| -> Object
#-----| Float
#-----| -> Numeric
#-----| MyStruct
#-----| NilClass
#-----| -> Object
@@ -31,11 +27,15 @@
#-----| Rational
#-----| -> Numeric
#-----| Struct
#-----| Symbol
#-----| TrueClass
#-----| -> Object
#-----| UnresolvedNamespace
#-----| UnresolvedNamespace::X1
#-----| UnresolvedNamespace::X1::X2

View File

@@ -5,22 +5,33 @@
import codeql.ruby.AST
int locationModuleRank(Module node) {
node =
rank[result](Module m, Location l |
l = m.getLocation()
|
m
order by
l.getFile().getBaseName(), l.getFile().getAbsolutePath(), l.getStartLine(),
l.getStartColumn(), l.getEndLine(), l.getEndColumn(), m.toString()
)
}
int stringModuleRank(Module node) {
node = rank[result](Module m | not exists(locationModuleRank(m)) | m order by m.toString())
}
int moduleRank(Module node) {
result = locationModuleRank(node) + max(stringModuleRank(_))
or
result = stringModuleRank(node)
}
query predicate nodes(Module node, string key, string value) {
key = "semmle.label" and value = node.toString()
or
key = "semmle.order" and
value =
any(int i |
node =
rank[i](Module m, Location l |
l = m.getLocation()
|
m
order by
l.getFile().getBaseName(), l.getFile().getAbsolutePath(), l.getStartLine(),
l.getStartColumn(), l.getEndLine(), l.getEndColumn(), m.toString()
)
).toString()
value = moduleRank(node).toString()
}
query predicate edges(Module source, Module target, string key, string value) {