Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions ruby/ql/test/library-tests/modules/ancestors.expected
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -31,11 +27,15 @@
#-----| Rational
#-----| super -> Numeric

#-----| Struct

#-----| Symbol

#-----| TrueClass
#-----| super -> Object

#-----| UnresolvedNamespace

#-----| UnresolvedNamespace::X1

#-----| UnresolvedNamespace::X1::X2
Expand Down
35 changes: 23 additions & 12 deletions ruby/ql/test/library-tests/modules/ancestors.ql
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
20 changes: 10 additions & 10 deletions ruby/ql/test/library-tests/modules/superclasses.expected
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
#-----| BasicObject

#-----| Class
#-----| -> Module

#-----| EsotericInstanceMethods

#-----| MyStruct

#-----| Struct

#-----| UnresolvedNamespace

#-----| BasicObject

#-----| Complex
#-----| -> Numeric

#-----| EsotericInstanceMethods

#-----| FalseClass
#-----| -> Object

#-----| Float
#-----| -> Numeric

#-----| MyStruct

#-----| NilClass
#-----| -> Object

Expand All @@ -31,11 +27,15 @@
#-----| Rational
#-----| -> Numeric

#-----| Struct

#-----| Symbol

#-----| TrueClass
#-----| -> Object

#-----| UnresolvedNamespace

#-----| UnresolvedNamespace::X1

#-----| UnresolvedNamespace::X1::X2
Expand Down
35 changes: 23 additions & 12 deletions ruby/ql/test/library-tests/modules/superclasses.ql
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading