Skip to content

Commit abf015b

Browse files
authored
Merge pull request #15485 from github/koesie10/ruby-model-only-relevant-types
Ruby: Only model relevant files for type models
2 parents 73f96fb + 817fd8c commit abf015b

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

ruby/ql/src/queries/modeling/internal/Types.qll

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ module Types {
4848
// class Type2 < Type1
4949
// class Type2; include Type1
5050
exists(Module m1, Module m2 |
51-
m2.getAnImmediateAncestor() = m1 and not m2.isBuiltin() and not m1.isBuiltin()
51+
m2.getAnImmediateAncestor() = m1 and
52+
not m2.isBuiltin() and
53+
not m1.isBuiltin() and
54+
m1.getLocation().getFile() instanceof Util::RelevantFile and
55+
m2.getLocation().getFile() instanceof Util::RelevantFile
5256
|
5357
m1.getQualifiedName() = type1 and m2.getQualifiedName() = type2 and path = ""
5458
)

ruby/ql/src/queries/modeling/internal/Util.qll

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,23 @@
55
private import ruby
66
private import codeql.ruby.ApiGraphs
77

8+
/**
9+
* A file that probably contains tests.
10+
*/
11+
class TestFile extends File {
12+
TestFile() {
13+
this.getRelativePath().regexpMatch(".*(test|spec|examples).+") and
14+
not this.getAbsolutePath().matches("%/ql/test/%") // allows our test cases to work
15+
}
16+
}
17+
818
/**
919
* A file that is relevant in the context of library modeling.
1020
*
1121
* In practice, this means a file that is not part of test code.
1222
*/
1323
class RelevantFile extends File {
14-
RelevantFile() { not this.getRelativePath().regexpMatch(".*/?test(case)?s?/.*") }
24+
RelevantFile() { not this instanceof TestFile }
1525
}
1626

1727
/**

ruby/ql/src/utils/modeleditor/ModelEditor.qll

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ private import codeql.ruby.frameworks.data.ModelsAsData
99
private import codeql.ruby.frameworks.data.internal.ApiGraphModelsExtensions
1010
private import queries.modeling.internal.Util as Util
1111

12-
/** Holds if the given callable is not worth supporting. */
13-
private predicate isUninteresting(DataFlow::MethodNode c) {
14-
c.getLocation().getFile() instanceof TestFile
15-
}
16-
1712
private predicate gemFileStep(Gem::GemSpec gem, Folder folder, int n) {
1813
n = 0 and folder.getAFile() = gem.(File)
1914
or
@@ -63,7 +58,7 @@ abstract class Endpoint instanceof DataFlow::Node {
6358
class MethodEndpoint extends Endpoint instanceof DataFlow::MethodNode {
6459
MethodEndpoint() {
6560
this.isPublic() and
66-
not isUninteresting(this)
61+
this.(DataFlow::MethodNode).getLocation().getFile() instanceof Util::RelevantFile
6762
}
6863

6964
DataFlow::MethodNode getNode() { result = this }
@@ -144,19 +139,12 @@ class MethodEndpoint extends Endpoint instanceof DataFlow::MethodNode {
144139
}
145140

146141
string methodClassification(Call method) {
147-
method.getFile() instanceof TestFile and result = "test"
142+
method.getFile() instanceof Util::TestFile and result = "test"
148143
or
149-
not method.getFile() instanceof TestFile and
144+
not method.getFile() instanceof Util::TestFile and
150145
result = "source"
151146
}
152147

153-
class TestFile extends File {
154-
TestFile() {
155-
this.getRelativePath().regexpMatch(".*(test|spec|examples).+") and
156-
not this.getAbsolutePath().matches("%/ql/test/%") // allows our test cases to work
157-
}
158-
}
159-
160148
/**
161149
* A callable where there exists a MaD sink model that applies to it.
162150
*/
@@ -222,7 +210,7 @@ class ModuleEndpoint extends Endpoint {
222210
n order by loc.getFile().getAbsolutePath(), loc.getStartLine(), loc.getStartColumn()
223211
) and
224212
not moduleNode.(Module).isBuiltin() and
225-
not moduleNode.getLocation().getFile() instanceof TestFile
213+
moduleNode.getLocation().getFile() instanceof Util::RelevantFile
226214
}
227215

228216
DataFlow::ModuleNode getNode() { result = moduleNode }

0 commit comments

Comments
 (0)