Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6b6aeb1

Browse files
committed
Improve performance of internal/Module.qll
1 parent 4798a1a commit 6b6aeb1

3 files changed

Lines changed: 46 additions & 19 deletions

File tree

ql/src/codeql_ruby/ast/internal/Module.qll

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ private ModuleBase enclosing(ModuleBase m, int level) {
143143
result = enclosing(m.getEnclosingModule(), level - 1)
144144
}
145145

146+
pragma[noinline]
147+
private Namespace enclosingNameSpaceConstantReadAccess(
148+
ConstantReadAccess c, int priority, string name
149+
) {
150+
result = enclosing(c.getEnclosingModule(), priority) and
151+
name = c.getName()
152+
}
153+
146154
/**
147155
* Resolve constant read access (typically a scope expression) to a qualified name. The
148156
* `priority` value indicates the precedence of the solution with respect to the lookup order.
@@ -154,26 +162,35 @@ private ModuleBase enclosing(ModuleBase m, int level) {
154162
private string resolveScopeExpr(ConstantReadAccess c, int priority) {
155163
c.hasGlobalScope() and result = c.getName() and priority = 0
156164
or
157-
result = qualifiedModuleName(resolveScopeExpr(c.getScopeExpr(), priority), c.getName())
165+
exists(string name |
166+
result = qualifiedModuleName(resolveScopeExprConstantReadAccess(c, priority, name), name)
167+
)
158168
or
159169
not exists(c.getScopeExpr()) and
160170
not c.hasGlobalScope() and
161171
(
162-
exists(Namespace n |
163-
result = qualifiedModuleName(constantDefinition0(n), c.getName()) and
164-
n = enclosing(c.getEnclosingModule(), priority)
172+
exists(string name |
173+
exists(Namespace n |
174+
n = enclosingNameSpaceConstantReadAccess(c, priority, name) and
175+
result = qualifiedModuleName(constantDefinition0(n), name)
176+
)
177+
or
178+
result =
179+
qualifiedModuleName(ancestors(qualifiedModuleNameConstantReadAccess(c, name),
180+
priority - maxDepth()), name)
165181
)
166182
or
167-
result =
168-
qualifiedModuleName(ancestors(qualifiedModuleName(c.getEnclosingModule()),
169-
priority - maxDepth()), c.getName())
170-
or
171-
result = c.getName() and
172183
priority = maxDepth() + 4 and
173-
qualifiedModuleName(c.getEnclosingModule()) != "BasicObject"
184+
qualifiedModuleNameConstantReadAccess(c, result) != "BasicObject"
174185
)
175186
}
176187

188+
pragma[noinline]
189+
private string resolveScopeExprConstantReadAccess(ConstantReadAccess c, int priority, string name) {
190+
result = resolveScopeExpr(c.getScopeExpr(), priority) and
191+
name = c.getName()
192+
}
193+
177194
bindingset[qualifier, name]
178195
private string scopeAppend(string qualifier, string name) {
179196
if qualifier = "Object" then result = name else result = qualifier + "::" + name
@@ -185,6 +202,18 @@ private string qualifiedModuleName(ModuleBase m) {
185202
result = constantDefinition0(m)
186203
}
187204

205+
pragma[noinline]
206+
private string qualifiedModuleNameConstantWriteAccess(ConstantWriteAccess c, string name) {
207+
result = qualifiedModuleName(c.getEnclosingModule()) and
208+
name = c.getName()
209+
}
210+
211+
pragma[noinline]
212+
private string qualifiedModuleNameConstantReadAccess(ConstantReadAccess c, string name) {
213+
result = qualifiedModuleName(c.getEnclosingModule()) and
214+
name = c.getName()
215+
}
216+
188217
/**
189218
* Get a qualified name for a constant definition. May return multiple qualified
190219
* names because we over-approximate when resolving scope resolutions and ignore
@@ -198,9 +227,7 @@ private string constantDefinition0(ConstantWriteAccess c) {
198227
or
199228
not exists(c.getScopeExpr()) and
200229
not c.hasGlobalScope() and
201-
exists(ModuleBase enclosing | enclosing = c.getEnclosingModule() |
202-
result = scopeAppend(qualifiedModuleName(enclosing), c.getName())
203-
)
230+
exists(string name | result = scopeAppend(qualifiedModuleNameConstantWriteAccess(c, name), name))
204231
}
205232

206233
/**

ql/test/library-tests/modules/ancestors.expected

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ modules.rb:
6060
# 30| Foo::ClassInAnotherDefinitionOfFoo
6161
#-----| super -> Object
6262

63+
# 116| XX::YY
64+
#-----| super -> YY
65+
6366
# 65| Test::Foo1
6467

6568
# 70| Test::Foo2
@@ -68,9 +71,6 @@ modules.rb:
6871

6972
# 84| Other::Foo1
7073

71-
# 116| XX::YY
72-
#-----| super -> YY
73-
7474
# 6| Foo::Bar::ClassInFooBar
7575
#-----| super -> Object
7676

ql/test/library-tests/modules/superclasses.expected

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ modules.rb:
5555
# 30| Foo::ClassInAnotherDefinitionOfFoo
5656
#-----| -> Object
5757

58+
# 116| XX::YY
59+
#-----| -> YY
60+
5861
# 65| Test::Foo1
5962

6063
# 70| Test::Foo2
@@ -63,9 +66,6 @@ modules.rb:
6366

6467
# 84| Other::Foo1
6568

66-
# 116| XX::YY
67-
#-----| -> YY
68-
6969
# 6| Foo::Bar::ClassInFooBar
7070
#-----| -> Object
7171

0 commit comments

Comments
 (0)